| OLD | NEW |
| (Empty) |
| 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. | |
| 11 | |
| 12 #ifndef MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_H_ | |
| 13 #define MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_H_ | |
| 14 | |
| 15 #include <stddef.h> | |
| 16 #include <stdint.h> | |
| 17 | |
| 18 #include <list> | |
| 19 #include <memory> | |
| 20 #include <string> | |
| 21 | |
| 22 #include "base/callback.h" | |
| 23 #include "base/files/file.h" | |
| 24 #include "base/logging.h" | |
| 25 #include "base/memory/ref_counted.h" | |
| 26 #include "base/single_thread_task_runner.h" | |
| 27 #include "base/time/time.h" | |
| 28 #include "build/build_config.h" | |
| 29 #include "media/base/video_capture_types.h" | |
| 30 #include "media/base/video_frame.h" | |
| 31 #include "media/capture/capture_export.h" | |
| 32 #include "media/capture/video/scoped_result_callback.h" | |
| 33 #include "media/capture/video/video_capture_device_descriptor.h" | |
| 34 #include "media/mojo/interfaces/image_capture.mojom.h" | |
| 35 #include "mojo/public/cpp/bindings/array.h" | |
| 36 #include "ui/gfx/gpu_memory_buffer.h" | |
| 37 | |
| 38 namespace tracked_objects { | |
| 39 class Location; | |
| 40 } // namespace tracked_objects | |
| 41 | |
| 42 namespace media { | |
| 43 | |
| 44 class CAPTURE_EXPORT VideoCaptureDevice { | |
| 45 public: | |
| 46 | |
| 47 // Interface defining the methods that clients of VideoCapture must have. It | |
| 48 // is actually two-in-one: clients may implement OnIncomingCapturedData() or | |
| 49 // ReserveOutputBuffer() + OnIncomingCapturedVideoFrame(), or all of them. | |
| 50 // All clients must implement OnError(). | |
| 51 class CAPTURE_EXPORT Client { | |
| 52 public: | |
| 53 // Memory buffer returned by Client::ReserveOutputBuffer(). | |
| 54 class CAPTURE_EXPORT Buffer { | |
| 55 public: | |
| 56 virtual ~Buffer() = 0; | |
| 57 virtual int id() const = 0; | |
| 58 virtual gfx::Size dimensions() const = 0; | |
| 59 virtual size_t mapped_size() const = 0; | |
| 60 virtual void* data(int plane) = 0; | |
| 61 void* data() { return data(0); } | |
| 62 virtual ClientBuffer AsClientBuffer(int plane) = 0; | |
| 63 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
| 64 virtual base::FileDescriptor AsPlatformFile() = 0; | |
| 65 #endif | |
| 66 }; | |
| 67 | |
| 68 virtual ~Client() {} | |
| 69 | |
| 70 // Captured a new video frame, data for which is pointed to by |data|. | |
| 71 // | |
| 72 // The format of the frame is described by |frame_format|, and is assumed to | |
| 73 // be tightly packed. This method will try to reserve an output buffer and | |
| 74 // copy from |data| into the output buffer. If no output buffer is | |
| 75 // available, the frame will be silently dropped. |reference_time| is | |
| 76 // system clock time when we detect the capture happens, it is used for | |
| 77 // Audio/Video sync, not an exact presentation time for playout, because it | |
| 78 // could contain noise. |timestamp| measures the ideal time span between the | |
| 79 // first frame in the stream and the current frame; however, the time source | |
| 80 // is determined by the platform's device driver and is often not the system | |
| 81 // clock, or even has a drift with respect to system clock. | |
| 82 virtual void OnIncomingCapturedData(const uint8_t* data, | |
| 83 int length, | |
| 84 const VideoCaptureFormat& frame_format, | |
| 85 int clockwise_rotation, | |
| 86 base::TimeTicks reference_time, | |
| 87 base::TimeDelta timestamp) = 0; | |
| 88 | |
| 89 // Reserve an output buffer into which contents can be captured directly. | |
| 90 // The returned Buffer will always be allocated with a memory size suitable | |
| 91 // for holding a packed video frame with pixels of |format| format, of | |
| 92 // |dimensions| frame dimensions. It is permissible for |dimensions| to be | |
| 93 // zero; in which case the returned Buffer does not guarantee memory | |
| 94 // backing, but functions as a reservation for external input for the | |
| 95 // purposes of buffer throttling. | |
| 96 // | |
| 97 // The output buffer stays reserved and mapped for use until the Buffer | |
| 98 // object is destroyed or returned. | |
| 99 virtual std::unique_ptr<Buffer> ReserveOutputBuffer( | |
| 100 const gfx::Size& dimensions, | |
| 101 VideoPixelFormat format, | |
| 102 VideoPixelStorage storage) = 0; | |
| 103 | |
| 104 // Captured new video data, held in |frame| or |buffer|, respectively for | |
| 105 // OnIncomingCapturedVideoFrame() and OnIncomingCapturedBuffer(). | |
| 106 // | |
| 107 // In both cases, as the frame is backed by a reservation returned by | |
| 108 // ReserveOutputBuffer(), delivery is guaranteed and will require no | |
| 109 // additional copies in the browser process. | |
| 110 // See OnIncomingCapturedData for details of |reference_time| and | |
| 111 // |timestamp|. | |
| 112 virtual void OnIncomingCapturedBuffer( | |
| 113 std::unique_ptr<Buffer> buffer, | |
| 114 const VideoCaptureFormat& frame_format, | |
| 115 base::TimeTicks reference_time, | |
| 116 base::TimeDelta timestamp) = 0; | |
| 117 virtual void OnIncomingCapturedVideoFrame( | |
| 118 std::unique_ptr<Buffer> buffer, | |
| 119 const scoped_refptr<VideoFrame>& frame) = 0; | |
| 120 | |
| 121 // Attempts to reserve the same Buffer provided in the last call to one of | |
| 122 // the OnIncomingCapturedXXX() methods. This will fail if the content of the | |
| 123 // Buffer has not been preserved, or if the |dimensions|, |format|, or | |
| 124 // |storage| disagree with how it was reserved via ReserveOutputBuffer(). | |
| 125 // When this operation fails, nullptr will be returned. | |
| 126 virtual std::unique_ptr<Buffer> ResurrectLastOutputBuffer( | |
| 127 const gfx::Size& dimensions, | |
| 128 VideoPixelFormat format, | |
| 129 VideoPixelStorage storage) = 0; | |
| 130 | |
| 131 // An error has occurred that cannot be handled and VideoCaptureDevice must | |
| 132 // be StopAndDeAllocate()-ed. |reason| is a text description of the error. | |
| 133 virtual void OnError(const tracked_objects::Location& from_here, | |
| 134 const std::string& reason) = 0; | |
| 135 | |
| 136 // VideoCaptureDevice requests the |message| to be logged. | |
| 137 virtual void OnLog(const std::string& message) {} | |
| 138 | |
| 139 // Returns the current buffer pool utilization, in the range 0.0 (no buffers | |
| 140 // are in use by producers or consumers) to 1.0 (all buffers are in use). | |
| 141 virtual double GetBufferPoolUtilization() const = 0; | |
| 142 }; | |
| 143 | |
| 144 virtual ~VideoCaptureDevice(); | |
| 145 | |
| 146 // Prepares the video capturer for use. StopAndDeAllocate() must be called | |
| 147 // before the object is deleted. | |
| 148 virtual void AllocateAndStart(const VideoCaptureParams& params, | |
| 149 std::unique_ptr<Client> client) = 0; | |
| 150 | |
| 151 // In cases where the video capturer self-pauses (e.g., a screen capturer | |
| 152 // where the screen's content has not changed in a while), consumers may call | |
| 153 // this to request a "refresh frame" be delivered to the Client. This is used | |
| 154 // in a number of circumstances, such as: | |
| 155 // | |
| 156 // 1. An additional consumer of video frames is starting up and requires a | |
| 157 // first frame (as opposed to not receiving a frame for an indeterminate | |
| 158 // amount of time). | |
| 159 // 2. A few repeats of the same frame would allow a lossy video encoder to | |
| 160 // improve the video quality of unchanging content. | |
| 161 // | |
| 162 // The default implementation is a no-op. VideoCaptureDevice implementations | |
| 163 // are not required to honor this request, especially if they do not | |
| 164 // self-pause and/or if honoring the request would cause them to exceed their | |
| 165 // configured maximum frame rate. Any VideoCaptureDevice that does self-pause, | |
| 166 // however, should provide an implementation of this method that makes | |
| 167 // reasonable attempts to honor these requests. | |
| 168 virtual void RequestRefreshFrame() {} | |
| 169 | |
| 170 // Deallocates the video capturer, possibly asynchronously. | |
| 171 // | |
| 172 // This call requires the device to do the following things, eventually: put | |
| 173 // hardware into a state where other applications could use it, free the | |
| 174 // memory associated with capture, and delete the |client| pointer passed into | |
| 175 // AllocateAndStart. | |
| 176 // | |
| 177 // If deallocation is done asynchronously, then the device implementation must | |
| 178 // ensure that a subsequent AllocateAndStart() operation targeting the same ID | |
| 179 // would be sequenced through the same task runner, so that deallocation | |
| 180 // happens first. | |
| 181 virtual void StopAndDeAllocate() = 0; | |
| 182 | |
| 183 // Retrieve the photo capabilities of the device (e.g. zoom levels etc). | |
| 184 using GetPhotoCapabilitiesCallback = | |
| 185 ScopedResultCallback<base::Callback<void(mojom::PhotoCapabilitiesPtr)>>; | |
| 186 virtual void GetPhotoCapabilities(GetPhotoCapabilitiesCallback callback); | |
| 187 | |
| 188 using SetPhotoOptionsCallback = | |
| 189 ScopedResultCallback<base::Callback<void(bool)>>; | |
| 190 virtual void SetPhotoOptions(mojom::PhotoSettingsPtr settings, | |
| 191 SetPhotoOptionsCallback callback); | |
| 192 | |
| 193 // Asynchronously takes a photo, possibly reconfiguring the capture objects | |
| 194 // and/or interrupting the capture flow. Runs |callback| on the thread | |
| 195 // where TakePhoto() is called, if the photo was successfully taken. | |
| 196 using TakePhotoCallback = | |
| 197 ScopedResultCallback<base::Callback<void(mojom::BlobPtr blob)>>; | |
| 198 virtual void TakePhoto(TakePhotoCallback callback); | |
| 199 | |
| 200 // Gets the power line frequency, either from the params if specified by the | |
| 201 // user or from the current system time zone. | |
| 202 PowerLineFrequency GetPowerLineFrequency( | |
| 203 const VideoCaptureParams& params) const; | |
| 204 | |
| 205 private: | |
| 206 // Gets the power line frequency from the current system time zone if this is | |
| 207 // defined, otherwise returns 0. | |
| 208 PowerLineFrequency GetPowerLineFrequencyForLocation() const; | |
| 209 }; | |
| 210 | |
| 211 } // namespace media | |
| 212 | |
| 213 #endif // MEDIA_CAPTURE_VIDEO_VIDEO_CAPTURE_DEVICE_H_ | |
| OLD | NEW |