o_sfu_core/engine/room/media_graph/
source_index.rs1use std::collections::{BTreeMap, BTreeSet};
2
3use o_sfu_model::UserId;
4use o_sfu_transport::TransportMediaId;
5
6use super::{PublishedSource, SourceKey, remove_from_index_set};
7use crate::engine::source_model::{
8 ActiveSpeakerGroup, ActiveSpeakerSourceRole, PublishedSourceId, SourceEncodingId, UserStreamId,
9};
10
11#[derive(Debug)]
12pub(in crate::engine) struct PublishedSources {
13 records: BTreeMap<PublishedSourceId, PublishedSource>,
14 id_by_key: BTreeMap<SourceKey, PublishedSourceId>,
15 ids_by_owner: BTreeMap<UserId, BTreeSet<PublishedSourceId>>,
16 id_by_transport: BTreeMap<TransportMediaId, PublishedSourceId>,
17 next_id: u64,
18 next_encoding_id: u64,
19}
20
21impl Default for PublishedSources {
22 fn default() -> Self {
23 Self {
24 records: BTreeMap::new(),
25 id_by_key: BTreeMap::new(),
26 ids_by_owner: BTreeMap::new(),
27 id_by_transport: BTreeMap::new(),
28 next_id: 1,
29 next_encoding_id: 1,
30 }
31 }
32}
33
34impl PublishedSources {
35 pub(super) fn allocate_id(&mut self) -> PublishedSourceId {
36 PublishedSourceId::allocate(&mut self.next_id)
37 }
38
39 pub(super) fn allocate_encoding_id(&mut self) -> SourceEncodingId {
40 SourceEncodingId::allocate(&mut self.next_encoding_id)
41 }
42
43 pub(super) fn publication_count(&self) -> usize {
44 self.records.len()
45 }
46
47 pub(in crate::engine) fn iter(&self) -> impl Iterator<Item = &PublishedSource> {
49 self.records.values()
50 }
51
52 pub(in crate::engine) fn source(&self, id: PublishedSourceId) -> Option<&PublishedSource> {
53 self.records.get(&id)
54 }
55
56 pub(super) fn source_mut(&mut self, id: PublishedSourceId) -> Option<&mut PublishedSource> {
57 self.records.get_mut(&id)
58 }
59
60 pub(in crate::engine) fn source_for_transport(
61 &self,
62 media: TransportMediaId,
63 ) -> Option<&PublishedSource> {
64 self.source(*self.id_by_transport.get(&media)?)
65 }
66
67 pub(in crate::engine::room) fn id_for_owner_stream(
68 &self,
69 owner: &UserId,
70 stream: &UserStreamId,
71 ) -> Option<PublishedSourceId> {
72 self.id_by_key.get(&SourceKey::new(owner, stream)).copied()
73 }
74
75 pub(super) fn owner_has_promotable_source_in_group(
76 &self,
77 owner: &UserId,
78 group: ActiveSpeakerGroup,
79 ) -> bool {
80 self.ids_for_owner(owner)
81 .filter_map(|id| self.source(id))
82 .filter(|source| source.active)
83 .map(|source| &source.descriptor)
84 .any(|source| {
85 source.policy().active_speaker().is_some_and(|policy| {
86 policy.group() == group && policy.role() == ActiveSpeakerSourceRole::Promotable
87 })
88 })
89 }
90
91 pub(super) fn insert(&mut self, source: PublishedSource) {
92 let id = source.descriptor.source_id();
93 let owner = source.descriptor.owner().user_id().clone();
94 self.id_by_key
95 .insert(SourceKey::new(&owner, source.descriptor.stream_id()), id);
96 self.ids_by_owner.entry(owner).or_default().insert(id);
97 self.id_by_transport
98 .insert(source.transport.transport_media_id(), id);
99 self.records.insert(id, source);
100 }
101
102 pub(super) fn ids_for_owner(
103 &self,
104 user: &UserId,
105 ) -> impl Iterator<Item = PublishedSourceId> + '_ {
106 self.ids_by_owner
107 .get(user)
108 .into_iter()
109 .flat_map(|ids| ids.iter().copied())
110 }
111
112 pub(super) fn remove(&mut self, id: PublishedSourceId) -> Option<PublishedSource> {
113 let source = self.records.remove(&id)?;
114 let owner = source.descriptor.owner().user_id();
115 self.id_by_key
116 .remove(&SourceKey::new(owner, source.descriptor.stream_id()));
117 remove_from_index_set(&mut self.ids_by_owner, owner, &id);
118 self.id_by_transport
119 .remove(&source.transport.transport_media_id());
120 Some(source)
121 }
122}