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
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
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>
FindConstraint(const MediaConstraints * constraints,const std::string & key,T * value,size_t * mandatory_constraints)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 <>
FindConstraint(const MediaConstraints * constraints,const std::string & key,std::string * value,size_t * mandatory_constraints)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
FindConstraint(const MediaConstraints * constraints,const std::string & key,bool * value,size_t * mandatory_constraints)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
FindConstraint(const MediaConstraints * constraints,const std::string & key,int * value,size_t * mandatory_constraints)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>
ConstraintToOptional(const MediaConstraints * constraints,const std::string & key,absl::optional<T> * value_out)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";
97 const char MediaConstraints::kAutoGainControl[] = "googAutoGainControl";
98 const char MediaConstraints::kExperimentalAutoGainControl[] =
99 "googAutoGainControl2";
100 const char MediaConstraints::kNoiseSuppression[] = "googNoiseSuppression";
101 const char MediaConstraints::kExperimentalNoiseSuppression[] =
102 "googNoiseSuppression2";
103 const char MediaConstraints::kHighpassFilter[] = "googHighpassFilter";
104 const char MediaConstraints::kTypingNoiseDetection[] =
105 "googTypingNoiseDetection";
106 const char MediaConstraints::kAudioMirroring[] = "googAudioMirroring";
107 const char MediaConstraints::kAudioNetworkAdaptorConfig[] =
108 "googAudioNetworkAdaptorConfig";
109
110 // Constraint keys for CreateOffer / CreateAnswer defined in W3C specification.
111 const char MediaConstraints::kOfferToReceiveAudio[] = "OfferToReceiveAudio";
112 const char MediaConstraints::kOfferToReceiveVideo[] = "OfferToReceiveVideo";
113 const char MediaConstraints::kVoiceActivityDetection[] =
114 "VoiceActivityDetection";
115 const char MediaConstraints::kIceRestart[] = "IceRestart";
116 // Google specific constraint for BUNDLE enable/disable.
117 const char MediaConstraints::kUseRtpMux[] = "googUseRtpMUX";
118
119 // Below constraints should be used during PeerConnection construction.
120 const char MediaConstraints::kEnableDtlsSrtp[] = "DtlsSrtpKeyAgreement";
121 const char MediaConstraints::kEnableRtpDataChannels[] = "RtpDataChannels";
122 // Google-specific constraint keys.
123 const char MediaConstraints::kEnableDscp[] = "googDscp";
124 const char MediaConstraints::kEnableIPv6[] = "googIPv6";
125 const char MediaConstraints::kEnableVideoSuspendBelowMinBitrate[] =
126 "googSuspendBelowMinBitrate";
127 const char MediaConstraints::kCombinedAudioVideoBwe[] =
128 "googCombinedAudioVideoBwe";
129 const char MediaConstraints::kScreencastMinBitrate[] =
130 "googScreencastMinBitrate";
131 // TODO(ronghuawu): Remove once cpu overuse detection is stable.
132 const char MediaConstraints::kCpuOveruseDetection[] = "googCpuOveruseDetection";
133
134 const char MediaConstraints::kRawPacketizationForVideoEnabled[] =
135 "googRawPacketizationForVideoEnabled";
136
137 const char MediaConstraints::kNumSimulcastLayers[] = "googNumSimulcastLayers";
138
139 // Set |value| to the value associated with the first appearance of |key|, or
140 // return false if |key| is not found.
FindFirst(const std::string & key,std::string * value) const141 bool MediaConstraints::Constraints::FindFirst(const std::string& key,
142 std::string* value) const {
143 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) {
144 if (iter->key == key) {
145 *value = iter->value;
146 return true;
147 }
148 }
149 return false;
150 }
151
CopyConstraintsIntoRtcConfiguration(const MediaConstraints * constraints,PeerConnectionInterface::RTCConfiguration * configuration)152 void CopyConstraintsIntoRtcConfiguration(
153 const MediaConstraints* constraints,
154 PeerConnectionInterface::RTCConfiguration* configuration) {
155 // Copy info from constraints into configuration, if present.
156 if (!constraints) {
157 return;
158 }
159
160 bool enable_ipv6;
161 if (FindConstraint(constraints, MediaConstraints::kEnableIPv6, &enable_ipv6,
162 nullptr)) {
163 configuration->disable_ipv6 = !enable_ipv6;
164 }
165 FindConstraint(constraints, MediaConstraints::kEnableDscp,
166 &configuration->media_config.enable_dscp, nullptr);
167 FindConstraint(constraints, MediaConstraints::kCpuOveruseDetection,
168 &configuration->media_config.video.enable_cpu_adaptation,
169 nullptr);
170 FindConstraint(constraints, MediaConstraints::kEnableRtpDataChannels,
171 &configuration->enable_rtp_data_channel, nullptr);
172 // Find Suspend Below Min Bitrate constraint.
173 FindConstraint(
174 constraints, MediaConstraints::kEnableVideoSuspendBelowMinBitrate,
175 &configuration->media_config.video.suspend_below_min_bitrate, nullptr);
176 ConstraintToOptional<int>(constraints,
177 MediaConstraints::kScreencastMinBitrate,
178 &configuration->screencast_min_bitrate);
179 ConstraintToOptional<bool>(constraints,
180 MediaConstraints::kCombinedAudioVideoBwe,
181 &configuration->combined_audio_video_bwe);
182 ConstraintToOptional<bool>(constraints, MediaConstraints::kEnableDtlsSrtp,
183 &configuration->enable_dtls_srtp);
184 }
185
CopyConstraintsIntoAudioOptions(const MediaConstraints * constraints,cricket::AudioOptions * options)186 void CopyConstraintsIntoAudioOptions(const MediaConstraints* constraints,
187 cricket::AudioOptions* options) {
188 if (!constraints) {
189 return;
190 }
191
192 ConstraintToOptional<bool>(constraints,
193 MediaConstraints::kGoogEchoCancellation,
194 &options->echo_cancellation);
195 ConstraintToOptional<bool>(constraints, MediaConstraints::kAutoGainControl,
196 &options->auto_gain_control);
197 ConstraintToOptional<bool>(constraints,
198 MediaConstraints::kExperimentalAutoGainControl,
199 &options->experimental_agc);
200 ConstraintToOptional<bool>(constraints, MediaConstraints::kNoiseSuppression,
201 &options->noise_suppression);
202 ConstraintToOptional<bool>(constraints,
203 MediaConstraints::kExperimentalNoiseSuppression,
204 &options->experimental_ns);
205 ConstraintToOptional<bool>(constraints, MediaConstraints::kHighpassFilter,
206 &options->highpass_filter);
207 ConstraintToOptional<bool>(constraints,
208 MediaConstraints::kTypingNoiseDetection,
209 &options->typing_detection);
210 ConstraintToOptional<bool>(constraints, MediaConstraints::kAudioMirroring,
211 &options->stereo_swapping);
212 ConstraintToOptional<std::string>(
213 constraints, MediaConstraints::kAudioNetworkAdaptorConfig,
214 &options->audio_network_adaptor_config);
215 // When |kAudioNetworkAdaptorConfig| is defined, it both means that audio
216 // network adaptor is desired, and provides the config string.
217 if (options->audio_network_adaptor_config) {
218 options->audio_network_adaptor = true;
219 }
220 }
221
CopyConstraintsIntoOfferAnswerOptions(const MediaConstraints * constraints,PeerConnectionInterface::RTCOfferAnswerOptions * offer_answer_options)222 bool CopyConstraintsIntoOfferAnswerOptions(
223 const MediaConstraints* constraints,
224 PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) {
225 if (!constraints) {
226 return true;
227 }
228
229 bool value = false;
230 size_t mandatory_constraints_satisfied = 0;
231
232 if (FindConstraint(constraints, MediaConstraints::kOfferToReceiveAudio,
233 &value, &mandatory_constraints_satisfied)) {
234 offer_answer_options->offer_to_receive_audio =
235 value ? PeerConnectionInterface::RTCOfferAnswerOptions::
236 kOfferToReceiveMediaTrue
237 : 0;
238 }
239
240 if (FindConstraint(constraints, MediaConstraints::kOfferToReceiveVideo,
241 &value, &mandatory_constraints_satisfied)) {
242 offer_answer_options->offer_to_receive_video =
243 value ? PeerConnectionInterface::RTCOfferAnswerOptions::
244 kOfferToReceiveMediaTrue
245 : 0;
246 }
247 if (FindConstraint(constraints, MediaConstraints::kVoiceActivityDetection,
248 &value, &mandatory_constraints_satisfied)) {
249 offer_answer_options->voice_activity_detection = value;
250 }
251 if (FindConstraint(constraints, MediaConstraints::kUseRtpMux, &value,
252 &mandatory_constraints_satisfied)) {
253 offer_answer_options->use_rtp_mux = value;
254 }
255 if (FindConstraint(constraints, MediaConstraints::kIceRestart, &value,
256 &mandatory_constraints_satisfied)) {
257 offer_answer_options->ice_restart = value;
258 }
259
260 if (FindConstraint(constraints,
261 MediaConstraints::kRawPacketizationForVideoEnabled, &value,
262 &mandatory_constraints_satisfied)) {
263 offer_answer_options->raw_packetization_for_video = value;
264 }
265
266 int layers;
267 if (FindConstraint(constraints, MediaConstraints::kNumSimulcastLayers,
268 &layers, &mandatory_constraints_satisfied)) {
269 offer_answer_options->num_simulcast_layers = layers;
270 }
271
272 return mandatory_constraints_satisfied == constraints->GetMandatory().size();
273 }
274
275 } // namespace webrtc
276