Skip to main content

o_sfu_core/engine/director/
allocation.rs

1//! Values Director decides into. Room records embed them and hand `&mut` to
2//! Director, so their fields change only here.
3//!
4//! ```text
5//! ActiveUser.allocation           ReceiverAllocation: budget, soft-pause deadline
6//! CurrentPublication.allocation   RouteAllocation: selector, pause reason
7//! CommittedConsumerRoute          Option<PendingUpgrade>: dropped with the route
8//! ```
9
10use std::time::Instant;
11
12use o_sfu_router::MediaKind;
13
14use crate::{
15    Bitrate,
16    engine::source_model::{PolicyPauseReason, SourceSelector, SourceSubscriptionIntent},
17};
18
19/// What one consumer gets: the encoding its gate forwards and the policy
20/// reason that withholds delivery.
21///
22/// Receiver intent stays with the subscription. A pause reason withholds
23/// delivery without erasing that intent.
24#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
25pub(in crate::engine) struct RouteAllocation {
26    pub(super) selector: SourceSelector,
27    pub(super) pause_reason: Option<PolicyPauseReason>,
28}
29
30impl RouteAllocation {
31    pub(in crate::engine) const fn selector(self) -> SourceSelector {
32        self.selector
33    }
34
35    pub(in crate::engine) const fn pause_reason(self) -> Option<PolicyPauseReason> {
36        self.pause_reason
37    }
38
39    pub(in crate::engine) const fn allows_delivery(self) -> bool {
40        self.pause_reason.is_none()
41    }
42
43    /// Whether packets reach the receiver: its intent wants the stream and no
44    /// policy pause withholds it.
45    pub(in crate::engine) const fn delivers(self, intent: SourceSubscriptionIntent) -> bool {
46        intent.is_active() && self.allows_delivery()
47    }
48
49    /// Pauses a route set up between policy turns that its receiver cannot
50    /// take yet: audio while the receiver is deaf, video beyond its download
51    /// limit. The next turn reevaluates both.
52    #[must_use]
53    pub(in crate::engine) fn admit_setup(
54        mut self,
55        kind: MediaKind,
56        receiver_deaf: bool,
57        video_download_limit_reached: bool,
58    ) -> Self {
59        if !self.allows_delivery() {
60            return self;
61        }
62        match kind {
63            MediaKind::Audio if receiver_deaf => self.pause_for_deaf_receiver(),
64            MediaKind::Video if video_download_limit_reached => {
65                self.pause_reason = Some(PolicyPauseReason::VideoDownloadLimit);
66            }
67            MediaKind::Audio | MediaKind::Video => {}
68        }
69        self
70    }
71
72    /// Deafness dominates every other audio pause reason.
73    pub(in crate::engine) const fn pause_for_deaf_receiver(&mut self) {
74        self.pause_reason = Some(PolicyPauseReason::ReceiverDeafened);
75    }
76}
77
78/// What Director last committed for one receiver connection.
79#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
80pub(in crate::engine) struct ReceiverAllocation {
81    pub(super) budget: ReceiverVideoBudgetDiagnostics,
82    /// Continuous receiver pressure survives victim changes within this connection.
83    pub(super) soft_pause_deadline: Option<Instant>,
84}
85
86impl ReceiverAllocation {
87    pub(in crate::engine) const fn budget(self) -> ReceiverVideoBudgetDiagnostics {
88        self.budget
89    }
90
91    pub(in crate::engine) const fn soft_pause_deadline(self) -> Option<Instant> {
92        self.soft_pause_deadline
93    }
94}
95
96/// Receiver video budget facts behind the last committed allocation.
97#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
98pub(in crate::engine) struct ReceiverVideoBudgetDiagnostics {
99    pub(super) latest_receiver_bandwidth: Option<Bitrate>,
100    pub(super) selected_video_budget: Option<Bitrate>,
101    pub(super) active_video_route_count: usize,
102    pub(super) selected_video_bitrate: Bitrate,
103}
104
105impl ReceiverVideoBudgetDiagnostics {
106    pub(in crate::engine) const fn latest_receiver_bandwidth(self) -> Option<Bitrate> {
107        self.latest_receiver_bandwidth
108    }
109
110    pub(in crate::engine) const fn selected_video_budget(self) -> Option<Bitrate> {
111        self.selected_video_budget
112    }
113
114    pub(in crate::engine) const fn active_video_route_count(self) -> usize {
115        self.active_video_route_count
116    }
117
118    pub(in crate::engine) const fn selected_video_bitrate(self) -> Bitrate {
119        self.selected_video_bitrate
120    }
121}
122
123/// Upgrade target whose eligibility must stay continuous until `deadline`.
124///
125/// The hold lives in the committed consumer, so a replaced route drops it.
126#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127pub(in crate::engine) struct PendingUpgrade {
128    pub(super) selector: SourceSelector,
129    pub(super) deadline: Instant,
130}
131
132impl PendingUpgrade {
133    pub(in crate::engine) const fn selector(self) -> SourceSelector {
134        self.selector
135    }
136
137    pub(in crate::engine) const fn deadline(self) -> Instant {
138        self.deadline
139    }
140}