Home | History | Annotate | Download | only in linux
      1 // Copyright (c) 2012 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 // Linux specific implementation of VideoCaptureDevice.
      6 // V4L2 is used for capturing. V4L2 does not provide its own thread for
      7 // capturing so this implementation uses a Chromium thread for fetching frames
      8 // from V4L2.
      9 
     10 #ifndef MEDIA_VIDEO_CAPTURE_LINUX_VIDEO_CAPTURE_DEVICE_LINUX_H_
     11 #define MEDIA_VIDEO_CAPTURE_LINUX_VIDEO_CAPTURE_DEVICE_LINUX_H_
     12 
     13 #include <string>
     14 
     15 #include "base/files/file_util.h"
     16 #include "base/files/scoped_file.h"
     17 #include "base/threading/thread.h"
     18 #include "media/video/capture/video_capture_device.h"
     19 #include "media/video/capture/video_capture_types.h"
     20 
     21 namespace media {
     22 
     23 class VideoCaptureDeviceLinux : public VideoCaptureDevice {
     24  public:
     25   static VideoPixelFormat V4l2ColorToVideoCaptureColorFormat(int32 v4l2_fourcc);
     26   static void GetListOfUsableFourCCs(bool favour_mjpeg,
     27                                      std::list<int>* fourccs);
     28 
     29   explicit VideoCaptureDeviceLinux(const Name& device_name);
     30   virtual ~VideoCaptureDeviceLinux();
     31 
     32   // VideoCaptureDevice implementation.
     33   virtual void AllocateAndStart(const VideoCaptureParams& params,
     34                                 scoped_ptr<Client> client) OVERRIDE;
     35 
     36   virtual void StopAndDeAllocate() OVERRIDE;
     37 
     38  protected:
     39   void SetRotation(int rotation);
     40 
     41   // Once |v4l2_thread_| is started, only called on that thread.
     42   void SetRotationOnV4L2Thread(int rotation);
     43 
     44  private:
     45   enum InternalState {
     46     kIdle,  // The device driver is opened but camera is not in use.
     47     kCapturing,  // Video is being captured.
     48     kError  // Error accessing HW functions.
     49             // User needs to recover by destroying the object.
     50   };
     51 
     52   // Buffers used to receive video frames from with v4l2.
     53   struct Buffer {
     54     Buffer() : start(0), length(0) {}
     55     void* start;
     56     size_t length;
     57   };
     58 
     59   // Called on the v4l2_thread_.
     60   void OnAllocateAndStart(int width,
     61                           int height,
     62                           float frame_rate,
     63                           scoped_ptr<Client> client);
     64   void OnStopAndDeAllocate();
     65   void OnCaptureTask();
     66 
     67   bool AllocateVideoBuffers();
     68   void DeAllocateVideoBuffers();
     69   void SetErrorState(const std::string& reason);
     70 
     71   InternalState state_;
     72   scoped_ptr<VideoCaptureDevice::Client> client_;
     73   Name device_name_;
     74   base::ScopedFD device_fd_;  // File descriptor for the opened camera device.
     75   base::Thread v4l2_thread_;  // Thread used for reading data from the device.
     76   Buffer* buffer_pool_;
     77   int buffer_pool_size_;  // Number of allocated buffers.
     78   int timeout_count_;
     79   VideoCaptureFormat capture_format_;
     80 
     81   // Clockwise rotation in degrees.  This value should be 0, 90, 180, or 270.
     82   // This is only used on |v4l2_thread_| when it is running, or the constructor
     83   // thread otherwise.
     84   int rotation_;
     85 
     86   DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceLinux);
     87 };
     88 
     89 }  // namespace media
     90 
     91 #endif  // MEDIA_VIDEO_CAPTURE_LINUX_VIDEO_CAPTURE_DEVICE_LINUX_H_
     92