| 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 // TODO(hclam): This class should be renamed to VideoCaptureService. | 5 // VideoCaptureImplManager owns VideoCaptureImpl objects. Clients who |
| 6 | 6 // want access to a video capture device call UseDevice() to get a handle |
| 7 // This class provides access to a video capture device in the browser | 7 // to VideoCaptureImpl. |
| 8 // process through IPC. The main function is to deliver video frames | |
| 9 // to a client. | |
| 10 // | 8 // |
| 11 // THREADING | 9 // THREADING |
| 12 // | 10 // |
| 13 // VideoCaptureImplManager lives only on the render thread. All methods | 11 // VideoCaptureImplManager lives only on the render thread. All methods |
| 14 // must be called on this thread. | 12 // must be called on this thread. |
| 15 // | 13 // |
| 16 // VideoFrames are delivered on the IO thread. Callbacks provided by | 14 // The handle returned by UseDevice() is thread-safe. It ensures |
| 17 // a client are also called on the IO thread. | 15 // destruction is handled on the render thread. |
| 18 | 16 |
| 19 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ | 17 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ |
| 20 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ | 18 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ |
| 21 | 19 |
| 22 #include <map> | 20 #include <map> |
| 23 | 21 |
| 24 #include "base/callback.h" | 22 #include "base/callback.h" |
| 25 #include "base/memory/linked_ptr.h" | 23 #include "base/memory/linked_ptr.h" |
| 26 #include "base/memory/ref_counted.h" | |
| 27 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
| 28 #include "base/memory/weak_ptr.h" | 25 #include "base/memory/weak_ptr.h" |
| 29 #include "base/message_loop/message_loop_proxy.h" | 26 #include "base/message_loop/message_loop_proxy.h" |
| 30 #include "base/synchronization/lock.h" | 27 #include "base/synchronization/lock.h" |
| 31 #include "base/threading/thread_checker.h" | 28 #include "base/threading/thread_checker.h" |
| 32 #include "content/common/content_export.h" | 29 #include "content/common/content_export.h" |
| 33 #include "content/common/media/video_capture.h" | 30 #include "media/video/capture/video_capture.h" |
| 34 #include "media/video/capture/video_capture_types.h" | |
| 35 | 31 |
| 36 namespace content { | 32 namespace content { |
| 37 | 33 |
| 38 class VideoCaptureImpl; | 34 class VideoCaptureImpl; |
| 35 class VideoCaptureImplManager; |
| 39 class VideoCaptureMessageFilter; | 36 class VideoCaptureMessageFilter; |
| 40 | 37 |
| 38 // Thread-safe wrapper for a media::VideoCapture object. During |
| 39 // destruction |destruction_cb| is called. This mechanism is used |
| 40 // by VideoCaptureImplManager to ensure de-initialization and |
| 41 // destruction of the media::VideoCapture object happens on the render |
| 42 // thread. |
| 43 class CONTENT_EXPORT VideoCaptureHandle : media::VideoCapture { |
| 44 public: |
| 45 virtual ~VideoCaptureHandle(); |
| 46 |
| 47 // media::VideoCapture implementations. |
| 48 virtual void StartCapture( |
| 49 EventHandler* handler, |
| 50 const media::VideoCaptureParams& params) OVERRIDE; |
| 51 virtual void StopCapture(EventHandler* handler) OVERRIDE; |
| 52 virtual bool CaptureStarted() OVERRIDE; |
| 53 virtual int CaptureFrameRate() OVERRIDE; |
| 54 virtual void GetDeviceSupportedFormats( |
| 55 const DeviceFormatsCallback& callback) OVERRIDE; |
| 56 virtual void GetDeviceFormatsInUse( |
| 57 const DeviceFormatsInUseCallback& callback) OVERRIDE; |
| 58 |
| 59 private: |
| 60 friend class VideoCaptureImplManager; |
| 61 |
| 62 VideoCaptureHandle(media::VideoCapture* impl, |
| 63 base::Closure destruction_cb); |
| 64 |
| 65 media::VideoCapture* impl_; |
| 66 base::Closure destruction_cb_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandle); |
| 69 }; |
| 70 |
| 41 class CONTENT_EXPORT VideoCaptureImplManager { | 71 class CONTENT_EXPORT VideoCaptureImplManager { |
| 42 public: | 72 public: |
| 43 VideoCaptureImplManager(); | 73 VideoCaptureImplManager(); |
| 44 virtual ~VideoCaptureImplManager(); | 74 virtual ~VideoCaptureImplManager(); |
| 45 | 75 |
| 46 // Open a device associated with the session ID. | 76 // Returns a video capture device referenced by |id|. |
| 47 // This method must be called before any methods with the same ID | 77 scoped_ptr<VideoCaptureHandle> UseDevice(media::VideoCaptureSessionId id); |
| 48 // is used. | |
| 49 // Returns a callback that should be used to release the acquired | |
| 50 // resources. | |
| 51 base::Closure UseDevice(media::VideoCaptureSessionId id); | |
| 52 | |
| 53 // Start receiving video frames for the given session ID. | |
| 54 // | |
| 55 // |state_update_cb| will be called on the IO thread when capturing | |
| 56 // state changes. | |
| 57 // States will be one of the following four: | |
| 58 // * VIDEO_CAPTURE_STATE_STARTED | |
| 59 // * VIDEO_CAPTURE_STATE_STOPPED | |
| 60 // * VIDEO_CAPTURE_STATE_PAUSED | |
| 61 // * VIDEO_CAPTURE_STATE_ERROR | |
| 62 // | |
| 63 // |deliver_frame_cb| will be called on the IO thread when a video | |
| 64 // frame is ready. | |
| 65 // | |
| 66 // Returns a callback that is used to stop capturing. Note that stopping | |
| 67 // video capture is not synchronous. Client should handle the case where | |
| 68 // callbacks are called after capturing is instructed to stop, typically | |
| 69 // by binding the passed callbacks on a WeakPtr. | |
| 70 base::Closure StartCapture( | |
| 71 media::VideoCaptureSessionId id, | |
| 72 const media::VideoCaptureParams& params, | |
| 73 const VideoCaptureStateUpdateCB& state_update_cb, | |
| 74 const VideoCaptureDeliverFrameCB& deliver_frame_cb); | |
| 75 | |
| 76 // Get supported formats supported by the device for the given session | |
| 77 // ID. |callback| will be called on the IO thread. | |
| 78 void GetDeviceSupportedFormats( | |
| 79 media::VideoCaptureSessionId id, | |
| 80 const VideoCaptureDeviceFormatsCB& callback); | |
| 81 | |
| 82 // Get supported formats currently in use for the given session ID. | |
| 83 // |callback| will be called on the IO thread. | |
| 84 void GetDeviceFormatsInUse( | |
| 85 media::VideoCaptureSessionId id, | |
| 86 const VideoCaptureDeviceFormatsCB& callback); | |
| 87 | 78 |
| 88 // Make all existing VideoCaptureImpl instances stop/resume delivering | 79 // Make all existing VideoCaptureImpl instances stop/resume delivering |
| 89 // video frames to their clients, depends on flag |suspend|. | 80 // video frames to their clients, depends on flag |suspend|. |
| 90 void SuspendDevices(bool suspend); | 81 void SuspendDevices(bool suspend); |
| 91 | 82 |
| 92 VideoCaptureMessageFilter* video_capture_message_filter() const { | 83 VideoCaptureMessageFilter* video_capture_message_filter() const { |
| 93 return filter_.get(); | 84 return filter_.get(); |
| 94 } | 85 } |
| 95 | 86 |
| 96 protected: | 87 protected: |
| 97 virtual VideoCaptureImpl* CreateVideoCaptureImplForTesting( | 88 // Used in tests to inject a mock VideoCaptureImpl. |
| 89 virtual VideoCaptureImpl* CreateVideoCaptureImpl( |
| 98 media::VideoCaptureSessionId id, | 90 media::VideoCaptureSessionId id, |
| 99 VideoCaptureMessageFilter* filter) const; | 91 VideoCaptureMessageFilter* filter) const; |
| 100 | 92 |
| 101 private: | 93 private: |
| 102 void StopCapture(int client_id, media::VideoCaptureSessionId id); | |
| 103 void UnrefDevice(media::VideoCaptureSessionId id); | 94 void UnrefDevice(media::VideoCaptureSessionId id); |
| 104 | 95 |
| 105 // The int is used to count clients of the corresponding VideoCaptureImpl. | 96 // The int is used to count clients of the corresponding VideoCaptureImpl. |
| 106 // VideoCaptureImpl objects are owned by this object. But they are | |
| 107 // destroyed on the IO thread. These are raw pointers because we destroy | |
| 108 // them manually. | |
| 109 typedef std::map<media::VideoCaptureSessionId, | 97 typedef std::map<media::VideoCaptureSessionId, |
| 110 std::pair<int, VideoCaptureImpl*> > | 98 std::pair<int, linked_ptr<VideoCaptureImpl> > > |
| 111 VideoCaptureDeviceMap; | 99 VideoCaptureDeviceMap; |
| 112 VideoCaptureDeviceMap devices_; | 100 VideoCaptureDeviceMap devices_; |
| 113 | 101 |
| 114 // This is an internal ID for identifying clients of VideoCaptureImpl. | |
| 115 // The ID is global for the render process. | |
| 116 int next_client_id_; | |
| 117 | |
| 118 scoped_refptr<VideoCaptureMessageFilter> filter_; | 102 scoped_refptr<VideoCaptureMessageFilter> filter_; |
| 119 | 103 |
| 120 // Bound to the render thread. | 104 // Bound to the render thread. |
| 121 base::ThreadChecker thread_checker_; | 105 base::ThreadChecker thread_checker_; |
| 122 | 106 |
| 123 // Bound to the render thread. | 107 // Bound to the render thread. |
| 124 // NOTE: Weak pointers must be invalidated before all other member variables. | 108 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 125 base::WeakPtrFactory<VideoCaptureImplManager> weak_factory_; | 109 base::WeakPtrFactory<VideoCaptureImplManager> weak_factory_; |
| 126 | 110 |
| 127 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager); | 111 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager); |
| 128 }; | 112 }; |
| 129 | 113 |
| 130 } // namespace content | 114 } // namespace content |
| 131 | 115 |
| 132 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ | 116 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ |
| OLD | NEW |