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_capture_types.h"
25 #include "media/base/video_frame.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 // Linux/CrOS targets Capture Api type: it can only be set on construction.
52 // Windows targets Capture Api type: it can only be set on construction.
56 DIRECT_SHOW_WDM_CROSSBAR
,
59 #elif defined(OS_MACOSX)
60 // Mac targets Capture Api type: it can only be set on construction.
67 // For AVFoundation Api, identify devices that are built-in or USB.
72 #elif defined (OS_ANDROID)
73 // Android targets Capture Api type: it can only be set on construction.
74 // Automatically generated enum to interface with Java world.
76 // A Java counterpart will be generated for this enum.
77 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.media
88 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
90 Name(const std::string
& name
, const std::string
& id
,
91 const CaptureApiType api_type
);
93 #if defined(OS_MACOSX)
94 Name(const std::string
& name
,
95 const std::string
& id
,
96 const CaptureApiType api_type
,
97 const TransportType transport_type
);
101 // Friendly name of a device
102 const std::string
& name() const { return device_name_
; }
104 // Unique name of a device. Even if there are multiple devices with the same
105 // friendly name connected to the computer this will be unique.
106 const std::string
& id() const { return unique_id_
; }
108 // The unique hardware model identifier of the capture device. Returns
109 // "[vid]:[pid]" when a USB device is detected, otherwise "".
110 // The implementation of this method is platform-dependent.
111 const std::string
GetModel() const;
113 // Friendly name of a device, plus the model identifier in parentheses.
114 const std::string
GetNameAndModel() const;
116 // These operators are needed due to storing the name in an STL container.
117 // In the shared build, all methods from the STL container will be exported
118 // so even though they're not used, they're still depended upon.
119 bool operator==(const Name
& other
) const {
120 return other
.id() == unique_id_
;
122 bool operator<(const Name
& other
) const {
123 return unique_id_
< other
.id();
126 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
128 CaptureApiType
capture_api_type() const {
129 return capture_api_class_
.capture_api_type();
131 const char* GetCaptureApiTypeString() const;
134 // Certain devices need an ID different from the |unique_id_| for
135 // capabilities retrieval.
136 const std::string
& capabilities_id() const {
137 return capabilities_id_
;
139 void set_capabilities_id(const std::string
& id
) {
140 capabilities_id_
= id
;
142 #endif // if defined(OS_WIN)
143 #if defined(OS_MACOSX)
144 TransportType
transport_type() const {
145 return transport_type_
;
147 bool is_blacklisted() const {
148 return is_blacklisted_
;
150 void set_is_blacklisted(bool is_blacklisted
) {
151 is_blacklisted_
= is_blacklisted
;
153 #endif // if defined(OS_MACOSX)
156 std::string device_name_
;
157 std::string unique_id_
;
158 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
160 // This class wraps the CaptureApiType to give it a by default value if not
162 class CaptureApiClass
{
164 CaptureApiClass(): capture_api_type_(API_TYPE_UNKNOWN
) {}
165 CaptureApiClass(const CaptureApiType api_type
)
166 : capture_api_type_(api_type
) {}
167 CaptureApiType
capture_api_type() const {
168 DCHECK_NE(capture_api_type_
, API_TYPE_UNKNOWN
);
169 return capture_api_type_
;
172 CaptureApiType capture_api_type_
;
175 CaptureApiClass capture_api_class_
;
178 // ID used for capabilities retrieval. By default is equal to |unique_id|.
179 std::string capabilities_id_
;
181 #if defined(OS_MACOSX)
182 TransportType transport_type_
;
183 // Flag used to mark blacklisted devices for QTKit Api.
184 bool is_blacklisted_
;
186 // Allow generated copy constructor and assignment.
189 // Manages a list of Name entries.
190 typedef std::list
<Name
> Names
;
192 // Interface defining the methods that clients of VideoCapture must have. It
193 // is actually two-in-one: clients may implement OnIncomingCapturedData() or
194 // ReserveOutputBuffer() + OnIncomingCapturedVideoFrame(), or all of them.
195 // All clients must implement OnError().
196 class MEDIA_EXPORT Client
{
198 // Memory buffer returned by Client::ReserveOutputBuffer().
199 class Buffer
: public base::RefCountedThreadSafe
<Buffer
> {
201 virtual int id() const = 0;
202 virtual void* data() const = 0;
203 virtual size_t size() const = 0;
206 friend class base::RefCountedThreadSafe
<Buffer
>;
212 // Captured a new video frame, data for which is pointed to by |data|.
214 // The format of the frame is described by |frame_format|, and is assumed to
215 // be tightly packed. This method will try to reserve an output buffer and
216 // copy from |data| into the output buffer. If no output buffer is
217 // available, the frame will be silently dropped.
218 virtual void OnIncomingCapturedData(const uint8
* data
,
220 const VideoCaptureFormat
& frame_format
,
221 int clockwise_rotation
,
222 const base::TimeTicks
& timestamp
) = 0;
224 // Captured a 3 planar YUV frame. Planes are possibly disjoint.
225 // |frame_format| must indicate I420.
226 virtual void OnIncomingCapturedYuvData(
233 const VideoCaptureFormat
& frame_format
,
234 int clockwise_rotation
,
235 const base::TimeTicks
& timestamp
) = 0;
237 // Reserve an output buffer into which contents can be captured directly.
238 // The returned Buffer will always be allocated with a memory size suitable
239 // for holding a packed video frame with pixels of |format| format, of
240 // |dimensions| frame dimensions. It is permissible for |dimensions| to be
241 // zero; in which case the returned Buffer does not guarantee memory
242 // backing, but functions as a reservation for external input for the
243 // purposes of buffer throttling.
245 // The output buffer stays reserved for use until the Buffer object is
247 virtual scoped_refptr
<Buffer
> ReserveOutputBuffer(
248 media::VideoFrame::Format format
,
249 const gfx::Size
& dimensions
) = 0;
251 // Captured a new video frame, held in |frame|.
253 // As the frame is backed by a reservation returned by
254 // ReserveOutputBuffer(), delivery is guaranteed and will require no
255 // additional copies in the browser process.
256 virtual void OnIncomingCapturedVideoFrame(
257 const scoped_refptr
<Buffer
>& buffer
,
258 const scoped_refptr
<media::VideoFrame
>& frame
,
259 const base::TimeTicks
& timestamp
) = 0;
261 // An error has occurred that cannot be handled and VideoCaptureDevice must
262 // be StopAndDeAllocate()-ed. |reason| is a text description of the error.
263 virtual void OnError(const std::string
& reason
) = 0;
265 // VideoCaptureDevice requests the |message| to be logged.
266 virtual void OnLog(const std::string
& message
) {}
269 virtual ~VideoCaptureDevice();
271 // Prepares the camera for use. After this function has been called no other
272 // applications can use the camera. StopAndDeAllocate() must be called before
273 // the object is deleted.
274 virtual void AllocateAndStart(const VideoCaptureParams
& params
,
275 scoped_ptr
<Client
> client
) = 0;
277 // Deallocates the camera, possibly asynchronously.
279 // This call requires the device to do the following things, eventually: put
280 // camera hardware into a state where other applications could use it, free
281 // the memory associated with capture, and delete the |client| pointer passed
282 // into AllocateAndStart.
284 // If deallocation is done asynchronously, then the device implementation must
285 // ensure that a subsequent AllocateAndStart() operation targeting the same ID
286 // would be sequenced through the same task runner, so that deallocation
288 virtual void StopAndDeAllocate() = 0;
290 // Gets the power line frequency from the current system time zone if this is
291 // defined, otherwise returns 0.
292 int GetPowerLineFrequencyForLocation() const;
295 static const int kPowerLine50Hz
= 50;
296 static const int kPowerLine60Hz
= 60;
301 #endif // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_DEVICE_H_