Add/resurrect support for bundles of WebStore items.
[chromium-blink-merge.git] / media / video / capture / video_capture_device.h
blobfe7756f738ac743a3e7f5511e32e9e4042657370
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.
4 //
5 // VideoCaptureDevice is the abstract base class for realizing video capture
6 // device support in Chromium. It provides the interface for OS dependent
7 // implementations.
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_
15 #include <list>
16 #include <string>
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"
27 namespace media {
29 class MEDIA_EXPORT VideoCaptureDevice {
30 public:
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 {
40 public:
41 Name();
42 Name(const std::string& name, const std::string& id);
44 #if defined(OS_LINUX)
45 // Linux/CrOS targets Capture Api type: it can only be set on construction.
46 enum CaptureApiType {
47 V4L2_SINGLE_PLANE,
48 V4L2_MULTI_PLANE,
49 API_TYPE_UNKNOWN
51 #elif defined(OS_WIN)
52 // Windows targets Capture Api type: it can only be set on construction.
53 enum CaptureApiType {
54 MEDIA_FOUNDATION,
55 DIRECT_SHOW,
56 DIRECT_SHOW_WDM_CROSSBAR,
57 API_TYPE_UNKNOWN
59 #elif defined(OS_MACOSX)
60 // Mac targets Capture Api type: it can only be set on construction.
61 enum CaptureApiType {
62 AVFOUNDATION,
63 QTKIT,
64 DECKLINK,
65 API_TYPE_UNKNOWN
67 // For AVFoundation Api, identify devices that are built-in or USB.
68 enum TransportType {
69 USB_OR_BUILT_IN,
70 OTHER_TRANSPORT
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
78 enum CaptureApiType {
79 API1,
80 API2_LEGACY,
81 API2_FULL,
82 API2_LIMITED,
83 TANGO,
84 API_TYPE_UNKNOWN
86 #endif
88 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
89 defined(OS_ANDROID)
90 Name(const std::string& name, const std::string& id,
91 const CaptureApiType api_type);
92 #endif
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);
98 #endif
99 ~Name();
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) || \
127 defined(OS_ANDROID)
128 CaptureApiType capture_api_type() const {
129 return capture_api_class_.capture_api_type();
131 const char* GetCaptureApiTypeString() const;
132 #endif
133 #if defined(OS_WIN)
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)
155 private:
156 std::string device_name_;
157 std::string unique_id_;
158 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
159 defined(OS_ANDROID)
160 // This class wraps the CaptureApiType to give it a by default value if not
161 // initialized.
162 class CaptureApiClass {
163 public:
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_;
171 private:
172 CaptureApiType capture_api_type_;
175 CaptureApiClass capture_api_class_;
176 #endif
177 #if defined(OS_WIN)
178 // ID used for capabilities retrieval. By default is equal to |unique_id|.
179 std::string capabilities_id_;
180 #endif
181 #if defined(OS_MACOSX)
182 TransportType transport_type_;
183 // Flag used to mark blacklisted devices for QTKit Api.
184 bool is_blacklisted_;
185 #endif
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 {
197 public:
198 // Memory buffer returned by Client::ReserveOutputBuffer().
199 class Buffer : public base::RefCountedThreadSafe<Buffer> {
200 public:
201 virtual int id() const = 0;
202 virtual void* data() const = 0;
203 virtual size_t size() const = 0;
205 protected:
206 friend class base::RefCountedThreadSafe<Buffer>;
207 virtual ~Buffer() {}
210 virtual ~Client() {}
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,
219 int length,
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(
227 const uint8* y_data,
228 const uint8* u_data,
229 const uint8* v_data,
230 size_t y_stride,
231 size_t u_stride,
232 size_t v_stride,
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
246 // destroyed.
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
287 // happens first.
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;
294 protected:
295 static const int kPowerLine50Hz = 50;
296 static const int kPowerLine60Hz = 60;
299 } // namespace media
301 #endif // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_DEVICE_H_