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 (!FindConstraint(constraints, key, &string_value, mandatory_constraints)) { |
| 35 return false; |
| 36 } |
| 37 return rtc::FromString(string_value, value); |
| 38 } |
| 39 |
| 40 // Specialization for std::string, since a string doesn't need conversion. |
| 41 template <> |
| 42 bool FindConstraint(const webrtc::MediaConstraintsInterface* constraints, |
| 43 const std::string& key, |
| 44 std::string* value, |
| 45 size_t* mandatory_constraints) { |
| 46 if (!constraints) { |
| 47 return false; |
| 48 } |
| 49 if (constraints->GetMandatory().FindFirst(key, value)) { |
| 50 if (mandatory_constraints) { |
| 51 ++*mandatory_constraints; |
| 52 } |
| 53 return true; |
| 54 } |
| 55 if (constraints->GetOptional().FindFirst(key, value)) { |
| 56 return true; |
| 57 } |
| 58 return false; |
| 59 } |
| 60 |
| 61 // Converts a constraint (mandatory takes precedence over optional) to an |
| 62 // rtc::Optional. |
| 63 template <typename T> |
| 64 void ConstraintToOptional(const webrtc::MediaConstraintsInterface* constraints, |
| 65 const std::string& key, |
| 66 rtc::Optional<T>* value_out) { |
| 67 T value; |
| 68 bool present = FindConstraint<T>(constraints, key, &value, nullptr); |
| 69 if (present) { |
| 70 *value_out = rtc::Optional<T>(value); |
| 71 } |
| 72 } |
| 73 } |
| 74 |
16 namespace webrtc { | 75 namespace webrtc { |
17 | 76 |
18 const char MediaConstraintsInterface::kValueTrue[] = "true"; | 77 const char MediaConstraintsInterface::kValueTrue[] = "true"; |
19 const char MediaConstraintsInterface::kValueFalse[] = "false"; | 78 const char MediaConstraintsInterface::kValueFalse[] = "false"; |
20 | 79 |
21 // Constraints declared as static members in mediastreaminterface.h | 80 // Constraints declared as static members in mediastreaminterface.h |
22 // Specified by draft-alvestrand-constraints-resolution-00b | 81 // Specified by draft-alvestrand-constraints-resolution-00b |
23 const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio"; | 82 const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio"; |
24 const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio"; | 83 const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio"; |
25 const char MediaConstraintsInterface::kMaxWidth[] = "maxWidth"; | 84 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 { | 160 const std::string& key, std::string* value) const { |
102 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) { | 161 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) { |
103 if (iter->key == key) { | 162 if (iter->key == key) { |
104 *value = iter->value; | 163 *value = iter->value; |
105 return true; | 164 return true; |
106 } | 165 } |
107 } | 166 } |
108 return false; | 167 return false; |
109 } | 168 } |
110 | 169 |
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, | 170 bool FindConstraint(const MediaConstraintsInterface* constraints, |
122 const std::string& key, bool* value, | 171 const std::string& key, bool* value, |
123 size_t* mandatory_constraints) { | 172 size_t* mandatory_constraints) { |
124 std::string string_value; | 173 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 } | 174 } |
139 | 175 |
140 // As above, but for integers. | |
141 bool FindConstraint(const MediaConstraintsInterface* constraints, | 176 bool FindConstraint(const MediaConstraintsInterface* constraints, |
142 const std::string& key, | 177 const std::string& key, |
143 int* value, | 178 int* value, |
144 size_t* mandatory_constraints) { | 179 size_t* mandatory_constraints) { |
145 std::string string_value; | 180 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 } | 181 } |
180 | 182 |
181 void CopyConstraintsIntoRtcConfiguration( | 183 void CopyConstraintsIntoRtcConfiguration( |
182 const MediaConstraintsInterface* constraints, | 184 const MediaConstraintsInterface* constraints, |
183 PeerConnectionInterface::RTCConfiguration* configuration) { | 185 PeerConnectionInterface::RTCConfiguration* configuration) { |
184 // Copy info from constraints into configuration, if present. | 186 // Copy info from constraints into configuration, if present. |
185 if (!constraints) { | 187 if (!constraints) { |
186 return; | 188 return; |
187 } | 189 } |
188 | 190 |
189 bool enable_ipv6; | 191 bool enable_ipv6; |
190 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, | 192 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, |
191 &enable_ipv6, nullptr)) { | 193 &enable_ipv6, nullptr)) { |
192 configuration->disable_ipv6 = !enable_ipv6; | 194 configuration->disable_ipv6 = !enable_ipv6; |
193 } | 195 } |
194 FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp, | 196 FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp, |
195 &configuration->media_config.enable_dscp, nullptr); | 197 &configuration->media_config.enable_dscp, nullptr); |
196 FindConstraint( | 198 FindConstraint( |
197 constraints, MediaConstraintsInterface::kCpuOveruseDetection, | 199 constraints, MediaConstraintsInterface::kCpuOveruseDetection, |
198 &configuration->media_config.video.enable_cpu_overuse_detection, nullptr); | 200 &configuration->media_config.video.enable_cpu_overuse_detection, nullptr); |
199 FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels, | 201 FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels, |
200 &configuration->enable_rtp_data_channel, nullptr); | 202 &configuration->enable_rtp_data_channel, nullptr); |
201 // Find Suspend Below Min Bitrate constraint. | 203 // Find Suspend Below Min Bitrate constraint. |
202 FindConstraint(constraints, | 204 FindConstraint(constraints, |
203 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate, | 205 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate, |
204 &configuration->media_config.video.suspend_below_min_bitrate, | 206 &configuration->media_config.video.suspend_below_min_bitrate, |
205 nullptr); | 207 nullptr); |
206 ConstraintToOptionalInt(constraints, | 208 ConstraintToOptional<int>(constraints, |
207 MediaConstraintsInterface::kScreencastMinBitrate, | 209 MediaConstraintsInterface::kScreencastMinBitrate, |
208 &configuration->screencast_min_bitrate); | 210 &configuration->screencast_min_bitrate); |
209 ConstraintToOptionalBool(constraints, | 211 ConstraintToOptional<bool>(constraints, |
210 MediaConstraintsInterface::kCombinedAudioVideoBwe, | 212 MediaConstraintsInterface::kCombinedAudioVideoBwe, |
211 &configuration->combined_audio_video_bwe); | 213 &configuration->combined_audio_video_bwe); |
212 ConstraintToOptionalBool(constraints, | 214 ConstraintToOptional<bool>(constraints, |
213 MediaConstraintsInterface::kEnableDtlsSrtp, | 215 MediaConstraintsInterface::kEnableDtlsSrtp, |
214 &configuration->enable_dtls_srtp); | 216 &configuration->enable_dtls_srtp); |
| 217 } |
| 218 |
| 219 void CopyConstraintsIntoAudioOptions( |
| 220 const MediaConstraintsInterface* constraints, |
| 221 cricket::AudioOptions* options) { |
| 222 if (!constraints) { |
| 223 return; |
| 224 } |
| 225 |
| 226 ConstraintToOptional<bool>(constraints, |
| 227 MediaConstraintsInterface::kGoogEchoCancellation, |
| 228 &options->echo_cancellation); |
| 229 ConstraintToOptional<bool>( |
| 230 constraints, MediaConstraintsInterface::kExtendedFilterEchoCancellation, |
| 231 &options->extended_filter_aec); |
| 232 ConstraintToOptional<bool>(constraints, |
| 233 MediaConstraintsInterface::kDAEchoCancellation, |
| 234 &options->delay_agnostic_aec); |
| 235 ConstraintToOptional<bool>(constraints, |
| 236 MediaConstraintsInterface::kAutoGainControl, |
| 237 &options->auto_gain_control); |
| 238 ConstraintToOptional<bool>( |
| 239 constraints, MediaConstraintsInterface::kExperimentalAutoGainControl, |
| 240 &options->experimental_agc); |
| 241 ConstraintToOptional<bool>(constraints, |
| 242 MediaConstraintsInterface::kNoiseSuppression, |
| 243 &options->noise_suppression); |
| 244 ConstraintToOptional<bool>( |
| 245 constraints, MediaConstraintsInterface::kExperimentalNoiseSuppression, |
| 246 &options->experimental_ns); |
| 247 ConstraintToOptional<bool>( |
| 248 constraints, MediaConstraintsInterface::kIntelligibilityEnhancer, |
| 249 &options->intelligibility_enhancer); |
| 250 ConstraintToOptional<bool>(constraints, |
| 251 MediaConstraintsInterface::kLevelControl, |
| 252 &options->level_control); |
| 253 ConstraintToOptional<bool>(constraints, |
| 254 MediaConstraintsInterface::kHighpassFilter, |
| 255 &options->highpass_filter); |
| 256 ConstraintToOptional<bool>(constraints, |
| 257 MediaConstraintsInterface::kTypingNoiseDetection, |
| 258 &options->typing_detection); |
| 259 ConstraintToOptional<bool>(constraints, |
| 260 MediaConstraintsInterface::kAudioMirroring, |
| 261 &options->stereo_swapping); |
| 262 ConstraintToOptional<float>( |
| 263 constraints, MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS, |
| 264 &options->level_control_initial_peak_level_dbfs); |
| 265 ConstraintToOptional<std::string>( |
| 266 constraints, MediaConstraintsInterface::kAudioNetworkAdaptorConfig, |
| 267 &options->audio_network_adaptor_config); |
| 268 // When |kAudioNetworkAdaptorConfig| is defined, it both means that audio |
| 269 // network adaptor is desired, and provides the config string. |
| 270 if (options->audio_network_adaptor_config) { |
| 271 options->audio_network_adaptor = rtc::Optional<bool>(true); |
| 272 } |
215 } | 273 } |
216 | 274 |
217 } // namespace webrtc | 275 } // namespace webrtc |
OLD | NEW |