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_GOOGLE_CAMERA_HAL_AIDL_SERVICE_AIDL_CAMERA_PROVIDER_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_AIDL_SERVICE_AIDL_CAMERA_PROVIDER_H_
19 
20 #include <aidl/android/hardware/camera/provider/BnCameraProvider.h>
21 #include <aidl/android/hardware/camera/provider/ICameraProviderCallback.h>
22 
23 #include <regex>
24 #include <string>
25 #ifdef __ANDROID_APEX__
26 #include <unordered_set>
27 #endif
28 
29 #include "camera_provider.h"
30 
31 namespace android {
32 namespace hardware {
33 namespace camera {
34 namespace provider {
35 namespace implementation {
36 
37 using aidl::android::hardware::camera::common::VendorTagSection;
38 using aidl::android::hardware::camera::device::ICameraDevice;
39 using aidl::android::hardware::camera::provider::BnCameraProvider;
40 using aidl::android::hardware::camera::provider::CameraIdAndStreamCombination;
41 using aidl::android::hardware::camera::provider::ConcurrentCameraIdCombination;
42 using aidl::android::hardware::camera::provider::ICameraProviderCallback;
43 
44 using ::android::google_camera_hal::CameraProvider;
45 using ndk::ScopedAStatus;
46 
47 // AidlCameraProvider implements the AIDL camera provider interface,
48 // ICameraProvider, to enumerate the available individual camera devices
49 // in the system, and provide updates about changes to device status.
50 class AidlCameraProvider : public BnCameraProvider {
51  public:
52   static const std::string kProviderName;
53   static std::shared_ptr<AidlCameraProvider> Create();
54   virtual ~AidlCameraProvider() = default;
55 
56   // Override functions in ICameraProvider.
57 
58   ScopedAStatus setCallback(
59       const std::shared_ptr<ICameraProviderCallback>& callback) override;
60 
61   ScopedAStatus getVendorTags(std::vector<VendorTagSection>* vts) override;
62 
63   ScopedAStatus getCameraIdList(std::vector<std::string>* camera_ids) override;
64 
65   ScopedAStatus getCameraDeviceInterface(
66       const std::string& in_cameraDeviceName,
67       std::shared_ptr<ICameraDevice>* device) override;
68 
69   ScopedAStatus notifyDeviceStateChange(int64_t in_deviceState) override;
70 
71   ScopedAStatus getConcurrentCameraIds(
72       std::vector<ConcurrentCameraIdCombination>* concurrent_camera_ids) override;
73 
74   ScopedAStatus isConcurrentStreamCombinationSupported(
75       const std::vector<CameraIdAndStreamCombination>& in_configs,
76       bool* support) override;
77 
78   // End of override functions in ICameraProvider.
79   AidlCameraProvider() = default;
80 
81  private:
82   static const std::regex kDeviceNameRegex;
83 
84   status_t Initialize();
85 
86   // Parse device version and camera ID.
87   bool ParseDeviceName(const std::string& device_name,
88                        std::string* device_version, std::string* camera_id);
89 
90   std::mutex callbacks_lock_;
91   std::shared_ptr<ICameraProviderCallback> callbacks_;
92 
93   std::unique_ptr<CameraProvider> google_camera_provider_;
94   google_camera_hal::CameraProviderCallback camera_provider_callback_;
95 
96 #ifdef __ANDROID_APEX__
97   std::unordered_set<uint32_t> available_camera_ids_;
98   bool camera_device_initialized_ = false;
99 #endif
100 };
101 
102 }  // namespace implementation
103 }  // namespace provider
104 }  // namespace camera
105 }  // namespace hardware
106 }  // namespace android
107 
108 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_AIDL_SERVICE_AIDL_CAMERA_PROVIDER_H_
109