xref: /aosp_15_r20/external/webrtc/sdk/media_constraints.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
16 namespace webrtc {
17 namespace {
18 
19 // Find the highest-priority instance of the T-valued constraint named by
20 // `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
22 // 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.
29 template <typename T>
FindConstraint(const MediaConstraints * constraints,const std::string & key,T * value,size_t * mandatory_constraints)30 bool 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.
42 template <>
FindConstraint(const MediaConstraints * constraints,const std::string & key,std::string * value,size_t * mandatory_constraints)43 bool 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 
FindConstraint(const MediaConstraints * constraints,const std::string & key,bool * value,size_t * mandatory_constraints)62 bool 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 
FindConstraint(const MediaConstraints * constraints,const std::string & key,int * value,size_t * mandatory_constraints)69 bool 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.
78 template <typename T>
ConstraintToOptional(const MediaConstraints * constraints,const std::string & key,absl::optional<T> * value_out)79 void 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 
90 const char MediaConstraints::kValueTrue[] = "true";
91 const char MediaConstraints::kValueFalse[] = "false";
92 
93 // Constraints declared as static members in mediastreaminterface.h
94 
95 // Audio constraints.
96 const char MediaConstraints::kGoogEchoCancellation[] = "googEchoCancellation";
97 const char MediaConstraints::kAutoGainControl[] = "googAutoGainControl";
98 const char MediaConstraints::kNoiseSuppression[] = "googNoiseSuppression";
99 const char MediaConstraints::kHighpassFilter[] = "googHighpassFilter";
100 const char MediaConstraints::kAudioMirroring[] = "googAudioMirroring";
101 const char MediaConstraints::kAudioNetworkAdaptorConfig[] =
102     "googAudioNetworkAdaptorConfig";
103 const char MediaConstraints::kInitAudioRecordingOnSend[] =
104     "InitAudioRecordingOnSend";
105 
106 // Constraint keys for CreateOffer / CreateAnswer defined in W3C specification.
107 const char MediaConstraints::kOfferToReceiveAudio[] = "OfferToReceiveAudio";
108 const char MediaConstraints::kOfferToReceiveVideo[] = "OfferToReceiveVideo";
109 const char MediaConstraints::kVoiceActivityDetection[] =
110     "VoiceActivityDetection";
111 const char MediaConstraints::kIceRestart[] = "IceRestart";
112 // Google specific constraint for BUNDLE enable/disable.
113 const char MediaConstraints::kUseRtpMux[] = "googUseRtpMUX";
114 
115 // Below constraints should be used during PeerConnection construction.
116 // Google-specific constraint keys.
117 const char MediaConstraints::kEnableDscp[] = "googDscp";
118 const char MediaConstraints::kEnableVideoSuspendBelowMinBitrate[] =
119     "googSuspendBelowMinBitrate";
120 const char MediaConstraints::kCombinedAudioVideoBwe[] =
121     "googCombinedAudioVideoBwe";
122 const char MediaConstraints::kScreencastMinBitrate[] =
123     "googScreencastMinBitrate";
124 // TODO(ronghuawu): Remove once cpu overuse detection is stable.
125 const char MediaConstraints::kCpuOveruseDetection[] = "googCpuOveruseDetection";
126 
127 const char MediaConstraints::kRawPacketizationForVideoEnabled[] =
128     "googRawPacketizationForVideoEnabled";
129 
130 const char MediaConstraints::kNumSimulcastLayers[] = "googNumSimulcastLayers";
131 
132 // Set `value` to the value associated with the first appearance of `key`, or
133 // return false if `key` is not found.
FindFirst(const std::string & key,std::string * value) const134 bool MediaConstraints::Constraints::FindFirst(const std::string& key,
135                                               std::string* value) const {
136   for (Constraints::const_iterator iter = begin(); iter != end(); ++iter) {
137     if (iter->key == key) {
138       *value = iter->value;
139       return true;
140     }
141   }
142   return false;
143 }
144 
CopyConstraintsIntoRtcConfiguration(const MediaConstraints * constraints,PeerConnectionInterface::RTCConfiguration * configuration)145 void CopyConstraintsIntoRtcConfiguration(
146     const MediaConstraints* constraints,
147     PeerConnectionInterface::RTCConfiguration* configuration) {
148   // Copy info from constraints into configuration, if present.
149   if (!constraints) {
150     return;
151   }
152 
153   FindConstraint(constraints, MediaConstraints::kEnableDscp,
154                  &configuration->media_config.enable_dscp, nullptr);
155   FindConstraint(constraints, MediaConstraints::kCpuOveruseDetection,
156                  &configuration->media_config.video.enable_cpu_adaptation,
157                  nullptr);
158   // Find Suspend Below Min Bitrate constraint.
159   FindConstraint(
160       constraints, MediaConstraints::kEnableVideoSuspendBelowMinBitrate,
161       &configuration->media_config.video.suspend_below_min_bitrate, nullptr);
162   ConstraintToOptional<int>(constraints,
163                             MediaConstraints::kScreencastMinBitrate,
164                             &configuration->screencast_min_bitrate);
165   ConstraintToOptional<bool>(constraints,
166                              MediaConstraints::kCombinedAudioVideoBwe,
167                              &configuration->combined_audio_video_bwe);
168 }
169 
CopyConstraintsIntoAudioOptions(const MediaConstraints * constraints,cricket::AudioOptions * options)170 void CopyConstraintsIntoAudioOptions(const MediaConstraints* constraints,
171                                      cricket::AudioOptions* options) {
172   if (!constraints) {
173     return;
174   }
175 
176   ConstraintToOptional<bool>(constraints,
177                              MediaConstraints::kGoogEchoCancellation,
178                              &options->echo_cancellation);
179   ConstraintToOptional<bool>(constraints, MediaConstraints::kAutoGainControl,
180                              &options->auto_gain_control);
181   ConstraintToOptional<bool>(constraints, MediaConstraints::kNoiseSuppression,
182                              &options->noise_suppression);
183   ConstraintToOptional<bool>(constraints, MediaConstraints::kHighpassFilter,
184                              &options->highpass_filter);
185   ConstraintToOptional<bool>(constraints, MediaConstraints::kAudioMirroring,
186                              &options->stereo_swapping);
187   ConstraintToOptional<std::string>(
188       constraints, MediaConstraints::kAudioNetworkAdaptorConfig,
189       &options->audio_network_adaptor_config);
190   // When `kAudioNetworkAdaptorConfig` is defined, it both means that audio
191   // network adaptor is desired, and provides the config string.
192   if (options->audio_network_adaptor_config) {
193     options->audio_network_adaptor = true;
194   }
195   ConstraintToOptional<bool>(constraints,
196                              MediaConstraints::kInitAudioRecordingOnSend,
197                              &options->init_recording_on_send);
198 }
199 
CopyConstraintsIntoOfferAnswerOptions(const MediaConstraints * constraints,PeerConnectionInterface::RTCOfferAnswerOptions * offer_answer_options)200 bool CopyConstraintsIntoOfferAnswerOptions(
201     const MediaConstraints* constraints,
202     PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) {
203   if (!constraints) {
204     return true;
205   }
206 
207   bool value = false;
208   size_t mandatory_constraints_satisfied = 0;
209 
210   if (FindConstraint(constraints, MediaConstraints::kOfferToReceiveAudio,
211                      &value, &mandatory_constraints_satisfied)) {
212     offer_answer_options->offer_to_receive_audio =
213         value ? PeerConnectionInterface::RTCOfferAnswerOptions::
214                     kOfferToReceiveMediaTrue
215               : 0;
216   }
217 
218   if (FindConstraint(constraints, MediaConstraints::kOfferToReceiveVideo,
219                      &value, &mandatory_constraints_satisfied)) {
220     offer_answer_options->offer_to_receive_video =
221         value ? PeerConnectionInterface::RTCOfferAnswerOptions::
222                     kOfferToReceiveMediaTrue
223               : 0;
224   }
225   if (FindConstraint(constraints, MediaConstraints::kVoiceActivityDetection,
226                      &value, &mandatory_constraints_satisfied)) {
227     offer_answer_options->voice_activity_detection = value;
228   }
229   if (FindConstraint(constraints, MediaConstraints::kUseRtpMux, &value,
230                      &mandatory_constraints_satisfied)) {
231     offer_answer_options->use_rtp_mux = value;
232   }
233   if (FindConstraint(constraints, MediaConstraints::kIceRestart, &value,
234                      &mandatory_constraints_satisfied)) {
235     offer_answer_options->ice_restart = value;
236   }
237 
238   if (FindConstraint(constraints,
239                      MediaConstraints::kRawPacketizationForVideoEnabled, &value,
240                      &mandatory_constraints_satisfied)) {
241     offer_answer_options->raw_packetization_for_video = value;
242   }
243 
244   int layers;
245   if (FindConstraint(constraints, MediaConstraints::kNumSimulcastLayers,
246                      &layers, &mandatory_constraints_satisfied)) {
247     offer_answer_options->num_simulcast_layers = layers;
248   }
249 
250   return mandatory_constraints_satisfied == constraints->GetMandatory().size();
251 }
252 
253 }  // namespace webrtc
254