From 2f6f25395dd8b336880f83ed305da0ef1dd8a0ef Mon Sep 17 00:00:00 2001 From: byndyx Date: Thu, 16 Nov 2023 20:05:52 +0800 Subject: [PATCH] modify function bug Signed-off-by: byndyx --- .../managersource/include/daudio_source_dev.h | 3 +- .../managersource/src/daudio_source_dev.cpp | 82 ++++++++++++++----- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/services/audiomanager/managersource/include/daudio_source_dev.h b/services/audiomanager/managersource/include/daudio_source_dev.h index efda6cda..e84aaf3e 100644 --- a/services/audiomanager/managersource/include/daudio_source_dev.h +++ b/services/audiomanager/managersource/include/daudio_source_dev.h @@ -122,7 +122,6 @@ private: static constexpr uint8_t EVENT_NOTIFY_CLOSE_MIC = 0x08; static constexpr uint8_t EVENT_NOTIFY_OPEN_CTRL = 0x10; static constexpr uint8_t EVENT_NOTIFY_CLOSE_CTRL = 0x20; - static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000; std::string devId_; std::shared_ptr mgrCallback_; @@ -147,6 +146,8 @@ private: void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; private: + void EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event); + void DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event); void OpenDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event); void CloseDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event); void OpenDMicCallback(const AppExecFwk::InnerEvent::Pointer &event); diff --git a/services/audiomanager/managersource/src/daudio_source_dev.cpp b/services/audiomanager/managersource/src/daudio_source_dev.cpp index 7dc350e9..44463380 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -45,6 +45,8 @@ constexpr uint32_t EVENT_MMAP_SPK_START = 81; constexpr uint32_t EVENT_MMAP_SPK_STOP = 82; constexpr uint32_t EVENT_MMAP_MIC_START = 83; constexpr uint32_t EVENT_MMAP_MIC_STOP = 84; +constexpr uint32_t EVENT_DAUDIO_ENABLE = 88; +constexpr uint32_t EVENT_DAUDIO_DISABLE = 89; constexpr uint32_t EVENT_SET_THREAD_STATUS = 90; } @@ -98,15 +100,7 @@ int32_t DAudioSourceDev::AwakeAudioDev() void DAudioSourceDev::SleepAudioDev() { - DHLOGD("Sleep audio dev."); - if (handler_ == nullptr) { - DHLOGI("Event handler is already stoped."); - return; - } - while (!handler_->IsIdle()) { - DHLOGI("handler is running, wait for idle."); - usleep(WAIT_HANDLER_IDLE_TIME_US); - } + handler_ = nullptr; DHLOGD("Sleep audio dev over."); } @@ -119,11 +113,12 @@ int32_t DAudioSourceDev::EnableDAudio(const std::string &dhId, const std::string return ERR_DH_AUDIO_NULLPTR; } json jParam = { { KEY_DEV_ID, devId_ }, { KEY_DH_ID, dhId }, { KEY_ATTRS, attrs } }; - int32_t ret = TaskEnableDAudio(jParam.dump()); - if (ret != DH_SUCCESS) { - DHLOGE("Enable daudio failed."); + auto eventParam = std::make_shared(jParam); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_DAUDIO_ENABLE, eventParam, 0); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; } - OnEnableTaskResult(ret, jParam.dump(), ""); DHLOGD("Enable audio task generate successfully."); return DH_SUCCESS; } @@ -147,11 +142,11 @@ int32_t DAudioSourceDev::DisableDAudio(const std::string &dhId) switch (GetDevTypeByDHId(dhIdNum)) { case AUDIO_DEVICE_TYPE_SPEAKER: event.type = CLOSE_SPEAKER; - TaskCloseDSpeaker(event.content); + HandleCloseDSpeaker(event); break; case AUDIO_DEVICE_TYPE_MIC: event.type = CLOSE_MIC; - TaskCloseDMic(event.content); + HandleCloseDMic(event); break; default: DHLOGE("Unknown audio device. dhId: %d.", dhIdNum); @@ -159,11 +154,12 @@ int32_t DAudioSourceDev::DisableDAudio(const std::string &dhId) } json jParam = { { KEY_DEV_ID, devId_ }, { KEY_DH_ID, dhId } }; - int32_t ret = TaskDisableDAudio(jParam.dump()); - if (ret != DH_SUCCESS) { - DHLOGE("Disable distributed audio failed."); + auto eventParam = std::make_shared(jParam); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_DAUDIO_DISABLE, eventParam, 0); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; } - OnDisableTaskResult(ret, jParam.dump(), ""); DHLOGD("Disable audio task generate successfully."); return DH_SUCCESS; } @@ -1276,6 +1272,8 @@ DAudioSourceDev::SourceEventHandler::SourceEventHandler(const std::shared_ptr &dev) : AppExecFwk::EventHandler(runner), sourceDev_(dev) { DHLOGD("Event handler is constructing."); + mapEventFuncs_[EVENT_DAUDIO_ENABLE] = &DAudioSourceDev::SourceEventHandler::EnableDAudioCallback; + mapEventFuncs_[EVENT_DAUDIO_DISABLE] = &DAudioSourceDev::SourceEventHandler::DisableDAudioCallback; mapEventFuncs_[EVENT_OPEN_SPEAKER] = &DAudioSourceDev::SourceEventHandler::OpenDSpeakerCallback; mapEventFuncs_[EVENT_CLOSE_SPEAKER] = &DAudioSourceDev::SourceEventHandler::CloseDSpeakerCallback; mapEventFuncs_[EVENT_OPEN_MIC] = &DAudioSourceDev::SourceEventHandler::OpenDMicCallback; @@ -1305,6 +1303,52 @@ void DAudioSourceDev::SourceEventHandler::ProcessEvent(const AppExecFwk::InnerEv (this->*func)(event); } +void DAudioSourceDev::SourceEventHandler::EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr) { + DHLOGE("The input event is null."); + return; + } + std::shared_ptr jParam = event->GetSharedObject(); + if (jParam == nullptr) { + DHLOGE("The json parameter is null."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + int32_t ret = sourceDevObj->TaskEnableDAudio(jParam->dump()); + if (ret != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); + } + sourceDevObj->OnEnableTaskResult(ret, jParam->dump(), ""); +} + +void DAudioSourceDev::SourceEventHandler::DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr) { + DHLOGE("The input event is null."); + return; + } + std::shared_ptr jParam = event->GetSharedObject(); + if (jParam == nullptr) { + DHLOGE("The json parameter is null."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + int32_t ret = sourceDevObj->TaskDisableDAudio(jParam->dump()); + if (ret != DH_SUCCESS) { + DHLOGE("Disable distributed audio failed."); + } + sourceDevObj->OnDisableTaskResult(ret, jParam->dump(), ""); +} + void DAudioSourceDev::SourceEventHandler::OpenDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event) { std::string eventParam; -- Gitee