blob: d5739865747dcdf597f32128e4cb9363025182c3 [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#ifndef ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H
18#define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H
19
20#include <memory>
21#include <mutex>
22#include <unordered_map>
23
24#include "VirtualCameraDevice.h"
25#include "VirtualCameraProvider.h"
26#include "aidl/android/companion/virtualcamera/BnVirtualCameraService.h"
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +010027#include "util/Permissions.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010028
29namespace android {
30namespace companion {
31namespace virtualcamera {
32
33// Implementation of Virtual Camera Service for managing virtual camera devices.
34class VirtualCameraService
35 : public aidl::android::companion::virtualcamera::BnVirtualCameraService {
36 public:
37 VirtualCameraService(
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +010038 std::shared_ptr<VirtualCameraProvider> virtualCameraProvider,
39 const PermissionsProxy& permissionProxy = PermissionsProxy::get());
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010040
41 // Register camera corresponding to the binder token.
42 ndk::ScopedAStatus registerCamera(
43 const ::ndk::SpAIBinder& token,
44 const ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration&
45 configuration,
46 bool* _aidl_return) override EXCLUDES(mLock);
47
48 // Unregisters camera corresponding to the binder token.
49 ndk::ScopedAStatus unregisterCamera(const ::ndk::SpAIBinder& token) override
50 EXCLUDES(mLock);
51
Biswarup Pal68137fc2023-11-24 18:06:54 +000052 // Returns the camera id corresponding to the binder token.
53 ndk::ScopedAStatus getCameraId(
54 const ::ndk::SpAIBinder& token, int32_t* _aidl_return) override EXCLUDES(mLock);
55
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010056 // Returns VirtualCameraDevice corresponding to binder token or nullptr if
57 // there's no camera asociated with the token.
58 std::shared_ptr<VirtualCameraDevice> getCamera(const ::ndk::SpAIBinder& token)
59 EXCLUDES(mLock);
60
61 // Handle cmd shell commands `adb shell cmd virtual_camera_service` [args].
62 binder_status_t handleShellCommand(int in, int out, int err, const char** args,
63 uint32_t numArgs) override;
64
65 private:
66 // Create and enable test camera instance if there's none.
67 void enableTestCameraCmd(int out, int err);
68 // Disable and destroy test camera instance if there's one.
69 void disableTestCameraCmd(int out);
70
71 std::shared_ptr<VirtualCameraProvider> mVirtualCameraProvider;
72
Jan Sebechlebskyde6f16f2023-11-29 09:27:36 +010073 const PermissionsProxy& mPermissionProxy;
74
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010075 std::mutex mLock;
76 struct BinderTokenHash {
77 std::size_t operator()(const ::ndk::SpAIBinder& key) const {
78 return std::hash<void*>{}(reinterpret_cast<void*>(key.get()));
79 }
80 };
81 // Map Binder tokens to names of cameras managed by camera provider.
82 std::unordered_map<::ndk::SpAIBinder, std::string, BinderTokenHash>
83 mTokenToCameraName GUARDED_BY(mLock);
84
85 // Local binder token for test camera instance, or nullptr if there's none.
86 ::ndk::SpAIBinder mTestCameraToken;
87};
88
89} // namespace virtualcamera
90} // namespace companion
91} // namespace android
92
93#endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H