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 // A helper class, VCC::VideoCaptureDeviceClient, is responsible for:
24 // * Conveying events from the device thread (where VideoCaptureDevices live)
25 // the IO thread (where the VideoCaptureController lives).
26 // * Performing some image transformations on the output of the Device;
27 // specifically, colorspace conversion and rotation.
29 // Interactions between VideoCaptureController and other classes:
31 // * VideoCaptureController indirectly observes a VideoCaptureDevice
32 // by means of its proxy, VideoCaptureDeviceClient, which implements
33 // the VideoCaptureDevice::Client interface. The proxy forwards
34 // observed events to the VideoCaptureController on the IO thread.
35 // * A VideoCaptureController interacts with its clients (VideoCaptureHosts)
36 // via the VideoCaptureControllerEventHandler interface.
37 // * Conversely, a VideoCaptureControllerEventHandler (typically,
38 // VideoCaptureHost) will interact directly with VideoCaptureController to
39 // return leased buffers by means of the ReturnBuffer() public method of
41 // * VideoCaptureManager (which owns the VCC) interacts directly with
42 // VideoCaptureController through its public methods, to add and remove
45 // VideoCaptureController is not thread safe and operates on the IO thread only.
47 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_
48 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_
52 #include "base/compiler_specific.h"
53 #include "base/memory/ref_counted.h"
54 #include "base/memory/scoped_ptr.h"
55 #include "base/memory/weak_ptr.h"
56 #include "base/process/process.h"
57 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h"
58 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
59 #include "content/common/content_export.h"
60 #include "content/common/media/video_capture.h"
61 #include "media/video/capture/video_capture.h"
62 #include "media/video/capture/video_capture_device.h"
63 #include "media/video/capture/video_capture_types.h"
66 class VideoCaptureBufferPool
;
68 class CONTENT_EXPORT VideoCaptureController
{
70 VideoCaptureController();
71 virtual ~VideoCaptureController();
73 base::WeakPtr
<VideoCaptureController
> GetWeakPtr();
75 // Return a new VideoCaptureDeviceClient to forward capture events to this
77 scoped_ptr
<media::VideoCaptureDevice::Client
> NewDeviceClient();
79 // Start video capturing and try to use the resolution specified in |params|.
80 // Buffers will be shared to the client as necessary. The client will continue
81 // to receive frames from the device until RemoveClient() is called.
82 void AddClient(const VideoCaptureControllerID
& id
,
83 VideoCaptureControllerEventHandler
* event_handler
,
84 base::ProcessHandle render_process
,
85 media::VideoCaptureSessionId session_id
,
86 const media::VideoCaptureParams
& params
);
88 // Stop video capture. This will take back all buffers held by by
89 // |event_handler|, and |event_handler| shouldn't use those buffers any more.
90 // Returns the session_id of the stopped client, or
91 // kInvalidMediaCaptureSessionId if the indicated client was not registered.
92 int RemoveClient(const VideoCaptureControllerID
& id
,
93 VideoCaptureControllerEventHandler
* event_handler
);
97 // API called directly by VideoCaptureManager in case the device is
98 // prematurely closed.
99 void StopSession(int session_id
);
101 // Return a buffer previously given in
102 // VideoCaptureControllerEventHandler::OnBufferReady.
103 void ReturnBuffer(const VideoCaptureControllerID
& id
,
104 VideoCaptureControllerEventHandler
* event_handler
,
107 const media::VideoCaptureFormat
& GetVideoCaptureFormat() const;
110 class VideoCaptureDeviceClient
;
112 struct ControllerClient
;
113 typedef std::list
<ControllerClient
*> ControllerClients
;
115 // Worker functions on IO thread. Called by the VideoCaptureDeviceClient.
116 void DoIncomingCapturedI420BufferOnIOThread(
117 scoped_refptr
<media::VideoCaptureDevice::Client::Buffer
> buffer
,
118 const gfx::Size
& dimensions
,
120 base::TimeTicks timestamp
);
121 void DoErrorOnIOThread();
122 void DoDeviceStoppedOnIOThread();
123 void DoBufferDestroyedOnIOThread(int buffer_id_to_drop
);
125 // Find a client of |id| and |handler| in |clients|.
126 ControllerClient
* FindClient(
127 const VideoCaptureControllerID
& id
,
128 VideoCaptureControllerEventHandler
* handler
,
129 const ControllerClients
& clients
);
131 // Find a client of |session_id| in |clients|.
132 ControllerClient
* FindClient(
134 const ControllerClients
& clients
);
136 // The pool of shared-memory buffers used for capturing.
137 const scoped_refptr
<VideoCaptureBufferPool
> buffer_pool_
;
139 // All clients served by this controller.
140 ControllerClients controller_clients_
;
142 // Takes on only the states 'STARTED' and 'ERROR'. 'ERROR' is an absorbing
143 // state which stops the flow of data to clients.
144 VideoCaptureState state_
;
146 media::VideoCaptureFormat video_capture_format_
;
148 base::WeakPtrFactory
<VideoCaptureController
> weak_ptr_factory_
;
150 DISALLOW_COPY_AND_ASSIGN(VideoCaptureController
);
153 } // namespace content
155 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_