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