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_
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"
20 class SingleThreadTaskRunner
;
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
43 class CONTENT_EXPORT VideoCaptureImpl
44 : public VideoCaptureMessageFilter::Delegate
{
46 ~VideoCaptureImpl() override
;
48 VideoCaptureImpl(media::VideoCaptureSessionId session_id
,
49 VideoCaptureMessageFilter
* filter
);
51 // Start listening to IPC messages.
54 // Stop listening to IPC messages.
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
);
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_
; }
81 friend class VideoCaptureImplTest
;
82 friend class MockVideoCaptureImpl
;
84 // Carries a shared memory for transferring video frames from browser to
89 // VideoCaptureMessageFilter::Delegate interface.
90 void OnBufferCreated(base::SharedMemoryHandle handle
,
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(
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
115 void OnClientBufferFinished(
117 const scoped_refptr
<ClientBuffer
>& buffer
,
118 uint32 release_sync_point
,
119 double consumer_resource_utilization
);
120 void OnClientBufferFinished2(
122 const scoped_refptr
<ClientBuffer2
>& buffer
,
123 uint32 release_sync_point
,
124 double consumer_resource_utilization
);
127 void RestartCapture();
128 void StartCaptureInternal();
130 virtual void Send(IPC::Message
* message
);
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.
149 const scoped_refptr
<VideoCaptureMessageFilter
> message_filter_
;
151 const int session_id_
;
153 // Vector of callbacks to be notified of device format enumerations, used only
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_