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_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
6 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 6 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <list> | 10 #include <list> |
11 #include <map> | 11 #include <map> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
17 #include "content/common/media/video_capture.h" | 17 #include "content/common/media/video_capture.h" |
18 #include "content/common/video_capture.mojom.h" | 18 #include "content/common/video_capture.mojom.h" |
19 #include "content/public/renderer/media_stream_video_sink.h" | |
20 #include "content/renderer/media/video_capture_message_filter.h" | |
21 #include "media/base/video_capture_types.h" | 19 #include "media/base/video_capture_types.h" |
22 #include "media/base/video_frame.h" | 20 #include "media/base/video_frame.h" |
23 #include "mojo/public/cpp/bindings/binding.h" | 21 #include "mojo/public/cpp/bindings/binding.h" |
24 | 22 |
25 namespace base { | |
26 class SingleThreadTaskRunner; | |
27 } // namespace base | |
28 | |
29 namespace content { | 23 namespace content { |
30 | 24 |
31 // VideoCaptureImpl represents a capture device in renderer process. It provides | 25 // VideoCaptureImpl represents a capture device in renderer process. It provides |
32 // interfaces for clients to Start/Stop capture. It also communicates to clients | 26 // an interface for clients to command the capture (Start, Stop, etc), and |
33 // when buffer is ready, state of capture device is changed. | 27 // communicates back to these clients e.g. the capture state or incoming |
34 // | 28 // captured VideoFrames. VideoCaptureImpl is created in the main Renderer thread |
35 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter to receive | 29 // but otherwise operates on |io_task_runner_|, which is usually the IO thread. |
36 // notification of the browser process being ready to communicate. | 30 class CONTENT_EXPORT VideoCaptureImpl : public mojom::VideoCaptureObserver { |
37 // | |
38 // VideoCaptureImpl is an IO thread only object. See the comments in | |
39 // video_capture_impl_manager.cc for the lifetime of this object. | |
40 // All methods must be called on the IO thread. | |
41 // | |
42 // This is an internal class used by VideoCaptureImplManager only. Do not access | |
43 // this directly. | |
44 class CONTENT_EXPORT VideoCaptureImpl | |
45 : public VideoCaptureMessageFilter::Delegate, | |
46 public mojom::VideoCaptureObserver { | |
47 public: | 31 public: |
48 VideoCaptureImpl( | 32 explicit VideoCaptureImpl(media::VideoCaptureSessionId session_id); |
49 media::VideoCaptureSessionId session_id, | |
50 VideoCaptureMessageFilter* filter, | |
51 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); | |
52 ~VideoCaptureImpl() override; | 33 ~VideoCaptureImpl() override; |
53 | 34 |
54 // Stop/resume delivering video frames to clients, based on flag |suspend|. | 35 // Stop/resume delivering video frames to clients, based on flag |suspend|. |
55 void SuspendCapture(bool suspend); | 36 void SuspendCapture(bool suspend); |
56 | 37 |
57 // Start capturing using the provided parameters. | 38 // Start capturing using the provided parameters. |
58 // |client_id| must be unique to this object in the render process. It is | 39 // |client_id| must be unique to this object in the render process. It is |
59 // used later to stop receiving video frames. | 40 // used later to stop receiving video frames. |
60 // |state_update_cb| will be called when state changes. | 41 // |state_update_cb| will be called when state changes. |
61 // |deliver_frame_cb| will be called when a frame is ready. | 42 // |deliver_frame_cb| will be called when a frame is ready. |
(...skipping 24 matching lines...) Expand all Loading... |
86 } | 67 } |
87 | 68 |
88 private: | 69 private: |
89 friend class VideoCaptureImplTest; | 70 friend class VideoCaptureImplTest; |
90 friend class MockVideoCaptureImpl; | 71 friend class MockVideoCaptureImpl; |
91 | 72 |
92 // Carries a shared memory for transferring video frames from browser to | 73 // Carries a shared memory for transferring video frames from browser to |
93 // renderer. | 74 // renderer. |
94 class ClientBuffer; | 75 class ClientBuffer; |
95 | 76 |
96 // Contains information for a video capture client. Including parameters | 77 // Contains information about a video capture client, including capture |
97 // for capturing and callbacks to the client. | 78 // parameters callbacks to the client. |
98 struct ClientInfo { | 79 struct ClientInfo; |
99 ClientInfo(); | |
100 ClientInfo(const ClientInfo& other); | |
101 ~ClientInfo(); | |
102 media::VideoCaptureParams params; | |
103 VideoCaptureStateUpdateCB state_update_cb; | |
104 VideoCaptureDeliverFrameCB deliver_frame_cb; | |
105 }; | |
106 using ClientInfoMap = std::map<int, ClientInfo>; | 80 using ClientInfoMap = std::map<int, ClientInfo>; |
107 | 81 |
108 using BufferFinishedCallback = | 82 using BufferFinishedCallback = |
109 base::Callback<void(const gpu::SyncToken& sync_token, | 83 base::Callback<void(const gpu::SyncToken& sync_token, |
110 double consumer_resource_utilization)>; | 84 double consumer_resource_utilization)>; |
111 | 85 |
112 // VideoCaptureMessageFilter::Delegate interface implementation. | |
113 void OnDelegateAdded(int32_t device_id) override; | |
114 | |
115 // mojom::VideoCaptureObserver implementation. | 86 // mojom::VideoCaptureObserver implementation. |
116 void OnStateChanged(mojom::VideoCaptureState state) override; | 87 void OnStateChanged(mojom::VideoCaptureState state) override; |
117 void OnBufferCreated(int32_t buffer_id, | 88 void OnBufferCreated(int32_t buffer_id, |
118 mojo::ScopedSharedBufferHandle handle) override; | 89 mojo::ScopedSharedBufferHandle handle) override; |
119 void OnBufferReady(int32_t buffer_id, mojom::VideoFrameInfoPtr info) override; | 90 void OnBufferReady(int32_t buffer_id, mojom::VideoFrameInfoPtr info) override; |
120 void OnBufferDestroyed(int32_t buffer_id) override; | 91 void OnBufferDestroyed(int32_t buffer_id) override; |
121 | 92 |
122 // Sends an IPC message to browser process when all clients are done with the | 93 // Sends an IPC message to browser process when all clients are done with the |
123 // buffer. | 94 // buffer. |
124 void OnClientBufferFinished(int buffer_id, | 95 void OnClientBufferFinished(int buffer_id, |
(...skipping 19 matching lines...) Expand all Loading... |
144 | 115 |
145 // Called (by an unknown thread) when all consumers are done with a VideoFrame | 116 // Called (by an unknown thread) when all consumers are done with a VideoFrame |
146 // and its ref-count has gone to zero. This helper function grabs the | 117 // and its ref-count has gone to zero. This helper function grabs the |
147 // RESOURCE_UTILIZATION value from the |metadata| and then runs the given | 118 // RESOURCE_UTILIZATION value from the |metadata| and then runs the given |
148 // callback, to trampoline back to the IO thread with the values. | 119 // callback, to trampoline back to the IO thread with the values. |
149 static void DidFinishConsumingFrame( | 120 static void DidFinishConsumingFrame( |
150 const media::VideoFrameMetadata* metadata, | 121 const media::VideoFrameMetadata* metadata, |
151 std::unique_ptr<gpu::SyncToken> release_sync_token, | 122 std::unique_ptr<gpu::SyncToken> release_sync_token, |
152 const BufferFinishedCallback& callback_to_io_thread); | 123 const BufferFinishedCallback& callback_to_io_thread); |
153 | 124 |
154 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; | 125 // |device_id_| and |session_id_| are different concepts, but we reuse the |
155 int device_id_; | 126 // same numerical value, passed on construction. |
| 127 const int device_id_; |
156 const int session_id_; | 128 const int session_id_; |
157 | 129 |
158 mojom::VideoCaptureHostAssociatedPtr video_capture_host_; | 130 // |video_capture_host_| is an IO-thread InterfacePtr to a remote service |
| 131 // implementation and is created by binding |video_capture_host_info_|, |
| 132 // unless a |video_capture_host_for_testing_| has been injected. |
| 133 mojom::VideoCaptureHostPtrInfo video_capture_host_info_; |
| 134 mojom::VideoCaptureHostPtr video_capture_host_; |
159 mojom::VideoCaptureHost* video_capture_host_for_testing_; | 135 mojom::VideoCaptureHost* video_capture_host_for_testing_; |
160 | 136 |
161 mojo::Binding<mojom::VideoCaptureObserver> observer_binding_; | 137 mojo::Binding<mojom::VideoCaptureObserver> observer_binding_; |
162 | 138 |
163 // Buffers available for sending to the client. | 139 // Buffers available for sending to the client. |
164 typedef std::map<int32_t, scoped_refptr<ClientBuffer>> ClientBufferMap; | 140 using ClientBufferMap = std::map<int32_t, scoped_refptr<ClientBuffer>>; |
165 ClientBufferMap client_buffers_; | 141 ClientBufferMap client_buffers_; |
166 | 142 |
167 ClientInfoMap clients_; | 143 ClientInfoMap clients_; |
168 ClientInfoMap clients_pending_on_filter_; | |
169 ClientInfoMap clients_pending_on_restart_; | 144 ClientInfoMap clients_pending_on_restart_; |
170 | 145 |
171 // Member params_ represents the video format requested by the | 146 // Video format requested by the client to this class via StartCapture(). |
172 // client to this class via StartCapture(). | |
173 media::VideoCaptureParams params_; | 147 media::VideoCaptureParams params_; |
174 | 148 |
175 // The device's first captured frame reference time sent from browser process | 149 // First captured frame reference time sent from browser process side. |
176 // side. | |
177 base::TimeTicks first_frame_ref_time_; | 150 base::TimeTicks first_frame_ref_time_; |
178 | 151 |
179 VideoCaptureState state_; | 152 VideoCaptureState state_; |
180 | 153 |
181 // IO message loop reference for checking correct class operation. | 154 base::ThreadChecker io_thread_checker_; |
182 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
183 | 155 |
184 // WeakPtrFactory pointing back to |this| object, for use with | 156 // WeakPtrFactory pointing back to |this| object, for use with |
185 // media::VideoFrames constructed in OnBufferReceived() from buffers cached | 157 // media::VideoFrames constructed in OnBufferReceived() from buffers cached |
186 // in |client_buffers_|. | 158 // in |client_buffers_|. |
187 // NOTE: Weak pointers must be invalidated before all other member variables. | |
188 base::WeakPtrFactory<VideoCaptureImpl> weak_factory_; | 159 base::WeakPtrFactory<VideoCaptureImpl> weak_factory_; |
189 | 160 |
190 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); | 161 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); |
191 }; | 162 }; |
192 | 163 |
193 } // namespace content | 164 } // namespace content |
194 | 165 |
195 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 166 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
OLD | NEW |