Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: content/renderer/media/media_stream_constraints_util.h

Issue 2941553003: Reland "SelectSettings algorithm for audio constraints." (Closed)
Patch Set: fix test Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_H_ 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_H_ 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "content/common/content_export.h" 11 #include "content/common/content_export.h"
12 #include "content/common/media/media_devices.mojom.h" 12 #include "content/common/media/media_devices.mojom.h"
13 #include "content/renderer/media/media_stream_audio_processor_options.h"
13 #include "content/renderer/media/video_track_adapter.h" 14 #include "content/renderer/media/video_track_adapter.h"
14 #include "media/capture/video_capture_types.h" 15 #include "media/capture/video_capture_types.h"
15 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" 16 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
16 #include "third_party/webrtc/base/optional.h" 17 #include "third_party/webrtc/base/optional.h"
17 18
18 namespace content { 19 namespace content {
19 20
20 class ResolutionSet; 21 class ResolutionSet;
21 template <typename T> 22 template <typename T>
22 class NumericRangeSet; 23 class NumericRangeSet;
(...skipping 20 matching lines...) Expand all
43 // Some MediaStreamVideoSink objects (e.g. MediaStreamVideoWebRtcSink) require 44 // Some MediaStreamVideoSink objects (e.g. MediaStreamVideoWebRtcSink) require
44 // configuration derived from constraints that cannot be obtained from the 45 // configuration derived from constraints that cannot be obtained from the
45 // source and track settings indicated above. The following fields are used 46 // source and track settings indicated above. The following fields are used
46 // to configure sinks: 47 // to configure sinks:
47 // * noise_reduction: used to control noise reduction for a screen-capture 48 // * noise_reduction: used to control noise reduction for a screen-capture
48 // track sent to a peer connection. Derive from the googNoiseReduction 49 // track sent to a peer connection. Derive from the googNoiseReduction
49 // constraint. 50 // constraint.
50 // * min_frame_rate and max_frame_rate: used to control frame refreshes in 51 // * min_frame_rate and max_frame_rate: used to control frame refreshes in
51 // screen-capture tracks sent to a peer connection. Derived from the 52 // screen-capture tracks sent to a peer connection. Derived from the
52 // frameRate constraint. 53 // frameRate constraint.
54 // If SelectSettings fails, the HasValue() method returns false and
55 // failed_constraint_name() returns the name of one of the (possibly multiple)
56 // constraints that could not be satisfied.
53 class CONTENT_EXPORT VideoCaptureSettings { 57 class CONTENT_EXPORT VideoCaptureSettings {
54 public: 58 public:
55 // Creates an object without value and with an empty failed constraint name. 59 // Creates an object without value and with an empty failed constraint name.
56 VideoCaptureSettings(); 60 VideoCaptureSettings();
57 61
58 // Creates an object without value and with the given 62 // Creates an object without value and with the given
59 // |failed_constraint_name|. Does not take ownership of 63 // |failed_constraint_name|. Does not take ownership of
60 // |failed_constraint_name|, so it must be null or point to a string that 64 // |failed_constraint_name|, so it must point to a string that remains
61 // remains accessible. 65 // accessible. |failed_constraint_name| must be non-null.
62 explicit VideoCaptureSettings(const char* failed_constraint_name); 66 explicit VideoCaptureSettings(const char* failed_constraint_name);
63 67
64 // Creates an object with the given values. 68 // Creates an object with the given values.
65 VideoCaptureSettings(std::string device_id, 69 VideoCaptureSettings(std::string device_id,
66 media::VideoCaptureParams capture_params_, 70 media::VideoCaptureParams capture_params_,
67 base::Optional<bool> noise_reduction_, 71 base::Optional<bool> noise_reduction_,
68 const VideoTrackAdapterSettings& track_adapter_settings, 72 const VideoTrackAdapterSettings& track_adapter_settings,
69 base::Optional<double> min_frame_rate, 73 base::Optional<double> min_frame_rate,
70 base::Optional<double> max_frame_rate); 74 base::Optional<double> max_frame_rate);
71 75
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 private: 137 private:
134 const char* failed_constraint_name_; 138 const char* failed_constraint_name_;
135 std::string device_id_; 139 std::string device_id_;
136 media::VideoCaptureParams capture_params_; 140 media::VideoCaptureParams capture_params_;
137 base::Optional<bool> noise_reduction_; 141 base::Optional<bool> noise_reduction_;
138 VideoTrackAdapterSettings track_adapter_settings_; 142 VideoTrackAdapterSettings track_adapter_settings_;
139 base::Optional<double> min_frame_rate_; 143 base::Optional<double> min_frame_rate_;
140 base::Optional<double> max_frame_rate_; 144 base::Optional<double> max_frame_rate_;
141 }; 145 };
142 146
147 // This class represents the output the SelectSettings algorithm for audio
148 // constraints (see https://w3c.github.io/mediacapture-main/#dfn-selectsettings)
149 // The input to SelectSettings is a user-supplied constraints object, and its
150 // output is a set of implementation-specific settings that are used to
151 // configure other Chromium objects such as sources, tracks and sinks so that
152 // they work in the way indicated by the specification. AudioCaptureSettings may
153 // also be used to implement other constraints-related functionality, such as
154 // the getSettings() function.
155 // The following fields are used to control MediaStreamVideoSource objects:
156 // * device_id: used for device selection and obtained from the deviceId
157 // * device_parameters: these are the hardware parameters for the device
158 // selected by SelectSettings. They can be used to verify that the
159 // parameters with which the audio stream is actually created corresponds
160 // to what SelectSettings selected. It can also be used to implement
161 // getSettings() for device-related properties such as sampleRate and
162 // channelCount.
163 // The following fields are used to control various audio features:
164 // * hotword_enabled
165 // * disable_local_echo
166 // * render_to_associated_sink
167 // The audio_properties field is used to control the audio-processing module,
168 // which provides features such as software-based echo cancellation.
169 // If SelectSettings fails, the HasValue() method returns false and
170 // failed_constraint_name() returns the name of one of the (possibly multiple)
171 // constraints that could not be satisfied.
172 class CONTENT_EXPORT AudioCaptureSettings {
173 public:
174 // Creates an object without value and with an empty failed constraint name.
175 AudioCaptureSettings();
176
177 // Creates an object without value and with the given
178 // |failed_constraint_name|. Does not take ownership of
179 // |failed_constraint_name|, so it must point to a string that remains
180 // accessible. |failed_constraint_name| must be non-null.
181 explicit AudioCaptureSettings(const char* failed_constraint_name);
182
183 // Creates an object with the given values.
184 explicit AudioCaptureSettings(
185 std::string device_id,
186 const media::AudioParameters& audio_parameters,
187 bool enable_hotword,
188 bool disable_local_echo,
189 bool enable_automatic_output_device_selection,
190 const AudioProcessingProperties& audio_processing_properties);
191 AudioCaptureSettings(const AudioCaptureSettings& other);
192 AudioCaptureSettings& operator=(const AudioCaptureSettings& other);
193 AudioCaptureSettings(AudioCaptureSettings&& other);
194 AudioCaptureSettings& operator=(AudioCaptureSettings&& other);
195
196 bool HasValue() const { return failed_constraint_name_ == nullptr; }
197
198 // Accessors.
199 const char* failed_constraint_name() const { return failed_constraint_name_; }
200 const std::string& device_id() const {
201 DCHECK(HasValue());
202 return device_id_;
203 }
204 // This field is meaningless in content capture.
205 const media::AudioParameters& device_parameters() const {
206 DCHECK(HasValue());
207 return audio_parameters_;
208 }
209 bool hotword_enabled() const {
210 DCHECK(HasValue());
211 return hotword_enabled_;
212 }
213 bool disable_local_echo() const {
214 DCHECK(HasValue());
215 return disable_local_echo_;
216 }
217 bool render_to_associated_sink() const {
218 DCHECK(HasValue());
219 return render_to_associated_sink_;
220 }
221 AudioProcessingProperties audio_processing_properties() const {
222 DCHECK(HasValue());
223 return audio_processing_properties_;
224 }
225
226 private:
227 const char* failed_constraint_name_;
228 std::string device_id_;
229 media::AudioParameters audio_parameters_;
230 bool hotword_enabled_;
231 bool disable_local_echo_;
232 bool render_to_associated_sink_;
233 AudioProcessingProperties audio_processing_properties_;
234 };
235
143 // Method to get boolean value of constraint with |name| from constraints. 236 // Method to get boolean value of constraint with |name| from constraints.
144 // Returns true if the constraint is specified in either mandatory or optional 237 // Returns true if the constraint is specified in either mandatory or optional
145 // constraints. 238 // constraints.
146 bool CONTENT_EXPORT GetConstraintValueAsBoolean( 239 bool CONTENT_EXPORT GetConstraintValueAsBoolean(
147 const blink::WebMediaConstraints& constraints, 240 const blink::WebMediaConstraints& constraints,
148 const blink::BooleanConstraint blink::WebMediaTrackConstraintSet::*picker, 241 const blink::BooleanConstraint blink::WebMediaTrackConstraintSet::*picker,
149 bool* value); 242 bool* value);
150 243
151 // Method to get int value of constraint with |name| from constraints. 244 // Method to get int value of constraint with |name| from constraints.
152 // Returns true if the constraint is specified in either mandatory or Optional 245 // Returns true if the constraint is specified in either mandatory or Optional
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 return constraint.HasExact() ? constraint.Exact() : constraint.Max(); 306 return constraint.HasExact() ? constraint.Exact() : constraint.Max();
214 } 307 }
215 308
216 template <typename ConstraintType> 309 template <typename ConstraintType>
217 auto ConstraintMin(const ConstraintType& constraint) 310 auto ConstraintMin(const ConstraintType& constraint)
218 -> decltype(constraint.Min()) { 311 -> decltype(constraint.Min()) {
219 DCHECK(ConstraintHasMin(constraint)); 312 DCHECK(ConstraintHasMin(constraint));
220 return constraint.HasExact() ? constraint.Exact() : constraint.Min(); 313 return constraint.HasExact() ? constraint.Exact() : constraint.Min();
221 } 314 }
222 315
316 // If |value| is outside the range of |constraint|, returns the name of the
317 // failed constraint. Otherwise, returns nullptr. The return value converts to
318 // bool in the expected way.
319 template <typename NumericConstraintType, typename ValueType>
320 const char* IsOutsideConstraintRange(NumericConstraintType constraint,
321 ValueType value) {
322 return (ConstraintHasMin(constraint) && value < ConstraintMin(constraint)) ||
323 (ConstraintHasMax(constraint) &&
324 value > ConstraintMax(constraint))
325 ? constraint.GetName()
326 : nullptr;
327 }
328
329 std::string GetMediaStreamSource(const blink::WebMediaConstraints& constraints);
330 bool IsDeviceCapture(const blink::WebMediaConstraints& constraints);
331
223 // This function selects track settings from a set of candidate resolutions and 332 // This function selects track settings from a set of candidate resolutions and
224 // frame rates, given the source video-capture format and ideal values. 333 // frame rates, given the source video-capture format and ideal values.
225 // The output are settings for a VideoTrackAdapter, which can adjust the 334 // The output are settings for a VideoTrackAdapter, which can adjust the
226 // resolution and frame rate of the source, and consist of maximum (or 335 // resolution and frame rate of the source, and consist of maximum (or
227 // target) width, height and frame rate, and minimum and maximum aspect ratio. 336 // target) width, height and frame rate, and minimum and maximum aspect ratio.
228 // * Minimum and maximum aspect ratios are taken from |resolution_set| and are 337 // * Minimum and maximum aspect ratios are taken from |resolution_set| and are
229 // not affected by ideal values. 338 // not affected by ideal values.
230 // * The selected frame rate is always the value within the |frame_rate_set| 339 // * The selected frame rate is always the value within the |frame_rate_set|
231 // range that is closest to the ideal frame rate (or the source frame rate 340 // range that is closest to the ideal frame rate (or the source frame rate
232 // if no ideal is supplied). If the chosen frame rate is greater than or equal 341 // if no ideal is supplied). If the chosen frame rate is greater than or equal
233 // to the source's frame rate, a value of 0.0 is returned, which means that 342 // to the source's frame rate, a value of 0.0 is returned, which means that
234 // there will be no frame-rate adjustment. 343 // there will be no frame-rate adjustment.
235 // * Width and height are selected using the 344 // * Width and height are selected using the
236 // ResolutionSet::SelectClosestPointToIdeal function, using ideal values from 345 // ResolutionSet::SelectClosestPointToIdeal function, using ideal values from
237 // |basic_constraint_set| and using the source's width and height as the 346 // |basic_constraint_set| and using the source's width and height as the
238 // default resolution. The width and height returned by 347 // default resolution. The width and height returned by
239 // SelectClosestPointToIdeal are rounded to the nearest int. For more details, 348 // SelectClosestPointToIdeal are rounded to the nearest int. For more details,
240 // see the documentation for ResolutionSet::SelectClosestPointToIdeal. 349 // see the documentation for ResolutionSet::SelectClosestPointToIdeal.
241 // Note that this function ignores the min/max/exact values from 350 // Note that this function ignores the min/max/exact values from
242 // |basic_constraint_set|. Only its ideal values are used. 351 // |basic_constraint_set|. Only its ideal values are used.
243 // This function has undefined behavior if any of |resolution_set| or 352 // This function has undefined behavior if any of |resolution_set| or
244 // |frame_rate_set| are empty. 353 // |frame_rate_set| are empty.
245 VideoTrackAdapterSettings CONTENT_EXPORT SelectVideoTrackAdapterSettings( 354 VideoTrackAdapterSettings CONTENT_EXPORT SelectVideoTrackAdapterSettings(
246 const blink::WebMediaTrackConstraintSet& basic_constraint_set, 355 const blink::WebMediaTrackConstraintSet& basic_constraint_set,
247 const ResolutionSet& resolution_set, 356 const ResolutionSet& resolution_set,
248 const NumericRangeSet<double>& frame_rate_set, 357 const NumericRangeSet<double>& frame_rate_set,
249 const media::VideoCaptureFormat& source_format, 358 const media::VideoCaptureFormat& source_format,
250 bool expect_source_native_size); 359 bool expect_source_native_size);
251 360
361 // Generic distance function between two values for numeric constraints. Based
362 // on the fitness-distance function described in
363 // https://w3c.github.io/mediacapture-main/#dfn-fitness-distance
364 double NumericConstraintFitnessDistance(double value1, double value2);
365
366 // Fitness distance between |value| and |constraint|.
367 // Based on https://w3c.github.io/mediacapture-main/#dfn-fitness-distance.
368 double StringConstraintFitnessDistance(
369 const blink::WebString& value,
370 const blink::StringConstraint& constraint);
371
252 } // namespace content 372 } // namespace content
253 373
254 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_H_ 374 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698