blob: 68a89795d9d4966e258f30c00824a7b606af5339 [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#include "android/binder_status.h"
19#define LOG_TAG "VirtualCameraService"
20#include "VirtualCameraService.h"
21
22#include <cinttypes>
23#include <cstdint>
24#include <cstdio>
25#include <memory>
26#include <mutex>
27
28#include "VirtualCameraDevice.h"
29#include "VirtualCameraProvider.h"
30#include "android/binder_auto_utils.h"
31#include "android/binder_libbinder.h"
32#include "binder/Status.h"
33
34using ::android::binder::Status;
35
36namespace android {
37namespace companion {
38namespace virtualcamera {
39
40using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration;
41
42namespace {
43
44constexpr char kEnableTestCameraCmd[] = "enable_test_camera";
45constexpr char kDisableTestCameraCmd[] = "disable_test_camera";
46constexpr char kShellCmdHelp[] = R"(
47Available commands:
48 * enable_test_camera
49 * disable_test_camera
50)";
51
52} // namespace
53
54VirtualCameraService::VirtualCameraService(
55 std::shared_ptr<VirtualCameraProvider> virtualCameraProvider)
56 : mVirtualCameraProvider(virtualCameraProvider) {
57}
58
59ndk::ScopedAStatus VirtualCameraService::registerCamera(
60 const ::ndk::SpAIBinder& token,
61 const VirtualCameraConfiguration& configuration, bool* _aidl_return) {
62 (void)configuration;
63 if (_aidl_return == nullptr) {
64 return ndk::ScopedAStatus::fromServiceSpecificError(
65 Status::EX_ILLEGAL_ARGUMENT);
66 }
67 *_aidl_return = true;
68
69 std::lock_guard lock(mLock);
70 if (mTokenToCameraName.find(token) != mTokenToCameraName.end()) {
71 ALOGE(
72 "Attempt to register camera corresponding to already registered binder "
73 "token: "
74 "0x%" PRIxPTR,
75 reinterpret_cast<uintptr_t>(token.get()));
76 *_aidl_return = false;
77 }
78
79 // TODO(b/301023410) Validate configuration and pass it to the camera.
80 std::shared_ptr<VirtualCameraDevice> camera =
81 mVirtualCameraProvider->createCamera(configuration.virtualCameraCallback);
82 if (camera == nullptr) {
83 ALOGE("Failed to create camera for binder token 0x%" PRIxPTR,
84 reinterpret_cast<uintptr_t>(token.get()));
85 *_aidl_return = false;
86 return ndk::ScopedAStatus::fromServiceSpecificError(
87 Status::EX_SERVICE_SPECIFIC);
88 }
89
90 mTokenToCameraName[token] = camera->getCameraName();
91 return ndk::ScopedAStatus::ok();
92}
93
94ndk::ScopedAStatus VirtualCameraService::unregisterCamera(
95 const ::ndk::SpAIBinder& token) {
96 std::lock_guard lock(mLock);
97
98 auto it = mTokenToCameraName.find(token);
99 if (it == mTokenToCameraName.end()) {
100 ALOGE(
101 "Attempt to unregister camera corresponding to unknown binder token: "
102 "0x%" PRIxPTR,
103 reinterpret_cast<uintptr_t>(token.get()));
104 return ndk::ScopedAStatus::ok();
105 }
106
107 mVirtualCameraProvider->removeCamera(it->second);
108
109 return ndk::ScopedAStatus::ok();
110}
111
Biswarup Pal68137fc2023-11-24 18:06:54 +0000112ndk::ScopedAStatus VirtualCameraService::getCameraId(
113 const ::ndk::SpAIBinder& token, int32_t* _aidl_return) {
114 if (_aidl_return == nullptr) {
115 return ndk::ScopedAStatus::fromServiceSpecificError(
116 Status::EX_ILLEGAL_ARGUMENT);
117 }
118
119 auto camera = getCamera(token);
120 if (camera == nullptr) {
121 ALOGE(
122 "Attempt to get camera id corresponding to unknown binder token: "
123 "0x%" PRIxPTR,
124 reinterpret_cast<uintptr_t>(token.get()));
125 return ndk::ScopedAStatus::ok();
126 }
127
128 *_aidl_return = camera->getCameraId();
129
130 return ndk::ScopedAStatus::ok();
131}
132
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100133std::shared_ptr<VirtualCameraDevice> VirtualCameraService::getCamera(
134 const ::ndk::SpAIBinder& token) {
135 if (token == nullptr) {
136 return nullptr;
137 }
138
139 std::lock_guard lock(mLock);
140 auto it = mTokenToCameraName.find(token);
141 if (it == mTokenToCameraName.end()) {
142 return nullptr;
143 }
144
145 return mVirtualCameraProvider->getCamera(it->second);
146}
147
148binder_status_t VirtualCameraService::handleShellCommand(int in, int out,
149 int err,
150 const char** args,
151 uint32_t numArgs) {
152 if (numArgs <= 0) {
153 dprintf(out, kShellCmdHelp);
154 }
155
156 if (args == nullptr || args[0] == nullptr) {
157 return STATUS_BAD_VALUE;
158 }
159 const char* const cmd = args[0];
160 if (strcmp(kEnableTestCameraCmd, cmd) == 0) {
161 enableTestCameraCmd(in, err);
162 } else if (strcmp(kDisableTestCameraCmd, cmd) == 0) {
163 disableTestCameraCmd(in);
164 } else {
165 dprintf(out, kShellCmdHelp);
166 }
167
168 fsync(out);
169 return STATUS_OK;
170}
171
172void VirtualCameraService::enableTestCameraCmd(const int out, const int err) {
173 if (mTestCameraToken != nullptr) {
174 dprintf(out, "Test camera is already enabled (%s).",
175 getCamera(mTestCameraToken)->getCameraName().c_str());
176 return;
177 }
178
179 sp<BBinder> token = sp<BBinder>::make();
180 mTestCameraToken.set(AIBinder_fromPlatformBinder(token));
181
182 bool ret;
183 registerCamera(mTestCameraToken, VirtualCameraConfiguration(), &ret);
184 if (ret) {
185 dprintf(out, "Successfully registered test camera %s",
186 getCamera(mTestCameraToken)->getCameraName().c_str());
187 } else {
188 dprintf(err, "Failed to create test camera");
189 }
190}
191
192void VirtualCameraService::disableTestCameraCmd(const int out) {
193 if (mTestCameraToken == nullptr) {
194 dprintf(out, "Test camera is not registered.");
195 }
196 unregisterCamera(mTestCameraToken);
197 mTestCameraToken.set(nullptr);
198}
199
200} // namespace virtualcamera
201} // namespace companion
202} // namespace android