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.
52 #if defined(OS_MACOSX)
53 // Mac targets Capture Api type: it can only be set on construction.
59 // For AVFoundation Api, identify devices that are built-in or USB.
65 #if defined(OS_WIN) || defined(OS_MACOSX)
66 Name(const std::string& name,
67 const std::string& id,
68 const CaptureApiType api_type);
70 #if defined(OS_MACOSX)
71 Name(const std::string& name,
72 const std::string& id,
73 const CaptureApiType api_type,
74 const TransportType transport_type);
78 // Friendly name of a device
79 const std::string& name() const { return device_name_; }
81 // Unique name of a device. Even if there are multiple devices with the same
82 // friendly name connected to the computer this will be unique.
83 const std::string& id() const { return unique_id_; }
85 // The unique hardware model identifier of the capture device. Returns
86 // "[vid]:[pid]" when a USB device is detected, otherwise "".
87 // The implementation of this method is platform-dependent.
88 const std::string GetModel() const;
90 // Friendly name of a device, plus the model identifier in parentheses.
91 const std::string GetNameAndModel() const;
93 // These operators are needed due to storing the name in an STL container.
94 // In the shared build, all methods from the STL container will be exported
95 // so even though they're not used, they're still depended upon.
96 bool operator==(const Name& other) const {
97 return other.id() == unique_id_;
99 bool operator<(const Name& other) const {
100 return unique_id_ < other.id();
103 #if defined(OS_WIN) || defined(OS_MACOSX)
104 CaptureApiType capture_api_type() const {
105 return capture_api_class_.capture_api_type();
108 #if defined(OS_MACOSX)
109 TransportType transport_type() const {
110 return transport_type_;
112 bool is_blacklisted() const {
113 return is_blacklisted_;
115 void set_is_blacklisted(bool is_blacklisted) {
116 is_blacklisted_ = is_blacklisted;
118 #endif // if defined(OS_WIN)
121 std::string device_name_;
122 std::string unique_id_;
123 #if defined(OS_WIN) || defined(OS_MACOSX)
124 // This class wraps the CaptureApiType to give it a by default value if not
126 class CaptureApiClass {
128 CaptureApiClass(): capture_api_type_(API_TYPE_UNKNOWN) {}
129 CaptureApiClass(const CaptureApiType api_type)
130 : capture_api_type_(api_type) {}
131 CaptureApiType capture_api_type() const {
132 DCHECK_NE(capture_api_type_, API_TYPE_UNKNOWN);
133 return capture_api_type_;
136 CaptureApiType capture_api_type_;
139 CaptureApiClass capture_api_class_;
141 #if defined(OS_MACOSX)
142 TransportType transport_type_;
143 // Flag used to mark blacklisted devices for QTKit Api.
144 bool is_blacklisted_;
146 // Allow generated copy constructor and assignment.
149 // Manages a list of Name entries.
150 typedef std::list<Name> Names;
152 class MEDIA_EXPORT Client {
154 // Memory buffer returned by Client::ReserveOutputBuffer().
155 class Buffer : public base::RefCountedThreadSafe<Buffer> {
157 int id() const { return id_; }
158 void* data() const { return data_; }
159 size_t size() const { return size_; }
162 friend class base::RefCountedThreadSafe<Buffer>;
164 Buffer(int id, void* data, size_t size)
165 : id_(id), data_(data), size_(size) {}
175 // Reserve an output buffer into which contents can be captured directly.
176 // The returned Buffer will always be allocated with a memory size suitable
177 // for holding a packed video frame with pixels of |format| format, of
178 // |dimensions| frame dimensions. It is permissible for |dimensions| to be
179 // zero; in which case the returned Buffer does not guarantee memory
180 // backing, but functions as a reservation for external input for the
181 // purposes of buffer throttling.
183 // The output buffer stays reserved for use until the Buffer object is
185 virtual scoped_refptr<Buffer> ReserveOutputBuffer(
186 media::VideoFrame::Format format,
187 const gfx::Size& dimensions) = 0;
189 // Captured a new video frame, data for which is pointed to by |data|.
191 // The format of the frame is described by |frame_format|, and is assumed to
192 // be tightly packed. This method will try to reserve an output buffer and
193 // copy from |data| into the output buffer. If no output buffer is
194 // available, the frame will be silently dropped.
195 virtual void OnIncomingCapturedData(const uint8* data,
197 const VideoCaptureFormat& frame_format,
198 int rotation, // Clockwise.
199 base::TimeTicks timestamp) = 0;
201 // Captured a new video frame, held in |frame|.
203 // As the frame is backed by a reservation returned by
204 // ReserveOutputBuffer(), delivery is guaranteed and will require no
205 // additional copies in the browser process.
206 virtual void OnIncomingCapturedVideoFrame(
207 const scoped_refptr<Buffer>& buffer,
208 const VideoCaptureFormat& buffer_format,
209 const scoped_refptr<media::VideoFrame>& frame,
210 base::TimeTicks timestamp) = 0;
212 // An error has occurred that cannot be handled and VideoCaptureDevice must
213 // be StopAndDeAllocate()-ed. |reason| is a text description of the error.
214 virtual void OnError(const std::string& reason) = 0;
216 // VideoCaptureDevice requests the |message| to be logged.
217 virtual void OnLog(const std::string& message) {}
220 // Creates a VideoCaptureDevice object.
221 // Return NULL if the hardware is not available.
222 static VideoCaptureDevice* Create(
223 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
224 const Name& device_name);
225 virtual ~VideoCaptureDevice();
227 // Gets the names of all video capture devices connected to this computer.
228 static void GetDeviceNames(Names* device_names);
230 // Gets the supported formats of a particular device attached to the system.
231 // This method should be called before allocating or starting a device. In
232 // case format enumeration is not supported, or there was a problem, the
233 // formats array will be empty.
234 static void GetDeviceSupportedFormats(const Name& device,
235 VideoCaptureFormats* supported_formats);
237 // Prepares the camera for use. After this function has been called no other
238 // applications can use the camera. StopAndDeAllocate() must be called before
239 // the object is deleted.
240 virtual void AllocateAndStart(const VideoCaptureParams& params,
241 scoped_ptr<Client> client) = 0;
243 // Deallocates the camera, possibly asynchronously.
245 // This call requires the device to do the following things, eventually: put
246 // camera hardware into a state where other applications could use it, free
247 // the memory associated with capture, and delete the |client| pointer passed
248 // into AllocateAndStart.
250 // If deallocation is done asynchronously, then the device implementation must
251 // ensure that a subsequent AllocateAndStart() operation targeting the same ID
252 // would be sequenced through the same task runner, so that deallocation
254 virtual void StopAndDeAllocate() = 0;
256 // Gets the power line frequency from the current system time zone if this is
257 // defined, otherwise returns 0.
258 int GetPowerLineFrequencyForLocation() const;
261 static const int kPowerLine50Hz = 50;
262 static const int kPowerLine60Hz = 60;
267 #endif // MEDIA_VIDEO_CAPTURE_VIDEO_CAPTURE_DEVICE_H_