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: fb70f64
9 // Note: it's also merged with generic_v4l2_device.h (head: fb70f64)
10 
11 #ifndef V4L2_DEVICE_H_
12 #define V4L2_DEVICE_H_
13 
14 #include <map>
15 #include <stddef.h>
16 #include <stdint.h>
17 
18 #include "base/files/scoped_file.h"
19 #include "base/memory/ref_counted.h"
20 #include "size.h"
21 #include "video_codecs.h"
22 #include "video_decode_accelerator.h"
23 #include "video_pixel_format.h"
24 #include "videodev2.h"
25 
26 // TODO(posciak): remove this once V4L2 headers are updated.
27 #define V4L2_PIX_FMT_MT21 v4l2_fourcc('M', 'T', '2', '1')
28 #ifndef V4L2_BUF_FLAG_LAST
29 #define V4L2_BUF_FLAG_LAST 0x00100000
30 #endif
31 
32 namespace media {
33 
34 // Implemented for decoder usage only.
35 class V4L2Device : public base::RefCountedThreadSafe<V4L2Device> {
36  public:
37   V4L2Device();
38   ~V4L2Device();
39 
40   // Utility format conversion functions
41   static VideoPixelFormat V4L2PixFmtToVideoPixelFormat(uint32_t format);
42   static uint32_t VideoPixelFormatToV4L2PixFmt(VideoPixelFormat format);
43   static uint32_t VideoCodecProfileToV4L2PixFmt(VideoCodecProfile profile,
44                                                 bool slice_based);
45   std::vector<VideoCodecProfile> V4L2PixFmtToVideoCodecProfiles(
46       uint32_t pix_fmt,
47       bool is_encoder);
48 
49   enum class Type {
50     kDecoder,
51     kEncoder,
52     kImageProcessor,
53     kJpegDecoder,
54   };
55 
56   // Open a V4L2 device of |type| for use with |v4l2_pixfmt|.
57   // Return true on success.
58   // The device will be closed in the destructor.
59   bool Open(Type type, uint32_t v4l2_pixfmt);
60 
61   // Parameters and return value are the same as for the standard ioctl() system
62   // call.
63   int Ioctl(int request, void* arg);
64 
65   // This method sleeps until either:
66   // - SetDevicePollInterrupt() is called (on another thread),
67   // - |poll_device| is true, and there is new data to be read from the device,
68   //   or an event from the device has arrived; in the latter case
69   //   |*event_pending| will be set to true.
70   // Returns false on error, true otherwise.
71   // This method should be called from a separate thread.
72   bool Poll(bool poll_device, bool* event_pending);
73 
74   // These methods are used to interrupt the thread sleeping on Poll() and force
75   // it to return regardless of device state, which is usually when the client
76   // is no longer interested in what happens with the device (on cleanup,
77   // client state change, etc.). When SetDevicePollInterrupt() is called, Poll()
78   // will return immediately, and any subsequent calls to it will also do so
79   // until ClearDevicePollInterrupt() is called.
80   bool SetDevicePollInterrupt();
81   bool ClearDevicePollInterrupt();
82 
83   // Wrappers for standard mmap/munmap system calls.
84   void* Mmap(void* addr,
85              unsigned int len,
86              int prot,
87              int flags,
88              unsigned int offset);
89   void Munmap(void* addr, unsigned int len);
90 
91   // Return a vector of dmabuf file descriptors, exported for V4L2 buffer with
92   // |index|, assuming the buffer contains |num_planes| V4L2 planes and is of
93   // |type|. Return an empty vector on failure.
94   // The caller is responsible for closing the file descriptors after use.
95   std::vector<base::ScopedFD> GetDmabufsForV4L2Buffer(
96       int index,
97       size_t num_planes,
98       enum v4l2_buf_type type);
99 
100   // NOTE: The below methods to query capabilities have a side effect of
101   // closing the previously-open device, if any, and should not be called after
102   // Open().
103   // TODO(posciak): fix this.
104 
105   // Get minimum and maximum resolution for fourcc |pixelformat| and store to
106   // |min_resolution| and |max_resolution|.
107   void GetSupportedResolution(uint32_t pixelformat,
108                               Size* min_resolution,
109                               Size* max_resolution);
110 
111   // Return supported profiles for decoder, including only profiles for given
112   // fourcc |pixelformats|.
113   VideoDecodeAccelerator::SupportedProfiles GetSupportedDecodeProfiles(
114       const size_t num_formats,
115       const uint32_t pixelformats[]);
116 
117  private:
118   friend class base::RefCountedThreadSafe<V4L2Device>;
119 
120   // Vector of video device node paths and corresponding pixelformats supported
121   // by each device node.
122   using Devices = std::vector<std::pair<std::string, std::vector<uint32_t>>>;
123 
124   VideoDecodeAccelerator::SupportedProfiles EnumerateSupportedDecodeProfiles(
125       const size_t num_formats,
126       const uint32_t pixelformats[]);
127 
128   std::vector<uint32_t> EnumerateSupportedPixelformats(v4l2_buf_type buf_type);
129 
130   // Open device node for |path| as a device of |type|.
131   bool OpenDevicePath(const std::string& path, Type type);
132 
133   // Close the currently open device.
134   void CloseDevice();
135 
136   // Enumerate all V4L2 devices on the system for |type| and store the results
137   // under devices_by_type_[type].
138   void EnumerateDevicesForType(Type type);
139 
140   // Return device information for all devices of |type| available in the
141   // system. Enumerates and queries devices on first run and caches the results
142   // for subsequent calls.
143   const Devices& GetDevicesForType(Type type);
144 
145   // Return device node path for device of |type| supporting |pixfmt|, or
146   // an empty string if the given combination is not supported by the system.
147   std::string GetDevicePathFor(Type type, uint32_t pixfmt);
148 
149   // Stores information for all devices available on the system
150   // for each device Type.
151   std::map<Type, Devices> devices_by_type_;
152 
153   // The actual device fd.
154   base::ScopedFD device_fd_;
155 
156   // eventfd fd to signal device poll thread when its poll() should be
157   // interrupted.
158   base::ScopedFD device_poll_interrupt_fd_;
159 
160   DISALLOW_COPY_AND_ASSIGN(V4L2Device);
161 };
162 
163 }  //  namespace media
164 
165 #endif  // V4L2_DEVICE_H_
166