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 // TODO(hclam): This class should be renamed to VideoCaptureService.
7 // This class provides access to a video capture device in the browser
8 // process through IPC. The main function is to deliver video frames
13 // VideoCaptureImplManager lives only on the render thread. All methods
14 // must be called on this thread.
16 // VideoFrames are delivered on the IO thread. Callbacks provided by
17 // a client are also called on the IO thread.
19 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
20 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
24 #include "base/callback.h"
25 #include "base/memory/linked_ptr.h"
26 #include "base/memory/ref_counted.h"
27 #include "base/memory/scoped_ptr.h"
28 #include "base/memory/weak_ptr.h"
29 #include "base/message_loop/message_loop_proxy.h"
30 #include "base/synchronization/lock.h"
31 #include "base/threading/thread_checker.h"
32 #include "content/common/content_export.h"
33 #include "content/common/media/video_capture.h"
34 #include "content/public/renderer/media_stream_video_sink.h"
35 #include "media/video/capture/video_capture_types.h"
39 class VideoCaptureImpl
;
40 class VideoCaptureMessageFilter
;
42 class CONTENT_EXPORT VideoCaptureImplManager
{
44 VideoCaptureImplManager();
45 virtual ~VideoCaptureImplManager();
47 // Open a device associated with the session ID.
48 // This method must be called before any methods with the same ID
50 // Returns a callback that should be used to release the acquired
52 base::Closure
UseDevice(media::VideoCaptureSessionId id
);
54 // Start receiving video frames for the given session ID.
56 // |state_update_cb| will be called on the IO thread when capturing
58 // States will be one of the following four:
59 // * VIDEO_CAPTURE_STATE_STARTED
60 // * VIDEO_CAPTURE_STATE_STOPPED
61 // * VIDEO_CAPTURE_STATE_PAUSED
62 // * VIDEO_CAPTURE_STATE_ERROR
64 // |deliver_frame_cb| will be called on the IO thread when a video
67 // Returns a callback that is used to stop capturing. Note that stopping
68 // video capture is not synchronous. Client should handle the case where
69 // callbacks are called after capturing is instructed to stop, typically
70 // by binding the passed callbacks on a WeakPtr.
71 base::Closure
StartCapture(
72 media::VideoCaptureSessionId id
,
73 const media::VideoCaptureParams
& params
,
74 const VideoCaptureStateUpdateCB
& state_update_cb
,
75 const VideoCaptureDeliverFrameCB
& deliver_frame_cb
);
77 // Get supported formats supported by the device for the given session
78 // ID. |callback| will be called on the IO thread.
79 void GetDeviceSupportedFormats(
80 media::VideoCaptureSessionId id
,
81 const VideoCaptureDeviceFormatsCB
& callback
);
83 // Get supported formats currently in use for the given session ID.
84 // |callback| will be called on the IO thread.
85 void GetDeviceFormatsInUse(
86 media::VideoCaptureSessionId id
,
87 const VideoCaptureDeviceFormatsCB
& callback
);
89 // Make all existing VideoCaptureImpl instances stop/resume delivering
90 // video frames to their clients, depends on flag |suspend|.
91 void SuspendDevices(bool suspend
);
93 VideoCaptureMessageFilter
* video_capture_message_filter() const {
98 virtual VideoCaptureImpl
* CreateVideoCaptureImplForTesting(
99 media::VideoCaptureSessionId id
,
100 VideoCaptureMessageFilter
* filter
) const;
103 void StopCapture(int client_id
, media::VideoCaptureSessionId id
);
104 void UnrefDevice(media::VideoCaptureSessionId id
);
106 // The int is used to count clients of the corresponding VideoCaptureImpl.
107 // VideoCaptureImpl objects are owned by this object. But they are
108 // destroyed on the IO thread. These are raw pointers because we destroy
110 typedef std::map
<media::VideoCaptureSessionId
,
111 std::pair
<int, VideoCaptureImpl
*> >
112 VideoCaptureDeviceMap
;
113 VideoCaptureDeviceMap devices_
;
115 // This is an internal ID for identifying clients of VideoCaptureImpl.
116 // The ID is global for the render process.
119 scoped_refptr
<VideoCaptureMessageFilter
> filter_
;
121 // Bound to the render thread.
122 base::ThreadChecker thread_checker_
;
124 // Bound to the render thread.
125 // NOTE: Weak pointers must be invalidated before all other member variables.
126 base::WeakPtrFactory
<VideoCaptureImplManager
> weak_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager
);
131 } // namespace content
133 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_