Home | History | Annotate | Download | only in 3_4
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef V4L2_CAMERA_HAL_V4L2_WRAPPER_H_
     18 #define V4L2_CAMERA_HAL_V4L2_WRAPPER_H_
     19 
     20 #include <array>
     21 #include <memory>
     22 #include <mutex>
     23 #include <set>
     24 #include <string>
     25 #include <vector>
     26 
     27 #include <android-base/unique_fd.h>
     28 
     29 #include "common.h"
     30 #include "stream_format.h"
     31 #include "v4l2_gralloc.h"
     32 
     33 namespace v4l2_camera_hal {
     34 class V4L2Wrapper {
     35  public:
     36   // Use this method to create V4L2Wrapper objects. Functionally equivalent
     37   // to "new V4L2Wrapper", except that it may return nullptr in case of failure.
     38   static V4L2Wrapper* NewV4L2Wrapper(const std::string device_path);
     39   virtual ~V4L2Wrapper();
     40 
     41   // Helper class to ensure all opened connections are closed.
     42   class Connection {
     43    public:
     44     Connection(std::shared_ptr<V4L2Wrapper> device)
     45         : device_(std::move(device)), connect_result_(device_->Connect()) {}
     46     ~Connection() {
     47       if (connect_result_ == 0) {
     48         device_->Disconnect();
     49       }
     50     }
     51     // Check whether the connection succeeded or not.
     52     inline int status() const { return connect_result_; }
     53 
     54    private:
     55     std::shared_ptr<V4L2Wrapper> device_;
     56     const int connect_result_;
     57   };
     58 
     59   // Turn the stream on or off.
     60   virtual int StreamOn();
     61   virtual int StreamOff();
     62   // Manage controls.
     63   virtual int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result);
     64   virtual int GetControl(uint32_t control_id, int32_t* value);
     65   virtual int SetControl(uint32_t control_id,
     66                          int32_t desired,
     67                          int32_t* result = nullptr);
     68   // Manage format.
     69   virtual int GetFormats(std::set<uint32_t>* v4l2_formats);
     70   virtual int GetFormatFrameSizes(uint32_t v4l2_format,
     71                                   std::set<std::array<int32_t, 2>>* sizes);
     72   // Durations are returned in ns.
     73   virtual int GetFormatFrameDurationRange(
     74       uint32_t v4l2_format,
     75       const std::array<int32_t, 2>& size,
     76       std::array<int64_t, 2>* duration_range);
     77   virtual int SetFormat(const StreamFormat& desired_format,
     78                         uint32_t* result_max_buffers);
     79   // Manage buffers.
     80   virtual int EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer,
     81                             uint32_t* enqueued_index = nullptr);
     82   virtual int DequeueBuffer(uint32_t* dequeued_index = nullptr);
     83 
     84  private:
     85   // Constructor is private to allow failing on bad input.
     86   // Use NewV4L2Wrapper instead.
     87   V4L2Wrapper(const std::string device_path,
     88               std::unique_ptr<V4L2Gralloc> gralloc);
     89 
     90   // Connect or disconnect to the device. Access by creating/destroying
     91   // a V4L2Wrapper::Connection object.
     92   int Connect();
     93   void Disconnect();
     94   // Perform an ioctl call in a thread-safe fashion.
     95   template <typename T>
     96   int IoctlLocked(int request, T data);
     97   // Request/release userspace buffer mode via VIDIOC_REQBUFS.
     98   int RequestBuffers(uint32_t num_buffers);
     99 
    100   inline bool connected() { return device_fd_.get() >= 0; }
    101 
    102   // The camera device path. For example, /dev/video0.
    103   const std::string device_path_;
    104   // The opened device fd.
    105   android::base::unique_fd device_fd_;
    106   // The underlying gralloc module.
    107   std::unique_ptr<V4L2Gralloc> gralloc_;
    108   // Whether or not the device supports the extended control query.
    109   bool extended_query_supported_;
    110   // The format this device is set up for.
    111   std::unique_ptr<StreamFormat> format_;
    112   // Map indecies to buffer status. True if the index is in-flight.
    113   // |buffers_.size()| will always be the maximum number of buffers this device
    114   // can handle in its current format.
    115   std::vector<bool> buffers_;
    116   // Lock protecting use of the buffer tracker.
    117   std::mutex buffer_queue_lock_;
    118   // Lock protecting use of the device.
    119   std::mutex device_lock_;
    120   // Lock protecting connecting/disconnecting the device.
    121   std::mutex connection_lock_;
    122   // Reference count connections.
    123   int connection_count_;
    124 
    125   friend class Connection;
    126   friend class V4L2WrapperMock;
    127 
    128   DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
    129 };
    130 
    131 }  // namespace v4l2_camera_hal
    132 
    133 #endif  // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_
    134