blob: 88261e7530c7c97e300a9410787c365765702fb5 [file] [log] [blame]
Niels Möllerdac03d92019-02-13 07:52:271/*
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
Artem Titovd7ac5812021-07-27 10:23:3920// `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
Niels Möllerdac03d92019-02-13 07:52:2722// 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";
Niels Möllerdac03d92019-02-13 07:52:2797const char MediaConstraints::kAutoGainControl[] = "googAutoGainControl";
Niels Möllerdac03d92019-02-13 07:52:2798const char MediaConstraints::kNoiseSuppression[] = "googNoiseSuppression";
Niels Möllerdac03d92019-02-13 07:52:2799const char MediaConstraints::kHighpassFilter[] = "googHighpassFilter";
Niels Möllerdac03d92019-02-13 07:52:27100const char MediaConstraints::kAudioMirroring[] = "googAudioMirroring";
101const char MediaConstraints::kAudioNetworkAdaptorConfig[] =
102 "googAudioNetworkAdaptorConfig";
Xavier Lepaul1e12f2a2022-01-13 16:06:26103const char MediaConstraints::kInitAudioRecordingOnSend[] =
104 "InitAudioRecordingOnSend";
Niels Möllerdac03d92019-02-13 07:52:27105
106// Constraint keys for CreateOffer / CreateAnswer defined in W3C specification.
107const char MediaConstraints::kOfferToReceiveAudio[] = "OfferToReceiveAudio";
108const char MediaConstraints::kOfferToReceiveVideo[] = "OfferToReceiveVideo";
109const char MediaConstraints::kVoiceActivityDetection[] =
110 "VoiceActivityDetection";
111const char MediaConstraints::kIceRestart[] = "IceRestart";
112// Google specific constraint for BUNDLE enable/disable.
113const char MediaConstraints::kUseRtpMux[] = "googUseRtpMUX";
114
115// Below constraints should be used during PeerConnection construction.
Niels Möllerdac03d92019-02-13 07:52:27116// Google-specific constraint keys.
117const char MediaConstraints::kEnableDscp[] = "googDscp";
Niels Möllerdac03d92019-02-13 07:52:27118const char MediaConstraints::kEnableVideoSuspendBelowMinBitrate[] =
119 "googSuspendBelowMinBitrate";
Niels Möllerdac03d92019-02-13 07:52:27120const char MediaConstraints::kScreencastMinBitrate[] =
121 "googScreencastMinBitrate";
122// TODO(ronghuawu): Remove once cpu overuse detection is stable.
123const char MediaConstraints::kCpuOveruseDetection[] = "googCpuOveruseDetection";
124
Mirta Dvornicic479a3c02019-06-04 13:38:50125const char MediaConstraints::kRawPacketizationForVideoEnabled[] =
126 "googRawPacketizationForVideoEnabled";
127
Niels Möllerdac03d92019-02-13 07:52:27128const char MediaConstraints::kNumSimulcastLayers[] = "googNumSimulcastLayers";
129
Artem Titovd7ac5812021-07-27 10:23:39130// Set `value` to the value associated with the first appearance of `key`, or
131// return false if `key` is not found.
Niels Möllerdac03d92019-02-13 07:52:27132bool MediaConstraints::Constraints::FindFirst(const std::string& key,
133 std::string* value) const {
134 for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) {
135 if (iter->key == key) {
136 *value = iter->value;
137 return true;
138 }
139 }
140 return false;
141}
142
143void CopyConstraintsIntoRtcConfiguration(
144 const MediaConstraints* constraints,
145 PeerConnectionInterface::RTCConfiguration* configuration) {
146 // Copy info from constraints into configuration, if present.
147 if (!constraints) {
148 return;
149 }
150
Niels Möllerdac03d92019-02-13 07:52:27151 FindConstraint(constraints, MediaConstraints::kEnableDscp,
152 &configuration->media_config.enable_dscp, nullptr);
153 FindConstraint(constraints, MediaConstraints::kCpuOveruseDetection,
154 &configuration->media_config.video.enable_cpu_adaptation,
155 nullptr);
Niels Möllerdac03d92019-02-13 07:52:27156 // Find Suspend Below Min Bitrate constraint.
157 FindConstraint(
158 constraints, MediaConstraints::kEnableVideoSuspendBelowMinBitrate,
159 &configuration->media_config.video.suspend_below_min_bitrate, nullptr);
160 ConstraintToOptional<int>(constraints,
161 MediaConstraints::kScreencastMinBitrate,
162 &configuration->screencast_min_bitrate);
Niels Möllerdac03d92019-02-13 07:52:27163}
164
165void CopyConstraintsIntoAudioOptions(const MediaConstraints* constraints,
166 cricket::AudioOptions* options) {
167 if (!constraints) {
168 return;
169 }
170
171 ConstraintToOptional<bool>(constraints,
172 MediaConstraints::kGoogEchoCancellation,
173 &options->echo_cancellation);
Niels Möllerdac03d92019-02-13 07:52:27174 ConstraintToOptional<bool>(constraints, MediaConstraints::kAutoGainControl,
175 &options->auto_gain_control);
Niels Möllerdac03d92019-02-13 07:52:27176 ConstraintToOptional<bool>(constraints, MediaConstraints::kNoiseSuppression,
177 &options->noise_suppression);
Niels Möllerdac03d92019-02-13 07:52:27178 ConstraintToOptional<bool>(constraints, MediaConstraints::kHighpassFilter,
179 &options->highpass_filter);
Niels Möllerdac03d92019-02-13 07:52:27180 ConstraintToOptional<bool>(constraints, MediaConstraints::kAudioMirroring,
181 &options->stereo_swapping);
182 ConstraintToOptional<std::string>(
183 constraints, MediaConstraints::kAudioNetworkAdaptorConfig,
184 &options->audio_network_adaptor_config);
Artem Titovd7ac5812021-07-27 10:23:39185 // When `kAudioNetworkAdaptorConfig` is defined, it both means that audio
Niels Möllerdac03d92019-02-13 07:52:27186 // network adaptor is desired, and provides the config string.
187 if (options->audio_network_adaptor_config) {
188 options->audio_network_adaptor = true;
189 }
Xavier Lepaul1e12f2a2022-01-13 16:06:26190 ConstraintToOptional<bool>(constraints,
191 MediaConstraints::kInitAudioRecordingOnSend,
192 &options->init_recording_on_send);
Niels Möllerdac03d92019-02-13 07:52:27193}
194
195bool CopyConstraintsIntoOfferAnswerOptions(
196 const MediaConstraints* constraints,
197 PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) {
198 if (!constraints) {
199 return true;
200 }
201
202 bool value = false;
203 size_t mandatory_constraints_satisfied = 0;
204
205 if (FindConstraint(constraints, MediaConstraints::kOfferToReceiveAudio,
206 &value, &mandatory_constraints_satisfied)) {
207 offer_answer_options->offer_to_receive_audio =
208 value ? PeerConnectionInterface::RTCOfferAnswerOptions::
209 kOfferToReceiveMediaTrue
210 : 0;
211 }
212
213 if (FindConstraint(constraints, MediaConstraints::kOfferToReceiveVideo,
214 &value, &mandatory_constraints_satisfied)) {
215 offer_answer_options->offer_to_receive_video =
216 value ? PeerConnectionInterface::RTCOfferAnswerOptions::
217 kOfferToReceiveMediaTrue
218 : 0;
219 }
220 if (FindConstraint(constraints, MediaConstraints::kVoiceActivityDetection,
221 &value, &mandatory_constraints_satisfied)) {
222 offer_answer_options->voice_activity_detection = value;
223 }
224 if (FindConstraint(constraints, MediaConstraints::kUseRtpMux, &value,
225 &mandatory_constraints_satisfied)) {
226 offer_answer_options->use_rtp_mux = value;
227 }
228 if (FindConstraint(constraints, MediaConstraints::kIceRestart, &value,
229 &mandatory_constraints_satisfied)) {
230 offer_answer_options->ice_restart = value;
231 }
232
Mirta Dvornicic479a3c02019-06-04 13:38:50233 if (FindConstraint(constraints,
234 MediaConstraints::kRawPacketizationForVideoEnabled, &value,
235 &mandatory_constraints_satisfied)) {
236 offer_answer_options->raw_packetization_for_video = value;
237 }
238
Niels Möllerdac03d92019-02-13 07:52:27239 int layers;
240 if (FindConstraint(constraints, MediaConstraints::kNumSimulcastLayers,
241 &layers, &mandatory_constraints_satisfied)) {
242 offer_answer_options->num_simulcast_layers = layers;
243 }
244
245 return mandatory_constraints_satisfied == constraints->GetMandatory().size();
246}
247
248} // namespace webrtc