henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 11 | #include "api/media_constraints_interface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 14 | #include "api/peer_connection_interface.h" |
| 15 | #include "media/base/media_config.h" |
| 16 | #include "rtc_base/string_encode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | // Find the highest-priority instance of the T-valued constraint named by |
| 21 | // |key| and return its value as |value|. |constraints| can be null. |
| 22 | // If |mandatory_constraints| is non-null, it is incremented if the key appears |
| 23 | // among the mandatory constraints. |
| 24 | // Returns true if the key was found and has a valid value for type T. |
| 25 | // If the key appears multiple times as an optional constraint, appearances |
| 26 | // after the first are ignored. |
| 27 | // Note: Because this uses FindFirst, repeated optional constraints whose |
| 28 | // first instance has an unrecognized value are not handled precisely in |
| 29 | // accordance with the specification. |
| 30 | template <typename T> |
| 31 | bool FindConstraint(const webrtc::MediaConstraintsInterface* constraints, |
| 32 | const std::string& key, |
| 33 | T* value, |
| 34 | size_t* mandatory_constraints) { |
| 35 | std::string string_value; |
| 36 | if (!FindConstraint(constraints, key, &string_value, mandatory_constraints)) { |
| 37 | return false; |
| 38 | } |
| 39 | return rtc::FromString(string_value, value); |
| 40 | } |
| 41 | |
| 42 | // Specialization for std::string, since a string doesn't need conversion. |
| 43 | template <> |
| 44 | bool FindConstraint(const webrtc::MediaConstraintsInterface* constraints, |
| 45 | const std::string& key, |
| 46 | std::string* value, |
| 47 | size_t* mandatory_constraints) { |
| 48 | if (!constraints) { |
| 49 | return false; |
| 50 | } |
| 51 | if (constraints->GetMandatory().FindFirst(key, value)) { |
| 52 | if (mandatory_constraints) { |
| 53 | ++*mandatory_constraints; |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | if (constraints->GetOptional().FindFirst(key, value)) { |
| 58 | return true; |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // Converts a constraint (mandatory takes precedence over optional) to an |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 64 | // absl::optional. |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 65 | template <typename T> |
| 66 | void ConstraintToOptional(const webrtc::MediaConstraintsInterface* constraints, |
| 67 | const std::string& key, |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 68 | absl::optional<T>* value_out) { |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 69 | T value; |
| 70 | bool present = FindConstraint<T>(constraints, key, &value, nullptr); |
| 71 | if (present) { |
Oskar Sundbom | 0e1d798 | 2017-11-16 10:52:42 +0100 | [diff] [blame] | 72 | *value_out = value; |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
oprypin | 803dc29 | 2017-02-01 01:55:59 -0800 | [diff] [blame] | 75 | } // namespace |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 76 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | namespace webrtc { |
| 78 | |
| 79 | const char MediaConstraintsInterface::kValueTrue[] = "true"; |
| 80 | const char MediaConstraintsInterface::kValueFalse[] = "false"; |
| 81 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 82 | // Constraints declared as static members in mediastreaminterface.h |
| 83 | // Specified by draft-alvestrand-constraints-resolution-00b |
| 84 | const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio"; |
| 85 | const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio"; |
| 86 | const char MediaConstraintsInterface::kMaxWidth[] = "maxWidth"; |
| 87 | const char MediaConstraintsInterface::kMinWidth[] = "minWidth"; |
| 88 | const char MediaConstraintsInterface::kMaxHeight[] = "maxHeight"; |
| 89 | const char MediaConstraintsInterface::kMinHeight[] = "minHeight"; |
| 90 | const char MediaConstraintsInterface::kMaxFrameRate[] = "maxFrameRate"; |
| 91 | const char MediaConstraintsInterface::kMinFrameRate[] = "minFrameRate"; |
| 92 | |
| 93 | // Audio constraints. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | const char MediaConstraintsInterface::kEchoCancellation[] = "echoCancellation"; |
Tommi | 70c7fe1 | 2015-06-15 09:14:03 +0200 | [diff] [blame] | 95 | const char MediaConstraintsInterface::kGoogEchoCancellation[] = |
| 96 | "googEchoCancellation"; |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 97 | const char MediaConstraintsInterface::kExtendedFilterEchoCancellation[] = |
| 98 | "googEchoCancellation2"; |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 99 | const char MediaConstraintsInterface::kDAEchoCancellation[] = |
| 100 | "googDAEchoCancellation"; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 101 | const char MediaConstraintsInterface::kAutoGainControl[] = |
| 102 | "googAutoGainControl"; |
| 103 | const char MediaConstraintsInterface::kExperimentalAutoGainControl[] = |
| 104 | "googAutoGainControl2"; |
| 105 | const char MediaConstraintsInterface::kNoiseSuppression[] = |
| 106 | "googNoiseSuppression"; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 107 | const char MediaConstraintsInterface::kExperimentalNoiseSuppression[] = |
| 108 | "googNoiseSuppression2"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 109 | const char MediaConstraintsInterface::kHighpassFilter[] = "googHighpassFilter"; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 110 | const char MediaConstraintsInterface::kTypingNoiseDetection[] = |
| 111 | "googTypingNoiseDetection"; |
| 112 | const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring"; |
minyue | ba41428 | 2016-12-11 02:17:52 -0800 | [diff] [blame] | 113 | const char MediaConstraintsInterface::kAudioNetworkAdaptorConfig[] = |
| 114 | "googAudioNetworkAdaptorConfig"; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 115 | |
| 116 | // Google-specific constraint keys for a local video source (getUserMedia). |
| 117 | const char MediaConstraintsInterface::kNoiseReduction[] = "googNoiseReduction"; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 118 | |
wu@webrtc.org | 1481491 | 2014-04-02 23:25:15 +0000 | [diff] [blame] | 119 | // Constraint keys for CreateOffer / CreateAnswer defined in W3C specification. |
| 120 | const char MediaConstraintsInterface::kOfferToReceiveAudio[] = |
| 121 | "OfferToReceiveAudio"; |
| 122 | const char MediaConstraintsInterface::kOfferToReceiveVideo[] = |
| 123 | "OfferToReceiveVideo"; |
| 124 | const char MediaConstraintsInterface::kVoiceActivityDetection[] = |
| 125 | "VoiceActivityDetection"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 126 | const char MediaConstraintsInterface::kIceRestart[] = "IceRestart"; |
wu@webrtc.org | 1481491 | 2014-04-02 23:25:15 +0000 | [diff] [blame] | 127 | // Google specific constraint for BUNDLE enable/disable. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | const char MediaConstraintsInterface::kUseRtpMux[] = "googUseRtpMUX"; |
wu@webrtc.org | 1481491 | 2014-04-02 23:25:15 +0000 | [diff] [blame] | 129 | |
| 130 | // Below constraints should be used during PeerConnection construction. |
| 131 | const char MediaConstraintsInterface::kEnableDtlsSrtp[] = |
| 132 | "DtlsSrtpKeyAgreement"; |
| 133 | const char MediaConstraintsInterface::kEnableRtpDataChannels[] = |
| 134 | "RtpDataChannels"; |
| 135 | // Google-specific constraint keys. |
| 136 | const char MediaConstraintsInterface::kEnableDscp[] = "googDscp"; |
| 137 | const char MediaConstraintsInterface::kEnableIPv6[] = "googIPv6"; |
| 138 | const char MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate[] = |
| 139 | "googSuspendBelowMinBitrate"; |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 140 | const char MediaConstraintsInterface::kCombinedAudioVideoBwe[] = |
| 141 | "googCombinedAudioVideoBwe"; |
henrike@webrtc.org | dce3feb | 2014-03-26 01:17:30 +0000 | [diff] [blame] | 142 | const char MediaConstraintsInterface::kScreencastMinBitrate[] = |
| 143 | "googScreencastMinBitrate"; |
henrike@webrtc.org | b0ecc1c | 2014-03-26 22:44:28 +0000 | [diff] [blame] | 144 | // TODO(ronghuawu): Remove once cpu overuse detection is stable. |
| 145 | const char MediaConstraintsInterface::kCpuOveruseDetection[] = |
| 146 | "googCpuOveruseDetection"; |
buildbot@webrtc.org | 44a317a | 2014-06-17 07:49:15 +0000 | [diff] [blame] | 147 | const char MediaConstraintsInterface::kPayloadPadding[] = "googPayloadPadding"; |
| 148 | |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 149 | const char MediaConstraintsInterface::kNumSimulcastLayers[] = |
| 150 | "googNumSimulcastLayers"; |
| 151 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 152 | // Set |value| to the value associated with the first appearance of |key|, or |
| 153 | // return false if |key| is not found. |
| 154 | bool MediaConstraintsInterface::Constraints::FindFirst( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 155 | const std::string& key, |
| 156 | std::string* value) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) { |
| 158 | if (iter->key == key) { |
| 159 | *value = iter->value; |
| 160 | return true; |
| 161 | } |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | bool FindConstraint(const MediaConstraintsInterface* constraints, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 167 | const std::string& key, |
| 168 | bool* value, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | size_t* mandatory_constraints) { |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 170 | return ::FindConstraint<bool>(constraints, key, value, mandatory_constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 171 | } |
| 172 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 173 | bool FindConstraint(const MediaConstraintsInterface* constraints, |
| 174 | const std::string& key, |
| 175 | int* value, |
| 176 | size_t* mandatory_constraints) { |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 177 | return ::FindConstraint<int>(constraints, key, value, mandatory_constraints); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void CopyConstraintsIntoRtcConfiguration( |
| 181 | const MediaConstraintsInterface* constraints, |
| 182 | PeerConnectionInterface::RTCConfiguration* configuration) { |
| 183 | // Copy info from constraints into configuration, if present. |
| 184 | if (!constraints) { |
| 185 | return; |
| 186 | } |
| 187 | |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 188 | bool enable_ipv6; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 189 | if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 190 | &enable_ipv6, nullptr)) { |
| 191 | configuration->disable_ipv6 = !enable_ipv6; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 192 | } |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 193 | FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp, |
| 194 | &configuration->media_config.enable_dscp, nullptr); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 195 | FindConstraint(constraints, MediaConstraintsInterface::kCpuOveruseDetection, |
| 196 | &configuration->media_config.video.enable_cpu_adaptation, |
| 197 | nullptr); |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 198 | FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels, |
| 199 | &configuration->enable_rtp_data_channel, nullptr); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 200 | // Find Suspend Below Min Bitrate constraint. |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 201 | FindConstraint(constraints, |
| 202 | MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate, |
| 203 | &configuration->media_config.video.suspend_below_min_bitrate, |
| 204 | nullptr); |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 205 | ConstraintToOptional<int>(constraints, |
| 206 | MediaConstraintsInterface::kScreencastMinBitrate, |
| 207 | &configuration->screencast_min_bitrate); |
| 208 | ConstraintToOptional<bool>(constraints, |
| 209 | MediaConstraintsInterface::kCombinedAudioVideoBwe, |
| 210 | &configuration->combined_audio_video_bwe); |
| 211 | ConstraintToOptional<bool>(constraints, |
| 212 | MediaConstraintsInterface::kEnableDtlsSrtp, |
| 213 | &configuration->enable_dtls_srtp); |
| 214 | } |
| 215 | |
| 216 | void CopyConstraintsIntoAudioOptions( |
| 217 | const MediaConstraintsInterface* constraints, |
| 218 | cricket::AudioOptions* options) { |
| 219 | if (!constraints) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | ConstraintToOptional<bool>(constraints, |
| 224 | MediaConstraintsInterface::kGoogEchoCancellation, |
| 225 | &options->echo_cancellation); |
| 226 | ConstraintToOptional<bool>( |
| 227 | constraints, MediaConstraintsInterface::kExtendedFilterEchoCancellation, |
| 228 | &options->extended_filter_aec); |
| 229 | ConstraintToOptional<bool>(constraints, |
| 230 | MediaConstraintsInterface::kDAEchoCancellation, |
| 231 | &options->delay_agnostic_aec); |
| 232 | ConstraintToOptional<bool>(constraints, |
| 233 | MediaConstraintsInterface::kAutoGainControl, |
| 234 | &options->auto_gain_control); |
| 235 | ConstraintToOptional<bool>( |
| 236 | constraints, MediaConstraintsInterface::kExperimentalAutoGainControl, |
| 237 | &options->experimental_agc); |
| 238 | ConstraintToOptional<bool>(constraints, |
| 239 | MediaConstraintsInterface::kNoiseSuppression, |
| 240 | &options->noise_suppression); |
| 241 | ConstraintToOptional<bool>( |
| 242 | constraints, MediaConstraintsInterface::kExperimentalNoiseSuppression, |
| 243 | &options->experimental_ns); |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 244 | ConstraintToOptional<bool>(constraints, |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 245 | MediaConstraintsInterface::kHighpassFilter, |
| 246 | &options->highpass_filter); |
| 247 | ConstraintToOptional<bool>(constraints, |
| 248 | MediaConstraintsInterface::kTypingNoiseDetection, |
| 249 | &options->typing_detection); |
| 250 | ConstraintToOptional<bool>(constraints, |
| 251 | MediaConstraintsInterface::kAudioMirroring, |
| 252 | &options->stereo_swapping); |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 253 | ConstraintToOptional<std::string>( |
| 254 | constraints, MediaConstraintsInterface::kAudioNetworkAdaptorConfig, |
| 255 | &options->audio_network_adaptor_config); |
| 256 | // When |kAudioNetworkAdaptorConfig| is defined, it both means that audio |
| 257 | // network adaptor is desired, and provides the config string. |
| 258 | if (options->audio_network_adaptor_config) { |
Oskar Sundbom | 0e1d798 | 2017-11-16 10:52:42 +0100 | [diff] [blame] | 259 | options->audio_network_adaptor = true; |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 260 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Niels Möller | f06f923 | 2018-08-07 12:32:18 +0200 | [diff] [blame] | 263 | bool CopyConstraintsIntoOfferAnswerOptions( |
| 264 | const MediaConstraintsInterface* constraints, |
| 265 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
| 266 | if (!constraints) { |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | bool value = false; |
| 271 | size_t mandatory_constraints_satisfied = 0; |
| 272 | |
| 273 | if (FindConstraint(constraints, |
| 274 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 275 | &mandatory_constraints_satisfied)) { |
| 276 | offer_answer_options->offer_to_receive_audio = |
| 277 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 278 | kOfferToReceiveMediaTrue |
| 279 | : 0; |
| 280 | } |
| 281 | |
| 282 | if (FindConstraint(constraints, |
| 283 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 284 | &mandatory_constraints_satisfied)) { |
| 285 | offer_answer_options->offer_to_receive_video = |
| 286 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 287 | kOfferToReceiveMediaTrue |
| 288 | : 0; |
| 289 | } |
| 290 | if (FindConstraint(constraints, |
| 291 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 292 | &mandatory_constraints_satisfied)) { |
| 293 | offer_answer_options->voice_activity_detection = value; |
| 294 | } |
| 295 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 296 | &mandatory_constraints_satisfied)) { |
| 297 | offer_answer_options->use_rtp_mux = value; |
| 298 | } |
| 299 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 300 | &value, &mandatory_constraints_satisfied)) { |
| 301 | offer_answer_options->ice_restart = value; |
| 302 | } |
| 303 | |
Jonas Oreland | fc1acd2 | 2018-08-24 10:58:37 +0200 | [diff] [blame] | 304 | int layers; |
| 305 | if (FindConstraint(constraints, |
| 306 | MediaConstraintsInterface::kNumSimulcastLayers, |
| 307 | &layers, &mandatory_constraints_satisfied)) { |
| 308 | offer_answer_options->num_simulcast_layers = layers; |
| 309 | } |
| 310 | |
Niels Möller | f06f923 | 2018-08-07 12:32:18 +0200 | [diff] [blame] | 311 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 312 | } |
| 313 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 314 | } // namespace webrtc |