blob: cf68c1b2a3381ac55370ddd69462a3cf4809c602 [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"
27
28namespace android {
29namespace companion {
30namespace virtualcamera {
31
32// Implementation of Virtual Camera Service for managing virtual camera devices.
33class VirtualCameraService
34 : public aidl::android::companion::virtualcamera::BnVirtualCameraService {
35 public:
36 VirtualCameraService(
37 std::shared_ptr<VirtualCameraProvider> virtualCameraProvider);
38
39 // Register camera corresponding to the binder token.
40 ndk::ScopedAStatus registerCamera(
41 const ::ndk::SpAIBinder& token,
42 const ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration&
43 configuration,
44 bool* _aidl_return) override EXCLUDES(mLock);
45
46 // Unregisters camera corresponding to the binder token.
47 ndk::ScopedAStatus unregisterCamera(const ::ndk::SpAIBinder& token) override
48 EXCLUDES(mLock);
49
50 // Returns VirtualCameraDevice corresponding to binder token or nullptr if
51 // there's no camera asociated with the token.
52 std::shared_ptr<VirtualCameraDevice> getCamera(const ::ndk::SpAIBinder& token)
53 EXCLUDES(mLock);
54
55 // Handle cmd shell commands `adb shell cmd virtual_camera_service` [args].
56 binder_status_t handleShellCommand(int in, int out, int err, const char** args,
57 uint32_t numArgs) override;
58
59 private:
60 // Create and enable test camera instance if there's none.
61 void enableTestCameraCmd(int out, int err);
62 // Disable and destroy test camera instance if there's one.
63 void disableTestCameraCmd(int out);
64
65 std::shared_ptr<VirtualCameraProvider> mVirtualCameraProvider;
66
67 std::mutex mLock;
68 struct BinderTokenHash {
69 std::size_t operator()(const ::ndk::SpAIBinder& key) const {
70 return std::hash<void*>{}(reinterpret_cast<void*>(key.get()));
71 }
72 };
73 // Map Binder tokens to names of cameras managed by camera provider.
74 std::unordered_map<::ndk::SpAIBinder, std::string, BinderTokenHash>
75 mTokenToCameraName GUARDED_BY(mLock);
76
77 // Local binder token for test camera instance, or nullptr if there's none.
78 ::ndk::SpAIBinder mTestCameraToken;
79};
80
81} // namespace virtualcamera
82} // namespace companion
83} // namespace android
84
85#endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASERVICE_H