• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 // This file defines the V4L2Device interface which is used by the
6 // V4L2DecodeAccelerator class to delegate/pass the device specific
7 // handling of any of the functionalities.
8 // Note: ported from Chromium commit head: 2f13d62f0c0d
9 // Note: the complete v4l2 device code is ported from Chromium, but some parts
10 // have been removed:
11 // - All V4L2 request functionality has been removed, as it required a newer
12 //   kernel version.
13 // - void SetConfigStore() has been removed as it depends on a newer kernel
14 //   version.
15 // - QueueDMABuf() from native pixmap planes has been removed, as
16 //   NativePixmapPlane have not been ported.
17 // - GetVideoFrame() is removed as it depends on some helper functions that have
18 //   not been ported.
19 // - GL-related functionality has been removed: canCreateEGLImageFrom(),
20 //   CreateEGLImage(), CreateGLImage() and GetTextureTarget()
21 // - V4L2PixFmtToDrmFormat() has been removed, as DRM is not supported yet.
22 
23 #ifndef V4L2_DEVICE_H_
24 #define V4L2_DEVICE_H_
25 
26 #include <linux/videodev2.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 
30 #include <queue>
31 #include <vector>
32 
33 #include "base/containers/flat_map.h"
34 #include "base/files/scoped_file.h"
35 #include "base/memory/ref_counted.h"
36 
37 #include "fourcc.h"
38 #include "size.h"
39 #include "v4l2_device_poller.h"
40 #include "video_codecs.h"
41 #include "video_decode_accelerator.h"
42 #include "video_encode_accelerator.h"
43 #include "video_frame.h"
44 #include "video_frame_layout.h"
45 #include "video_pixel_format.h"
46 
47 // TODO(mojahsu): remove this once V4L2 headers are updated.
48 #ifndef V4L2_PIX_FMT_JPEG_RAW
49 #define V4L2_PIX_FMT_JPEG_RAW v4l2_fourcc('J', 'P', 'G', 'R')
50 #endif
51 #ifndef V4L2_CID_JPEG_LUMA_QUANTIZATION
52 #define V4L2_CID_JPEG_LUMA_QUANTIZATION (V4L2_CID_JPEG_CLASS_BASE + 5)
53 #endif
54 #ifndef V4L2_CID_JPEG_CHROMA_QUANTIZATION
55 #define V4L2_CID_JPEG_CHROMA_QUANTIZATION (V4L2_CID_JPEG_CLASS_BASE + 6)
56 #endif
57 
58 // TODO(b/132589320): remove this once V4L2 header is updated.
59 #ifndef V4L2_PIX_FMT_MM21
60 // MTK 8-bit block mode, two non-contiguous planes.
61 #define V4L2_PIX_FMT_MM21 v4l2_fourcc('M', 'M', '2', '1')
62 #endif
63 
64 namespace media {
65 
66 class V4L2Queue;
67 class V4L2BufferRefBase;
68 class V4L2BuffersList;
69 class V4L2DecodeSurface;
70 
71 // Dummy V4L2RequestRef. The full request queue functionality could not be
72 // ported as it requires a newer kernel header.
73 class V4L2RequestRef {};
74 
75 // Wrapper for the 'v4l2_ext_control' structure.
76 struct V4L2ExtCtrl {
77   V4L2ExtCtrl(uint32_t id);
78   V4L2ExtCtrl(uint32_t id, int32_t val);
79   struct v4l2_ext_control ctrl;
80 };
81 
82 // A unique reference to a buffer for clients to prepare and submit.
83 //
84 // Clients can prepare a buffer for queuing using the methods of this class, and
85 // then either queue it using the Queue() method corresponding to the memory
86 // type of the buffer, or drop the reference to make the buffer available again.
87 class V4L2WritableBufferRef {
88  public:
89   V4L2WritableBufferRef(V4L2WritableBufferRef&& other);
90   V4L2WritableBufferRef() = delete;
91   V4L2WritableBufferRef& operator=(V4L2WritableBufferRef&& other);
92 
93   // Return the memory type of the buffer. Useful to e.g. decide which Queue()
94   // method to use.
95   enum v4l2_memory Memory() const;
96 
97   // Queue a MMAP buffer.
98   // When requests are supported, a |request_ref| can be passed along this
99   // the buffer to be submitted.
100   // If successful, true is returned and the reference to the buffer is dropped
101   // so this reference becomes invalid.
102   // In case of error, false is returned and the buffer is returned to the free
103   // list.
104   bool QueueMMap(V4L2RequestRef* request_ref = nullptr) &&;
105   // Queue a USERPTR buffer, assigning |ptrs| as pointer for each plane.
106   // The size of |ptrs| must be equal to the number of planes of this buffer.
107   // When requests are supported, a |request_ref| can be passed along this
108   // the buffer to be submitted.
109   // If successful, true is returned and the reference to the buffer is dropped
110   // so this reference becomes invalid.
111   // In case of error, false is returned and the buffer is returned to the free
112   // list.
113   bool QueueUserPtr(const std::vector<void*>& ptrs,
114                     V4L2RequestRef* request_ref = nullptr) &&;
115   // Queue a DMABUF buffer, assigning |fds| as file descriptors for each plane.
116   // It is allowed the number of |fds| might be greater than the number of
117   // planes of this buffer. It happens when the v4l2 pixel format is single
118   // planar. The fd of the first plane is only used in that case.
119   // When requests are supported, a |request_ref| can be passed along this
120   // the buffer to be submitted.
121   // If successful, true is returned and the reference to the buffer is dropped
122   // so this reference becomes invalid.
123   // In case of error, false is returned and the buffer is returned to the free
124   // list.
125   bool QueueDMABuf(const std::vector<base::ScopedFD>& scoped_fds,
126                    V4L2RequestRef* request_ref = nullptr) &&;
127   // Queue a DMABUF buffer, assigning |fds| as file descriptors for each plane.
128   // It is allowed the number of |fds| might be greater than the number of
129   // planes of this buffer. It happens when the v4l2 pixel format is single
130   // planar. The fd of the first plane is only used in that case.
131   // When requests are supported, a |request_ref| can be passed along this
132   // the buffer to be submitted.
133   // If successful, true is returned and the reference to the buffer is dropped
134   // so this reference becomes invalid.
135   // In case of error, false is returned and the buffer is returned to the free
136   // list.
137   bool QueueDMABuf(const std::vector<int>& fds,
138                    V4L2RequestRef* request_ref = nullptr) &&;
139 
140   // Returns the number of planes in this buffer.
141   size_t PlanesCount() const;
142   // Returns the size of the requested |plane|, in bytes.
143   size_t GetPlaneSize(const size_t plane) const;
144   // Set the size of the requested |plane|, in bytes. It is only valid for
145   // USERPTR and DMABUF buffers. When using MMAP buffer, this method triggers a
146   // DCHECK and is a no-op for release builds.
147   void SetPlaneSize(const size_t plane, const size_t size);
148   // This method can only be used with MMAP buffers.
149   // It will return a pointer to the data of the |plane|th plane.
150   // In case of error (invalid plane index or mapping failed), a nullptr is
151   // returned.
152   void* GetPlaneMapping(const size_t plane);
153   // Set the timestamp field for this buffer.
154   void SetTimeStamp(const struct timeval& timestamp);
155   // Return the previously-set timestamp field for this buffer.
156   const struct timeval& GetTimeStamp() const;
157   // Set the number of bytes used for |plane|.
158   void SetPlaneBytesUsed(const size_t plane, const size_t bytes_used);
159   // Returns the previously-set number of bytes used for |plane|.
160   size_t GetPlaneBytesUsed(const size_t plane) const;
161   // Set the data offset for |plane|, in bytes.
162   void SetPlaneDataOffset(const size_t plane, const size_t data_offset);
163 
164   // Return the V4L2 buffer ID of the underlying buffer.
165   // TODO(acourbot) This is used for legacy clients but should be ultimately
166   // removed. See crbug/879971
167   size_t BufferId() const;
168 
169   ~V4L2WritableBufferRef();
170 
171  private:
172   // Do the actual queue operation once the v4l2_buffer structure is properly
173   // filled.
174   // When requests are supported, a |request_ref| can be passed along this
175   // the buffer to be submitted.
176   bool DoQueue(V4L2RequestRef* request_ref) &&;
177 
178   V4L2WritableBufferRef(const struct v4l2_buffer& v4l2_buffer,
179                         base::WeakPtr<V4L2Queue> queue);
180   friend class V4L2BufferRefFactory;
181 
182   std::unique_ptr<V4L2BufferRefBase> buffer_data_;
183 
184   SEQUENCE_CHECKER(sequence_checker_);
185   DISALLOW_COPY_AND_ASSIGN(V4L2WritableBufferRef);
186 };
187 
188 // A reference to a read-only, dequeued buffer.
189 //
190 // Clients use this class to query the buffer state and content, and are
191 // guaranteed that the buffer will not be reused until all references are
192 // destroyed.
193 // All methods of this class must be called from the same sequence, but
194 // instances of V4L2ReadableBuffer objects can be destroyed from any sequence.
195 // They can even outlive the V4L2 buffers they originate from. This flexibility
196 // is required because V4L2ReadableBufferRefs can be embedded into VideoFrames,
197 // which are then passed to other threads and not necessarily destroyed before
198 // the V4L2Queue buffers are freed.
199 class V4L2ReadableBuffer
200     : public base::RefCountedThreadSafe<V4L2ReadableBuffer> {
201  public:
202   // Returns whether the V4L2_BUF_FLAG_LAST flag is set for this buffer.
203   bool IsLast() const;
204   // Returns whether the V4L2_BUF_FLAG_KEYFRAME flag is set for this buffer.
205   bool IsKeyframe() const;
206   // Return the timestamp set by the driver on this buffer.
207   struct timeval GetTimeStamp() const;
208   // Returns the number of planes in this buffer.
209   size_t PlanesCount() const;
210   // Returns the number of bytes used for |plane|.
211   size_t GetPlaneBytesUsed(size_t plane) const;
212   // Returns the data offset for |plane|.
213   size_t GetPlaneDataOffset(size_t plane) const;
214   // This method can only be used with MMAP buffers.
215   // It will return a pointer to the data of the |plane|th plane.
216   // In case of error (invalid plane index or mapping failed), a nullptr is
217   // returned.
218   const void* GetPlaneMapping(const size_t plane) const;
219 
220   // Return the V4L2 buffer ID of the underlying buffer.
221   // TODO(acourbot) This is used for legacy clients but should be ultimately
222   // removed. See crbug/879971
223   size_t BufferId() const;
224 
225  private:
226   friend class V4L2BufferRefFactory;
227   friend class base::RefCountedThreadSafe<V4L2ReadableBuffer>;
228 
229   ~V4L2ReadableBuffer();
230 
231   V4L2ReadableBuffer(const struct v4l2_buffer& v4l2_buffer,
232                      base::WeakPtr<V4L2Queue> queue);
233 
234   std::unique_ptr<V4L2BufferRefBase> buffer_data_;
235 
236   SEQUENCE_CHECKER(sequence_checker_);
237   DISALLOW_COPY_AND_ASSIGN(V4L2ReadableBuffer);
238 };
239 
240 // Shortcut for naming consistency.
241 using V4L2ReadableBufferRef = scoped_refptr<V4L2ReadableBuffer>;
242 
243 class V4L2Device;
244 class V4L2Buffer;
245 
246 // Interface representing a specific queue of a |V4L2Device|. It provides free
247 // and queued buffer management that is commonly required by clients.
248 //
249 // Buffers managed by this class undergo the following cycle:
250 // 1) Allocated buffers are put into a free buffers pool, indicating that they
251 //    are used neither by the client nor the hardware.
252 // 2) The client obtains a unique, writable reference to one of the free
253 //    buffers in order to set its content and other parameters.
254 // 3) The client then queues the buffer obtained in 2), which invalidates its
255 //    reference. The buffer is now prepared to be processed by the hardware.
256 // 4) Once the hardware is done with the buffer, it is ready to be dequeued by
257 //    the client. The client obtains a read-only, counted reference to the
258 //    buffer and can read its content and metadata, as well as making other
259 //    references to it. The buffer will not be reused until all the references
260 //    are dropped. Once this happens, the buffer goes back to the free list
261 //    described in 1).
262 class V4L2Queue : public base::RefCountedThreadSafe<V4L2Queue> {
263  public:
264   // Set |fourcc| as the current format on this queue. |size| corresponds to the
265   // desired buffer's dimensions (i.e. width and height members of
266   // v4l2_pix_format_mplane (if not applicable, pass Size()).
267   // |buffer_size| is the desired size in bytes of the buffer for single-planar
268   // formats (i.e. sizeimage of the first plane). It can be set to 0 if not
269   // relevant for the desired format.
270   // If the format could be set, then the |v4l2_format| reflecting the actual
271   // format is returned. It is guaranteed to feature the specified |fourcc|,
272   // but any other parameter (including |size| and |buffer_size| may have been
273   // adjusted by the driver, so the caller must check their values.
274   base::Optional<struct v4l2_format> SetFormat(uint32_t fourcc,
275                                                const Size& size,
276                                                size_t buffer_size)
277       WARN_UNUSED_RESULT;
278 
279   // Allocate |count| buffers for the current format of this queue, with a
280   // specific |memory| allocation, and returns the number of buffers allocated
281   // or zero if an error occurred, or if references to any previously allocated
282   // buffers are still held by any clients.
283   //
284   // The number of allocated buffers may be larger than the number requested, so
285   // callers must always check the return value.
286   //
287   // Calling this method while buffers are still allocated results in an error.
288   size_t AllocateBuffers(size_t count,
289                          enum v4l2_memory memory) WARN_UNUSED_RESULT;
290 
291   // Deallocate all buffers previously allocated by |AllocateBuffers|. Any
292   // references to buffers previously allocated held by the client must be
293   // released, or this call will fail.
294   bool DeallocateBuffers();
295 
296   // Returns the memory usage of v4l2 buffers owned by this V4L2Queue which are
297   // mapped in user space memory.
298   size_t GetMemoryUsage() const;
299 
300   // Returns |memory_|, memory type of last buffers allocated by this V4L2Queue.
301   v4l2_memory GetMemoryType() const;
302 
303   // Return a reference to a free buffer for the caller to prepare and submit,
304   // or nullopt if no buffer is currently free.
305   //
306   // If the caller discards the returned reference, the underlying buffer is
307   // made available to clients again.
308   base::Optional<V4L2WritableBufferRef> GetFreeBuffer();
309   base::Optional<V4L2WritableBufferRef> GetFreeBuffer(
310       size_t requested_buffer_id);
311 
312   // Attempt to dequeue a buffer, and return a reference to it if one was
313   // available.
314   //
315   // The first element of the returned pair will be false if an error occurred,
316   // in which case the second element will be nullptr. If no error occurred,
317   // then the first element will be true and the second element will contain a
318   // reference to the dequeued buffer if one was available, or nullptr
319   // otherwise.
320   // Dequeued buffers will not be reused by the driver until all references to
321   // them are dropped.
322   std::pair<bool, V4L2ReadableBufferRef> DequeueBuffer();
323 
324   // Returns true if this queue is currently streaming.
325   bool IsStreaming() const;
326   // If not currently streaming, starts streaming. Returns true if we started
327   // streaming, or were already streaming, or false if we were not streaming
328   // and an error occurred when attempting to start the stream. On failure, any
329   // previously-queued buffers will be dequeued without processing and made
330   // available to the client, while any buffers held by the client will remain
331   // unchanged and their ownership will remain with the client.
332   bool Streamon();
333   // If currently streaming, stops streaming. Also make all queued buffers
334   // available to the client again regardless of the streaming state.
335   // If an error occurred while attempting to stop streaming, then false is
336   // returned and queued buffers are left untouched since the V4L2 queue may
337   // still be using them.
338   bool Streamoff();
339 
340   // Returns the number of buffers currently allocated for this queue.
341   size_t AllocatedBuffersCount() const;
342   // Returns the number of currently free buffers on this queue.
343   size_t FreeBuffersCount() const;
344   // Returns the number of buffers currently queued on this queue.
345   size_t QueuedBuffersCount() const;
346 
347   // Returns true if requests are supported by this queue.
348   bool SupportsRequests();
349 
350  private:
351   ~V4L2Queue();
352 
353   // Called when clients request a buffer to be queued.
354   bool QueueBuffer(struct v4l2_buffer* v4l2_buffer);
355 
356   const enum v4l2_buf_type type_;
357   enum v4l2_memory memory_ = V4L2_MEMORY_MMAP;
358   bool is_streaming_ = false;
359   // Set to true if the queue supports requests.
360   bool supports_requests_ = false;
361   size_t planes_count_ = 0;
362   // Current format as set by SetFormat.
363   base::Optional<struct v4l2_format> current_format_;
364 
365   std::vector<std::unique_ptr<V4L2Buffer>> buffers_;
366 
367   // Buffers that are available for client to get and submit.
368   // Buffers in this list are not referenced by anyone else than ourselves.
369   scoped_refptr<V4L2BuffersList> free_buffers_;
370   // Buffers that have been queued by the client, and not dequeued yet.
371   std::set<size_t> queued_buffers_;
372 
373   scoped_refptr<V4L2Device> device_;
374   // Callback to call in this queue's destructor.
375   base::OnceClosure destroy_cb_;
376 
377   V4L2Queue(scoped_refptr<V4L2Device> dev,
378             enum v4l2_buf_type type,
379             base::OnceClosure destroy_cb);
380   friend class V4L2QueueFactory;
381   friend class V4L2BufferRefBase;
382   friend class base::RefCountedThreadSafe<V4L2Queue>;
383 
384   SEQUENCE_CHECKER(sequence_checker_);
385 
386   base::WeakPtrFactory<V4L2Queue> weak_this_factory_;
387 
388   DISALLOW_COPY_AND_ASSIGN(V4L2Queue);
389 };
390 
391 class V4L2Device : public base::RefCountedThreadSafe<V4L2Device> {
392  public:
393   // Utility format conversion functions
394   // If there is no corresponding single- or multi-planar format, returns 0.
395   static uint32_t VideoCodecProfileToV4L2PixFmt(VideoCodecProfile profile,
396                                                 bool slice_based);
397   static VideoCodecProfile V4L2ProfileToVideoCodecProfile(VideoCodec codec,
398                                                           uint32_t profile);
399   std::vector<VideoCodecProfile> V4L2PixFmtToVideoCodecProfiles(
400       uint32_t pix_fmt,
401       bool is_encoder);
402   // Calculates the largest plane's allocation size requested by a V4L2 device.
403   static Size AllocatedSizeFromV4L2Format(const struct v4l2_format& format);
404 
405   // Convert required H264 profile and level to V4L2 enums.
406   static int32_t VideoCodecProfileToV4L2H264Profile(VideoCodecProfile profile);
407   static int32_t H264LevelIdcToV4L2H264Level(uint8_t level_idc);
408 
409   // Converts v4l2_memory to a string.
410   static const char* V4L2MemoryToString(const v4l2_memory memory);
411 
412   // Returns the printable name of a v4l2_buf_type.
413   static const char* V4L2BufferTypeToString(const enum v4l2_buf_type buf_type);
414 
415   // Composes human readable string of v4l2_format.
416   static std::string V4L2FormatToString(const struct v4l2_format& format);
417 
418   // Composes human readable string of v4l2_buffer.
419   static std::string V4L2BufferToString(const struct v4l2_buffer& buffer);
420 
421   // Composes VideoFrameLayout based on v4l2_format.
422   // If error occurs, it returns base::nullopt.
423   static base::Optional<VideoFrameLayout> V4L2FormatToVideoFrameLayout(
424       const struct v4l2_format& format);
425 
426   // Returns number of planes of |pix_fmt|.
427   static size_t GetNumPlanesOfV4L2PixFmt(uint32_t pix_fmt);
428 
429   enum class Type {
430     kDecoder,
431     kEncoder,
432     kImageProcessor,
433     kJpegDecoder,
434     kJpegEncoder,
435   };
436 
437   // Create and initialize an appropriate V4L2Device instance for the current
438   // platform, or return nullptr if not available.
439   static scoped_refptr<V4L2Device> Create();
440 
441   // Open a V4L2 device of |type| for use with |v4l2_pixfmt|.
442   // Return true on success.
443   // The device will be closed in the destructor.
444   virtual bool Open(Type type, uint32_t v4l2_pixfmt) = 0;
445 
446   // Returns the V4L2Queue corresponding to the requested |type|, or nullptr
447   // if the requested queue type is not supported.
448   scoped_refptr<V4L2Queue> GetQueue(enum v4l2_buf_type type);
449 
450   // Parameters and return value are the same as for the standard ioctl() system
451   // call.
452   virtual int Ioctl(int request, void* arg) = 0;
453 
454   // This method sleeps until either:
455   // - SetDevicePollInterrupt() is called (on another thread),
456   // - |poll_device| is true, and there is new data to be read from the device,
457   //   or an event from the device has arrived; in the latter case
458   //   |*event_pending| will be set to true.
459   // Returns false on error, true otherwise.
460   // This method should be called from a separate thread.
461   virtual bool Poll(bool poll_device, bool* event_pending) = 0;
462 
463   // These methods are used to interrupt the thread sleeping on Poll() and force
464   // it to return regardless of device state, which is usually when the client
465   // is no longer interested in what happens with the device (on cleanup,
466   // client state change, etc.). When SetDevicePollInterrupt() is called, Poll()
467   // will return immediately, and any subsequent calls to it will also do so
468   // until ClearDevicePollInterrupt() is called.
469   virtual bool SetDevicePollInterrupt() = 0;
470   virtual bool ClearDevicePollInterrupt() = 0;
471 
472   // Wrappers for standard mmap/munmap system calls.
473   virtual void* Mmap(void* addr,
474                      unsigned int len,
475                      int prot,
476                      int flags,
477                      unsigned int offset) = 0;
478   virtual void Munmap(void* addr, unsigned int len) = 0;
479 
480   // Return a vector of dmabuf file descriptors, exported for V4L2 buffer with
481   // |index|, assuming the buffer contains |num_planes| V4L2 planes and is of
482   // |type|. Return an empty vector on failure.
483   // The caller is responsible for closing the file descriptors after use.
484   virtual std::vector<base::ScopedFD> GetDmabufsForV4L2Buffer(
485       int index,
486       size_t num_planes,
487       enum v4l2_buf_type type) = 0;
488 
489   // Returns the preferred V4L2 input formats for |type| or empty if none.
490   virtual std::vector<uint32_t> PreferredInputFormat(Type type) = 0;
491 
492   // NOTE: The below methods to query capabilities have a side effect of
493   // closing the previously-open device, if any, and should not be called after
494   // Open().
495   // TODO(posciak): fix this.
496 
497   // Get minimum and maximum resolution for fourcc |pixelformat| and store to
498   // |min_resolution| and |max_resolution|.
499   void GetSupportedResolution(uint32_t pixelformat,
500                               Size* min_resolution,
501                               Size* max_resolution);
502 
503   std::vector<uint32_t> EnumerateSupportedPixelformats(v4l2_buf_type buf_type);
504 
505   // Return V4L2 pixelformats supported by the available image processor
506   // devices for |buf_type|.
507   virtual std::vector<uint32_t> GetSupportedImageProcessorPixelformats(
508       v4l2_buf_type buf_type) = 0;
509 
510   // Return supported profiles for decoder, including only profiles for given
511   // fourcc |pixelformats|.
512   virtual VideoDecodeAccelerator::SupportedProfiles GetSupportedDecodeProfiles(
513       const size_t num_formats,
514       const uint32_t pixelformats[]) = 0;
515 
516   // Return supported profiles for encoder.
517   virtual VideoEncodeAccelerator::SupportedProfiles
518   GetSupportedEncodeProfiles() = 0;
519 
520   // Return true if image processing is supported, false otherwise.
521   virtual bool IsImageProcessingSupported() = 0;
522 
523   // Return true if JPEG codec is supported, false otherwise.
524   virtual bool IsJpegDecodingSupported() = 0;
525   virtual bool IsJpegEncodingSupported() = 0;
526 
527   // Start polling on this V4L2Device. |event_callback| will be posted to
528   // the caller's sequence if a buffer is ready to be dequeued and/or a V4L2
529   // event has been posted. |error_callback| will be posted to the client's
530   // sequence if a polling error has occurred.
531   bool StartPolling(V4L2DevicePoller::EventCallback event_callback,
532                     base::RepeatingClosure error_callback);
533   // Stop polling this V4L2Device if polling was active. No new events will
534   // be posted after this method has returned.
535   bool StopPolling();
536   // Schedule a polling event if polling is enabled. This method is intended
537   // to be called from V4L2Queue, clients should not need to call it directly.
538   void SchedulePoll();
539 
540   // Check whether the V4L2 control with specified |ctrl_id| is supported.
541   bool IsCtrlExposed(uint32_t ctrl_id);
542   // Set the specified list of |ctrls| for the specified |ctrl_class|, returns
543   // whether the operation succeeded.
544   bool SetExtCtrls(uint32_t ctrl_class, std::vector<V4L2ExtCtrl> ctrls);
545 
546   // Check whether the V4L2 command with specified |command_id| is supported.
547   bool IsCommandSupported(uint32_t command_id);
548   // Check whether the V4L2 device has the specified |capabilities|.
549   bool HasCapabilities(uint32_t capabilities);
550 
551  protected:
552   friend class base::RefCountedThreadSafe<V4L2Device>;
553   V4L2Device();
554   virtual ~V4L2Device();
555 
556   VideoDecodeAccelerator::SupportedProfiles EnumerateSupportedDecodeProfiles(
557       const size_t num_formats,
558       const uint32_t pixelformats[]);
559 
560   VideoEncodeAccelerator::SupportedProfiles EnumerateSupportedEncodeProfiles();
561 
562  private:
563   // Perform platform-specific initialization of the device instance.
564   // Return true on success, false on error or if the particular implementation
565   // is not available.
566   virtual bool Initialize() = 0;
567 
568   // Associates a v4l2_buf_type to its queue.
569   base::flat_map<enum v4l2_buf_type, V4L2Queue*> queues_;
570 
571   // Callback that is called upon a queue's destruction, to cleanup its pointer
572   // in queues_.
573   void OnQueueDestroyed(v4l2_buf_type buf_type);
574 
575   // Used if EnablePolling() is called to signal the user that an event
576   // happened or a buffer is ready to be dequeued.
577   std::unique_ptr<V4L2DevicePoller> device_poller_;
578 
579   // Indicates whether the request queue creation has been tried once.
580   bool requests_queue_creation_called_ = false;
581 
582   SEQUENCE_CHECKER(client_sequence_checker_);
583 };
584 
585 }  //  namespace media
586 
587 #endif  // V4L2_DEVICE_H_
588