diff --git a/services/audiomanager/managersink/include/daudio_sink_dev.h b/services/audiomanager/managersink/include/daudio_sink_dev.h index e97cf96bdbb6b66abf854d09f91c636815549a01..da343918266bb887cd82b1b555ffd35250e3bb3d 100644 --- a/services/audiomanager/managersink/include/daudio_sink_dev.h +++ b/services/audiomanager/managersink/include/daudio_sink_dev.h @@ -66,6 +66,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); @@ -89,6 +90,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 590e7121e978ec308a9b6afad626ec29ee8714de..e207e541870d8479c8072b5c0bcb281179888d59 100644 --- a/services/audiomanager/managersink/src/daudio_sink_dev.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_dev.cpp @@ -54,7 +54,15 @@ int32_t DAudioSinkDev::AwakeAudioDev() void DAudioSinkDev::SleepAudioDev() { - handler_ = nullptr; + 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); + } DHLOGD("Sleep audio dev over."); } @@ -87,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) { @@ -98,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()); @@ -167,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; } @@ -294,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 7774b7286c980c8a4d820f662df552b63f64715c..781bca305c95f27fd0874634e6206426147575a6 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 e84aaf3eb2f81d1d5c185afe8b72eae8f365a21a..efda6cdae6ada9830c8f2821f358d10963cef7c4 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 44463380b525ce3123b26a2222e563393bede5fe..7dc350e9f0f5505a13a56ed97810b5b68c0406ce 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,7 +98,15 @@ int32_t DAudioSourceDev::AwakeAudioDev() void DAudioSourceDev::SleepAudioDev() { - handler_ = nullptr; + 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); + } DHLOGD("Sleep audio dev over."); } @@ -113,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; } @@ -142,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); @@ -154,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; } @@ -1272,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; @@ -1303,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 164c04d984a62b9f7bdf107b55ee66aaf0bd12e3..f52dc54687e7c922be5ada602158a66483aa2510 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 5660ff40cc1f7552b8504ba6bc3597947d2cc952..469f14dc030a4c33cc50413ed311fb1029c5014d 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,