OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <memory> | 9 #include <memory> |
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/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/process/process.h" | 15 #include "base/process/process.h" |
| 16 #include "content/browser/renderer_host/media/buildable_video_capture_device.h" |
16 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 17 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
17 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
18 #include "content/common/media/video_capture.h" | 19 #include "content/common/media/video_capture.h" |
| 20 #include "content/public/common/media_stream_request.h" |
19 #include "media/capture/video/video_frame_receiver.h" | 21 #include "media/capture/video/video_frame_receiver.h" |
20 #include "media/capture/video_capture_types.h" | 22 #include "media/capture/video_capture_types.h" |
21 | 23 |
22 namespace content { | 24 namespace content { |
23 | 25 |
24 // Implementation of media::VideoFrameReceiver that distributes received frames | 26 // Implementation of media::VideoFrameReceiver that distributes received frames |
25 // to potentially multiple connected clients. | 27 // to potentially multiple connected clients. |
26 class CONTENT_EXPORT VideoCaptureController : public media::VideoFrameReceiver { | 28 // A call to CreateAndStartDeviceAsync() asynchronously brings up the device. If |
| 29 // CreateAndStartDeviceAsync() has been called, ReleaseDeviceAsync() must be |
| 30 // called before releasing the instance. |
| 31 // Instances must be RefCountedThreadSafe, because an owner |
| 32 // (VideoCaptureManager) wants to be able to release its reference during an |
| 33 // (asynchronously executing) run of CreateAndStartDeviceAsync(). To this end, |
| 34 // the owner passes in the shared ownership as part of |context_reference| into |
| 35 // CreateAndStartDeviceAsync(). |
| 36 class CONTENT_EXPORT VideoCaptureController |
| 37 : public media::VideoFrameReceiver, |
| 38 public base::RefCountedThreadSafe<VideoCaptureController> { |
27 public: | 39 public: |
28 VideoCaptureController(); | 40 VideoCaptureController( |
29 ~VideoCaptureController() override; | 41 const std::string& device_id, |
| 42 MediaStreamType stream_type, |
| 43 const media::VideoCaptureParams& params, |
| 44 std::unique_ptr<BuildableVideoCaptureDevice> buildable_device); |
30 | 45 |
31 base::WeakPtr<VideoCaptureController> GetWeakPtrForIOThread(); | 46 base::WeakPtr<VideoCaptureController> GetWeakPtrForIOThread(); |
32 | 47 |
33 // Factory code creating instances of VideoCaptureController may optionally | 48 // Factory code creating instances of VideoCaptureController may optionally |
34 // set a VideoFrameConsumerFeedbackObserver. Setting the observer is done in | 49 // set a VideoFrameConsumerFeedbackObserver. Setting the observer is done in |
35 // this method separate from the constructor to allow clients to create and | 50 // this method separate from the constructor to allow clients to create and |
36 // use instances before they can provide the observer. (This is the case with | 51 // use instances before they can provide the observer. (This is the case with |
37 // VideoCaptureManager). | 52 // VideoCaptureManager). |
38 void SetConsumerFeedbackObserver( | 53 void SetConsumerFeedbackObserver( |
39 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> observer); | 54 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> observer); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 std::unique_ptr< | 112 std::unique_ptr< |
98 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> | 113 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> |
99 buffer_read_permission, | 114 buffer_read_permission, |
100 media::mojom::VideoFrameInfoPtr frame_info) override; | 115 media::mojom::VideoFrameInfoPtr frame_info) override; |
101 void OnBufferRetired(int buffer_id) override; | 116 void OnBufferRetired(int buffer_id) override; |
102 void OnError() override; | 117 void OnError() override; |
103 void OnLog(const std::string& message) override; | 118 void OnLog(const std::string& message) override; |
104 void OnStarted() override; | 119 void OnStarted() override; |
105 void OnStartedUsingGpuDecode() override; | 120 void OnStartedUsingGpuDecode() override; |
106 | 121 |
| 122 void CreateAndStartDeviceAsync(const media::VideoCaptureParams& params, |
| 123 DeviceBuildContext context); |
| 124 void ReleaseDeviceAsync(base::OnceClosure done_cb); |
| 125 bool IsDeviceAlive() const; |
| 126 bool CorrespondsToController(const VideoCaptureController* controller) const; |
| 127 void GetPhotoCapabilities( |
| 128 media::VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) const; |
| 129 void SetPhotoOptions( |
| 130 media::mojom::PhotoSettingsPtr settings, |
| 131 media::VideoCaptureDevice::SetPhotoOptionsCallback callback); |
| 132 void TakePhoto(media::VideoCaptureDevice::TakePhotoCallback callback); |
| 133 void MaybeSuspend(); |
| 134 void Resume(); |
| 135 void RequestRefreshFrame(); |
| 136 void SetDesktopCaptureWindowIdAsync(gfx::NativeViewId window_id, |
| 137 base::OnceClosure done_cb); |
| 138 int serial_id() const { return serial_id_; } |
| 139 const std::string& device_id() const { return device_id_; } |
| 140 MediaStreamType stream_type() const { return stream_type_; } |
| 141 const media::VideoCaptureParams& parameters() const { return parameters_; } |
| 142 |
107 private: | 143 private: |
| 144 friend class base::RefCountedThreadSafe<VideoCaptureController>; |
108 struct ControllerClient; | 145 struct ControllerClient; |
109 typedef std::list<std::unique_ptr<ControllerClient>> ControllerClients; | 146 typedef std::list<std::unique_ptr<ControllerClient>> ControllerClients; |
110 | 147 |
111 class BufferContext { | 148 class BufferContext { |
112 public: | 149 public: |
113 BufferContext( | 150 BufferContext( |
114 int buffer_context_id, | 151 int buffer_context_id, |
115 int buffer_id, | 152 int buffer_id, |
116 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer, | 153 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer, |
117 mojo::ScopedSharedBufferHandle handle); | 154 mojo::ScopedSharedBufferHandle handle); |
(...skipping 28 matching lines...) Expand all Loading... |
146 int frame_feedback_id_; | 183 int frame_feedback_id_; |
147 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer_; | 184 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer_; |
148 mojo::ScopedSharedBufferHandle buffer_handle_; | 185 mojo::ScopedSharedBufferHandle buffer_handle_; |
149 double max_consumer_utilization_; | 186 double max_consumer_utilization_; |
150 int consumer_hold_count_; | 187 int consumer_hold_count_; |
151 std::unique_ptr< | 188 std::unique_ptr< |
152 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> | 189 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> |
153 buffer_read_permission_; | 190 buffer_read_permission_; |
154 }; | 191 }; |
155 | 192 |
| 193 ~VideoCaptureController() override; |
| 194 |
156 // Find a client of |id| and |handler| in |clients|. | 195 // Find a client of |id| and |handler| in |clients|. |
157 ControllerClient* FindClient(VideoCaptureControllerID id, | 196 ControllerClient* FindClient(VideoCaptureControllerID id, |
158 VideoCaptureControllerEventHandler* handler, | 197 VideoCaptureControllerEventHandler* handler, |
159 const ControllerClients& clients); | 198 const ControllerClients& clients); |
160 | 199 |
161 // Find a client of |session_id| in |clients|. | 200 // Find a client of |session_id| in |clients|. |
162 ControllerClient* FindClient(int session_id, | 201 ControllerClient* FindClient(int session_id, |
163 const ControllerClients& clients); | 202 const ControllerClients& clients); |
164 | 203 |
165 std::vector<BufferContext>::iterator FindBufferContextFromBufferContextId( | 204 std::vector<BufferContext>::iterator FindBufferContextFromBufferContextId( |
166 int buffer_context_id); | 205 int buffer_context_id); |
167 std::vector<BufferContext>::iterator FindUnretiredBufferContextFromBufferId( | 206 std::vector<BufferContext>::iterator FindUnretiredBufferContextFromBufferId( |
168 int buffer_id); | 207 int buffer_id); |
169 | 208 |
170 void OnClientFinishedConsumingBuffer(ControllerClient* client, | 209 void OnClientFinishedConsumingBuffer(ControllerClient* client, |
171 int buffer_id, | 210 int buffer_id, |
172 double consumer_resource_utilization); | 211 double consumer_resource_utilization); |
173 void ReleaseBufferContext( | 212 void ReleaseBufferContext( |
174 const std::vector<BufferContext>::iterator& buffer_state_iter); | 213 const std::vector<BufferContext>::iterator& buffer_state_iter); |
175 | 214 |
176 using EventHandlerAction = | 215 using EventHandlerAction = |
177 base::Callback<void(VideoCaptureControllerEventHandler* client, | 216 base::Callback<void(VideoCaptureControllerEventHandler* client, |
178 VideoCaptureControllerID id)>; | 217 VideoCaptureControllerID id)>; |
179 void PerformForClientsWithOpenSession(EventHandlerAction action); | 218 void PerformForClientsWithOpenSession(EventHandlerAction action); |
180 | 219 |
| 220 const int serial_id_; |
| 221 const std::string device_id_; |
| 222 const MediaStreamType stream_type_; |
| 223 const media::VideoCaptureParams parameters_; |
| 224 std::unique_ptr<BuildableVideoCaptureDevice> buildable_device_; |
| 225 |
181 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> | 226 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> |
182 consumer_feedback_observer_; | 227 consumer_feedback_observer_; |
183 | 228 |
184 std::vector<BufferContext> buffer_contexts_; | 229 std::vector<BufferContext> buffer_contexts_; |
185 | 230 |
186 // All clients served by this controller. | 231 // All clients served by this controller. |
187 ControllerClients controller_clients_; | 232 ControllerClients controller_clients_; |
188 | 233 |
189 // Takes on only the states 'STARTING', 'STARTED' and 'ERROR'. 'ERROR' is an | 234 // Takes on only the states 'STARTING', 'STARTED' and 'ERROR'. 'ERROR' is an |
190 // absorbing state which stops the flow of data to clients. | 235 // absorbing state which stops the flow of data to clients. |
191 VideoCaptureState state_; | 236 VideoCaptureState state_; |
192 | 237 |
193 int next_buffer_context_id_ = 0; | 238 int next_buffer_context_id_ = 0; |
194 | 239 |
195 // True if the controller has received a video frame from the device. | 240 // True if the controller has received a video frame from the device. |
196 bool has_received_frames_; | 241 bool has_received_frames_; |
197 | 242 |
198 media::VideoCaptureFormat video_capture_format_; | 243 media::VideoCaptureFormat video_capture_format_; |
199 | 244 |
200 base::WeakPtrFactory<VideoCaptureController> weak_ptr_factory_; | 245 base::WeakPtrFactory<VideoCaptureController> weak_ptr_factory_; |
201 | 246 |
202 DISALLOW_COPY_AND_ASSIGN(VideoCaptureController); | 247 DISALLOW_COPY_AND_ASSIGN(VideoCaptureController); |
203 }; | 248 }; |
204 | 249 |
205 } // namespace content | 250 } // namespace content |
206 | 251 |
207 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 252 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
OLD | NEW |