Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / media / video_capture_impl.h
blobc8a75b2a5868918ea376fdf18c568d62e73320b3
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 media {
28 class VideoFrame;
29 } // namespace media
31 namespace content {
33 // VideoCaptureImpl represents a capture device in renderer process. It provides
34 // interfaces for clients to Start/Stop capture. It also communicates to clients
35 // when buffer is ready, state of capture device is changed.
37 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter which relays
38 // operation of a capture device to the browser process and receives responses
39 // from browser process.
41 // VideoCaptureImpl is an IO thread only object. See the comments in
42 // video_capture_impl_manager.cc for the lifetime of this object.
43 // All methods must be called on the IO thread.
45 // This is an internal class used by VideoCaptureImplManager only. Do not access
46 // this directly.
47 class CONTENT_EXPORT VideoCaptureImpl
48 : public VideoCaptureMessageFilter::Delegate {
49 public:
50 ~VideoCaptureImpl() override;
52 VideoCaptureImpl(media::VideoCaptureSessionId session_id,
53 VideoCaptureMessageFilter* filter);
55 // Start listening to IPC messages.
56 void Init();
58 // Stop listening to IPC messages.
59 void DeInit();
61 // Stop/resume delivering video frames to clients, based on flag |suspend|.
62 void SuspendCapture(bool suspend);
64 // Start capturing using the provided parameters.
65 // |client_id| must be unique to this object in the render process. It is
66 // used later to stop receiving video frames.
67 // |state_update_cb| will be called when state changes.
68 // |deliver_frame_cb| will be called when a frame is ready.
69 void StartCapture(int client_id,
70 const media::VideoCaptureParams& params,
71 const VideoCaptureStateUpdateCB& state_update_cb,
72 const VideoCaptureDeliverFrameCB& deliver_frame_cb);
74 // Stop capturing. |client_id| is the identifier used to call StartCapture.
75 void StopCapture(int client_id);
77 // Get capturing formats supported by this device.
78 // |callback| will be invoked with the results.
79 void GetDeviceSupportedFormats(const VideoCaptureDeviceFormatsCB& callback);
81 // Get capturing formats currently in use by this device.
82 // |callback| will be invoked with the results.
83 void GetDeviceFormatsInUse(const VideoCaptureDeviceFormatsCB& callback);
85 media::VideoCaptureSessionId session_id() const { return session_id_; }
87 private:
88 friend class VideoCaptureImplTest;
89 friend class MockVideoCaptureImpl;
91 // Carries a shared memory for transferring video frames from browser to
92 // renderer.
93 class ClientBuffer;
95 // Contains information for a video capture client. Including parameters
96 // for capturing and callbacks to the client.
97 struct ClientInfo {
98 ClientInfo();
99 ~ClientInfo();
100 media::VideoCaptureParams params;
101 VideoCaptureStateUpdateCB state_update_cb;
102 VideoCaptureDeliverFrameCB deliver_frame_cb;
104 typedef std::map<int, ClientInfo> ClientInfoMap;
106 // VideoCaptureMessageFilter::Delegate interface.
107 void OnBufferCreated(base::SharedMemoryHandle handle,
108 int length,
109 int buffer_id) override;
110 void OnBufferDestroyed(int buffer_id) override;
111 void OnBufferReceived(int buffer_id,
112 base::TimeTicks timestamp,
113 const base::DictionaryValue& metadata,
114 media::VideoPixelFormat pixel_format,
115 media::VideoFrame::StorageType storage_type,
116 const gfx::Size& coded_size,
117 const gfx::Rect& visible_rect,
118 const gpu::MailboxHolder& mailbox_holder) override;
119 void OnStateChanged(VideoCaptureState state) override;
120 void OnDeviceSupportedFormatsEnumerated(
121 const media::VideoCaptureFormats& supported_formats) override;
122 void OnDeviceFormatsInUseReceived(
123 const media::VideoCaptureFormats& formats_in_use) override;
124 void OnDelegateAdded(int32 device_id) override;
126 // Sends an IPC message to browser process when all clients are done with the
127 // buffer.
128 void OnClientBufferFinished(int buffer_id,
129 const scoped_refptr<ClientBuffer>& buffer,
130 uint32 release_sync_point,
131 double consumer_resource_utilization);
133 void StopDevice();
134 void RestartCapture();
135 void StartCaptureInternal();
137 virtual void Send(IPC::Message* message);
139 // Helpers.
140 bool RemoveClient(int client_id, ClientInfoMap* clients);
142 // Called (by an unknown thread) when all consumers are done with a VideoFrame
143 // and its ref-count has gone to zero. This helper function grabs the
144 // RESOURCE_UTILIZATION value from the |metadata| and then runs the given
145 // callback, to trampoline back to the IO thread with the values.
146 static void DidFinishConsumingFrame(
147 const media::VideoFrameMetadata* metadata,
148 uint32* release_sync_point_storage, // Takes ownership.
149 const base::Callback<void(uint32, double)>& callback_to_io_thread);
151 const scoped_refptr<VideoCaptureMessageFilter> message_filter_;
152 int device_id_;
153 const int session_id_;
155 // Vector of callbacks to be notified of device format enumerations, used only
156 // on IO Thread.
157 std::vector<VideoCaptureDeviceFormatsCB> device_formats_cb_queue_;
158 // Vector of callbacks to be notified of a device's in use capture format(s),
159 // used only on IO Thread.
160 std::vector<VideoCaptureDeviceFormatsCB> device_formats_in_use_cb_queue_;
162 // Buffers available for sending to the client.
163 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap;
164 ClientBufferMap client_buffers_;
166 ClientInfoMap clients_;
167 ClientInfoMap clients_pending_on_filter_;
168 ClientInfoMap clients_pending_on_restart_;
170 // Member params_ represents the video format requested by the
171 // client to this class via StartCapture().
172 media::VideoCaptureParams params_;
174 // The device's first captured frame timestamp sent from browser process side.
175 base::TimeTicks first_frame_timestamp_;
177 bool suspended_;
178 VideoCaptureState state_;
180 // IO message loop reference for checking correct class operation.
181 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
183 // WeakPtrFactory pointing back to |this| object, for use with
184 // media::VideoFrames constructed in OnBufferReceived() from buffers cached
185 // in |client_buffers_|.
186 // NOTE: Weak pointers must be invalidated before all other member variables.
187 base::WeakPtrFactory<VideoCaptureImpl> weak_factory_;
189 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl);
192 } // namespace content
194 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_