#[derive(Default, Debug, Clone, PartialEq)]
pub struct ConstrainRange<T> {
pub max: Option<T>,
pub min: Option<T>,
pub exact: Option<T>,
pub ideal: Option<T>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Constrain<T> {
Value(T),
Range(ConstrainRange<T>),
}
#[derive(Debug, Clone)]
pub struct MediaTrackConstraintSet {
width: Option<Constrain<u32>>,
height: Option<Constrain<u32>>,
aspect_ratio: Option<Constrain<f64>>,
frame_rate: Option<Constrain<f64>>,
facing_mode: Constrain<String>,
resize_mode: Constrain<String>,
sample_rate: Constrain<u32>,
sample_size: Constrain<u32>,
echo_cancellation: Constrain<bool>,
auto_gain_control: Constrain<bool>,
noise_suppression: Constrain<bool>,
latency: Constrain<f64>,
channel_count: Constrain<u32>,
device_id: Constrain<String>,
group_id: Constrain<String>,
background_blur: Constrain<bool>,
}
impl MediaTrackConstraintSet {
pub fn with_width(mut self, width: Constrain<u32>) -> Self {
self.width = Some(width);
self
}
pub fn with_height(mut self, height: Constrain<u32>) -> Self {
self.height = Some(height);
self
}
pub fn with_aspect_ratio(mut self, aspect_ratio: Constrain<f64>) -> Self {
self.aspect_ratio = Some(aspect_ratio);
self
}
pub fn with_frame_rate(mut self, frame_rate: Constrain<f64>) -> Self {
self.frame_rate = Some(frame_rate);
self
}
pub fn width(&self) -> Option<&Constrain<u32>> {
self.width.as_ref()
}
pub fn height(&self) -> Option<&Constrain<u32>> {
self.height.as_ref()
}
pub fn aspect_ratio(&self) -> Option<&Constrain<f64>> {
self.aspect_ratio.as_ref()
}
pub fn frame_rate(&self) -> Option<&Constrain<f64>> {
self.frame_rate.as_ref()
}
fn remote_video_property(&self) -> Option<&'static str> {
[
("width", self.width.is_some()),
("height", self.height.is_some()),
("frameRate", self.frame_rate.is_some()),
("aspectRatio", self.aspect_ratio.is_some()),
]
.into_iter()
.find_map(|(name, set)| set.then_some(name))
}
}
impl Default for MediaTrackConstraintSet {
fn default() -> Self {
Self {
width: None,
height: None,
aspect_ratio: None,
frame_rate: None,
facing_mode: Constrain::Value("".to_string()),
resize_mode: Constrain::Value("".to_string()),
sample_rate: Constrain::Value(0),
sample_size: Constrain::Value(0),
echo_cancellation: Constrain::Value(false),
auto_gain_control: Constrain::Value(false),
noise_suppression: Constrain::Value(false),
latency: Constrain::Value(0.0),
channel_count: Constrain::Value(0),
device_id: Constrain::Value("".to_string()),
group_id: Constrain::Value("".to_string()),
background_blur: Constrain::Value(false),
}
}
}
#[derive(Default, Debug, Clone)]
pub struct MediaTrackConstraints {
basic: MediaTrackConstraintSet,
advanced: Vec<MediaTrackConstraintSet>,
}
impl MediaTrackConstraints {
pub fn new(basic: MediaTrackConstraintSet, advanced: Vec<MediaTrackConstraintSet>) -> Self {
Self { basic, advanced }
}
pub fn basic(&self) -> &MediaTrackConstraintSet {
&self.basic
}
pub fn advanced(&self) -> &[MediaTrackConstraintSet] {
&self.advanced
}
pub(crate) fn remote_video_property(&self) -> Option<&'static str> {
std::iter::once(&self.basic)
.chain(&self.advanced)
.find_map(MediaTrackConstraintSet::remote_video_property)
}
}