[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_controller.h
blob2954688bd8d8fa074949d27c97f076d8aa74e997
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 // VideoCaptureController is the glue between a VideoCaptureDevice and all
6 // VideoCaptureHosts that have connected to it. A controller exists on behalf of
7 // one (and only one) VideoCaptureDevice; both are owned by the
8 // VideoCaptureManager.
9 //
10 // The VideoCaptureController is responsible for:
12 // * Allocating and keeping track of shared memory buffers, and filling them
13 // with I420 video frames for IPC communication between VideoCaptureHost (in
14 // the browser process) and VideoCaptureMessageFilter (in the renderer
15 // process).
16 // * Broadcasting the events from a single VideoCaptureDevice, fanning them
17 // out to multiple clients.
18 // * Keeping track of the clients on behalf of the VideoCaptureManager, making
19 // it possible for the Manager to delete the Controller and its Device when
20 // there are no clients left.
22 // Interactions between VideoCaptureController and other classes:
24 // * VideoCaptureController indirectly observes a VideoCaptureDevice
25 // by means of its proxy, VideoCaptureDeviceClient, which implements
26 // the VideoCaptureDevice::Client interface. The proxy forwards
27 // observed events to the VideoCaptureController on the IO thread.
28 // * A VideoCaptureController interacts with its clients (VideoCaptureHosts)
29 // via the VideoCaptureControllerEventHandler interface.
30 // * Conversely, a VideoCaptureControllerEventHandler (typically,
31 // VideoCaptureHost) will interact directly with VideoCaptureController to
32 // return leased buffers by means of the ReturnBuffer() public method of
33 // VCC.
34 // * VideoCaptureManager (which owns the VCC) interacts directly with
35 // VideoCaptureController through its public methods, to add and remove
36 // clients.
38 // VideoCaptureController is not thread safe and operates on the IO thread only.
40 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_
41 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_
43 #include <list>
45 #include "base/compiler_specific.h"
46 #include "base/memory/ref_counted.h"
47 #include "base/memory/scoped_ptr.h"
48 #include "base/memory/weak_ptr.h"
49 #include "base/process/process.h"
50 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
51 #include "content/common/content_export.h"
52 #include "content/common/media/video_capture.h"
53 #include "media/base/video_capture_types.h"
54 #include "media/video/capture/video_capture_device.h"
56 namespace content {
57 class VideoCaptureBufferPool;
59 class CONTENT_EXPORT VideoCaptureController {
60 public:
61 // |max_buffers| is the maximum number of video frame buffers in-flight at any
62 // one time. This value should be based on the logical capacity of the
63 // capture pipeline, and not on hardware performance. For example, tab
64 // capture requires more buffers than webcam capture because the pipeline is
65 // longer (it includes read-backs pending in the GPU pipeline).
66 explicit VideoCaptureController(int max_buffers);
67 virtual ~VideoCaptureController();
69 base::WeakPtr<VideoCaptureController> GetWeakPtrForIOThread();
71 // Return a new VideoCaptureDeviceClient to forward capture events to this
72 // instance. Some device clients need to allocate resources for the given
73 // capture |format| and/or work on Capture Thread (|capture_task_runner|).
74 scoped_ptr<media::VideoCaptureDevice::Client> NewDeviceClient(
75 const scoped_refptr<base::SingleThreadTaskRunner>& capture_task_runner,
76 const media::VideoCaptureFormat& format);
78 // Start video capturing and try to use the resolution specified in |params|.
79 // Buffers will be shared to the client as necessary. The client will continue
80 // to receive frames from the device until RemoveClient() is called.
81 void AddClient(VideoCaptureControllerID id,
82 VideoCaptureControllerEventHandler* event_handler,
83 base::ProcessHandle render_process,
84 media::VideoCaptureSessionId session_id,
85 const media::VideoCaptureParams& params);
87 // Stop video capture. This will take back all buffers held by by
88 // |event_handler|, and |event_handler| shouldn't use those buffers any more.
89 // Returns the session_id of the stopped client, or
90 // kInvalidMediaCaptureSessionId if the indicated client was not registered.
91 int RemoveClient(VideoCaptureControllerID id,
92 VideoCaptureControllerEventHandler* event_handler);
94 // Pause or resume the video capture for specified client.
95 void PauseOrResumeClient(VideoCaptureControllerID id,
96 VideoCaptureControllerEventHandler* event_handler,
97 bool pause);
99 int GetClientCount() const;
101 // Return the number of clients that aren't paused.
102 int GetActiveClientCount() const;
104 // API called directly by VideoCaptureManager in case the device is
105 // prematurely closed.
106 void StopSession(int session_id);
108 // Return a buffer with id |buffer_id| previously given in
109 // VideoCaptureControllerEventHandler::OnBufferReady. In the case that the
110 // buffer was backed by a texture, |sync_point| will be waited on before
111 // destroying or recycling the texture, to synchronize with texture users in
112 // the renderer process.
113 void ReturnBuffer(VideoCaptureControllerID id,
114 VideoCaptureControllerEventHandler* event_handler,
115 int buffer_id,
116 uint32 sync_point);
118 const media::VideoCaptureFormat& GetVideoCaptureFormat() const;
120 bool has_received_frames() const { return has_received_frames_; }
122 // Worker functions on IO thread. Called by the VideoCaptureDeviceClient.
123 void DoIncomingCapturedVideoFrameOnIOThread(
124 const scoped_refptr<media::VideoCaptureDevice::Client::Buffer>& buffer,
125 const scoped_refptr<media::VideoFrame>& frame,
126 const base::TimeTicks& timestamp);
127 void DoErrorOnIOThread();
128 void DoLogOnIOThread(const std::string& message);
129 void DoBufferDestroyedOnIOThread(int buffer_id_to_drop);
131 private:
132 struct ControllerClient;
133 typedef std::list<ControllerClient*> ControllerClients;
135 // Find a client of |id| and |handler| in |clients|.
136 ControllerClient* FindClient(VideoCaptureControllerID id,
137 VideoCaptureControllerEventHandler* handler,
138 const ControllerClients& clients);
140 // Find a client of |session_id| in |clients|.
141 ControllerClient* FindClient(int session_id,
142 const ControllerClients& clients);
144 // The pool of shared-memory buffers used for capturing.
145 const scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
147 // All clients served by this controller.
148 ControllerClients controller_clients_;
150 // Takes on only the states 'STARTED' and 'ERROR'. 'ERROR' is an absorbing
151 // state which stops the flow of data to clients.
152 VideoCaptureState state_;
154 // True if the controller has received a video frame from the device.
155 bool has_received_frames_;
157 media::VideoCaptureFormat video_capture_format_;
159 base::WeakPtrFactory<VideoCaptureController> weak_ptr_factory_;
161 DISALLOW_COPY_AND_ASSIGN(VideoCaptureController);
164 } // namespace content
166 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_