Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(989)

Side by Side Diff: media/video/capture/linux/v4l2_video_capture_delegate.h

Issue 967793002: Linux Video Capture: Add V4L2VideoCaptureDelegate{Single,Multi}Plane. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: posciak@ comments on PS5. Minor rebase of video_capture_controller_unittest.cc and BUILD.gn Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 V4L2VideoCaptureDelegate
25 : public base::RefCountedThreadSafe<V4L2VideoCaptureDelegate> {
26 public:
27 // Creates the appropiate VideoCaptureDelegate according to parameters.
28 static scoped_refptr<V4L2VideoCaptureDelegate> CreateV4L2VideoCaptureDelegate(
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 return static_cast<uint8_t* const>(planes_[plane].start);
perkj_chrome 2015/03/19 20:38:17 why is start a void ptr if you want a uint ptr?
mcasas 2015/03/19 22:57:01 mmap() returns a void*. I think it's easier to do
66 }
67 size_t GetPlaneLength(size_t plane) const { return planes_[plane].length; }
68
69 protected:
70 friend class base::RefCounted<BufferTracker>;
71 virtual ~BufferTracker();
72 // Adds a given mmap()ed plane to |planes_|.
73 void AddMmapedPlane(void* const start, size_t length);
74
75 private:
76 struct Plane {
77 void* start;
78 size_t length;
79 };
80 std::vector<Plane> planes_;
81 };
82
83 V4L2VideoCaptureDelegate(
84 const VideoCaptureDevice::Name& device_name,
85 const scoped_refptr<base::SingleThreadTaskRunner>& v4l2_task_runner,
86 int power_line_frequency);
87 virtual ~V4L2VideoCaptureDelegate();
88
89 // Creates the necessary, planarity-specific, internal tracking schemes,
90 virtual scoped_refptr<BufferTracker> CreateBufferTracker() = 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) = 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) = 0;
104
105 // A few accessors for SendBuffer()'s to access private member variables.
106 VideoCaptureFormat capture_format() const { return capture_format_; }
107 VideoCaptureDevice::Client* client() const { return client_.get(); }
108 int rotation() const { return rotation_; }
109
110 private:
111 friend class base::RefCountedThreadSafe<V4L2VideoCaptureDelegate>;
112
113 // Returns the input |fourcc| as a std::string four char representation.
114 static std::string FourccToString(uint32_t fourcc);
115 // VIDIOC_QUERYBUFs a buffer from V4L2, creates a BufferTracker for it and
116 // enqueues it (VIDIOC_QBUF) back into V4L2.
117 bool MapAndQueueBuffer(int index);
118 // Fills all common parts of |buffer|. Delegates to FinishFillingV4L2Buffer()
119 // for filling in the planar-dependent parts.
120 void FillV4L2Buffer(v4l2_buffer* buffer, int i) const;
121 void DoCapture();
122 void SetErrorState(const std::string& reason);
123
124 const v4l2_buf_type capture_type_;
125 const scoped_refptr<base::SingleThreadTaskRunner> v4l2_task_runner_;
126 const VideoCaptureDevice::Name device_name_;
127 const int power_line_frequency_;
128
129 // The following members are only known on AllocateAndStart().
130 VideoCaptureFormat capture_format_;
131 scoped_ptr<VideoCaptureDevice::Client> client_;
132 base::ScopedFD device_fd_;
133
134 // Vector of BufferTracker to keep track of mmap()ed pointers and their use.
135 std::vector<scoped_refptr<BufferTracker>> buffer_tracker_pool_;
136
137 bool is_capturing_;
138 int timeout_count_;
139
140 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270.
141 int rotation_;
142
143 DISALLOW_COPY_AND_ASSIGN(V4L2VideoCaptureDelegate);
144 };
145
146 } // namespace media
147
148 #endif // MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698