| 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 | pub fn capture_height(self) -> u32 { |
| 12 | match self { |
| 13 | Self::Auto | Self::P720 => 720, |
| 14 | Self::P1080 => 1080, |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | pub fn capture_width(self) -> u32 { |
| 19 | self.capture_height() * 16 / 9 |
| 20 | } |
| 21 | |
| 22 | /// Highest sensor height we may open before downscaling to the capture ceiling. |
| 23 | pub fn largest_sensor_height(self) -> u32 { |
| 24 | match self { |
| 25 | Self::Auto | Self::P720 => 1080, |
| 26 | Self::P1080 => 1440, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | pub fn from_meta(value: &str) -> Self { |
| 31 | match value { |
| 32 | "720" => Self::P720, |
| 33 | "1080" => Self::P1080, |
| 34 | _ => Self::Auto, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub fn to_meta(self) -> &'static str { |
| 39 | match self { |
| 40 | Self::Auto => "auto", |
| 41 | Self::P720 => "720", |
| 42 | Self::P1080 => "1080", |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[cfg(test)] |
| 48 | mod tests { |
| 49 | use super::*; |
| 50 | |
| 51 | #[test] |
| 52 | fn auto_and_720_share_a_720_capture_ceiling() { |
| 53 | assert_eq!(VideoQuality::Auto.capture_height(), 720); |
| 54 | assert_eq!(VideoQuality::P720.capture_height(), 720); |
| 55 | assert_eq!(VideoQuality::P1080.capture_height(), 1080); |
| 56 | assert_eq!(VideoQuality::Auto.capture_width(), 1280); |
| 57 | assert_eq!(VideoQuality::P1080.capture_width(), 1920); |
| 58 | assert_eq!(VideoQuality::Auto.largest_sensor_height(), 1080); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn meta_round_trips() { |
| 63 | for quality in [VideoQuality::Auto, VideoQuality::P720, VideoQuality::P1080] { |
| 64 | assert_eq!(VideoQuality::from_meta(quality.to_meta()), quality); |
| 65 | } |
| 66 | assert_eq!(VideoQuality::from_meta(""), VideoQuality::Auto); |
| 67 | assert_eq!(VideoQuality::from_meta("garbage"), VideoQuality::Auto); |
| 68 | } |
| 69 | } |