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 |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace tracked_objects { | 23 namespace tracked_objects { |
24 class Location; | 24 class Location; |
25 } // namespace tracked_objects | 25 } // namespace tracked_objects |
26 | 26 |
27 namespace media { | 27 namespace media { |
28 | 28 |
29 // Class doing the actual Linux capture using V4L2 API. V4L2 SPLANE/MPLANE | 29 // Class doing the actual Linux capture using V4L2 API. V4L2 SPLANE/MPLANE |
30 // capture specifics are implemented in derived classes. Created and destroyed | 30 // capture specifics are implemented in derived classes. Created and destroyed |
31 // on the owner's thread, otherwise living and operating on |v4l2_task_runner_|. | 31 // on the owner's thread, otherwise living and operating on |v4l2_task_runner_|. |
32 class V4L2CaptureDelegate | 32 // TODO(mcasas): Make this class a non-ref-counted. |
| 33 class V4L2CaptureDelegate final |
33 : public base::RefCountedThreadSafe<V4L2CaptureDelegate> { | 34 : public base::RefCountedThreadSafe<V4L2CaptureDelegate> { |
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 |
51 // Forward-to versions of VideoCaptureDevice virtual methods. | 51 // Forward-to versions of VideoCaptureDevice virtual methods. |
52 void AllocateAndStart(int width, | 52 void AllocateAndStart(int width, |
53 int height, | 53 int height, |
54 float frame_rate, | 54 float frame_rate, |
55 scoped_ptr<VideoCaptureDevice::Client> client); | 55 scoped_ptr<VideoCaptureDevice::Client> client); |
56 void StopAndDeAllocate(); | 56 void StopAndDeAllocate(); |
57 | 57 |
58 void SetRotation(int rotation); | 58 void SetRotation(int rotation); |
59 | 59 |
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: | 60 private: |
138 friend class base::RefCountedThreadSafe<V4L2CaptureDelegate>; | 61 friend class base::RefCountedThreadSafe<V4L2CaptureDelegate>; |
| 62 ~V4L2CaptureDelegate(); |
139 | 63 |
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 | 64 // VIDIOC_QUERYBUFs a buffer from V4L2, creates a BufferTracker for it and |
143 // enqueues it (VIDIOC_QBUF) back into V4L2. | 65 // enqueues it (VIDIOC_QBUF) back into V4L2. |
144 bool MapAndQueueBuffer(int index); | 66 bool MapAndQueueBuffer(int index); |
145 // Fills all common parts of |buffer|. Delegates to FinishFillingV4L2Buffer() | 67 |
146 // for filling in the planar-dependent parts. | |
147 void FillV4L2Buffer(v4l2_buffer* buffer, int i) const; | |
148 void DoCapture(); | 68 void DoCapture(); |
| 69 |
149 void SetErrorState(const tracked_objects::Location& from_here, | 70 void SetErrorState(const tracked_objects::Location& from_here, |
150 const std::string& reason); | 71 const std::string& reason); |
151 | 72 |
152 const v4l2_buf_type capture_type_; | |
153 const scoped_refptr<base::SingleThreadTaskRunner> v4l2_task_runner_; | 73 const scoped_refptr<base::SingleThreadTaskRunner> v4l2_task_runner_; |
154 const VideoCaptureDevice::Name device_name_; | 74 const VideoCaptureDevice::Name device_name_; |
155 const int power_line_frequency_; | 75 const int power_line_frequency_; |
156 | 76 |
157 // The following members are only known on AllocateAndStart(). | 77 // The following members are only known on AllocateAndStart(). |
158 VideoCaptureFormat capture_format_; | 78 VideoCaptureFormat capture_format_; |
159 v4l2_format video_fmt_; | 79 v4l2_format video_fmt_; |
160 scoped_ptr<VideoCaptureDevice::Client> client_; | 80 scoped_ptr<VideoCaptureDevice::Client> client_; |
161 base::ScopedFD device_fd_; | 81 base::ScopedFD device_fd_; |
162 | 82 |
163 // Vector of BufferTracker to keep track of mmap()ed pointers and their use. | 83 // Vector of BufferTracker to keep track of mmap()ed pointers and their use. |
| 84 class BufferTracker; |
164 std::vector<scoped_refptr<BufferTracker>> buffer_tracker_pool_; | 85 std::vector<scoped_refptr<BufferTracker>> buffer_tracker_pool_; |
165 | 86 |
166 bool is_capturing_; | 87 bool is_capturing_; |
167 int timeout_count_; | 88 int timeout_count_; |
168 | 89 |
169 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270. | 90 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270. |
170 int rotation_; | 91 int rotation_; |
171 | 92 |
172 DISALLOW_COPY_AND_ASSIGN(V4L2CaptureDelegate); | 93 DISALLOW_COPY_AND_ASSIGN(V4L2CaptureDelegate); |
173 }; | 94 }; |
174 | 95 |
175 } // namespace media | 96 } // namespace media |
176 | 97 |
177 #endif // MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_ | 98 #endif // MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_ |
OLD | NEW |