Skip to main content

o_sfu_core/engine/source_model/
ids.rs

1use std::fmt::{self, Display, Formatter};
2
3use o_sfu_model::UserId;
4use serde::{Deserialize, Serialize};
5
6/// User-scoped stream identity supplied with a publish intent.
7///
8/// The room treats this value as an opaque slot name scoped by the publishing
9/// user. Callers allocate the slot before entering core and pass media kind
10/// plus room-policy metadata through the publish intent.
11///
12/// # Invariant
13///
14/// `UserStreamId` is not globally unique. It is unique only together with the
15/// publishing user id. Room indexes that need publication identity must pair it
16/// with the publishing user or use `PublishedSourceId` after a publish
17/// commits.
18#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
19#[serde(transparent)]
20pub struct UserStreamId(String);
21
22impl UserStreamId {
23    #[must_use]
24    pub fn new(value: impl Into<String>) -> Self {
25        Self(value.into())
26    }
27
28    #[must_use]
29    pub fn as_str(&self) -> &str {
30        &self.0
31    }
32}
33
34impl Display for UserStreamId {
35    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
36        formatter.write_str(self.as_str())
37    }
38}
39
40impl From<&str> for UserStreamId {
41    fn from(value: &str) -> Self {
42        Self::new(value)
43    }
44}
45
46impl From<String> for UserStreamId {
47    fn from(value: String) -> Self {
48        Self::new(value)
49    }
50}
51
52/// Stable room-domain identity for one logical published source.
53///
54/// A source id identifies the publication itself, not the SDP media section,
55/// negotiated RID, RTP SSRC or transport media handle currently realizing it.
56/// Room state should allocate it when a publish becomes a room-domain source
57/// and keep using it across renegotiation or transport reattachment.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
59pub struct PublishedSourceId(u64);
60
61impl PublishedSourceId {
62    #[must_use]
63    pub fn allocate(next_source_id: &mut u64) -> Self {
64        let source_id = Self(*next_source_id);
65        *next_source_id = next_source_id.saturating_add(1);
66        source_id
67    }
68
69    #[must_use]
70    pub const fn as_u64(self) -> u64 {
71        self.0
72    }
73}
74
75impl Display for PublishedSourceId {
76    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
77        write!(formatter, "source-{}", self.0)
78    }
79}
80
81/// Stable room-domain identity for one advertised source encoding
82///
83/// The id names an advertised encoding of a source such as a simulcast `lo` or
84/// `hi` layer. It does not encode a RID string or worker-local route so
85/// selectors can keep pointing at the same encoding after transport details are
86/// refreshed.
87#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
88pub struct SourceEncodingId(u64);
89
90impl SourceEncodingId {
91    #[must_use]
92    pub fn allocate(next_encoding_id: &mut u64) -> Self {
93        let encoding_id = Self(*next_encoding_id);
94        *next_encoding_id = next_encoding_id.saturating_add(1);
95        encoding_id
96    }
97
98    #[must_use]
99    pub const fn as_u64(self) -> u64 {
100        self.0
101    }
102}
103
104impl Display for SourceEncodingId {
105    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
106        write!(formatter, "encoding-{}", self.0)
107    }
108}
109
110/// Logical publishing user attached to a source descriptor.
111///
112/// The user identifies the logical owner visible to room policy. Connection
113/// freshness is tracked by producer and transport indexes, because source
114/// descriptors are room-domain metadata rather than async commit guards.
115#[derive(Debug, Clone, PartialEq, Eq)]
116pub struct PublishedSourceOwner {
117    user_id: UserId,
118}
119
120impl PublishedSourceOwner {
121    #[must_use]
122    pub fn new(user_id: UserId) -> Self {
123        Self { user_id }
124    }
125
126    #[must_use]
127    pub fn user_id(&self) -> &UserId {
128        &self.user_id
129    }
130}
131
132#[cfg(test)]
133#[path = "TESTS/ids_support.rs"]
134mod ids_support;