From 2ac7e1481ddb0de7211be2b969b1b96848dfcbfc Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Wed, 15 Jan 2025 10:43:44 +0000 Subject: [PATCH] resolve the problem of camera enabling Signed-off-by: zhuzhihui7 --- .../channel/include/dcamera_softbus_adapter.h | 2 + .../channel/src/dcamera_softbus_adapter.cpp | 45 +++++++++++++++---- .../include/pipeline/dcamera_pipeline_sink.h | 3 ++ .../encoder/encode_data_process.h | 3 ++ .../src/pipeline/dcamera_pipeline_sink.cpp | 3 ++ .../encoder/encode_data_process.cpp | 6 ++- 6 files changed, 52 insertions(+), 10 deletions(-) diff --git a/services/channel/include/dcamera_softbus_adapter.h b/services/channel/include/dcamera_softbus_adapter.h index 9aa91b7e..103aef7d 100644 --- a/services/channel/include/dcamera_softbus_adapter.h +++ b/services/channel/include/dcamera_softbus_adapter.h @@ -78,6 +78,7 @@ private: int32_t DCameraSoftbusSourceGetSession(int32_t socket, std::shared_ptr& session); int32_t DCameraSoftbusSinkGetSession(int32_t socket, std::shared_ptr& session); std::string FindSessNameByPeerSessName(const std::string peerSessionName); + std::string ReplaceSuffix(std::string &mySessNmRep, const std::string &suffix, const std::string &replacement); private: std::mutex optLock_; @@ -92,6 +93,7 @@ private: std::map sessionModeAndDataTypeMap_; std::mutex mySessionNamePeerDevIdLock_; std::map peerDevIdMySessionNameMap_; + std::map mySessionNameMapV2_; std::mutex mySocketSetLock_; std::set mySocketSet_; diff --git a/services/channel/src/dcamera_softbus_adapter.cpp b/services/channel/src/dcamera_softbus_adapter.cpp index d608e25b..eb9aa8a7 100644 --- a/services/channel/src/dcamera_softbus_adapter.cpp +++ b/services/channel/src/dcamera_softbus_adapter.cpp @@ -142,6 +142,22 @@ DCameraSoftbusAdapter::~DCameraSoftbusAdapter() { } +std::string DCameraSoftbusAdapter::ReplaceSuffix(std::string &mySessNmRep, const std::string &suffix, + const std::string &replacement) +{ + DHLOGI("replacing suffix in mySessionName: %{public}s", mySessNmRep.c_str()); + bool isModified = false; + if (mySessNmRep.length() >= suffix.length() && + mySessNmRep.compare(mySessNmRep.length() - suffix.length(), suffix.length(), + suffix) == 0) { + mySessNmRep.replace(mySessNmRep.length() - suffix.length(), suffix.length(), + replacement); + isModified = true; + } + DHLOGI("suffix replaced? %{public}s - Modified: %{public}s", isModified ? "Y" : "N", mySessNmRep.c_str()); + return mySessNmRep; +} + int32_t DCameraSoftbusAdapter::CreatSoftBusSinkSocketServer(std::string mySessionName, DCAMERA_CHANNEL_ROLE role, DCameraSessionMode sessionMode, std::string peerDevId, std::string peerSessionName) { @@ -186,9 +202,14 @@ int32_t DCameraSoftbusAdapter::CreatSoftBusSinkSocketServer(std::string mySessio std::lock_guard autoLock(mySessionNamePeerDevIdLock_); std::string peerDevIdMySessionName = peerDevId + std::string("_") + mySessionName; peerDevIdMySessionNameMap_[peerDevIdMySessionName] = mySessionName; + } else { + std::lock_guard autoLock(mySessionNamePeerDevIdLock_); + std::string mySessNmRep(mySessionName); + std::string peerDevIdMySessionName = ReplaceSuffix(mySessNmRep, "_sender", "_receiver"); + mySessionNameMapV2_[peerDevIdMySessionName] = mySessNmRep; } DHLOGI("create socket server end, mySessionName: %{public}s, peerSessionName: %{public}s", - GetAnonyString(mySessionName).c_str(), GetAnonyString(peerSessionName).c_str()); + GetAnonyString(mySessionName).c_str(), GetAnonyString(peerSessionName).c_str()); return DCAMERA_OK; } @@ -529,16 +550,22 @@ int32_t DCameraSoftbusAdapter::DCameraSoftBusGetSessionByPeerSocket(int32_t sock } mySessionName = sessionNameIter->second; } else { - mySessionName = FindSessNameByPeerSessName(info.name); - } - if (mySessionName.empty()) { - DHLOGE("find mySessionName is empty"); - return DCAMERA_BAD_VALUE; + std::lock_guard autoLock(mySessionNamePeerDevIdLock_); + auto sessionNameIter = mySessionNameMapV2_.find(info.name); + if (sessionNameIter == mySessionNameMapV2_.end()) { + DHLOGE("find session by peer socket error, socket %{public}d", socket); + return DCAMERA_NOT_FOUND; + } + mySessionName = sessionNameIter->second; + if (mySessionName.empty()) { + DHLOGE("find mySessionName is empty"); + return DCAMERA_BAD_VALUE; + } + mySessionName = ReplaceSuffix(mySessionName, "_receiver", "_sender"); } - auto iter = sinkSessions_.find(std::string(mySessionName)); + auto iter = sinkSessions_.find(mySessionName); if (iter == sinkSessions_.end()) { - DHLOGE("find session by peer socket error, mySessionName %{public}s", - GetAnonyString(mySessionName).c_str()); + DHLOGE("find session by peer socket error, mySessionName %{public}s", GetAnonyString(mySessionName).c_str()); return DCAMERA_NOT_FOUND; } session = iter->second; diff --git a/services/data_process/include/pipeline/dcamera_pipeline_sink.h b/services/data_process/include/pipeline/dcamera_pipeline_sink.h index fdf09458..1f4cac6a 100644 --- a/services/data_process/include/pipeline/dcamera_pipeline_sink.h +++ b/services/data_process/include/pipeline/dcamera_pipeline_sink.h @@ -58,6 +58,9 @@ private: constexpr static int32_t MAX_VIDEO_HEIGHT = 1080; std::shared_ptr processListener_ = nullptr; + std::mutex processListenerMtx_; + std::condition_variable processListenerCond_; + std::shared_ptr pipelineHead_ = nullptr; bool isProcess_ = false; diff --git a/services/data_process/include/pipeline_node/multimedia_codec/encoder/encode_data_process.h b/services/data_process/include/pipeline_node/multimedia_codec/encoder/encode_data_process.h index 87b1dfff..ea3b151a 100644 --- a/services/data_process/include/pipeline_node/multimedia_codec/encoder/encode_data_process.h +++ b/services/data_process/include/pipeline_node/multimedia_codec/encoder/encode_data_process.h @@ -132,6 +132,9 @@ private: sptr encodeProducerSurface_ = nullptr; std::atomic isEncoderProcess_ = false; + std::mutex isEncoderProcessMtx_; + std::condition_variable isEncoderProcessCond_; + int32_t waitEncoderOutputCount_ = 0; int64_t lastFeedEncoderInputBufferTimeUs_ = 0; int64_t inputTimeStampUs_ = 0; diff --git a/services/data_process/src/pipeline/dcamera_pipeline_sink.cpp b/services/data_process/src/pipeline/dcamera_pipeline_sink.cpp index c3103305..a4943e0b 100644 --- a/services/data_process/src/pipeline/dcamera_pipeline_sink.cpp +++ b/services/data_process/src/pipeline/dcamera_pipeline_sink.cpp @@ -65,6 +65,7 @@ int32_t DCameraPipelineSink::CreateDataProcessPipeline(PipelineType piplineType, } piplineType_ = piplineType; processListener_ = listener; + processListenerCond_.notify_all(); isProcess_ = true; return DCAMERA_OK; } @@ -177,6 +178,8 @@ void DCameraPipelineSink::OnError(DataProcessErrorType errorType) void DCameraPipelineSink::OnProcessedVideoBuffer(const std::shared_ptr& videoResult) { DHLOGD("Sink pipeline output the processed video buffer."); + std::unique_lock lock(processListenerMtx_); + processListenerCond_.wait(lock, [this] { return processListener_ != nullptr; }); if (processListener_ == nullptr) { DHLOGE("The process listener of sink pipeline is empty."); return; diff --git a/services/data_process/src/pipeline_node/multimedia_codec/encoder/encode_data_process.cpp b/services/data_process/src/pipeline_node/multimedia_codec/encoder/encode_data_process.cpp index 09bbc934..4728bc0e 100644 --- a/services/data_process/src/pipeline_node/multimedia_codec/encoder/encode_data_process.cpp +++ b/services/data_process/src/pipeline_node/multimedia_codec/encoder/encode_data_process.cpp @@ -74,6 +74,7 @@ int32_t EncodeDataProcess::InitNode(const VideoConfigParams& sourceConfig, const processedConfig_ = sourceConfig; processedConfig = processedConfig_; isEncoderProcess_.store(true); + isEncoderProcessCond_.notify_all(); return DCAMERA_OK; } @@ -85,6 +86,7 @@ int32_t EncodeDataProcess::InitNode(const VideoConfigParams& sourceConfig, const } processedConfig = processedConfig_; isEncoderProcess_.store(true); + isEncoderProcessCond_.notify_all(); return DCAMERA_OK; } @@ -460,7 +462,7 @@ int32_t EncodeDataProcess::GetEncoderOutputBuffer(uint32_t index, MediaAVCodec:: DHLOGE("Failed to get the output shared memory, index : %{public}u", index); return DCAMERA_BAD_OPERATE; } - + CHECK_AND_RETURN_RET_LOG(info.size <= 0 || info.size > DATABUFF_MAX_SIZE, DCAMERA_BAD_VALUE, "AVCodecBufferInfo error, buffer size : %{public}d", info.size); size_t outputMemoDataSize = static_cast(info.size); @@ -541,6 +543,8 @@ void EncodeDataProcess::OnOutputFormatChanged(const Media::Format &format) void EncodeDataProcess::OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info, MediaAVCodec::AVCodecBufferFlag flag, std::shared_ptr buffer) { + std::unique_lock lock(isEncoderProcessMtx_); + isEncoderProcessCond_.wait(lock, [this] { return isEncoderProcess_.load(); }); if (!isEncoderProcess_.load()) { DHLOGE("EncodeNode occurred error or start release."); return; -- Gitee