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. 4 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. 10 11 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 12 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 13 14 #include <list> 15 #include <map> 16 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" 23 24 namespace content { 25 class MockVideoCaptureManager; 26 class VideoCaptureController; 27 class VideoCaptureControllerEventHandler; 28 29 // VideoCaptureManager opens/closes and start/stops video capture devices. 30 class CONTENT_EXPORT VideoCaptureManager : public MediaStreamProvider { 31 public: 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 }; 36 37 VideoCaptureManager(); 38 39 // Implements MediaStreamProvider. 40 virtual void Register(MediaStreamProviderListener* listener, 41 base::MessageLoopProxy* device_thread_loop) OVERRIDE; 42 43 virtual void Unregister() OVERRIDE; 44 45 virtual void EnumerateDevices(MediaStreamType stream_type) OVERRIDE; 46 47 virtual int Open(const StreamDeviceInfo& device) OVERRIDE; 48 49 virtual void Close(int capture_session_id) OVERRIDE; 50 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); 57 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); 63 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. 67 void UseFakeDevice(); 68 69 // Called by VideoCaptureHost to get a controller for |capture_params|. 70 // The controller is returned via calling |added_cb|. 71 void AddController( 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); 79 80 private: 81 friend class MockVideoCaptureManager; 82 83 virtual ~VideoCaptureManager(); 84 85 typedef std::list<VideoCaptureControllerEventHandler*> Handlers; 86 struct Controller; 87 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); 103 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); 111 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); 119 120 // Helpers 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); 129 130 // The message loop of media stream device thread that this object runs on. 131 scoped_refptr<base::MessageLoopProxy> device_loop_; 132 133 // Only accessed on Browser::IO thread. 134 MediaStreamProviderListener* listener_; 135 int new_capture_session_id_; 136 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. 140 struct DeviceEntry { 141 MediaStreamType stream_type; 142 media::VideoCaptureDevice* capture_device; // Maybe shared across sessions. 143 }; 144 typedef std::map<int, DeviceEntry> VideoCaptureDevices; 145 VideoCaptureDevices devices_; // Maps capture_session_id to DeviceEntry. 146 147 // Set to true if using fake video capture devices for testing, 148 // false by default. This is only used for the MEDIA_DEVICE_VIDEO_CAPTURE 149 // device type. 150 bool use_fake_device_; 151 152 // Only accessed from device thread. 153 // VideoCaptureManager owns all VideoCaptureController's and is responsible 154 // for deleting the instances when they are not used any longer. 155 // VideoCaptureDevice is one-to-one mapped to VideoCaptureController. 156 typedef std::map<media::VideoCaptureDevice*, Controller*> Controllers; 157 Controllers controllers_; 158 159 // We cache the enumerated video capture devices in GetAvailableDevices 160 // (e.g. called by OnEnumerateDevices) and then look up the requested ID when 161 // a device is opened (see OnOpen). 162 // Used only on the device thread. 163 media::VideoCaptureDevice::Names video_capture_devices_; 164 165 DISALLOW_COPY_AND_ASSIGN(VideoCaptureManager); 166 }; 167 168 } // namespace content 169 170 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_MANAGER_H_ 171