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/files/file.h"
19 #include "base/logging.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/single_thread_task_runner.h"
23 #include "base/time/time.h"
24 #include "media/base/media_export.h"
25 #include "media/base/video_capture_types.h"
26 #include "media/base/video_frame.h"
27 #include "ui/gfx/gpu_memory_buffer.h"
31 class MEDIA_EXPORT VideoCaptureDevice
{
33 // Represents a capture device name and ID.
34 // You should not create an instance of this class directly by e.g. setting
35 // various properties directly. Instead use
36 // VideoCaptureDevice::GetDeviceNames to do this for you and if you need to
37 // cache your own copy of a name, you can do so via the copy constructor.
38 // The reason for this is that a device name might contain platform specific
39 // settings that are relevant only to the platform specific implementation of
40 // VideoCaptureDevice::Create.
41 class MEDIA_EXPORT Name
{
44 Name(const std::string
& name
, const std::string
& id
);
47 // Linux/CrOS targets Capture Api type: it can only be set on construction.
54 // Windows targets Capture Api type: it can only be set on construction.
55 enum CaptureApiType
{ MEDIA_FOUNDATION
, DIRECT_SHOW
, API_TYPE_UNKNOWN
};
56 #elif defined(OS_MACOSX)
57 // Mac targets Capture Api type: it can only be set on construction.
58 enum CaptureApiType
{ AVFOUNDATION
, QTKIT
, DECKLINK
, API_TYPE_UNKNOWN
};
59 // For AVFoundation Api, identify devices that are built-in or USB.
60 enum TransportType
{ USB_OR_BUILT_IN
, OTHER_TRANSPORT
};
61 #elif defined(OS_ANDROID)
62 // Android targets Capture Api type: it can only be set on construction.
63 // Automatically generated enum to interface with Java world.
65 // A Java counterpart will be generated for this enum.
66 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.media
77 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
79 Name(const std::string
& name
,
80 const std::string
& id
,
81 const CaptureApiType api_type
);
83 #if defined(OS_MACOSX)
84 Name(const std::string
& name
,
85 const std::string
& id
,
86 const CaptureApiType api_type
,
87 const TransportType transport_type
);
91 // Friendly name of a device
92 const std::string
& name() const { return device_name_
; }
94 // Unique name of a device. Even if there are multiple devices with the same
95 // friendly name connected to the computer this will be unique.
96 const std::string
& id() const { return unique_id_
; }
98 // The unique hardware model identifier of the capture device. Returns
99 // "[vid]:[pid]" when a USB device is detected, otherwise "".
100 // The implementation of this method is platform-dependent.
101 const std::string
GetModel() const;
103 // Friendly name of a device, plus the model identifier in parentheses.
104 const std::string
GetNameAndModel() const;
106 // These operators are needed due to storing the name in an STL container.
107 // In the shared build, all methods from the STL container will be exported
108 // so even though they're not used, they're still depended upon.
109 bool operator==(const Name
& other
) const {
110 return other
.id() == unique_id_
;
112 bool operator<(const Name
& other
) const { return unique_id_
< other
.id(); }
114 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
116 CaptureApiType
capture_api_type() const {
117 return capture_api_class_
.capture_api_type();
119 const char* GetCaptureApiTypeString() const;
122 // Certain devices need an ID different from the |unique_id_| for
123 // capabilities retrieval.
124 const std::string
& capabilities_id() const { return capabilities_id_
; }
125 void set_capabilities_id(const std::string
& id
) { capabilities_id_
= id
; }
126 #endif // if defined(OS_WIN)
127 #if defined(OS_MACOSX)
128 TransportType
transport_type() const { return transport_type_
; }
129 bool is_blacklisted() const { return is_blacklisted_
; }
130 void set_is_blacklisted(bool is_blacklisted
) {
131 is_blacklisted_
= is_blacklisted
;
133 #endif // if defined(OS_MACOSX)
136 std::string device_name_
;
137 std::string unique_id_
;
138 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
140 // This class wraps the CaptureApiType to give it a by default value if not
142 class CaptureApiClass
{
144 CaptureApiClass() : capture_api_type_(API_TYPE_UNKNOWN
) {}
145 CaptureApiClass(const CaptureApiType api_type
)
146 : capture_api_type_(api_type
) {}
147 CaptureApiType
capture_api_type() const {
148 DCHECK_NE(capture_api_type_
, API_TYPE_UNKNOWN
);
149 return capture_api_type_
;
153 CaptureApiType capture_api_type_
;
156 CaptureApiClass capture_api_class_
;
159 // ID used for capabilities retrieval. By default is equal to |unique_id|.
160 std::string capabilities_id_
;
162 #if defined(OS_MACOSX)
163 TransportType transport_type_
;
164 // Flag used to mark blacklisted devices for QTKit Api.
165 bool is_blacklisted_
;
167 // Allow generated copy constructor and assignment.
170 // Manages a list of Name entries.
171 typedef std::list
<Name
> Names
;
173 // Interface defining the methods that clients of VideoCapture must have. It
174 // is actually two-in-one: clients may implement OnIncomingCapturedData() or
175 // ReserveOutputBuffer() + OnIncomingCapturedVideoFrame(), or all of them.
176 // All clients must implement OnError().
177 class MEDIA_EXPORT Client
{
179 // Memory buffer returned by Client::ReserveOutputBuffer().
180 class MEDIA_EXPORT Buffer
{
182 virtual ~Buffer() = 0;
183 virtual int id() const = 0;
184 virtual size_t size() const = 0;
185 virtual void* data() = 0;
186 virtual ClientBuffer
AsClientBuffer() = 0;
187 #if defined(OS_POSIX)
188 virtual base::FileDescriptor
AsPlatformFile() = 0;
194 // Captured a new video frame, data for which is pointed to by |data|.
196 // The format of the frame is described by |frame_format|, and is assumed to
197 // be tightly packed. This method will try to reserve an output buffer and
198 // copy from |data| into the output buffer. If no output buffer is
199 // available, the frame will be silently dropped.
200 virtual void OnIncomingCapturedData(const uint8
* data
,
202 const VideoCaptureFormat
& frame_format
,
203 int clockwise_rotation
,
204 const base::TimeTicks
& timestamp
) = 0;
206 // Captured a 3 planar YUV frame. Planes are possibly disjoint.
207 // |frame_format| must indicate I420.
208 virtual void OnIncomingCapturedYuvData(
215 const VideoCaptureFormat
& frame_format
,
216 int clockwise_rotation
,
217 const base::TimeTicks
& timestamp
) = 0;
219 // Reserve an output buffer into which contents can be captured directly.
220 // The returned Buffer will always be allocated with a memory size suitable
221 // for holding a packed video frame with pixels of |format| format, of
222 // |dimensions| frame dimensions. It is permissible for |dimensions| to be
223 // zero; in which case the returned Buffer does not guarantee memory
224 // backing, but functions as a reservation for external input for the
225 // purposes of buffer throttling.
227 // The output buffer stays reserved and mapped for use until the Buffer
228 // object is destroyed or returned.
229 virtual scoped_ptr
<Buffer
> ReserveOutputBuffer(
230 const gfx::Size
& dimensions
,
231 VideoCapturePixelFormat format
,
232 VideoPixelStorage storage
) = 0;
234 // Captured new video data, held in |frame| or |buffer|, respectively for
235 // OnIncomingCapturedVideoFrame() and OnIncomingCapturedBuffer().
237 // In both cases, as the frame is backed by a reservation returned by
238 // ReserveOutputBuffer(), delivery is guaranteed and will require no
239 // additional copies in the browser process.
240 virtual void OnIncomingCapturedBuffer(
241 scoped_ptr
<Buffer
> buffer
,
242 const VideoCaptureFormat
& frame_format
,
243 const base::TimeTicks
& timestamp
) = 0;
244 virtual void OnIncomingCapturedVideoFrame(
245 scoped_ptr
<Buffer
> buffer
,
246 const scoped_refptr
<VideoFrame
>& frame
,
247 const base::TimeTicks
& timestamp
) = 0;
249 // An error has occurred that cannot be handled and VideoCaptureDevice must
250 // be StopAndDeAllocate()-ed. |reason| is a text description of the error.
251 virtual void OnError(const std::string
& reason
) = 0;
253 // VideoCaptureDevice requests the |message| to be logged.
254 virtual void OnLog(const std::string
& message
) {}
256 // Returns the current buffer pool utilization, in the range 0.0 (no buffers
257 // are in use by producers or consumers) to 1.0 (all buffers are in use).
258 virtual double GetBufferPoolUtilization() const = 0;
261 virtual ~VideoCaptureDevice();
263 // Prepares the camera for use. After this function has been called no other
264 // applications can use the camera. StopAndDeAllocate() must be called before
265 // the object is deleted.
266 virtual void AllocateAndStart(const VideoCaptureParams
& params
,
267 scoped_ptr
<Client
> client
) = 0;
269 // Deallocates the camera, possibly asynchronously.
271 // This call requires the device to do the following things, eventually: put
272 // camera hardware into a state where other applications could use it, free
273 // the memory associated with capture, and delete the |client| pointer passed
274 // into AllocateAndStart.
276 // If deallocation is done asynchronously, then the device implementation must
277 // ensure that a subsequent AllocateAndStart() operation targeting the same ID
278 // would be sequenced through the same task runner, so that deallocation
280 virtual void StopAndDeAllocate() = 0;
282 // Gets the power line frequency from the current system time zone if this is
283 // defined, otherwise returns 0.
284 int GetPowerLineFrequencyForLocation() const;
287 static const int kPowerLine50Hz
= 50;
288 static const int kPowerLine60Hz
= 60;
293 #endif // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_DEVICE_H_