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