| 1 | /// User-facing camera / stage video quality ceiling. No 4K mode. |
| 2 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 3 | pub enum VideoQuality { |
| 4 | #[default] |
| 5 | Auto, |
| 6 | P720, |
| 7 | P1080, |
| 8 | } |
| 9 | |
| 10 | impl VideoQuality { |
| 11 | /// Preferred capture height for this choice (Auto prefers 720 to keep uplink lean). |
| 12 | pub fn capture_height(self) -> u32 { |
| 13 | match self { |
| 14 | Self::Auto | Self::P720 => 720, |
| 15 | Self::P1080 => 1080, |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | pub fn capture_width(self) -> u32 { |
| 20 | self.capture_height() * 16 / 9 |
| 21 | } |
| 22 | |
| 23 | /// Highest sensor height we may open before downscaling to the capture ceiling. |
| 24 | pub fn largest_sensor_height(self) -> u32 { |
| 25 | match self { |
| 26 | Self::Auto | Self::P720 => 1080, |
| 27 | Self::P1080 => 1440, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | pub fn from_meta(value: &str) -> Self { |
| 32 | match value { |
| 33 | "720" => Self::P720, |
| 34 | "1080" => Self::P1080, |
| 35 | _ => Self::Auto, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn to_meta(self) -> &'static str { |
| 40 | match self { |
| 41 | Self::Auto => "auto", |
| 42 | Self::P720 => "720", |
| 43 | Self::P1080 => "1080", |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[cfg(test)] |
| 49 | mod tests { |
| 50 | use super::*; |
| 51 | |
| 52 | #[test] |
| 53 | fn auto_and_720_share_a_720_capture_ceiling() { |
| 54 | assert_eq!(VideoQuality::Auto.capture_height(), 720); |
| 55 | assert_eq!(VideoQuality::P720.capture_height(), 720); |
| 56 | assert_eq!(VideoQuality::P1080.capture_height(), 1080); |
| 57 | assert_eq!(VideoQuality::Auto.capture_width(), 1280); |
| 58 | assert_eq!(VideoQuality::P1080.capture_width(), 1920); |
| 59 | assert_eq!(VideoQuality::Auto.largest_sensor_height(), 1080); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn meta_round_trips() { |
| 64 | for quality in [VideoQuality::Auto, VideoQuality::P720, VideoQuality::P1080] { |
| 65 | assert_eq!(VideoQuality::from_meta(quality.to_meta()), quality); |
| 66 | } |
| 67 | assert_eq!(VideoQuality::from_meta(""), VideoQuality::Auto); |
| 68 | assert_eq!(VideoQuality::from_meta("garbage"), VideoQuality::Auto); |
| 69 | } |
| 70 | } |