use bitcode::{Decode, Encode}; use crate::{constant::MAXIMUM_SHARE_LAYERS, model::Id}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)] pub enum Codec { H264, H265, Av1, Vp8, Vp9, } impl Codec { pub const ALL: [Self; 5] = [Self::H264, Self::H265, Self::Av1, Self::Vp8, Self::Vp9]; pub const PREFERENCE: [Self; 3] = [Self::Av1, Self::H265, Self::H264]; pub fn name(self) -> &'static str { match self { Self::H264 => "H264", Self::H265 => "H265", Self::Av1 => "AV1", Self::Vp8 => "VP8", Self::Vp9 => "VP9", } } pub fn parse(name: &str) -> Option { Self::ALL.into_iter().find(|codec| codec.name() == name) } pub fn bits_per_pixel(self) -> f32 { match self { Self::Vp8 => 0.18, Self::H264 => 0.16, Self::Vp9 => 0.14, Self::H265 | Self::Av1 => 0.12, } } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)] pub enum Resolution { P360, P480, P720, P1080, P1440, P2160, } impl Resolution { pub const ALL: [Self; 6] = [ Self::P360, Self::P480, Self::P720, Self::P1080, Self::P1440, Self::P2160, ]; pub fn height(self) -> u32 { match self { Self::P360 => 360, Self::P480 => 480, Self::P720 => 720, Self::P1080 => 1080, Self::P1440 => 1440, Self::P2160 => 2160, } } pub fn size(self, source_width: u32, source_height: u32) -> (u32, u32) { let height = self.height().min(source_height); let width = (source_width as u64 * height as u64 / source_height.max(1) as u64) as u32; (width & !7, height & !7) } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)] pub enum Fps { F15, F30, F60, } impl Fps { pub const ALL: [Self; 3] = [Self::F15, Self::F30, Self::F60]; pub fn value(self) -> u32 { match self { Self::F15 => 15, Self::F30 => 30, Self::F60 => 60, } } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)] pub struct Quality { pub resolution: Resolution, pub fps: Fps, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)] pub struct Layer { pub quality: Quality, pub codec: Codec, } impl Layer { pub fn to_byte(self) -> u8 { (self.quality.resolution as u8) << 5 | (self.quality.fps as u8) << 3 | self.codec as u8 } pub fn from_byte(byte: u8) -> Option { Some(Self { quality: Quality { resolution: *Resolution::ALL.get((byte >> 5) as usize)?, fps: *Fps::ALL.get((byte >> 3 & 0b11) as usize)?, }, codec: *Codec::ALL.get((byte & 0b111) as usize)?, }) } pub fn bitrate(self, source_width: u32, source_height: u32) -> u32 { let (width, height) = self.quality.resolution.size(source_width, source_height); (width as f32 * height as f32 * self.quality.fps.value() as f32 * self.codec.bits_per_pixel()) as u32 } } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Source { pub width: u32, pub height: u32, pub top: Quality, pub encode: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Encode, Decode)] pub enum Kind { Screen, Camera, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Share { pub user_id: Id, pub kind: Kind, pub source: Source, pub started_ms: u64, } pub type Mode = (u32, u32); pub fn options( modes: &[Mode], resolution: Resolution, max_height: u32, ) -> (Vec, Vec) { let reachable = |height: u32| modes.iter().filter(move |(mode, _)| *mode >= height); let resolutions: Vec = Resolution::ALL .into_iter() .filter(|option| { option.height() <= max_height && reachable(option.height()).next().is_some() }) .collect(); let height = resolutions .iter() .copied() .filter(|option| *option <= resolution) .max() .or(resolutions.first().copied()) .map_or(0, Resolution::height); let fps = Fps::ALL .into_iter() .filter(|fps| reachable(height).any(|(_, rate)| rate.saturating_add(1) >= fps.value())) .collect(); (resolutions, fps) } pub fn clamp(top: Quality, modes: &[Mode], max_height: u32) -> Quality { let (resolutions, _) = options(modes, top.resolution, max_height); let resolution = resolutions .into_iter() .filter(|resolution| *resolution <= top.resolution) .max() .unwrap_or(Resolution::P360); let (_, frame_rates) = options(modes, resolution, max_height); Quality { resolution, fps: frame_rates .into_iter() .filter(|fps| *fps <= top.fps) .max() .unwrap_or(Fps::F15), } } pub fn best_codec(encode: &[Codec], decode: &[Codec]) -> Option { Codec::PREFERENCE .into_iter() .find(|codec| encode.contains(codec) && decode.contains(codec)) } pub fn choose(source: &Source, wanted: Quality, decode: &[Codec], used: &[Layer]) -> Option { let codec = best_codec(&source.encode, decode)?; let quality = Quality { resolution: wanted.resolution.min(source.top.resolution), fps: wanted.fps.min(source.top.fps), }; let layer = Layer { quality, codec }; if used.len() < MAXIMUM_SHARE_LAYERS || used.contains(&layer) { return Some(layer); } used.iter() .copied() .filter(|used| used.quality <= quality && decode.contains(&used.codec)) .max() .or_else(|| { used.iter() .copied() .filter(|used| decode.contains(&used.codec)) .min() }) } #[cfg(test)] mod tests { use super::*; #[test] fn fps_follows_resolution() { let modes = [(720, 60), (1080, 30)]; let (resolutions, fps) = options(&modes, Resolution::P1080, u32::MAX); assert!(resolutions.contains(&Resolution::P1080)); assert_eq!(fps, vec![Fps::F15, Fps::F30]); assert!( options(&modes, Resolution::P720, u32::MAX) .1 .contains(&Fps::F60) ); let top = Quality { resolution: Resolution::P1080, fps: Fps::F60, }; assert_eq!(clamp(top, &modes, u32::MAX).fps, Fps::F30); assert_eq!( options(&[(1440, 59)], Resolution::P1440, u32::MAX).1.last(), Some(&Fps::F60) ); } #[test] fn layer_byte_round_trip_and_choice() { for resolution in Resolution::ALL { for fps in Fps::ALL { for codec in Codec::ALL { let layer = Layer { quality: Quality { resolution, fps }, codec, }; assert_eq!(Layer::from_byte(layer.to_byte()), Some(layer)); } } } let source = Source { width: 2560, height: 1440, top: Quality { resolution: Resolution::P1440, fps: Fps::F60, }, encode: vec![Codec::H264, Codec::Av1], }; let wanted = Quality { resolution: Resolution::P1080, fps: Fps::F60, }; let layer = choose(&source, wanted, &[Codec::H264, Codec::H265], &[]).unwrap(); assert_eq!(layer.quality.resolution, Resolution::P1080); assert_eq!(layer.codec, Codec::H264); } }