blob: e35c067acc06624525c1ce2dcd67cab4bff1e5f8 [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";
peaha3333bf2016-06-30 00:02:34 -070051const char MediaConstraintsInterface::kLevelControl[] = "levelControl";
aleloie33c5d92016-10-20 01:53:27 -070052const char MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS[] =
53 "levelControlInitialPeakLevelDBFS";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000054const char MediaConstraintsInterface::kHighpassFilter[] =
55 "googHighpassFilter";
56const char MediaConstraintsInterface::kTypingNoiseDetection[] =
57 "googTypingNoiseDetection";
58const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring";
minyueba414282016-12-11 02:17:52 -080059const char MediaConstraintsInterface::kAudioNetworkAdaptorConfig[] =
60 "googAudioNetworkAdaptorConfig";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000061
62// Google-specific constraint keys for a local video source (getUserMedia).
63const char MediaConstraintsInterface::kNoiseReduction[] = "googNoiseReduction";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000064
wu@webrtc.org14814912014-04-02 23:25:15 +000065// Constraint keys for CreateOffer / CreateAnswer defined in W3C specification.
66const char MediaConstraintsInterface::kOfferToReceiveAudio[] =
67 "OfferToReceiveAudio";
68const char MediaConstraintsInterface::kOfferToReceiveVideo[] =
69 "OfferToReceiveVideo";
70const char MediaConstraintsInterface::kVoiceActivityDetection[] =
71 "VoiceActivityDetection";
72const char MediaConstraintsInterface::kIceRestart[] =
73 "IceRestart";
74// Google specific constraint for BUNDLE enable/disable.
75const char MediaConstraintsInterface::kUseRtpMux[] =
76 "googUseRtpMUX";
77
78// Below constraints should be used during PeerConnection construction.
79const char MediaConstraintsInterface::kEnableDtlsSrtp[] =
80 "DtlsSrtpKeyAgreement";
81const char MediaConstraintsInterface::kEnableRtpDataChannels[] =
82 "RtpDataChannels";
83// Google-specific constraint keys.
84const char MediaConstraintsInterface::kEnableDscp[] = "googDscp";
85const char MediaConstraintsInterface::kEnableIPv6[] = "googIPv6";
86const char MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate[] =
87 "googSuspendBelowMinBitrate";
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +000088const char MediaConstraintsInterface::kCombinedAudioVideoBwe[] =
89 "googCombinedAudioVideoBwe";
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +000090const char MediaConstraintsInterface::kScreencastMinBitrate[] =
91 "googScreencastMinBitrate";
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +000092// TODO(ronghuawu): Remove once cpu overuse detection is stable.
93const char MediaConstraintsInterface::kCpuOveruseDetection[] =
94 "googCpuOveruseDetection";
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +000095const char MediaConstraintsInterface::kPayloadPadding[] = "googPayloadPadding";
96
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +000097
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098// Set |value| to the value associated with the first appearance of |key|, or
99// return false if |key| is not found.
100bool MediaConstraintsInterface::Constraints::FindFirst(
101 const std::string& key, std::string* value) const {
102 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) {
103 if (iter->key == key) {
104 *value = iter->value;
105 return true;
106 }
107 }
108 return false;
109}
110
111// Find the highest-priority instance of the boolean-valued constraint) named by
112// |key| and return its value as |value|. |constraints| can be null.
113// If |mandatory_constraints| is non-null, it is incremented if the key appears
114// among the mandatory constraints.
115// Returns true if the key was found and has a valid boolean value.
116// If the key appears multiple times as an optional constraint, appearances
117// after the first are ignored.
118// Note: Because this uses FindFirst, repeated optional constraints whose
119// first instance has an unrecognized value are not handled precisely in
120// accordance with the specification.
121bool FindConstraint(const MediaConstraintsInterface* constraints,
122 const std::string& key, bool* value,
123 size_t* mandatory_constraints) {
124 std::string string_value;
125 if (!constraints) {
126 return false;
127 }
128 if (constraints->GetMandatory().FindFirst(key, &string_value)) {
htaa2a49d92016-03-04 02:51:39 -0800129 if (mandatory_constraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 ++*mandatory_constraints;
htaa2a49d92016-03-04 02:51:39 -0800131 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000132 return rtc::FromString(string_value, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 }
134 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000135 return rtc::FromString(string_value, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 }
137 return false;
138}
139
htaa2a49d92016-03-04 02:51:39 -0800140// As above, but for integers.
141bool FindConstraint(const MediaConstraintsInterface* constraints,
142 const std::string& key,
143 int* value,
144 size_t* mandatory_constraints) {
145 std::string string_value;
146 if (!constraints) {
147 return false;
148 }
149 if (constraints->GetMandatory().FindFirst(key, &string_value)) {
150 if (mandatory_constraints) {
151 ++*mandatory_constraints;
152 }
153 return rtc::FromString(string_value, value);
154 }
155 if (constraints->GetOptional().FindFirst(key, &string_value)) {
156 return rtc::FromString(string_value, value);
157 }
158 return false;
159}
160
161void ConstraintToOptionalBool(const MediaConstraintsInterface* constraints,
162 const std::string& key,
163 rtc::Optional<bool>* value_out) {
164 bool value;
165 bool present = FindConstraint(constraints, key, &value, nullptr);
166 if (present) {
167 *value_out = rtc::Optional<bool>(value);
168 }
169}
170
171void ConstraintToOptionalInt(const MediaConstraintsInterface* constraints,
172 const std::string& key,
173 rtc::Optional<int>* value_out) {
174 int value;
175 bool present = FindConstraint(constraints, key, &value, nullptr);
176 if (present) {
177 *value_out = rtc::Optional<int>(value);
178 }
179}
180
181void CopyConstraintsIntoRtcConfiguration(
182 const MediaConstraintsInterface* constraints,
183 PeerConnectionInterface::RTCConfiguration* configuration) {
184 // Copy info from constraints into configuration, if present.
185 if (!constraints) {
186 return;
187 }
188
nissec36b31b2016-04-11 23:25:29 -0700189 bool enable_ipv6;
htaa2a49d92016-03-04 02:51:39 -0800190 if (FindConstraint(constraints, MediaConstraintsInterface::kEnableIPv6,
nissec36b31b2016-04-11 23:25:29 -0700191 &enable_ipv6, nullptr)) {
192 configuration->disable_ipv6 = !enable_ipv6;
htaa2a49d92016-03-04 02:51:39 -0800193 }
nissec36b31b2016-04-11 23:25:29 -0700194 FindConstraint(constraints, MediaConstraintsInterface::kEnableDscp,
195 &configuration->media_config.enable_dscp, nullptr);
196 FindConstraint(
197 constraints, MediaConstraintsInterface::kCpuOveruseDetection,
198 &configuration->media_config.video.enable_cpu_overuse_detection, nullptr);
199 FindConstraint(constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
200 &configuration->enable_rtp_data_channel, nullptr);
htaa2a49d92016-03-04 02:51:39 -0800201 // Find Suspend Below Min Bitrate constraint.
nissec36b31b2016-04-11 23:25:29 -0700202 FindConstraint(constraints,
203 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
204 &configuration->media_config.video.suspend_below_min_bitrate,
205 nullptr);
htaa2a49d92016-03-04 02:51:39 -0800206 ConstraintToOptionalInt(constraints,
207 MediaConstraintsInterface::kScreencastMinBitrate,
208 &configuration->screencast_min_bitrate);
209 ConstraintToOptionalBool(constraints,
210 MediaConstraintsInterface::kCombinedAudioVideoBwe,
211 &configuration->combined_audio_video_bwe);
212 ConstraintToOptionalBool(constraints,
213 MediaConstraintsInterface::kEnableDtlsSrtp,
214 &configuration->enable_dtls_srtp);
215}
216
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217} // namespace webrtc