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 <camera/CameraUtils.h>
21
22 #include "VirtualDeviceCameraIdMapper.h"
23
24 namespace android {
25
addCamera(const std::string & cameraId,int32_t deviceId,const std::string & mappedCameraId)26 void VirtualDeviceCameraIdMapper::addCamera(const std::string& cameraId,
27 int32_t deviceId, const std::string& mappedCameraId) {
28 if (deviceId == kDefaultDeviceId) {
29 ALOGV("%s: Not adding entry for a camera of the default device", __func__);
30 return;
31 }
32
33 ALOGV("%s: Adding camera %s for device %d with mapped id %s", __func__, cameraId.c_str(),
34 deviceId, mappedCameraId.c_str());
35
36 std::scoped_lock lock(mLock);
37 mDeviceIdMappedCameraIdPairToCameraIdMap[{deviceId, mappedCameraId}] = cameraId;
38 mCameraIdToDeviceIdMappedCameraIdPairMap[cameraId] = {deviceId, mappedCameraId};
39 }
40
removeCamera(const std::string & cameraId)41 void VirtualDeviceCameraIdMapper::removeCamera(const std::string& cameraId) {
42 auto deviceIdAndMappedCameraIdPair = getDeviceIdAndMappedCameraIdPair(cameraId);
43
44 std::scoped_lock lock(mLock);
45 mCameraIdToDeviceIdMappedCameraIdPairMap.erase(cameraId);
46 mDeviceIdMappedCameraIdPairToCameraIdMap.erase(deviceIdAndMappedCameraIdPair);
47 }
48
getActualCameraId(int32_t deviceId,const std::string & mappedCameraId) const49 std::optional<std::string> VirtualDeviceCameraIdMapper::getActualCameraId(
50 int32_t deviceId, const std::string& mappedCameraId) const {
51 if (deviceId == kDefaultDeviceId) {
52 ALOGV("%s: Returning the camera id as the mapped camera id for camera %s, as it "
53 "belongs to the default device", __func__, mappedCameraId.c_str());
54 return mappedCameraId;
55 }
56
57 std::scoped_lock lock(mLock);
58 auto iterator = mDeviceIdMappedCameraIdPairToCameraIdMap.find(
59 {deviceId, mappedCameraId});
60 if (iterator == mDeviceIdMappedCameraIdPairToCameraIdMap.end()) {
61 ALOGV("%s: No entry found for device id %d and mapped camera id %s", __func__,
62 deviceId, mappedCameraId.c_str());
63 return std::nullopt;
64 }
65 return iterator->second;
66 }
67
getDeviceIdAndMappedCameraIdPair(const std::string & cameraId) const68 std::pair<int32_t, std::string> VirtualDeviceCameraIdMapper::getDeviceIdAndMappedCameraIdPair(
69 const std::string& cameraId) const {
70 std::scoped_lock lock(mLock);
71 auto iterator = mCameraIdToDeviceIdMappedCameraIdPairMap.find(cameraId);
72 if (iterator != mCameraIdToDeviceIdMappedCameraIdPairMap.end()) {
73 return iterator->second;
74 }
75 ALOGV("%s: No device id and mapped camera id found for camera id %s, so it must belong "
76 "to the default device ?", __func__, cameraId.c_str());
77 return std::make_pair(kDefaultDeviceId, cameraId);
78 }
79
getNumberOfCameras(int32_t deviceId) const80 int VirtualDeviceCameraIdMapper::getNumberOfCameras(int32_t deviceId) const {
81 int numOfCameras = 0;
82 std::scoped_lock lock(mLock);
83 for (const auto& [deviceIdMappedCameraIdPair, _]
84 : mDeviceIdMappedCameraIdPairToCameraIdMap) {
85 if (deviceIdMappedCameraIdPair.first == deviceId) {
86 numOfCameras++;
87 }
88 }
89 return numOfCameras;
90 }
91
getActualCameraId(int api1CameraId,int32_t deviceId) const92 std::optional<std::string> VirtualDeviceCameraIdMapper::getActualCameraId(
93 int api1CameraId, int32_t deviceId) const {
94 int matchingCameraIndex = 0;
95 std::scoped_lock lock(mLock);
96 for (const auto& [deviceIdMappedCameraIdPair, actualCameraId]
97 : mDeviceIdMappedCameraIdPairToCameraIdMap) {
98 if (deviceIdMappedCameraIdPair.first == deviceId) {
99 if (matchingCameraIndex == api1CameraId) {
100 return actualCameraId;
101 }
102 matchingCameraIndex++;
103 }
104 }
105 ALOGV("%s: No entry found for device id %d and API 1 camera id %d", __func__,
106 deviceId, api1CameraId);
107 return std::nullopt;
108 }
109
110 } // namespace android
111