xref: /webrtc/constraints/src/enumerations.rs (revision c19675fc)
1 /// The directions that the camera can face, as seen from the user's perspective.
2 ///
3 /// # Note
4 /// The enumeration is not exhaustive and merely provides a list of known values.
5 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
6 pub enum FacingMode {
7     /// The source is facing toward the user (a self-view camera).
8     User,
9 
10     /// The source is facing away from the user (viewing the environment).
11     Environment,
12 
13     /// The source is facing to the left of the user.
14     Left,
15 
16     /// The source is facing to the right of the user.
17     Right,
18 }
19 
20 impl FacingMode {
21     pub fn user() -> String {
22         Self::User.to_string()
23     }
24 
25     pub fn environment() -> String {
26         Self::Environment.to_string()
27     }
28 
29     pub fn left() -> String {
30         Self::Left.to_string()
31     }
32 
33     pub fn right() -> String {
34         Self::Right.to_string()
35     }
36 }
37 
38 impl std::fmt::Display for FacingMode {
39     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40         match self {
41             Self::User => f.write_str("user"),
42             Self::Environment => f.write_str("environment"),
43             Self::Left => f.write_str("left"),
44             Self::Right => f.write_str("right"),
45         }
46     }
47 }
48 
49 /// The means by which the resolution can be derived by the client.
50 ///
51 /// # Note
52 /// The enumeration is not exhaustive and merely provides a list of known values.
53 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
54 pub enum ResizeMode {
55     /// This resolution and frame rate is offered by the camera, its driver, or the OS.
56     ///
57     /// # Note
58     /// The user agent MAY report this value to disguise concurrent use,
59     /// but only when the camera is in use in another browsing context.
60     ///
61     /// # Important
62     /// This value is a possible finger-printing surface.
63     None,
64 
65     /// This resolution is downscaled and/or cropped from a higher camera resolution by the user agent,
66     /// or its frame rate is decimated by the User Agent.
67     ///
68     /// # Important
69     /// The media MUST NOT be upscaled, stretched or have fake data created that did not occur in the input source.
70     CropAndScale,
71 }
72 
73 impl ResizeMode {
74     pub fn none() -> String {
75         Self::None.to_string()
76     }
77 
78     pub fn crop_and_scale() -> String {
79         Self::CropAndScale.to_string()
80     }
81 }
82 
83 impl std::fmt::Display for ResizeMode {
84     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85         match self {
86             Self::None => f.write_str("none"),
87             Self::CropAndScale => f.write_str("crop-and-scale"),
88         }
89     }
90 }
91 
92 #[cfg(test)]
93 mod tests {
94     use super::*;
95 
96     mod facing_mode {
97         use super::*;
98 
99         #[test]
100         fn to_string() {
101             assert_eq!(FacingMode::User.to_string(), "user");
102             assert_eq!(FacingMode::Environment.to_string(), "environment");
103             assert_eq!(FacingMode::Left.to_string(), "left");
104             assert_eq!(FacingMode::Right.to_string(), "right");
105 
106             assert_eq!(FacingMode::user(), "user");
107             assert_eq!(FacingMode::environment(), "environment");
108             assert_eq!(FacingMode::left(), "left");
109             assert_eq!(FacingMode::right(), "right");
110         }
111     }
112 
113     mod resize_mode {
114         use super::*;
115 
116         #[test]
117         fn to_string() {
118             assert_eq!(ResizeMode::None.to_string(), "none");
119             assert_eq!(ResizeMode::CropAndScale.to_string(), "crop-and-scale");
120 
121             assert_eq!(ResizeMode::none(), "none");
122             assert_eq!(ResizeMode::crop_and_scale(), "crop-and-scale");
123         }
124     }
125 }
126