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 // VideoCaptureManager is used to open/close, start/stop, enumerate available
6 // video capture devices, and manage VideoCaptureController's.
7 // All functions are expected to be called from Browser::IO thread.
8 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice.
9 // A device can only be opened once.
11 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
12 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_
17 #include "base/memory/ref_counted.h"
18 #include "content/browser/renderer_host/media/media_stream_provider.h"
19 #include "content/common/content_export.h"
20 #include "content/common/media/media_stream_options.h"
21 #include "media/video/capture/video_capture_device.h"
22 #include "media/video/capture/video_capture_types.h"
25 class MockVideoCaptureManager
;
26 class VideoCaptureController
;
27 class VideoCaptureControllerEventHandler
;
29 // VideoCaptureManager opens/closes and start/stops video capture devices.
30 class CONTENT_EXPORT VideoCaptureManager
: public MediaStreamProvider
{
32 // Calling |Start| of this id will open the first device, even though open has
33 // not been called. This is used to be able to use video capture devices
34 // before MediaStream is implemented in Chrome and WebKit.
35 enum { kStartOpenSessionId
= 1 };
37 VideoCaptureManager();
39 // Implements MediaStreamProvider.
40 virtual void Register(MediaStreamProviderListener
* listener
,
41 base::MessageLoopProxy
* device_thread_loop
) OVERRIDE
;
43 virtual void Unregister() OVERRIDE
;
45 virtual void EnumerateDevices(MediaStreamType stream_type
) OVERRIDE
;
47 virtual int Open(const StreamDeviceInfo
& device
) OVERRIDE
;
49 virtual void Close(int capture_session_id
) OVERRIDE
;
51 // Functions used to start and stop media flow.
52 // Start allocates the device and no other application can use the device
53 // before Stop is called. Captured video frames will be delivered to
54 // video_capture_receiver.
55 virtual void Start(const media::VideoCaptureParams
& capture_params
,
56 media::VideoCaptureDevice::EventHandler
* video_capture_receiver
);
58 // Stops capture device referenced by |capture_session_id|. No more frames
59 // will be delivered to the frame receiver, and |stopped_cb| will be called.
60 // |stopped_cb| can be NULL.
61 virtual void Stop(const media::VideoCaptureSessionId
& capture_session_id
,
62 base::Closure stopped_cb
);
64 // Used by unit test to make sure a fake device is used instead of a real
65 // video capture device. Due to timing requirements, the function must be
66 // called before EnumerateDevices and Open.
69 // Called by VideoCaptureHost to get a controller for |capture_params|.
70 // The controller is returned via calling |added_cb|.
72 const media::VideoCaptureParams
& capture_params
,
73 VideoCaptureControllerEventHandler
* handler
,
74 base::Callback
<void(VideoCaptureController
*)> added_cb
);
75 // Called by VideoCaptureHost to remove the |controller|.
76 void RemoveController(
77 VideoCaptureController
* controller
,
78 VideoCaptureControllerEventHandler
* handler
);
81 friend class MockVideoCaptureManager
;
83 virtual ~VideoCaptureManager();
85 typedef std::list
<VideoCaptureControllerEventHandler
*> Handlers
;
88 // Called by the public functions, executed on device thread.
89 void OnEnumerateDevices(MediaStreamType stream_type
);
90 void OnOpen(int capture_session_id
, const StreamDeviceInfo
& device
);
91 void OnClose(int capture_session_id
);
92 void OnStart(const media::VideoCaptureParams capture_params
,
93 media::VideoCaptureDevice::EventHandler
* video_capture_receiver
);
94 void OnStop(const media::VideoCaptureSessionId capture_session_id
,
95 base::Closure stopped_cb
);
96 void DoAddControllerOnDeviceThread(
97 const media::VideoCaptureParams capture_params
,
98 VideoCaptureControllerEventHandler
* handler
,
99 base::Callback
<void(VideoCaptureController
*)> added_cb
);
100 void DoRemoveControllerOnDeviceThread(
101 VideoCaptureController
* controller
,
102 VideoCaptureControllerEventHandler
* handler
);
104 // Executed on Browser::IO thread to call Listener.
105 void OnOpened(MediaStreamType type
, int capture_session_id
);
106 void OnClosed(MediaStreamType type
, int capture_session_id
);
107 void OnDevicesEnumerated(MediaStreamType stream_type
,
108 scoped_ptr
<StreamDeviceInfoArray
> devices
);
109 void OnError(MediaStreamType type
, int capture_session_id
,
110 MediaStreamProviderError error
);
112 // Executed on device thread to make sure Listener is called from
113 // Browser::IO thread.
114 void PostOnOpened(MediaStreamType type
, int capture_session_id
);
115 void PostOnClosed(MediaStreamType type
, int capture_session_id
);
116 void PostOnDevicesEnumerated(MediaStreamType stream_type
,
117 scoped_ptr
<StreamDeviceInfoArray
> devices
);
118 void PostOnError(int capture_session_id
, MediaStreamProviderError error
);
121 void GetAvailableDevices(MediaStreamType stream_type
,
122 media::VideoCaptureDevice::Names
* device_names
);
123 bool DeviceOpened(const media::VideoCaptureDevice::Name
& device_name
);
124 bool DeviceInUse(const media::VideoCaptureDevice
* video_capture_device
);
125 media::VideoCaptureDevice
* GetOpenedDevice(
126 const StreamDeviceInfo
& device_info
);
127 bool IsOnDeviceThread() const;
128 media::VideoCaptureDevice
* GetDeviceInternal(int capture_session_id
);
130 // The message loop of media stream device thread that this object runs on.
131 scoped_refptr
<base::MessageLoopProxy
> device_loop_
;
133 // Only accessed on Browser::IO thread.
134 MediaStreamProviderListener
* listener_
;
135 int new_capture_session_id_
;
137 // Only accessed from device thread.
138 // VideoCaptureManager owns all VideoCaptureDevices and is responsible for
139 // deleting the instances when they are not used any longer.
141 MediaStreamType stream_type
;
142 media::VideoCaptureDevice
* capture_device
; // Maybe shared across sessions.
144 typedef std::map
<int, DeviceEntry
> VideoCaptureDevices
;
145 VideoCaptureDevices devices_
; // Maps capture_session_id to DeviceEntry.
147 // Set to true if using fake devices for testing, false by default.
148 bool use_fake_device_
;
150 // Only accessed from device thread.
151 // VideoCaptureManager owns all VideoCaptureController's and is responsible
152 // for deleting the instances when they are not used any longer.
153 // VideoCaptureDevice is one-to-one mapped to VideoCaptureController.
154 typedef std::map
<media::VideoCaptureDevice
*, Controller
*> Controllers
;
155 Controllers controllers_
;
157 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager
);
160 } // namespace content
162 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_