Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / capture / video / linux / v4l2_capture_delegate.h
blob0a87ed794ac795c33a8710cdae95df9d7c26846a
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.
5 #ifndef MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_
6 #define MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_
8 #if defined(OS_OPENBSD)
9 #include <sys/videoio.h>
10 #else
11 #include <linux/videodev2.h>
12 #endif
14 #include "base/files/scoped_file.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_vector.h"
17 #include "media/capture/video/video_capture_device.h"
19 namespace media {
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);
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
36 // VIDEO_CAPTURE_PIXEL_FORMAT_UNKNOWN.
37 static VideoCapturePixelFormat V4l2FourCcToChromiumPixelFormat(
38 uint32_t v4l2_fourcc);
40 // Composes a list of usable and supported pixel formats, in order of
41 // preference, with MJPEG prioritised depending on |prefer_mjpeg|.
42 static std::list<uint32_t> GetListOfUsableFourCcs(bool prefer_mjpeg);
44 // Forward-to versions of VideoCaptureDevice virtual methods.
45 void AllocateAndStart(int width,
46 int height,
47 float frame_rate,
48 scoped_ptr<VideoCaptureDevice::Client> client);
49 void StopAndDeAllocate();
51 void SetRotation(int rotation);
53 protected:
54 // Class keeping track of SPLANE/MPLANE V4L2 buffers, mmap()ed on construction
55 // and munmap()ed on destruction. Destruction is syntactically equal for
56 // S/MPLANE but not construction, so this is implemented in derived classes.
57 // Internally it has a vector of planes, which for SPLANE will contain only
58 // one element.
59 class BufferTracker : public base::RefCounted<BufferTracker> {
60 public:
61 BufferTracker();
62 // Abstract method to mmap() given |fd| according to |buffer|, planarity
63 // specific.
64 virtual bool Init(int fd, const v4l2_buffer& buffer) = 0;
66 uint8_t* const GetPlaneStart(size_t plane) const {
67 DCHECK_LT(plane, planes_.size());
68 return planes_[plane].start;
71 size_t GetPlanePayloadSize(size_t plane) const {
72 DCHECK_LT(plane, planes_.size());
73 return planes_[plane].payload_size;
76 void SetPlanePayloadSize(size_t plane, size_t payload_size) {
77 DCHECK_LT(plane, planes_.size());
78 DCHECK_LE(payload_size, planes_[plane].length);
79 planes_[plane].payload_size = payload_size;
82 protected:
83 friend class base::RefCounted<BufferTracker>;
84 virtual ~BufferTracker();
85 // Adds a given mmap()ed plane to |planes_|.
86 void AddMmapedPlane(uint8_t* const start, size_t length);
88 private:
89 struct Plane {
90 uint8_t* start;
91 size_t length;
92 size_t payload_size;
94 std::vector<Plane> planes_;
97 V4L2CaptureDelegate(
98 const VideoCaptureDevice::Name& device_name,
99 const scoped_refptr<base::SingleThreadTaskRunner>& v4l2_task_runner,
100 int power_line_frequency);
101 virtual ~V4L2CaptureDelegate();
103 // Creates the necessary, planarity-specific, internal tracking schemes,
104 virtual scoped_refptr<BufferTracker> CreateBufferTracker() const = 0;
106 // Fill in |format| with the given parameters, in a planarity dependent way.
107 virtual bool FillV4L2Format(v4l2_format* format,
108 uint32_t width,
109 uint32_t height,
110 uint32_t pixelformat_fourcc) const = 0;
112 // Finish filling |buffer| struct with planarity-dependent data.
113 virtual void FinishFillingV4L2Buffer(v4l2_buffer* buffer) const = 0;
115 // Fetch the number of bytes occupied by data in |buffer| and set to
116 // |buffer_tracker|.
117 virtual void SetPayloadSize(
118 const scoped_refptr<BufferTracker>& buffer_tracker,
119 const v4l2_buffer& buffer) const = 0;
121 // Sends the captured |buffer| to the |client_|, synchronously.
122 virtual void SendBuffer(const scoped_refptr<BufferTracker>& buffer_tracker,
123 const v4l2_format& format) const = 0;
125 // A few accessors for SendBuffer()'s to access private member variables.
126 VideoCaptureFormat capture_format() const { return capture_format_; }
127 VideoCaptureDevice::Client* client() const { return client_.get(); }
128 int rotation() const { return rotation_; }
130 private:
131 friend class base::RefCountedThreadSafe<V4L2CaptureDelegate>;
133 // Returns the input |fourcc| as a std::string four char representation.
134 static std::string FourccToString(uint32_t fourcc);
135 // VIDIOC_QUERYBUFs a buffer from V4L2, creates a BufferTracker for it and
136 // enqueues it (VIDIOC_QBUF) back into V4L2.
137 bool MapAndQueueBuffer(int index);
138 // Fills all common parts of |buffer|. Delegates to FinishFillingV4L2Buffer()
139 // for filling in the planar-dependent parts.
140 void FillV4L2Buffer(v4l2_buffer* buffer, int i) const;
141 void DoCapture();
142 void SetErrorState(const std::string& reason);
144 const v4l2_buf_type capture_type_;
145 const scoped_refptr<base::SingleThreadTaskRunner> v4l2_task_runner_;
146 const VideoCaptureDevice::Name device_name_;
147 const int power_line_frequency_;
149 // The following members are only known on AllocateAndStart().
150 VideoCaptureFormat capture_format_;
151 v4l2_format video_fmt_;
152 scoped_ptr<VideoCaptureDevice::Client> client_;
153 base::ScopedFD device_fd_;
155 // Vector of BufferTracker to keep track of mmap()ed pointers and their use.
156 std::vector<scoped_refptr<BufferTracker>> buffer_tracker_pool_;
158 bool is_capturing_;
159 int timeout_count_;
161 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270.
162 int rotation_;
164 DISALLOW_COPY_AND_ASSIGN(V4L2CaptureDelegate);
167 } // namespace media
169 #endif // MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_