blob: 90a957cfa48c5f38013d50aed9dfc74367d3876e [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/mediaconstraintsinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "api/peerconnectioninterface.h"
14#include "rtc_base/stringencode.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
deadbeeffe0fd412017-01-13 11:47:56 -080016namespace {
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.
28template <typename T>
29bool 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.
41template <>
42bool 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.
63template <typename T>
64void 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) {
Oskar Sundbom0e1d7982017-11-16 10:52:42 +010070 *value_out = value;
deadbeeffe0fd412017-01-13 11:47:56 -080071 }
72}
oprypin803dc292017-02-01 01:55:59 -080073} // namespace
deadbeeffe0fd412017-01-13 11:47:56 -080074
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075namespace webrtc {
76
77const char MediaConstraintsInterface::kValueTrue[] = "true";
78const char MediaConstraintsInterface::kValueFalse[] = "false";
79
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000080// Constraints declared as static members in mediastreaminterface.h
81// Specified by draft-alvestrand-constraints-resolution-00b
82const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio";
83const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio";
84const char MediaConstraintsInterface::kMaxWidth[] = "maxWidth";
85const char MediaConstraintsInterface::kMinWidth[] = "minWidth";
86const char MediaConstraintsInterface::kMaxHeight[] = "maxHeight";
87const char MediaConstraintsInterface::kMinHeight[] = "minHeight";
88const char MediaConstraintsInterface::kMaxFrameRate[] = "maxFrameRate";
89const char MediaConstraintsInterface::kMinFrameRate[] = "minFrameRate";
90
91// Audio constraints.
92const char MediaConstraintsInterface::kEchoCancellation[] =
tommi39b31002015-06-23 09:50:47 -070093 "echoCancellation";
Tommi70c7fe12015-06-15 09:14:03 +020094const char MediaConstraintsInterface::kGoogEchoCancellation[] =
95 "googEchoCancellation";
Henrik Lundin441f6342015-06-09 16:03:13 +020096const char MediaConstraintsInterface::kExtendedFilterEchoCancellation[] =
97 "googEchoCancellation2";
Bjorn Volckerbf395c12015-03-25 22:45:56 +010098const char MediaConstraintsInterface::kDAEchoCancellation[] =
99 "googDAEchoCancellation";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000100const char MediaConstraintsInterface::kAutoGainControl[] =
101 "googAutoGainControl";
102const char MediaConstraintsInterface::kExperimentalAutoGainControl[] =
103 "googAutoGainControl2";
104const char MediaConstraintsInterface::kNoiseSuppression[] =
105 "googNoiseSuppression";
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000106const char MediaConstraintsInterface::kExperimentalNoiseSuppression[] =
107 "googNoiseSuppression2";
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700108const char MediaConstraintsInterface::kIntelligibilityEnhancer[] =
109 "intelligibilityEnhancer";
peaha3333bf2016-06-30 00:02:34 -0700110const char MediaConstraintsInterface::kLevelControl[] = "levelControl";
aleloie33c5d92016-10-20 01:53:27 -0700111const char MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS[] =
112 "levelControlInitialPeakLevelDBFS";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000113const char MediaConstraintsInterface::kHighpassFilter[] =
114 "googHighpassFilter";
115const char MediaConstraintsInterface::kTypingNoiseDetection[] =
116 "googTypingNoiseDetection";
117const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring";
minyueba414282016-12-11 02:17:52 -0800118const char MediaConstraintsInterface::kAudioNetworkAdaptorConfig[] =
119 "googAudioNetworkAdaptorConfig";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000120
121// Google-specific constraint keys for a local video source (getUserMedia).
122const char MediaConstraintsInterface::kNoiseReduction[] = "googNoiseReduction";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000123
wu@webrtc.org14814912014-04-02 23:25:15 +0000124// Constraint keys for CreateOffer / CreateAnswer defined in W3C specification.
125const char MediaConstraintsInterface::kOfferToReceiveAudio[] =
126 "OfferToReceiveAudio";
127const char MediaConstraintsInterface::kOfferToReceiveVideo[] =
128 "OfferToReceiveVideo";
129const char MediaConstraintsInterface::kVoiceActivityDetection[] =
130 "VoiceActivityDetection";
131const char MediaConstraintsInterface::kIceRestart[] =
132 "IceRestart";
133// Google specific constraint for BUNDLE enable/disable.
134const char MediaConstraintsInterface::kUseRtpMux[] =
135 "googUseRtpMUX";
136
137// Below constraints should be used during PeerConnection construction.
138const char MediaConstraintsInterface::kEnableDtlsSrtp[] =
139 "DtlsSrtpKeyAgreement";
140const char MediaConstraintsInterface::kEnableRtpDataChannels[] =
141 "RtpDataChannels";
142// Google-specific constraint keys.
143const char MediaConstraintsInterface::kEnableDscp[] = "googDscp";
144const char MediaConstraintsInterface::kEnableIPv6[] = "googIPv6";
145const char MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate[] =
146 "googSuspendBelowMinBitrate";
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000147const char MediaConstraintsInterface::kCombinedAudioVideoBwe[] =
148 "googCombinedAudioVideoBwe";
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000149const char MediaConstraintsInterface::kScreencastMinBitrate[] =
150 "googScreencastMinBitrate";
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000151// TODO(ronghuawu): Remove once cpu overuse detection is stable.
152const char MediaConstraintsInterface::kCpuOveruseDetection[] =
153 "googCpuOveruseDetection";
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000154const char MediaConstraintsInterface::kPayloadPadding[] = "googPayloadPadding";
155
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +0000156
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157// Set |value| to the value associated with the first appearance of |key|, or
158// return false if |key| is not found.
159bool 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.org28e20752013-07-10 00:45:36 +0000170bool FindConstraint(const MediaConstraintsInterface* constraints,
171 const std::string& key, bool* value,
172 size_t* mandatory_constraints) {
deadbeeffe0fd412017-01-13 11:47:56 -0800173 return ::FindConstraint<bool>(constraints, key, value, mandatory_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174}
175
htaa2a49d92016-03-04 02:51:39 -0800176bool FindConstraint(const MediaConstraintsInterface* constraints,
177 const std::string& key,
178 int* value,
179 size_t* mandatory_constraints) {
deadbeeffe0fd412017-01-13 11:47:56 -0800180 return ::FindConstraint<int>(constraints, key, value, mandatory_constraints);
htaa2a49d92016-03-04 02:51:39 -0800181}
182
183void 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
nissec36b31b2016-04-11 23:25:29 -0700191 bool enable_ipv6;
htaa2a49d92016-03-04 02:51:39 -0800192 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6,
nissec36b31b2016-04-11 23:25:29 -0700193 &enable_ipv6, nullptr)) {
194 configuration->disable_ipv6 = !enable_ipv6;
htaa2a49d92016-03-04 02:51:39 -0800195 }
nissec36b31b2016-04-11 23:25:29 -0700196 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);
htaa2a49d92016-03-04 02:51:39 -0800203 // Find Suspend Below Min Bitrate constraint.
nissec36b31b2016-04-11 23:25:29 -0700204 FindConstraint(constraints,
205 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
206 &configuration->media_config.video.suspend_below_min_bitrate,
207 nullptr);
deadbeeffe0fd412017-01-13 11:47:56 -0800208 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
219void 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) {
Oskar Sundbom0e1d7982017-11-16 10:52:42 +0100271 options->audio_network_adaptor = true;
deadbeeffe0fd412017-01-13 11:47:56 -0800272 }
htaa2a49d92016-03-04 02:51:39 -0800273}
274
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275} // namespace webrtc