Skip to main content

o_sfu_core/engine/director/planner/video/
input.rs

1use o_sfu_model::VideoLayoutIntent;
2use o_sfu_router::MediaKind;
3
4use super::super::{PolicyFacts, speakers::SpeakerPlan};
5use crate::{
6    Bitrate,
7    engine::{
8        director::allocation::{PendingUpgrade, RouteAllocation},
9        source_model::{
10            PublishedSourceDescriptor, SourceAdaptationPolicy, SourceRoomPolicySelector,
11        },
12    },
13};
14
15/// Video consumer with adaptation or a source bitrate cap.
16#[derive(Debug)]
17pub(super) struct ReceiverVideoRouteInput<'a> {
18    /// Index in [`PolicyFacts::routes`].
19    pub(super) index: usize,
20    /// Index in [`PolicyFacts::receivers`].
21    pub(super) receiver: usize,
22    pub(super) user_count: usize,
23    pub(super) source: &'a PublishedSourceDescriptor,
24    pub(super) current: RouteAllocation,
25    pub(super) pending_upgrade: Option<PendingUpgrade>,
26    pub(super) layout_role: SourceRoomPolicySelector,
27    pub(super) visible_scalable_route_count: usize,
28    pub(super) active_speaker_rank: Option<usize>,
29    pub(super) receiver_bandwidth: Option<Bitrate>,
30    pub(super) source_bitrate: Option<Bitrate>,
31    pub(super) audio_budget_reserve: Bitrate,
32}
33
34pub(super) fn receiver_video_routes<'a>(
35    facts: &PolicyFacts<'a>,
36    speakers: &SpeakerPlan,
37    audio_reserves: &[Bitrate],
38) -> Vec<ReceiverVideoRouteInput<'a>> {
39    let mut routes = Vec::with_capacity(facts.routes.len());
40    for (index, route) in facts.routes.iter().enumerate() {
41        let source = route.source;
42        if source.media_kind() != MediaKind::Video
43            || (source.policy().adaptation() == SourceAdaptationPolicy::None
44                && source.policy().video_bitrate_cap().is_none())
45        {
46            continue;
47        }
48        let Some(receiver) = facts.receivers.get(route.receiver) else {
49            continue;
50        };
51        let owner = source.owner().user_id();
52        routes.push(ReceiverVideoRouteInput {
53            index,
54            receiver: route.receiver,
55            user_count: facts.receivers.len(),
56            source,
57            current: route.allocation,
58            pending_upgrade: route.pending_upgrade,
59            layout_role: layout_role(
60                source,
61                route.layout,
62                speakers.featured_source_owners.contains(owner),
63            ),
64            visible_scalable_route_count: 1,
65            active_speaker_rank: speakers.rank_by_owner.get(owner).copied(),
66            receiver_bandwidth: receiver.bandwidth,
67            source_bitrate: route.source_bitrate,
68            audio_budget_reserve: audio_reserves
69                .get(route.receiver)
70                .copied()
71                .unwrap_or_else(Bitrate::zero),
72        });
73    }
74    for receiver_routes in routes.chunk_by_mut(|left, right| left.receiver == right.receiver) {
75        let visible_scalable_route_count = receiver_routes
76            .iter()
77            .filter(|route| {
78                route.source.policy().adaptation() == SourceAdaptationPolicy::ScalableVideo
79                    && route.layout_role.counts_toward_visible_budget()
80            })
81            .count()
82            .max(1);
83        for route in receiver_routes {
84            route.visible_scalable_route_count = visible_scalable_route_count;
85        }
86    }
87    routes
88}
89
90/// Role a receiver's layout gives a source. Sources without a layout policy
91/// stay hidden.
92pub(in crate::engine::director) fn layout_role(
93    source: &PublishedSourceDescriptor,
94    preference: Option<VideoLayoutIntent>,
95    active_speaker: bool,
96) -> SourceRoomPolicySelector {
97    source
98        .policy()
99        .layout()
100        .map_or(SourceRoomPolicySelector::Hidden, |policy| {
101            policy.resolve(preference, active_speaker)
102        })
103}