From d4b1f822bc3f4d87285a2154aabbafb87cf90d28 Mon Sep 17 00:00:00 2001 From: Bobie Date: Sat, 5 Aug 2023 14:16:03 +0800 Subject: [PATCH 1/2] sourcehandler Signed-off-by: Bobie --- .../managersink/src/daudio_sink_dev.cpp | 4 +- .../managersource/include/daudio_source_dev.h | 33 + .../managersource/src/daudio_source_dev.cpp | 623 +++++++++++++++--- services/audiomanager/servicesource/BUILD.gn | 1 + 4 files changed, 571 insertions(+), 90 deletions(-) diff --git a/services/audiomanager/managersink/src/daudio_sink_dev.cpp b/services/audiomanager/managersink/src/daudio_sink_dev.cpp index ff347463..d59b286d 100644 --- a/services/audiomanager/managersink/src/daudio_sink_dev.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_dev.cpp @@ -648,8 +648,8 @@ void DAudioSinkDev::SinkEventHandler::NotifySetVolume(const AppExecFwk::InnerEve DHLOGE("The audio event is nullptr."); return; } - if (sinkDev_.lock()->TaskCloseDMic(audioEvent->content) != DH_SUCCESS) { - DHLOGE("Close mic failed."); + if (sinkDev_.lock()->TaskSetVolume(audioEvent->content) != DH_SUCCESS) { + DHLOGE("Set volume falied."); return; } } diff --git a/services/audiomanager/managersource/include/daudio_source_dev.h b/services/audiomanager/managersource/include/daudio_source_dev.h index 976a88a5..707e6f86 100644 --- a/services/audiomanager/managersource/include/daudio_source_dev.h +++ b/services/audiomanager/managersource/include/daudio_source_dev.h @@ -25,6 +25,7 @@ #include "daudio_source_mgr_callback.h" #include "dmic_dev.h" #include "dspeaker_dev.h" +#include "event_handler.h" #include "iaudio_event_callback.h" #include "iaudio_data_transport.h" #include "iaudio_datatrans_callback.h" @@ -136,9 +137,41 @@ private: bool rpcResult_ = false; uint8_t rpcNotify_ = 0; + class SourceEventHandler : public AppExecFwk::EventHandler { + public: + SourceEventHandler(const std::shared_ptr &runner, const std::shared_ptr &dev); + ~SourceEventHandler() override; + 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); + void CloseDMicCallback(const AppExecFwk::InnerEvent::Pointer &event); + void OpenCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event); + void CloseCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event); + void SetVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event); + void ChangeVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event); + void ChangeFocusCallback(const AppExecFwk::InnerEvent::Pointer &event); + void ChangeRenderStateCallback(const AppExecFwk::InnerEvent::Pointer &event); + void PlayStatusChangeCallback(const AppExecFwk::InnerEvent::Pointer &event); + void SpkMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event); + void SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event); + void MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event); + void MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event); + + private: + using SourceEventFunc = void (SourceEventHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); + std::map mapEventFuncs_; + std::weak_ptr sourceDev_; + }; + using DAudioSourceDevFunc = int32_t (DAudioSourceDev::*)(const AudioEvent &audioEvent); std::map memberFuncMap_; std::map eventNotifyMap_; + std::shared_ptr handler_; }; } // DistributedHardware } // OHOS diff --git a/services/audiomanager/managersource/src/daudio_source_dev.cpp b/services/audiomanager/managersource/src/daudio_source_dev.cpp index b21b9c44..9784ef05 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -30,6 +30,53 @@ namespace OHOS { namespace DistributedHardware { +namespace { +uint32_t EVENT_OPEN_CTRL = 1; +uint32_t EVENT_CLOSE_CTRL = 2; +uint32_t EVENT_OPEN_SPEAKER = 11; +uint32_t EVENT_CLOSE_SPEAKER = 12; +uint32_t EVENT_OPEN_MIC = 21; +uint32_t EVENT_CLOSE_MIC = 22; +uint32_t EVENT_VOLUME_SET = 31; +uint32_t EVENT_VOLUME_CHANGE = 33; +uint32_t EVENT_AUDIO_FOCUS_CHANGE = 41; +uint32_t EVENT_AUDIO_RENDER_STATE_CHANGE = 42; +uint32_t EVENT_CHANGE_PLAY_STATUS = 71; +uint32_t EVENT_MMAP_SPK_START = 81; +uint32_t EVENT_MMAP_SPK_STOP = 82; +uint32_t EVENT_MMAP_MIC_START = 83; +uint32_t EVENT_MMAP_MIC_STOP = 84; +uint32_t EVENT_DAUDIO_ENABLE = 88; +uint32_t EVENT_DAUDIO_DISABLE = 89; + + +// reserve +// uint32_t EVENT_CTRL_OPENED = 3; +// uint32_t EVENT_CTRL_CLOSED = 4; +// uint32_t EVENT_NOTIFY_OPEN_CTRL_RESULT = 5; +// uint32_t EVENT_NOTIFY_CLOSE_CTRL_RESULT = 6; +// uint32_t EVENT_DATA_OPENED = 7; +// uint32_t EVENT_DATA_CLOSED = 8; +// uint32_t EVENT_SPEAKER_OPENED = 13; +// uint32_t EVENT_SPEAKER_CLOSED = 14; +// uint32_t EVENT_NOTIFY_OPEN_SPEAKER_RESULT = 15; +// uint32_t EVENT_NOTIFY_CLOSE_SPEAKER_RESULT = 16; +// uint32_t EVENT_MIC_OPENED = 23; +// uint32_t EVENT_MIC_CLOSED = 24; +// uint32_t EVENT_NOTIFY_OPEN_MIC_RESULT = 25; +// uint32_t EVENT_NOTIFY_CLOSE_MIC_RESULT = 26; +// uint32_t EVENT_VOLUME_GET = 32; +// uint32_t EVENT_VOLUME_MIN_GET = 34; +// uint32_t EVENT_VOLUME_MAX_GET = 35; +// uint32_t EVENT_VOLUME_MUTE_SET = 36; +// uint32_t EVENT_SET_PARAM = 51; +// uint32_t EVENT_SEND_PARAM = 52; +// uint32_t EVENT_AUDIO_ENCODER_ERR = 61; +// uint32_t EVENT_AUDIO_DECODER_ERR = 62; +// uint32_t EVENT_AUDIO_START = 85; +// uint32_t EVENT_AUDIO_STOP = 86; +} + DAudioSourceDev::DAudioSourceDev(const std::string &devId, const std::shared_ptr &callback) : devId_(devId), mgrCallback_(callback) { @@ -72,6 +119,13 @@ int32_t DAudioSourceDev::AwakeAudioDev() constexpr size_t capacity = 20; taskQueue_ = std::make_shared(capacity); taskQueue_->Start(); + + auto runner = AppExecFwk::EventRunner::Create(true); + if (runner == nullptr) { + DHLOGE("Create runner failed."); + return ERR_DH_AUDIO_NULLPTR; + } + handler_ = std::make_shared(runner, shared_from_this()); return DH_SUCCESS; } @@ -83,30 +137,38 @@ void DAudioSourceDev::SleepAudioDev() } taskQueue_->Stop(); taskQueue_ = nullptr; + + if (handler_ == nullptr) { + DHLOGI("Event handler is already stoped."); + return; + } + while (!handler_->IsIdle()) { + DHLOGD("Event handler is proccesing."); + } } int32_t DAudioSourceDev::EnableDAudio(const std::string &dhId, const std::string &attrs) { DHLOGI("Enable audio device, dhId: %s.", dhId.c_str()); isRpcOpen_.store(true); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - json jParam = { { KEY_DEV_ID, devId_ }, { KEY_DH_ID, dhId }, { KEY_ATTRS, attrs } }; - auto task = - GenerateTask(this, &DAudioSourceDev::TaskEnableDAudio, jParam.dump(), "", &DAudioSourceDev::OnEnableTaskResult); - DHLOGD("Enable audio task generate success."); - return taskQueue_->Produce(task); + auto eventParam = std::make_shared(jParam); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_DAUDIO_ENABLE, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Send event success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::DisableDAudio(const std::string &dhId) { DHLOGI("Disable audio device, dhId: %s.", dhId.c_str()); isRpcOpen_.store(false); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } @@ -132,10 +194,11 @@ int32_t DAudioSourceDev::DisableDAudio(const std::string &dhId) } json jParam = { { KEY_DEV_ID, devId_ }, { KEY_DH_ID, dhId } }; - auto task = GenerateTask(this, &DAudioSourceDev::TaskDisableDAudio, jParam.dump(), "", - &DAudioSourceDev::OnDisableTaskResult); + auto eventParam = std::make_shared(jParam); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_DAUDIO_DISABLE, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); DHLOGD("Disable audio task generate success."); - return taskQueue_->Produce(task); + return DH_SUCCESS; } void DAudioSourceDev::NotifyEvent(const AudioEvent &event) @@ -153,30 +216,33 @@ void DAudioSourceDev::NotifyEvent(const AudioEvent &event) int32_t DAudioSourceDev::HandleOpenDSpeaker(const AudioEvent &event) { DHLOGI("Open speaker device."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } int32_t ret = OpenCtrlTrans(event); if (ret != DH_SUCCESS) { return ret; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskOpenDSpeaker, event.content, "Open Spk Device", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_OPEN_SPEAKER, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Opening DSpeakerevent is sent success."); + return DH_SUCCESS;; } int32_t DAudioSourceDev::HandleCloseDSpeaker(const AudioEvent &event) { DHLOGI("Close speaker device."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskCloseDSpeaker, event.content, "Close Spk Device", - &DAudioSourceDev::OnTaskResult); - taskQueue_->Produce(task); + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_CLOSE_SPEAKER, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Closing DSpeaker event is sent success."); return CloseCtrlTrans(event, true); } @@ -200,29 +266,34 @@ int32_t DAudioSourceDev::HandleDSpeakerClosed(const AudioEvent &event) int32_t DAudioSourceDev::HandleOpenDMic(const AudioEvent &event) { DHLOGI("Open mic device."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } int32_t ret = OpenCtrlTrans(event); if (ret != DH_SUCCESS) { return ret; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskOpenDMic, event.content, "Open Mic Device", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_OPEN_MIC, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Speaker Mmap Start event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleCloseDMic(const AudioEvent &event) { DHLOGI("Close mic device."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskCloseDMic, event.content, "Close Mic Device", - &DAudioSourceDev::OnTaskResult); - taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_CLOSE_MIC, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Closing DSpeaker event is sent success."); return CloseCtrlTrans(event, false); } @@ -256,26 +327,31 @@ int32_t DAudioSourceDev::CloseCtrlTrans(const AudioEvent &event, bool isSpk) int32_t DAudioSourceDev::HandleOpenCtrlTrans(const AudioEvent &event) { DHLOGI("Open control trans."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskOpenCtrlChannel, event.content, "Open Ctrl Trans", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_OPEN_CTRL, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Opening ctrl trans channel event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleCloseCtrlTrans(const AudioEvent &event) { - (void)event; DHLOGI("Close control trans."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskCloseCtrlChannel, event.content, "Close Ctrl Trans", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_CLOSE_CTRL, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Close ctrl trans channel event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleCtrlTransClosed(const AudioEvent &event) @@ -320,109 +396,136 @@ int32_t DAudioSourceDev::HandleNotifyRPC(const AudioEvent &event) int32_t DAudioSourceDev::HandleVolumeSet(const AudioEvent &event) { DHLOGD("Start handle volume set."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskSetVolume, event.content, "set volume", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_VOLUME_SET, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Volume setting event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleVolumeChange(const AudioEvent &event) { DHLOGD("Start handle volume change."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskChangeVolume, event.content, "volume change", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_VOLUME_CHANGE, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Volume change event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleFocusChange(const AudioEvent &event) { DHLOGD("Start handle focus change."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskChangeFocus, event.content, "focus change", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_AUDIO_FOCUS_CHANGE, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Focus change event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleRenderStateChange(const AudioEvent &event) { DHLOGD("Start handle render state change."); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskChangeRenderState, event.content, "render state change", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_AUDIO_RENDER_STATE_CHANGE, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Render state change event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandlePlayStatusChange(const AudioEvent &event) { DHLOGD("Play status change, content: %s.", event.content.c_str()); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskPlayStatusChange, event.content, "play state change", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_CHANGE_PLAY_STATUS, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Play state change event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleSpkMmapStart(const AudioEvent &event) { DHLOGI("Spk mmap start, content: %s.", event.content.c_str()); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskSpkMmapStart, event.content, "spk mmap start", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_MMAP_SPK_START, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Speaker Mmap Start event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleSpkMmapStop(const AudioEvent &event) { DHLOGI("Spk mmap stop, content: %s.", event.content.c_str()); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskSpkMmapStop, event.content, "spk mmap stop", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_MMAP_SPK_STOP, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Speaker Mmap Stop event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleMicMmapStart(const AudioEvent &event) { DHLOGI("Mic mmap start, content: %s.", event.content.c_str()); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskMicMmapStart, event.content, "mic mmap start", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_MMAP_MIC_START, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Mic Mmap Start event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleMicMmapStop(const AudioEvent &event) { DHLOGI("Mic mmap stop, content: %s.", event.content.c_str()); - if (taskQueue_ == nullptr) { - DHLOGE("Task queue is null."); + if (handler_ == nullptr) { + DHLOGE("Event handler is null."); return ERR_DH_AUDIO_NULLPTR; } - auto task = GenerateTask(this, &DAudioSourceDev::TaskMicMmapStop, event.content, "mic mmap stop", - &DAudioSourceDev::OnTaskResult); - return taskQueue_->Produce(task); + + auto eventParam = std::make_shared(event); + auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_MMAP_MIC_STOP, eventParam, 0); + handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + DHLOGD("Mic Mmap Stop event is sent success."); + return DH_SUCCESS; } int32_t DAudioSourceDev::WaitForRPC(const AudioEventType type) @@ -1051,5 +1154,349 @@ void DAudioSourceDev::to_json(json &j, const AudioParam ¶m) { KEY_SOURCE_TYPE, param.captureOpts.sourceType }, }; } + +DAudioSourceDev::SourceEventHandler::SourceEventHandler(const std::shared_ptr &runner, + 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; + mapEventFuncs_[EVENT_CLOSE_MIC] = &DAudioSourceDev::SourceEventHandler::CloseDMicCallback; + mapEventFuncs_[EVENT_OPEN_CTRL] = &DAudioSourceDev::SourceEventHandler::OpenCtrlCallback; + mapEventFuncs_[EVENT_CLOSE_CTRL] = &DAudioSourceDev::SourceEventHandler::CloseCtrlCallback; + mapEventFuncs_[EVENT_VOLUME_SET] = &DAudioSourceDev::SourceEventHandler::SetVolumeCallback; + mapEventFuncs_[EVENT_VOLUME_CHANGE] = &DAudioSourceDev::SourceEventHandler::ChangeVolumeCallback; + mapEventFuncs_[EVENT_AUDIO_FOCUS_CHANGE] = &DAudioSourceDev::SourceEventHandler::ChangeFocusCallback; + mapEventFuncs_[EVENT_AUDIO_RENDER_STATE_CHANGE] = &DAudioSourceDev::SourceEventHandler::ChangeRenderStateCallback; + mapEventFuncs_[EVENT_CHANGE_PLAY_STATUS] = &DAudioSourceDev::SourceEventHandler::PlayStatusChangeCallback; + mapEventFuncs_[EVENT_MMAP_SPK_START] = &DAudioSourceDev::SourceEventHandler::SpkMmapStartCallback; + mapEventFuncs_[EVENT_MMAP_SPK_STOP] = &DAudioSourceDev::SourceEventHandler::SpkMmapStopCallback; + mapEventFuncs_[EVENT_MMAP_MIC_START] = &DAudioSourceDev::SourceEventHandler::MicMmapStartCallback; + mapEventFuncs_[EVENT_MMAP_MIC_STOP] = &DAudioSourceDev::SourceEventHandler::MicMmapStopCallback; +} + +DAudioSourceDev::SourceEventHandler::~SourceEventHandler(){} + +void DAudioSourceDev::SourceEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) +{ + auto iter = mapEventFuncs_.find(event->GetInnerEventId()); + if (iter == mapEventFuncs_.end()) { + DHLOGE("Event Id is invaild.", event->GetInnerEventId()); + return; + } + SourceEventFunc &func = iter->second; + (this->*func)(event); +} + +void DAudioSourceDev::SourceEventHandler::EnableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr jParam = event->GetSharedObject(); + if (jParam == nullptr) { + DHLOGE("The json parameter is null."); + return; + } + int32_t ret = sourceDev_.lock()->TaskEnableDAudio(jParam->dump()); + sourceDev_.lock()->OnEnableTaskResult(DH_SUCCESS, jParam->dump(), ""); + if (ret != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); + return; + } +} + +void DAudioSourceDev::SourceEventHandler::DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr jParam = event->GetSharedObject(); + if (jParam == nullptr) { + DHLOGE("The json parameter is null."); + return; + } + int32_t ret = sourceDev_.lock()->TaskDisableDAudio(jParam->dump()); + sourceDev_.lock()->OnDisableTaskResult(ret, jParam->dump(), ""); + if (ret != DH_SUCCESS) { + DHLOGE("Disable distributed audio failed."); + return; + } +} + +void DAudioSourceDev::SourceEventHandler::OpenDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskOpenDSpeaker(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::CloseDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskCloseDSpeaker(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::OpenDMicCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskOpenDMic(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::CloseDMicCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskCloseDMic(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::OpenCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskOpenCtrlChannel(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::CloseCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskCloseCtrlChannel(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::SetVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskSetVolume(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::ChangeVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskChangeVolume(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::ChangeFocusCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskChangeFocus(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::ChangeRenderStateCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskChangeRenderState(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::PlayStatusChangeCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskPlayStatusChange(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::SpkMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskSpkMmapStart(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskSpkMmapStop(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskMicMmapStart(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} + +void DAudioSourceDev::SourceEventHandler::MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + if (event == nullptr || sourceDev_.expired()) { + DHLOGE("The input event or sink dev is null."); + return; + } + std::shared_ptr eventParam = event->GetSharedObject(); + if (eventParam == nullptr) { + DHLOGE("The event parameter is null."); + return; + } + if (sourceDev_.lock()->TaskSpkMmapStop(eventParam->content) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); // TODO: + return; + } + // TODO: callback +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/audiomanager/servicesource/BUILD.gn b/services/audiomanager/servicesource/BUILD.gn index 53e3d26a..b5ad8d02 100755 --- a/services/audiomanager/servicesource/BUILD.gn +++ b/services/audiomanager/servicesource/BUILD.gn @@ -97,6 +97,7 @@ ohos_shared_library("distributed_audio_source") { "distributed_hardware_fwk:distributed_av_sender", "drivers_interface_audio:libaudio_proxy_1.0", "dsoftbus:softbus_client", + "eventhandler:libeventhandler", "hdf_core:libhdi", "hisysevent:libhisysevent", "hitrace:hitrace_meter", -- Gitee From 1f41e490e2dea3a697976fe14fa16792f71042b4 Mon Sep 17 00:00:00 2001 From: Bobie Date: Tue, 8 Aug 2023 17:42:22 +0800 Subject: [PATCH 2/2] sourcehandler 2 Signed-off-by: Bobie --- .../managersink/include/daudio_sink_dev.h | 1 + .../managersink/src/daudio_sink_dev.cpp | 254 +++++---- .../managersource/include/daudio_source_dev.h | 11 +- .../managersource/src/daudio_source_dev.cpp | 496 ++++++++++-------- .../src/daudio_source_dev_test.cpp | 44 +- 5 files changed, 435 insertions(+), 371 deletions(-) diff --git a/services/audiomanager/managersink/include/daudio_sink_dev.h b/services/audiomanager/managersink/include/daudio_sink_dev.h index 58fb5fc9..905a7b0b 100644 --- a/services/audiomanager/managersink/include/daudio_sink_dev.h +++ b/services/audiomanager/managersink/include/daudio_sink_dev.h @@ -120,6 +120,7 @@ private: void NotifyFocusChange(const AppExecFwk::InnerEvent::Pointer &event); void NotifyRenderStateChange(const AppExecFwk::InnerEvent::Pointer &event); void NotifyPlayStatusChange(const AppExecFwk::InnerEvent::Pointer &event); + int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam); private: using SinkEventFunc = void (SinkEventHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); diff --git a/services/audiomanager/managersink/src/daudio_sink_dev.cpp b/services/audiomanager/managersink/src/daudio_sink_dev.cpp index d59b286d..053b88db 100644 --- a/services/audiomanager/managersink/src/daudio_sink_dev.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_dev.cpp @@ -87,8 +87,11 @@ void DAudioSinkDev::NotifyEvent(const AudioEvent &audioEvent) DHLOGD("Notify event, eventType: %d.", (int32_t)audioEvent.type); auto eventParam = std::make_shared(audioEvent); auto msgEvent = AppExecFwk::InnerEvent::Get(static_cast(audioEvent.type), eventParam, 0); - if (handler_ != nullptr) { - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + if (handler_ == nullptr) { + DHLOGE("The event handler is null."); + return; + } + if (handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { DHLOGD("Send event success."); } } @@ -441,16 +444,17 @@ void DAudioSinkDev::SinkEventHandler::ProcessEvent(const AppExecFwk::InnerEvent: void DAudioSinkDev::SinkEventHandler::NotifyOpenCtrlChannel(const AppExecFwk::InnerEvent::Pointer &event) { DHLOGI("Notify open ctrl channel."); - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskOpenCtrlChannel(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskOpenCtrlChannel(eventParam) != DH_SUCCESS) { DHLOGE("Open ctrl channel failed."); return; } @@ -459,16 +463,17 @@ void DAudioSinkDev::SinkEventHandler::NotifyOpenCtrlChannel(const AppExecFwk::In void DAudioSinkDev::SinkEventHandler::NotifyCloseCtrlChannel(const AppExecFwk::InnerEvent::Pointer &event) { DHLOGI("Notify close ctrl channel."); - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskCloseCtrlChannel(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskCloseCtrlChannel(eventParam) != DH_SUCCESS) { DHLOGE("Close ctrl channel falied."); return; } @@ -483,42 +488,44 @@ void DAudioSinkDev::SinkEventHandler::NotifyCtrlOpened(const AppExecFwk::InnerEv void DAudioSinkDev::SinkEventHandler::NotifyCtrlClosed(const AppExecFwk::InnerEvent::Pointer &event) { DHLOGI("Notify ctrl closed."); - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskCloseCtrlChannel(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskCloseCtrlChannel(eventParam) != DH_SUCCESS) { DHLOGE("Close ctrl channel failed."); return; } - if (sinkDev_.lock()->TaskCloseDSpeaker(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskCloseDSpeaker(eventParam) != DH_SUCCESS) { DHLOGE("Close speaker failed."); return; } - if (sinkDev_.lock()->TaskCloseDMic(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskCloseDMic(eventParam) != DH_SUCCESS) { DHLOGE("Close mic failed."); return; } - sinkDev_.lock()->JudgeDeviceStatus(); + sinkDevObj->JudgeDeviceStatus(); } void DAudioSinkDev::SinkEventHandler::NotifyOpenSpeaker(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskOpenDSpeaker(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskOpenDSpeaker(eventParam) != DH_SUCCESS) { DHLOGE("Open speaker failed."); return; } @@ -526,16 +533,17 @@ void DAudioSinkDev::SinkEventHandler::NotifyOpenSpeaker(const AppExecFwk::InnerE void DAudioSinkDev::SinkEventHandler::NotifyCloseSpeaker(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskOpenDSpeaker(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskOpenDSpeaker(eventParam) != DH_SUCCESS) { DHLOGE("Open speaker failed."); return; } @@ -544,20 +552,21 @@ void DAudioSinkDev::SinkEventHandler::NotifyCloseSpeaker(const AppExecFwk::Inner void DAudioSinkDev::SinkEventHandler::NotifySpeakerOpened(const AppExecFwk::InnerEvent::Pointer &event) { DHLOGD("Starting render."); - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskStartRender() != DH_SUCCESS) { + if (sinkDevObj->TaskStartRender() != DH_SUCCESS) { DHLOGE("Speaker client start failed."); return; } - if (sinkDev_.lock()->TaskVolumeChange(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskVolumeChange(eventParam) != DH_SUCCESS) { DHLOGE("Notify pimary volume to source device failed."); return; } @@ -565,16 +574,17 @@ void DAudioSinkDev::SinkEventHandler::NotifySpeakerOpened(const AppExecFwk::Inne void DAudioSinkDev::SinkEventHandler::NotifySpeakerClosed(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskCloseDSpeaker(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskCloseDSpeaker(eventParam) != DH_SUCCESS) { DHLOGE("Close speaker failed."); return; } @@ -582,16 +592,17 @@ void DAudioSinkDev::SinkEventHandler::NotifySpeakerClosed(const AppExecFwk::Inne void DAudioSinkDev::SinkEventHandler::NotifyOpenMic(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskOpenDMic(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskOpenDMic(eventParam) != DH_SUCCESS) { DHLOGE("Open mic failed."); return; } @@ -599,16 +610,17 @@ void DAudioSinkDev::SinkEventHandler::NotifyOpenMic(const AppExecFwk::InnerEvent void DAudioSinkDev::SinkEventHandler::NotifyCloseMic(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskCloseDMic(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskCloseDMic(eventParam) != DH_SUCCESS) { DHLOGE("Close mic failed."); return; } @@ -622,16 +634,17 @@ void DAudioSinkDev::SinkEventHandler::NotifyMicOpened(const AppExecFwk::InnerEve void DAudioSinkDev::SinkEventHandler::NotifyMicClosed(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskCloseDMic(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskCloseDMic(eventParam) != DH_SUCCESS) { DHLOGE("Close mic failed."); return; } @@ -639,33 +652,35 @@ void DAudioSinkDev::SinkEventHandler::NotifyMicClosed(const AppExecFwk::InnerEve void DAudioSinkDev::SinkEventHandler::NotifySetVolume(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskSetVolume(audioEvent->content) != DH_SUCCESS) { - DHLOGE("Set volume falied."); + if (sinkDevObj->TaskSetVolume(eventParam) != DH_SUCCESS) { + DHLOGE("Set volume failed."); return; } } void DAudioSinkDev::SinkEventHandler::NotifyVolumeChange(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskVolumeChange(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskVolumeChange(eventParam) != DH_SUCCESS) { DHLOGE("Notify volume change status to source device failed."); return; } @@ -673,16 +688,17 @@ void DAudioSinkDev::SinkEventHandler::NotifyVolumeChange(const AppExecFwk::Inner void DAudioSinkDev::SinkEventHandler::NotifySetParam(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskSetParameter(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskSetParameter(eventParam) != DH_SUCCESS) { DHLOGE("Set parameters failed."); return; } @@ -690,16 +706,17 @@ void DAudioSinkDev::SinkEventHandler::NotifySetParam(const AppExecFwk::InnerEven void DAudioSinkDev::SinkEventHandler::NotifySetMute(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskSetMute(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskSetMute(eventParam) != DH_SUCCESS) { DHLOGE("Set mute failed."); return; } @@ -707,16 +724,17 @@ void DAudioSinkDev::SinkEventHandler::NotifySetMute(const AppExecFwk::InnerEvent void DAudioSinkDev::SinkEventHandler::NotifyFocusChange(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskFocusChange(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskFocusChange(eventParam) != DH_SUCCESS) { DHLOGE("Handle focus change event failed."); return; } @@ -724,16 +742,17 @@ void DAudioSinkDev::SinkEventHandler::NotifyFocusChange(const AppExecFwk::InnerE void DAudioSinkDev::SinkEventHandler::NotifyRenderStateChange(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskRenderStateChange(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskRenderStateChange(eventParam) != DH_SUCCESS) { DHLOGE("Handle render state change failed."); return; } @@ -741,19 +760,36 @@ void DAudioSinkDev::SinkEventHandler::NotifyRenderStateChange(const AppExecFwk:: void DAudioSinkDev::SinkEventHandler::NotifyPlayStatusChange(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sinkDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr audioEvent = event->GetSharedObject(); - if (audioEvent == nullptr) { - DHLOGE("The audio event is nullptr."); + auto sinkDevObj = sinkDev_.lock(); + if (sinkDevObj == nullptr) { + DHLOGE("Sink dev is invalid."); return; } - if (sinkDev_.lock()->TaskPlayStatusChange(audioEvent->content) != DH_SUCCESS) { + if (sinkDevObj->TaskPlayStatusChange(eventParam) != DH_SUCCESS) { DHLOGE("Handle play status change event failed."); return; } } + +int32_t DAudioSinkDev::SinkEventHandler::GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, + std::string &eventParam) +{ + if (event == nullptr) { + DHLOGE("The input event is null."); + return ERR_DH_AUDIO_NULLPTR; + } + std::shared_ptr paramObj = event->GetSharedObject(); + if (paramObj == nullptr) { + DHLOGE("The event parameter object is nullptr."); + return ERR_DH_AUDIO_NULLPTR; + } + eventParam = paramObj->content; + return DH_SUCCESS; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/audiomanager/managersource/include/daudio_source_dev.h b/services/audiomanager/managersource/include/daudio_source_dev.h index 707e6f86..b8a35fab 100644 --- a/services/audiomanager/managersource/include/daudio_source_dev.h +++ b/services/audiomanager/managersource/include/daudio_source_dev.h @@ -20,18 +20,18 @@ #include #include "nlohmann/json.hpp" +#include "event_handler.h" + #include "audio_event.h" #include "daudio_source_dev_ctrl_manager.h" #include "daudio_source_mgr_callback.h" #include "dmic_dev.h" #include "dspeaker_dev.h" -#include "event_handler.h" #include "iaudio_event_callback.h" #include "iaudio_data_transport.h" #include "iaudio_datatrans_callback.h" #include "idaudio_ipc_callback.h" #include "idaudio_hdi_callback.h" -#include "task_queue.h" using json = nlohmann::json; @@ -124,12 +124,9 @@ private: std::string devId_; std::shared_ptr mgrCallback_; - std::shared_ptr taskQueue_; std::shared_ptr speaker_; std::shared_ptr mic_; - std::shared_ptr audioCtrlMgr_; - std::mutex taskQueueMutex_; std::mutex rpcWaitMutex_; std::condition_variable rpcWaitCond_; @@ -139,7 +136,8 @@ private: class SourceEventHandler : public AppExecFwk::EventHandler { public: - SourceEventHandler(const std::shared_ptr &runner, const std::shared_ptr &dev); + SourceEventHandler(const std::shared_ptr &runner, + const std::shared_ptr &dev); ~SourceEventHandler() override; void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; @@ -161,6 +159,7 @@ private: void SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event); void MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event); void MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event); + int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam); private: using SourceEventFunc = void (SourceEventHandler::*)(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 9784ef05..1ae4cbc0 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -31,50 +31,23 @@ namespace OHOS { namespace DistributedHardware { namespace { -uint32_t EVENT_OPEN_CTRL = 1; -uint32_t EVENT_CLOSE_CTRL = 2; -uint32_t EVENT_OPEN_SPEAKER = 11; -uint32_t EVENT_CLOSE_SPEAKER = 12; -uint32_t EVENT_OPEN_MIC = 21; -uint32_t EVENT_CLOSE_MIC = 22; -uint32_t EVENT_VOLUME_SET = 31; -uint32_t EVENT_VOLUME_CHANGE = 33; -uint32_t EVENT_AUDIO_FOCUS_CHANGE = 41; -uint32_t EVENT_AUDIO_RENDER_STATE_CHANGE = 42; -uint32_t EVENT_CHANGE_PLAY_STATUS = 71; -uint32_t EVENT_MMAP_SPK_START = 81; -uint32_t EVENT_MMAP_SPK_STOP = 82; -uint32_t EVENT_MMAP_MIC_START = 83; -uint32_t EVENT_MMAP_MIC_STOP = 84; -uint32_t EVENT_DAUDIO_ENABLE = 88; -uint32_t EVENT_DAUDIO_DISABLE = 89; - - -// reserve -// uint32_t EVENT_CTRL_OPENED = 3; -// uint32_t EVENT_CTRL_CLOSED = 4; -// uint32_t EVENT_NOTIFY_OPEN_CTRL_RESULT = 5; -// uint32_t EVENT_NOTIFY_CLOSE_CTRL_RESULT = 6; -// uint32_t EVENT_DATA_OPENED = 7; -// uint32_t EVENT_DATA_CLOSED = 8; -// uint32_t EVENT_SPEAKER_OPENED = 13; -// uint32_t EVENT_SPEAKER_CLOSED = 14; -// uint32_t EVENT_NOTIFY_OPEN_SPEAKER_RESULT = 15; -// uint32_t EVENT_NOTIFY_CLOSE_SPEAKER_RESULT = 16; -// uint32_t EVENT_MIC_OPENED = 23; -// uint32_t EVENT_MIC_CLOSED = 24; -// uint32_t EVENT_NOTIFY_OPEN_MIC_RESULT = 25; -// uint32_t EVENT_NOTIFY_CLOSE_MIC_RESULT = 26; -// uint32_t EVENT_VOLUME_GET = 32; -// uint32_t EVENT_VOLUME_MIN_GET = 34; -// uint32_t EVENT_VOLUME_MAX_GET = 35; -// uint32_t EVENT_VOLUME_MUTE_SET = 36; -// uint32_t EVENT_SET_PARAM = 51; -// uint32_t EVENT_SEND_PARAM = 52; -// uint32_t EVENT_AUDIO_ENCODER_ERR = 61; -// uint32_t EVENT_AUDIO_DECODER_ERR = 62; -// uint32_t EVENT_AUDIO_START = 85; -// uint32_t EVENT_AUDIO_STOP = 86; +constexpr uint32_t EVENT_OPEN_CTRL = 1; +constexpr uint32_t EVENT_CLOSE_CTRL = 2; +constexpr uint32_t EVENT_OPEN_SPEAKER = 11; +constexpr uint32_t EVENT_CLOSE_SPEAKER = 12; +constexpr uint32_t EVENT_OPEN_MIC = 21; +constexpr uint32_t EVENT_CLOSE_MIC = 22; +constexpr uint32_t EVENT_VOLUME_SET = 31; +constexpr uint32_t EVENT_VOLUME_CHANGE = 33; +constexpr uint32_t EVENT_AUDIO_FOCUS_CHANGE = 41; +constexpr uint32_t EVENT_AUDIO_RENDER_STATE_CHANGE = 42; +constexpr uint32_t EVENT_CHANGE_PLAY_STATUS = 71; +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; } DAudioSourceDev::DAudioSourceDev(const std::string &devId, const std::shared_ptr &callback) @@ -116,13 +89,9 @@ DAudioSourceDev::DAudioSourceDev(const std::string &devId, const std::shared_ptr int32_t DAudioSourceDev::AwakeAudioDev() { - constexpr size_t capacity = 20; - taskQueue_ = std::make_shared(capacity); - taskQueue_->Start(); - auto runner = AppExecFwk::EventRunner::Create(true); if (runner == nullptr) { - DHLOGE("Create runner failed."); + DHLOGE("Create runner failed."); return ERR_DH_AUDIO_NULLPTR; } handler_ = std::make_shared(runner, shared_from_this()); @@ -131,13 +100,6 @@ int32_t DAudioSourceDev::AwakeAudioDev() void DAudioSourceDev::SleepAudioDev() { - if (taskQueue_ == nullptr) { - DHLOGI("Task queue already stop."); - return; - } - taskQueue_->Stop(); - taskQueue_ = nullptr; - if (handler_ == nullptr) { DHLOGI("Event handler is already stoped."); return; @@ -158,8 +120,11 @@ int32_t DAudioSourceDev::EnableDAudio(const std::string &dhId, const std::string 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); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Send event success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Enable audio task generate successfully."); return DH_SUCCESS; } @@ -196,8 +161,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); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Disable audio task generate success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Disable audio task generate successfully."); return DH_SUCCESS; } @@ -226,9 +194,12 @@ int32_t DAudioSourceDev::HandleOpenDSpeaker(const AudioEvent &event) } auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_OPEN_SPEAKER, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Opening DSpeakerevent is sent success."); - return DH_SUCCESS;; + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Opening DSpeaker event is sent successfully."); + return DH_SUCCESS; } int32_t DAudioSourceDev::HandleCloseDSpeaker(const AudioEvent &event) @@ -241,8 +212,11 @@ int32_t DAudioSourceDev::HandleCloseDSpeaker(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_CLOSE_SPEAKER, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Closing DSpeaker event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Closing DSpeaker event is sent successfully."); return CloseCtrlTrans(event, true); } @@ -277,8 +251,11 @@ int32_t DAudioSourceDev::HandleOpenDMic(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_OPEN_MIC, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Speaker Mmap Start event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Speaker Mmap Start event is sent successfully."); return DH_SUCCESS; } @@ -292,8 +269,11 @@ int32_t DAudioSourceDev::HandleCloseDMic(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_CLOSE_MIC, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Closing DSpeaker event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Closing DSpeaker event is sent successfully."); return CloseCtrlTrans(event, false); } @@ -334,8 +314,11 @@ int32_t DAudioSourceDev::HandleOpenCtrlTrans(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_OPEN_CTRL, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Opening ctrl trans channel event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Opening ctrl trans channel event is sent successfully."); return DH_SUCCESS; } @@ -349,8 +332,11 @@ int32_t DAudioSourceDev::HandleCloseCtrlTrans(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_CLOSE_CTRL, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Close ctrl trans channel event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Close ctrl trans channel event is sent successfully."); return DH_SUCCESS; } @@ -403,8 +389,11 @@ int32_t DAudioSourceDev::HandleVolumeSet(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_VOLUME_SET, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Volume setting event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Volume setting event is sent successfully."); return DH_SUCCESS; } @@ -418,8 +407,11 @@ int32_t DAudioSourceDev::HandleVolumeChange(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_VOLUME_CHANGE, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Volume change event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Volume change event is sent successfully."); return DH_SUCCESS; } @@ -433,8 +425,11 @@ int32_t DAudioSourceDev::HandleFocusChange(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_AUDIO_FOCUS_CHANGE, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Focus change event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Focus change event is sent successfully."); return DH_SUCCESS; } @@ -448,8 +443,11 @@ int32_t DAudioSourceDev::HandleRenderStateChange(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_AUDIO_RENDER_STATE_CHANGE, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Render state change event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Render state change event is sent successfully."); return DH_SUCCESS; } @@ -463,8 +461,11 @@ int32_t DAudioSourceDev::HandlePlayStatusChange(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_CHANGE_PLAY_STATUS, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Play state change event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Play state change event is sent successfully."); return DH_SUCCESS; } @@ -478,8 +479,11 @@ int32_t DAudioSourceDev::HandleSpkMmapStart(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_MMAP_SPK_START, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Speaker Mmap Start event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Speaker Mmap Start event is sent successfully."); return DH_SUCCESS; } @@ -493,8 +497,11 @@ int32_t DAudioSourceDev::HandleSpkMmapStop(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_MMAP_SPK_STOP, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Speaker Mmap Stop event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Speaker Mmap Stop event is sent successfully."); return DH_SUCCESS; } @@ -508,8 +515,11 @@ int32_t DAudioSourceDev::HandleMicMmapStart(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_MMAP_MIC_START, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Mic Mmap Start event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Mic Mmap Start event is sent successfully."); return DH_SUCCESS; } @@ -523,8 +533,11 @@ int32_t DAudioSourceDev::HandleMicMmapStop(const AudioEvent &event) auto eventParam = std::make_shared(event); auto msgEvent = AppExecFwk::InnerEvent::Get(EVENT_MMAP_MIC_STOP, eventParam, 0); - handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); - DHLOGD("Mic Mmap Stop event is sent success."); + if (!handler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) { + DHLOGE("Send event failed."); + return ERR_DH_AUDIO_FAILED; + } + DHLOGD("Mic Mmap Stop event is sent successfully."); return DH_SUCCESS; } @@ -1178,7 +1191,7 @@ DAudioSourceDev::SourceEventHandler::SourceEventHandler(const std::shared_ptr jParam = event->GetSharedObject(); @@ -1202,18 +1215,22 @@ void DAudioSourceDev::SourceEventHandler::EnableDAudioCallback(const AppExecFwk: DHLOGE("The json parameter is null."); return; } - int32_t ret = sourceDev_.lock()->TaskEnableDAudio(jParam->dump()); - sourceDev_.lock()->OnEnableTaskResult(DH_SUCCESS, jParam->dump(), ""); + 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."); - return; } + sourceDevObj->OnEnableTaskResult(ret, jParam->dump(), ""); } void DAudioSourceDev::SourceEventHandler::DisableDAudioCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + if (event == nullptr) { + DHLOGE("The input event is null."); return; } std::shared_ptr jParam = event->GetSharedObject(); @@ -1221,282 +1238,317 @@ void DAudioSourceDev::SourceEventHandler::DisableDAudioCallback(const AppExecFwk DHLOGE("The json parameter is null."); return; } - int32_t ret = sourceDev_.lock()->TaskDisableDAudio(jParam->dump()); - sourceDev_.lock()->OnDisableTaskResult(ret, jParam->dump(), ""); + 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."); - return; } + sourceDevObj->OnDisableTaskResult(ret, jParam->dump(), ""); } void DAudioSourceDev::SourceEventHandler::OpenDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskOpenDSpeaker(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskOpenDSpeaker(eventParam) != DH_SUCCESS) { + DHLOGE("Open speaker failed."); return; } - // TODO: callback + DHLOGI("Open speaker successfully."); } void DAudioSourceDev::SourceEventHandler::CloseDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskCloseDSpeaker(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskCloseDSpeaker(eventParam) != DH_SUCCESS) { + DHLOGE("Close speaker failed."); return; } - // TODO: callback + DHLOGI("Close speaker successfully."); } void DAudioSourceDev::SourceEventHandler::OpenDMicCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskOpenDMic(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskOpenDMic(eventParam) != DH_SUCCESS) { + DHLOGE("Open mic failed."); return; } - // TODO: callback + DHLOGI("Open mic successfully."); } void DAudioSourceDev::SourceEventHandler::CloseDMicCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskCloseDMic(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskCloseDMic(eventParam) != DH_SUCCESS) { + DHLOGE("Close mic failed."); return; } - // TODO: callback + DHLOGI("Close mic successfully."); } void DAudioSourceDev::SourceEventHandler::OpenCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskOpenCtrlChannel(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskOpenCtrlChannel(eventParam) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); return; } - // TODO: callback + DHLOGI("Open ctrl channel successfully."); } void DAudioSourceDev::SourceEventHandler::CloseCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskCloseCtrlChannel(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskCloseCtrlChannel(eventParam) != DH_SUCCESS) { + DHLOGE("Close ctrl channel failed."); return; } - // TODO: callback + DHLOGI("Close ctrl channel successfully."); } void DAudioSourceDev::SourceEventHandler::SetVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskSetVolume(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskSetVolume(eventParam) != DH_SUCCESS) { + DHLOGE("Set volume failed."); return; } - // TODO: callback + DHLOGI("Set audio volume successfully."); } void DAudioSourceDev::SourceEventHandler::ChangeVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskChangeVolume(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskChangeVolume(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to process volume change event."); return; } - // TODO: callback + DHLOGI("Processing volume change event successfully."); } void DAudioSourceDev::SourceEventHandler::ChangeFocusCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskChangeFocus(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskChangeFocus(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to process focus change event."); return; } - // TODO: callback + DHLOGI("Processing volume change event successfully."); } void DAudioSourceDev::SourceEventHandler::ChangeRenderStateCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskChangeRenderState(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskChangeRenderState(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to process render state change event."); return; } - // TODO: callback + DHLOGI("Processing render state change event successfully."); } void DAudioSourceDev::SourceEventHandler::PlayStatusChangeCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskPlayStatusChange(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskPlayStatusChange(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to process playing status change event."); return; } - // TODO: callback + DHLOGI("Processing playing status change event successfully."); } void DAudioSourceDev::SourceEventHandler::SpkMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskSpkMmapStart(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskSpkMmapStart(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to start speaker with mmap mode."); return; } - // TODO: callback + DHLOGI("Start speaker with mmap mode successfully."); } void DAudioSourceDev::SourceEventHandler::SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskSpkMmapStop(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskSpkMmapStop(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to stop speaker with mmap mode."); return; } - // TODO: callback + DHLOGI("Stop speaker with mmap mode successfully."); } void DAudioSourceDev::SourceEventHandler::MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskMicMmapStart(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskMicMmapStart(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to start mic with mmap mode."); return; } - // TODO: callback + DHLOGI("Start mic with mmap mode successfully."); } void DAudioSourceDev::SourceEventHandler::MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event) { - if (event == nullptr || sourceDev_.expired()) { - DHLOGE("The input event or sink dev is null."); + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); return; } - std::shared_ptr eventParam = event->GetSharedObject(); - if (eventParam == nullptr) { - DHLOGE("The event parameter is null."); + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); return; } - if (sourceDev_.lock()->TaskSpkMmapStop(eventParam->content) != DH_SUCCESS) { - DHLOGE("Open ctrl channel failed."); // TODO: + if (sourceDevObj->TaskMicMmapStop(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to stop mic with mmap mode."); return; } - // TODO: callback + DHLOGI("Stop mic with mmap mode successfully."); +} + +int32_t DAudioSourceDev::SourceEventHandler::GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, + std::string &eventParam) +{ + if (event == nullptr) { + DHLOGE("The input event is null."); + return ERR_DH_AUDIO_NULLPTR; + } + std::shared_ptr paramObj = event->GetSharedObject(); + if (paramObj == nullptr) { + DHLOGE("The event parameter object is nullptr."); + return ERR_DH_AUDIO_NULLPTR; + } + eventParam = paramObj->content; + return DH_SUCCESS; } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp b/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp index 29a4200f..5e9903a3 100644 --- a/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp +++ b/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp @@ -153,14 +153,12 @@ HWTEST_F(DAudioSourceDevTest, CreatTasks_002, TestSize.Level1) */ HWTEST_F(DAudioSourceDevTest, CreatTasks_003, TestSize.Level1) { - size_t tempCapacity = 0; - sourceDev_->taskQueue_ = std::make_shared(tempCapacity); + sourceDev_->AwakeAudioDev(); AudioEvent event = AudioEvent(OPEN_SPEAKER, ""); EXPECT_EQ(ERR_DH_AUDIO_SA_OPEN_CTRL_FAILED, sourceDev_->HandleOpenDSpeaker(event)); event.type = OPEN_MIC; EXPECT_EQ(ERR_DH_AUDIO_SA_OPEN_CTRL_FAILED, sourceDev_->HandleOpenDMic(event)); - sourceDev_->taskQueue_ = nullptr; } /** @@ -191,9 +189,8 @@ HWTEST_F(DAudioSourceDevTest, HandlePlayStatusChange_001, TestSize.Level1) AudioEvent event = AudioEvent(CHANGE_PLAY_STATUS, ""); EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, sourceDev_->HandlePlayStatusChange(event)); - sourceDev_->taskQueue_ = std::make_shared(TASK_QUEUE_LEN); + sourceDev_->AwakeAudioDev(); EXPECT_EQ(DH_SUCCESS, sourceDev_->HandlePlayStatusChange(event)); - sourceDev_->taskQueue_ = nullptr; } /** @@ -297,11 +294,8 @@ HWTEST_F(DAudioSourceDevTest, OpenCtrlTrans_001, TestSize.Level1) EXPECT_EQ(DH_SUCCESS, sourceDev_->OpenCtrlTrans(event)); sourceDev_->audioCtrlMgr_->isOpened_ = false; - size_t tempCapacity = 0; - sourceDev_->taskQueue_ = std::make_shared(tempCapacity); + sourceDev_->AwakeAudioDev(); EXPECT_EQ(DH_SUCCESS, sourceDev_->OpenCtrlTrans(event)); - sourceDev_->taskQueue_ = nullptr; - sourceDev_->audioCtrlMgr_ = nullptr; } @@ -330,7 +324,7 @@ HWTEST_F(DAudioSourceDevTest, CloseCtrlTrans_001, TestSize.Level1) HWTEST_F(DAudioSourceDevTest, CloseCtrlTrans_002, TestSize.Level1) { AudioEvent event; - sourceDev_->taskQueue_ = std::make_shared(TASK_QUEUE_LEN); + sourceDev_->AwakeAudioDev(); bool isSpk = false; sourceDev_->mic_ = nullptr; @@ -349,8 +343,6 @@ HWTEST_F(DAudioSourceDevTest, CloseCtrlTrans_002, TestSize.Level1) sourceDev_->mic_->isOpened_ = true; EXPECT_EQ(DH_SUCCESS, sourceDev_->CloseCtrlTrans(event, isSpk)); - - sourceDev_->taskQueue_ = nullptr; } /** @@ -364,14 +356,8 @@ HWTEST_F(DAudioSourceDevTest, HandleOpenCtrlTrans_001, TestSize.Level1) AudioEvent event; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, sourceDev_->HandleOpenCtrlTrans(event)); - sourceDev_->taskQueue_ = std::make_shared(TASK_QUEUE_LEN); + sourceDev_->AwakeAudioDev(); EXPECT_EQ(DH_SUCCESS, sourceDev_->HandleOpenCtrlTrans(event)); - sourceDev_->taskQueue_ = nullptr; - - size_t tempCapacity = 0; - sourceDev_->taskQueue_ = std::make_shared(tempCapacity); - EXPECT_EQ(ERR_DH_AUDIO_SA_TASKQUEUE_FULL, sourceDev_->HandleOpenCtrlTrans(event)); - sourceDev_->taskQueue_ = nullptr; } /** @@ -385,14 +371,8 @@ HWTEST_F(DAudioSourceDevTest, HandleCloseCtrlTrans_001, TestSize.Level1) AudioEvent event; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, sourceDev_->HandleCloseCtrlTrans(event)); - sourceDev_->taskQueue_ = std::make_shared(TASK_QUEUE_LEN); + sourceDev_->AwakeAudioDev(); EXPECT_EQ(DH_SUCCESS, sourceDev_->HandleCloseCtrlTrans(event)); - sourceDev_->taskQueue_ = nullptr; - - size_t tempCapacity = 0; - sourceDev_->taskQueue_ = std::make_shared(tempCapacity); - EXPECT_EQ(ERR_DH_AUDIO_SA_TASKQUEUE_FULL, sourceDev_->HandleCloseCtrlTrans(event)); - sourceDev_->taskQueue_ = nullptr; } /** @@ -475,9 +455,8 @@ HWTEST_F(DAudioSourceDevTest, HandleSpkMmapStart_001, TestSize.Level1) AudioEvent event; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, sourceDev_->HandleSpkMmapStart(event)); - sourceDev_->taskQueue_ = std::make_shared(TASK_QUEUE_LEN); + sourceDev_->AwakeAudioDev(); EXPECT_EQ(DH_SUCCESS, sourceDev_->HandleSpkMmapStart(event)); - sourceDev_->taskQueue_ = nullptr; } /** @@ -491,9 +470,8 @@ HWTEST_F(DAudioSourceDevTest, HandleSpkMmapStop_001, TestSize.Level1) AudioEvent event; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, sourceDev_->HandleSpkMmapStop(event)); - sourceDev_->taskQueue_ = std::make_shared(TASK_QUEUE_LEN); + sourceDev_->AwakeAudioDev(); EXPECT_EQ(DH_SUCCESS, sourceDev_->HandleSpkMmapStop(event)); - sourceDev_->taskQueue_ = nullptr; } /** @@ -507,9 +485,8 @@ HWTEST_F(DAudioSourceDevTest, HandleMicMmapStart_001, TestSize.Level1) AudioEvent event; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, sourceDev_->HandleMicMmapStart(event)); - sourceDev_->taskQueue_ = std::make_shared(TASK_QUEUE_LEN); + sourceDev_->AwakeAudioDev(); EXPECT_EQ(DH_SUCCESS, sourceDev_->HandleMicMmapStart(event)); - sourceDev_->taskQueue_ = nullptr; } /** @@ -523,9 +500,8 @@ HWTEST_F(DAudioSourceDevTest, HandleMicMmapStop_001, TestSize.Level1) AudioEvent event; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, sourceDev_->HandleMicMmapStop(event)); - sourceDev_->taskQueue_ = std::make_shared(TASK_QUEUE_LEN); + sourceDev_->AwakeAudioDev(); EXPECT_EQ(DH_SUCCESS, sourceDev_->HandleMicMmapStop(event)); - sourceDev_->taskQueue_ = nullptr; } /** -- Gitee