blob: 6a014a2b039963e71e1828a93e5a4825c29be3b4 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/mediaconstraintsinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
htaa2a49d92016-03-04 02:51:39 -080013#include "webrtc/api/peerconnectioninterface.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000014#include "webrtc/base/stringencode.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
16namespace webrtc {
17
18const char MediaConstraintsInterface::kValueTrue[] = "true";
19const char MediaConstraintsInterface::kValueFalse[] = "false";
20
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000021// Constraints declared as static members in mediastreaminterface.h
22// Specified by draft-alvestrand-constraints-resolution-00b
23const char MediaConstraintsInterface::kMinAspectRatio[] = "minAspectRatio";
24const char MediaConstraintsInterface::kMaxAspectRatio[] = "maxAspectRatio";
25const char MediaConstraintsInterface::kMaxWidth[] = "maxWidth";
26const char MediaConstraintsInterface::kMinWidth[] = "minWidth";
27const char MediaConstraintsInterface::kMaxHeight[] = "maxHeight";
28const char MediaConstraintsInterface::kMinHeight[] = "minHeight";
29const char MediaConstraintsInterface::kMaxFrameRate[] = "maxFrameRate";
30const char MediaConstraintsInterface::kMinFrameRate[] = "minFrameRate";
31
32// Audio constraints.
33const char MediaConstraintsInterface::kEchoCancellation[] =
tommi39b31002015-06-23 09:50:47 -070034 "echoCancellation";
Tommi70c7fe12015-06-15 09:14:03 +020035const char MediaConstraintsInterface::kGoogEchoCancellation[] =
36 "googEchoCancellation";
Henrik Lundin441f6342015-06-09 16:03:13 +020037const char MediaConstraintsInterface::kExtendedFilterEchoCancellation[] =
38 "googEchoCancellation2";
Bjorn Volckerbf395c12015-03-25 22:45:56 +010039const char MediaConstraintsInterface::kDAEchoCancellation[] =
40 "googDAEchoCancellation";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000041const char MediaConstraintsInterface::kAutoGainControl[] =
42 "googAutoGainControl";
43const char MediaConstraintsInterface::kExperimentalAutoGainControl[] =
44 "googAutoGainControl2";
45const char MediaConstraintsInterface::kNoiseSuppression[] =
46 "googNoiseSuppression";
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000047const char MediaConstraintsInterface::kExperimentalNoiseSuppression[] =
48 "googNoiseSuppression2";
Alejandro Luebsc9b0c262016-05-16 15:32:38 -070049const char MediaConstraintsInterface::kIntelligibilityEnhancer[] =
50 "intelligibilityEnhancer";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000051const char MediaConstraintsInterface::kHighpassFilter[] =
52 "googHighpassFilter";
53const char MediaConstraintsInterface::kTypingNoiseDetection[] =
54 "googTypingNoiseDetection";
55const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring";
56
57// Google-specific constraint keys for a local video source (getUserMedia).
58const char MediaConstraintsInterface::kNoiseReduction[] = "googNoiseReduction";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000059
wu@webrtc.org14814912014-04-02 23:25:15 +000060// Constraint keys for CreateOffer / CreateAnswer defined in W3C specification.
61const char MediaConstraintsInterface::kOfferToReceiveAudio[] =
62 "OfferToReceiveAudio";
63const char MediaConstraintsInterface::kOfferToReceiveVideo[] =
64 "OfferToReceiveVideo";
65const char MediaConstraintsInterface::kVoiceActivityDetection[] =
66 "VoiceActivityDetection";
67const char MediaConstraintsInterface::kIceRestart[] =
68 "IceRestart";
69// Google specific constraint for BUNDLE enable/disable.
70const char MediaConstraintsInterface::kUseRtpMux[] =
71 "googUseRtpMUX";
72
73// Below constraints should be used during PeerConnection construction.
74const char MediaConstraintsInterface::kEnableDtlsSrtp[] =
75 "DtlsSrtpKeyAgreement";
76const char MediaConstraintsInterface::kEnableRtpDataChannels[] =
77 "RtpDataChannels";
78// Google-specific constraint keys.
79const char MediaConstraintsInterface::kEnableDscp[] = "googDscp";
80const char MediaConstraintsInterface::kEnableIPv6[] = "googIPv6";
81const char MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate[] =
82 "googSuspendBelowMinBitrate";
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +000083const char MediaConstraintsInterface::kCombinedAudioVideoBwe[] =
84 "googCombinedAudioVideoBwe";
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +000085const char MediaConstraintsInterface::kScreencastMinBitrate[] =
86 "googScreencastMinBitrate";
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000087// TODO(ronghuawu): Remove once cpu overuse detection is stable.
88const char MediaConstraintsInterface::kCpuOveruseDetection[] =
89 "googCpuOveruseDetection";
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +000090const char MediaConstraintsInterface::kPayloadPadding[] = "googPayloadPadding";
91
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +000092
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093// Set |value| to the value associated with the first appearance of |key|, or
94// return false if |key| is not found.
95bool MediaConstraintsInterface::Constraints::FindFirst(
96 const std::string& key, std::string* value) const {
97 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) {
98 if (iter->key == key) {
99 *value = iter->value;
100 return true;
101 }
102 }
103 return false;
104}
105
106// Find the highest-priority instance of the boolean-valued constraint) named by
107// |key| and return its value as |value|. |constraints| can be null.
108// If |mandatory_constraints| is non-null, it is incremented if the key appears
109// among the mandatory constraints.
110// Returns true if the key was found and has a valid boolean value.
111// If the key appears multiple times as an optional constraint, appearances
112// after the first are ignored.
113// Note: Because this uses FindFirst, repeated optional constraints whose
114// first instance has an unrecognized value are not handled precisely in
115// accordance with the specification.
116bool FindConstraint(const MediaConstraintsInterface* constraints,
117 const std::string& key, bool* value,
118 size_t* mandatory_constraints) {
119 std::string string_value;
120 if (!constraints) {
121 return false;
122 }
123 if (constraints->GetMandatory().FindFirst(key, &string_value)) {
htaa2a49d92016-03-04 02:51:39 -0800124 if (mandatory_constraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 ++*mandatory_constraints;
htaa2a49d92016-03-04 02:51:39 -0800126 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000127 return rtc::FromString(string_value, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 }
129 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000130 return rtc::FromString(string_value, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 }
132 return false;
133}
134
htaa2a49d92016-03-04 02:51:39 -0800135// As above, but for integers.
136bool FindConstraint(const MediaConstraintsInterface* constraints,
137 const std::string& key,
138 int* value,
139 size_t* mandatory_constraints) {
140 std::string string_value;
141 if (!constraints) {
142 return false;
143 }
144 if (constraints->GetMandatory().FindFirst(key, &string_value)) {
145 if (mandatory_constraints) {
146 ++*mandatory_constraints;
147 }
148 return rtc::FromString(string_value, value);
149 }
150 if (constraints->GetOptional().FindFirst(key, &string_value)) {
151 return rtc::FromString(string_value, value);
152 }
153 return false;
154}
155
156void ConstraintToOptionalBool(const MediaConstraintsInterface* constraints,
157 const std::string& key,
158 rtc::Optional<bool>* value_out) {
159 bool value;
160 bool present = FindConstraint(constraints, key, &value, nullptr);
161 if (present) {
162 *value_out = rtc::Optional<bool>(value);
163 }
164}
165
166void ConstraintToOptionalInt(const MediaConstraintsInterface* constraints,
167 const std::string& key,
168 rtc::Optional<int>* value_out) {
169 int value;
170 bool present = FindConstraint(constraints, key, &value, nullptr);
171 if (present) {
172 *value_out = rtc::Optional<int>(value);
173 }
174}
175
176void CopyConstraintsIntoRtcConfiguration(
177 const MediaConstraintsInterface* constraints,
178 PeerConnectionInterface::RTCConfiguration* configuration) {
179 // Copy info from constraints into configuration, if present.
180 if (!constraints) {
181 return;
182 }
183
nissec36b31b2016-04-11 23:25:29 -0700184 bool enable_ipv6;
htaa2a49d92016-03-04 02:51:39 -0800185 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6,
nissec36b31b2016-04-11 23:25:29 -0700186 &enable_ipv6, nullptr)) {
187 configuration->disable_ipv6 = !enable_ipv6;
htaa2a49d92016-03-04 02:51:39 -0800188 }
nissec36b31b2016-04-11 23:25:29 -0700189 FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp,
190 &configuration->media_config.enable_dscp, nullptr);
191 FindConstraint(
192 constraints, MediaConstraintsInterface::kCpuOveruseDetection,
193 &configuration->media_config.video.enable_cpu_overuse_detection, nullptr);
194 FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
195 &configuration->enable_rtp_data_channel, nullptr);
htaa2a49d92016-03-04 02:51:39 -0800196 // Find Suspend Below Min Bitrate constraint.
nissec36b31b2016-04-11 23:25:29 -0700197 FindConstraint(constraints,
198 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
199 &configuration->media_config.video.suspend_below_min_bitrate,
200 nullptr);
htaa2a49d92016-03-04 02:51:39 -0800201 ConstraintToOptionalInt(constraints,
202 MediaConstraintsInterface::kScreencastMinBitrate,
203 &configuration->screencast_min_bitrate);
204 ConstraintToOptionalBool(constraints,
205 MediaConstraintsInterface::kCombinedAudioVideoBwe,
206 &configuration->combined_audio_video_bwe);
207 ConstraintToOptionalBool(constraints,
208 MediaConstraintsInterface::kEnableDtlsSrtp,
209 &configuration->enable_dtls_srtp);
210}
211
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212} // namespace webrtc