Skip to main content

o_sfu_core/engine/room/transition/
subscription.rs

1//! Receiver intent and consumer-route realization.
2//!
3//! Intent survives a missing publication or unready receiver. Readiness reserves
4//! routes under the room lock, declares transport without it and commits only if
5//! the reservation still matches the publication and receiver connection.
6
7use std::collections::BTreeMap;
8
9use o_sfu_model::UserId;
10use o_sfu_router::rtp::MediaCapabilities;
11use o_sfu_transport::{SessionOffer, TransportAdapterError, TransportMediaId, TransportSessionKey};
12
13use super::super::{Room, effects::batch::RoomEffects, outbound::RemoteTrackSnapshot};
14use crate::{
15    ConnectionId,
16    engine::source_model::{SourceSubscriptionIntent, UserStreamId},
17};
18
19impl Room {
20    /// Returns `None` for a missing or stale connection. The first accepted
21    /// capabilities commit realizes receiver routes waiting on negotiation.
22    pub(crate) async fn apply_session_negotiated(
23        &self,
24        user_id: &UserId,
25        connection_id: ConnectionId,
26        capabilities: MediaCapabilities,
27        declined_consumers: &[TransportMediaId],
28    ) -> Option<()> {
29        let became_ready = {
30            let mut state = self.state.write().await;
31            state.set_user_negotiated(user_id, connection_id, capabilities)
32        }?;
33        if became_ready {
34            self.apply_session_refreshed(user_id, connection_id, declined_consumers)
35                .await
36        } else {
37            Some(())
38        }
39    }
40
41    /// Returns `None` for a missing, stale or not yet negotiated connection.
42    pub(crate) async fn apply_session_refreshed(
43        &self,
44        user_id: &UserId,
45        connection_id: ConnectionId,
46        declined_consumers: &[TransportMediaId],
47    ) -> Option<()> {
48        let commit = {
49            let mut state = self.state.write().await;
50            state.refresh_consumer_readiness(user_id, connection_id, declined_consumers)
51        };
52        let commit = commit?;
53        RoomEffects::from(commit).execute(self).await;
54        Some(())
55    }
56
57    /// Creates this connection's next offer from the tracks the room has
58    /// committed for it and returns both.
59    ///
60    /// The offer adds only the consumers of the returned tracks, so a caller
61    /// that names those tracks before the offer names every consumer MID it
62    /// adds. The tracks are read once, before the transport call: a setup turn
63    /// declares its consumer in the transport before the room commits its
64    /// route. A consumer committed later joins the next offer. A publisher
65    /// that leaves later is retracted by the snapshot of its departure.
66    ///
67    /// # Errors
68    ///
69    /// Returns [`TransportAdapterError::TransportUnavailable`] when the session
70    /// cannot be addressed, [`TransportAdapterError::InvalidInput`] before the
71    /// initial answer or while an offer awaits its answer and
72    /// [`TransportAdapterError::UnsupportedFeature`] when no change needs an
73    /// offer.
74    pub(crate) async fn create_renegotiation_offer(
75        &self,
76        session_key: &TransportSessionKey,
77    ) -> Result<(SessionOffer, RemoteTrackSnapshot), TransportAdapterError> {
78        let tracks = self
79            .state
80            .read()
81            .await
82            .committed_remote_tracks(session_key.user_id(), session_key.connection_id());
83        let committed_consumers = tracks
84            .tracks
85            .iter()
86            .map(|track| track.consumer_media)
87            .collect();
88        let offer = self
89            .media_transport
90            .create_session_renegotiation_offer(session_key, committed_consumers)
91            .await?;
92        Ok((offer, tracks))
93    }
94
95    pub(crate) async fn apply_receiver_intent(
96        &self,
97        user_id: &UserId,
98        connection_id: ConnectionId,
99        target_user_id: &UserId,
100        intents: &BTreeMap<UserStreamId, SourceSubscriptionIntent>,
101    ) -> Option<()> {
102        let commit = self.state.write().await.apply_receiver_intent(
103            user_id,
104            connection_id,
105            target_user_id,
106            intents,
107        )?;
108        self.metrics
109            .record_subscription_intent_evictions(commit.work.evictions);
110        RoomEffects::from(commit).execute(self).await;
111        Some(())
112    }
113}
114
115#[cfg(test)]
116#[path = "TESTS/subscription.rs"]
117mod tests;