| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_VIDEO_DEVICE_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_VIDEO_DEVICE_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_VIDEO_DEVICE_H_ | 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_VIDEO_DEVICE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/optional.h" |
| 12 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
| 13 #include "content/common/media/media_devices.mojom.h" | 13 #include "content/common/media/media_devices.mojom.h" |
| 14 #include "content/renderer/media/media_stream_constraints_util.h" |
| 14 #include "media/capture/video_capture_types.h" | 15 #include "media/capture/video_capture_types.h" |
| 15 #include "third_party/webrtc/base/optional.h" | |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 class WebString; | 18 class WebString; |
| 19 class WebMediaConstraints; | 19 class WebMediaConstraints; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 | 23 |
| 24 // Calculates and returns videoKind value for |format|. | 24 // Calculates and returns videoKind value for |format|. |
| 25 // See https://w3c.github.io/mediacapture-depth. | 25 // See https://w3c.github.io/mediacapture-depth. |
| 26 blink::WebString CONTENT_EXPORT | 26 blink::WebString CONTENT_EXPORT |
| 27 GetVideoKindForFormat(const media::VideoCaptureFormat& format); | 27 GetVideoKindForFormat(const media::VideoCaptureFormat& format); |
| 28 | 28 |
| 29 struct CONTENT_EXPORT VideoDeviceCaptureCapabilities { | 29 struct CONTENT_EXPORT VideoDeviceCaptureCapabilities { |
| 30 VideoDeviceCaptureCapabilities(); | 30 VideoDeviceCaptureCapabilities(); |
| 31 VideoDeviceCaptureCapabilities(VideoDeviceCaptureCapabilities&& other); | 31 VideoDeviceCaptureCapabilities(VideoDeviceCaptureCapabilities&& other); |
| 32 ~VideoDeviceCaptureCapabilities(); | 32 ~VideoDeviceCaptureCapabilities(); |
| 33 VideoDeviceCaptureCapabilities& operator=( | 33 VideoDeviceCaptureCapabilities& operator=( |
| 34 VideoDeviceCaptureCapabilities&& other); | 34 VideoDeviceCaptureCapabilities&& other); |
| 35 | 35 |
| 36 // Each field is independent of each other. | 36 // Each field is independent of each other. |
| 37 std::vector<::mojom::VideoInputDeviceCapabilitiesPtr> device_capabilities; | 37 std::vector<::mojom::VideoInputDeviceCapabilitiesPtr> device_capabilities; |
| 38 std::vector<media::PowerLineFrequency> power_line_capabilities; | 38 std::vector<media::PowerLineFrequency> power_line_capabilities; |
| 39 std::vector<rtc::Optional<bool>> noise_reduction_capabilities; | 39 std::vector<base::Optional<bool>> noise_reduction_capabilities; |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 class CONTENT_EXPORT VideoDeviceCaptureSourceSelectionResult { | 42 // This function performs source, source-settings and track-settings selection |
| 43 public: | 43 // based on the given |capabilities| and |constraints|. |
| 44 // Creates a result without value and with an empty failed constraint name. | |
| 45 VideoDeviceCaptureSourceSelectionResult(); | |
| 46 | |
| 47 // Creates a result without value and with the given |failed_constraint_name|. | |
| 48 // Does not take ownership of |failed_constraint_name|, so it must be null or | |
| 49 // point to a string that remains accessible. | |
| 50 explicit VideoDeviceCaptureSourceSelectionResult( | |
| 51 const char* failed_constraint_name); | |
| 52 | |
| 53 // Creates a result with the given values. | |
| 54 VideoDeviceCaptureSourceSelectionResult( | |
| 55 const std::string& device_id, | |
| 56 ::mojom::FacingMode facing_mode_, | |
| 57 media::VideoCaptureParams capture_params_, | |
| 58 rtc::Optional<bool> noise_reduction_); | |
| 59 | |
| 60 VideoDeviceCaptureSourceSelectionResult( | |
| 61 const VideoDeviceCaptureSourceSelectionResult& other); | |
| 62 VideoDeviceCaptureSourceSelectionResult& operator=( | |
| 63 const VideoDeviceCaptureSourceSelectionResult& other); | |
| 64 VideoDeviceCaptureSourceSelectionResult( | |
| 65 VideoDeviceCaptureSourceSelectionResult&& other); | |
| 66 VideoDeviceCaptureSourceSelectionResult& operator=( | |
| 67 VideoDeviceCaptureSourceSelectionResult&& other); | |
| 68 ~VideoDeviceCaptureSourceSelectionResult(); | |
| 69 | |
| 70 bool HasValue() const { return failed_constraint_name_ == nullptr; } | |
| 71 | |
| 72 // Convenience accessors for fields embedded in |capture_params|. | |
| 73 const media::VideoCaptureFormat& Format() const { | |
| 74 return capture_params_.requested_format; | |
| 75 } | |
| 76 int Width() const { | |
| 77 DCHECK(HasValue()); | |
| 78 return capture_params_.requested_format.frame_size.width(); | |
| 79 } | |
| 80 int Height() const { | |
| 81 DCHECK(HasValue()); | |
| 82 return capture_params_.requested_format.frame_size.height(); | |
| 83 } | |
| 84 float FrameRate() const { | |
| 85 DCHECK(HasValue()); | |
| 86 return capture_params_.requested_format.frame_rate; | |
| 87 } | |
| 88 media::PowerLineFrequency PowerLineFrequency() const { | |
| 89 DCHECK(HasValue()); | |
| 90 return capture_params_.power_line_frequency; | |
| 91 } | |
| 92 | |
| 93 // Other accessors. | |
| 94 const char* failed_constraint_name() const { return failed_constraint_name_; } | |
| 95 | |
| 96 const std::string& device_id() const { | |
| 97 DCHECK(HasValue()); | |
| 98 return device_id_; | |
| 99 } | |
| 100 | |
| 101 ::mojom::FacingMode facing_mode() const { | |
| 102 DCHECK(HasValue()); | |
| 103 return facing_mode_; | |
| 104 } | |
| 105 | |
| 106 const media::VideoCaptureParams& capture_params() const { | |
| 107 DCHECK(HasValue()); | |
| 108 return capture_params_; | |
| 109 } | |
| 110 | |
| 111 const rtc::Optional<bool>& noise_reduction() const { | |
| 112 DCHECK(HasValue()); | |
| 113 return noise_reduction_; | |
| 114 } | |
| 115 | |
| 116 private: | |
| 117 const char* failed_constraint_name_; | |
| 118 std::string device_id_; | |
| 119 ::mojom::FacingMode facing_mode_; | |
| 120 media::VideoCaptureParams capture_params_; | |
| 121 rtc::Optional<bool> noise_reduction_; | |
| 122 }; | |
| 123 | |
| 124 // This function performs source and source-settings selection based on | |
| 125 // the given |capabilities| and |constraints|. | |
| 126 // Chromium performs constraint resolution in two steps. First, a source and its | 44 // Chromium performs constraint resolution in two steps. First, a source and its |
| 127 // settings are selected; then a track is created, connected to the source, and | 45 // settings are selected, then track settings are selected based on the source |
| 128 // finally the track settings are selected. This function implements an | 46 // settings. This function implements both steps. Sources are not a user-visible |
| 129 // algorithm for the first step. Sources are not a user-visible concept, so the | 47 // concept, so the spec only specifies an algorithm for track settings. |
| 130 // spec only specifies an algorithm for track settings. | 48 // The algorithm for sources is compatible with the spec algorithm for tracks, |
| 131 // This algorithm for sources is compatible with the spec algorithm for tracks, | |
| 132 // as defined in https://w3c.github.io/mediacapture-main/#dfn-selectsettings, | 49 // as defined in https://w3c.github.io/mediacapture-main/#dfn-selectsettings, |
| 133 // but it is customized to account for differences between sources and tracks, | 50 // but it is customized to account for differences between sources and tracks, |
| 134 // and to break ties when multiple source settings are equally good according to | 51 // and to break ties when multiple source settings are equally good according to |
| 135 // the spec algorithm. | 52 // the spec algorithm. |
| 136 // The main difference between a source and a track with regards to the spec | 53 // The main difference between a source and a track with regards to the spec |
| 137 // algorithm is that a candidate source can support a range of values for some | 54 // algorithm is that a candidate source can support a range of values for some |
| 138 // constraints while a candidate track supports a single value. For example, | 55 // properties while a candidate track supports a single value. For example, |
| 139 // cropping allows a source with native resolution AxB to support the range of | 56 // cropping allows a source with native resolution AxB to support the range of |
| 140 // resolutions from 1x1 to AxB. | 57 // resolutions from 1x1 to AxB. |
| 141 // Only candidates that satisfy the basic constraint set are valid. If no | 58 // Only candidates that satisfy the basic constraint set are valid. If no |
| 142 // candidate can satisfy the basic constraint set, this function returns | 59 // candidate can satisfy the basic constraint set, this function returns |
| 143 // a result without a valid |settings| field and with the name of a failed | 60 // a result without value and with the name of a failed constraint accessible |
| 144 // constraint in field |failed_constraint_name|. If at least one candidate that | 61 // via the failed_constraint_name() method. If at least one candidate that |
| 145 // satisfies the basic constraint set can be found, this function returns a | 62 // satisfies the basic constraint set can be found, this function returns a |
| 146 // result with a valid |settings| field and a null |failed_constraint_name|. | 63 // result with a valid value. |
| 147 // If there are no candidates at all, this function returns a result with an | 64 // If there are no candidates at all, this function returns a result without |
| 148 // empty string in |failed_constraint_name| and without a valid |settings| | 65 // value and an empty failed constraint name. |
| 149 // field. | |
| 150 // The criteria to decide if a valid candidate is better than another one are as | 66 // The criteria to decide if a valid candidate is better than another one are as |
| 151 // follows: | 67 // follows: |
| 152 // 1. Given advanced constraint sets A[0],A[1]...,A[n], candidate C1 is better | 68 // 1. Given advanced constraint sets A[0],A[1]...,A[n], candidate C1 is better |
| 153 // than candidate C2 if C1 supports the first advanced set for which C1's | 69 // than candidate C2 if C1 supports the first advanced set for which C1's |
| 154 // support is different than C2's support. | 70 // support is different than C2's support. |
| 155 // Examples: | 71 // Examples: |
| 156 // * One advanced set, C1 supports it, and C2 does not. C1 is better. | 72 // * One advanced set, C1 supports it, and C2 does not. C1 is better. |
| 157 // * Two sets, C1 supports both, C2 supports only the first. C1 is better. | 73 // * Two sets, C1 supports both, C2 supports only the first. C1 is better. |
| 158 // * Three sets, C1 supports the first and second set, C2 supports the first | 74 // * Three sets, C1 supports the first and second set, C2 supports the first |
| 159 // and third set. C1 is better. | 75 // and third set. C1 is better. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 176 // native resolution of 1200x1200, while C2 has a native resolution of | 92 // native resolution of 1200x1200, while C2 has a native resolution of |
| 177 // 2000x2000, then C1 is better because it can support the ideal value with | 93 // 2000x2000, then C1 is better because it can support the ideal value with |
| 178 // lower resource usage. Both C1 and C2 are better than a candidate C3 with | 94 // lower resource usage. Both C1 and C2 are better than a candidate C3 with |
| 179 // a native resolution of 999x999, since C3 has a nonzero distance to the | 95 // a native resolution of 999x999, since C3 has a nonzero distance to the |
| 180 // ideal value and thus has worse fitness according to step 2, even if C3's | 96 // ideal value and thus has worse fitness according to step 2, even if C3's |
| 181 // native fitness is better than C1's and C2's. | 97 // native fitness is better than C1's and C2's. |
| 182 // 5. C1 is better than C2 if its settings are closer to certain default | 98 // 5. C1 is better than C2 if its settings are closer to certain default |
| 183 // settings that include the device ID, power-line frequency, noise | 99 // settings that include the device ID, power-line frequency, noise |
| 184 // reduction, resolution, and frame rate, in that order. Note that there is | 100 // reduction, resolution, and frame rate, in that order. Note that there is |
| 185 // no default facing mode or aspect ratio. | 101 // no default facing mode or aspect ratio. |
| 186 VideoDeviceCaptureSourceSelectionResult CONTENT_EXPORT | 102 // This function uses the SelectVideoTrackAdapterSettings function to compute |
| 187 SelectVideoDeviceCaptureSourceSettings( | 103 // some track-specific settings. These are available in the returned value via |
| 104 // the track_adapter_settings() accessor. For more details about the algorithm |
| 105 // for track adapter settings, see the SelectVideoTrackAdapterSettings |
| 106 // documentation. |
| 107 VideoCaptureSettings CONTENT_EXPORT SelectSettingsVideoDeviceCapture( |
| 188 const VideoDeviceCaptureCapabilities& capabilities, | 108 const VideoDeviceCaptureCapabilities& capabilities, |
| 189 const blink::WebMediaConstraints& constraints); | 109 const blink::WebMediaConstraints& constraints); |
| 190 | 110 |
| 191 } // namespace content | 111 } // namespace content |
| 192 | 112 |
| 193 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_VIDEO_DEVICE_H_ | 113 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_CONSTRAINTS_UTIL_VIDEO_DEVICE_H_ |
| OLD | NEW |