Skip to main content

o_sfu_core/engine/director/
layout.rs

1//! Layout roles Director gives Room between policy turns.
2
3use o_sfu_model::{UserId, VideoLayoutIntent};
4use o_sfu_router::MediaKind;
5
6use super::planner::{self, VideoAdmissionRank};
7use crate::engine::{
8    room::{RoomState, SubscriptionKey},
9    source_model::{
10        PublishedSourceDescriptor, PublishedSourceId, SourceRoomPolicySelector, SourceRoutePriority,
11    },
12};
13
14/// Orders consumer setups before their first policy turn.
15///
16/// Setup has no active-speaker snapshot, so video ranks by layout role alone
17/// and the next policy turn applies the speaker order. Audio shares the
18/// priority of pinned or featured video and the source id breaks their tie.
19#[must_use]
20pub(in crate::engine) fn initial_video_admission_rank(
21    state: &RoomState,
22    receiver: &UserId,
23    kind: MediaKind,
24    source_id: PublishedSourceId,
25) -> VideoAdmissionRank {
26    if kind != MediaKind::Video {
27        return VideoAdmissionRank::new(SourceRoutePriority::PinnedOrFeatured, None, source_id);
28    }
29    let Some(source) = state.topology.sources().source(source_id) else {
30        return VideoAdmissionRank::new(SourceRoutePriority::HiddenOrOverflow, None, source_id);
31    };
32    let preference = layout_preference(state, receiver, &source.descriptor);
33    VideoAdmissionRank::new(
34        planner::layout_role(&source.descriptor, preference, false).priority(),
35        None,
36        source_id,
37    )
38}
39
40/// Layout role shown in diagnostics. The committed featured flag stands in for
41/// the speaker observations of a policy turn.
42#[must_use]
43pub(in crate::engine) fn diagnostics_video_layout_role(
44    state: &RoomState,
45    receiver: &UserId,
46    source: &PublishedSourceDescriptor,
47) -> Option<SourceRoomPolicySelector> {
48    let policy = source.policy().layout()?;
49    let preference = layout_preference(state, receiver, source);
50    let active_speaker = state
51        .users
52        .get(source.owner().user_id())
53        .is_some_and(|user| user.featured() == Some(true));
54    Some(policy.resolve(preference, active_speaker))
55}
56
57fn layout_preference(
58    state: &RoomState,
59    receiver: &UserId,
60    source: &PublishedSourceDescriptor,
61) -> Option<VideoLayoutIntent> {
62    state
63        .topology
64        .route_graph()
65        .intent(&SubscriptionKey::new(
66            receiver,
67            source.owner().user_id(),
68            source.stream_id(),
69        ))
70        .layout()
71}