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