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 MessageLoopProxy
;
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
47 class CONTENT_EXPORT VideoCaptureImpl
48 : public VideoCaptureMessageFilter::Delegate
{
50 ~VideoCaptureImpl() override
;
52 VideoCaptureImpl(media::VideoCaptureSessionId session_id
,
53 VideoCaptureMessageFilter
* filter
);
55 // Start listening to IPC messages.
58 // Stop listening to IPC messages.
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_
; }
88 friend class VideoCaptureImplTest
;
89 friend class MockVideoCaptureImpl
;
91 // Carries a shared memory for transferring video frames from browser to
95 // Contains information for a video capture client. Including parameters
96 // for capturing and callbacks to the client.
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
,
109 int buffer_id
) override
;
110 void OnBufferDestroyed(int buffer_id
) override
;
111 void OnBufferReceived(int buffer_id
,
112 const gfx::Size
& coded_size
,
113 const gfx::Rect
& visible_rect
,
114 base::TimeTicks timestamp
,
115 const base::DictionaryValue
& metadata
) override
;
116 void OnMailboxBufferReceived(int buffer_id
,
117 const gpu::MailboxHolder
& mailbox_holder
,
118 const gfx::Size
& packed_frame_size
,
119 base::TimeTicks timestamp
,
120 const base::DictionaryValue
& metadata
) override
;
121 void OnStateChanged(VideoCaptureState state
) override
;
122 void OnDeviceSupportedFormatsEnumerated(
123 const media::VideoCaptureFormats
& supported_formats
) override
;
124 void OnDeviceFormatsInUseReceived(
125 const media::VideoCaptureFormats
& formats_in_use
) override
;
126 void OnDelegateAdded(int32 device_id
) override
;
128 // Sends an IPC message to browser process when all clients are done with the
130 void OnClientBufferFinished(int buffer_id
,
131 const scoped_refptr
<ClientBuffer
>& buffer
,
132 uint32 release_sync_point
);
135 void RestartCapture();
136 void StartCaptureInternal();
138 virtual void Send(IPC::Message
* message
);
141 bool RemoveClient(int client_id
, ClientInfoMap
* clients
);
143 const scoped_refptr
<VideoCaptureMessageFilter
> message_filter_
;
145 const int session_id_
;
147 // Vector of callbacks to be notified of device format enumerations, used only
149 std::vector
<VideoCaptureDeviceFormatsCB
> device_formats_cb_queue_
;
150 // Vector of callbacks to be notified of a device's in use capture format(s),
151 // used only on IO Thread.
152 std::vector
<VideoCaptureDeviceFormatsCB
> device_formats_in_use_cb_queue_
;
154 // Buffers available for sending to the client.
155 typedef std::map
<int32
, scoped_refptr
<ClientBuffer
> > ClientBufferMap
;
156 ClientBufferMap client_buffers_
;
158 ClientInfoMap clients_
;
159 ClientInfoMap clients_pending_on_filter_
;
160 ClientInfoMap clients_pending_on_restart_
;
162 // Member params_ represents the video format requested by the
163 // client to this class via StartCapture().
164 media::VideoCaptureParams params_
;
166 // The device's first captured frame timestamp sent from browser process side.
167 base::TimeTicks first_frame_timestamp_
;
170 VideoCaptureState state_
;
172 // IO message loop reference for checking correct class operation.
173 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
175 // WeakPtrFactory pointing back to |this| object, for use with
176 // media::VideoFrames constructed in OnBufferReceived() from buffers cached
177 // in |client_buffers_|.
178 // NOTE: Weak pointers must be invalidated before all other member variables.
179 base::WeakPtrFactory
<VideoCaptureImpl
> weak_factory_
;
181 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl
);
184 } // namespace content
186 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_