Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / video_capture_impl.h
blobb46f4a62e6fe6aa53406dc3a23cf45cd5124a4c7
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
6 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
8 #include <list>
9 #include <map>
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/thread_checker.h"
13 #include "content/common/content_export.h"
14 #include "content/common/media/video_capture.h"
15 #include "content/public/renderer/media_stream_video_sink.h"
16 #include "content/renderer/media/video_capture_message_filter.h"
17 #include "media/base/video_capture_types.h"
19 namespace base {
20 class SingleThreadTaskRunner;
21 } // namespace base
23 namespace gpu {
24 struct MailboxHolder;
25 } // namespace gpu
27 namespace content {
29 // VideoCaptureImpl represents a capture device in renderer process. It provides
30 // an interface for a single client to Start/Stop capture. It also communicates
31 // to the client when buffer is ready, when state of capture device is changed.
33 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter which relays
34 // operation of a capture device to the browser process and receives responses
35 // from browser process.
37 // VideoCaptureImpl is an IO thread only object. See the comments in
38 // video_capture_impl_manager.cc for the lifetime of this object.
39 // All methods must be called on the IO thread.
41 // This is an internal class used by VideoCaptureImplManager only. Do not access
42 // this directly.
43 class CONTENT_EXPORT VideoCaptureImpl
44 : public VideoCaptureMessageFilter::Delegate {
45 public:
46 ~VideoCaptureImpl() override;
48 VideoCaptureImpl(media::VideoCaptureSessionId session_id,
49 VideoCaptureMessageFilter* filter);
51 // Start listening to IPC messages.
52 void Init();
54 // Stop listening to IPC messages.
55 void DeInit();
57 // Stop/resume delivering video frames to clients, based on flag |suspend|.
58 void SuspendCapture(bool suspend);
60 // Start capturing using the provided parameters.
61 // |state_update_cb| will be called when state changes.
62 // |deliver_frame_cb| will be called when a frame is ready.
63 void StartCapture(const media::VideoCaptureParams& params,
64 const VideoCaptureStateUpdateCB& state_update_cb,
65 const VideoCaptureDeliverFrameCB& deliver_frame_cb);
67 // Stop capturing.
68 void StopCapture();
70 // Get capturing formats supported by this device.
71 // |callback| will be invoked with the results.
72 void GetDeviceSupportedFormats(const VideoCaptureDeviceFormatsCB& callback);
74 // Get capturing formats currently in use by this device.
75 // |callback| will be invoked with the results.
76 void GetDeviceFormatsInUse(const VideoCaptureDeviceFormatsCB& callback);
78 media::VideoCaptureSessionId session_id() const { return session_id_; }
80 private:
81 friend class VideoCaptureImplTest;
82 friend class MockVideoCaptureImpl;
84 // Carries a shared memory for transferring video frames from browser to
85 // renderer.
86 class ClientBuffer;
87 class ClientBuffer2;
89 // VideoCaptureMessageFilter::Delegate interface.
90 void OnBufferCreated(base::SharedMemoryHandle handle,
91 int length,
92 int buffer_id) override;
93 void OnBufferCreated2(const std::vector<gfx::GpuMemoryBufferHandle>& handles,
94 const gfx::Size& size,
95 int buffer_id) override;
96 void OnBufferDestroyed(int buffer_id) override;
97 void OnBufferReceived(
98 int buffer_id,
99 base::TimeTicks timestamp,
100 const base::DictionaryValue& metadata,
101 media::VideoPixelFormat pixel_format,
102 media::VideoFrame::StorageType storage_type,
103 const gfx::Size& coded_size,
104 const gfx::Rect& visible_rect,
105 const std::vector<gpu::MailboxHolder>& mailbox_holders) override;
106 void OnStateChanged(VideoCaptureState state) override;
107 void OnDeviceSupportedFormatsEnumerated(
108 const media::VideoCaptureFormats& supported_formats) override;
109 void OnDeviceFormatsInUseReceived(
110 const media::VideoCaptureFormats& formats_in_use) override;
111 void OnDelegateAdded(int32 device_id) override;
113 // Sends an IPC message to browser process when all clients are done with the
114 // buffer.
115 void OnClientBufferFinished(
116 int buffer_id,
117 const scoped_refptr<ClientBuffer>& buffer,
118 uint32 release_sync_point,
119 double consumer_resource_utilization);
120 void OnClientBufferFinished2(
121 int buffer_id,
122 const scoped_refptr<ClientBuffer2>& buffer,
123 uint32 release_sync_point,
124 double consumer_resource_utilization);
126 void StopDevice();
127 void RestartCapture();
128 void StartCaptureInternal();
130 virtual void Send(IPC::Message* message);
132 // Helpers.
133 // Called (by an unknown thread) when all consumers are done with a VideoFrame
134 // and its ref-count has gone to zero. This helper function grabs the
135 // RESOURCE_UTILIZATION value from the |metadata| and then runs the given
136 // callback, to trampoline back to the IO thread with the values.
137 static void DidFinishConsumingFrame(
138 const media::VideoFrameMetadata* metadata,
139 uint32* release_sync_point_storage, // Takes ownership.
140 const base::Callback<void(uint32, double)>& callback_to_io_thread);
142 // Use |state_update_cb_| and |deliver_frame_cb_| to determine whether
143 // the VideoCaptureImpl has been initialized.
144 bool IsInitialized() const;
146 // Reset to a "fresh" client state.
147 void ResetClient();
149 const scoped_refptr<VideoCaptureMessageFilter> message_filter_;
150 int device_id_;
151 const int session_id_;
153 // Vector of callbacks to be notified of device format enumerations, used only
154 // on IO Thread.
155 std::vector<VideoCaptureDeviceFormatsCB> device_formats_cb_queue_;
156 // Vector of callbacks to be notified of a device's in use capture format(s),
157 // used only on IO Thread.
158 std::vector<VideoCaptureDeviceFormatsCB> device_formats_in_use_cb_queue_;
160 // Buffers available for sending to the client.
161 typedef std::map<int32, scoped_refptr<ClientBuffer>> ClientBufferMap;
162 ClientBufferMap client_buffers_;
163 typedef std::map<int32, scoped_refptr<ClientBuffer2>> ClientBuffer2Map;
164 ClientBuffer2Map client_buffer2s_;
166 // Track information for the video capture client, consisting of parameters
167 // for capturing and callbacks to the client.
168 media::VideoCaptureParams client_params_;
169 VideoCaptureStateUpdateCB state_update_cb_;
170 VideoCaptureDeliverFrameCB deliver_frame_cb_;
172 // The device's first captured frame timestamp sent from browser process side.
173 base::TimeTicks first_frame_timestamp_;
175 // State of this VideoCaptureImpl.
176 VideoCaptureState state_;
178 // IO message loop reference for checking correct class operation.
179 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
181 // WeakPtrFactory pointing back to |this| object, for use with
182 // media::VideoFrames constructed in OnBufferReceived() from buffers cached
183 // in |client_buffers_|.
184 // NOTE: Weak pointers must be invalidated before all other member variables.
185 base::WeakPtrFactory<VideoCaptureImpl> weak_factory_;
187 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl);
190 } // namespace content
192 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_