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 manages video capture devices in renderer process. | 5 // VideoCaptureImplManager owns VideoCaptureImpl objects. Clients who |
6 // The video capture clients use AddDevice() to get a pointer to | 6 // want access to a video capture device call UseDevice() to get a handle |
7 // video capture device. VideoCaputreImplManager supports multiple clients | 7 // to VideoCaptureImpl. |
8 // accessing same device. | 8 // |
| 9 // THREADING |
| 10 // |
| 11 // VideoCaptureImplManager lives only on the render thread. All methods |
| 12 // must be called on this thread. |
| 13 // |
| 14 // The handle returned by UseDevice() is thread-safe. It ensures |
| 15 // destruction is handled on the render thread. |
9 | 16 |
10 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ | 17 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ |
11 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ | 18 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ |
12 | 19 |
13 #include <list> | |
14 #include <map> | 20 #include <map> |
15 | 21 |
| 22 #include "base/callback.h" |
| 23 #include "base/memory/linked_ptr.h" |
| 24 #include "base/memory/scoped_ptr.h" |
| 25 #include "base/memory/weak_ptr.h" |
16 #include "base/message_loop/message_loop_proxy.h" | 26 #include "base/message_loop/message_loop_proxy.h" |
17 #include "base/threading/thread.h" | |
18 #include "base/synchronization/lock.h" | 27 #include "base/synchronization/lock.h" |
| 28 #include "base/threading/thread_checker.h" |
19 #include "content/common/content_export.h" | 29 #include "content/common/content_export.h" |
20 #include "media/video/capture/video_capture.h" | 30 #include "media/video/capture/video_capture.h" |
21 | 31 |
22 namespace content { | 32 namespace content { |
23 | 33 |
24 class VideoCaptureImpl; | 34 class VideoCaptureImpl; |
| 35 class VideoCaptureImplManager; |
25 class VideoCaptureMessageFilter; | 36 class VideoCaptureMessageFilter; |
26 | 37 |
27 class CONTENT_EXPORT VideoCaptureImplManager | 38 // Thread-safe wrapper for a media::VideoCapture object. During |
28 : public base::RefCountedThreadSafe<VideoCaptureImplManager> { | 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 |
| 55 private: |
| 56 friend class VideoCaptureImplManager; |
| 57 |
| 58 VideoCaptureHandle(media::VideoCapture* impl, |
| 59 base::Closure destruction_cb); |
| 60 |
| 61 media::VideoCapture* impl_; |
| 62 base::Closure destruction_cb_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandle); |
| 65 }; |
| 66 |
| 67 class CONTENT_EXPORT VideoCaptureImplManager { |
29 public: | 68 public: |
30 VideoCaptureImplManager(); | 69 VideoCaptureImplManager(); |
| 70 virtual ~VideoCaptureImplManager(); |
31 | 71 |
32 // Called by video capture client |handler| to add device referenced | 72 // Returns a video capture device referenced by |id|. |
33 // by |id| to VideoCaptureImplManager's list of opened device list. | 73 scoped_ptr<VideoCaptureHandle> UseDevice(media::VideoCaptureSessionId id); |
34 // A pointer to VideoCapture is returned to client so that client can | |
35 // operate on that pointer, such as StartCaptrue, StopCapture. | |
36 virtual media::VideoCapture* AddDevice( | |
37 media::VideoCaptureSessionId id, | |
38 media::VideoCapture::EventHandler* handler); | |
39 | |
40 // Called by video capture client |handler| to remove device referenced | |
41 // by |id| from VideoCaptureImplManager's list of opened device list. | |
42 virtual void RemoveDevice(media::VideoCaptureSessionId id, | |
43 media::VideoCapture::EventHandler* handler); | |
44 | 74 |
45 // Make all existing VideoCaptureImpl instances stop/resume delivering | 75 // Make all existing VideoCaptureImpl instances stop/resume delivering |
46 // video frames to their clients, depends on flag |suspend|. | 76 // video frames to their clients, depends on flag |suspend|. |
47 virtual void SuspendDevices(bool suspend); | 77 void SuspendDevices(bool suspend); |
48 | 78 |
49 VideoCaptureMessageFilter* video_capture_message_filter() const { | 79 VideoCaptureMessageFilter* video_capture_message_filter() const { |
50 return filter_.get(); | 80 return filter_.get(); |
51 } | 81 } |
52 | 82 |
53 protected: | 83 protected: |
54 virtual ~VideoCaptureImplManager(); | 84 // Used in tests to inject a mock VideoCaptureImpl. |
| 85 virtual VideoCaptureImpl* CreateVideoCaptureImpl( |
| 86 media::VideoCaptureSessionId id, |
| 87 VideoCaptureMessageFilter* filter) const; |
55 | 88 |
56 private: | 89 private: |
57 friend class base::RefCountedThreadSafe<VideoCaptureImplManager>; | 90 void UnrefDevice(media::VideoCaptureSessionId id); |
58 | 91 |
59 struct Device { | 92 // The int is used to count clients of the corresponding VideoCaptureImpl. |
60 Device(VideoCaptureImpl* device, | 93 typedef std::map<media::VideoCaptureSessionId, |
61 media::VideoCapture::EventHandler* handler); | 94 std::pair<int, linked_ptr<VideoCaptureImpl> > > |
62 ~Device(); | 95 VideoCaptureDeviceMap; |
| 96 VideoCaptureDeviceMap devices_; |
63 | 97 |
64 VideoCaptureImpl* vc; | 98 scoped_refptr<VideoCaptureMessageFilter> filter_; |
65 std::list<media::VideoCapture::EventHandler*> clients; | |
66 }; | |
67 | 99 |
68 void FreeDevice(VideoCaptureImpl* vc); | 100 // Following two members are bound to the render thread. |
69 | 101 base::WeakPtrFactory<VideoCaptureImplManager> weak_factory_; |
70 typedef std::map<media::VideoCaptureSessionId, Device*> Devices; | 102 base::ThreadChecker thread_checker_; |
71 Devices devices_; | |
72 base::Lock lock_; | |
73 scoped_refptr<VideoCaptureMessageFilter> filter_; | |
74 base::Thread thread_; | |
75 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
76 | 103 |
77 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager); | 104 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager); |
78 }; | 105 }; |
79 | 106 |
80 } // namespace content | 107 } // namespace content |
81 | 108 |
82 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ | 109 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_ |
OLD | NEW |