1 /*
2  * Copyright 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_VIRTUALCAMERAPROVIDER_H
18 #define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERAPROVIDER_H
19 
20 #include <atomic>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 
25 #include "VirtualCameraDevice.h"
26 #include "aidl/android/companion/virtualcamera/BnVirtualCameraCallback.h"
27 #include "aidl/android/hardware/camera/common/VendorTagSection.h"
28 #include "aidl/android/hardware/camera/device/ICameraDevice.h"
29 #include "aidl/android/hardware/camera/provider/BnCameraProvider.h"
30 #include "aidl/android/hardware/camera/provider/CameraIdAndStreamCombination.h"
31 #include "aidl/android/hardware/camera/provider/ConcurrentCameraIdCombination.h"
32 #include "aidl/android/hardware/camera/provider/ICameraProviderCallback.h"
33 #include "utils/Mutex.h"
34 
35 namespace android {
36 namespace companion {
37 namespace virtualcamera {
38 
39 // Entry point for virtual camera HAL.
40 // Allows to create and keep track of virtual camera and implements
41 // IAudioProvider AIDL interface to expose virtual camera devices to camera framework.
42 class VirtualCameraProvider
43     : public ::aidl::android::hardware::camera::provider::BnCameraProvider {
44  public:
45   ~VirtualCameraProvider() override = default;
46 
47   ndk::ScopedAStatus setCallback(
48       const std::shared_ptr<
49           ::aidl::android::hardware::camera::provider::ICameraProviderCallback>&
50           in_callback) override;
51 
52   ndk::ScopedAStatus getVendorTags(
53       std::vector<::aidl::android::hardware::camera::common::VendorTagSection>*
54           _aidl_return) override;
55 
56   ndk::ScopedAStatus getCameraIdList(
57       std::vector<std::string>* _aidl_return) override;
58 
59   ndk::ScopedAStatus getCameraDeviceInterface(
60       const std::string& in_cameraDeviceName,
61       std::shared_ptr<::aidl::android::hardware::camera::device::ICameraDevice>*
62           _aidl_return) override;
63 
64   ndk::ScopedAStatus notifyDeviceStateChange(int64_t in_deviceState) override;
65 
66   ndk::ScopedAStatus getConcurrentCameraIds(
67       std::vector<::aidl::android::hardware::camera::provider::
68                       ConcurrentCameraIdCombination>* _aidl_return) override;
69 
70   ndk::ScopedAStatus isConcurrentStreamCombinationSupported(
71       const std::vector<::aidl::android::hardware::camera::provider::
72                             CameraIdAndStreamCombination>& in_configs,
73       bool* _aidl_return) override;
74 
75   // Create new virtual camera devices
76   // Returns nullptr if creation was not successful.
77   std::shared_ptr<VirtualCameraDevice> createCamera(
78       const aidl::android::companion::virtualcamera::VirtualCameraConfiguration&
79           configuration,
80       const std::string& cameraId, int32_t deviceId);
81 
82   std::shared_ptr<VirtualCameraDevice> getCamera(const std::string& name);
83 
84   bool removeCamera(const std::string& name);
85 
86  private:
87   std::mutex mLock;
88 
89   std::shared_ptr<
90       ::aidl::android::hardware::camera::provider::ICameraProviderCallback>
91       mCameraProviderCallback GUARDED_BY(mLock);
92 
93   std::map<std::string, std::shared_ptr<VirtualCameraDevice>> mCameras
94       GUARDED_BY(mLock);
95 };
96 
97 }  // namespace virtualcamera
98 }  // namespace companion
99 }  // namespace android
100 
101 #endif  // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERAPROVIDER_H
102