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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/mediaconstraintsinterface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "api/peerconnectioninterface.h" |
| 14 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 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 | } |
oprypin | 803dc29 | 2017-02-01 01:55:59 -0800 | [diff] [blame] | 73 | } // namespace |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 74 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | namespace webrtc { |
| 76 | |
| 77 | const char MediaConstraintsInterface::kValueTrue[] = "true"; |
| 78 | const char MediaConstraintsInterface::kValueFalse[] = "false"; |
| 79 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 80 | // Constraints declared as static members in mediastreaminterface.h |
| 81 | // Specified by draft-alvestrand-constraints-resolution-00b |
| 82 | const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio"; |
| 83 | const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio"; |
| 84 | const char MediaConstraintsInterface::kMaxWidth[] = "maxWidth"; |
| 85 | const char MediaConstraintsInterface::kMinWidth[] = "minWidth"; |
| 86 | const char MediaConstraintsInterface::kMaxHeight[] = "maxHeight"; |
| 87 | const char MediaConstraintsInterface::kMinHeight[] = "minHeight"; |
| 88 | const char MediaConstraintsInterface::kMaxFrameRate[] = "maxFrameRate"; |
| 89 | const char MediaConstraintsInterface::kMinFrameRate[] = "minFrameRate"; |
| 90 | |
| 91 | // Audio constraints. |
| 92 | const char MediaConstraintsInterface::kEchoCancellation[] = |
tommi | 39b3100 | 2015-06-23 09:50:47 -0700 | [diff] [blame] | 93 | "echoCancellation"; |
Tommi | 70c7fe1 | 2015-06-15 09:14:03 +0200 | [diff] [blame] | 94 | const char MediaConstraintsInterface::kGoogEchoCancellation[] = |
| 95 | "googEchoCancellation"; |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 96 | const char MediaConstraintsInterface::kExtendedFilterEchoCancellation[] = |
| 97 | "googEchoCancellation2"; |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 98 | const char MediaConstraintsInterface::kDAEchoCancellation[] = |
| 99 | "googDAEchoCancellation"; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 100 | const char MediaConstraintsInterface::kAutoGainControl[] = |
| 101 | "googAutoGainControl"; |
| 102 | const char MediaConstraintsInterface::kExperimentalAutoGainControl[] = |
| 103 | "googAutoGainControl2"; |
| 104 | const char MediaConstraintsInterface::kNoiseSuppression[] = |
| 105 | "googNoiseSuppression"; |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 106 | const char MediaConstraintsInterface::kExperimentalNoiseSuppression[] = |
| 107 | "googNoiseSuppression2"; |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 108 | const char MediaConstraintsInterface::kIntelligibilityEnhancer[] = |
| 109 | "intelligibilityEnhancer"; |
peah | a3333bf | 2016-06-30 00:02:34 -0700 | [diff] [blame] | 110 | const char MediaConstraintsInterface::kLevelControl[] = "levelControl"; |
aleloi | e33c5d9 | 2016-10-20 01:53:27 -0700 | [diff] [blame] | 111 | const char MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS[] = |
| 112 | "levelControlInitialPeakLevelDBFS"; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 113 | const char MediaConstraintsInterface::kHighpassFilter[] = |
| 114 | "googHighpassFilter"; |
| 115 | const char MediaConstraintsInterface::kTypingNoiseDetection[] = |
| 116 | "googTypingNoiseDetection"; |
| 117 | const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring"; |
minyue | ba41428 | 2016-12-11 02:17:52 -0800 | [diff] [blame] | 118 | const char MediaConstraintsInterface::kAudioNetworkAdaptorConfig[] = |
| 119 | "googAudioNetworkAdaptorConfig"; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 120 | |
| 121 | // Google-specific constraint keys for a local video source (getUserMedia). |
| 122 | const char MediaConstraintsInterface::kNoiseReduction[] = "googNoiseReduction"; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 123 | |
wu@webrtc.org | 1481491 | 2014-04-02 23:25:15 +0000 | [diff] [blame] | 124 | // Constraint keys for CreateOffer / CreateAnswer defined in W3C specification. |
| 125 | const char MediaConstraintsInterface::kOfferToReceiveAudio[] = |
| 126 | "OfferToReceiveAudio"; |
| 127 | const char MediaConstraintsInterface::kOfferToReceiveVideo[] = |
| 128 | "OfferToReceiveVideo"; |
| 129 | const char MediaConstraintsInterface::kVoiceActivityDetection[] = |
| 130 | "VoiceActivityDetection"; |
| 131 | const char MediaConstraintsInterface::kIceRestart[] = |
| 132 | "IceRestart"; |
| 133 | // Google specific constraint for BUNDLE enable/disable. |
| 134 | const char MediaConstraintsInterface::kUseRtpMux[] = |
| 135 | "googUseRtpMUX"; |
| 136 | |
| 137 | // Below constraints should be used during PeerConnection construction. |
| 138 | const char MediaConstraintsInterface::kEnableDtlsSrtp[] = |
| 139 | "DtlsSrtpKeyAgreement"; |
| 140 | const char MediaConstraintsInterface::kEnableRtpDataChannels[] = |
| 141 | "RtpDataChannels"; |
| 142 | // Google-specific constraint keys. |
| 143 | const char MediaConstraintsInterface::kEnableDscp[] = "googDscp"; |
| 144 | const char MediaConstraintsInterface::kEnableIPv6[] = "googIPv6"; |
| 145 | const char MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate[] = |
| 146 | "googSuspendBelowMinBitrate"; |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 147 | const char MediaConstraintsInterface::kCombinedAudioVideoBwe[] = |
| 148 | "googCombinedAudioVideoBwe"; |
henrike@webrtc.org | dce3feb | 2014-03-26 01:17:30 +0000 | [diff] [blame] | 149 | const char MediaConstraintsInterface::kScreencastMinBitrate[] = |
| 150 | "googScreencastMinBitrate"; |
henrike@webrtc.org | b0ecc1c | 2014-03-26 22:44:28 +0000 | [diff] [blame] | 151 | // TODO(ronghuawu): Remove once cpu overuse detection is stable. |
| 152 | const char MediaConstraintsInterface::kCpuOveruseDetection[] = |
| 153 | "googCpuOveruseDetection"; |
buildbot@webrtc.org | 44a317a | 2014-06-17 07:49:15 +0000 | [diff] [blame] | 154 | const char MediaConstraintsInterface::kPayloadPadding[] = "googPayloadPadding"; |
| 155 | |
henrike@webrtc.org | dce3feb | 2014-03-26 01:17:30 +0000 | [diff] [blame] | 156 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | // Set |value| to the value associated with the first appearance of |key|, or |
| 158 | // return false if |key| is not found. |
| 159 | bool MediaConstraintsInterface::Constraints::FindFirst( |
| 160 | const std::string& key, std::string* value) const { |
| 161 | for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) { |
| 162 | if (iter->key == key) { |
| 163 | *value = iter->value; |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | bool FindConstraint(const MediaConstraintsInterface* constraints, |
| 171 | const std::string& key, bool* value, |
| 172 | size_t* mandatory_constraints) { |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 173 | return ::FindConstraint<bool>(constraints, key, value, mandatory_constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 174 | } |
| 175 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 176 | bool FindConstraint(const MediaConstraintsInterface* constraints, |
| 177 | const std::string& key, |
| 178 | int* value, |
| 179 | size_t* mandatory_constraints) { |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 180 | return ::FindConstraint<int>(constraints, key, value, mandatory_constraints); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void CopyConstraintsIntoRtcConfiguration( |
| 184 | const MediaConstraintsInterface* constraints, |
| 185 | PeerConnectionInterface::RTCConfiguration* configuration) { |
| 186 | // Copy info from constraints into configuration, if present. |
| 187 | if (!constraints) { |
| 188 | return; |
| 189 | } |
| 190 | |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 191 | bool enable_ipv6; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 192 | if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6, |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 193 | &enable_ipv6, nullptr)) { |
| 194 | configuration->disable_ipv6 = !enable_ipv6; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 195 | } |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 196 | FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp, |
| 197 | &configuration->media_config.enable_dscp, nullptr); |
| 198 | FindConstraint( |
| 199 | constraints, MediaConstraintsInterface::kCpuOveruseDetection, |
| 200 | &configuration->media_config.video.enable_cpu_overuse_detection, nullptr); |
| 201 | FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels, |
| 202 | &configuration->enable_rtp_data_channel, nullptr); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 203 | // Find Suspend Below Min Bitrate constraint. |
nisse | c36b31b | 2016-04-11 23:25:29 -0700 | [diff] [blame] | 204 | FindConstraint(constraints, |
| 205 | MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate, |
| 206 | &configuration->media_config.video.suspend_below_min_bitrate, |
| 207 | nullptr); |
deadbeef | fe0fd41 | 2017-01-13 11:47:56 -0800 | [diff] [blame] | 208 | ConstraintToOptional<int>(constraints, |
| 209 | MediaConstraintsInterface::kScreencastMinBitrate, |
| 210 | &configuration->screencast_min_bitrate); |
| 211 | ConstraintToOptional<bool>(constraints, |
| 212 | MediaConstraintsInterface::kCombinedAudioVideoBwe, |
| 213 | &configuration->combined_audio_video_bwe); |
| 214 | ConstraintToOptional<bool>(constraints, |
| 215 | MediaConstraintsInterface::kEnableDtlsSrtp, |
| 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 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 273 | } |
| 274 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 275 | } // namespace webrtc |