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 #ifndef ANDROID_SERVERS_CAMERA_VIRTUAL_DEVICE_CAMERA_ID_MAPPER_H 18 #define ANDROID_SERVERS_CAMERA_VIRTUAL_DEVICE_CAMERA_ID_MAPPER_H 19 20 #include <string> 21 #include <map> 22 #include <mutex> 23 24 #include <utils/Mutex.h> 25 26 namespace android { 27 28 class VirtualDeviceCameraIdMapper { 29 public: VirtualDeviceCameraIdMapper()30 VirtualDeviceCameraIdMapper() {} 31 ~VirtualDeviceCameraIdMapper()32 virtual ~VirtualDeviceCameraIdMapper() {} 33 34 void addCamera(const std::string& cameraId, int32_t deviceId, 35 const std::string& mappedCameraId) EXCLUDES(mLock); 36 37 void removeCamera(const std::string& cameraId) EXCLUDES(mLock); 38 39 /** 40 * Return the actual camera id for a given device id (i.e., the id of the device owning 41 * the camera, for a virtual camera this would be the id of the virtual device, and for 42 * any other cameras this would be default device id, i.e., 0) and mapped camera 43 * id (for virtual devices, the back and front virtual cameras of that device would have 44 * 0 and 1 respectively as their mapped camera id, and for any other cameras this 45 * would be their actual camera id). When the camera device awareness flag is disabled, 46 * this will return the given camera id itself. 47 */ 48 std::optional<std::string> getActualCameraId(int32_t deviceId, 49 const std::string& mappedCameraId) const EXCLUDES(mLock); 50 51 /** 52 * Return the device id (i.e., the id of the device owning the camera, for a virtual 53 * camera this would be the id of the virtual device, and for any other cameras this 54 * would be default device id, i.e., 0) and the mapped camera id (for virtual 55 * devices, the back and front virtual cameras of that device would have 0 and 1 56 * respectively as their mapped camera id, and for any other cameras this would 57 * be their actual camera id) for a given camera id. When the camera device awareness flag is 58 * disabled, this will return a pair of kDefaultDeviceId and the given cameraId. 59 */ 60 std::pair<int32_t, std::string> getDeviceIdAndMappedCameraIdPair( 61 const std::string& cameraId) const EXCLUDES(mLock); 62 63 /** 64 * Return the number of virtual cameras corresponding to the legacy camera API 65 * getNumberOfCameras. When the camera device awareness flag is disabled, this will return 0. 66 */ 67 int getNumberOfCameras(int32_t deviceId) const EXCLUDES(mLock); 68 69 /** 70 * Return the actual camera id corresponding to the virtual camera with the given API 1 camera 71 * id. When the camera device awareness flag is disabled, this will return std::nullopt. 72 */ 73 std::optional<std::string> getActualCameraId(int api1CameraId, int32_t deviceId) 74 const EXCLUDES(mLock); 75 76 private: 77 mutable std::mutex mLock; 78 79 // Map of (deviceId, app-visible cameraId) -> HAL-visible cameraId 80 std::map<std::pair<int32_t, std::string>, std::string> 81 mDeviceIdMappedCameraIdPairToCameraIdMap GUARDED_BY(mLock); 82 // Map of HAL-visible cameraId -> (deviceId, app-visible cameraId) 83 std::map<std::string, std::pair<int32_t, std::string>> 84 mCameraIdToDeviceIdMappedCameraIdPairMap GUARDED_BY(mLock); 85 }; 86 87 } // namespace android 88 89 #endif // ANDROID_SERVERS_CAMERA_VIRTUAL_DEVICE_CAMERA_ID_MAPPER_H 90