Chromium Code Reviews| 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 // | |
| 5 // VideoCaptureController is the glue between a VideoCaptureDevice and all | |
| 6 // VideoCaptureHosts that have connected to it. A controller exists on behalf of | |
| 7 // one (and only one) VideoCaptureDevice; both are owned by the | |
| 8 // VideoCaptureManager. | |
| 9 // | |
| 10 // The VideoCaptureController is responsible for: | |
| 11 // | |
| 12 // * Allocating and keeping track of shared memory buffers, and filling them | |
| 13 // with I420 video frames for IPC communication between VideoCaptureHost (in | |
| 14 // the browser process) and VideoCaptureMessageFilter (in the renderer | |
| 15 // process). | |
| 16 // * Broadcasting the events from a single VideoCaptureDevice, fanning them | |
| 17 // out to multiple clients. | |
| 18 // * Keeping track of the clients on behalf of the VideoCaptureManager, making | |
| 19 // it possible for the Manager to delete the Controller and its Device when | |
| 20 // there are no clients left. | |
| 21 // | |
| 22 // Interactions between VideoCaptureController and other classes: | |
| 23 // | |
| 24 // * VideoCaptureController indirectly observes a VideoCaptureDevice | |
| 25 // by means of its proxy, VideoCaptureDeviceClient, which implements | |
| 26 // the VideoCaptureDevice::Client interface. The proxy forwards | |
| 27 // observed events to the VideoCaptureController on the IO thread. | |
| 28 // * A VideoCaptureController interacts with its clients (VideoCaptureHosts) | |
| 29 // via the VideoCaptureControllerEventHandler interface. | |
| 30 // * Conversely, a VideoCaptureControllerEventHandler (typically, | |
| 31 // VideoCaptureHost) will interact directly with VideoCaptureController to | |
| 32 // return leased buffers by means of the ReturnBuffer() public method of | |
| 33 // VCC. | |
| 34 // * VideoCaptureManager (which owns the VCC) interacts directly with | |
| 35 // VideoCaptureController through its public methods, to add and remove | |
| 36 // clients. | |
| 37 // | |
| 38 // VideoCaptureController is not thread safe and operates on the IO thread only. | |
| 39 | 4 |
| 40 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
| 41 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
| 42 | 7 |
| 43 #include <list> | 8 #include <list> |
| 44 #include <memory> | 9 #include <memory> |
| 45 | 10 |
| 46 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 47 #include "base/macros.h" | 12 #include "base/macros.h" |
| 48 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 49 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 50 #include "base/process/process.h" | 15 #include "base/process/process.h" |
| 51 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" | 16 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" |
| 52 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 53 #include "content/common/media/video_capture.h" | 18 #include "content/common/media/video_capture.h" |
| 54 #include "media/capture/video/video_frame_receiver.h" | 19 #include "media/capture/video/video_frame_receiver.h" |
| 55 #include "media/capture/video_capture_types.h" | 20 #include "media/capture/video_capture_types.h" |
| 56 | 21 |
| 57 namespace media { | |
| 58 class FrameBufferPool; | |
| 59 } | |
| 60 | |
| 61 namespace content { | 22 namespace content { |
| 62 | 23 |
| 24 // Implementation of media::VideoFrameReceiver that distributes received frames | |
| 25 // to potentially multiple connected clients. | |
| 63 class CONTENT_EXPORT VideoCaptureController : public media::VideoFrameReceiver { | 26 class CONTENT_EXPORT VideoCaptureController : public media::VideoFrameReceiver { |
| 64 public: | 27 public: |
| 65 VideoCaptureController(); | 28 VideoCaptureController(); |
| 66 ~VideoCaptureController() override; | 29 ~VideoCaptureController() override; |
| 67 | 30 |
| 68 base::WeakPtr<VideoCaptureController> GetWeakPtrForIOThread(); | 31 base::WeakPtr<VideoCaptureController> GetWeakPtrForIOThread(); |
| 69 | 32 |
| 70 // A FrameBufferPool may optionally be set in order to receive notifications | |
| 71 // when a frame is starting to get consumed and has finished getting consumed. | |
| 72 void SetFrameBufferPool( | |
| 73 std::unique_ptr<media::FrameBufferPool> frame_buffer_pool); | |
| 74 | |
| 75 // Factory code creating instances of VideoCaptureController may optionally | 33 // Factory code creating instances of VideoCaptureController may optionally |
| 76 // set a VideoFrameConsumerFeedbackObserver. Setting the observer is done in | 34 // set a VideoFrameConsumerFeedbackObserver. Setting the observer is done in |
| 77 // this method separate from the constructor to allow clients to create and | 35 // this method separate from the constructor to allow clients to create and |
| 78 // use instances before they can provide the observer. (This is the case with | 36 // use instances before they can provide the observer. (This is the case with |
| 79 // VideoCaptureManager). | 37 // VideoCaptureManager). |
| 80 void SetConsumerFeedbackObserver( | 38 void SetConsumerFeedbackObserver( |
| 81 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> observer); | 39 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> observer); |
| 82 | 40 |
| 83 // Start video capturing and try to use the resolution specified in |params|. | 41 // Start video capturing and try to use the resolution specified in |params|. |
| 84 // Buffers will be shared to the client as necessary. The client will continue | 42 // Buffers will be shared to the client as necessary. The client will continue |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 void ReturnBuffer(VideoCaptureControllerID id, | 80 void ReturnBuffer(VideoCaptureControllerID id, |
| 123 VideoCaptureControllerEventHandler* event_handler, | 81 VideoCaptureControllerEventHandler* event_handler, |
| 124 int buffer_id, | 82 int buffer_id, |
| 125 double consumer_resource_utilization); | 83 double consumer_resource_utilization); |
| 126 | 84 |
| 127 const media::VideoCaptureFormat& GetVideoCaptureFormat() const; | 85 const media::VideoCaptureFormat& GetVideoCaptureFormat() const; |
| 128 | 86 |
| 129 bool has_received_frames() const { return has_received_frames_; } | 87 bool has_received_frames() const { return has_received_frames_; } |
| 130 | 88 |
| 131 // Implementation of media::VideoFrameReceiver interface: | 89 // Implementation of media::VideoFrameReceiver interface: |
| 132 void OnIncomingCapturedVideoFrame( | 90 void OnNewBufferHandle( |
| 133 media::VideoCaptureDevice::Client::Buffer buffer, | 91 int buffer_id, |
| 134 scoped_refptr<media::VideoFrame> frame) override; | 92 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer::HandleProvider> |
| 93 handle_provider) override; | |
| 94 void OnFrameReadyInBuffer( | |
| 95 int buffer_id, | |
| 96 int frame_feedback_id, | |
| 97 std::unique_ptr< | |
| 98 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> | |
| 99 buffer_read_permission, | |
| 100 media::mojom::VideoFrameInfoPtr frame_info) override; | |
| 101 void OnBufferRetired(int buffer_id) override; | |
| 135 void OnError() override; | 102 void OnError() override; |
| 136 void OnLog(const std::string& message) override; | 103 void OnLog(const std::string& message) override; |
| 137 void OnBufferRetired(int buffer_id) override; | |
| 138 | 104 |
| 139 private: | 105 private: |
| 140 struct ControllerClient; | 106 struct ControllerClient; |
| 141 typedef std::list<std::unique_ptr<ControllerClient>> ControllerClients; | 107 typedef std::list<std::unique_ptr<ControllerClient>> ControllerClients; |
| 142 | 108 |
| 143 class BufferContext { | 109 class BufferContext { |
| 144 public: | 110 public: |
| 145 BufferContext( | 111 BufferContext( |
| 146 int buffer_context_id, | 112 int buffer_context_id, |
| 147 int buffer_id, | 113 int buffer_id, |
| 148 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer, | 114 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer, |
| 149 media::FrameBufferPool* frame_buffer_pool); | 115 mojo::ScopedSharedBufferHandle handle); |
| 150 ~BufferContext(); | 116 ~BufferContext(); |
| 151 BufferContext(const BufferContext& other); | 117 BufferContext(BufferContext&& other); |
| 152 BufferContext& operator=(const BufferContext& other); | 118 BufferContext& operator=(BufferContext&& other); |
| 153 int buffer_context_id() const { return buffer_context_id_; } | 119 int buffer_context_id() const { return buffer_context_id_; } |
| 154 int buffer_id() const { return buffer_id_; } | 120 int buffer_id() const { return buffer_id_; } |
| 155 bool is_retired() const { return is_retired_; } | 121 bool is_retired() const { return is_retired_; } |
| 156 void set_is_retired() { is_retired_ = true; } | 122 void set_is_retired() { is_retired_ = true; } |
| 157 void set_frame_feedback_id(int id) { frame_feedback_id_ = id; } | 123 void set_frame_feedback_id(int id) { frame_feedback_id_ = id; } |
| 158 void set_consumer_feedback_observer( | 124 void set_consumer_feedback_observer( |
| 159 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer) { | 125 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer) { |
| 160 consumer_feedback_observer_ = consumer_feedback_observer; | 126 consumer_feedback_observer_ = consumer_feedback_observer; |
| 161 } | 127 } |
| 162 void set_frame_buffer_pool(media::FrameBufferPool* frame_buffer_pool) { | 128 void set_read_permission( |
| 163 frame_buffer_pool_ = frame_buffer_pool; | 129 std::unique_ptr< |
| 130 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> | |
| 131 buffer_read_permission) { | |
| 132 buffer_read_permission_ = std::move(buffer_read_permission); | |
| 164 } | 133 } |
| 165 void RecordConsumerUtilization(double utilization); | 134 void RecordConsumerUtilization(double utilization); |
| 166 void IncreaseConsumerCount(); | 135 void IncreaseConsumerCount(); |
| 167 void DecreaseConsumerCount(); | 136 void DecreaseConsumerCount(); |
| 168 bool HasZeroConsumerHoldCount(); | 137 bool has_consumers() const { return consumer_hold_count_ > 0; } |
|
mcasas
2017/02/16 23:45:07
nit: hacker case for methods is reserved for plain
chfremer
2017/02/16 23:57:20
Done.
| |
| 138 mojo::ScopedSharedBufferHandle CloneHandle(); | |
| 169 | 139 |
| 170 private: | 140 private: |
| 171 int buffer_context_id_; | 141 int buffer_context_id_; |
| 172 int buffer_id_; | 142 int buffer_id_; |
| 173 bool is_retired_; | 143 bool is_retired_; |
| 174 int frame_feedback_id_; | 144 int frame_feedback_id_; |
| 175 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer_; | 145 media::VideoFrameConsumerFeedbackObserver* consumer_feedback_observer_; |
| 176 media::FrameBufferPool* frame_buffer_pool_; | 146 mojo::ScopedSharedBufferHandle buffer_handle_; |
| 177 double max_consumer_utilization_; | 147 double max_consumer_utilization_; |
| 178 int consumer_hold_count_; | 148 int consumer_hold_count_; |
| 149 std::unique_ptr< | |
| 150 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> | |
| 151 buffer_read_permission_; | |
| 179 }; | 152 }; |
| 180 | 153 |
| 181 // Find a client of |id| and |handler| in |clients|. | 154 // Find a client of |id| and |handler| in |clients|. |
| 182 ControllerClient* FindClient(VideoCaptureControllerID id, | 155 ControllerClient* FindClient(VideoCaptureControllerID id, |
| 183 VideoCaptureControllerEventHandler* handler, | 156 VideoCaptureControllerEventHandler* handler, |
| 184 const ControllerClients& clients); | 157 const ControllerClients& clients); |
| 185 | 158 |
| 186 // Find a client of |session_id| in |clients|. | 159 // Find a client of |session_id| in |clients|. |
| 187 ControllerClient* FindClient(int session_id, | 160 ControllerClient* FindClient(int session_id, |
| 188 const ControllerClients& clients); | 161 const ControllerClients& clients); |
| 189 | 162 |
| 190 std::vector<BufferContext>::iterator FindBufferContextFromBufferContextId( | 163 std::vector<BufferContext>::iterator FindBufferContextFromBufferContextId( |
| 191 int buffer_context_id); | 164 int buffer_context_id); |
| 192 std::vector<BufferContext>::iterator FindUnretiredBufferContextFromBufferId( | 165 std::vector<BufferContext>::iterator FindUnretiredBufferContextFromBufferId( |
| 193 int buffer_id); | 166 int buffer_id); |
| 194 | 167 |
| 195 void OnClientFinishedConsumingBuffer(ControllerClient* client, | 168 void OnClientFinishedConsumingBuffer(ControllerClient* client, |
| 196 int buffer_id, | 169 int buffer_id, |
| 197 double consumer_resource_utilization); | 170 double consumer_resource_utilization); |
| 198 void ReleaseBufferContext( | 171 void ReleaseBufferContext( |
| 199 const std::vector<BufferContext>::iterator& buffer_state_iter); | 172 const std::vector<BufferContext>::iterator& buffer_state_iter); |
| 200 | 173 |
| 201 std::unique_ptr<media::FrameBufferPool> frame_buffer_pool_; | |
| 202 | |
| 203 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> | 174 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> |
| 204 consumer_feedback_observer_; | 175 consumer_feedback_observer_; |
| 205 | 176 |
| 206 std::vector<BufferContext> buffer_contexts_; | 177 std::vector<BufferContext> buffer_contexts_; |
| 207 | 178 |
| 208 // All clients served by this controller. | 179 // All clients served by this controller. |
| 209 ControllerClients controller_clients_; | 180 ControllerClients controller_clients_; |
| 210 | 181 |
| 211 // Takes on only the states 'STARTED' and 'ERROR'. 'ERROR' is an absorbing | 182 // Takes on only the states 'STARTED' and 'ERROR'. 'ERROR' is an absorbing |
| 212 // state which stops the flow of data to clients. | 183 // state which stops the flow of data to clients. |
| 213 VideoCaptureState state_; | 184 VideoCaptureState state_; |
| 214 | 185 |
| 215 int next_buffer_context_id_ = 0; | 186 int next_buffer_context_id_ = 0; |
| 216 | 187 |
| 217 // True if the controller has received a video frame from the device. | 188 // True if the controller has received a video frame from the device. |
| 218 bool has_received_frames_; | 189 bool has_received_frames_; |
| 219 | 190 |
| 220 media::VideoCaptureFormat video_capture_format_; | 191 media::VideoCaptureFormat video_capture_format_; |
| 221 | 192 |
| 222 base::WeakPtrFactory<VideoCaptureController> weak_ptr_factory_; | 193 base::WeakPtrFactory<VideoCaptureController> weak_ptr_factory_; |
| 223 | 194 |
| 224 DISALLOW_COPY_AND_ASSIGN(VideoCaptureController); | 195 DISALLOW_COPY_AND_ASSIGN(VideoCaptureController); |
| 225 }; | 196 }; |
| 226 | 197 |
| 227 } // namespace content | 198 } // namespace content |
| 228 | 199 |
| 229 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ | 200 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_ |
| OLD | NEW |