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