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.
5 // VideoCaptureImplManager owns VideoCaptureImpl objects. Clients who
6 // want access to a video capture device call UseDevice() to get a handle
7 // to VideoCaptureImpl.
11 // VideoCaptureImplManager lives only on the render thread. All methods
12 // must be called on this thread.
14 // The handle returned by UseDevice() is thread-safe. It ensures
15 // destruction is handled on the render thread.
17 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
18 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
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"
26 #include "base/message_loop/message_loop_proxy.h"
27 #include "base/synchronization/lock.h"
28 #include "base/threading/thread_checker.h"
29 #include "content/common/content_export.h"
30 #include "media/video/capture/video_capture.h"
34 class VideoCaptureImpl;
35 class VideoCaptureImplManager;
36 class VideoCaptureMessageFilter;
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
43 class CONTENT_EXPORT VideoCaptureHandle : media::VideoCapture {
45 virtual ~VideoCaptureHandle();
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;
60 friend class VideoCaptureImplManager;
62 VideoCaptureHandle(media::VideoCapture* impl,
63 base::Closure destruction_cb);
65 media::VideoCapture* impl_;
66 base::Closure destruction_cb_;
68 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandle);
71 class CONTENT_EXPORT VideoCaptureImplManager {
73 VideoCaptureImplManager();
74 virtual ~VideoCaptureImplManager();
76 // Returns a video capture device referenced by |id|.
77 scoped_ptr<VideoCaptureHandle> UseDevice(media::VideoCaptureSessionId id);
79 // Make all existing VideoCaptureImpl instances stop/resume delivering
80 // video frames to their clients, depends on flag |suspend|.
81 void SuspendDevices(bool suspend);
83 VideoCaptureMessageFilter* video_capture_message_filter() const {
88 // Used in tests to inject a mock VideoCaptureImpl.
89 virtual VideoCaptureImpl* CreateVideoCaptureImpl(
90 media::VideoCaptureSessionId id,
91 VideoCaptureMessageFilter* filter) const;
94 void UnrefDevice(media::VideoCaptureSessionId id);
96 // The int is used to count clients of the corresponding VideoCaptureImpl.
97 typedef std::map<media::VideoCaptureSessionId,
98 std::pair<int, linked_ptr<VideoCaptureImpl> > >
99 VideoCaptureDeviceMap;
100 VideoCaptureDeviceMap devices_;
102 scoped_refptr<VideoCaptureMessageFilter> filter_;
104 // Following two members are bound to the render thread.
105 base::WeakPtrFactory<VideoCaptureImplManager> weak_factory_;
106 base::ThreadChecker thread_checker_;
108 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager);
111 } // namespace content
113 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_