OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // VideoCaptureManager is used to open/close, start/stop, enumerate available | 5 // VideoCaptureManager is used to open/close, start/stop, enumerate available |
6 // video capture devices, and manage VideoCaptureController's. | 6 // video capture devices, and manage VideoCaptureController's. |
7 // All functions are expected to be called from Browser::IO thread. | 7 // All functions are expected to be called from Browser::IO thread. |
8 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice. | 8 // VideoCaptureManager will open OS dependent instances of VideoCaptureDevice. |
9 // A device can only be opened once. | 9 // A device can only be opened once. |
10 | 10 |
11 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 11 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
12 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 12 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
13 | 13 |
14 #include <list> | 14 #include <list> |
15 #include <map> | 15 #include <map> |
16 | 16 |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/threading/thread.h" | |
19 #include "content/browser/renderer_host/media/media_stream_provider.h" | 18 #include "content/browser/renderer_host/media/media_stream_provider.h" |
20 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
21 #include "content/common/media/media_stream_options.h" | 20 #include "content/common/media/media_stream_options.h" |
22 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
23 #include "media/video/capture/video_capture_device.h" | 22 #include "media/video/capture/video_capture_device.h" |
24 #include "media/video/capture/video_capture_types.h" | 23 #include "media/video/capture/video_capture_types.h" |
25 | 24 |
26 class MockVideoCaptureManager; | 25 class MockVideoCaptureManager; |
27 class VideoCaptureController; | 26 class VideoCaptureController; |
28 class VideoCaptureControllerEventHandler; | 27 class VideoCaptureControllerEventHandler; |
29 | 28 |
30 namespace media_stream { | 29 namespace media_stream { |
31 | 30 |
32 // VideoCaptureManager opens/closes and start/stops video capture devices. | 31 // VideoCaptureManager opens/closes and start/stops video capture devices. |
33 // It is deleted on the FILE thread so that it can call base::Thread::Stop() | |
34 // there without blocking UI/IO threads. This is also needed for incognito | |
35 // window. When incognito window is closed, IO thread is not in shutdown mode | |
36 // and base::Thread::Stop() can't be called on IO thread. | |
37 class CONTENT_EXPORT VideoCaptureManager | 32 class CONTENT_EXPORT VideoCaptureManager |
38 : public base::RefCountedThreadSafe<VideoCaptureManager, | 33 : public base::RefCountedThreadSafe<VideoCaptureManager>, |
39 content::BrowserThread::DeleteOnFileThread>, | |
40 public MediaStreamProvider { | 34 public MediaStreamProvider { |
41 public: | 35 public: |
42 // Calling |Start| of this id will open the first device, even though open has | 36 // Calling |Start| of this id will open the first device, even though open has |
43 // not been called. This is used to be able to use video capture devices | 37 // not been called. This is used to be able to use video capture devices |
44 // before MediaStream is implemented in Chrome and WebKit. | 38 // before MediaStream is implemented in Chrome and WebKit. |
45 enum { kStartOpenSessionId = 1 }; | 39 enum { kStartOpenSessionId = 1 }; |
46 | 40 |
47 VideoCaptureManager(); | 41 VideoCaptureManager(); |
48 | 42 |
49 // Implements MediaStreamProvider. | 43 // Implements MediaStreamProvider. |
50 virtual void Register(MediaStreamProviderListener* listener) OVERRIDE; | 44 virtual void Register(MediaStreamProviderListener* listener, |
| 45 base::MessageLoopProxy* device_thread_loop) OVERRIDE; |
51 | 46 |
52 virtual void Unregister() OVERRIDE; | 47 virtual void Unregister() OVERRIDE; |
53 | 48 |
54 virtual void EnumerateDevices() OVERRIDE; | 49 virtual void EnumerateDevices() OVERRIDE; |
55 | 50 |
56 virtual int Open(const StreamDeviceInfo& device) OVERRIDE; | 51 virtual int Open(const StreamDeviceInfo& device) OVERRIDE; |
57 | 52 |
58 virtual void Close(int capture_session_id) OVERRIDE; | 53 virtual void Close(int capture_session_id) OVERRIDE; |
59 | 54 |
60 // Functions used to start and stop media flow. | 55 // Functions used to start and stop media flow. |
(...skipping 10 matching lines...) Expand all Loading... |
71 base::Closure stopped_cb); | 66 base::Closure stopped_cb); |
72 | 67 |
73 // A capture device error has occurred for |capture_session_id|. The device | 68 // A capture device error has occurred for |capture_session_id|. The device |
74 // won't stream any more captured frames. | 69 // won't stream any more captured frames. |
75 virtual void Error(const media::VideoCaptureSessionId& capture_session_id); | 70 virtual void Error(const media::VideoCaptureSessionId& capture_session_id); |
76 | 71 |
77 // Used by unit test to make sure a fake device is used instead of a real | 72 // Used by unit test to make sure a fake device is used instead of a real |
78 // video capture device. Due to timing requirements, the function must be | 73 // video capture device. Due to timing requirements, the function must be |
79 // called before EnumerateDevices and Open. | 74 // called before EnumerateDevices and Open. |
80 void UseFakeDevice(); | 75 void UseFakeDevice(); |
81 MessageLoop* GetMessageLoop(); | |
82 | 76 |
83 // Called by VideoCaptureHost to get a controller for |capture_params|. | 77 // Called by VideoCaptureHost to get a controller for |capture_params|. |
84 // The controller is returned via calling |added_cb|. | 78 // The controller is returned via calling |added_cb|. |
85 void AddController( | 79 void AddController( |
86 const media::VideoCaptureParams& capture_params, | 80 const media::VideoCaptureParams& capture_params, |
87 VideoCaptureControllerEventHandler* handler, | 81 VideoCaptureControllerEventHandler* handler, |
88 base::Callback<void(VideoCaptureController*)> added_cb); | 82 base::Callback<void(VideoCaptureController*)> added_cb); |
89 // Called by VideoCaptureHost to remove the |controller|. | 83 // Called by VideoCaptureHost to remove the |controller|. |
90 void RemoveController( | 84 void RemoveController( |
91 VideoCaptureController* controller, | 85 VideoCaptureController* controller, |
92 VideoCaptureControllerEventHandler* handler); | 86 VideoCaptureControllerEventHandler* handler); |
93 | 87 |
94 private: | 88 private: |
95 friend class ::MockVideoCaptureManager; | 89 friend class ::MockVideoCaptureManager; |
96 friend struct content::BrowserThread::DeleteOnThread< | 90 friend class base::RefCountedThreadSafe<VideoCaptureManager>; |
97 content::BrowserThread::FILE>; | |
98 friend class base::DeleteHelper<VideoCaptureManager>; | |
99 friend class base::RefCountedThreadSafe<VideoCaptureManager, | |
100 content::BrowserThread::DeleteOnFileThread>; | |
101 | 91 |
102 virtual ~VideoCaptureManager(); | 92 virtual ~VideoCaptureManager(); |
103 | 93 |
104 typedef std::list<VideoCaptureControllerEventHandler*> Handlers; | 94 typedef std::list<VideoCaptureControllerEventHandler*> Handlers; |
105 struct Controller; | 95 struct Controller; |
106 | 96 |
107 // Called by the public functions, executed on vc_device_thread_. | 97 // Called by the public functions, executed on device thread. |
108 void OnEnumerateDevices(); | 98 void OnEnumerateDevices(); |
109 void OnOpen(int capture_session_id, const StreamDeviceInfo& device); | 99 void OnOpen(int capture_session_id, const StreamDeviceInfo& device); |
110 void OnClose(int capture_session_id); | 100 void OnClose(int capture_session_id); |
111 void OnStart(const media::VideoCaptureParams capture_params, | 101 void OnStart(const media::VideoCaptureParams capture_params, |
112 media::VideoCaptureDevice::EventHandler* video_capture_receiver); | 102 media::VideoCaptureDevice::EventHandler* video_capture_receiver); |
113 void OnStop(const media::VideoCaptureSessionId capture_session_id, | 103 void OnStop(const media::VideoCaptureSessionId capture_session_id, |
114 base::Closure stopped_cb); | 104 base::Closure stopped_cb); |
115 void DoAddControllerOnDeviceThread( | 105 void DoAddControllerOnDeviceThread( |
116 const media::VideoCaptureParams capture_params, | 106 const media::VideoCaptureParams capture_params, |
117 VideoCaptureControllerEventHandler* handler, | 107 VideoCaptureControllerEventHandler* handler, |
118 base::Callback<void(VideoCaptureController*)> added_cb); | 108 base::Callback<void(VideoCaptureController*)> added_cb); |
119 void DoRemoveControllerOnDeviceThread( | 109 void DoRemoveControllerOnDeviceThread( |
120 VideoCaptureController* controller, | 110 VideoCaptureController* controller, |
121 VideoCaptureControllerEventHandler* handler); | 111 VideoCaptureControllerEventHandler* handler); |
122 | 112 |
123 // Executed on Browser::IO thread to call Listener. | 113 // Executed on Browser::IO thread to call Listener. |
124 void OnOpened(int capture_session_id); | 114 void OnOpened(int capture_session_id); |
125 void OnClosed(int capture_session_id); | 115 void OnClosed(int capture_session_id); |
126 void OnDevicesEnumerated(const StreamDeviceInfoArray& devices); | 116 void OnDevicesEnumerated(const StreamDeviceInfoArray& devices); |
127 void OnError(int capture_session_id, MediaStreamProviderError error); | 117 void OnError(int capture_session_id, MediaStreamProviderError error); |
128 | 118 |
129 // Executed on vc_device_thread_ to make sure Listener is called from | 119 // Executed on device thread to make sure Listener is called from |
130 // Browser::IO thread. | 120 // Browser::IO thread. |
131 void PostOnOpened(int capture_session_id); | 121 void PostOnOpened(int capture_session_id); |
132 void PostOnClosed(int capture_session_id); | 122 void PostOnClosed(int capture_session_id); |
133 void PostOnDevicesEnumerated(const StreamDeviceInfoArray& devices); | 123 void PostOnDevicesEnumerated(const StreamDeviceInfoArray& devices); |
134 void PostOnError(int capture_session_id, MediaStreamProviderError error); | 124 void PostOnError(int capture_session_id, MediaStreamProviderError error); |
135 | 125 |
136 // Helpers | 126 // Helpers |
137 void GetAvailableDevices(media::VideoCaptureDevice::Names* device_names); | 127 void GetAvailableDevices(media::VideoCaptureDevice::Names* device_names); |
138 bool DeviceOpened(const media::VideoCaptureDevice::Name& device_name); | 128 bool DeviceOpened(const media::VideoCaptureDevice::Name& device_name); |
139 bool DeviceInUse(const media::VideoCaptureDevice* video_capture_device); | 129 bool DeviceInUse(const media::VideoCaptureDevice* video_capture_device); |
140 media::VideoCaptureDevice* GetOpenedDevice( | 130 media::VideoCaptureDevice* GetOpenedDevice( |
141 const StreamDeviceInfo& device_info); | 131 const StreamDeviceInfo& device_info); |
142 bool IsOnCaptureDeviceThread() const; | 132 bool IsOnDeviceThread() const; |
143 media::VideoCaptureDevice* GetDeviceInternal(int capture_session_id); | 133 media::VideoCaptureDevice* GetDeviceInternal(int capture_session_id); |
144 | 134 |
145 // Thread for all calls to VideoCaptureDevice. | 135 // The message loop of media stream device thread that this object runs on. |
146 base::Thread vc_device_thread_; | 136 scoped_refptr<base::MessageLoopProxy> device_loop_; |
147 | 137 |
148 // Only accessed on Browser::IO thread. | 138 // Only accessed on Browser::IO thread. |
149 MediaStreamProviderListener* listener_; | 139 MediaStreamProviderListener* listener_; |
150 int new_capture_session_id_; | 140 int new_capture_session_id_; |
151 | 141 |
152 // Only accessed from vc_device_thread_. | 142 // Only accessed from device thread. |
153 // VideoCaptureManager owns all VideoCaptureDevices and is responsible for | 143 // VideoCaptureManager owns all VideoCaptureDevices and is responsible for |
154 // deleting the instances when they are not used any longer. | 144 // deleting the instances when they are not used any longer. |
155 typedef std::map<int, media::VideoCaptureDevice*> VideoCaptureDevices; | 145 typedef std::map<int, media::VideoCaptureDevice*> VideoCaptureDevices; |
156 VideoCaptureDevices devices_; | 146 VideoCaptureDevices devices_; |
157 | 147 |
158 // Set to true if using fake devices for testing, false by default. | 148 // Set to true if using fake devices for testing, false by default. |
159 bool use_fake_device_; | 149 bool use_fake_device_; |
160 | 150 |
161 // Only accessed from vc_device_thread_. | 151 // Only accessed from device thread. |
162 // VideoCaptureManager owns all VideoCaptureController's and is responsible | 152 // VideoCaptureManager owns all VideoCaptureController's and is responsible |
163 // for deleting the instances when they are not used any longer. | 153 // for deleting the instances when they are not used any longer. |
164 // VideoCaptureDevice is one-to-one mapped to VideoCaptureController. | 154 // VideoCaptureDevice is one-to-one mapped to VideoCaptureController. |
165 typedef std::map<media::VideoCaptureDevice*, Controller*> Controllers; | 155 typedef std::map<media::VideoCaptureDevice*, Controller*> Controllers; |
166 Controllers controllers_; | 156 Controllers controllers_; |
167 | 157 |
168 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); | 158 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
169 }; | 159 }; |
170 | 160 |
171 } // namespace media_stream | 161 } // namespace media_stream |
172 | 162 |
173 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ | 163 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |