o_sfu_core/engine/room/media_graph/
mod.rs1use std::collections::{BTreeMap, BTreeSet};
2
3use o_sfu_model::UserId;
4use o_sfu_router::{MediaKind, rtp};
5use o_sfu_transport::{
6 SourceActivityRevision, TransportConsumerRoute, TransportMediaId, TransportSourceKey,
7};
8
9use crate::engine::{
10 director::{PendingUpgrade, RouteAllocation},
11 source_model::{PublishedSourceDescriptor, SourceSubscriptionIntent, UserStreamId},
12};
13
14mod consumer_setup;
15mod producer;
16mod route_graph;
17mod source_index;
18mod subscription;
19mod topology;
20
21#[cfg(test)]
22#[expect(non_snake_case, reason = "test modules map to local TESTS directories")]
23mod TESTS;
24
25pub(super) use self::{
26 consumer_setup::{
27 CommittedConsumerSetup, ConsumerSetupOrigin, ConsumerSetupOutcome, ConsumerSetupTarget,
28 DeclaredConsumerSetup, PendingConsumerSetup,
29 },
30 producer::{ProducerActivityCommit, PublishCommit, PublishIntentPlan, ValidatedPublish},
31 subscription::{ReceiverRouteActivity, ReceiverRouteCommit, ReceiverRouteWork},
32 topology::{RoomTopology, SessionPlacementCommit, SessionPlacementRejection},
33};
34
35#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
36pub(in crate::engine) struct SubscriptionKey {
37 pub receiver: UserId,
38 pub publisher: UserId,
39 pub stream: UserStreamId,
40}
41
42impl SubscriptionKey {
43 pub fn new(receiver: &UserId, publisher: &UserId, stream: &UserStreamId) -> Self {
44 Self {
45 receiver: receiver.clone(),
46 publisher: publisher.clone(),
47 stream: stream.clone(),
48 }
49 }
50}
51
52#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
53pub(super) struct SourceKey {
54 owner_user_id: UserId,
55 stream_id: UserStreamId,
56}
57
58#[derive(Debug)]
59pub(in crate::engine) struct PublishedSource {
60 pub descriptor: PublishedSourceDescriptor,
61 pub transport: TransportSourceKey,
62 pub rtp: rtp::MediaStream,
63 pub active: bool,
64 pub activity_revision: SourceActivityRevision,
65}
66
67#[derive(Debug, Clone)]
68pub(in crate::engine) struct ConsumerRouteView<'a> {
69 pub key: &'a SubscriptionKey,
70 pub route: &'a TransportConsumerRoute,
71 pub pending_upgrade: Option<PendingUpgrade>,
72 pub source: &'a PublishedSource,
73 pub intent: SourceSubscriptionIntent,
74 pub allocation: RouteAllocation,
75}
76
77impl ConsumerRouteView<'_> {
78 pub const fn delivery_active(&self) -> bool {
79 self.allocation.delivers(self.intent)
80 }
81
82 pub fn target(&self) -> ConsumerRouteTarget {
83 ConsumerRouteTarget::new(
84 self.route.clone(),
85 self.key.stream.clone(),
86 self.source.descriptor.media_kind(),
87 )
88 }
89}
90
91#[derive(Debug, Clone, Copy)]
92pub(super) struct PendingConsumerRouteView<'a> {
93 pub source: &'a PublishedSource,
94 pub intent: SourceSubscriptionIntent,
95 pub allocation: RouteAllocation,
96}
97
98impl PendingConsumerRouteView<'_> {
99 pub const fn delivery_active(&self) -> bool {
100 self.allocation.delivers(self.intent)
101 }
102}
103
104#[derive(Debug, Clone, PartialEq, Eq)]
105pub struct ConsumerRouteTarget {
106 transport_route: TransportConsumerRoute,
107 stream_id: UserStreamId,
108 kind: MediaKind,
109}
110
111impl ConsumerRouteTarget {
112 fn new(
113 transport_route: TransportConsumerRoute,
114 stream_id: UserStreamId,
115 kind: MediaKind,
116 ) -> Self {
117 Self {
118 transport_route,
119 stream_id,
120 kind,
121 }
122 }
123
124 pub const fn transport_route(&self) -> &TransportConsumerRoute {
125 &self.transport_route
126 }
127
128 pub const fn consumer_media_id(&self) -> TransportMediaId {
129 self.transport_route.consumer_transport_media_id()
130 }
131
132 pub fn producer_user_id(&self) -> &UserId {
133 self.transport_route.source_session_key().user_id()
134 }
135
136 pub const fn source_media_id(&self) -> TransportMediaId {
137 self.transport_route.source_transport_media_id()
138 }
139
140 pub fn stream_id(&self) -> &UserStreamId {
141 &self.stream_id
142 }
143
144 pub fn request_keyframe_after_activity(&self, active: bool) -> bool {
145 active && self.kind == MediaKind::Video
146 }
147}
148
149impl SourceKey {
150 pub fn new(owner_user_id: &UserId, stream_id: &UserStreamId) -> Self {
151 Self {
152 owner_user_id: owner_user_id.clone(),
153 stream_id: stream_id.clone(),
154 }
155 }
156}
157
158fn remove_from_index_set<K, V>(index: &mut BTreeMap<K, BTreeSet<V>>, key: &K, value: &V)
159where
160 K: Ord,
161 V: Ord,
162{
163 let Some(values) = index.get_mut(key) else {
164 return;
165 };
166 values.remove(value);
167 if values.is_empty() {
168 index.remove(key);
169 }
170}