| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 #ifndef MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_ | |
| 6 #define MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_ | |
| 7 | |
| 8 #if defined(OS_OPENBSD) | |
| 9 #include <sys/videoio.h> | |
| 10 #else | |
| 11 #include <linux/videodev2.h> | |
| 12 #endif | |
| 13 | |
| 14 #include "base/files/scoped_file.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/scoped_vector.h" | |
| 17 #include "media/video/capture/video_capture_device.h" | |
| 18 | |
| 19 namespace media { | |
| 20 | |
| 21 // Class doing the actual Linux capture using V4L2 API. V4L2 SPLANE/MPLANE | |
| 22 // capture specifics are implemented in derived classes. Created and destroyed | |
| 23 // on the owner's thread, otherwise living and operating on |v4l2_task_runner_|. | |
| 24 class V4L2CaptureDelegate | |
| 25 : public base::RefCountedThreadSafe<V4L2CaptureDelegate> { | |
| 26 public: | |
| 27 // Creates the appropiate VideoCaptureDelegate according to parameters. | |
| 28 static scoped_refptr<V4L2CaptureDelegate> CreateV4L2CaptureDelegate( | |
| 29 const VideoCaptureDevice::Name& device_name, | |
| 30 const scoped_refptr<base::SingleThreadTaskRunner>& v4l2_task_runner, | |
| 31 int power_line_frequency); | |
| 32 | |
| 33 // Retrieves the #planes for a given |fourcc|, or 0 if unknown. | |
| 34 static size_t GetNumPlanesForFourCc(uint32_t fourcc); | |
| 35 // Returns the Chrome pixel format for |v4l2_fourcc| or PIXEL_FORMAT_UNKNOWN. | |
| 36 static VideoPixelFormat V4l2FourCcToChromiumPixelFormat(uint32_t v4l2_fourcc); | |
| 37 | |
| 38 // Composes a list of usable and supported pixel formats, in order of | |
| 39 // preference, with MJPEG prioritised depending on |prefer_mjpeg|. | |
| 40 static std::list<uint32_t> GetListOfUsableFourCcs(bool prefer_mjpeg); | |
| 41 | |
| 42 // Forward-to versions of VideoCaptureDevice virtual methods. | |
| 43 void AllocateAndStart(int width, | |
| 44 int height, | |
| 45 float frame_rate, | |
| 46 scoped_ptr<VideoCaptureDevice::Client> client); | |
| 47 void StopAndDeAllocate(); | |
| 48 | |
| 49 void SetRotation(int rotation); | |
| 50 | |
| 51 protected: | |
| 52 // Class keeping track of SPLANE/MPLANE V4L2 buffers, mmap()ed on construction | |
| 53 // and munmap()ed on destruction. Destruction is syntactically equal for | |
| 54 // S/MPLANE but not construction, so this is implemented in derived classes. | |
| 55 // Internally it has a vector of planes, which for SPLANE will contain only | |
| 56 // one element. | |
| 57 class BufferTracker : public base::RefCounted<BufferTracker> { | |
| 58 public: | |
| 59 BufferTracker(); | |
| 60 // Abstract method to mmap() given |fd| according to |buffer|, planarity | |
| 61 // specific. | |
| 62 virtual bool Init(int fd, const v4l2_buffer& buffer) = 0; | |
| 63 | |
| 64 uint8_t* const GetPlaneStart(size_t plane) const { | |
| 65 DCHECK_LT(plane, planes_.size()); | |
| 66 return planes_[plane].start; | |
| 67 } | |
| 68 | |
| 69 protected: | |
| 70 friend class base::RefCounted<BufferTracker>; | |
| 71 virtual ~BufferTracker(); | |
| 72 // Adds a given mmap()ed plane to |planes_|. | |
| 73 void AddMmapedPlane(uint8_t* const start, size_t length); | |
| 74 | |
| 75 private: | |
| 76 struct Plane { | |
| 77 uint8_t* start; | |
| 78 size_t length; | |
| 79 }; | |
| 80 std::vector<Plane> planes_; | |
| 81 }; | |
| 82 | |
| 83 V4L2CaptureDelegate( | |
| 84 const VideoCaptureDevice::Name& device_name, | |
| 85 const scoped_refptr<base::SingleThreadTaskRunner>& v4l2_task_runner, | |
| 86 int power_line_frequency); | |
| 87 virtual ~V4L2CaptureDelegate(); | |
| 88 | |
| 89 // Creates the necessary, planarity-specific, internal tracking schemes, | |
| 90 virtual scoped_refptr<BufferTracker> CreateBufferTracker() const = 0; | |
| 91 | |
| 92 // Fill in |format| with the given parameters, in a planarity dependent way. | |
| 93 virtual bool FillV4L2Format(v4l2_format* format, | |
| 94 uint32_t width, | |
| 95 uint32_t height, | |
| 96 uint32_t pixelformat_fourcc) const = 0; | |
| 97 | |
| 98 // Finish filling |buffer| struct with planarity-dependent data. | |
| 99 virtual void FinishFillingV4L2Buffer(v4l2_buffer* buffer) const = 0; | |
| 100 | |
| 101 // Sends the captured |buffer| to the |client_|, synchronously. | |
| 102 virtual void SendBuffer( | |
| 103 const scoped_refptr<BufferTracker>& buffer_tracker, | |
| 104 const v4l2_format& format) const = 0; | |
| 105 | |
| 106 // A few accessors for SendBuffer()'s to access private member variables. | |
| 107 VideoCaptureFormat capture_format() const { return capture_format_; } | |
| 108 VideoCaptureDevice::Client* client() const { return client_.get(); } | |
| 109 int rotation() const { return rotation_; } | |
| 110 | |
| 111 private: | |
| 112 friend class base::RefCountedThreadSafe<V4L2CaptureDelegate>; | |
| 113 | |
| 114 // Returns the input |fourcc| as a std::string four char representation. | |
| 115 static std::string FourccToString(uint32_t fourcc); | |
| 116 // VIDIOC_QUERYBUFs a buffer from V4L2, creates a BufferTracker for it and | |
| 117 // enqueues it (VIDIOC_QBUF) back into V4L2. | |
| 118 bool MapAndQueueBuffer(int index); | |
| 119 // Fills all common parts of |buffer|. Delegates to FinishFillingV4L2Buffer() | |
| 120 // for filling in the planar-dependent parts. | |
| 121 void FillV4L2Buffer(v4l2_buffer* buffer, int i) const; | |
| 122 void DoCapture(); | |
| 123 void SetErrorState(const std::string& reason); | |
| 124 | |
| 125 const v4l2_buf_type capture_type_; | |
| 126 const scoped_refptr<base::SingleThreadTaskRunner> v4l2_task_runner_; | |
| 127 const VideoCaptureDevice::Name device_name_; | |
| 128 const int power_line_frequency_; | |
| 129 | |
| 130 // The following members are only known on AllocateAndStart(). | |
| 131 VideoCaptureFormat capture_format_; | |
| 132 v4l2_format video_fmt_; | |
| 133 scoped_ptr<VideoCaptureDevice::Client> client_; | |
| 134 base::ScopedFD device_fd_; | |
| 135 | |
| 136 // Vector of BufferTracker to keep track of mmap()ed pointers and their use. | |
| 137 std::vector<scoped_refptr<BufferTracker>> buffer_tracker_pool_; | |
| 138 | |
| 139 bool is_capturing_; | |
| 140 int timeout_count_; | |
| 141 | |
| 142 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270. | |
| 143 int rotation_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(V4L2CaptureDelegate); | |
| 146 }; | |
| 147 | |
| 148 } // namespace media | |
| 149 | |
| 150 #endif // MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_ | |
| OLD | NEW |