1 /* 2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_CAMERA_H_CAMERA_DEVICE_MANAGER_H 17 #define OHOS_CAMERA_H_CAMERA_DEVICE_MANAGER_H 18 19 #include <refbase.h> 20 #include <set> 21 #include "hcamera_device.h" 22 #include "camera_util.h" 23 #include "mem_mgr_client.h" 24 #include "safe_map.h" 25 26 namespace OHOS { 27 namespace CameraStandard { 28 29 30 class CameraProcessPriority : public RefBase { 31 public: CameraProcessPriority(int32_t uid,int32_t state,int32_t focusState)32 CameraProcessPriority(int32_t uid, int32_t state, int32_t focusState) : processUid_(uid), 33 processState_(state), focusState_(focusState) {} 34 35 inline bool operator == (const CameraProcessPriority& rhs) const 36 { 37 return (this->processState_ == rhs.processState_) && (this->focusState_ == rhs.focusState_); 38 } 39 40 inline bool operator < (const CameraProcessPriority& rhs) const 41 { 42 if (this->processUid_ < maxSysUid_) { 43 MEDIA_DEBUG_LOG("this->processUid_ :%{public}d", this->processUid_); 44 return true; 45 } else if (this->processUid_ >= maxSysUid_ && rhs.processUid_ < maxSysUid_) { 46 MEDIA_DEBUG_LOG("this->processUid_ :%{public}d, rhs.processUid_ :%{public}d", 47 this->processUid_, rhs.processUid_); 48 return false; 49 } 50 if (this->processState_ == rhs.processState_) { 51 MEDIA_DEBUG_LOG("this->processState_ :%{public}d == rhs.processState_: %{public}d", 52 this->processState_, rhs.processState_); 53 return this->focusState_ < rhs.focusState_; 54 } else { 55 MEDIA_DEBUG_LOG("this->processState:%{public}d, rhs.processState_:%{public}d", 56 this->processState_, rhs.processState_); 57 return this->processState_ > rhs.processState_; 58 } 59 } 60 61 inline bool operator > (const CameraProcessPriority& rhs) const 62 { 63 return rhs < *this; 64 } 65 66 inline bool operator <= (const CameraProcessPriority& rhs) const 67 { 68 if (this->processUid_ < maxSysUid_ && rhs.processUid_ < maxSysUid_) { 69 return true; 70 } 71 return !(*this > rhs); 72 } 73 74 inline bool operator >= (const CameraProcessPriority& rhs) const 75 { 76 if (this->processUid_ < maxSysUid_ && rhs.processUid_ < maxSysUid_) { 77 return false; 78 } 79 return !(*this < rhs); 80 } 81 SetProcessState(int32_t state)82 inline void SetProcessState(int32_t state) 83 { 84 processState_ = state; 85 } 86 SetProcessFocusState(int32_t focusState)87 inline void SetProcessFocusState(int32_t focusState) 88 { 89 focusState_ = focusState; 90 } 91 GetUid()92 inline int32_t GetUid() const{ return processUid_; } 93 GetState()94 inline int32_t GetState() const { return processState_; } 95 GetFocusState()96 inline int32_t GetFocusState() const { return focusState_; } 97 98 private: 99 const int32_t maxSysUid_ = 10000; 100 int32_t processUid_; 101 int32_t processState_; 102 int32_t focusState_; 103 }; 104 105 class HCameraDeviceHolder : public RefBase { 106 public: HCameraDeviceHolder(int32_t pid,int32_t uid,int32_t state,int32_t focusState,sptr<HCameraDevice> device,uint32_t accessTokenId,int32_t cost,const std::set<std::string> & conflicting)107 HCameraDeviceHolder(int32_t pid, int32_t uid, int32_t state, int32_t focusState, 108 sptr<HCameraDevice> device, uint32_t accessTokenId, int32_t cost, const std::set<std::string> &conflicting) 109 :pid_(pid), uid_(uid), state_(state), focusState_(focusState), accessTokenId_(accessTokenId), device_(device), 110 cost_(cost), conflicting_(conflicting) 111 { 112 processPriority_ = new CameraProcessPriority(uid, state, focusState); 113 } SetPid(int32_t pid)114 inline void SetPid(int32_t pid) { pid_ = pid; } SetUid(int32_t uid)115 inline void SetUid(int32_t uid) { uid_ = uid; } SetState(int32_t state)116 inline void SetState(int32_t state) 117 { 118 processPriority_->SetProcessState(state); 119 state_ = state; 120 } SetFocusState(int32_t focusState)121 inline void SetFocusState(int32_t focusState) 122 { 123 processPriority_->SetProcessFocusState(focusState); 124 focusState_ = focusState; 125 } 126 GetPid()127 inline int32_t GetPid() const {return pid_;} 128 GetUid()129 inline int32_t GetUid() const{ return uid_; } 130 GetState()131 inline int32_t GetState() const { return state_; } 132 GetFocusState()133 inline int32_t GetFocusState() const { return focusState_; } 134 GetAccessTokenId()135 inline uint32_t GetAccessTokenId() const { return accessTokenId_; } 136 GetDevice()137 inline sptr<HCameraDevice> GetDevice() const { return device_; } 138 GetPriority()139 inline sptr<CameraProcessPriority> GetPriority() const {return processPriority_;} 140 GetCost()141 inline int32_t GetCost() const{ return cost_; } 142 IsConflicting(const std::string & cameraId)143 inline bool IsConflicting(const std::string &cameraId) const 144 { 145 std::string curCameraId = device_->GetCameraId(); 146 if (cameraId == curCameraId) { 147 return true; 148 } 149 for (const auto &x : conflicting_) { 150 if (cameraId == x) { 151 return true; 152 } 153 } 154 return false; 155 } 156 GetConflicting()157 inline std::set<std::string> GetConflicting() const { return conflicting_; } 158 159 private: 160 int32_t pid_; 161 int32_t uid_; 162 int32_t state_; 163 int32_t focusState_; 164 uint32_t accessTokenId_; 165 sptr<CameraProcessPriority> processPriority_; 166 sptr<HCameraDevice> device_; 167 int32_t cost_; 168 std::set<std::string> conflicting_; 169 }; 170 171 class CameraConcurrentSelector : public RefBase { 172 public: 173 CameraConcurrentSelector() = default; 174 ~CameraConcurrentSelector() = default; 175 176 /** 177 * @brief Setting up a baseline camera and producing a table of concurrent cameras 178 */ 179 void SetRequestCameraId(sptr<HCameraDeviceHolder> requestCameraHolder); 180 181 /** 182 * @brief Check and save whether the camera can be retained. 183 */ 184 bool SaveConcurrentCameras(std::vector<sptr<HCameraDeviceHolder>> holdersSortedByProprity, 185 sptr<HCameraDeviceHolder> holderWaitToConfirm); 186 187 /** 188 * @brief Return to cameras that must be retained. 189 * 190 * @return Return to cameras that must be retained. 191 */ GetCamerasRetainable()192 inline std::vector<sptr<HCameraDeviceHolder>> GetCamerasRetainable() 193 { 194 return listOfCameraRetainable_; 195 } 196 197 /** 198 * @brief Return the Concurrent List. 199 * 200 * @return Return the Concurrent List. 201 */ GetConcurrentCameraTable()202 inline std::vector<std::vector<std::int32_t>> GetConcurrentCameraTable() 203 { 204 return concurrentCameraTable_; 205 } 206 207 bool CanOpenCameraconcurrently(std::vector<sptr<HCameraDeviceHolder>> reservedCameras, 208 std::vector<std::vector<std::int32_t>> concurrentCameraTable); 209 210 private: 211 sptr<HCameraDeviceHolder> requestCameraHolder_; 212 std::vector<sptr<HCameraDeviceHolder>> listOfCameraRetainable_ = {}; 213 std::vector<std::vector<std::int32_t>> concurrentCameraTable_ = {}; 214 int32_t GetCameraIdNumber(std::string); 215 bool ConcurrentWithRetainedDevicesOrNot(sptr<HCameraDeviceHolder> cameraIdNeedConfirm); 216 }; 217 218 class HCameraDeviceManager : public RefBase { 219 public: 220 /** 221 * @brief the default maxinum "cost" allowed before evicting. 222 * 223 */ 224 static constexpr int32_t DEFAULT_MAX_COST = 100; 225 226 ~HCameraDeviceManager(); 227 /** 228 * @brief Get camera device manager instance. 229 * 230 * @return Returns pointer to camera device manager instance. 231 */ 232 static sptr<HCameraDeviceManager> &GetInstance(); 233 234 /** 235 * @brief Add opened device in camera device manager. 236 * 237 * @param device Device that have been turned on. 238 * @param pid Pid for opening the device. 239 */ 240 void AddDevice(pid_t pid, sptr<HCameraDevice> device); 241 242 /** 243 * @brief remove camera in camera device manager. 244 * 245 * @param device Device that have been turned off. 246 */ 247 void RemoveDevice(const std::string &cameraId); 248 249 /** 250 * @brief Get cameraHolder by active process pid. 251 * 252 * @param pid Pid of active process. 253 */ 254 std::vector<sptr<HCameraDeviceHolder>> GetCameraHolderByPid(pid_t pid); 255 256 /** 257 * @brief Get cameras by active process pid. 258 * 259 * @param pid Pid of active process. 260 */ 261 std::vector<sptr<HCameraDevice>> GetCamerasByPid(pid_t pidRequest); 262 263 /** 264 * @brief Get process pid device manager instance. 265 * 266 * @return Returns pointer to camera device manager instance. 267 */ 268 std::vector<pid_t> GetActiveClient(); 269 270 /** 271 * @brief Get the existing holder object. 272 * 273 * @return Returns the list of existing holders. 274 */ 275 276 std::vector<sptr<HCameraDeviceHolder>> GetActiveCameraHolders(); 277 278 void SetStateOfACamera(std::string cameraId, int32_t state); 279 280 bool IsMultiCameraActive(int32_t pid); 281 282 void SetPeerCallback(sptr<ICameraBroker>& callback); 283 284 void UnsetPeerCallback(); 285 286 size_t GetActiveCamerasCount(); 287 288 SafeMap<std::string, int32_t> &GetCameraStateOfASide(); 289 290 /** 291 * @brief remove camera in camera device manager. 292 * 293 * @param camerasNeedEvict Devices that need to be shut down. 294 * @param cameraIdRequestOpen device is requested to turn on. 295 */ 296 bool GetConflictDevices(std::vector<sptr<HCameraDevice>> &cameraNeedEvict, sptr<HCameraDevice> cameraIdRequestOpen, 297 int32_t concurrentTypeOfRequest); 298 /** 299 * @brief handle active camera evictions in camera device manager. 300 * 301 * @param evictedClients Devices that need to be shut down. 302 * @param cameraRequestOpen device is requested to turn on. 303 */ 304 bool HandleCameraEvictions(std::vector<sptr<HCameraDeviceHolder>> &evictedClients, 305 sptr<HCameraDeviceHolder> &cameraRequestOpen); 306 307 bool IsProcessHasConcurrentDevice(pid_t pid); 308 309 std::mutex mapMutex_; 310 private: 311 HCameraDeviceManager(); 312 static sptr<HCameraDeviceManager> cameraDeviceManager_; 313 static std::mutex instanceMutex_; 314 std::map<pid_t, std::vector<sptr<HCameraDeviceHolder>>> pidToCameras_; 315 SafeMap<std::string, int32_t> stateOfRgmCamera_; 316 // LRU ordered, most recent at end 317 std::vector<sptr<HCameraDeviceHolder>> activeCameras_; 318 std::vector<sptr<HCameraDeviceHolder>> holderSortedByProprity_; 319 sptr<ICameraBroker> peerCallback_; 320 std::mutex peerCbMutex_; 321 sptr<CameraConcurrentSelector> concurrentSelector_; 322 std::string GetACameraId(); 323 bool IsAllowOpen(pid_t activeClient); 324 int32_t GetCurrentCost() const; 325 std::vector<sptr<HCameraDeviceHolder>> WouldEvict(sptr<HCameraDeviceHolder> &cameraRequestOpen); 326 void GenerateProcessCameraState(int32_t& activeState, int32_t& requestState, 327 uint32_t activeAccessTokenId, uint32_t requestAccessTokenId); 328 void GenerateEachProcessCameraState(int32_t& processState, uint32_t processTokenId); 329 void PrintClientInfo(sptr<HCameraDeviceHolder> activeCameraHolder, sptr<HCameraDeviceHolder> requestCameraHolder); 330 sptr<HCameraDeviceHolder> GenerateCameraHolder(sptr<HCameraDevice> device, pid_t pid, int32_t uid, 331 uint32_t accessTokenId); 332 std::vector<sptr<HCameraDeviceHolder>> SortDeviceByPriority(); 333 }; 334 } // namespace CameraStandard 335 } // namespace OHOS 336 #endif // OHOS_CAMERA_H_CAMERA_DEVICE_MANAGER_H