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 "hcamera_device.h" 21 #include "camera_util.h" 22 #include "mem_mgr_client.h" 23 #include "safe_map.h" 24 25 namespace OHOS { 26 namespace CameraStandard { 27 28 29 class CameraProcessPriority : public RefBase { 30 public: CameraProcessPriority(int32_t uid,int32_t state,int32_t focusState)31 CameraProcessPriority(int32_t uid, int32_t state, int32_t focusState) : processUid_(uid), 32 processState_(state), focusState_(focusState) {} 33 34 inline bool operator == (const CameraProcessPriority& rhs) const 35 { 36 return (this->processState_ == rhs.processState_) && (this->focusState_ == rhs.focusState_); 37 } 38 39 inline bool operator < (const CameraProcessPriority& rhs) const 40 { 41 if (this->processUid_ < maxSysUid_) { 42 MEDIA_DEBUG_LOG("this->processUid_ :%{public}d", this->processUid_); 43 return true; 44 } else if (this->processUid_ >= maxSysUid_ && rhs.processUid_ < maxSysUid_) { 45 MEDIA_DEBUG_LOG("this->processUid_ :%{public}d, rhs.processUid_ :%{public}d", 46 this->processUid_, rhs.processUid_); 47 return false; 48 } 49 if (this->processState_ == rhs.processState_) { 50 MEDIA_DEBUG_LOG("this->processState_ :%{public}d == rhs.processState_: %{public}d", 51 this->processState_, rhs.processState_); 52 return this->focusState_ < rhs.focusState_; 53 } else { 54 MEDIA_DEBUG_LOG("this->processState:%{public}d, rhs.processState_:%{public}d", 55 this->processState_, rhs.processState_); 56 return this->processState_ > rhs.processState_; 57 } 58 } 59 60 inline bool operator > (const CameraProcessPriority& rhs) const 61 { 62 return rhs < *this; 63 } 64 65 inline bool operator <= (const CameraProcessPriority& rhs) const 66 { 67 if (this->processUid_ < maxSysUid_ && rhs.processUid_ < maxSysUid_) { 68 return true; 69 } 70 return !(*this > rhs); 71 } 72 73 inline bool operator >= (const CameraProcessPriority& rhs) const 74 { 75 if (this->processUid_ < maxSysUid_ && rhs.processUid_ < maxSysUid_) { 76 return false; 77 } 78 return !(*this < rhs); 79 } 80 SetProcessState(int32_t state)81 inline void SetProcessState(int32_t state) 82 { 83 processState_ = state; 84 } 85 SetProcessFocusState(int32_t focusState)86 inline void SetProcessFocusState(int32_t focusState) 87 { 88 focusState_ = focusState; 89 } 90 GetUid()91 inline int32_t GetUid() const{ return processUid_; } 92 GetState()93 inline int32_t GetState() const { return processState_; } 94 GetFocusState()95 inline int32_t GetFocusState() const { return focusState_; } 96 97 private: 98 const int32_t maxSysUid_ = 10000; 99 int32_t processUid_; 100 int32_t processState_; 101 int32_t focusState_; 102 }; 103 104 class HCameraDeviceHolder : public RefBase { 105 public: HCameraDeviceHolder(int32_t pid,int32_t uid,int32_t state,int32_t focusState,sptr<HCameraDevice> device,uint32_t accessTokenId)106 HCameraDeviceHolder(int32_t pid, int32_t uid, int32_t state, int32_t focusState, 107 sptr<HCameraDevice> device, uint32_t accessTokenId) 108 :pid_(pid), uid_(uid), state_(state), focusState_(focusState), accessTokenId_(accessTokenId), device_(device) 109 { 110 processPriority_ = new CameraProcessPriority(uid, state, focusState); 111 } SetPid(int32_t pid)112 inline void SetPid(int32_t pid) { pid_ = pid; } SetUid(int32_t uid)113 inline void SetUid(int32_t uid) { uid_ = uid; } SetState(int32_t state)114 inline void SetState(int32_t state) 115 { 116 processPriority_->SetProcessState(state); 117 state_ = state; 118 } SetFocusState(int32_t focusState)119 inline void SetFocusState(int32_t focusState) 120 { 121 processPriority_->SetProcessFocusState(focusState); 122 focusState_ = focusState; 123 } 124 GetPid()125 inline int32_t GetPid() const {return pid_;} 126 GetUid()127 inline int32_t GetUid() const{ return uid_; } 128 GetState()129 inline int32_t GetState() const { return state_; } 130 GetFocusState()131 inline int32_t GetFocusState() const { return focusState_; } 132 GetAccessTokenId()133 inline uint32_t GetAccessTokenId() const { return accessTokenId_; } 134 GetDevice()135 inline sptr<HCameraDevice> GetDevice() const { return device_; } 136 GetPriority()137 inline sptr<CameraProcessPriority> GetPriority() const {return processPriority_;} 138 139 private: 140 int32_t pid_; 141 int32_t uid_; 142 int32_t state_; 143 int32_t focusState_; 144 uint32_t accessTokenId_; 145 sptr<CameraProcessPriority> processPriority_; 146 sptr<HCameraDevice> device_; 147 }; 148 149 class HCameraDeviceManager : public RefBase { 150 public: 151 ~HCameraDeviceManager(); 152 /** 153 * @brief Get camera device manager instance. 154 * 155 * @return Returns pointer to camera device manager instance. 156 */ 157 static sptr<HCameraDeviceManager> &GetInstance(); 158 159 /** 160 * @brief Add opened device in camera device manager. 161 * 162 * @param device Device that have been turned on. 163 * @param pid Pid for opening the device. 164 */ 165 void AddDevice(pid_t pid, sptr<HCameraDevice> device); 166 167 /** 168 * @brief remove camera in camera device manager. 169 * 170 * @param device Device that have been turned off. 171 */ 172 void RemoveDevice(); 173 174 /** 175 * @brief Get cameraHolder by active process pid. 176 * 177 * @param pid Pid of active process. 178 */ 179 sptr<HCameraDeviceHolder> GetCameraHolderByPid(pid_t pid); 180 181 /** 182 * @brief Get cameras by active process pid. 183 * 184 * @param pid Pid of active process. 185 */ 186 sptr<HCameraDevice> GetCameraByPid(pid_t pidRequest); 187 188 /** 189 * @brief Get process pid device manager instance. 190 * 191 * @return Returns pointer to camera device manager instance. 192 */ 193 pid_t GetActiveClient(); 194 195 void SetStateOfACamera(std::string cameraId, int32_t state); 196 197 void SetPeerCallback(sptr<ICameraBroker>& callback); 198 199 void UnsetPeerCallback(); 200 201 SafeMap<std::string, int32_t> &GetCameraStateOfASide(); 202 203 /** 204 * @brief remove camera in camera device manager. 205 * 206 * @param camerasNeedEvict Devices that need to be shut down. 207 * @param cameraIdRequestOpen device is requested to turn on. 208 */ 209 bool GetConflictDevices(sptr<HCameraDevice> &camerasNeedEvict, sptr<HCameraDevice> cameraIdRequestOpen); 210 private: 211 HCameraDeviceManager(); 212 static sptr<HCameraDeviceManager> cameraDeviceManager_; 213 static std::mutex instanceMutex_; 214 SafeMap<pid_t, sptr<HCameraDeviceHolder>> pidToCameras_; 215 SafeMap<std::string, int32_t> stateOfACamera_; 216 std::mutex mapMutex_; 217 sptr<ICameraBroker> peerCallback_; 218 std::mutex peerCbMutex_; 219 std::string GetACameraId(); 220 bool IsAllowOpen(pid_t activeClient); 221 void UpdateProcessState(int32_t& activeState, int32_t& requestState, 222 uint32_t activeAccessTokenId, uint32_t requestAccessTokenId); 223 void PrintClientInfo(sptr<HCameraDeviceHolder> activeCameraHolder, sptr<HCameraDeviceHolder> requestCameraHolder); 224 }; 225 } // namespace CameraStandard 226 } // namespace OHOS 227 #endif // OHOS_CAMERA_H_CAMERA_DEVICE_MANAGER_H