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 // Loosely based on hardware/libhardware/modules/camera/ExampleCamera.h
     18 
     19 #ifndef V4L2_CAMERA_HAL_V4L2_CAMERA_H_
     20 #define V4L2_CAMERA_HAL_V4L2_CAMERA_H_
     21 
     22 #include <array>
     23 #include <condition_variable>
     24 #include <map>
     25 #include <queue>
     26 #include <string>
     27 
     28 #include <camera/CameraMetadata.h>
     29 #include <utils/StrongPointer.h>
     30 #include <utils/Thread.h>
     31 
     32 #include "camera.h"
     33 #include "common.h"
     34 #include "metadata/metadata.h"
     35 #include "v4l2_wrapper.h"
     36 
     37 namespace v4l2_camera_hal {
     38 // V4L2Camera is a specific V4L2-supported camera device. The Camera object
     39 // contains all logic common between all cameras (e.g. front and back cameras),
     40 // while a specific camera device (e.g. V4L2Camera) holds all specific
     41 // metadata and logic about that device.
     42 class V4L2Camera : public default_camera_hal::Camera {
     43  public:
     44   // Use this method to create V4L2Camera objects. Functionally equivalent
     45   // to "new V4L2Camera", except that it may return nullptr in case of failure.
     46   static V4L2Camera* NewV4L2Camera(int id, const std::string path);
     47   ~V4L2Camera();
     48 
     49  private:
     50   // Constructor private to allow failing on bad input.
     51   // Use NewV4L2Camera instead.
     52   V4L2Camera(int id,
     53              std::shared_ptr<V4L2Wrapper> v4l2_wrapper,
     54              std::unique_ptr<Metadata> metadata);
     55 
     56   // default_camera_hal::Camera virtual methods.
     57   // Connect to the device: open dev nodes, etc.
     58   int connect() override;
     59   // Disconnect from the device: close dev nodes, etc.
     60   void disconnect() override;
     61   // Initialize static camera characteristics for individual device.
     62   int initStaticInfo(android::CameraMetadata* out) override;
     63   // Initialize a template of the given type.
     64   int initTemplate(int type, android::CameraMetadata* out) override;
     65   // Initialize device info: resource cost and conflicting devices
     66   // (/conflicting devices length).
     67   void initDeviceInfo(camera_info_t* info) override;
     68   // Extra initialization of device when opened.
     69   int initDevice() override;
     70   // Verify stream configuration dataspaces and rotation values
     71   bool validateDataspacesAndRotations(
     72       const camera3_stream_configuration_t* stream_config) override;
     73   // Set up the streams, including seting usage & max_buffers
     74   int setupStreams(camera3_stream_configuration_t* stream_config) override;
     75   // Verify settings are valid for a capture or reprocessing.
     76   bool isValidRequestSettings(const android::CameraMetadata& settings) override;
     77   // Enqueue a request to receive data from the camera.
     78   int enqueueRequest(
     79       std::shared_ptr<default_camera_hal::CaptureRequest> request) override;
     80   // Flush in flight buffers.
     81   int flushBuffers() override;
     82 
     83   // Async request processing helpers.
     84   // Dequeue a request from the waiting queue.
     85   // Blocks until a request is available.
     86   std::shared_ptr<default_camera_hal::CaptureRequest> dequeueRequest();
     87 
     88   // Thread functions. Return true to loop, false to exit.
     89   // Pass buffers for enqueued requests to the device.
     90   bool enqueueRequestBuffers();
     91   // Retreive buffers from the device.
     92   bool dequeueRequestBuffers();
     93 
     94   // V4L2 helper.
     95   std::shared_ptr<V4L2Wrapper> device_;
     96   std::unique_ptr<V4L2Wrapper::Connection> connection_;
     97   std::unique_ptr<Metadata> metadata_;
     98   std::mutex request_queue_lock_;
     99   std::queue<std::shared_ptr<default_camera_hal::CaptureRequest>>
    100       request_queue_;
    101   std::mutex in_flight_lock_;
    102   // Maps buffer index : request.
    103   std::map<uint32_t, std::shared_ptr<default_camera_hal::CaptureRequest>>
    104       in_flight_;
    105   // Threads require holding an Android strong pointer.
    106   android::sp<android::Thread> buffer_enqueuer_;
    107   android::sp<android::Thread> buffer_dequeuer_;
    108   std::condition_variable requests_available_;
    109   std::condition_variable buffers_in_flight_;
    110 
    111   int32_t max_input_streams_;
    112   std::array<int, 3> max_output_streams_;  // {raw, non-stalling, stalling}.
    113 
    114   DISALLOW_COPY_AND_ASSIGN(V4L2Camera);
    115 };
    116 
    117 }  // namespace v4l2_camera_hal
    118 
    119 #endif  // V4L2_CAMERA_HAL_V4L2_CAMERA_H_
    120