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 // VideoCaptureImpl represents a capture device in renderer process. It provides
6 // interfaces for clients to Start/Stop capture. It also communicates to clients
7 // when buffer is ready, state of capture device is changed.
9 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter which relays
10 // operation of a capture device to the browser process and receives responses
11 // from browser process.
13 // All public methods of VideoCaptureImpl can be called on any thread.
14 // Internally it runs on the IO thread. Clients of this class implement
15 // interface media::VideoCapture::EventHandler which is called only on the IO
18 // Implementation note: tasks are posted bound to Unretained(this) to the I/O
19 // thread and this is safe (even though the I/O thread is scoped to the renderer
20 // process) because VideoCaptureImplManager only triggers deletion of its
21 // VideoCaptureImpl's by calling DeInit which detours through the I/O thread, so
22 // as long as nobody posts tasks after the DeInit() call is made, it is
23 // guaranteed none of these Unretained posted tasks will dangle after the delete
24 // goes through. The "as long as" is guaranteed by clients of
25 // VideoCaptureImplManager not using devices after they've released
26 // VideoCaptureHandle, which is a wrapper of this object.
28 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
29 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
34 #include "base/memory/weak_ptr.h"
35 #include "content/common/content_export.h"
36 #include "content/common/media/video_capture.h"
37 #include "content/renderer/media/video_capture_message_filter.h"
38 #include "media/video/capture/video_capture.h"
39 #include "media/video/capture/video_capture_types.h"
42 class MessageLoopProxy;
47 class CONTENT_EXPORT VideoCaptureImpl
48 : public media::VideoCapture, public VideoCaptureMessageFilter::Delegate {
50 VideoCaptureImpl(media::VideoCaptureSessionId session_id,
51 VideoCaptureMessageFilter* filter);
52 virtual ~VideoCaptureImpl();
54 // Start listening to IPC messages.
57 // Stop listening to IPC messages. Call |done_cb| when done.
58 void DeInit(base::Closure done_cb);
60 // Stop/resume delivering video frames to clients, based on flag |suspend|.
61 void SuspendCapture(bool suspend);
63 // media::VideoCapture interface.
64 virtual void StartCapture(
65 media::VideoCapture::EventHandler* handler,
66 const media::VideoCaptureParams& params) OVERRIDE;
67 virtual void StopCapture(media::VideoCapture::EventHandler* handler) OVERRIDE;
68 virtual bool CaptureStarted() OVERRIDE;
69 virtual int CaptureFrameRate() OVERRIDE;
70 virtual void GetDeviceSupportedFormats(
71 const DeviceFormatsCallback& callback) OVERRIDE;
72 virtual void GetDeviceFormatsInUse(
73 const DeviceFormatsInUseCallback& callback) OVERRIDE;
75 media::VideoCaptureSessionId session_id() const { return session_id_; }
78 friend class VideoCaptureImplTest;
79 friend class MockVideoCaptureImpl;
82 typedef std::map<media::VideoCapture::EventHandler*,
83 media::VideoCaptureParams> ClientInfo;
85 void InitOnIOThread();
86 void DeInitOnIOThread(base::Closure done_cb);
87 void SuspendCaptureOnIOThread(bool suspend);
88 void StartCaptureOnIOThread(
89 media::VideoCapture::EventHandler* handler,
90 const media::VideoCaptureParams& params);
91 void StopCaptureOnIOThread(media::VideoCapture::EventHandler* handler);
92 void GetDeviceSupportedFormatsOnIOThread(
93 const DeviceFormatsCallback& callback);
94 void GetDeviceFormatsInUseOnIOThread(
95 const DeviceFormatsInUseCallback& callback);
97 // VideoCaptureMessageFilter::Delegate interface.
98 virtual void OnBufferCreated(base::SharedMemoryHandle handle,
100 int buffer_id) OVERRIDE;
101 virtual void OnBufferDestroyed(int buffer_id) OVERRIDE;
102 virtual void OnBufferReceived(
104 base::TimeTicks timestamp,
105 const media::VideoCaptureFormat& format) OVERRIDE;
106 virtual void OnStateChanged(VideoCaptureState state) OVERRIDE;
107 virtual void OnDeviceSupportedFormatsEnumerated(
108 const media::VideoCaptureFormats& supported_formats) OVERRIDE;
109 virtual void OnDeviceFormatsInUseReceived(
110 const media::VideoCaptureFormats& formats_in_use) OVERRIDE;
111 virtual 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);
120 void RestartCapture();
121 void StartCaptureInternal();
123 virtual void Send(IPC::Message* message);
126 bool RemoveClient(media::VideoCapture::EventHandler* handler,
127 ClientInfo* clients);
129 const scoped_refptr<VideoCaptureMessageFilter> message_filter_;
130 const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
132 const int session_id_;
134 // Vector of callbacks to be notified of device format enumerations, used only
136 std::vector<DeviceFormatsCallback> device_formats_callback_queue_;
137 // Vector of callbacks to be notified of a device's in use capture format(s),
138 // used only on IO Thread.
139 std::vector<DeviceFormatsInUseCallback> device_formats_in_use_callback_queue_;
141 // Buffers available for sending to the client.
142 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap;
143 ClientBufferMap client_buffers_;
146 ClientInfo clients_pending_on_filter_;
147 ClientInfo clients_pending_on_restart_;
149 // Member params_ represents the video format requested by the
150 // client to this class via StartCapture().
151 media::VideoCaptureParams params_;
153 // The device's video capture format sent from browser process side.
154 media::VideoCaptureFormat last_frame_format_;
156 // The device's first captured frame timestamp sent from browser process side.
157 base::TimeTicks first_frame_timestamp_;
160 VideoCaptureState state_;
162 // WeakPtrFactory pointing back to |this| object, for use with
163 // media::VideoFrames constructed in OnBufferReceived() from buffers cached
164 // in |client_buffers_|.
165 base::WeakPtrFactory<VideoCaptureImpl> weak_this_factory_;
167 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl);
170 } // namespace content
172 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_