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