Skip to main content

o_sfu_core/options/
media.rs

1use std::time::Duration;
2
3use crate::Bitrate;
4
5/// Room media activation limits.
6///
7/// These limits control receiver delivery. They do not erase publication state
8/// or user subscription intent.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct RoomMediaLimits {
11    max_active_audio_speakers: usize,
12    max_video_downloads_per_receiver: usize,
13}
14
15/// Invalid room media limit input.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
17pub enum RoomMediaLimitsError {
18    #[error("maximum active audio speakers must be greater than zero")]
19    MaxActiveAudioSpeakersZero,
20    #[error("maximum video downloads per receiver must be greater than zero")]
21    MaxVideoDownloadsPerReceiverZero,
22}
23
24impl RoomMediaLimits {
25    pub const DEFAULT_MAX_ACTIVE_AUDIO_SPEAKERS: usize = 4;
26    pub const DEFAULT_MAX_VIDEO_DOWNLOADS_PER_RECEIVER: usize = 10;
27
28    /// Build room media limits after validating their invariants.
29    ///
30    /// # Errors
31    ///
32    /// Returns [`RoomMediaLimitsError`] when a limit is zero.
33    pub const fn try_new(
34        max_active_audio_speakers: usize,
35        max_video_downloads_per_receiver: usize,
36    ) -> Result<Self, RoomMediaLimitsError> {
37        if max_active_audio_speakers == 0 {
38            return Err(RoomMediaLimitsError::MaxActiveAudioSpeakersZero);
39        }
40        if max_video_downloads_per_receiver == 0 {
41            return Err(RoomMediaLimitsError::MaxVideoDownloadsPerReceiverZero);
42        }
43        Ok(Self {
44            max_active_audio_speakers,
45            max_video_downloads_per_receiver,
46        })
47    }
48
49    #[must_use]
50    pub const fn conservative() -> Self {
51        Self {
52            max_active_audio_speakers: Self::DEFAULT_MAX_ACTIVE_AUDIO_SPEAKERS,
53            max_video_downloads_per_receiver: Self::DEFAULT_MAX_VIDEO_DOWNLOADS_PER_RECEIVER,
54        }
55    }
56
57    #[must_use]
58    pub const fn max_active_audio_speakers(self) -> usize {
59        self.max_active_audio_speakers
60    }
61
62    #[must_use]
63    pub const fn max_video_downloads_per_receiver(self) -> usize {
64        self.max_video_downloads_per_receiver
65    }
66}
67
68impl Default for RoomMediaLimits {
69    fn default() -> Self {
70        Self::conservative()
71    }
72}
73
74/// Room-wide video budget and adaptation dwell.
75///
76/// When room membership reaches `multiparty_scalable_video_threshold`,
77/// scalable-video per-route targets use available receiver bandwidth and the
78/// resolved layout role. Pinned, featured, readable-detail and active-speaker
79/// targets use the full receiver budget. When several visible scalable routes
80/// share a receiver, other scalable targets divide that budget by their count and
81/// `thumbnail_budget_divisor`. Soft pauses require continuous receiver pressure
82/// for `soft_pause_dwell`. Upgrades require one eligible target for `upgrade_dwell`.
83/// Headroom is removed before `audio_reserve_per_speaker` is subtracted
84/// for each admitted audio route the receiver consumes.
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub struct VideoAdaptationTuning {
87    pub(crate) multiparty_scalable_video_threshold: usize,
88    pub(crate) thumbnail_budget_divisor: u64,
89    pub(crate) soft_pause_dwell: Duration,
90    pub(crate) upgrade_dwell: Duration,
91    pub(crate) receiver_budget_headroom_percent: u8,
92    pub(crate) audio_reserve_per_speaker: Bitrate,
93}
94
95#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
96pub enum VideoAdaptationTuningError {
97    #[error("multiparty scalable video threshold must be greater than zero")]
98    MultipartyScalableVideoThresholdZero,
99    #[error("thumbnail budget divisor must be greater than zero")]
100    ThumbnailBudgetDivisorZero,
101    #[error("soft pause dwell must be greater than zero")]
102    SoftPauseDwellZero,
103    #[error("upgrade dwell must be greater than zero")]
104    UpgradeDwellZero,
105    #[error("soft pause dwell exceeds the portable deadline range")]
106    SoftPauseDwellTooLong,
107    #[error("upgrade dwell exceeds the portable deadline range")]
108    UpgradeDwellTooLong,
109    #[error("receiver budget headroom percent must not exceed 100")]
110    ReceiverBudgetHeadroomPercentTooHigh,
111}
112
113impl VideoAdaptationTuning {
114    pub const DEFAULT_MULTIPARTY_SCALABLE_VIDEO_THRESHOLD: usize = 3;
115    pub const DEFAULT_THUMBNAIL_BUDGET_DIVISOR: u64 = 2;
116    pub const DEFAULT_SOFT_PAUSE_DWELL: Duration = Duration::from_millis(750);
117    pub const DEFAULT_UPGRADE_DWELL: Duration = Duration::from_millis(750);
118    /// Bounds deadline addition to the roughly 100-year portable `Instant` range.
119    ///
120    /// See [`Instant` OS-specific behavior](std::time::Instant#os-specific-behaviors).
121    pub const MAX_DWELL: Duration = Duration::from_hours(100 * 365 * 24);
122    pub const DEFAULT_RECEIVER_BUDGET_HEADROOM_PERCENT: u8 = 0;
123    pub const DEFAULT_AUDIO_RESERVE_PER_SPEAKER: Bitrate = Bitrate::zero();
124
125    /// Builds validated video adaptation tuning.
126    ///
127    /// # Errors
128    ///
129    /// Returns [`VideoAdaptationTuningError`] when the scalable-video threshold,
130    /// the thumbnail budget divisor or either dwell is zero, either dwell exceeds
131    /// [`Self::MAX_DWELL`] or the headroom percent exceeds 100.
132    pub const fn try_new(
133        multiparty_scalable_video_threshold: usize,
134        thumbnail_budget_divisor: u64,
135        soft_pause_dwell: Duration,
136        upgrade_dwell: Duration,
137        receiver_budget_headroom_percent: u8,
138        audio_reserve_per_speaker: Bitrate,
139    ) -> Result<Self, VideoAdaptationTuningError> {
140        if multiparty_scalable_video_threshold == 0 {
141            return Err(VideoAdaptationTuningError::MultipartyScalableVideoThresholdZero);
142        }
143        if thumbnail_budget_divisor == 0 {
144            return Err(VideoAdaptationTuningError::ThumbnailBudgetDivisorZero);
145        }
146        if soft_pause_dwell.is_zero() {
147            return Err(VideoAdaptationTuningError::SoftPauseDwellZero);
148        }
149        if upgrade_dwell.is_zero() {
150            return Err(VideoAdaptationTuningError::UpgradeDwellZero);
151        }
152        if soft_pause_dwell.as_nanos() > Self::MAX_DWELL.as_nanos() {
153            return Err(VideoAdaptationTuningError::SoftPauseDwellTooLong);
154        }
155        if upgrade_dwell.as_nanos() > Self::MAX_DWELL.as_nanos() {
156            return Err(VideoAdaptationTuningError::UpgradeDwellTooLong);
157        }
158        if receiver_budget_headroom_percent > 100 {
159            return Err(VideoAdaptationTuningError::ReceiverBudgetHeadroomPercentTooHigh);
160        }
161        Ok(Self {
162            multiparty_scalable_video_threshold,
163            thumbnail_budget_divisor,
164            soft_pause_dwell,
165            upgrade_dwell,
166            receiver_budget_headroom_percent,
167            audio_reserve_per_speaker,
168        })
169    }
170}
171
172impl Default for VideoAdaptationTuning {
173    fn default() -> Self {
174        Self {
175            multiparty_scalable_video_threshold: Self::DEFAULT_MULTIPARTY_SCALABLE_VIDEO_THRESHOLD,
176            thumbnail_budget_divisor: Self::DEFAULT_THUMBNAIL_BUDGET_DIVISOR,
177            soft_pause_dwell: Self::DEFAULT_SOFT_PAUSE_DWELL,
178            upgrade_dwell: Self::DEFAULT_UPGRADE_DWELL,
179            receiver_budget_headroom_percent: Self::DEFAULT_RECEIVER_BUDGET_HEADROOM_PERCENT,
180            audio_reserve_per_speaker: Self::DEFAULT_AUDIO_RESERVE_PER_SPEAKER,
181        }
182    }
183}
184
185#[cfg(test)]
186#[path = "TESTS/media.rs"]
187mod tests;