blob: d299b9f70f59a87f0e66c03d7f05dd87ab39fb03 [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
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
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070019#ifndef V4L2_CAMERA_HAL_V4L2_CAMERA_H_
20#define V4L2_CAMERA_HAL_V4L2_CAMERA_H_
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070021
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070022#include <array>
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070023#include <queue>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070024#include <string>
25
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070026#include <camera/CameraMetadata.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070027
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070028#include "camera.h"
29#include "common.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070030#include "metadata/metadata.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070031#include "v4l2_wrapper.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070032
33namespace v4l2_camera_hal {
34// V4L2Camera is a specific V4L2-supported camera device. The Camera object
35// contains all logic common between all cameras (e.g. front and back cameras),
36// while a specific camera device (e.g. V4L2Camera) holds all specific
37// metadata and logic about that device.
38class V4L2Camera : public default_camera_hal::Camera {
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070039 public:
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070040 // Use this method to create V4L2Camera objects. Functionally equivalent
41 // to "new V4L2Camera", except that it may return nullptr in case of failure.
42 static V4L2Camera* NewV4L2Camera(int id, const std::string path);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070043 ~V4L2Camera();
44
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070045 private:
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070046 // Constructor private to allow failing on bad input.
47 // Use NewV4L2Camera instead.
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070048 V4L2Camera(int id,
49 std::shared_ptr<V4L2Wrapper> v4l2_wrapper,
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070050 std::unique_ptr<Metadata> metadata);
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070051
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070052 // default_camera_hal::Camera virtual methods.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070053 // Connect to the device: open dev nodes, etc.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070054 int connect() override;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070055 // Disconnect from the device: close dev nodes, etc.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070056 void disconnect() override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070057 // Initialize static camera characteristics for individual device.
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070058 int initStaticInfo(android::CameraMetadata* out) override;
59 // Initialize a template of the given type.
60 int initTemplate(int type, android::CameraMetadata* out) override;
61 // Initialize device info: resource cost and conflicting devices
62 // (/conflicting devices length).
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070063 void initDeviceInfo(camera_info_t* info) override;
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070064 // Extra initialization of device when opened.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070065 int initDevice() override;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070066 // Verify stream configuration is device-compatible.
67 bool isSupportedStreamSet(default_camera_hal::Stream** streams,
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070068 int count,
69 uint32_t mode) override;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070070 // Set up the device for a stream, and get the maximum number of
71 // buffers that stream can handle (max_buffers is an output parameter).
72 int setupStream(default_camera_hal::Stream* stream,
73 uint32_t* max_buffers) override;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070074 // Verify settings are valid for a capture or reprocessing.
75 bool isValidRequest(
76 const default_camera_hal::CaptureRequest& request) 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
81 // Async request processing.
82 // Dequeue a request from the waiting queue.
83 std::shared_ptr<default_camera_hal::CaptureRequest> dequeueRequest();
84 // Pass buffers for enqueued requests to the device.
85 void enqueueRequestBuffers();
86 // Retreive buffers from the device.
87 void dequeueRequestBuffers();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070088
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070089 // V4L2 helper.
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070090 std::shared_ptr<V4L2Wrapper> device_;
91 std::unique_ptr<V4L2Wrapper::Connection> connection_;
92 std::unique_ptr<Metadata> metadata_;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070093 std::mutex request_queue_lock_;
94 std::queue<std::shared_ptr<default_camera_hal::CaptureRequest>>
95 request_queue_;
96 std::mutex in_flight_lock_;
97 std::queue<std::shared_ptr<default_camera_hal::CaptureRequest>> in_flight_;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070098
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070099 int32_t max_input_streams_;
100 std::array<int, 3> max_output_streams_; // {raw, non-stalling, stalling}.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700101
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700102 DISALLOW_COPY_AND_ASSIGN(V4L2Camera);
103};
104
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700105} // namespace v4l2_camera_hal
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700106
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700107#endif // V4L2_CAMERA_HAL_V4L2_CAMERA_H