Skip to main content

o_sfu_core/engine/director/planner/
mod.rs

1//! Pure receiver policy: facts and time in, allocations and wakeups out.
2//!
3//! [`plan`] takes no lock, reads no clock, applies no control and emits no
4//! telemetry. Turn execution captures [`PolicyFacts`] under the room read
5//! guard and applies the returned [`PolicyPlan`] after transport accepts it.
6//!
7//! ```text
8//! speakers -> audio admission -> per receiver video:
9//!   filter -> plan -> download limit -> overload fit -> dwell -> route plans
10//! ```
11
12mod audio;
13mod speakers;
14#[cfg(test)]
15#[path = "TESTS/mod.rs"]
16mod tests;
17mod video;
18
19use std::time::Instant;
20
21use o_sfu_model::{UserId, VideoLayoutIntent};
22
23pub(super) use self::video::{VideoAdmissionRank, layout_role};
24use super::allocation::{PendingUpgrade, ReceiverAllocation, RouteAllocation};
25use crate::{
26    Bitrate, RoomMediaLimits, VideoAdaptationTuning,
27    engine::source_model::{PolicyPauseReason, PublishedSourceDescriptor, PublishedSourceId},
28};
29
30/// Room facts and transport observations one policy turn plans from.
31#[derive(Debug)]
32pub(super) struct PolicyFacts<'facts> {
33    /// Every room user, ordered by user id.
34    pub(super) receivers: Vec<ReceiverFacts<'facts>>,
35    /// Consumers whose source and subscription are active, ordered by
36    /// receiver, publisher and stream. Policy-paused consumers stay listed so
37    /// a later turn can resume them.
38    pub(super) routes: Vec<RouteFacts<'facts>>,
39    /// Speaker observations of the room's active sources.
40    pub(super) speakers: Vec<SpeakerFacts>,
41    pub(super) media_limits: RoomMediaLimits,
42    pub(super) tuning: VideoAdaptationTuning,
43}
44
45#[derive(Debug)]
46pub(super) struct ReceiverFacts<'facts> {
47    pub(super) user_id: &'facts UserId,
48    pub(super) deaf: bool,
49    pub(super) featured: Option<bool>,
50    /// Bandwidth estimate of the receiver's current connection.
51    pub(super) bandwidth: Option<Bitrate>,
52    pub(super) allocation: ReceiverAllocation,
53}
54
55#[derive(Debug)]
56pub(super) struct RouteFacts<'facts> {
57    /// Index of the consumer's receiver in [`PolicyFacts::receivers`].
58    pub(super) receiver: usize,
59    pub(super) source: &'facts PublishedSourceDescriptor,
60    /// Source bitrate observed across all of its encodings.
61    pub(super) source_bitrate: Option<Bitrate>,
62    pub(super) layout: Option<VideoLayoutIntent>,
63    pub(super) allocation: RouteAllocation,
64    pub(super) pending_upgrade: Option<PendingUpgrade>,
65}
66
67#[derive(Debug)]
68pub(super) struct SpeakerFacts {
69    pub(super) source_id: PublishedSourceId,
70    pub(super) observed_at: Instant,
71    pub(super) audio_level_dbov: Option<i8>,
72    pub(super) owner_screensharing: bool,
73    /// Owner the speaker features: the detector's owner while that owner has
74    /// a promotable source in the same group.
75    pub(super) featured_owner: Option<UserId>,
76}
77
78#[derive(Debug)]
79pub(super) struct PolicyPlan {
80    /// Aligned with [`PolicyFacts::routes`].
81    pub(super) routes: Vec<RoutePlan>,
82    /// Aligned with [`PolicyFacts::receivers`].
83    pub(super) receivers: Vec<ReceiverPlan>,
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub(super) struct RoutePlan {
88    pub(super) allocation: RouteAllocation,
89    pub(super) pending_upgrade: Option<PendingUpgrade>,
90    /// The plan cancels or retargets the captured upgrade hold.
91    pub(super) interrupts_upgrade: bool,
92    /// A new encoding or a resumed route may not share the receiver's last
93    /// decodable reference chain.
94    pub(super) request_keyframe: bool,
95    pub(super) transition: Option<VideoRouteTransition>,
96    /// Estimated bitrate of the planned encoding, reported with a transition.
97    pub(super) selected_estimated_bitrate: Option<Bitrate>,
98    /// Set on the video consumers that share their receiver's video budget.
99    pub(super) budget_bitrate: Option<BudgetBitrate>,
100}
101
102/// Bitrate one consumer counts against its receiver's video budget.
103#[derive(Debug, Clone, Copy, PartialEq, Eq)]
104pub(super) struct BudgetBitrate {
105    pub(super) captured: Bitrate,
106    pub(super) planned: Bitrate,
107}
108
109#[derive(Debug, Clone, Copy, PartialEq, Eq)]
110pub(super) struct ReceiverPlan {
111    pub(super) allocation: ReceiverAllocation,
112    /// Audio reserve plus the video demand that bandwidth probing aims for.
113    pub(super) bwe_target: Bitrate,
114    pub(super) featured: Option<bool>,
115    /// Earliest future wakeup of the planned allocation.
116    pub(super) next_deadline: Option<Instant>,
117    /// Earliest future wakeup that was already committed. It survives the
118    /// rejection of a sibling control.
119    pub(super) retained_deadline: Option<Instant>,
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub(super) enum VideoRouteTransition {
124    Degraded,
125    Paused { reason: PolicyPauseReason },
126    Resumed { cleared_reason: PolicyPauseReason },
127}
128
129impl RoutePlan {
130    const fn unchanged(route: &RouteFacts<'_>) -> Self {
131        Self {
132            allocation: route.allocation,
133            pending_upgrade: route.pending_upgrade,
134            interrupts_upgrade: false,
135            request_keyframe: false,
136            transition: None,
137            selected_estimated_bitrate: None,
138            budget_bitrate: None,
139        }
140    }
141}
142
143#[must_use]
144pub(super) fn plan(facts: &PolicyFacts<'_>, now: Instant) -> PolicyPlan {
145    let speakers = speakers::SpeakerPlan::new(facts);
146    let audio_reserves = audio::reserve_by_receiver(facts, &speakers);
147    let mut plan = PolicyPlan {
148        routes: facts.routes.iter().map(RoutePlan::unchanged).collect(),
149        receivers: facts
150            .receivers
151            .iter()
152            .zip(&audio_reserves)
153            .map(|(receiver, &audio_reserve)| ReceiverPlan {
154                allocation: ReceiverAllocation::default(),
155                bwe_target: audio_reserve,
156                featured: speakers.featured(receiver),
157                next_deadline: None,
158                retained_deadline: None,
159            })
160            .collect(),
161    };
162    audio::plan(facts, &speakers, &mut plan.routes);
163    video::plan(facts, &speakers, &audio_reserves, now, &mut plan);
164    plan
165}