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 // VideoCaptureDevice is the abstract base class for realizing video capture
6 // device support in Chromium. It provides the interface for OS dependent
8 // The class is created and functions are invoked on a thread owned by
9 // VideoCaptureManager. Capturing is done on other threads, depending on the OS
10 // specific implementation.
12 #ifndef MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_DEVICE_H_
13 #define MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_DEVICE_H_
18 #include "base/logging.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/single_thread_task_runner.h"
22 #include "base/time/time.h"
23 #include "media/base/media_export.h"
24 #include "media/base/video_frame.h"
25 #include "media/video/capture/video_capture_types.h"
29 class MEDIA_EXPORT VideoCaptureDevice
{
31 // Represents a capture device name and ID.
32 // You should not create an instance of this class directly by e.g. setting
33 // various properties directly. Instead use
34 // VideoCaptureDevice::GetDeviceNames to do this for you and if you need to
35 // cache your own copy of a name, you can do so via the copy constructor.
36 // The reason for this is that a device name might contain platform specific
37 // settings that are relevant only to the platform specific implementation of
38 // VideoCaptureDevice::Create.
39 class MEDIA_EXPORT Name
{
42 Name(const std::string
& name
, const std::string
& id
);
45 // Windows targets Capture Api type: it can only be set on construction.
49 DIRECT_SHOW_WDM_CROSSBAR
,
53 #if defined(OS_MACOSX)
54 // Mac targets Capture Api type: it can only be set on construction.
61 // For AVFoundation Api, identify devices that are built-in or USB.
67 #if defined(OS_WIN) || defined(OS_MACOSX)
68 Name(const std::string
& name
,
69 const std::string
& id
,
70 const CaptureApiType api_type
);
72 #if defined(OS_MACOSX)
73 Name(const std::string
& name
,
74 const std::string
& id
,
75 const CaptureApiType api_type
,
76 const TransportType transport_type
);
80 // Friendly name of a device
81 const std::string
& name() const { return device_name_
; }
83 // Unique name of a device. Even if there are multiple devices with the same
84 // friendly name connected to the computer this will be unique.
85 const std::string
& id() const { return unique_id_
; }
87 // The unique hardware model identifier of the capture device. Returns
88 // "[vid]:[pid]" when a USB device is detected, otherwise "".
89 // The implementation of this method is platform-dependent.
90 const std::string
GetModel() const;
92 // Friendly name of a device, plus the model identifier in parentheses.
93 const std::string
GetNameAndModel() const;
95 // These operators are needed due to storing the name in an STL container.
96 // In the shared build, all methods from the STL container will be exported
97 // so even though they're not used, they're still depended upon.
98 bool operator==(const Name
& other
) const {
99 return other
.id() == unique_id_
;
101 bool operator<(const Name
& other
) const {
102 return unique_id_
< other
.id();
105 #if defined(OS_WIN) || defined(OS_MACOSX)
106 CaptureApiType
capture_api_type() const {
107 return capture_api_class_
.capture_api_type();
109 const char* GetCaptureApiTypeString() const;
112 // Certain devices need an ID different from the |unique_id_| for
113 // capabilities retrieval.
114 const std::string
& capabilities_id() const {
115 return capabilities_id_
;
117 void set_capabilities_id(const std::string
& id
) {
118 capabilities_id_
= id
;
121 #if defined(OS_MACOSX)
122 TransportType
transport_type() const {
123 return transport_type_
;
125 bool is_blacklisted() const {
126 return is_blacklisted_
;
128 void set_is_blacklisted(bool is_blacklisted
) {
129 is_blacklisted_
= is_blacklisted
;
131 #endif // if defined(OS_WIN)
134 std::string device_name_
;
135 std::string unique_id_
;
136 #if defined(OS_WIN) || defined(OS_MACOSX)
137 // This class wraps the CaptureApiType to give it a by default value if not
139 class CaptureApiClass
{
141 CaptureApiClass(): capture_api_type_(API_TYPE_UNKNOWN
) {}
142 CaptureApiClass(const CaptureApiType api_type
)
143 : capture_api_type_(api_type
) {}
144 CaptureApiType
capture_api_type() const {
145 DCHECK_NE(capture_api_type_
, API_TYPE_UNKNOWN
);
146 return capture_api_type_
;
149 CaptureApiType capture_api_type_
;
152 CaptureApiClass capture_api_class_
;
155 // ID used for capabilities retrieval. By default is equal to |unique_id|.
156 std::string capabilities_id_
;
158 #if defined(OS_MACOSX)
159 TransportType transport_type_
;
160 // Flag used to mark blacklisted devices for QTKit Api.
161 bool is_blacklisted_
;
163 // Allow generated copy constructor and assignment.
166 // Manages a list of Name entries.
167 typedef std::list
<Name
> Names
;
169 class MEDIA_EXPORT Client
{
171 // Memory buffer returned by Client::ReserveOutputBuffer().
172 class Buffer
: public base::RefCountedThreadSafe
<Buffer
> {
174 int id() const { return id_
; }
175 void* data() const { return data_
; }
176 size_t size() const { return size_
; }
179 friend class base::RefCountedThreadSafe
<Buffer
>;
181 Buffer(int id
, void* data
, size_t size
)
182 : id_(id
), data_(data
), size_(size
) {}
192 // Reserve an output buffer into which contents can be captured directly.
193 // The returned Buffer will always be allocated with a memory size suitable
194 // for holding a packed video frame with pixels of |format| format, of
195 // |dimensions| frame dimensions. It is permissible for |dimensions| to be
196 // zero; in which case the returned Buffer does not guarantee memory
197 // backing, but functions as a reservation for external input for the
198 // purposes of buffer throttling.
200 // The output buffer stays reserved for use until the Buffer object is
202 virtual scoped_refptr
<Buffer
> ReserveOutputBuffer(
203 media::VideoFrame::Format format
,
204 const gfx::Size
& dimensions
) = 0;
206 // Captured a new video frame, data for which is pointed to by |data|.
208 // The format of the frame is described by |frame_format|, and is assumed to
209 // be tightly packed. This method will try to reserve an output buffer and
210 // copy from |data| into the output buffer. If no output buffer is
211 // available, the frame will be silently dropped.
212 virtual void OnIncomingCapturedData(const uint8
* data
,
214 const VideoCaptureFormat
& frame_format
,
215 int rotation
, // Clockwise.
216 base::TimeTicks timestamp
) = 0;
218 // Captured a new video frame, held in |frame|.
220 // As the frame is backed by a reservation returned by
221 // ReserveOutputBuffer(), delivery is guaranteed and will require no
222 // additional copies in the browser process.
223 virtual void OnIncomingCapturedVideoFrame(
224 const scoped_refptr
<Buffer
>& buffer
,
225 const VideoCaptureFormat
& buffer_format
,
226 const scoped_refptr
<media::VideoFrame
>& frame
,
227 base::TimeTicks timestamp
) = 0;
229 // An error has occurred that cannot be handled and VideoCaptureDevice must
230 // be StopAndDeAllocate()-ed. |reason| is a text description of the error.
231 virtual void OnError(const std::string
& reason
) = 0;
233 // VideoCaptureDevice requests the |message| to be logged.
234 virtual void OnLog(const std::string
& message
) {}
236 // The video stream has been muted. After this callback, no more
237 // OnIncomingCapturedData() will be called. This may happen when
238 // CaptureImage() has called. After the still image captured, the client
239 // will get notified by OnUnmute() and the video stream will be resumed.
240 virtual void OnMute() {}
242 // The video stream has resumed.
243 virtual void OnUnmute() {}
246 // Interface for clients that use VideoCaptureDevice for taking still images.
247 class MEDIA_EXPORT ImageClient
{
249 virtual ~ImageClient() {}
251 // Callback function to notify the client a captured image is available.
253 // The captured still image is stored at address |data| and is of |length|
254 // bytes. The format of the frame is described by |format|, and is assumed
255 // to be tightly packed. The still image should be rotated |rotation|
256 // degrees clockwise for viewing.
258 // Note that the content in |data| will not be valid after this callback
259 // returns. Copy the content to use it later.
260 virtual void OnIncomingCapturedData(const uint8
* data
,
262 const ImageCaptureFormat
& format
,
264 base::TimeTicks timestamp
) = 0;
266 // Callback function to notify the client about a failure of the image
267 // capture. The VideoCaptureDevice must be StopAndDeAllocate()-ed.
268 // |reason| contains a text description of the error.
269 virtual void OnError(const std::string
& reason
) = 0;
272 virtual ~VideoCaptureDevice();
274 // Prepares the camera for use. After this function has been called no other
275 // applications can use the camera. StopAndDeAllocate() must be called before
276 // the object is deleted.
277 virtual void AllocateAndStart(const VideoCaptureParams
& params
,
278 scoped_ptr
<Client
> client
) = 0;
280 // Deallocates the camera, possibly asynchronously.
282 // This call requires the device to do the following things, eventually: put
283 // camera hardware into a state where other applications could use it, free
284 // the memory associated with capture, and delete the |client| pointer passed
285 // into AllocateAndStart.
287 // If deallocation is done asynchronously, then the device implementation must
288 // ensure that a subsequent AllocateAndStart() operation targeting the same ID
289 // would be sequenced through the same task runner, so that deallocation
291 virtual void StopAndDeAllocate() = 0;
293 // Gets the power line frequency from the current system time zone if this is
294 // defined, otherwise returns 0.
295 int GetPowerLineFrequencyForLocation() const;
297 // Initializes the device for still image capture for the given image format.
298 // This call is synchronous and returns true iff the initialization is
301 // This function must be called between AllocateAndStart() and
302 // StopAndDeAllocate().
303 virtual bool InitializeImageCapture(const ImageCaptureFormat
& image_format
,
304 scoped_ptr
<ImageClient
> client
);
306 // Releases resources for image capture.
308 // The ImageClient passed from InitializeImageCapture will be freed. This
309 // method must be called between InitializeImageCapture() and
310 // StopAndDeAllocate().
311 virtual void ReleaseImageCapture() {}
313 // Requests one image from the device.
315 // The image will be returned via the ImageClient::OnIncomingCapturedData()
316 // callback. If the video stream has to be stopped to capture the still image,
317 // the Client::OnMute() and Client::OnUnmute() will be called.
319 // This function must be called between InitializeImageCapture() and
320 // ReleaseImageCapture().
321 virtual void CaptureImage() {}
324 static const int kPowerLine50Hz
= 50;
325 static const int kPowerLine60Hz
= 60;
330 #endif // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_DEVICE_H_