xref: /webrtc/constraints/examples/json.rs (revision ce739920)
1 use std::iter::FromIterator;
2 
3 use webrtc_constraints::{
4     algorithms::{
5         select_settings_candidates, ClosestToIdealPolicy, DeviceInformationExposureMode,
6         TieBreakingPolicy,
7     },
8     property::all::name::*,
9     MediaTrackConstraints, MediaTrackSettings, MediaTrackSupportedConstraints,
10 };
11 
main()12 fn main() {
13     let supported_constraints =
14         MediaTrackSupportedConstraints::from_iter(vec![&DEVICE_ID, &HEIGHT, &WIDTH, &RESIZE_MODE]);
15 
16     // Deserialize possible settings from JSON:
17     let possible_settings: Vec<MediaTrackSettings> = {
18         let json = serde_json::json!([
19             { "deviceId": "480p", "width": 720, "height": 480, "resizeMode": "crop-and-scale" },
20             { "deviceId": "720p", "width": 1280, "height": 720, "resizeMode": "crop-and-scale" },
21             { "deviceId": "1080p", "width": 1920, "height": 1080, "resizeMode": "none" },
22             { "deviceId": "1440p", "width": 2560, "height": 1440, "resizeMode": "none" },
23             { "deviceId": "2160p", "width": 3840, "height": 2160, "resizeMode": "none" },
24         ]);
25         serde_json::from_value(json).unwrap()
26     };
27 
28     // Deserialize constraints from JSON:
29     let constraints: MediaTrackConstraints = {
30         let json = serde_json::json!({
31             "width": {
32                 "max": 2560,
33             },
34             "height": {
35                 "max": 1440,
36             },
37             // Unsupported constraint, which should thus get ignored:
38             "frameRate": {
39                 "exact": 30.0
40             },
41             // Ideal resize-mode:
42             "resizeMode": "none",
43             "advanced": [
44                 // The first advanced constraint set of "exact 800p" does not match
45                 // any candidate and should thus get ignored by the algorithm:
46                 { "height": 800 },
47                 // The second advanced constraint set of "no resizing" does match
48                 // candidates and should thus be applied by the algorithm:
49                 { "resizeMode": "none" },
50             ]
51         });
52         serde_json::from_value(json).unwrap()
53     };
54 
55     // Resolve bare values to proper constraints:
56     let resolved_constraints = constraints.into_resolved();
57 
58     // Sanitize constraints, removing empty and unsupported constraints:
59     let sanitized_constraints = resolved_constraints.into_sanitized(&supported_constraints);
60 
61     let candidates = select_settings_candidates(
62         &possible_settings,
63         &sanitized_constraints,
64         DeviceInformationExposureMode::Protected,
65     )
66     .unwrap();
67 
68     // Specify a tie-breaking policy
69     //
70     // A couple of basic policies are provided batteries-included,
71     // but for more sophisticated needs you can implement your own `TieBreakingPolicy`:
72     let tie_breaking_policy =
73         ClosestToIdealPolicy::new(possible_settings[2].clone(), &supported_constraints);
74 
75     let actual = tie_breaking_policy.select_candidate(candidates);
76 
77     let expected = &possible_settings[2];
78 
79     assert_eq!(actual, expected);
80 }
81