OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 // VideoCaptureManager is used to open/close, start/stop as well as enumerate |
| 6 // available video capture devices. All functions are expected to be called from |
| 7 // the Browser::IO thread. VideoCaptureManager will open OS dependent instances |
| 8 // of VideoCaptureDevice. A device can only be opened once. |
| 9 |
| 10 #ifndef CONTENT_BROWSER_MEDIA_STREAM_VIDEO_CAPTURE_MANAGER_H_ |
| 11 #define CONTENT_BROWSER_MEDIA_STREAM_VIDEO_CAPTURE_MANAGER_H_ |
| 12 |
| 13 #include <map> |
| 14 |
| 15 #include "base/lazy_instance.h" |
| 16 #include "base/threading/thread.h" |
| 17 #include "content/browser/media_stream/media_stream_provider.h" |
| 18 #include "media/video/capture/video_capture_device.h" |
| 19 #include "media/video/capture/video_capture_types.h" |
| 20 |
| 21 namespace media_stream { |
| 22 |
| 23 // VideoCaptureManager opens/closes and start/stops video capture devices. |
| 24 class VideoCaptureManager : public MediaStreamProvider { |
| 25 public: |
| 26 // Calling |Start| of this id will open the first device, even though open has |
| 27 // not been called. This is used to be able to use video capture devices |
| 28 // before MediaStream is implemented in Chrome and WebKit. |
| 29 enum { kStartOpenSessionId = 1 }; |
| 30 |
| 31 // Called to get a pointer to the singleton |
| 32 static VideoCaptureManager* Get(); |
| 33 |
| 34 // Implements MediaStreamProvider. |
| 35 virtual bool Register(MediaStreamProviderListener* listener); |
| 36 |
| 37 virtual void Unregister(); |
| 38 |
| 39 virtual void EnumerateDevices(); |
| 40 |
| 41 virtual MediaCaptureSessionId Open(const MediaCaptureDeviceInfo& device); |
| 42 |
| 43 virtual void Close(MediaCaptureSessionId capture_session_id); |
| 44 |
| 45 // Functions used to start and stop media flow. |
| 46 // Start allocates the device and no other application can use the device |
| 47 // before Stop is called. Captured video frames will be delivered to |
| 48 // video_capture_receiver. |
| 49 void Start(const media::VideoCaptureParams& capture_params, |
| 50 media::VideoCaptureDevice::EventHandler* video_capture_receiver); |
| 51 |
| 52 // Stops capture device referenced by |capture_session_id|. No more frames |
| 53 // will be delivered to the frame receiver, and |stopped_task| will be called. |
| 54 void Stop(const media::VideoCaptureSessionId capture_session_id, |
| 55 Task* stopped_task); |
| 56 |
| 57 virtual ~VideoCaptureManager(); |
| 58 |
| 59 // Used by unit test to make sure a fake device is used instead of a real |
| 60 // video capture device. Due to timing requirements, the function must be |
| 61 // called before EnumerateDevices and Open. |
| 62 void UseFakeDevice(); |
| 63 MessageLoop* GetMessageLoop(); |
| 64 |
| 65 private: |
| 66 friend struct base::DefaultLazyInstanceTraits<VideoCaptureManager>; |
| 67 |
| 68 VideoCaptureManager(); |
| 69 |
| 70 // Called by the public functions, executed on vc_device_thread_. |
| 71 void OnEnumerateDevices(); |
| 72 void OnOpen(MediaCaptureSessionId capture_session_id, |
| 73 const MediaCaptureDeviceInfo device); |
| 74 void OnClose(MediaCaptureSessionId capture_session_id); |
| 75 void OnStart(const media::VideoCaptureParams capture_params, |
| 76 media::VideoCaptureDevice::EventHandler* video_capture_receiver); |
| 77 void OnStop(const media::VideoCaptureSessionId capture_session_id, |
| 78 Task* stopped_task); |
| 79 |
| 80 |
| 81 // Executed on Browser::IO thread to call Listener. |
| 82 void OnOpened(MediaCaptureSessionId capture_session_id); |
| 83 void OnClosed(MediaCaptureSessionId capture_session_id); |
| 84 void OnDevicesEnumerated(const MediaCaptureDevices& devices); |
| 85 void OnError(MediaCaptureSessionId capture_session_id, |
| 86 MediaStreamProviderError error); |
| 87 |
| 88 // Executed on vc_device_thread_ to make sure Listener is called from |
| 89 // Browser::IO thread. |
| 90 void PostOnOpened(MediaCaptureSessionId capture_session_id); |
| 91 void PostOnClosed(MediaCaptureSessionId capture_session_id); |
| 92 void PostOnDevicesEnumerated(MediaCaptureDevices devices); |
| 93 void PostOnError(MediaCaptureSessionId capture_session_id, |
| 94 MediaStreamProviderError error); |
| 95 |
| 96 // Helpers |
| 97 void GetAvailableDevices(media::VideoCaptureDevice::Names* device_names); |
| 98 bool DeviceOpened(const media::VideoCaptureDevice::Name& device_name); |
| 99 bool DeviceOpened(const MediaCaptureDeviceInfo& device_info); |
| 100 bool IsOnCaptureDeviceThread() const; |
| 101 |
| 102 // Thread for all calls to VideoCaptureDevice |
| 103 base::Thread vc_device_thread_; |
| 104 |
| 105 // Only accessed on Browser::IO thread |
| 106 MediaStreamProviderListener* listener_; |
| 107 MediaCaptureSessionId new_capture_session_id_; |
| 108 |
| 109 // Only accessed from vc_device_thread_ |
| 110 // TODO(mflodman) Change map key type when changing typedef for |
| 111 // MediaCaptureSessionId. |
| 112 typedef std::map<int, media::VideoCaptureDevice*> VideoCaptureDevices; |
| 113 VideoCaptureDevices devices_; |
| 114 |
| 115 // Set to true if using fake devices for testing, false by default. |
| 116 bool use_fake_device_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); |
| 119 }; |
| 120 |
| 121 } // namespace media_stream |
| 122 |
| 123 DISABLE_RUNNABLE_METHOD_REFCOUNT(media_stream::VideoCaptureManager); |
| 124 |
| 125 #endif // CONTENT_BROWSER_MEDIA_STREAM_VIDEO_CAPTURE_MANAGER_H_ |
OLD | NEW |