1 /*
2 * Copyright (C) 2024 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 #define LOG_TAG "VirtualDeviceCameraIdMapper"
19
20 #include <android_companion_virtualdevice_flags.h>
21 #include <camera/CameraUtils.h>
22
23 #include "VirtualDeviceCameraIdMapper.h"
24
25 namespace android {
26
27 namespace vd_flags = android::companion::virtualdevice::flags;
28
addCamera(const std::string & cameraId,int32_t deviceId,const std::string & mappedCameraId)29 void VirtualDeviceCameraIdMapper::addCamera(const std::string& cameraId,
30 int32_t deviceId, const std::string& mappedCameraId) {
31 if (!vd_flags::camera_device_awareness()) {
32 ALOGV("%s: Device-aware camera feature is not enabled", __func__);
33 return;
34 }
35
36 if (deviceId == kDefaultDeviceId) {
37 ALOGV("%s: Not adding entry for a camera of the default device", __func__);
38 return;
39 }
40
41 ALOGV("%s: Adding camera %s for device %d with mapped id %s", __func__, cameraId.c_str(),
42 deviceId, mappedCameraId.c_str());
43
44 std::scoped_lock lock(mLock);
45 mDeviceIdMappedCameraIdPairToCameraIdMap[{deviceId, mappedCameraId}] = cameraId;
46 mCameraIdToDeviceIdMappedCameraIdPairMap[cameraId] = {deviceId, mappedCameraId};
47 }
48
removeCamera(const std::string & cameraId)49 void VirtualDeviceCameraIdMapper::removeCamera(const std::string& cameraId) {
50 if (!vd_flags::camera_device_awareness()) {
51 ALOGV("%s: Device-aware camera feature is not enabled", __func__);
52 return;
53 }
54
55 auto deviceIdAndMappedCameraIdPair = getDeviceIdAndMappedCameraIdPair(cameraId);
56
57 std::scoped_lock lock(mLock);
58 mCameraIdToDeviceIdMappedCameraIdPairMap.erase(cameraId);
59 mDeviceIdMappedCameraIdPairToCameraIdMap.erase(deviceIdAndMappedCameraIdPair);
60 }
61
getActualCameraId(int32_t deviceId,const std::string & mappedCameraId) const62 std::optional<std::string> VirtualDeviceCameraIdMapper::getActualCameraId(
63 int32_t deviceId, const std::string& mappedCameraId) const {
64 if (deviceId == kDefaultDeviceId) {
65 ALOGV("%s: Returning the camera id as the mapped camera id for camera %s, as it "
66 "belongs to the default device", __func__, mappedCameraId.c_str());
67 return mappedCameraId;
68 }
69
70 if (!vd_flags::camera_device_awareness()) {
71 ALOGV("%s: Device-aware camera feature is not enabled, returning the camera id as "
72 "the mapped camera id for camera %s", __func__, mappedCameraId.c_str());
73 return mappedCameraId;
74 }
75
76 std::scoped_lock lock(mLock);
77 auto iterator = mDeviceIdMappedCameraIdPairToCameraIdMap.find(
78 {deviceId, mappedCameraId});
79 if (iterator == mDeviceIdMappedCameraIdPairToCameraIdMap.end()) {
80 ALOGV("%s: No entry found for device id %d and mapped camera id %s", __func__,
81 deviceId, mappedCameraId.c_str());
82 return std::nullopt;
83 }
84 return iterator->second;
85 }
86
getDeviceIdAndMappedCameraIdPair(const std::string & cameraId) const87 std::pair<int32_t, std::string> VirtualDeviceCameraIdMapper::getDeviceIdAndMappedCameraIdPair(
88 const std::string& cameraId) const {
89 if (!vd_flags::camera_device_awareness()) {
90 ALOGV("%s: Device-aware camera feature is not enabled", __func__);
91 return std::make_pair(kDefaultDeviceId, cameraId);
92 }
93
94 std::scoped_lock lock(mLock);
95 auto iterator = mCameraIdToDeviceIdMappedCameraIdPairMap.find(cameraId);
96 if (iterator != mCameraIdToDeviceIdMappedCameraIdPairMap.end()) {
97 return iterator->second;
98 }
99 ALOGV("%s: No device id and mapped camera id found for camera id %s, so it must belong "
100 "to the default device ?", __func__, cameraId.c_str());
101 return std::make_pair(kDefaultDeviceId, cameraId);
102 }
103
getNumberOfCameras(int32_t deviceId) const104 int VirtualDeviceCameraIdMapper::getNumberOfCameras(int32_t deviceId) const {
105 if (!vd_flags::camera_device_awareness()) {
106 return 0;
107 }
108
109 int numOfCameras = 0;
110 std::scoped_lock lock(mLock);
111 for (const auto& [deviceIdMappedCameraIdPair, _]
112 : mDeviceIdMappedCameraIdPairToCameraIdMap) {
113 if (deviceIdMappedCameraIdPair.first == deviceId) {
114 numOfCameras++;
115 }
116 }
117 return numOfCameras;
118 }
119
getActualCameraId(int api1CameraId,int32_t deviceId) const120 std::optional<std::string> VirtualDeviceCameraIdMapper::getActualCameraId(
121 int api1CameraId, int32_t deviceId) const {
122 if (!vd_flags::camera_device_awareness()) {
123 ALOGV("%s: Device-aware camera feature is not enabled", __func__);
124 return std::nullopt;
125 }
126
127 int matchingCameraIndex = 0;
128 std::scoped_lock lock(mLock);
129 for (const auto& [deviceIdMappedCameraIdPair, actualCameraId]
130 : mDeviceIdMappedCameraIdPairToCameraIdMap) {
131 if (deviceIdMappedCameraIdPair.first == deviceId) {
132 if (matchingCameraIndex == api1CameraId) {
133 return actualCameraId;
134 }
135 matchingCameraIndex++;
136 }
137 }
138 ALOGV("%s: No entry found for device id %d and API 1 camera id %d", __func__,
139 deviceId, api1CameraId);
140 return std::nullopt;
141 }
142
143 } // namespace android