use super::proto::models::TrackType;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct SubscriptionTarget {
pub session_id: String,
pub track_type: TrackType,
pub dimension: Option<(u32, u32)>,
}
impl SubscriptionTarget {
pub fn new(session_id: impl Into<String>, track_type: TrackType) -> Self {
Self {
session_id: session_id.into(),
track_type,
dimension: None,
}
}
#[must_use]
pub fn with_dimension(mut self, width: u32, height: u32) -> Self {
self.dimension = Some((width, height));
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SubscriptionConfig {
pub audio: bool,
pub video: bool,
pub screen_share: bool,
pub video_dimension: Option<(u32, u32)>,
}
impl Default for SubscriptionConfig {
fn default() -> Self {
Self {
audio: true,
video: false,
screen_share: false,
video_dimension: None,
}
}
}
impl SubscriptionConfig {
pub fn audio_all() -> Self {
Self::default()
}
pub fn audio_video() -> Self {
Self {
audio: true,
video: true,
video_dimension: Some((1280, 720)),
..Self::default()
}
}
pub fn all() -> Self {
Self {
audio: true,
video: true,
screen_share: true,
video_dimension: Some((1280, 720)),
}
}
pub fn none() -> Self {
Self {
audio: false,
video: false,
screen_share: false,
video_dimension: None,
}
}
pub fn matches(&self, track_type: TrackType) -> bool {
match track_type {
TrackType::Audio => self.audio,
TrackType::Video => self.video,
TrackType::ScreenShare | TrackType::ScreenShareAudio => self.screen_share,
TrackType::Unspecified => false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct TrackKey {
pub session_id: String,
pub track_type: i32,
}
impl TrackKey {
pub(crate) fn new(session_id: impl Into<String>, track_type: TrackType) -> Self {
Self {
session_id: session_id.into(),
track_type: track_type as i32,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_audio_only() {
let c = SubscriptionConfig::default();
assert!(c.matches(TrackType::Audio));
assert!(!c.matches(TrackType::Video));
assert!(!c.matches(TrackType::ScreenShare));
}
#[test]
fn audio_video_opts_in_video() {
let c = SubscriptionConfig::audio_video();
assert!(c.matches(TrackType::Audio));
assert!(c.matches(TrackType::Video));
assert!(!c.matches(TrackType::ScreenShare));
}
#[test]
fn none_matches_nothing() {
let c = SubscriptionConfig::none();
assert!(!c.matches(TrackType::Audio));
assert!(!c.matches(TrackType::Video));
}
#[test]
fn target_builder_preserves_session_track_and_dimension() {
let target =
SubscriptionTarget::new("session-1", TrackType::Video).with_dimension(640, 360);
assert_eq!(target.session_id, "session-1");
assert_eq!(target.track_type, TrackType::Video);
assert_eq!(target.dimension, Some((640, 360)));
}
}