blob: af258917e7acd6f6ce942706f0607caf0f5e5d42 [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";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000049const char MediaConstraintsInterface::kHighpassFilter[] =
50 "googHighpassFilter";
51const char MediaConstraintsInterface::kTypingNoiseDetection[] =
52 "googTypingNoiseDetection";
53const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring";
54
55// Google-specific constraint keys for a local video source (getUserMedia).
56const char MediaConstraintsInterface::kNoiseReduction[] = "googNoiseReduction";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000057
wu@webrtc.org14814912014-04-02 23:25:15 +000058// Constraint keys for CreateOffer / CreateAnswer defined in W3C specification.
59const char MediaConstraintsInterface::kOfferToReceiveAudio[] =
60 "OfferToReceiveAudio";
61const char MediaConstraintsInterface::kOfferToReceiveVideo[] =
62 "OfferToReceiveVideo";
63const char MediaConstraintsInterface::kVoiceActivityDetection[] =
64 "VoiceActivityDetection";
65const char MediaConstraintsInterface::kIceRestart[] =
66 "IceRestart";
67// Google specific constraint for BUNDLE enable/disable.
68const char MediaConstraintsInterface::kUseRtpMux[] =
69 "googUseRtpMUX";
70
71// Below constraints should be used during PeerConnection construction.
72const char MediaConstraintsInterface::kEnableDtlsSrtp[] =
73 "DtlsSrtpKeyAgreement";
74const char MediaConstraintsInterface::kEnableRtpDataChannels[] =
75 "RtpDataChannels";
76// Google-specific constraint keys.
77const char MediaConstraintsInterface::kEnableDscp[] = "googDscp";
78const char MediaConstraintsInterface::kEnableIPv6[] = "googIPv6";
79const char MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate[] =
80 "googSuspendBelowMinBitrate";
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +000081const char MediaConstraintsInterface::kCombinedAudioVideoBwe[] =
82 "googCombinedAudioVideoBwe";
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +000083const char MediaConstraintsInterface::kScreencastMinBitrate[] =
84 "googScreencastMinBitrate";
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000085// TODO(ronghuawu): Remove once cpu overuse detection is stable.
86const char MediaConstraintsInterface::kCpuOveruseDetection[] =
87 "googCpuOveruseDetection";
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +000088const char MediaConstraintsInterface::kPayloadPadding[] = "googPayloadPadding";
89
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +000090
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091// Set |value| to the value associated with the first appearance of |key|, or
92// return false if |key| is not found.
93bool MediaConstraintsInterface::Constraints::FindFirst(
94 const std::string& key, std::string* value) const {
95 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) {
96 if (iter->key == key) {
97 *value = iter->value;
98 return true;
99 }
100 }
101 return false;
102}
103
104// Find the highest-priority instance of the boolean-valued constraint) named by
105// |key| and return its value as |value|. |constraints| can be null.
106// If |mandatory_constraints| is non-null, it is incremented if the key appears
107// among the mandatory constraints.
108// Returns true if the key was found and has a valid boolean value.
109// If the key appears multiple times as an optional constraint, appearances
110// after the first are ignored.
111// Note: Because this uses FindFirst, repeated optional constraints whose
112// first instance has an unrecognized value are not handled precisely in
113// accordance with the specification.
114bool FindConstraint(const MediaConstraintsInterface* constraints,
115 const std::string& key, bool* value,
116 size_t* mandatory_constraints) {
117 std::string string_value;
118 if (!constraints) {
119 return false;
120 }
121 if (constraints->GetMandatory().FindFirst(key, &string_value)) {
htaa2a49d92016-03-04 02:51:39 -0800122 if (mandatory_constraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 ++*mandatory_constraints;
htaa2a49d92016-03-04 02:51:39 -0800124 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000125 return rtc::FromString(string_value, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 }
127 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000128 return rtc::FromString(string_value, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 }
130 return false;
131}
132
htaa2a49d92016-03-04 02:51:39 -0800133// As above, but for integers.
134bool FindConstraint(const MediaConstraintsInterface* constraints,
135 const std::string& key,
136 int* value,
137 size_t* mandatory_constraints) {
138 std::string string_value;
139 if (!constraints) {
140 return false;
141 }
142 if (constraints->GetMandatory().FindFirst(key, &string_value)) {
143 if (mandatory_constraints) {
144 ++*mandatory_constraints;
145 }
146 return rtc::FromString(string_value, value);
147 }
148 if (constraints->GetOptional().FindFirst(key, &string_value)) {
149 return rtc::FromString(string_value, value);
150 }
151 return false;
152}
153
154void ConstraintToOptionalBool(const MediaConstraintsInterface* constraints,
155 const std::string& key,
156 rtc::Optional<bool>* value_out) {
157 bool value;
158 bool present = FindConstraint(constraints, key, &value, nullptr);
159 if (present) {
160 *value_out = rtc::Optional<bool>(value);
161 }
162}
163
164void ConstraintToOptionalInt(const MediaConstraintsInterface* constraints,
165 const std::string& key,
166 rtc::Optional<int>* value_out) {
167 int value;
168 bool present = FindConstraint(constraints, key, &value, nullptr);
169 if (present) {
170 *value_out = rtc::Optional<int>(value);
171 }
172}
173
174void CopyConstraintsIntoRtcConfiguration(
175 const MediaConstraintsInterface* constraints,
176 PeerConnectionInterface::RTCConfiguration* configuration) {
177 // Copy info from constraints into configuration, if present.
178 if (!constraints) {
179 return;
180 }
181
182 bool value;
183 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6,
184 &value, nullptr)) {
185 if (!value) {
186 configuration->disable_ipv6 = true;
187 }
188 }
189 ConstraintToOptionalBool(constraints, MediaConstraintsInterface::kEnableDscp,
190 &configuration->enable_dscp);
191 ConstraintToOptionalBool(constraints,
192 MediaConstraintsInterface::kCpuOveruseDetection,
193 &configuration->cpu_overuse_detection);
194 if (FindConstraint(constraints,
195 MediaConstraintsInterface::kEnableRtpDataChannels, &value,
196 NULL) &&
197 value) {
198 configuration->enable_rtp_data_channel = true;
199 }
200 // Find Suspend Below Min Bitrate constraint.
201 ConstraintToOptionalBool(
202 constraints,
203 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
204 &configuration->suspend_below_min_bitrate);
205 ConstraintToOptionalInt(constraints,
206 MediaConstraintsInterface::kScreencastMinBitrate,
207 &configuration->screencast_min_bitrate);
208 ConstraintToOptionalBool(constraints,
209 MediaConstraintsInterface::kCombinedAudioVideoBwe,
210 &configuration->combined_audio_video_bwe);
211 ConstraintToOptionalBool(constraints,
212 MediaConstraintsInterface::kEnableDtlsSrtp,
213 &configuration->enable_dtls_srtp);
214}
215
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216} // namespace webrtc