From 4f2b3a8a9c2be1bd203ff9ea5a0825baf5b92203 Mon Sep 17 00:00:00 2001 From: zhuxu Date: Fri, 25 Nov 2022 14:10:51 +0800 Subject: [PATCH] fix: distributed camera bug Signed-off-by: zhuxu --- .../dcamera_sink_data_process.h | 3 ++ .../dcamera_sink_data_process.cpp | 34 ++++++++++++++++--- .../dcamera_sink_data_process_common.cpp | 29 +++++++++++++--- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/services/cameraservice/sinkservice/include/distributedcameramgr/dcamera_sink_data_process.h b/services/cameraservice/sinkservice/include/distributedcameramgr/dcamera_sink_data_process.h index 01c108d3..353f3658 100644 --- a/services/cameraservice/sinkservice/include/distributedcameramgr/dcamera_sink_data_process.h +++ b/services/cameraservice/sinkservice/include/distributedcameramgr/dcamera_sink_data_process.h @@ -21,6 +21,8 @@ #include "dcamera_video_output_event.h" #include "icamera_sink_data_process.h" +#include + #include "icamera_channel.h" #include "idata_process_pipeline.h" #include "image_common_type.h" @@ -50,6 +52,7 @@ private: VideoCodecType GetPipelineCodecType(DCEncodeType encodeType); Videoformat GetPipelineFormat(int32_t format); + std::mutex eventLock_; std::string dhId_; std::shared_ptr captureInfo_; std::shared_ptr eventBus_; diff --git a/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_data_process.cpp b/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_data_process.cpp index 1133c184..6016c07c 100644 --- a/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_data_process.cpp +++ b/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_data_process.cpp @@ -29,11 +29,6 @@ DCameraSinkDataProcess::DCameraSinkDataProcess(const std::string& dhId, std::sha : dhId_(dhId), channel_(channel) { DHLOGI("DCameraSinkDataProcess Constructor dhId: %s", GetAnonyString(dhId_).c_str()); - eventBus_ = std::make_shared("SinkDPHandler"); - DCameraPhotoOutputEvent photoEvent(*this); - DCameraVideoOutputEvent videoEvent(*this); - eventBus_->AddHandler(photoEvent.GetType(), *this); - eventBus_->AddHandler(videoEvent.GetType(), *this); } int32_t DCameraSinkDataProcess::StartCapture(std::shared_ptr& captureInfo) @@ -42,6 +37,18 @@ int32_t DCameraSinkDataProcess::StartCapture(std::shared_ptr GetAnonyString(dhId_).c_str(), captureInfo->width_, captureInfo->height_, captureInfo->format_, captureInfo->streamType_, captureInfo->encodeType_); captureInfo_ = captureInfo; + { + std::lock_guard eventMutex(eventLock_); + if (eventBus_ == nullptr) { + DHLOGI("DCameraSinkDataProcess::StartCapture %s create event bus", GetAnonyString(dhId_).c_str()); + eventBus_ = std::make_shared("SinkDPHandler"); + DCameraPhotoOutputEvent photoEvent(*this); + DCameraVideoOutputEvent videoEvent(*this); + eventBus_->AddHandler(photoEvent.GetType(), *this); + eventBus_->AddHandler(videoEvent.GetType(), *this); + } + } + if (pipeline_ != nullptr) { DHLOGI("DCameraSinkDataProcess::StartCapture %s pipeline already exits", GetAnonyString(dhId_).c_str()); return DCAMERA_OK; @@ -80,6 +87,8 @@ int32_t DCameraSinkDataProcess::StopCapture() pipeline_->DestroyDataProcessPipeline(); pipeline_ = nullptr; } + std::lock_guard eventMutex(eventLock_); + eventBus_ = nullptr; return DCAMERA_OK; } @@ -98,6 +107,11 @@ int32_t DCameraSinkDataProcess::FeedStream(std::shared_ptr& dataBuff break; } case SNAPSHOT_FRAME: { + std::lock_guard eventMutex(eventLock_); + if (eventBus_ == nullptr) { + DHLOGE("DCameraSinkDataProcess::FeedStream %s event bus is null", GetAnonyString(dhId_).c_str()); + return DCAMERA_BAD_VALUE; + } DCameraPhotoOutputEvent photoEvent(*this, dataBuffer); eventBus_->PostEvent(photoEvent, POSTMODE::POST_ASYNC); break; @@ -131,6 +145,11 @@ void DCameraSinkDataProcess::OnEvent(DCameraVideoOutputEvent& event) void DCameraSinkDataProcess::OnProcessedVideoBuffer(const std::shared_ptr& videoResult) { + std::lock_guard eventMutex(eventLock_); + if (eventBus_ == nullptr) { + DHLOGE("DCameraSinkDataProcess::OnProcessedVideoBuffer %s event bus is null", GetAnonyString(dhId_).c_str()); + return; + } DCameraVideoOutputEvent videoEvent(*this, videoResult); eventBus_->PostEvent(videoEvent, POSTMODE::POST_ASYNC); } @@ -145,6 +164,11 @@ int32_t DCameraSinkDataProcess::FeedStreamInner(std::shared_ptr& dat { if (captureInfo_->format_ == captureInfo_->encodeType_) { DHLOGI("DCameraSinkDataProcess::FeedStreamInner %s send video buffer", GetAnonyString(dhId_).c_str()); + std::lock_guard eventMutex(eventLock_); + if (eventBus_ == nullptr) { + DHLOGE("DCameraSinkDataProcess::FeedStreamInner %s event bus is null", GetAnonyString(dhId_).c_str()); + return DCAMERA_BAD_VALUE; + } DCameraVideoOutputEvent videoEvent(*this, dataBuffer); eventBus_->PostEvent(videoEvent, POSTMODE::POST_ASYNC); return DCAMERA_OK; diff --git a/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_data_process_common.cpp b/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_data_process_common.cpp index 8c76340f..67504e3b 100644 --- a/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_data_process_common.cpp +++ b/services/cameraservice/sinkservice/src/distributedcameramgr/dcamera_sink_data_process_common.cpp @@ -29,11 +29,6 @@ DCameraSinkDataProcess::DCameraSinkDataProcess(const std::string& dhId, std::sha : dhId_(dhId), channel_(channel) { DHLOGI("DCameraSinkDataProcess Constructor dhId: %s", GetAnonyString(dhId_).c_str()); - eventBus_ = std::make_shared("SinkDPHandler"); - DCameraPhotoOutputEvent photoEvent(*this); - DCameraVideoOutputEvent videoEvent(*this); - eventBus_->AddHandler(photoEvent.GetType(), *this); - eventBus_->AddHandler(videoEvent.GetType(), *this); } int32_t DCameraSinkDataProcess::StartCapture(std::shared_ptr& captureInfo) @@ -42,6 +37,18 @@ int32_t DCameraSinkDataProcess::StartCapture(std::shared_ptr GetAnonyString(dhId_).c_str(), captureInfo->width_, captureInfo->height_, captureInfo->format_, captureInfo->streamType_, captureInfo->encodeType_); captureInfo_ = captureInfo; + { + std::lock_guard eventMutex(eventLock_); + if (eventBus_ == nullptr) { + DHLOGI("DCameraSinkDataProcess::StartCapture %s create event bus", GetAnonyString(dhId_).c_str()); + eventBus_ = std::make_shared("SinkDPHandler"); + DCameraPhotoOutputEvent photoEvent(*this); + DCameraVideoOutputEvent videoEvent(*this); + eventBus_->AddHandler(photoEvent.GetType(), *this); + eventBus_->AddHandler(videoEvent.GetType(), *this); + } + } + if (pipeline_ != nullptr) { DHLOGI("DCameraSinkDataProcess::StartCapture %s pipeline already exits", GetAnonyString(dhId_).c_str()); return DCAMERA_OK; @@ -80,6 +87,8 @@ int32_t DCameraSinkDataProcess::StopCapture() pipeline_->DestroyDataProcessPipeline(); pipeline_ = nullptr; } + std::lock_guard eventMutex(eventLock_); + eventBus_ = nullptr; return DCAMERA_OK; } @@ -98,6 +107,11 @@ int32_t DCameraSinkDataProcess::FeedStream(std::shared_ptr& dataBuff break; } case SNAPSHOT_FRAME: { + std::lock_guard eventMutex(eventLock_); + if (eventBus_ == nullptr) { + DHLOGE("DCameraSinkDataProcess::FeedStream %s event bus is null", GetAnonyString(dhId_).c_str()); + return DCAMERA_BAD_VALUE; + } DCameraPhotoOutputEvent photoEvent(*this, dataBuffer); eventBus_->PostEvent(photoEvent, POSTMODE::POST_ASYNC); break; @@ -131,6 +145,11 @@ void DCameraSinkDataProcess::OnEvent(DCameraVideoOutputEvent& event) void DCameraSinkDataProcess::OnProcessedVideoBuffer(const std::shared_ptr& videoResult) { + std::lock_guard eventMutex(eventLock_); + if (eventBus_ == nullptr) { + DHLOGE("DCameraSinkDataProcess::OnProcessedVideoBuffer %s event bus is null", GetAnonyString(dhId_).c_str()); + return; + } DCameraVideoOutputEvent videoEvent(*this, videoResult); eventBus_->PostEvent(videoEvent, POSTMODE::POST_ASYNC); } -- Gitee