blob: 62dc08bea20f9ee30ac36a7c05cf1bbbf38554ba [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
112std::shared_ptr<VirtualCameraDevice> VirtualCameraService::getCamera(
113 const ::ndk::SpAIBinder& token) {
114 if (token == nullptr) {
115 return nullptr;
116 }
117
118 std::lock_guard lock(mLock);
119 auto it = mTokenToCameraName.find(token);
120 if (it == mTokenToCameraName.end()) {
121 return nullptr;
122 }
123
124 return mVirtualCameraProvider->getCamera(it->second);
125}
126
127binder_status_t VirtualCameraService::handleShellCommand(int in, int out,
128 int err,
129 const char** args,
130 uint32_t numArgs) {
131 if (numArgs <= 0) {
132 dprintf(out, kShellCmdHelp);
133 }
134
135 if (args == nullptr || args[0] == nullptr) {
136 return STATUS_BAD_VALUE;
137 }
138 const char* const cmd = args[0];
139 if (strcmp(kEnableTestCameraCmd, cmd) == 0) {
140 enableTestCameraCmd(in, err);
141 } else if (strcmp(kDisableTestCameraCmd, cmd) == 0) {
142 disableTestCameraCmd(in);
143 } else {
144 dprintf(out, kShellCmdHelp);
145 }
146
147 fsync(out);
148 return STATUS_OK;
149}
150
151void VirtualCameraService::enableTestCameraCmd(const int out, const int err) {
152 if (mTestCameraToken != nullptr) {
153 dprintf(out, "Test camera is already enabled (%s).",
154 getCamera(mTestCameraToken)->getCameraName().c_str());
155 return;
156 }
157
158 sp<BBinder> token = sp<BBinder>::make();
159 mTestCameraToken.set(AIBinder_fromPlatformBinder(token));
160
161 bool ret;
162 registerCamera(mTestCameraToken, VirtualCameraConfiguration(), &ret);
163 if (ret) {
164 dprintf(out, "Successfully registered test camera %s",
165 getCamera(mTestCameraToken)->getCameraName().c_str());
166 } else {
167 dprintf(err, "Failed to create test camera");
168 }
169}
170
171void VirtualCameraService::disableTestCameraCmd(const int out) {
172 if (mTestCameraToken == nullptr) {
173 dprintf(out, "Test camera is not registered.");
174 }
175 unregisterCamera(mTestCameraToken);
176 mTestCameraToken.set(nullptr);
177}
178
179} // namespace virtualcamera
180} // namespace companion
181} // namespace android