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
10 // relays operation of capture device to browser process and receives response
11 // from browser process.
13 // The media::VideoCapture and VideoCaptureMessageFilter::Delegate are
14 // asynchronous interfaces, which means callers can call those interfaces
15 // from any threads without worrying about thread safety.
16 // The |capture_message_loop_proxy_| is the working thread of VideoCaptureImpl.
17 // All non-const members are accessed only on that working thread.
19 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
20 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
25 #include "content/common/content_export.h"
26 #include "content/common/media/video_capture.h"
27 #include "content/renderer/media/video_capture_message_filter.h"
28 #include "media/video/capture/video_capture.h"
29 #include "media/video/capture/video_capture_types.h"
32 class MessageLoopProxy
;
37 class CONTENT_EXPORT VideoCaptureImpl
38 : public media::VideoCapture
, public VideoCaptureMessageFilter::Delegate
{
40 // media::VideoCapture interface.
41 virtual void StartCapture(
42 media::VideoCapture::EventHandler
* handler
,
43 const media::VideoCaptureCapability
& capability
) OVERRIDE
;
44 virtual void StopCapture(media::VideoCapture::EventHandler
* handler
) OVERRIDE
;
45 virtual void FeedBuffer(scoped_refptr
<VideoFrameBuffer
> buffer
) OVERRIDE
;
46 virtual bool CaptureStarted() OVERRIDE
;
47 virtual int CaptureWidth() OVERRIDE
;
48 virtual int CaptureHeight() OVERRIDE
;
49 virtual int CaptureFrameRate() OVERRIDE
;
51 // VideoCaptureMessageFilter::Delegate interface.
52 virtual void OnBufferCreated(base::SharedMemoryHandle handle
,
53 int length
, int buffer_id
) OVERRIDE
;
54 virtual void OnBufferReceived(int buffer_id
, base::Time timestamp
) OVERRIDE
;
55 virtual void OnStateChanged(VideoCaptureState state
) OVERRIDE
;
56 virtual void OnDeviceInfoReceived(
57 const media::VideoCaptureParams
& device_info
) OVERRIDE
;
58 virtual void OnDelegateAdded(int32 device_id
) OVERRIDE
;
60 // Stop/resume delivering video frames to clients, based on flag |suspend|.
61 virtual void SuspendCapture(bool suspend
);
64 friend class VideoCaptureImplManager
;
65 friend class VideoCaptureImplTest
;
66 friend class MockVideoCaptureImpl
;
69 typedef std::map
<media::VideoCapture::EventHandler
*,
70 media::VideoCaptureCapability
> ClientInfo
;
72 VideoCaptureImpl(media::VideoCaptureSessionId id
,
73 base::MessageLoopProxy
* capture_message_loop_proxy
,
74 VideoCaptureMessageFilter
* filter
);
75 virtual ~VideoCaptureImpl();
77 void DoStartCaptureOnCaptureThread(
78 media::VideoCapture::EventHandler
* handler
,
79 const media::VideoCaptureCapability
& capability
);
80 void DoStopCaptureOnCaptureThread(media::VideoCapture::EventHandler
* handler
);
81 void DoFeedBufferOnCaptureThread(scoped_refptr
<VideoFrameBuffer
> buffer
);
83 void DoBufferCreatedOnCaptureThread(base::SharedMemoryHandle handle
,
84 int length
, int buffer_id
);
85 void DoBufferReceivedOnCaptureThread(int buffer_id
, base::Time timestamp
);
86 void DoStateChangedOnCaptureThread(VideoCaptureState state
);
87 void DoDeviceInfoReceivedOnCaptureThread(
88 const media::VideoCaptureParams
& device_info
);
89 void DoDelegateAddedOnCaptureThread(int32 device_id
);
91 void DoSuspendCaptureOnCaptureThread(bool suspend
);
94 void DeInit(base::Closure task
);
95 void DoDeInitOnCaptureThread(base::Closure task
);
97 void RestartCapture();
98 void StartCaptureInternal();
99 void AddDelegateOnIOThread();
100 void RemoveDelegateOnIOThread(base::Closure task
);
101 virtual void Send(IPC::Message
* message
);
104 bool ClientHasDIB() const;
105 bool RemoveClient(media::VideoCapture::EventHandler
* handler
,
106 ClientInfo
* clients
);
108 const scoped_refptr
<VideoCaptureMessageFilter
> message_filter_
;
109 const scoped_refptr
<base::MessageLoopProxy
> capture_message_loop_proxy_
;
110 const scoped_refptr
<base::MessageLoopProxy
> io_message_loop_proxy_
;
113 // Pool of DIBs. The key is buffer_id.
114 typedef std::map
<int, DIBBuffer
*> CachedDIB
;
115 CachedDIB cached_dibs_
;
119 ClientInfo clients_pending_on_filter_
;
120 ClientInfo clients_pending_on_restart_
;
122 media::VideoCaptureCapability::Format video_type_
;
124 // The parameter is being used in current capture session. A capture session
125 // starts with StartCapture and ends with StopCapture.
126 media::VideoCaptureParams current_params_
;
128 // The information about the device sent from browser process side.
129 media::VideoCaptureParams device_info_
;
130 bool device_info_available_
;
133 VideoCaptureState state_
;
135 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl
);
138 } // namespace content
140 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_