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 VideoCaptureHost,
6 // VideoCaptureManager and VideoCaptureDevice.
7 // It provides functions for VideoCaptureHost to start a VideoCaptureDevice and
8 // is responsible for keeping track of shared DIBs and filling them with I420
9 // video frames for IPC communication between VideoCaptureHost and
10 // VideoCaptureMessageFilter.
11 // It implements media::VideoCaptureDevice::EventHandler to get video frames
12 // from a VideoCaptureDevice object and do color conversion straight into the
13 // shared DIBs to avoid a memory copy.
14 // It serves multiple VideoCaptureControllerEventHandlers.
16 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_
17 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_
22 #include "base/compiler_specific.h"
23 #include "base/memory/ref_counted.h"
24 #include "base/process.h"
25 #include "base/synchronization/lock.h"
26 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h"
27 #include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
28 #include "content/common/content_export.h"
29 #include "content/common/media/video_capture.h"
30 #include "media/video/capture/video_capture.h"
31 #include "media/video/capture/video_capture_device.h"
32 #include "media/video/capture/video_capture_types.h"
35 class VideoCaptureManager
;
36 class VideoCaptureBufferPool
;
38 class CONTENT_EXPORT VideoCaptureController
39 : public base::RefCountedThreadSafe
<VideoCaptureController
>,
40 public media::VideoCaptureDevice::EventHandler
{
42 VideoCaptureController(VideoCaptureManager
* video_capture_manager
);
44 // Start video capturing and try to use the resolution specified in
46 // When capturing has started, the |event_handler| receives a call OnFrameInfo
47 // with resolution that best matches the requested that the video
48 // capture device support.
49 void StartCapture(const VideoCaptureControllerID
& id
,
50 VideoCaptureControllerEventHandler
* event_handler
,
51 base::ProcessHandle render_process
,
52 const media::VideoCaptureParams
& params
);
54 // Stop video capture.
55 // This will take back all buffers held by by |event_handler|, and
56 // |event_handler| shouldn't use those buffers any more.
57 void StopCapture(const VideoCaptureControllerID
& id
,
58 VideoCaptureControllerEventHandler
* event_handler
);
60 // API called directly by VideoCaptureManager in case the device is
61 // prematurely closed.
62 void StopSession(int session_id
);
64 // Return a buffer previously given in
65 // VideoCaptureControllerEventHandler::OnBufferReady.
66 void ReturnBuffer(const VideoCaptureControllerID
& id
,
67 VideoCaptureControllerEventHandler
* event_handler
,
70 // Implement media::VideoCaptureDevice::EventHandler.
71 virtual scoped_refptr
<media::VideoFrame
> ReserveOutputBuffer() OVERRIDE
;
72 virtual void OnIncomingCapturedFrame(const uint8
* data
,
77 bool flip_horiz
) OVERRIDE
;
78 virtual void OnIncomingCapturedVideoFrame(
79 const scoped_refptr
<media::VideoFrame
>& frame
,
80 base::Time timestamp
) OVERRIDE
;
81 virtual void OnError() OVERRIDE
;
82 virtual void OnFrameInfo(
83 const media::VideoCaptureCapability
& info
) OVERRIDE
;
86 virtual ~VideoCaptureController();
89 friend class base::RefCountedThreadSafe
<VideoCaptureController
>;
91 struct ControllerClient
;
92 typedef std::list
<ControllerClient
*> ControllerClients
;
94 // Callback when manager has stopped device.
95 void OnDeviceStopped();
97 // Worker functions on IO thread.
98 void DoIncomingCapturedFrameOnIOThread(
99 const scoped_refptr
<media::VideoFrame
>& captured_frame
,
100 base::Time timestamp
);
101 void DoFrameInfoOnIOThread();
102 void DoErrorOnIOThread();
103 void DoDeviceStoppedOnIOThread();
105 // Send frame info and init buffers to |client|.
106 void SendFrameInfoAndBuffers(ControllerClient
* client
);
108 // Find a client of |id| and |handler| in |clients|.
109 ControllerClient
* FindClient(
110 const VideoCaptureControllerID
& id
,
111 VideoCaptureControllerEventHandler
* handler
,
112 const ControllerClients
& clients
);
114 // Find a client of |session_id| in |clients|.
115 ControllerClient
* FindClient(
117 const ControllerClients
& clients
);
119 // Decide what to do after kStopping state. Dependent on events, controller
120 // can stay in kStopping state, or go to kStopped, or restart capture.
123 // Protects access to the |buffer_pool_| pointer on non-IO threads.
124 // TODO(nick): Make it so that this lock isn't required.
125 base::Lock buffer_pool_lock_
;
127 // The pool of shared-memory buffers used for capturing.
128 scoped_refptr
<VideoCaptureBufferPool
> buffer_pool_
;
130 // All clients served by this controller.
131 ControllerClients controller_clients_
;
133 // All clients waiting for service.
134 ControllerClients pending_clients_
;
136 // The parameter that currently used for the capturing.
137 media::VideoCaptureParams current_params_
;
139 // It's modified on caller thread, assuming there is only one OnFrameInfo()
140 // call per StartCapture().
141 media::VideoCaptureCapability frame_info_
;
143 // Chopped pixels in width/height in case video capture device has odd numbers
148 // It's accessed only on IO thread.
149 bool frame_info_available_
;
151 VideoCaptureManager
* video_capture_manager_
;
154 VideoCaptureState state_
;
156 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureController
);
159 } // namespace content
161 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_CONTROLLER_H_