diff --git a/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h b/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h index 8da2db80f82b5a0ed695121bbbabf15d3808181a..57e8d4befd94e6046b73e6ad2983053150a115a5 100644 --- a/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h +++ b/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h @@ -50,7 +50,10 @@ public: int Dump(int32_t fd, const std::vector& args) override; static void GetDumpInfo(CameraDumpInfo& camDump); - static std::map> camerasMap_; + static void CamDevInsert(DCameraIndex& index, std::shared_ptr& camDev); + static std::shared_ptr GetCamDevByIndex(DCameraIndex& index); + static void CamDevErase(DCameraIndex& index); + static uint32_t GetCamDevNum(); protected: void OnStart() override; @@ -69,6 +72,9 @@ private: std::string sourceVer_; const size_t MAX_CAMERAS_NUMBER = 32; const size_t DUMP_MAX_SIZE = 10 * 1024; + + static std::map> camerasMap_; + static std::mutex camDevMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp index bca5ee252999fc33b5c6b6c4a0d91db767f0ec67..6aa1dd7eb38f98f0c3a99d9e45dfc57b4b5ba33c 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/dcamera_service_state_listener.cpp @@ -55,7 +55,7 @@ int32_t DCameraServiceStateListener::OnRegisterNotify(const std::string& devId, DHLOGI("DCameraServiceStateListener OnRegisterNotify thread delete devId: %s dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); DCameraIndex camIndex(devId, dhId); - DistributedCameraSourceService::camerasMap_.erase(camIndex); + DistributedCameraSourceService::CamDevErase(camIndex); if (callbackProxy_ == nullptr) { DHLOGE("DCameraServiceStateListener OnRegisterNotify callbackProxy_ is nullptr"); return; @@ -94,7 +94,7 @@ int32_t DCameraServiceStateListener::OnUnregisterNotify(const std::string& devId DHLOGI("DCameraServiceStateListener OnUnregisterNotify thread delete devId: %s dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); DCameraIndex camIndex(devId, dhId); - DistributedCameraSourceService::camerasMap_.erase(camIndex); + DistributedCameraSourceService::CamDevErase(camIndex); int32_t ret = callbackProxy_->OnNotifyUnregResult(devId, dhId, reqId, status, data); if (ret != DCAMERA_OK) { diff --git a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp index 32e2b987a907628d52832b2df651e60d0c1ba08b..fb0dfadd690f0e124e0ffaf5cd5baf5949763f0f 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp @@ -37,6 +37,7 @@ namespace DistributedHardware { REGISTER_SYSTEM_ABILITY_BY_ID(DistributedCameraSourceService, DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID, true); std::map> DistributedCameraSourceService::camerasMap_; +std::mutex DistributedCameraSourceService::camDevMutex_; DistributedCameraSourceService::DistributedCameraSourceService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) @@ -117,15 +118,14 @@ int32_t DistributedCameraSourceService::RegisterDistributedHardware(const std::s { DHLOGI("DistributedCameraSourceService RegisterDistributedHardware devId: %s, dhId: %s, version: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), param.version.c_str()); - if (camerasMap_.size() > MAX_CAMERAS_NUMBER) { + if (GetCamDevNum() > MAX_CAMERAS_NUMBER) { DHLOGE("DistributedCameraSourceService RegisterDistributedHardware cameras exceed the upper limit"); return DCAMERA_BAD_VALUE; } DCameraIndex camIndex(devId, dhId); - std::shared_ptr camDev = nullptr; int32_t ret = DCAMERA_OK; - auto iter = camerasMap_.find(camIndex); - if (iter == camerasMap_.end()) { + std::shared_ptr camDev = GetCamDevByIndex(camIndex); + if (camDev == nullptr) { DHLOGI("DistributedCameraSourceService RegisterDistributedHardware new dev devId: %s, dhId: %s, version: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), param.version.c_str()); camDev = std::make_shared(devId, dhId, listener_); @@ -138,7 +138,7 @@ int32_t DistributedCameraSourceService::RegisterDistributedHardware(const std::s ret, GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); return ret; } - camerasMap_.emplace(camIndex, camDev); + CamDevInsert(camIndex, camDev); } else { DHLOGE("DistributedCameraSourceService RegisterDistributedHardware exist devId: %s, dhId: %s, version: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), param.version.c_str()); @@ -150,7 +150,7 @@ int32_t DistributedCameraSourceService::RegisterDistributedHardware(const std::s DHLOGE("DistributedCameraSourceService RegisterDistributedHardware failed, ret: %d", ret); ReportRegisterCameraFail(DCAMERA_REGISTER_FAIL, GetAnonyString(devId), dhId, param.version, "dcamera source RegisterDistributedHardware fail."); - camerasMap_.erase(camIndex); + CamDevErase(camIndex); } DHLOGI("DistributedCameraSourceService RegisterDistributedHardware end devId: %s, dhId: %s, version: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), param.version.c_str()); @@ -163,13 +163,12 @@ int32_t DistributedCameraSourceService::UnregisterDistributedHardware(const std: DHLOGI("DistributedCameraSourceService UnregisterDistributedHardware devId: %s, dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); DCameraIndex camIndex(devId, dhId); - auto iter = camerasMap_.find(camIndex); - if (iter == camerasMap_.end()) { + std::shared_ptr camDev = GetCamDevByIndex(camIndex); + if (camDev == nullptr) { DHLOGE("DistributedCameraSourceService UnregisterDistributedHardware not found device"); return DCAMERA_NOT_FOUND; } - std::shared_ptr camDev = iter->second; int32_t ret = camDev->UnRegisterDistributedHardware(devId, dhId, reqId); if (ret != DCAMERA_OK) { DHLOGE("DistributedCameraSourceService UnregisterDistributedHardware failed, ret: %d", ret); @@ -183,13 +182,12 @@ int32_t DistributedCameraSourceService::DCameraNotify(const std::string& devId, DHLOGI("DistributedCameraSourceService DCameraNotify devId: %s, dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); DCameraIndex camIndex(devId, dhId); - auto iter = camerasMap_.find(camIndex); - if (iter == camerasMap_.end()) { + std::shared_ptr camDev = GetCamDevByIndex(camIndex); + if (camDev == nullptr) { DHLOGE("DistributedCameraSourceService DCameraNotify not found device"); return DCAMERA_NOT_FOUND; } - std::shared_ptr camDev = iter->second; int32_t ret = camDev->DCameraNotify(events); if (ret != DCAMERA_OK) { DHLOGE("DistributedCameraSourceService DCameraNotify failed, ret: %d", ret); @@ -252,6 +250,7 @@ int DistributedCameraSourceService::Dump(int32_t fd, const std::vector camLock(camDevMutex_); camDump.regNumber = static_cast(camerasMap_.size()); std::map curState; for (auto it = camerasMap_.begin(); it != camerasMap_.end(); it++) { @@ -265,5 +264,33 @@ void DistributedCameraSourceService::GetDumpInfo(CameraDumpInfo& camDump) } camDump.curState = curState; } + +void DistributedCameraSourceService::CamDevInsert(DCameraIndex& index, std::shared_ptr& camDev) +{ + std::lock_guard camLock(camDevMutex_); + camerasMap_.emplace(index, camDev); +} + +std::shared_ptr DistributedCameraSourceService::GetCamDevByIndex(DCameraIndex& index) +{ + std::lock_guard camLock(camDevMutex_); + auto iter = camerasMap_.find(index); + if (iter == camerasMap_.end()) { + return nullptr; + } + return iter->second; +} + +void DistributedCameraSourceService::CamDevErase(DCameraIndex& index) +{ + std::lock_guard camLock(camDevMutex_); + camerasMap_.erase(index); +} + +uint32_t DistributedCameraSourceService::GetCamDevNum() +{ + std::lock_guard camLock(camDevMutex_); + return camerasMap_.size(); +} } // namespace DistributedHardware } // namespace OHOS