blob: c9a7a711861769fa9e4f5ef7f8ecbbf1485576a2 [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
19#ifndef V4L2_CAMERA_H
20#define V4L2_CAMERA_H
21
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070022#include <array>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070023#include <string>
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070024#include <vector>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070025
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070026#include <nativehelper/ScopedFd.h>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070027#include <system/camera_metadata.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070028
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070029#include "ArrayVector.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070030#include "Camera.h"
31#include "Common.h"
32
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 {
39public:
40 V4L2Camera(int id, const std::string path);
41 ~V4L2Camera();
42
43private:
44 // default_camera_hal::Camera virtual methods.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070045 // Connect to the device: open dev nodes, etc.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070046 int connect() override;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070047 // Disconnect from the device: close dev nodes, etc.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070048 void disconnect() override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070049 // Initialize static camera characteristics for individual device.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070050 int initStaticInfo(camera_metadata_t** out) override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070051 // Initialize device info: facing, orientation, resource cost,
52 // and conflicting devices (/conflicting devices length).
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070053 void initDeviceInfo(camera_info_t* info) override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070054 // Initialize whole device (templates/etc) when opened.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070055 int initDevice() override;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070056 // Verify stream configuration is device-compatible.
57 bool isSupportedStreamSet(default_camera_hal::Stream** streams,
58 int count, uint32_t mode) override;
59 // Set up the device for a stream, and get the maximum number of
60 // buffers that stream can handle (max_buffers is an output parameter).
61 int setupStream(default_camera_hal::Stream* stream,
62 uint32_t* max_buffers) override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070063 // Verify settings are valid for a capture with this device.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070064 bool isValidCaptureSettings(const camera_metadata_t* settings) override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070065
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070066 // Helper functions for V4L2 functionality.
67 int setFormat(const default_camera_hal::Stream* stream);
68 int setupBuffers();
69
70 // HAL <-> V4L2 conversions.
71 uint32_t halToV4L2PixelFormat(int hal_format); // 0 for unrecognized.
72 int V4L2ToHalPixelFormat(uint32_t v4l2_format); // -1 for unrecognized.
73
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070074 // The camera device path. For example, /dev/video0.
75 const std::string mDevicePath;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070076 // The opened device fd.
77 ScopedFd mDeviceFd;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070078 // Lock protecting use of the device.
79 android::Mutex mDeviceLock;
80 // Wrapper around ioctl.
81 template<typename T>
82 int ioctlLocked(int request, T data);
83
84 // Current output stream settings.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -070085 uint32_t mOutStreamType;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070086 uint32_t mOutStreamFormat;
87 uint32_t mOutStreamWidth;
88 uint32_t mOutStreamHeight;
89 uint32_t mOutStreamMaxBuffers;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070090
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070091 bool mTemplatesInitialized;
92 int initTemplates();
93
94 // Camera characteristics.
95 bool mCharacteristicsInitialized; // If false, characteristics are invalid.
96 // Fixed characteristics.
97 float mAperture;
98 float mFilterDensity;
99 float mFocalLength;
100 int32_t mOrientation;
101 std::array<float, 2> mPhysicalSize; // {width, height}, in mm.
102 std::array<int32_t, 4> mPixelArraySize; // {xmin, ymin, width, height}.
103 uint8_t mCropType;
104 float mMaxZoom;
105 std::array<int32_t, 2> mAeCompensationRange; // {min, max}.
106 camera_metadata_rational mAeCompensationStep;
107 uint8_t mAeLockAvailable;
108 uint8_t mAwbLockAvailable;
109 uint8_t mFlashAvailable;
110 float mFocusDistance;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700111 int32_t mMaxRawOutputStreams;
112 int32_t mMaxYuvOutputStreams;
113 int32_t mMaxJpegOutputStreams;
114 int32_t mMaxInputStreams;
115 std::vector<int32_t> mSupportedFormats;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700116 // Variable characteristics available options.
117 std::vector<uint8_t> mAeModes;
118 std::vector<uint8_t> mAeAntibandingModes;
119 std::vector<uint8_t> mAfModes;
120 std::vector<uint8_t> mAwbModes;
121 std::vector<uint8_t> mSceneModes;
122 std::vector<uint8_t> mControlModes;
123 std::vector<uint8_t> mEffects;
124 std::vector<uint8_t> mLeds;
125 std::vector<uint8_t> mOpticalStabilizationModes;
126 std::vector<uint8_t> mVideoStabilizationModes;
127 // {format, width, height, direction} (input or output).
128 ArrayVector<int32_t, 4> mStreamConfigs;
129 // {format, width, height, duration} (duration in ns).
130 ArrayVector<int64_t, 4> mMinFrameDurations;
131 int64_t mMaxFrameDuration;
132 // {format, width, height, duration} (duration in ns).
133 ArrayVector<int64_t, 4> mStallDurations;
134 // {min, max} (in fps).
135 ArrayVector<int32_t, 2> mFpsRanges;
136
137 // Initialize characteristics and set mCharacteristicsInitialized to True.
138 int initCharacteristics();
139
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700140 DISALLOW_COPY_AND_ASSIGN(V4L2Camera);
141};
142
143} // namespace v4l2_camera_hal
144
145#endif // V4L2_CAMERA_H