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