Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include "sdk/media_constraints.h" |
| 12 | |
| 13 | #include "absl/types/optional.h" |
| 14 | #include "api/peer_connection_interface.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | namespace { |
| 18 | |
| 19 | // Find the highest-priority instance of the T-valued constraint named by |
Artem Titov | d7ac581 | 2021-07-27 10:23:39 | [diff] [blame] | 20 | // `key` and return its value as `value`. `constraints` can be null. |
| 21 | // If `mandatory_constraints` is non-null, it is incremented if the key appears |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 22 | // among the mandatory constraints. |
| 23 | // Returns true if the key was found and has a valid value for type T. |
| 24 | // If the key appears multiple times as an optional constraint, appearances |
| 25 | // after the first are ignored. |
| 26 | // Note: Because this uses FindFirst, repeated optional constraints whose |
| 27 | // first instance has an unrecognized value are not handled precisely in |
| 28 | // accordance with the specification. |
| 29 | template <typename T> |
| 30 | bool FindConstraint(const MediaConstraints* constraints, |
| 31 | const std::string& key, |
| 32 | T* value, |
| 33 | size_t* mandatory_constraints) { |
| 34 | std::string string_value; |
| 35 | if (!FindConstraint(constraints, key, &string_value, mandatory_constraints)) { |
| 36 | return false; |
| 37 | } |
| 38 | return rtc::FromString(string_value, value); |
| 39 | } |
| 40 | |
| 41 | // Specialization for std::string, since a string doesn't need conversion. |
| 42 | template <> |
| 43 | bool FindConstraint(const MediaConstraints* constraints, |
| 44 | const std::string& key, |
| 45 | std::string* value, |
| 46 | size_t* mandatory_constraints) { |
| 47 | if (!constraints) { |
| 48 | return false; |
| 49 | } |
| 50 | if (constraints->GetMandatory().FindFirst(key, value)) { |
| 51 | if (mandatory_constraints) { |
| 52 | ++*mandatory_constraints; |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | if (constraints->GetOptional().FindFirst(key, value)) { |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | bool FindConstraint(const MediaConstraints* constraints, |
| 63 | const std::string& key, |
| 64 | bool* value, |
| 65 | size_t* mandatory_constraints) { |
| 66 | return FindConstraint<bool>(constraints, key, value, mandatory_constraints); |
| 67 | } |
| 68 | |
| 69 | bool FindConstraint(const MediaConstraints* constraints, |
| 70 | const std::string& key, |
| 71 | int* value, |
| 72 | size_t* mandatory_constraints) { |
| 73 | return FindConstraint<int>(constraints, key, value, mandatory_constraints); |
| 74 | } |
| 75 | |
| 76 | // Converts a constraint (mandatory takes precedence over optional) to an |
| 77 | // absl::optional. |
| 78 | template <typename T> |
| 79 | void ConstraintToOptional(const MediaConstraints* constraints, |
| 80 | const std::string& key, |
| 81 | absl::optional<T>* value_out) { |
| 82 | T value; |
| 83 | bool present = FindConstraint<T>(constraints, key, &value, nullptr); |
| 84 | if (present) { |
| 85 | *value_out = value; |
| 86 | } |
| 87 | } |
| 88 | } // namespace |
| 89 | |
| 90 | const char MediaConstraints::kValueTrue[] = "true"; |
| 91 | const char MediaConstraints::kValueFalse[] = "false"; |
| 92 | |
| 93 | // Constraints declared as static members in mediastreaminterface.h |
| 94 | |
| 95 | // Audio constraints. |
| 96 | const char MediaConstraints::kGoogEchoCancellation[] = "googEchoCancellation"; |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 97 | const char MediaConstraints::kAutoGainControl[] = "googAutoGainControl"; |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 98 | const char MediaConstraints::kNoiseSuppression[] = "googNoiseSuppression"; |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 99 | const char MediaConstraints::kHighpassFilter[] = "googHighpassFilter"; |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 100 | const char MediaConstraints::kAudioMirroring[] = "googAudioMirroring"; |
| 101 | const char MediaConstraints::kAudioNetworkAdaptorConfig[] = |
| 102 | "googAudioNetworkAdaptorConfig"; |
Xavier Lepaul | 1e12f2a | 2022-01-13 16:06:26 | [diff] [blame] | 103 | const char MediaConstraints::kInitAudioRecordingOnSend[] = |
| 104 | "InitAudioRecordingOnSend"; |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 105 | |
| 106 | // Constraint keys for CreateOffer / CreateAnswer defined in W3C specification. |
| 107 | const char MediaConstraints::kOfferToReceiveAudio[] = "OfferToReceiveAudio"; |
| 108 | const char MediaConstraints::kOfferToReceiveVideo[] = "OfferToReceiveVideo"; |
| 109 | const char MediaConstraints::kVoiceActivityDetection[] = |
| 110 | "VoiceActivityDetection"; |
| 111 | const char MediaConstraints::kIceRestart[] = "IceRestart"; |
| 112 | // Google specific constraint for BUNDLE enable/disable. |
| 113 | const char MediaConstraints::kUseRtpMux[] = "googUseRtpMUX"; |
| 114 | |
| 115 | // Below constraints should be used during PeerConnection construction. |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 116 | // Google-specific constraint keys. |
| 117 | const char MediaConstraints::kEnableDscp[] = "googDscp"; |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 118 | const char MediaConstraints::kEnableVideoSuspendBelowMinBitrate[] = |
| 119 | "googSuspendBelowMinBitrate"; |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 120 | const char MediaConstraints::kScreencastMinBitrate[] = |
| 121 | "googScreencastMinBitrate"; |
| 122 | // TODO(ronghuawu): Remove once cpu overuse detection is stable. |
| 123 | const char MediaConstraints::kCpuOveruseDetection[] = "googCpuOveruseDetection"; |
| 124 | |
Mirta Dvornicic | 479a3c0 | 2019-06-04 13:38:50 | [diff] [blame] | 125 | const char MediaConstraints::kRawPacketizationForVideoEnabled[] = |
| 126 | "googRawPacketizationForVideoEnabled"; |
| 127 | |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 128 | const char MediaConstraints::kNumSimulcastLayers[] = "googNumSimulcastLayers"; |
| 129 | |
Artem Titov | d7ac581 | 2021-07-27 10:23:39 | [diff] [blame] | 130 | // Set `value` to the value associated with the first appearance of `key`, or |
| 131 | // return false if `key` is not found. |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 132 | bool MediaConstraints::Constraints::FindFirst(const std::string& key, |
| 133 | std::string* value) const { |
| 134 | for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) { |
| 135 | if (iter->key == key) { |
| 136 | *value = iter->value; |
| 137 | return true; |
| 138 | } |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | void CopyConstraintsIntoRtcConfiguration( |
| 144 | const MediaConstraints* constraints, |
| 145 | PeerConnectionInterface::RTCConfiguration* configuration) { |
| 146 | // Copy info from constraints into configuration, if present. |
| 147 | if (!constraints) { |
| 148 | return; |
| 149 | } |
| 150 | |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 151 | FindConstraint(constraints, MediaConstraints::kEnableDscp, |
| 152 | &configuration->media_config.enable_dscp, nullptr); |
| 153 | FindConstraint(constraints, MediaConstraints::kCpuOveruseDetection, |
| 154 | &configuration->media_config.video.enable_cpu_adaptation, |
| 155 | nullptr); |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 156 | // Find Suspend Below Min Bitrate constraint. |
| 157 | FindConstraint( |
| 158 | constraints, MediaConstraints::kEnableVideoSuspendBelowMinBitrate, |
| 159 | &configuration->media_config.video.suspend_below_min_bitrate, nullptr); |
| 160 | ConstraintToOptional<int>(constraints, |
| 161 | MediaConstraints::kScreencastMinBitrate, |
| 162 | &configuration->screencast_min_bitrate); |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void CopyConstraintsIntoAudioOptions(const MediaConstraints* constraints, |
| 166 | cricket::AudioOptions* options) { |
| 167 | if (!constraints) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | ConstraintToOptional<bool>(constraints, |
| 172 | MediaConstraints::kGoogEchoCancellation, |
| 173 | &options->echo_cancellation); |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 174 | ConstraintToOptional<bool>(constraints, MediaConstraints::kAutoGainControl, |
| 175 | &options->auto_gain_control); |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 176 | ConstraintToOptional<bool>(constraints, MediaConstraints::kNoiseSuppression, |
| 177 | &options->noise_suppression); |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 178 | ConstraintToOptional<bool>(constraints, MediaConstraints::kHighpassFilter, |
| 179 | &options->highpass_filter); |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 180 | ConstraintToOptional<bool>(constraints, MediaConstraints::kAudioMirroring, |
| 181 | &options->stereo_swapping); |
| 182 | ConstraintToOptional<std::string>( |
| 183 | constraints, MediaConstraints::kAudioNetworkAdaptorConfig, |
| 184 | &options->audio_network_adaptor_config); |
Artem Titov | d7ac581 | 2021-07-27 10:23:39 | [diff] [blame] | 185 | // When `kAudioNetworkAdaptorConfig` is defined, it both means that audio |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 186 | // network adaptor is desired, and provides the config string. |
| 187 | if (options->audio_network_adaptor_config) { |
| 188 | options->audio_network_adaptor = true; |
| 189 | } |
Xavier Lepaul | 1e12f2a | 2022-01-13 16:06:26 | [diff] [blame] | 190 | ConstraintToOptional<bool>(constraints, |
| 191 | MediaConstraints::kInitAudioRecordingOnSend, |
| 192 | &options->init_recording_on_send); |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | bool CopyConstraintsIntoOfferAnswerOptions( |
| 196 | const MediaConstraints* constraints, |
| 197 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
| 198 | if (!constraints) { |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | bool value = false; |
| 203 | size_t mandatory_constraints_satisfied = 0; |
| 204 | |
| 205 | if (FindConstraint(constraints, MediaConstraints::kOfferToReceiveAudio, |
| 206 | &value, &mandatory_constraints_satisfied)) { |
| 207 | offer_answer_options->offer_to_receive_audio = |
| 208 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 209 | kOfferToReceiveMediaTrue |
| 210 | : 0; |
| 211 | } |
| 212 | |
| 213 | if (FindConstraint(constraints, MediaConstraints::kOfferToReceiveVideo, |
| 214 | &value, &mandatory_constraints_satisfied)) { |
| 215 | offer_answer_options->offer_to_receive_video = |
| 216 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 217 | kOfferToReceiveMediaTrue |
| 218 | : 0; |
| 219 | } |
| 220 | if (FindConstraint(constraints, MediaConstraints::kVoiceActivityDetection, |
| 221 | &value, &mandatory_constraints_satisfied)) { |
| 222 | offer_answer_options->voice_activity_detection = value; |
| 223 | } |
| 224 | if (FindConstraint(constraints, MediaConstraints::kUseRtpMux, &value, |
| 225 | &mandatory_constraints_satisfied)) { |
| 226 | offer_answer_options->use_rtp_mux = value; |
| 227 | } |
| 228 | if (FindConstraint(constraints, MediaConstraints::kIceRestart, &value, |
| 229 | &mandatory_constraints_satisfied)) { |
| 230 | offer_answer_options->ice_restart = value; |
| 231 | } |
| 232 | |
Mirta Dvornicic | 479a3c0 | 2019-06-04 13:38:50 | [diff] [blame] | 233 | if (FindConstraint(constraints, |
| 234 | MediaConstraints::kRawPacketizationForVideoEnabled, &value, |
| 235 | &mandatory_constraints_satisfied)) { |
| 236 | offer_answer_options->raw_packetization_for_video = value; |
| 237 | } |
| 238 | |
Niels Möller | dac03d9 | 2019-02-13 07:52:27 | [diff] [blame] | 239 | int layers; |
| 240 | if (FindConstraint(constraints, MediaConstraints::kNumSimulcastLayers, |
| 241 | &layers, &mandatory_constraints_satisfied)) { |
| 242 | offer_answer_options->num_simulcast_layers = layers; |
| 243 | } |
| 244 | |
| 245 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 246 | } |
| 247 | |
| 248 | } // namespace webrtc |