OLD | NEW |
(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 // Returns the Chrome pixel format for |v4l2_fourcc| or PIXEL_FORMAT_UNKNOWN. |
| 34 static VideoPixelFormat V4l2FourCcToChromiumPixelFormat(uint32_t v4l2_fourcc); |
| 35 |
| 36 // Composes a list of usable and supported pixel formats, in order of |
| 37 // preference, with MJPEG prioritised depending on |prefer_mjpeg|. |
| 38 static std::list<uint32_t> GetListOfUsableFourCcs(bool prefer_mjpeg); |
| 39 |
| 40 // Forward-to versions of VideoCaptureDevice virtual methods. |
| 41 void AllocateAndStart(int width, |
| 42 int height, |
| 43 float frame_rate, |
| 44 scoped_ptr<VideoCaptureDevice::Client> client); |
| 45 void StopAndDeAllocate(); |
| 46 |
| 47 void SetRotation(int rotation); |
| 48 |
| 49 protected: |
| 50 // Class for keeping track of SPLANE/MPLANE V4L2 mmap()ed buffers, |
| 51 class BufferTracker; |
| 52 |
| 53 V4L2VideoCaptureDelegate( |
| 54 const VideoCaptureDevice::Name& device_name, |
| 55 const scoped_refptr<base::SingleThreadTaskRunner>& v4l2_task_runner, |
| 56 int power_line_frequency); |
| 57 virtual ~V4L2VideoCaptureDelegate(); |
| 58 |
| 59 // Creates the necessary, planarity-specific, internal tracking schemes, |
| 60 virtual scoped_refptr<BufferTracker> CreateBufferTracker() = 0; |
| 61 |
| 62 // Fill in |format| with the given parameters, in a planarity dependent way. |
| 63 virtual bool FillV4L2Format(v4l2_format* format, |
| 64 uint32_t width, |
| 65 uint32_t height, |
| 66 uint32_t pixelformat_fourcc) = 0; |
| 67 |
| 68 // Finish filling |buffer| struct with planarity-dependent data. |
| 69 virtual void FinishFillingV4L2Buffer(v4l2_buffer* buffer) const = 0; |
| 70 |
| 71 // Sends the captured |buffer| to the |client_|, synchronously. |
| 72 virtual void SendBuffer(const scoped_refptr<BufferTracker>& buffer) = 0; |
| 73 |
| 74 // A few accessors for SendBuffer()'s to access private member variables. |
| 75 VideoCaptureFormat capture_format() const { return capture_format_; } |
| 76 VideoCaptureDevice::Client* client() const { return client_.get(); } |
| 77 int rotation() const { return rotation_; } |
| 78 |
| 79 private: |
| 80 friend class base::RefCountedThreadSafe<V4L2VideoCaptureDelegate>; |
| 81 |
| 82 // Request a buffer from V4L2, create and initialize a BufferTracker for it. |
| 83 bool AllocateVideoBuffer(int index); |
| 84 // Fills all common parts of |buffer|. Delegates to FinishFillingV4L2Buffer() |
| 85 // for filling in the planar-dependent parts. |
| 86 void FillV4L2Buffer(v4l2_buffer* buffer, int i) const; |
| 87 void DoCapture(); |
| 88 void SetErrorState(const std::string& reason); |
| 89 |
| 90 const v4l2_buf_type capture_type_; |
| 91 const scoped_refptr<base::SingleThreadTaskRunner> v4l2_task_runner_; |
| 92 const VideoCaptureDevice::Name device_name_; |
| 93 const int power_line_frequency_; |
| 94 |
| 95 // The following members are only known on AllocateAndStart(). |
| 96 VideoCaptureFormat capture_format_; |
| 97 scoped_ptr<VideoCaptureDevice::Client> client_; |
| 98 base::ScopedFD device_fd_; |
| 99 |
| 100 // Vector of BufferTracker to keep track of mmap()ed pointers and their use. |
| 101 std::vector<scoped_refptr<BufferTracker>> buffer_tracker_pool_; |
| 102 |
| 103 bool is_capturing_; |
| 104 int timeout_count_; |
| 105 |
| 106 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270. |
| 107 int rotation_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(V4L2VideoCaptureDelegate); |
| 110 }; |
| 111 |
| 112 } // namespace media |
| 113 |
| 114 #endif // MEDIA_VIDEO_CAPTURE_LINUX_V4L2_VIDEO_CAPTURE_DELEGATE_H_ |
OLD | NEW |