diff --git a/services/audiomanager/managersink/include/daudio_sink_dev.h b/services/audiomanager/managersink/include/daudio_sink_dev.h index 58fb5fc99d4e264078fd1906c99d5ee63d7c427e..905a7b0b5b654db181f7f706fb78e5f531c6129b 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 ff347463ee949cfd82a762a47e89d44618231228..053b88dbfcf1f9e59bb3b0446872a9a25a6529c0 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()->TaskCloseDMic(audioEvent->content) != DH_SUCCESS) { - DHLOGE("Close mic failed."); + 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 976a88a5ec215594f3ce444d567004aca45e58ea..b8a35fabf1e1d8970543df5c93b9a0bf86451d92 100644 --- a/services/audiomanager/managersource/include/daudio_source_dev.h +++ b/services/audiomanager/managersource/include/daudio_source_dev.h @@ -20,6 +20,8 @@ #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" @@ -30,7 +32,6 @@ #include "iaudio_datatrans_callback.h" #include "idaudio_ipc_callback.h" #include "idaudio_hdi_callback.h" -#include "task_queue.h" using json = nlohmann::json; @@ -123,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_; @@ -136,9 +134,43 @@ 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); + int32_t GetEventParam(const AppExecFwk::InnerEvent::Pointer &event, std::string &eventParam); + + 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 b21b9c441551d3d51642dfefb80657a985c456b3..1ae4cbc03c76c4028c441a9925940a5cb75c3a3c 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -30,6 +30,26 @@ namespace OHOS { namespace DistributedHardware { +namespace { +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) : devId_(devId), mgrCallback_(callback) { @@ -69,44 +89,51 @@ 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."); + return ERR_DH_AUDIO_NULLPTR; + } + handler_ = std::make_shared(runner, shared_from_this()); return DH_SUCCESS; } void DAudioSourceDev::SleepAudioDev() { - if (taskQueue_ == nullptr) { - DHLOGI("Task queue already stop."); + if (handler_ == nullptr) { + DHLOGI("Event handler is already stoped."); return; } - taskQueue_->Stop(); - taskQueue_ = nullptr; + 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); + 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; } 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 +159,14 @@ 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); - DHLOGD("Disable audio task generate success."); - return taskQueue_->Produce(task); + 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; + } + DHLOGD("Disable audio task generate successfully."); + return DH_SUCCESS; } void DAudioSourceDev::NotifyEvent(const AudioEvent &event) @@ -153,30 +184,39 @@ 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); + 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) { 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); + 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); } @@ -200,29 +240,40 @@ 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); + 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; } 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); + 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); } @@ -256,26 +307,37 @@ 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); + 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; } 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); + 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; } int32_t DAudioSourceDev::HandleCtrlTransClosed(const AudioEvent &event) @@ -320,109 +382,163 @@ 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); + 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; } 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); + 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; } 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); + 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; } 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); + 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; } 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); + 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; } 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); + 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; } 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); + 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; } 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); + 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; } 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); + 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; } int32_t DAudioSourceDev::WaitForRPC(const AudioEventType type) @@ -1051,5 +1167,388 @@ 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) { + 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; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskOpenDSpeaker(eventParam) != DH_SUCCESS) { + DHLOGE("Open speaker failed."); + return; + } + DHLOGI("Open speaker successfully."); +} + +void DAudioSourceDev::SourceEventHandler::CloseDSpeakerCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskCloseDSpeaker(eventParam) != DH_SUCCESS) { + DHLOGE("Close speaker failed."); + return; + } + DHLOGI("Close speaker successfully."); +} + +void DAudioSourceDev::SourceEventHandler::OpenDMicCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskOpenDMic(eventParam) != DH_SUCCESS) { + DHLOGE("Open mic failed."); + return; + } + DHLOGI("Open mic successfully."); +} + +void DAudioSourceDev::SourceEventHandler::CloseDMicCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskCloseDMic(eventParam) != DH_SUCCESS) { + DHLOGE("Close mic failed."); + return; + } + DHLOGI("Close mic successfully."); +} + +void DAudioSourceDev::SourceEventHandler::OpenCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskOpenCtrlChannel(eventParam) != DH_SUCCESS) { + DHLOGE("Open ctrl channel failed."); + return; + } + DHLOGI("Open ctrl channel successfully."); +} + +void DAudioSourceDev::SourceEventHandler::CloseCtrlCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskCloseCtrlChannel(eventParam) != DH_SUCCESS) { + DHLOGE("Close ctrl channel failed."); + return; + } + DHLOGI("Close ctrl channel successfully."); +} + +void DAudioSourceDev::SourceEventHandler::SetVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskSetVolume(eventParam) != DH_SUCCESS) { + DHLOGE("Set volume failed."); + return; + } + DHLOGI("Set audio volume successfully."); +} + +void DAudioSourceDev::SourceEventHandler::ChangeVolumeCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskChangeVolume(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to process volume change event."); + return; + } + DHLOGI("Processing volume change event successfully."); +} + +void DAudioSourceDev::SourceEventHandler::ChangeFocusCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskChangeFocus(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to process focus change event."); + return; + } + DHLOGI("Processing volume change event successfully."); +} + +void DAudioSourceDev::SourceEventHandler::ChangeRenderStateCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskChangeRenderState(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to process render state change event."); + return; + } + DHLOGI("Processing render state change event successfully."); +} + +void DAudioSourceDev::SourceEventHandler::PlayStatusChangeCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskPlayStatusChange(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to process playing status change event."); + return; + } + DHLOGI("Processing playing status change event successfully."); +} + +void DAudioSourceDev::SourceEventHandler::SpkMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskSpkMmapStart(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to start speaker with mmap mode."); + return; + } + DHLOGI("Start speaker with mmap mode successfully."); +} + +void DAudioSourceDev::SourceEventHandler::SpkMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskSpkMmapStop(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to stop speaker with mmap mode."); + return; + } + DHLOGI("Stop speaker with mmap mode successfully."); +} + +void DAudioSourceDev::SourceEventHandler::MicMmapStartCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskMicMmapStart(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to start mic with mmap mode."); + return; + } + DHLOGI("Start mic with mmap mode successfully."); +} + +void DAudioSourceDev::SourceEventHandler::MicMmapStopCallback(const AppExecFwk::InnerEvent::Pointer &event) +{ + std::string eventParam; + if (GetEventParam(event, eventParam) != DH_SUCCESS) { + DHLOGE("Failed to get event parameters."); + return; + } + auto sourceDevObj = sourceDev_.lock(); + if (sourceDevObj == nullptr) { + DHLOGE("Source dev is invalid."); + return; + } + if (sourceDevObj->TaskMicMmapStop(eventParam) != DH_SUCCESS) { + DHLOGE("Failed to stop mic with mmap mode."); + return; + } + 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/servicesource/BUILD.gn b/services/audiomanager/servicesource/BUILD.gn index 53e3d26a09df8c6f848937b9bcc68b8d1ed2888e..b5ad8d023371ac7b462a03b067990a4399866a16 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", 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 29a4200f590bfecb350b3898bd36ff33b827122d..5e9903a305b8cfbf3f82a9d845153c6d1a1b7f30 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; } /**