diff --git a/bundle.json b/bundle.json index bb98223786fd5cdea6724ad97980a1b5b3b1ce7f..6db1a4b6770816b74fc146a805f849548bd84017 100644 --- a/bundle.json +++ b/bundle.json @@ -39,6 +39,7 @@ "drivers_peripheral_display", "c_utils", "dsoftbus", + "hicollie", "hisysevent", "hilog", "samgr", 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 4b29786bbd1f9b34192283dc269012f811074296..d5ace0be830914f695a31a82ea9efa9451f62970 100644 --- a/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h +++ b/services/cameraservice/sourceservice/include/distributedcamera/distributed_camera_source_service.h @@ -64,6 +64,7 @@ private: bool Init(); int32_t LoadDCameraHDF(); int32_t UnLoadCameraHDF(); + void ListenAudioDev(); bool registerToService_ = false; DCameraServiceState state_ = DCameraServiceState::DCAMERA_SRV_STATE_NOT_START; @@ -72,9 +73,14 @@ private: std::string sourceVer_; const size_t MAX_CAMERAS_NUMBER = 32; const size_t DUMP_MAX_SIZE = 10 * 1024; + static constexpr const char* LISTEN_THREAD = "sourceListenTh"; + static constexpr int32_t WATCHDOG_INTERVAL_TIME = 20000; + static constexpr int32_t WATCHDOG_DELAY_TIME = 5000; + static constexpr size_t SLEEP_TIME = 5000; static std::map> camerasMap_; static std::mutex camDevMutex_; + std::thread listenThread_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamera_source_dev.h b/services/cameraservice/sourceservice/include/distributedcameramgr/dcamera_source_dev.h index fce546f046f19da3ad401f49926467eb0acc4587..0c1de647b08326821a6280c8e87861142db1edcd 100644 --- a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamera_source_dev.h +++ b/services/cameraservice/sourceservice/include/distributedcameramgr/dcamera_source_dev.h @@ -56,6 +56,10 @@ public: int32_t OnChannelConnectedEvent(); int32_t OnChannelDisconnectedEvent(); + int32_t RestoreThreadStatus(); + void SetThreadStatusFlag(); + bool GetThreadStatusFlag(); + public: virtual int32_t Register(std::shared_ptr& param); virtual int32_t UnRegister(std::shared_ptr& param); @@ -78,6 +82,7 @@ private: void NotifyRegisterResult(DCAMERA_EVENT eventType, DCameraSourceEvent& event, int32_t result); void NotifyHalResult(DCAMERA_EVENT eventType, DCameraSourceEvent& event, int32_t result); void HitraceAndHisyseventImpl(std::vector>& captureInfos); + void SetThreadStatusFlagTrue(const AppExecFwk::InnerEvent::Pointer &event); private: std::string devId_; @@ -90,6 +95,8 @@ private: std::shared_ptr controller_; std::shared_ptr input_; sptr hdiCallback_; + std::atomic threadStatusFlag_ = false; + std::weak_ptr sourceDev_; std::map memberFuncMap_; std::map eventResultMap_; diff --git a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamera_source_event.h b/services/cameraservice/sourceservice/include/distributedcameramgr/dcamera_source_event.h index 725bba076d422656c0a551da6c033d632ca8cb6b..bc43f911f2932ae634a41d68a22623d51cc2e9d6 100644 --- a/services/cameraservice/sourceservice/include/distributedcameramgr/dcamera_source_event.h +++ b/services/cameraservice/sourceservice/include/distributedcameramgr/dcamera_source_event.h @@ -38,6 +38,7 @@ typedef enum { DCAMERA_EVENT_STOP_CAPTURE = 7, DCAMERA_EVENT_UPDATE_SETTINGS = 8, DCAMERA_EVENT_NOFIFY = 9, + DCAMERA_EVENT_SET_THREAD_STATUS = 10, } DCAMERA_EVENT; class DCameraRegistParam { diff --git a/services/cameraservice/sourceservice/include/distributedcameramgr/dcameradata/dcamera_stream_data_process_producer.h b/services/cameraservice/sourceservice/include/distributedcameramgr/dcameradata/dcamera_stream_data_process_producer.h index f133fcfdb8533c67ef46eadbe56e139d47c6b8b1..18283b2871ed9d156bb5afd5ea557751c96bc54d 100644 --- a/services/cameraservice/sourceservice/include/distributedcameramgr/dcameradata/dcamera_stream_data_process_producer.h +++ b/services/cameraservice/sourceservice/include/distributedcameramgr/dcameradata/dcamera_stream_data_process_producer.h @@ -60,6 +60,7 @@ private: std::thread eventThread_; std::thread producerThread_; + std::thread listenThread_; std::condition_variable eventCon_; std::condition_variable producerCon_; std::mutex bufferMutex_; 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 952e414424c28a3a5129481cf86009ed4238f4e0..9e8ecf8f9b58ad5be15c87d9455fdd6ef25fda29 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp @@ -21,6 +21,7 @@ #include "iservice_registry.h" #include "string_ex.h" #include "system_ability_definition.h" +#include "xcollie/watchdog.h" #include "anonymous_string.h" #include "dcamera_hdf_operate.h" @@ -55,6 +56,10 @@ void DistributedCameraSourceService::OnStart() DHLOGE("DistributedCameraSourceService init failed"); return; } + listenThread_ = std::thread(&DistributedCameraSourceService::ListenCameraDev, this); + if (pthread_setname_np(listenThread_.native_handle(), LISTEN_THREAD) != DH_SUCCESS) { + DHLOGE("Dev clear thread setname failed."); + } state_ = DCameraServiceState::DCAMERA_SRV_STATE_RUNNING; DHLOGI("start service success."); } @@ -82,6 +87,9 @@ void DistributedCameraSourceService::OnStop() state_ = DCameraServiceState::DCAMERA_SRV_STATE_NOT_START; registerToService_ = false; listener_ = nullptr; + if (listenThread_.joinable()) { + listenThread_.join(); + } DCameraSourceServiceIpc::GetInstance().UnInit(); } @@ -279,6 +287,33 @@ void DistributedCameraSourceService::CamDevInsert(DCameraIndex& index, std::shar camerasMap_.emplace(index, camDev); } +void DistributedCameraSourceService::ListenCameraDev() +{ + auto taskFunc = [this]() { + std::lock_guard lock(devMapMtx_); + for (auto &iter : audioDevMap_) { + if (iter.second.dev->GetThreadStatusFlag()) { + iter.second.dev->SetThreadStatusFlag(); + } else { + DHLOGE("Exit the current proces"); + _Exit(0); + } + } + }; + OHOS::HiviewDFX::Watchdog::GetInstance().RunPeriodicalTask("SourceService", taskFunc, + WATCHDOG_INTERVAL_TIME, WATCHDOG_DELAY_TIME); + + while (true) { + std::lock_guard lock(devMapMtx_); + if (!eventBus_.empty()) { + for (auto &iter : eventBus_) { + iter.second.dev->RestoreThreadStatus(); + } + } + usleep(SLEEP_TIME); + } +} + std::shared_ptr DistributedCameraSourceService::GetCamDevByIndex(DCameraIndex& index) { std::lock_guard camLock(camDevMutex_); diff --git a/services/cameraservice/sourceservice/src/distributedcameramgr/dcamera_source_dev.cpp b/services/cameraservice/sourceservice/src/distributedcameramgr/dcamera_source_dev.cpp index ba11a523a96ff308fa45df536ebe7a8fb25c9eb4..ad1149c351b116c8d47dcfd5c1cf45f19f80021e 100644 --- a/services/cameraservice/sourceservice/src/distributedcameramgr/dcamera_source_dev.cpp +++ b/services/cameraservice/sourceservice/src/distributedcameramgr/dcamera_source_dev.cpp @@ -45,6 +45,7 @@ DCameraSourceDev::DCameraSourceDev(std::string devId, std::string dhId, memberFuncMap_[DCAMERA_EVENT_START_CAPTURE] = &DCameraSourceDev::NotifyHalResult; memberFuncMap_[DCAMERA_EVENT_STOP_CAPTURE] = &DCameraSourceDev::NotifyHalResult; memberFuncMap_[DCAMERA_EVENT_UPDATE_SETTINGS] = &DCameraSourceDev::NotifyHalResult; + memberFuncMap_[DCAMERA_EVENT_SET_THREAD_STATUS] = &DCameraSourceDev::SetThreadStatusFlagTrue; eventResultMap_[DCAMERA_EVENT_OPEN] = DCAMERA_EVENT_OPEN_CHANNEL_ERROR; eventResultMap_[DCAMERA_EVENT_CLOSE] = DCAMERA_EVENT_CLOSE_CHANNEL_ERROR; @@ -545,6 +546,44 @@ int32_t DCameraSourceDev::CameraEventNotify(std::shared_ptr& event return DCAMERA_OK; } +int32_t DCameraSourceDev::RestoreThreadStatus() +{ + if (handler_ == nullptr) { + DHLOGI("Event handler is null."); + return ERR_DH_AUDIO_NULLPTR; + } + json jParam; + auto eventParam = std::make_shared(jParam); + auto msgEvent = AppExecFwk::InnerEvent::Get(DCAMERA_EVENT_SET_THREAD_STATUS, eventParam, 0); + if (!eventBus_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Enable audio task generate successfully."); + return DH_SUCCESS; +} + +bool DCameraSourceDev::GetThreadStatusFlag() +{ + return threadStatusFlag_; +} + +void DCameraSourceDev::SetThreadStatusFlag() +{ + threadStatusFlag_ = false; +} + +void DCameraSourceDev::SetThreadStatusFlagTrue(const AppExecFwk::InnerEvent::Pointer &event) +{ + (void) event; + auto sourceDevobj = sourceDev_.lock(); + if (sourceDevobj == nullptr) { + DHLOGE("Source dev is invaild."); + return; + } + sourceDevobj->threadStatusFlag_ = true; +} + void DCameraSourceDev::NotifyResult(DCAMERA_EVENT eventType, DCameraSourceEvent& event, int32_t result) { auto itFunc = memberFuncMap_.find(eventType);