blob: 67eaec008b54d67bb996e3af6aee607ad58e3a86 [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(
Biswarup Pal37a75182024-01-16 15:53:35 +0000153 const VirtualCameraConfiguration& configuration, const int cameraId,
154 const int32_t deviceId) {
Marvin Ramina8196132024-03-15 15:55:22 +0000155 if (cameraId < 0) {
156 ALOGE("%s: Cannot create camera with negative id. cameraId: %d", __func__,
157 cameraId);
158 return nullptr;
159 }
160
Biswarup Pal37a75182024-01-16 15:53:35 +0000161 auto camera = ndk::SharedRefBase::make<VirtualCameraDevice>(
162 cameraId, configuration, deviceId);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100163 std::shared_ptr<ICameraProviderCallback> callback;
164 {
165 const std::lock_guard<std::mutex> lock(mLock);
166 if (mCameras.find(camera->getCameraName()) != mCameras.end()) {
167 ALOGE("Camera with identical name already exists.");
168 return nullptr;
169 }
170 mCameras.emplace(std::piecewise_construct,
171 std::forward_as_tuple(camera->getCameraName()),
172 std::forward_as_tuple(camera));
173 callback = mCameraProviderCallback;
174 }
175
176 if (callback != nullptr) {
177 auto ret = callback->cameraDeviceStatusChange(camera->getCameraName(),
178 CameraDeviceStatus::PRESENT);
179 if (!ret.isOk()) {
180 ALOGE("Failed to announce camera %s status change (PRESENT): %s",
181 camera->getCameraName().c_str(), ret.getDescription().c_str());
182 }
183 }
184 return camera;
185}
186
187std::shared_ptr<VirtualCameraDevice> VirtualCameraProvider::getCamera(
188 const std::string& cameraName) {
189 const std::lock_guard<std::mutex> lock(mLock);
190 auto it = mCameras.find(cameraName);
191 return it == mCameras.end() ? nullptr : it->second;
192}
193
194bool VirtualCameraProvider::removeCamera(const std::string& name) {
195 std::shared_ptr<ICameraProviderCallback> callback;
196 {
197 const std::lock_guard<std::mutex> lock(mLock);
198 auto it = mCameras.find(name);
199 if (it == mCameras.end()) {
200 ALOGE("Cannot remove camera %s: no such camera", name.c_str());
201 return false;
202 }
203 // TODO(b/301023410) Gracefully shut down camera.
204 mCameras.erase(it);
205 callback = mCameraProviderCallback;
206 }
207
208 if (callback != nullptr) {
209 auto ret = callback->cameraDeviceStatusChange(
210 name, CameraDeviceStatus::NOT_PRESENT);
211 if (!ret.isOk()) {
212 ALOGE("Failed to announce camera %s status change (NOT_PRESENT): %s",
213 name.c_str(), ret.getDescription().c_str());
214 }
215 }
216
217 return true;
218}
219
220} // namespace virtualcamera
221} // namespace companion
222} // namespace android