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 // 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.
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
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
34 // * VideoCaptureManager (which owns the VCC) interacts directly with
35 // VideoCaptureController through its public methods, to add and remove
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_
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/capture/video/video_capture_device.h"
57 class VideoCaptureBufferPool
;
59 class CONTENT_EXPORT VideoCaptureController
{
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
);
77 // Start video capturing and try to use the resolution specified in |params|.
78 // Buffers will be shared to the client as necessary. The client will continue
79 // to receive frames from the device until RemoveClient() is called.
80 void AddClient(VideoCaptureControllerID id
,
81 VideoCaptureControllerEventHandler
* event_handler
,
82 base::ProcessHandle render_process
,
83 media::VideoCaptureSessionId session_id
,
84 const media::VideoCaptureParams
& params
);
86 // Stop video capture. This will take back all buffers held by by
87 // |event_handler|, and |event_handler| shouldn't use those buffers any more.
88 // Returns the session_id of the stopped client, or
89 // kInvalidMediaCaptureSessionId if the indicated client was not registered.
90 int RemoveClient(VideoCaptureControllerID id
,
91 VideoCaptureControllerEventHandler
* event_handler
);
93 // Pause or resume the video capture for specified client.
94 void PauseOrResumeClient(VideoCaptureControllerID id
,
95 VideoCaptureControllerEventHandler
* event_handler
,
98 int GetClientCount() const;
100 // Return the number of clients that aren't paused.
101 int GetActiveClientCount() const;
103 // API called directly by VideoCaptureManager in case the device is
104 // prematurely closed.
105 void StopSession(int session_id
);
107 // Return a buffer with id |buffer_id| previously given in
108 // VideoCaptureControllerEventHandler::OnBufferReady. In the case that the
109 // buffer was backed by a texture, |sync_point| will be waited on before
110 // destroying or recycling the texture, to synchronize with texture users in
111 // the renderer process. If the consumer provided resource utilization
112 // feedback, this will be passed here (-1.0 indicates no feedback).
113 void ReturnBuffer(VideoCaptureControllerID id
,
114 VideoCaptureControllerEventHandler
* event_handler
,
117 double consumer_resource_utilization
);
119 const media::VideoCaptureFormat
& GetVideoCaptureFormat() const;
121 bool has_received_frames() const { return has_received_frames_
; }
123 // Worker functions on IO thread. Called by the VideoCaptureDeviceClient.
124 virtual void DoIncomingCapturedVideoFrameOnIOThread(
125 scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
> buffer
,
126 const scoped_refptr
<media::VideoFrame
>& frame
,
127 const base::TimeTicks
& timestamp
);
128 virtual void DoErrorOnIOThread();
129 virtual void DoLogOnIOThread(const std::string
& message
);
130 virtual void DoBufferDestroyedOnIOThread(int buffer_id_to_drop
);
133 struct ControllerClient
;
134 typedef std::list
<ControllerClient
*> ControllerClients
;
136 // Find a client of |id| and |handler| in |clients|.
137 ControllerClient
* FindClient(VideoCaptureControllerID id
,
138 VideoCaptureControllerEventHandler
* handler
,
139 const ControllerClients
& clients
);
141 // Find a client of |session_id| in |clients|.
142 ControllerClient
* FindClient(int session_id
,
143 const ControllerClients
& clients
);
145 // The pool of shared-memory buffers used for capturing.
146 const scoped_refptr
<VideoCaptureBufferPool
> buffer_pool_
;
148 // All clients served by this controller.
149 ControllerClients controller_clients_
;
151 // Takes on only the states 'STARTED' and 'ERROR'. 'ERROR' is an absorbing
152 // state which stops the flow of data to clients.
153 VideoCaptureState state_
;
155 // True if the controller has received a video frame from the device.
156 bool has_received_frames_
;
158 media::VideoCaptureFormat video_capture_format_
;
160 base::WeakPtrFactory
<VideoCaptureController
> weak_ptr_factory_
;
162 DISALLOW_COPY_AND_ASSIGN(VideoCaptureController
);
165 } // namespace content
167 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_