From e1192e9e619f7093e0369e981817ed997499f044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cry521=E2=80=9D?= Date: Wed, 22 Nov 2023 11:03:46 +0800 Subject: [PATCH] hicoile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “ry521” --- bundle.json | 1 + .../distributed_camera_source_service.h | 6 +++ .../distributedcameramgr/dcamera_source_dev.h | 7 ++++ .../dcamera_source_event.h | 1 + .../dcamera_stream_data_process_producer.h | 1 + .../distributed_camera_source_service.cpp | 35 +++++++++++++++++ .../dcamera_source_dev.cpp | 39 +++++++++++++++++++ 7 files changed, 90 insertions(+) diff --git a/bundle.json b/bundle.json index bb982237..6db1a4b6 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 4b29786b..d5ace0be 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 fce546f0..0c1de647 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 725bba07..bc43f911 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 f133fcfd..18283b28 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 952e4144..9e8ecf8f 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 ba11a523..ad1149c3 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); -- Gitee