| OLD | NEW |
| 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_VIDEO_SOURCE_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_ | 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/optional.h" |
| 15 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 16 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 17 #include "content/common/media/video_capture.h" | 18 #include "content/common/media/video_capture.h" |
| 18 #include "content/public/renderer/media_stream_video_sink.h" | |
| 19 #include "content/renderer/media/media_stream_source.h" | 19 #include "content/renderer/media/media_stream_source.h" |
| 20 #include "content/renderer/media/secure_display_link_tracker.h" | 20 #include "content/renderer/media/secure_display_link_tracker.h" |
| 21 #include "media/base/video_frame.h" | 21 #include "media/base/video_frame.h" |
| 22 #include "media/capture/video_capture_types.h" | 22 #include "media/capture/video_capture_types.h" |
| 23 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | 23 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
| 24 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 24 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 25 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 25 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 26 | 26 |
| 27 namespace content { | 27 namespace content { |
| 28 | 28 |
| 29 class MediaStreamVideoTrack; | 29 class MediaStreamVideoTrack; |
| 30 class VideoTrackAdapter; | 30 class VideoTrackAdapter; |
| 31 struct VideoTrackAdapterSettings; |
| 32 |
| 33 CONTENT_EXPORT bool IsOldVideoConstraints(); |
| 31 | 34 |
| 32 // MediaStreamVideoSource is an interface used for sending video frames to a | 35 // MediaStreamVideoSource is an interface used for sending video frames to a |
| 33 // MediaStreamVideoTrack. | 36 // MediaStreamVideoTrack. |
| 34 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html | 37 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html |
| 35 // The purpose of this base class is to be able to implement different | 38 // The purpose of this base class is to be able to implement different |
| 36 // MediaStreaVideoSources such as local video capture, video sources received | 39 // MediaStreaVideoSources such as local video capture, video sources received |
| 37 // on a PeerConnection or a source created in NaCl. | 40 // on a PeerConnection or a source created in NaCl. |
| 38 // All methods calls will be done from the main render thread. | 41 // All methods calls will be done from the main render thread. |
| 39 // | 42 // |
| 40 // When the first track is added to the source by calling AddTrack, the | 43 // When the first track is added to the source by calling AddTrack, the |
| (...skipping 21 matching lines...) Expand all Loading... |
| 62 | 65 |
| 63 MediaStreamVideoSource(); | 66 MediaStreamVideoSource(); |
| 64 ~MediaStreamVideoSource() override; | 67 ~MediaStreamVideoSource() override; |
| 65 | 68 |
| 66 // Returns the MediaStreamVideoSource object owned by |source|. | 69 // Returns the MediaStreamVideoSource object owned by |source|. |
| 67 static MediaStreamVideoSource* GetVideoSource( | 70 static MediaStreamVideoSource* GetVideoSource( |
| 68 const blink::WebMediaStreamSource& source); | 71 const blink::WebMediaStreamSource& source); |
| 69 | 72 |
| 70 // Puts |track| in the registered tracks list. | 73 // Puts |track| in the registered tracks list. |
| 71 void AddTrack(MediaStreamVideoTrack* track, | 74 void AddTrack(MediaStreamVideoTrack* track, |
| 75 const VideoTrackAdapterSettings& track_adapter_settings, |
| 72 const VideoCaptureDeliverFrameCB& frame_callback, | 76 const VideoCaptureDeliverFrameCB& frame_callback, |
| 73 const blink::WebMediaConstraints& constraints, | |
| 74 const ConstraintsCallback& callback); | 77 const ConstraintsCallback& callback); |
| 78 // TODO(guidou): Remove this method. http://crbug.com/706408 |
| 79 void AddTrackLegacy(MediaStreamVideoTrack* track, |
| 80 const VideoCaptureDeliverFrameCB& frame_callback, |
| 81 const blink::WebMediaConstraints& constraints, |
| 82 const ConstraintsCallback& callback); |
| 75 void RemoveTrack(MediaStreamVideoTrack* track); | 83 void RemoveTrack(MediaStreamVideoTrack* track); |
| 76 | 84 |
| 77 // Called by |track| to notify the source whether it has any paths to a | 85 // Called by |track| to notify the source whether it has any paths to a |
| 78 // consuming endpoint. | 86 // consuming endpoint. |
| 79 void UpdateHasConsumers(MediaStreamVideoTrack* track, bool has_consumers); | 87 void UpdateHasConsumers(MediaStreamVideoTrack* track, bool has_consumers); |
| 80 | 88 |
| 81 void UpdateCapturingLinkSecure(MediaStreamVideoTrack* track, bool is_secure); | 89 void UpdateCapturingLinkSecure(MediaStreamVideoTrack* track, bool is_secure); |
| 82 | 90 |
| 83 // Request underlying source to capture a new frame. | 91 // Request underlying source to capture a new frame. |
| 84 virtual void RequestRefreshFrame() {} | 92 virtual void RequestRefreshFrame() {} |
| 85 | 93 |
| 86 // Returns the task runner where video frames will be delivered on. | 94 // Returns the task runner where video frames will be delivered on. |
| 87 base::SingleThreadTaskRunner* io_task_runner() const; | 95 base::SingleThreadTaskRunner* io_task_runner() const; |
| 88 | 96 |
| 89 const media::VideoCaptureFormat* GetCurrentFormat() const; | 97 base::Optional<media::VideoCaptureFormat> GetCurrentFormat() const; |
| 90 | 98 |
| 91 base::WeakPtr<MediaStreamVideoSource> GetWeakPtr() { | 99 base::WeakPtr<MediaStreamVideoSource> GetWeakPtr() { |
| 92 return weak_factory_.GetWeakPtr(); | 100 return weak_factory_.GetWeakPtr(); |
| 93 } | 101 } |
| 94 | 102 |
| 95 protected: | 103 protected: |
| 96 void DoStopSource() override; | 104 void DoStopSource() override; |
| 97 | 105 |
| 98 // Sets ready state and notifies the ready state to all registered tracks. | 106 // Sets ready state and notifies the ready state to all registered tracks. |
| 99 virtual void SetReadyState(blink::WebMediaStreamSource::ReadyState state); | 107 virtual void SetReadyState(blink::WebMediaStreamSource::ReadyState state); |
| 100 | 108 |
| 101 // Sets muted state and notifies it to all registered tracks. | 109 // Sets muted state and notifies it to all registered tracks. |
| 102 virtual void SetMutedState(bool state); | 110 virtual void SetMutedState(bool state); |
| 103 | 111 |
| 104 // An implementation must fetch the formats that can currently be used by | 112 // An implementation must fetch the formats that can currently be used by |
| 105 // the source and call OnSupportedFormats when done. | 113 // the source and call OnSupportedFormats when done. |
| 106 // |max_requested_height| and |max_requested_width| is the max height and | 114 // |max_requested_height| and |max_requested_width| is the max height and |
| 107 // width set as a mandatory constraint if set when calling | 115 // width set as a mandatory constraint if set when calling |
| 108 // MediaStreamVideoSource::AddTrack. If max height and max width is not set | 116 // MediaStreamVideoSource::AddTrack. If max height and max width is not set |
| 109 // |max_requested_height| and |max_requested_width| are 0. | 117 // |max_requested_height| and |max_requested_width| are 0. |
| 118 // TODO(guidou): Remove when the standard constraints code stabilizes. |
| 119 // http://crbug.com/706408 |
| 110 virtual void GetCurrentSupportedFormats( | 120 virtual void GetCurrentSupportedFormats( |
| 111 int max_requested_width, | 121 int max_requested_width, |
| 112 int max_requested_height, | 122 int max_requested_height, |
| 113 double max_requested_frame_rate, | 123 double max_requested_frame_rate, |
| 114 const VideoCaptureDeviceFormatsCB& callback) = 0; | 124 const VideoCaptureDeviceFormatsCB& callback) = 0; |
| 115 | 125 |
| 126 // TODO(guidou): Rename to GetCurrentFormat. http://crbug.com/706804 |
| 127 virtual base::Optional<media::VideoCaptureFormat> GetCurrentFormatImpl() |
| 128 const; |
| 129 |
| 116 // An implementation must start capturing frames using the requested | 130 // An implementation must start capturing frames using the requested |
| 117 // |format|. The fulfilled |constraints| are provided as additional context, | 131 // |format|. The fulfilled |constraints| are provided as additional context, |
| 118 // and may be used to modify the behavior of the source. When the source has | 132 // and may be used to modify the behavior of the source. When the source has |
| 119 // started or the source failed to start OnStartDone must be called. An | 133 // started or the source failed to start OnStartDone must be called. An |
| 120 // implementation must call |frame_callback| on the IO thread with the | 134 // implementation must call |frame_callback| on the IO thread with the |
| 121 // captured frames. | 135 // captured frames. |
| 136 // TODO(guidou): Remove |format| and |constraints| parameters. |
| 137 // http://crbug.com/706408 |
| 122 virtual void StartSourceImpl( | 138 virtual void StartSourceImpl( |
| 123 const media::VideoCaptureFormat& format, | 139 const media::VideoCaptureFormat& format, |
| 124 const blink::WebMediaConstraints& constraints, | 140 const blink::WebMediaConstraints& constraints, |
| 125 const VideoCaptureDeliverFrameCB& frame_callback) = 0; | 141 const VideoCaptureDeliverFrameCB& frame_callback) = 0; |
| 126 void OnStartDone(MediaStreamRequestResult result); | 142 void OnStartDone(MediaStreamRequestResult result); |
| 127 | 143 |
| 128 // An implementation must immediately stop capture video frames and must not | 144 // An implementation must immediately stop capture video frames and must not |
| 129 // call OnSupportedFormats after this method has been called. After this | 145 // call OnSupportedFormats after this method has been called. After this |
| 130 // method has been called, MediaStreamVideoSource may be deleted. | 146 // method has been called, MediaStreamVideoSource may be deleted. |
| 131 virtual void StopSourceImpl() = 0; | 147 virtual void StopSourceImpl() = 0; |
| 132 | 148 |
| 133 // Optionally overridden by subclasses to act on whether there are any | 149 // Optionally overridden by subclasses to act on whether there are any |
| 134 // consumers present. When none are present, the source can stop delivering | 150 // consumers present. When none are present, the source can stop delivering |
| 135 // frames, giving it the option of running in an "idle" state to minimize | 151 // frames, giving it the option of running in an "idle" state to minimize |
| 136 // resource usage. | 152 // resource usage. |
| 137 virtual void OnHasConsumers(bool has_consumers) {} | 153 virtual void OnHasConsumers(bool has_consumers) {} |
| 138 | 154 |
| 139 // Optionally overridden by subclasses to act on whether the capturing link | 155 // Optionally overridden by subclasses to act on whether the capturing link |
| 140 // has become secure or insecure. | 156 // has become secure or insecure. |
| 141 virtual void OnCapturingLinkSecured(bool is_secure) {} | 157 virtual void OnCapturingLinkSecured(bool is_secure) {} |
| 142 | 158 |
| 143 enum State { | 159 enum State { |
| 144 NEW, | 160 NEW, |
| 161 // TODO(guidou): Remove this state. http://crbug.com/706408 |
| 145 RETRIEVING_CAPABILITIES, | 162 RETRIEVING_CAPABILITIES, |
| 146 STARTING, | 163 STARTING, |
| 147 STARTED, | 164 STARTED, |
| 148 ENDED | 165 ENDED |
| 149 }; | 166 }; |
| 150 State state() const { return state_; } | 167 State state() const { return state_; } |
| 151 | 168 |
| 152 private: | 169 private: |
| 153 void OnSupportedFormats(const media::VideoCaptureFormats& formats); | 170 void OnSupportedFormats(const media::VideoCaptureFormats& formats); |
| 154 | 171 |
| 155 // Finds the first WebMediaConstraints in |track_descriptors_| that allows | 172 // Finds the first WebMediaConstraints in |track_descriptors_| that allows |
| 156 // the use of one of the |formats|. |best_format| and |fulfilled_constraints| | 173 // the use of one of the |formats|. |best_format| and |fulfilled_constraints| |
| 157 // are set to the results of this search-and-match operation. Returns false | 174 // are set to the results of this search-and-match operation. Returns false |
| 158 // if no WebMediaConstraints allow the use any of the |formats|. | 175 // if no WebMediaConstraints allow the use any of the |formats|. |
| 159 bool FindBestFormatWithConstraints( | 176 bool FindBestFormatWithConstraints( |
| 160 const media::VideoCaptureFormats& formats, | 177 const media::VideoCaptureFormats& formats, |
| 161 media::VideoCaptureFormat* best_format, | 178 media::VideoCaptureFormat* best_format, |
| 162 blink::WebMediaConstraints* fulfilled_constraints); | 179 blink::WebMediaConstraints* fulfilled_constraints); |
| 163 | 180 |
| 164 // Trigger all cached callbacks from AddTrack. AddTrack is successful | 181 // Trigger all cached callbacks from AddTrack. AddTrack is successful |
| 165 // if the capture delegate has started and the constraints provided in | 182 // if the capture delegate has started and the constraints provided in |
| 166 // AddTrack match the format that was used to start the device. | 183 // AddTrack match the format that was used to start the device. |
| 167 // Note that it must be ok to delete the MediaStreamVideoSource object | 184 // Note that it must be ok to delete the MediaStreamVideoSource object |
| 168 // in the context of the callback. If gUM fail, the implementation will | 185 // in the context of the callback. If gUM fails, the implementation will |
| 169 // simply drop the references to the blink source and track which will lead | 186 // simply drop the references to the blink source and track which will lead |
| 170 // to that this object is deleted. | 187 // to this object being deleted. |
| 171 void FinalizeAddTrack(); | 188 void FinalizeAddTrack(); |
| 189 // TODO(guidou): Remove this method. http://crbug.com/706408 |
| 190 void FinalizeAddTrackLegacy(); |
| 172 | 191 |
| 173 State state_; | 192 State state_; |
| 174 | 193 |
| 194 // TODO(guidou): Remove this field. http://crbug.com/706408 |
| 175 media::VideoCaptureFormat current_format_; | 195 media::VideoCaptureFormat current_format_; |
| 176 | 196 |
| 177 struct TrackDescriptor { | 197 struct TrackDescriptor { |
| 178 TrackDescriptor(MediaStreamVideoTrack* track, | 198 TrackDescriptor(MediaStreamVideoTrack* track, |
| 179 const VideoCaptureDeliverFrameCB& frame_callback, | 199 const VideoCaptureDeliverFrameCB& frame_callback, |
| 180 const blink::WebMediaConstraints& constraints, | 200 const blink::WebMediaConstraints& constraints, |
| 181 const ConstraintsCallback& callback); | 201 const ConstraintsCallback& callback); |
| 182 TrackDescriptor(const TrackDescriptor& other); | 202 TrackDescriptor(MediaStreamVideoTrack* track, |
| 203 const VideoCaptureDeliverFrameCB& frame_callback, |
| 204 std::unique_ptr<VideoTrackAdapterSettings> adapter_settings, |
| 205 const ConstraintsCallback& callback); |
| 206 TrackDescriptor(TrackDescriptor&& other); |
| 207 TrackDescriptor& operator=(TrackDescriptor&& other); |
| 183 ~TrackDescriptor(); | 208 ~TrackDescriptor(); |
| 184 | 209 |
| 185 MediaStreamVideoTrack* track; | 210 MediaStreamVideoTrack* track; |
| 186 VideoCaptureDeliverFrameCB frame_callback; | 211 VideoCaptureDeliverFrameCB frame_callback; |
| 212 // TODO(guidou): remove this field. http://crbug.com/706408 |
| 187 blink::WebMediaConstraints constraints; | 213 blink::WebMediaConstraints constraints; |
| 214 // TODO(guidou): Make |adapter_settings| a regular field instead of a |
| 215 // unique_ptr. |
| 216 std::unique_ptr<VideoTrackAdapterSettings> adapter_settings; |
| 188 ConstraintsCallback callback; | 217 ConstraintsCallback callback; |
| 189 }; | 218 }; |
| 190 std::vector<TrackDescriptor> track_descriptors_; | 219 std::vector<TrackDescriptor> track_descriptors_; |
| 191 | 220 |
| 192 media::VideoCaptureFormats supported_formats_; | 221 media::VideoCaptureFormats supported_formats_; |
| 193 | 222 |
| 194 // |track_adapter_| delivers video frames to the tracks on the IO-thread. | 223 // |track_adapter_| delivers video frames to the tracks on the IO-thread. |
| 195 const scoped_refptr<VideoTrackAdapter> track_adapter_; | 224 const scoped_refptr<VideoTrackAdapter> track_adapter_; |
| 196 | 225 |
| 197 // Tracks that currently are connected to this source. | 226 // Tracks that currently are connected to this source. |
| 198 std::vector<MediaStreamVideoTrack*> tracks_; | 227 std::vector<MediaStreamVideoTrack*> tracks_; |
| 199 | 228 |
| 200 // Tracks that have no paths to a consuming endpoint, and so do not need | 229 // Tracks that have no paths to a consuming endpoint, and so do not need |
| 201 // frames delivered from the source. This is a subset of |tracks_|. | 230 // frames delivered from the source. This is a subset of |tracks_|. |
| 202 std::vector<MediaStreamVideoTrack*> suspended_tracks_; | 231 std::vector<MediaStreamVideoTrack*> suspended_tracks_; |
| 203 | 232 |
| 204 // This is used for tracking if all connected video sinks are secure. | 233 // This is used for tracking if all connected video sinks are secure. |
| 205 SecureDisplayLinkTracker<MediaStreamVideoTrack> secure_tracker_; | 234 SecureDisplayLinkTracker<MediaStreamVideoTrack> secure_tracker_; |
| 206 | 235 |
| 207 // NOTE: Weak pointers must be invalidated before all other member variables. | 236 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 208 base::WeakPtrFactory<MediaStreamVideoSource> weak_factory_; | 237 base::WeakPtrFactory<MediaStreamVideoSource> weak_factory_; |
| 209 | 238 |
| 210 DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoSource); | 239 DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoSource); |
| 211 }; | 240 }; |
| 212 | 241 |
| 213 } // namespace content | 242 } // namespace content |
| 214 | 243 |
| 215 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_ | 244 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_ |
| OLD | NEW |