Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / video_capture_impl_manager.h
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 // VideoCaptureImplManager owns VideoCaptureImpl objects. Clients who
6 // want access to a video capture device call UseDevice() to get a handle
7 // to VideoCaptureImpl.
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.
16
17 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
18 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
19
20 #include <map>
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"
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"
31
32 namespace content {
33
34 class VideoCaptureImpl;
35 class VideoCaptureImplManager;
36 class VideoCaptureMessageFilter;
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
71 class CONTENT_EXPORT VideoCaptureImplManager {
72  public:
73   VideoCaptureImplManager();
74   virtual ~VideoCaptureImplManager();
75
76   // Returns a video capture device referenced by |id|.
77   scoped_ptr<VideoCaptureHandle> UseDevice(media::VideoCaptureSessionId id);
78
79   // Make all existing VideoCaptureImpl instances stop/resume delivering
80   // video frames to their clients, depends on flag |suspend|.
81   void SuspendDevices(bool suspend);
82
83   VideoCaptureMessageFilter* video_capture_message_filter() const {
84     return filter_.get();
85   }
86
87  protected:
88   // Used in tests to inject a mock VideoCaptureImpl.
89   virtual VideoCaptureImpl* CreateVideoCaptureImpl(
90       media::VideoCaptureSessionId id,
91       VideoCaptureMessageFilter* filter) const;
92
93  private:
94   void UnrefDevice(media::VideoCaptureSessionId id);
95
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_;
101
102   scoped_refptr<VideoCaptureMessageFilter> filter_;
103
104   // Following two members are bound to the render thread.
105   base::WeakPtrFactory<VideoCaptureImplManager> weak_factory_;
106   base::ThreadChecker thread_checker_;
107
108   DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager);
109 };
110
111 }  // namespace content
112
113 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_