blob: eed3e8547b9782ef6725c8da752579904d106063 [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
Biswarup Pal6152a302023-12-19 12:44:09 +00002 * Copyright 2023 The Android Open Source Project
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01003 *
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
Biswarup Pal6152a302023-12-19 12:44:09 +000036using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010037using ::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
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010045ndk::ScopedAStatus VirtualCameraProvider::setCallback(
46 const std::shared_ptr<ICameraProviderCallback>& in_callback) {
47 ALOGV("%s", __func__);
48
49 if (in_callback == nullptr) {
50 return cameraStatus(Status::ILLEGAL_ARGUMENT);
51 }
52
53 {
54 const std::lock_guard<std::mutex> lock(mLock);
55 mCameraProviderCallback = in_callback;
56
57 for (const auto& [cameraName, _] : mCameras) {
58 auto ret = mCameraProviderCallback->cameraDeviceStatusChange(
59 cameraName, CameraDeviceStatus::PRESENT);
60 if (!ret.isOk()) {
61 ALOGE("Failed to announce camera status change: %s",
62 ret.getDescription().c_str());
63 }
64 }
65 }
66 return ndk::ScopedAStatus::ok();
67}
68
69ndk::ScopedAStatus VirtualCameraProvider::getVendorTags(
70 std::vector<VendorTagSection>* _aidl_return) {
71 ALOGV("%s", __func__);
72
73 if (_aidl_return == nullptr) {
74 return cameraStatus(Status::ILLEGAL_ARGUMENT);
75 }
76
77 // No vendor tags supported.
78 _aidl_return->clear();
79 return ndk::ScopedAStatus::ok();
80}
81
82ndk::ScopedAStatus VirtualCameraProvider::getCameraIdList(
83 std::vector<std::string>* _aidl_return) {
84 ALOGV("%s", __func__);
85
86 if (_aidl_return == nullptr) {
87 return cameraStatus(Status::ILLEGAL_ARGUMENT);
88 }
89
90 {
91 const std::lock_guard<std::mutex> lock(mLock);
92 _aidl_return->clear();
93 _aidl_return->reserve(mCameras.size());
94 for (const auto& [cameraName, _] : mCameras) {
95 _aidl_return->emplace_back(cameraName);
96 }
97 }
98 return ndk::ScopedAStatus::ok();
99}
100
101ndk::ScopedAStatus VirtualCameraProvider::getCameraDeviceInterface(
102 const std::string& in_cameraDeviceName,
103 std::shared_ptr<ICameraDevice>* _aidl_return) {
104 ALOGV("%s cameraDeviceName %s", __func__, in_cameraDeviceName.c_str());
105
106 if (_aidl_return == nullptr) {
107 return cameraStatus(Status::ILLEGAL_ARGUMENT);
108 }
109
110 {
111 const std::lock_guard<std::mutex> lock(mLock);
112 const auto it = mCameras.find(in_cameraDeviceName);
113 *_aidl_return = (it == mCameras.end()) ? nullptr : it->second;
114 }
115
116 return ndk::ScopedAStatus::ok();
117}
118
119ndk::ScopedAStatus VirtualCameraProvider::notifyDeviceStateChange(
120 int64_t in_deviceState) {
121 ALOGV("%s", __func__);
122 (void)in_deviceState;
123 return ndk::ScopedAStatus::ok();
124}
125
126ndk::ScopedAStatus VirtualCameraProvider::getConcurrentCameraIds(
127 std::vector<ConcurrentCameraIdCombination>* _aidl_return) {
128 ALOGV("%s", __func__);
129 if (_aidl_return == nullptr) {
130 return cameraStatus(Status::ILLEGAL_ARGUMENT);
131 }
132
133 // No support for any concurrent combination.
134 _aidl_return->clear();
135 return ndk::ScopedAStatus::ok();
136}
137
138ndk::ScopedAStatus VirtualCameraProvider::isConcurrentStreamCombinationSupported(
139 const std::vector<CameraIdAndStreamCombination>& in_configs,
140 bool* _aidl_return) {
141 ALOGV("%s", __func__);
142 (void)in_configs;
143 if (_aidl_return == nullptr) {
144 return cameraStatus(Status::ILLEGAL_ARGUMENT);
145 }
146
147 // No support for any stream combination at the moment.
148 *_aidl_return = false;
149 return ndk::ScopedAStatus::ok();
150}
151
152std::shared_ptr<VirtualCameraDevice> VirtualCameraProvider::createCamera(
Marvin Ramina8196132024-03-15 15:55:22 +0000153 const VirtualCameraConfiguration& configuration, const int cameraId) {
154 if (cameraId < 0) {
155 ALOGE("%s: Cannot create camera with negative id. cameraId: %d", __func__,
156 cameraId);
157 return nullptr;
158 }
159
Biswarup Pal6152a302023-12-19 12:44:09 +0000160 auto camera =
Marvin Ramina8196132024-03-15 15:55:22 +0000161 ndk::SharedRefBase::make<VirtualCameraDevice>(cameraId, configuration);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100162 std::shared_ptr<ICameraProviderCallback> callback;
163 {
164 const std::lock_guard<std::mutex> lock(mLock);
165 if (mCameras.find(camera->getCameraName()) != mCameras.end()) {
166 ALOGE("Camera with identical name already exists.");
167 return nullptr;
168 }
169 mCameras.emplace(std::piecewise_construct,
170 std::forward_as_tuple(camera->getCameraName()),
171 std::forward_as_tuple(camera));
172 callback = mCameraProviderCallback;
173 }
174
175 if (callback != nullptr) {
176 auto ret = callback->cameraDeviceStatusChange(camera->getCameraName(),
177 CameraDeviceStatus::PRESENT);
178 if (!ret.isOk()) {
179 ALOGE("Failed to announce camera %s status change (PRESENT): %s",
180 camera->getCameraName().c_str(), ret.getDescription().c_str());
181 }
182 }
183 return camera;
184}
185
186std::shared_ptr<VirtualCameraDevice> VirtualCameraProvider::getCamera(
187 const std::string& cameraName) {
188 const std::lock_guard<std::mutex> lock(mLock);
189 auto it = mCameras.find(cameraName);
190 return it == mCameras.end() ? nullptr : it->second;
191}
192
193bool VirtualCameraProvider::removeCamera(const std::string& name) {
194 std::shared_ptr<ICameraProviderCallback> callback;
195 {
196 const std::lock_guard<std::mutex> lock(mLock);
197 auto it = mCameras.find(name);
198 if (it == mCameras.end()) {
199 ALOGE("Cannot remove camera %s: no such camera", name.c_str());
200 return false;
201 }
202 // TODO(b/301023410) Gracefully shut down camera.
203 mCameras.erase(it);
204 callback = mCameraProviderCallback;
205 }
206
207 if (callback != nullptr) {
208 auto ret = callback->cameraDeviceStatusChange(
209 name, CameraDeviceStatus::NOT_PRESENT);
210 if (!ret.isOk()) {
211 ALOGE("Failed to announce camera %s status change (NOT_PRESENT): %s",
212 name.c_str(), ret.getDescription().c_str());
213 }
214 }
215
216 return true;
217}
218
219} // namespace virtualcamera
220} // namespace companion
221} // namespace android