1 /*
2  * Copyright (C) 2022 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 HARDWARE_INTERFACES_CAMERA_DEVICE_DEFAULT_EXTERNALCAMERADEVICE_H_
18 #define HARDWARE_INTERFACES_CAMERA_DEVICE_DEFAULT_EXTERNALCAMERADEVICE_H_
19 
20 #include <ExternalCameraDeviceSession.h>
21 #include <ExternalCameraUtils.h>
22 #include <aidl/android/hardware/camera/device/BnCameraDevice.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace camera {
27 namespace device {
28 namespace implementation {
29 
30 using ::aidl::android::hardware::camera::common::CameraResourceCost;
31 using ::aidl::android::hardware::camera::device::BnCameraDevice;
32 using ::aidl::android::hardware::camera::device::CameraMetadata;
33 using ::aidl::android::hardware::camera::device::ICameraDeviceCallback;
34 using ::aidl::android::hardware::camera::device::ICameraDeviceSession;
35 using ::aidl::android::hardware::camera::device::ICameraInjectionSession;
36 using ::aidl::android::hardware::camera::device::StreamConfiguration;
37 using ::android::hardware::camera::external::common::ExternalCameraConfig;
38 
39 class ExternalCameraDevice : public BnCameraDevice {
40   public:
41     // Called by external camera provider HAL.
42     // Provider HAL must ensure the uniqueness of CameraDevice object per cameraId, or there could
43     // be multiple CameraDevice trying to access the same physical camera.  Also, provider will have
44     // to keep track of all CameraDevice objects in order to notify CameraDevice when the underlying
45     // camera is detached.
46     ExternalCameraDevice(const std::string& devicePath, const ExternalCameraConfig& config);
47     ~ExternalCameraDevice() override;
48 
49     ndk::ScopedAStatus getCameraCharacteristics(CameraMetadata* _aidl_return) override;
50     ndk::ScopedAStatus getPhysicalCameraCharacteristics(const std::string& in_physicalCameraId,
51                                                         CameraMetadata* _aidl_return) override;
52     ndk::ScopedAStatus getResourceCost(CameraResourceCost* _aidl_return) override;
53     ndk::ScopedAStatus isStreamCombinationSupported(const StreamConfiguration& in_streams,
54                                                     bool* _aidl_return) override;
55     ndk::ScopedAStatus open(const std::shared_ptr<ICameraDeviceCallback>& in_callback,
56                             std::shared_ptr<ICameraDeviceSession>* _aidl_return) override;
57     ndk::ScopedAStatus openInjectionSession(
58             const std::shared_ptr<ICameraDeviceCallback>& in_callback,
59             std::shared_ptr<ICameraInjectionSession>* _aidl_return) override;
60     ndk::ScopedAStatus setTorchMode(bool in_on) override;
61     ndk::ScopedAStatus turnOnTorchWithStrengthLevel(int32_t in_torchStrength) override;
62     ndk::ScopedAStatus getTorchStrengthLevel(int32_t* _aidl_return) override;
63 
64     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
65 
66     // Caller must use this method to check if CameraDevice ctor failed
67     bool isInitFailed();
68 
69     // Device version to be used by the external camera provider.
70     // Should be of the form <major>.<minor>
71     static std::string kDeviceVersion;
72 
73   private:
74     virtual std::shared_ptr<ExternalCameraDeviceSession> createSession(
75             const std::shared_ptr<ICameraDeviceCallback>&, const ExternalCameraConfig& cfg,
76             const std::vector<SupportedV4L2Format>& sortedFormats, const CroppingType& croppingType,
77             const common::V1_0::helper::CameraMetadata& chars, const std::string& cameraId,
78             unique_fd v4l2Fd);
79 
80     bool isInitFailedLocked();
81 
82     // Init supported w/h/format/fps in mSupportedFormats. Caller still owns fd
83     void initSupportedFormatsLocked(int fd);
84 
85     // Calls into virtual member function. Do not use it in constructor
86     status_t initCameraCharacteristics();
87     // Init available capabilities keys
88     virtual status_t initAvailableCapabilities(
89             ::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
90     // Init non-device dependent keys
91     virtual status_t initDefaultCharsKeys(
92             ::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
93     // Init camera control chars keys. Caller still owns fd
94     status_t initCameraControlsCharsKeys(
95             int fd, ::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
96     // Init camera output configuration related keys.  Caller still owns fd
97     status_t initOutputCharsKeys(
98             int fd, ::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
99 
100     // Helper function for initOutputCharskeys
101     template <size_t SIZE>
102     status_t initOutputCharsKeysByFormat(
103             ::android::hardware::camera::common::V1_0::helper::CameraMetadata* metadata,
104             uint32_t fourcc, const std::array<int, SIZE>& halFormats, int streamConfigTag,
105             int streamConfiguration, int minFrameDuration, int stallDuration);
106 
107     status_t calculateMinFps(::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
108 
109     static void getFrameRateList(int fd, double fpsUpperBound, SupportedV4L2Format* format);
110 
111     static void updateFpsBounds(int fd, CroppingType cropType,
112                                 const std::vector<ExternalCameraConfig::FpsLimitation>& fpsLimits,
113                                 SupportedV4L2Format format,
114                                 std::vector<SupportedV4L2Format>& outFmts);
115 
116     // Get candidate supported formats list of input cropping type.
117     static std::vector<SupportedV4L2Format> getCandidateSupportedFormatsLocked(
118             int fd, CroppingType cropType,
119             const std::vector<ExternalCameraConfig::FpsLimitation>& fpsLimits,
120             const std::vector<ExternalCameraConfig::FpsLimitation>& depthFpsLimits,
121             const Size& minStreamSize, bool depthEnabled);
122     // Trim supported format list by the cropping type. Also sort output formats by width/height
123     static void trimSupportedFormats(CroppingType cropType,
124                                      /*inout*/ std::vector<SupportedV4L2Format>* pFmts);
125 
126     Mutex mLock;
127     bool mInitialized = false;
128     bool mInitFailed = false;
129     std::string mCameraId;
130     std::string mDevicePath;
131     const ExternalCameraConfig& mCfg;
132     std::vector<SupportedV4L2Format> mSupportedFormats;
133     CroppingType mCroppingType;
134 
135     std::weak_ptr<ExternalCameraDeviceSession> mSession =
136             std::weak_ptr<ExternalCameraDeviceSession>();
137 
138     ::android::hardware::camera::common::V1_0::helper::CameraMetadata mCameraCharacteristics;
139 
140     const std::vector<int32_t> AVAILABLE_CHARACTERISTICS_KEYS = {
141             ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
142             ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
143             ANDROID_CONTROL_AE_AVAILABLE_MODES,
144             ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
145             ANDROID_CONTROL_AE_COMPENSATION_RANGE,
146             ANDROID_CONTROL_AE_COMPENSATION_STEP,
147             ANDROID_CONTROL_AE_LOCK_AVAILABLE,
148             ANDROID_CONTROL_AF_AVAILABLE_MODES,
149             ANDROID_CONTROL_AVAILABLE_EFFECTS,
150             ANDROID_CONTROL_AVAILABLE_MODES,
151             ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
152             ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
153             ANDROID_CONTROL_AWB_AVAILABLE_MODES,
154             ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
155             ANDROID_CONTROL_MAX_REGIONS,
156             ANDROID_FLASH_INFO_AVAILABLE,
157             ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
158             ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
159             ANDROID_LENS_FACING,
160             ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
161             ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
162             ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
163             ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
164             ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
165             ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
166             ANDROID_REQUEST_PARTIAL_RESULT_COUNT,
167             ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
168             ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
169             ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
170             ANDROID_SCALER_CROPPING_TYPE,
171             ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
172             ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
173             ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
174             ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE,
175             ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
176             ANDROID_SENSOR_ORIENTATION,
177             ANDROID_SHADING_AVAILABLE_MODES,
178             ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
179             ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
180             ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
181             ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
182             ANDROID_SYNC_MAX_LATENCY};
183 };
184 
185 }  // namespace implementation
186 }  // namespace device
187 }  // namespace camera
188 }  // namespace hardware
189 }  // namespace android
190 
191 #endif  // HARDWARE_INTERFACES_CAMERA_DEVICE_DEFAULT_EXTERNALCAMERADEVICE_H_
192