From de75116ca76f0737583d7ea20af9f700b4a2d5fd Mon Sep 17 00:00:00 2001 From: byndyx Date: Mon, 13 Nov 2023 21:21:44 +0800 Subject: [PATCH] modify pause-restart bug Signed-off-by: byndyx --- .../managersink/include/daudio_sink_dev.h | 2 + .../managersink/src/daudio_sink_dev.cpp | 26 +++++-- .../managersink/src/daudio_sink_manager.cpp | 11 +-- .../managersource/include/daudio_source_dev.h | 3 +- .../managersource/src/daudio_source_dev.cpp | 78 ++++--------------- .../src/av_receiver_engine_adapter.cpp | 3 +- services/common/audioparam/audio_event.h | 1 + 7 files changed, 44 insertions(+), 80 deletions(-) diff --git a/services/audiomanager/managersink/include/daudio_sink_dev.h b/services/audiomanager/managersink/include/daudio_sink_dev.h index fa5f34a0..db4bb1b4 100644 --- a/services/audiomanager/managersink/include/daudio_sink_dev.h +++ b/services/audiomanager/managersink/include/daudio_sink_dev.h @@ -71,6 +71,7 @@ private: int32_t TaskSetVolume(const std::string &args); int32_t TaskSetMute(const std::string &args); int32_t TaskPlayStatusChange(const std::string &args); + int32_t TaskDisableDevice(const std::string &args); void NotifySourceDev(const AudioEventType type, const std::string dhId, const int32_t result); int32_t from_json(const json &j, AudioParam &audioParam); @@ -94,6 +95,7 @@ private: std::mutex micClientMutex_; std::map> micClientMap_; std::shared_ptr audioCtrlMgr_ = nullptr; + static constexpr size_t WAIT_HANDLER_IDLE_TIME_US = 10000; std::atomic isSpkInUse_ = false; std::atomic isMicInUse_ = false; diff --git a/services/audiomanager/managersink/src/daudio_sink_dev.cpp b/services/audiomanager/managersink/src/daudio_sink_dev.cpp index 983cbb3b..e207e541 100644 --- a/services/audiomanager/managersink/src/daudio_sink_dev.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_dev.cpp @@ -54,11 +54,15 @@ int32_t DAudioSinkDev::AwakeAudioDev() void DAudioSinkDev::SleepAudioDev() { + DHLOGD("Sleep audio dev."); if (handler_ == nullptr) { DHLOGI("Event handler is already stoped."); return; } - while (!handler_->IsIdle()) {}; + while (!handler_->IsIdle()) { + DHLOGI("handler is running, wait for idle."); + usleep(WAIT_HANDLER_IDLE_TIME_US); + } DHLOGD("Sleep audio dev over."); } @@ -91,6 +95,10 @@ int32_t DAudioSinkDev::InitAVTransEngines(const ChannelState channelState, IAVEn void DAudioSinkDev::NotifyEvent(const AudioEvent &audioEvent) { DHLOGD("Notify event, eventType: %d.", (int32_t)audioEvent.type); + if ((int32_t)audioEvent.type == DISABLE_DEVICE) { + TaskDisableDevice(audioEvent.content); + return; + } auto eventParam = std::make_shared(audioEvent); auto msgEvent = AppExecFwk::InnerEvent::Get(static_cast(audioEvent.type), eventParam, 0); if (handler_ == nullptr) { @@ -102,6 +110,18 @@ void DAudioSinkDev::NotifyEvent(const AudioEvent &audioEvent) } } +int32_t DAudioSinkDev::TaskDisableDevice(const std::string &args) +{ + if (args.find(OWNER_NAME_D_SPEAKER) != args.npos) { + isSpkInUse_.store(false); + } + if (args.find(OWNER_NAME_D_MIC) != args.npos) { + isMicInUse_.store(false); + } + JudgeDeviceStatus(); + return DH_SUCCESS; +} + int32_t DAudioSinkDev::TaskOpenDSpeaker(const std::string &args) { DHLOGI("Open speaker device, args = %s.", args.c_str()); @@ -171,8 +191,6 @@ int32_t DAudioSinkDev::TaskCloseDSpeaker(const std::string &args) DHLOGE("Release speaker client failed, ret: %d.", ret); } spkClientMap_.erase(dhId); - isSpkInUse_.store(false); - JudgeDeviceStatus(); DHLOGI("Close speaker device task excute success."); return DH_SUCCESS; } @@ -298,8 +316,6 @@ int32_t DAudioSinkDev::TaskCloseDMic(const std::string &args) DHLOGE("Release mic client failed, ret: %d.", ret); } micClientMap_.erase(dhId); - isMicInUse_.store(false); - JudgeDeviceStatus(); DHLOGI("Close mic device task excute success."); return DH_SUCCESS; } diff --git a/services/audiomanager/managersink/src/daudio_sink_manager.cpp b/services/audiomanager/managersink/src/daudio_sink_manager.cpp index 7774b728..781bca30 100644 --- a/services/audiomanager/managersink/src/daudio_sink_manager.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_manager.cpp @@ -364,15 +364,8 @@ int32_t EngineProviderListener::OnProviderEvent(const AVTransEvent &event) } else if (event.type == EventType::EVENT_CHANNEL_CLOSED) { DHLOGI("Received control channel closed event, clear audio device for peerDevId=%s", GetAnonyString(event.peerDevId).c_str()); - if (event.content.find(OWNER_NAME_D_SPEAKER) != event.content.npos) { - DHLOGD("Notify audio event, event type: %d, event content: %s.", CLOSE_SPEAKER, - PARAM_CLOSE_SPEAKER.c_str()); - DAudioSinkManager::GetInstance().NotifyEvent(event.peerDevId, CLOSE_SPEAKER, PARAM_CLOSE_SPEAKER); - } - if (event.content.find(OWNER_NAME_D_MIC) != event.content.npos) { - DHLOGD("Notify audio event, event type: %d, event content: %s.", CLOSE_MIC, PARAM_CLOSE_MIC.c_str()); - DAudioSinkManager::GetInstance().NotifyEvent(event.peerDevId, CLOSE_MIC, PARAM_CLOSE_MIC); - } + std::string eventStr = event.content; + DAudioSinkManager::GetInstance().NotifyEvent(event.peerDevId, DISABLE_DEVICE, eventStr); } else { DHLOGE("Invaild event type."); } diff --git a/services/audiomanager/managersource/include/daudio_source_dev.h b/services/audiomanager/managersource/include/daudio_source_dev.h index e84aaf3e..efda6cda 100644 --- a/services/audiomanager/managersource/include/daudio_source_dev.h +++ b/services/audiomanager/managersource/include/daudio_source_dev.h @@ -122,6 +122,7 @@ 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_; @@ -146,8 +147,6 @@ 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 61887d22..7dc350e9 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -45,8 +45,6 @@ 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; } @@ -100,11 +98,15 @@ int32_t DAudioSourceDev::AwakeAudioDev() void DAudioSourceDev::SleepAudioDev() { + DHLOGD("Sleep audio dev."); if (handler_ == nullptr) { DHLOGI("Event handler is already stoped."); return; } - while (!handler_->IsIdle()) {}; + while (!handler_->IsIdle()) { + DHLOGI("handler is running, wait for idle."); + usleep(WAIT_HANDLER_IDLE_TIME_US); + } DHLOGD("Sleep audio dev over."); } @@ -117,12 +119,11 @@ 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 } }; - 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; + int32_t ret = TaskEnableDAudio(jParam.dump()); + if (ret != DH_SUCCESS) { + DHLOGE("Enable daudio failed."); } + OnEnableTaskResult(ret, jParam.dump(), ""); DHLOGD("Enable audio task generate successfully."); return DH_SUCCESS; } @@ -146,11 +147,11 @@ int32_t DAudioSourceDev::DisableDAudio(const std::string &dhId) switch (GetDevTypeByDHId(dhIdNum)) { case AUDIO_DEVICE_TYPE_SPEAKER: event.type = CLOSE_SPEAKER; - HandleCloseDSpeaker(event); + TaskCloseDSpeaker(event.content); break; case AUDIO_DEVICE_TYPE_MIC: event.type = CLOSE_MIC; - HandleCloseDMic(event); + TaskCloseDMic(event.content); break; default: DHLOGE("Unknown audio device. dhId: %d.", dhIdNum); @@ -158,12 +159,11 @@ int32_t DAudioSourceDev::DisableDAudio(const std::string &dhId) } json jParam = { { KEY_DEV_ID, devId_ }, { KEY_DH_ID, dhId } }; - 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; + int32_t ret = TaskDisableDAudio(jParam.dump()); + if (ret != DH_SUCCESS) { + DHLOGE("Disable distributed audio failed."); } + OnDisableTaskResult(ret, jParam.dump(), ""); DHLOGD("Disable audio task generate successfully."); return DH_SUCCESS; } @@ -1276,8 +1276,6 @@ 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; @@ -1307,52 +1305,6 @@ 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; diff --git a/services/audiotransport/receiverengine/src/av_receiver_engine_adapter.cpp b/services/audiotransport/receiverengine/src/av_receiver_engine_adapter.cpp index 164c04d9..f52dc546 100644 --- a/services/audiotransport/receiverengine/src/av_receiver_engine_adapter.cpp +++ b/services/audiotransport/receiverengine/src/av_receiver_engine_adapter.cpp @@ -50,7 +50,8 @@ int32_t AVTransReceiverAdapter::Release() { DHLOGI("Release!"); if (receiverEngine_ != nullptr) { - if (!receiverEngine_->Release()) { + int32_t ret = receiverEngine_->Release(); + if (ret != DH_SUCCESS) { DHLOGE("Release av transport receiver engine failed"); } } diff --git a/services/common/audioparam/audio_event.h b/services/common/audioparam/audio_event.h index 5660ff40..469f14dc 100644 --- a/services/common/audioparam/audio_event.h +++ b/services/common/audioparam/audio_event.h @@ -46,6 +46,7 @@ typedef enum { MIC_CLOSED = 24, NOTIFY_OPEN_MIC_RESULT = 25, NOTIFY_CLOSE_MIC_RESULT = 26, + DISABLE_DEVICE = 27, VOLUME_SET = 31, VOLUME_GET = 32, -- Gitee