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
;
71 media::VideoCaptureSessionId
session_id() const { return session_id_
; }
74 friend class VideoCaptureImplTest
;
75 friend class MockVideoCaptureImpl
;
78 typedef std::map
<media::VideoCapture::EventHandler
*,
79 media::VideoCaptureParams
> ClientInfo
;
81 void InitOnIOThread();
82 void DeInitOnIOThread(base::Closure done_cb
);
83 void SuspendCaptureOnIOThread(bool suspend
);
84 void StartCaptureOnIOThread(
85 media::VideoCapture::EventHandler
* handler
,
86 const media::VideoCaptureParams
& params
);
87 void StopCaptureOnIOThread(media::VideoCapture::EventHandler
* handler
);
89 // VideoCaptureMessageFilter::Delegate interface.
90 virtual void OnBufferCreated(base::SharedMemoryHandle handle
,
92 int buffer_id
) OVERRIDE
;
93 virtual void OnBufferDestroyed(int buffer_id
) OVERRIDE
;
94 virtual void OnBufferReceived(
96 base::TimeTicks timestamp
,
97 const media::VideoCaptureFormat
& format
) OVERRIDE
;
98 virtual void OnStateChanged(VideoCaptureState state
) OVERRIDE
;
99 virtual void OnDelegateAdded(int32 device_id
) OVERRIDE
;
101 // Sends an IPC message to browser process when all clients are done with the
103 void OnClientBufferFinished(
105 const scoped_refptr
<ClientBuffer
>& buffer
);
108 void RestartCapture();
109 void StartCaptureInternal();
110 virtual void Send(IPC::Message
* message
);
113 bool RemoveClient(media::VideoCapture::EventHandler
* handler
,
114 ClientInfo
* clients
);
116 const scoped_refptr
<VideoCaptureMessageFilter
> message_filter_
;
117 const scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy_
;
119 const int session_id_
;
121 // Buffers available for sending to the client.
122 typedef std::map
<int32
, scoped_refptr
<ClientBuffer
> > ClientBufferMap
;
123 ClientBufferMap client_buffers_
;
126 ClientInfo clients_pending_on_filter_
;
127 ClientInfo clients_pending_on_restart_
;
129 // Member params_ represents the video format requested by the
130 // client to this class via StartCapture().
131 media::VideoCaptureParams params_
;
133 // The device's video capture format sent from browser process side.
134 media::VideoCaptureFormat last_frame_format_
;
136 // The device's first captured frame timestamp sent from browser process side.
137 base::TimeTicks first_frame_timestamp_
;
140 VideoCaptureState state_
;
142 // WeakPtrFactory pointing back to |this| object, for use with
143 // media::VideoFrames constructed in OnBufferReceived() from buffers cached
144 // in |client_buffers_|.
145 base::WeakPtrFactory
<VideoCaptureImpl
> weak_this_factory_
;
147 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl
);
150 } // namespace content
152 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_