Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / video_capture_impl_manager.h
blob4fce3c7af05f009f212cafdb5a39fd75753a0b21
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 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
6 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
8 #include <map>
10 #include "base/callback.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/synchronization/lock.h"
17 #include "base/threading/thread_checker.h"
18 #include "content/common/content_export.h"
19 #include "content/common/media/video_capture.h"
20 #include "content/public/renderer/media_stream_video_sink.h"
21 #include "media/base/video_capture_types.h"
23 namespace content {
25 class VideoCaptureImpl;
26 class VideoCaptureMessageFilter;
28 // TODO(hclam): This class should be renamed to VideoCaptureService.
30 // This class provides access to a video capture device in the browser
31 // process through IPC. The main function is to deliver video frames
32 // to a client.
34 // THREADING
36 // VideoCaptureImplManager lives only on the Render Main thread. All methods
37 // must be called on this thread.
39 // VideoFrames are delivered on the IO thread. Callbacks provided by
40 // a client are also called on the IO thread.
41 class CONTENT_EXPORT VideoCaptureImplManager {
42 public:
43 VideoCaptureImplManager();
44 virtual ~VideoCaptureImplManager();
46 // Open a device associated with the session ID.
47 // This method must be called before any methods with the same 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);
53 // Start receiving video frames for the given session ID.
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
63 // |deliver_frame_cb| will be called on the IO thread when a video
64 // frame is ready.
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);
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(media::VideoCaptureSessionId id,
79 const VideoCaptureDeviceFormatsCB& callback);
81 // Get supported formats currently in use for the given session ID.
82 // |callback| will be called on the IO thread.
83 void GetDeviceFormatsInUse(media::VideoCaptureSessionId id,
84 const VideoCaptureDeviceFormatsCB& callback);
86 // Make all existing VideoCaptureImpl instances stop/resume delivering
87 // video frames to their clients, depends on flag |suspend|.
88 void SuspendDevices(bool suspend);
90 VideoCaptureMessageFilter* video_capture_message_filter() const {
91 return filter_.get();
94 protected:
95 virtual VideoCaptureImpl* CreateVideoCaptureImplForTesting(
96 media::VideoCaptureSessionId id,
97 VideoCaptureMessageFilter* filter) const;
99 private:
100 void StopCapture(int client_id, media::VideoCaptureSessionId id);
101 void UnrefDevice(media::VideoCaptureSessionId id);
103 // The int is used to count clients of the corresponding VideoCaptureImpl.
104 // VideoCaptureImpl objects are owned by this object. But they are
105 // destroyed on the IO thread. These are raw pointers because we destroy
106 // them manually.
107 typedef std::map<media::VideoCaptureSessionId,
108 std::pair<int, VideoCaptureImpl*> >
109 VideoCaptureDeviceMap;
110 VideoCaptureDeviceMap devices_;
112 // This is an internal ID for identifying clients of VideoCaptureImpl.
113 // The ID is global for the render process.
114 int next_client_id_;
116 const scoped_refptr<VideoCaptureMessageFilter> filter_;
118 // Hold a pointer to the Render Main message loop to check we operate on the
119 // right thread.
120 const scoped_refptr<base::MessageLoopProxy> render_main_message_loop_;
122 // Bound to the render thread.
123 // NOTE: Weak pointers must be invalidated before all other member variables.
124 base::WeakPtrFactory<VideoCaptureImplManager> weak_factory_;
126 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager);
129 } // namespace content
131 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_