blob: b2bdd06cc67e1d981bb139e8186c41e90cc5795c [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
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// #define LOG_NDEBUG 0
18#define LOG_TAG "VirtualCameraProvider"
19#include "VirtualCameraProvider.h"
20
21#include <atomic>
22#include <memory>
23#include <mutex>
24#include <tuple>
25#include <utility>
26
27#include "VirtualCameraDevice.h"
28#include "aidl/android/hardware/camera/common/Status.h"
29#include "log/log.h"
30#include "util/Util.h"
31
32namespace android {
33namespace companion {
34namespace virtualcamera {
35
36using ::aidl::android::companion::virtualcamera::IVirtualCameraCallback;
37using ::aidl::android::hardware::camera::common::CameraDeviceStatus;
38using ::aidl::android::hardware::camera::common::Status;
39using ::aidl::android::hardware::camera::common::VendorTagSection;
40using ::aidl::android::hardware::camera::device::ICameraDevice;
41using ::aidl::android::hardware::camera::provider::CameraIdAndStreamCombination;
42using ::aidl::android::hardware::camera::provider::ConcurrentCameraIdCombination;
43using ::aidl::android::hardware::camera::provider::ICameraProviderCallback;
44
45// TODO(b/301023410) Make camera id range configurable / dynamic
46// based on already registered devices.
47std::atomic_int VirtualCameraProvider::sNextId{42};
48
49ndk::ScopedAStatus VirtualCameraProvider::setCallback(
50 const std::shared_ptr<ICameraProviderCallback>& in_callback) {
51 ALOGV("%s", __func__);
52
53 if (in_callback == nullptr) {
54 return cameraStatus(Status::ILLEGAL_ARGUMENT);
55 }
56
57 {
58 const std::lock_guard<std::mutex> lock(mLock);
59 mCameraProviderCallback = in_callback;
60
61 for (const auto& [cameraName, _] : mCameras) {
62 auto ret = mCameraProviderCallback->cameraDeviceStatusChange(
63 cameraName, CameraDeviceStatus::PRESENT);
64 if (!ret.isOk()) {
65 ALOGE("Failed to announce camera status change: %s",
66 ret.getDescription().c_str());
67 }
68 }
69 }
70 return ndk::ScopedAStatus::ok();
71}
72
73ndk::ScopedAStatus VirtualCameraProvider::getVendorTags(
74 std::vector<VendorTagSection>* _aidl_return) {
75 ALOGV("%s", __func__);
76
77 if (_aidl_return == nullptr) {
78 return cameraStatus(Status::ILLEGAL_ARGUMENT);
79 }
80
81 // No vendor tags supported.
82 _aidl_return->clear();
83 return ndk::ScopedAStatus::ok();
84}
85
86ndk::ScopedAStatus VirtualCameraProvider::getCameraIdList(
87 std::vector<std::string>* _aidl_return) {
88 ALOGV("%s", __func__);
89
90 if (_aidl_return == nullptr) {
91 return cameraStatus(Status::ILLEGAL_ARGUMENT);
92 }
93
94 {
95 const std::lock_guard<std::mutex> lock(mLock);
96 _aidl_return->clear();
97 _aidl_return->reserve(mCameras.size());
98 for (const auto& [cameraName, _] : mCameras) {
99 _aidl_return->emplace_back(cameraName);
100 }
101 }
102 return ndk::ScopedAStatus::ok();
103}
104
105ndk::ScopedAStatus VirtualCameraProvider::getCameraDeviceInterface(
106 const std::string& in_cameraDeviceName,
107 std::shared_ptr<ICameraDevice>* _aidl_return) {
108 ALOGV("%s cameraDeviceName %s", __func__, in_cameraDeviceName.c_str());
109
110 if (_aidl_return == nullptr) {
111 return cameraStatus(Status::ILLEGAL_ARGUMENT);
112 }
113
114 {
115 const std::lock_guard<std::mutex> lock(mLock);
116 const auto it = mCameras.find(in_cameraDeviceName);
117 *_aidl_return = (it == mCameras.end()) ? nullptr : it->second;
118 }
119
120 return ndk::ScopedAStatus::ok();
121}
122
123ndk::ScopedAStatus VirtualCameraProvider::notifyDeviceStateChange(
124 int64_t in_deviceState) {
125 ALOGV("%s", __func__);
126 (void)in_deviceState;
127 return ndk::ScopedAStatus::ok();
128}
129
130ndk::ScopedAStatus VirtualCameraProvider::getConcurrentCameraIds(
131 std::vector<ConcurrentCameraIdCombination>* _aidl_return) {
132 ALOGV("%s", __func__);
133 if (_aidl_return == nullptr) {
134 return cameraStatus(Status::ILLEGAL_ARGUMENT);
135 }
136
137 // No support for any concurrent combination.
138 _aidl_return->clear();
139 return ndk::ScopedAStatus::ok();
140}
141
142ndk::ScopedAStatus VirtualCameraProvider::isConcurrentStreamCombinationSupported(
143 const std::vector<CameraIdAndStreamCombination>& in_configs,
144 bool* _aidl_return) {
145 ALOGV("%s", __func__);
146 (void)in_configs;
147 if (_aidl_return == nullptr) {
148 return cameraStatus(Status::ILLEGAL_ARGUMENT);
149 }
150
151 // No support for any stream combination at the moment.
152 *_aidl_return = false;
153 return ndk::ScopedAStatus::ok();
154}
155
156std::shared_ptr<VirtualCameraDevice> VirtualCameraProvider::createCamera(
157 std::shared_ptr<IVirtualCameraCallback> virtualCameraClientCallback) {
158 auto camera = ndk::SharedRefBase::make<VirtualCameraDevice>(
159 sNextId++, virtualCameraClientCallback);
160 std::shared_ptr<ICameraProviderCallback> callback;
161 {
162 const std::lock_guard<std::mutex> lock(mLock);
163 if (mCameras.find(camera->getCameraName()) != mCameras.end()) {
164 ALOGE("Camera with identical name already exists.");
165 return nullptr;
166 }
167 mCameras.emplace(std::piecewise_construct,
168 std::forward_as_tuple(camera->getCameraName()),
169 std::forward_as_tuple(camera));
170 callback = mCameraProviderCallback;
171 }
172
173 if (callback != nullptr) {
174 auto ret = callback->cameraDeviceStatusChange(camera->getCameraName(),
175 CameraDeviceStatus::PRESENT);
176 if (!ret.isOk()) {
177 ALOGE("Failed to announce camera %s status change (PRESENT): %s",
178 camera->getCameraName().c_str(), ret.getDescription().c_str());
179 }
180 }
181 return camera;
182}
183
184std::shared_ptr<VirtualCameraDevice> VirtualCameraProvider::getCamera(
185 const std::string& cameraName) {
186 const std::lock_guard<std::mutex> lock(mLock);
187 auto it = mCameras.find(cameraName);
188 return it == mCameras.end() ? nullptr : it->second;
189}
190
191bool VirtualCameraProvider::removeCamera(const std::string& name) {
192 std::shared_ptr<ICameraProviderCallback> callback;
193 {
194 const std::lock_guard<std::mutex> lock(mLock);
195 auto it = mCameras.find(name);
196 if (it == mCameras.end()) {
197 ALOGE("Cannot remove camera %s: no such camera", name.c_str());
198 return false;
199 }
200 // TODO(b/301023410) Gracefully shut down camera.
201 mCameras.erase(it);
202 callback = mCameraProviderCallback;
203 }
204
205 if (callback != nullptr) {
206 auto ret = callback->cameraDeviceStatusChange(
207 name, CameraDeviceStatus::NOT_PRESENT);
208 if (!ret.isOk()) {
209 ALOGE("Failed to announce camera %s status change (NOT_PRESENT): %s",
210 name.c_str(), ret.getDescription().c_str());
211 }
212 }
213
214 return true;
215}
216
217} // namespace virtualcamera
218} // namespace companion
219} // namespace android