OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/api/mediaconstraintsinterface.h" | 11 #include "webrtc/api/mediaconstraintsinterface.h" |
12 | 12 |
13 #include "webrtc/api/peerconnectioninterface.h" | 13 #include "webrtc/api/peerconnectioninterface.h" |
14 #include "webrtc/base/stringencode.h" | 14 #include "webrtc/base/stringencode.h" |
15 | 15 |
16 namespace { | |
17 | |
18 // Find the highest-priority instance of the T-valued constraint named by | |
19 // |key| and return its value as |value|. |constraints| can be null. | |
20 // If |mandatory_constraints| is non-null, it is incremented if the key appears | |
21 // among the mandatory constraints. | |
22 // Returns true if the key was found and has a valid value for type T. | |
23 // If the key appears multiple times as an optional constraint, appearances | |
24 // after the first are ignored. | |
25 // Note: Because this uses FindFirst, repeated optional constraints whose | |
26 // first instance has an unrecognized value are not handled precisely in | |
27 // accordance with the specification. | |
28 template <typename T> | |
29 bool FindConstraint(const webrtc::MediaConstraintsInterface* constraints, | |
30 const std::string& key, | |
31 T* value, | |
32 size_t* mandatory_constraints) { | |
33 std::string string_value; | |
34 if (!constraints) { | |
35 return false; | |
36 } | |
37 if (constraints->GetMandatory().FindFirst(key, &string_value)) { | |
38 if (mandatory_constraints) { | |
39 ++*mandatory_constraints; | |
40 } | |
41 return rtc::FromString(string_value, value); | |
42 } | |
43 if (constraints->GetOptional().FindFirst(key, &string_value)) { | |
44 return rtc::FromString(string_value, value); | |
45 } | |
pthatcher1
2017/01/12 23:15:26
Why doesn't this use FindConstraint with a string
Taylor Brandstetter
2017/01/12 23:38:06
Great idea, not sure why I didn't notice that.
| |
46 return false; | |
47 } | |
48 | |
49 // Specialization for std::string, since a string doesn't need conversion. | |
50 template <> | |
51 bool FindConstraint(const webrtc::MediaConstraintsInterface* constraints, | |
52 const std::string& key, | |
53 std::string* value, | |
54 size_t* mandatory_constraints) { | |
55 if (!constraints) { | |
56 return false; | |
57 } | |
58 if (constraints->GetMandatory().FindFirst(key, value)) { | |
59 if (mandatory_constraints) { | |
60 ++*mandatory_constraints; | |
61 } | |
62 return true; | |
63 } | |
64 if (constraints->GetOptional().FindFirst(key, value)) { | |
65 return true; | |
66 } | |
67 return false; | |
68 } | |
69 | |
70 // Converts a constraint (mandatory takes precedence over optional) to an | |
71 // rtc::Optional. | |
72 template <typename T> | |
73 void ConstraintToOptional(const webrtc::MediaConstraintsInterface* constraints, | |
74 const std::string& key, | |
75 rtc::Optional<T>* value_out) { | |
76 T value; | |
77 bool present = FindConstraint<T>(constraints, key, &value, nullptr); | |
78 if (present) { | |
79 *value_out = rtc::Optional<T>(value); | |
80 } | |
81 } | |
82 } | |
83 | |
16 namespace webrtc { | 84 namespace webrtc { |
17 | 85 |
18 const char MediaConstraintsInterface::kValueTrue[] = "true"; | 86 const char MediaConstraintsInterface::kValueTrue[] = "true"; |
19 const char MediaConstraintsInterface::kValueFalse[] = "false"; | 87 const char MediaConstraintsInterface::kValueFalse[] = "false"; |
20 | 88 |
21 // Constraints declared as static members in mediastreaminterface.h | 89 // Constraints declared as static members in mediastreaminterface.h |
22 // Specified by draft-alvestrand-constraints-resolution-00b | 90 // Specified by draft-alvestrand-constraints-resolution-00b |
23 const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio"; | 91 const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio"; |
24 const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio"; | 92 const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio"; |
25 const char MediaConstraintsInterface::kMaxWidth[] = "maxWidth"; | 93 const char MediaConstraintsInterface::kMaxWidth[] = "maxWidth"; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 const std::string& key, std::string* value) const { | 169 const std::string& key, std::string* value) const { |
102 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) { | 170 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) { |
103 if (iter->key == key) { | 171 if (iter->key == key) { |
104 *value = iter->value; | 172 *value = iter->value; |
105 return true; | 173 return true; |
106 } | 174 } |
107 } | 175 } |
108 return false; | 176 return false; |
109 } | 177 } |
110 | 178 |
111 // Find the highest-priority instance of the boolean-valued constraint) named by | |
112 // |key| and return its value as |value|. |constraints| can be null. | |
113 // If |mandatory_constraints| is non-null, it is incremented if the key appears | |
114 // among the mandatory constraints. | |
115 // Returns true if the key was found and has a valid boolean value. | |
116 // If the key appears multiple times as an optional constraint, appearances | |
117 // after the first are ignored. | |
118 // Note: Because this uses FindFirst, repeated optional constraints whose | |
119 // first instance has an unrecognized value are not handled precisely in | |
120 // accordance with the specification. | |
121 bool FindConstraint(const MediaConstraintsInterface* constraints, | 179 bool FindConstraint(const MediaConstraintsInterface* constraints, |
122 const std::string& key, bool* value, | 180 const std::string& key, bool* value, |
123 size_t* mandatory_constraints) { | 181 size_t* mandatory_constraints) { |
124 std::string string_value; | 182 return ::FindConstraint<bool>(constraints, key, value, mandatory_constraints); |
125 if (!constraints) { | |
126 return false; | |
127 } | |
128 if (constraints->GetMandatory().FindFirst(key, &string_value)) { | |
129 if (mandatory_constraints) { | |
130 ++*mandatory_constraints; | |
131 } | |
132 return rtc::FromString(string_value, value); | |
133 } | |
134 if (constraints->GetOptional().FindFirst(key, &string_value)) { | |
135 return rtc::FromString(string_value, value); | |
136 } | |
137 return false; | |
138 } | 183 } |
139 | 184 |
140 // As above, but for integers. | |
141 bool FindConstraint(const MediaConstraintsInterface* constraints, | 185 bool FindConstraint(const MediaConstraintsInterface* constraints, |
142 const std::string& key, | 186 const std::string& key, |
143 int* value, | 187 int* value, |
144 size_t* mandatory_constraints) { | 188 size_t* mandatory_constraints) { |
145 std::string string_value; | 189 return ::FindConstraint<int>(constraints, key, value, mandatory_constraints); |
146 if (!constraints) { | |
147 return false; | |
148 } | |
149 if (constraints->GetMandatory().FindFirst(key, &string_value)) { | |
150 if (mandatory_constraints) { | |
151 ++*mandatory_constraints; | |
152 } | |
153 return rtc::FromString(string_value, value); | |
154 } | |
155 if (constraints->GetOptional().FindFirst(key, &string_value)) { | |
156 return rtc::FromString(string_value, value); | |
157 } | |
158 return false; | |
159 } | |
160 | |
161 void ConstraintToOptionalBool(const MediaConstraintsInterface* constraints, | |
162 const std::string& key, | |
163 rtc::Optional<bool>* value_out) { | |
164 bool value; | |
165 bool present = FindConstraint(constraints, key, &value, nullptr); | |
166 if (present) { | |
167 *value_out = rtc::Optional<bool>(value); | |
168 } | |
169 } | |
170 | |
171 void ConstraintToOptionalInt(const MediaConstraintsInterface* constraints, | |
172 const std::string& key, | |
173 rtc::Optional<int>* value_out) { | |
174 int value; | |
175 bool present = FindConstraint(constraints, key, &value, nullptr); | |
176 if (present) { | |
177 *value_out = rtc::Optional<int>(value); | |
178 } | |
179 } | 190 } |
180 | 191 |
181 void CopyConstraintsIntoRtcConfiguration( | 192 void CopyConstraintsIntoRtcConfiguration( |
182 const MediaConstraintsInterface* constraints, | 193 const MediaConstraintsInterface* constraints, |
183 PeerConnectionInterface::RTCConfiguration* configuration) { | 194 PeerConnectionInterface::RTCConfiguration* configuration) { |
184 // Copy info from constraints into configuration, if present. | 195 // Copy info from constraints into configuration, if present. |
185 if (!constraints) { | 196 if (!constraints) { |
186 return; | 197 return; |
187 } | 198 } |
188 | 199 |
189 bool enable_ipv6; | 200 bool enable_ipv6; |
190 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, | 201 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, |
191 &enable_ipv6, nullptr)) { | 202 &enable_ipv6, nullptr)) { |
192 configuration->disable_ipv6 = !enable_ipv6; | 203 configuration->disable_ipv6 = !enable_ipv6; |
193 } | 204 } |
194 FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp, | 205 FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp, |
195 &configuration->media_config.enable_dscp, nullptr); | 206 &configuration->media_config.enable_dscp, nullptr); |
196 FindConstraint( | 207 FindConstraint( |
197 constraints, MediaConstraintsInterface::kCpuOveruseDetection, | 208 constraints, MediaConstraintsInterface::kCpuOveruseDetection, |
198 &configuration->media_config.video.enable_cpu_overuse_detection, nullptr); | 209 &configuration->media_config.video.enable_cpu_overuse_detection, nullptr); |
199 FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels, | 210 FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels, |
200 &configuration->enable_rtp_data_channel, nullptr); | 211 &configuration->enable_rtp_data_channel, nullptr); |
201 // Find Suspend Below Min Bitrate constraint. | 212 // Find Suspend Below Min Bitrate constraint. |
202 FindConstraint(constraints, | 213 FindConstraint(constraints, |
203 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate, | 214 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate, |
204 &configuration->media_config.video.suspend_below_min_bitrate, | 215 &configuration->media_config.video.suspend_below_min_bitrate, |
205 nullptr); | 216 nullptr); |
206 ConstraintToOptionalInt(constraints, | 217 ConstraintToOptional<int>(constraints, |
207 MediaConstraintsInterface::kScreencastMinBitrate, | 218 MediaConstraintsInterface::kScreencastMinBitrate, |
208 &configuration->screencast_min_bitrate); | 219 &configuration->screencast_min_bitrate); |
209 ConstraintToOptionalBool(constraints, | 220 ConstraintToOptional<bool>(constraints, |
210 MediaConstraintsInterface::kCombinedAudioVideoBwe, | 221 MediaConstraintsInterface::kCombinedAudioVideoBwe, |
211 &configuration->combined_audio_video_bwe); | 222 &configuration->combined_audio_video_bwe); |
212 ConstraintToOptionalBool(constraints, | 223 ConstraintToOptional<bool>(constraints, |
213 MediaConstraintsInterface::kEnableDtlsSrtp, | 224 MediaConstraintsInterface::kEnableDtlsSrtp, |
214 &configuration->enable_dtls_srtp); | 225 &configuration->enable_dtls_srtp); |
226 } | |
227 | |
228 void CopyConstraintsIntoAudioOptions( | |
229 const MediaConstraintsInterface* constraints, | |
230 cricket::AudioOptions* options) { | |
231 if (!constraints) { | |
232 return; | |
233 } | |
234 | |
235 ConstraintToOptional<bool>(constraints, | |
236 MediaConstraintsInterface::kGoogEchoCancellation, | |
237 &options->echo_cancellation); | |
238 ConstraintToOptional<bool>( | |
239 constraints, MediaConstraintsInterface::kExtendedFilterEchoCancellation, | |
240 &options->extended_filter_aec); | |
241 ConstraintToOptional<bool>(constraints, | |
242 MediaConstraintsInterface::kDAEchoCancellation, | |
243 &options->delay_agnostic_aec); | |
244 ConstraintToOptional<bool>(constraints, | |
245 MediaConstraintsInterface::kAutoGainControl, | |
246 &options->auto_gain_control); | |
247 ConstraintToOptional<bool>( | |
248 constraints, MediaConstraintsInterface::kExperimentalAutoGainControl, | |
249 &options->experimental_agc); | |
250 ConstraintToOptional<bool>(constraints, | |
251 MediaConstraintsInterface::kNoiseSuppression, | |
252 &options->noise_suppression); | |
253 ConstraintToOptional<bool>( | |
254 constraints, MediaConstraintsInterface::kExperimentalNoiseSuppression, | |
255 &options->experimental_ns); | |
256 ConstraintToOptional<bool>( | |
257 constraints, MediaConstraintsInterface::kIntelligibilityEnhancer, | |
258 &options->intelligibility_enhancer); | |
259 ConstraintToOptional<bool>(constraints, | |
260 MediaConstraintsInterface::kLevelControl, | |
261 &options->level_control); | |
262 ConstraintToOptional<bool>(constraints, | |
263 MediaConstraintsInterface::kHighpassFilter, | |
264 &options->highpass_filter); | |
265 ConstraintToOptional<bool>(constraints, | |
266 MediaConstraintsInterface::kTypingNoiseDetection, | |
267 &options->typing_detection); | |
268 ConstraintToOptional<bool>(constraints, | |
269 MediaConstraintsInterface::kAudioMirroring, | |
270 &options->stereo_swapping); | |
271 ConstraintToOptional<float>( | |
272 constraints, MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS, | |
273 &options->level_control_initial_peak_level_dbfs); | |
274 ConstraintToOptional<std::string>( | |
275 constraints, MediaConstraintsInterface::kAudioNetworkAdaptorConfig, | |
276 &options->audio_network_adaptor_config); | |
277 // When |kAudioNetworkAdaptorConfig| is defined, it both means that audio | |
278 // network adaptor is desired, and provides the config string. | |
279 if (options->audio_network_adaptor_config) { | |
280 options->audio_network_adaptor = rtc::Optional<bool>(true); | |
281 } | |
215 } | 282 } |
216 | 283 |
217 } // namespace webrtc | 284 } // namespace webrtc |
OLD | NEW |