xref: /webrtc/constraints/examples/macros.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     macros::*,
9     property::all::name::*,
10     settings, MediaTrackSupportedConstraints, ResizeMode,
11 };
12 
main()13 fn main() {
14     let supported_constraints =
15         MediaTrackSupportedConstraints::from_iter(vec![&DEVICE_ID, &HEIGHT, &WIDTH, &RESIZE_MODE]);
16 
17     let possible_settings = vec![
18         settings![
19             &DEVICE_ID => "480p",
20             &HEIGHT => 480,
21             &WIDTH => 720,
22             &RESIZE_MODE => ResizeMode::crop_and_scale(),
23         ],
24         settings![
25             &DEVICE_ID => "720p",
26             &HEIGHT => 720,
27             &WIDTH => 1280,
28             &RESIZE_MODE => ResizeMode::crop_and_scale(),
29         ],
30         settings![
31             &DEVICE_ID => "1080p",
32             &HEIGHT => 1080,
33             &WIDTH => 1920,
34             &RESIZE_MODE => ResizeMode::none(),
35         ],
36         settings![
37             &DEVICE_ID => "1440p",
38             &HEIGHT => 1440,
39             &WIDTH => 2560,
40             &RESIZE_MODE => ResizeMode::none(),
41         ],
42         settings![
43             &DEVICE_ID => "2160p",
44             &HEIGHT => 2160,
45             &WIDTH => 3840,
46             &RESIZE_MODE => ResizeMode::none(),
47         ],
48     ];
49 
50     let constraints = constraints! {
51         mandatory: {
52             &WIDTH => value_range_constraint!{
53                 max: 2560
54             },
55             &HEIGHT => value_range_constraint!{
56                 max: 1440
57             },
58             // Unsupported constraint, which should thus get ignored:
59             &FRAME_RATE => value_range_constraint!{
60                 exact: 30.0
61             },
62         },
63         advanced: [
64             // The first advanced constraint set of "exact 800p" does not match
65             // any candidate and should thus get ignored by the algorithm:
66             {
67                 &HEIGHT => value_range_constraint!{
68                     exact: 800
69                 }
70             },
71             // The second advanced constraint set of "no resizing" does match
72             // candidates and should thus be applied by the algorithm:
73             {
74                 &RESIZE_MODE => value_constraint!{
75                     exact: ResizeMode::none()
76                 }
77             },
78         ]
79     };
80 
81     // Resolve bare values to proper constraints:
82     let resolved_constraints = constraints.into_resolved();
83 
84     // Sanitize constraints, removing empty and unsupported constraints:
85     let sanitized_constraints = resolved_constraints.to_sanitized(&supported_constraints);
86 
87     let candidates = select_settings_candidates(
88         &possible_settings,
89         &sanitized_constraints,
90         DeviceInformationExposureMode::Exposed,
91     )
92     .unwrap();
93 
94     // Specify a tie-breaking policy
95     //
96     // A couple of basic policies are provided batteries-included,
97     // but for more sophisticated needs you can implement your own `TieBreakingPolicy`:
98     let tie_breaking_policy =
99         ClosestToIdealPolicy::new(possible_settings[2].clone(), &supported_constraints);
100 
101     let actual = tie_breaking_policy.select_candidate(candidates);
102 
103     let expected = &possible_settings[2];
104 
105     assert_eq!(actual, expected);
106 }
107