1 /*
2  * Copyright (C) 2023 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 #include <charconv>
18 
19 #include <inttypes.h>
20 
21 #include <log/log.h>
22 
23 #include "CameraProvider.h"
24 #include "CameraDevice.h"
25 #include "HwCamera.h"
26 #include "debug.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace camera {
31 namespace provider {
32 namespace implementation {
33 namespace {
34 constexpr char kCameraIdPrefix[] = "device@1.1/internal/";
35 
getLogicalCameraId(const int index)36 std::string getLogicalCameraId(const int index) {
37     char buf[sizeof(kCameraIdPrefix) + 8];
38     snprintf(buf, sizeof(buf), "%s%d", kCameraIdPrefix, index);
39     return buf;
40 }
41 
parseLogicalCameraId(const std::string_view str)42 std::optional<int> parseLogicalCameraId(const std::string_view str) {
43     if (str.size() < sizeof(kCameraIdPrefix)) {
44         return FAILURE(std::nullopt);
45     }
46 
47     if (memcmp(str.data(), kCameraIdPrefix, sizeof(kCameraIdPrefix) - 1) != 0) {
48         return FAILURE(std::nullopt);
49     }
50 
51     int index;
52     const auto r = std::from_chars(&str[sizeof(kCameraIdPrefix) - 1],
53                                    &*str.end(), index, 10);
54     if (r.ec == std::errc()) {
55         return index;
56     } else {
57         return FAILURE(std::nullopt);
58     }
59 }
60 }  // namespace
61 
62 using aidl::android::hardware::camera::common::Status;
63 
CameraProvider(const int deviceIdBase,Span<const hw::HwCameraFactory> availableCameras)64 CameraProvider::CameraProvider(const int deviceIdBase,
65                                Span<const hw::HwCameraFactory> availableCameras)
66         : mDeviceIdBase(deviceIdBase)
67         , mAvailableCameras(availableCameras) {}
68 
~CameraProvider()69 CameraProvider::~CameraProvider() {}
70 
setCallback(const std::shared_ptr<ICameraProviderCallback> & callback)71 ScopedAStatus CameraProvider::setCallback(
72         const std::shared_ptr<ICameraProviderCallback>& callback) {
73     mCallback = callback;
74     return ScopedAStatus::ok();
75 }
76 
getVendorTags(std::vector<VendorTagSection> * vts)77 ScopedAStatus CameraProvider::getVendorTags(std::vector<VendorTagSection>* vts) {
78     *vts = {};
79     return ScopedAStatus::ok();
80 }
81 
getCameraIdList(std::vector<std::string> * camera_ids)82 ScopedAStatus CameraProvider::getCameraIdList(std::vector<std::string>* camera_ids) {
83     camera_ids->reserve(mAvailableCameras.size());
84 
85     for (int i = 0; i < mAvailableCameras.size(); ++i) {
86         camera_ids->push_back(getLogicalCameraId(mDeviceIdBase + i));
87     }
88 
89     return ScopedAStatus::ok();
90 }
91 
getCameraDeviceInterface(const std::string & name,std::shared_ptr<ICameraDevice> * device)92 ScopedAStatus CameraProvider::getCameraDeviceInterface(
93         const std::string& name,
94         std::shared_ptr<ICameraDevice>* device) {
95     const std::optional<int> maybeIndex = parseLogicalCameraId(name);
96     if (!maybeIndex) {
97         return toScopedAStatus(FAILURE(Status::ILLEGAL_ARGUMENT));
98     }
99 
100     const int index = maybeIndex.value() - mDeviceIdBase;
101     if ((index >= 0) && (index < mAvailableCameras.size())) {
102         auto hwCamera = mAvailableCameras[index]();
103 
104         if (hwCamera) {
105             auto p = ndk::SharedRefBase::make<CameraDevice>(std::move(hwCamera));
106             p->mSelf = p;
107             *device = std::move(p);
108             return ScopedAStatus::ok();
109         } else {
110             return toScopedAStatus(FAILURE(Status::INTERNAL_ERROR));
111         }
112     } else {
113         return toScopedAStatus(FAILURE(Status::ILLEGAL_ARGUMENT));
114     }
115 }
116 
notifyDeviceStateChange(const int64_t)117 ScopedAStatus CameraProvider::notifyDeviceStateChange(const int64_t /*deviceState*/) {
118     return ScopedAStatus::ok();
119 }
120 
getConcurrentCameraIds(std::vector<ConcurrentCameraIdCombination> * concurrentCameraIds)121 ScopedAStatus CameraProvider::getConcurrentCameraIds(
122         std::vector<ConcurrentCameraIdCombination>* concurrentCameraIds) {
123     *concurrentCameraIds = {};
124     return ScopedAStatus::ok();
125 }
126 
isConcurrentStreamCombinationSupported(const std::vector<CameraIdAndStreamCombination> &,bool * support)127 ScopedAStatus CameraProvider::isConcurrentStreamCombinationSupported(
128         const std::vector<CameraIdAndStreamCombination>& /*configs*/,
129         bool* support) {
130     *support = false;
131     return ScopedAStatus::ok();
132 }
133 
134 }  // namespace implementation
135 }  // namespace provider
136 }  // namespace camera
137 }  // namespace hardware
138 }  // namespace android
139