From 4fd57ca3c7bc9e033a9393dd2176f84e3642c4c5 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Fri, 13 Sep 2024 14:12:50 +0800 Subject: [PATCH] add notify Signed-off-by: w30042960 --- common/include/daudio_log.h | 8 +++ .../audioclient/micclient/src/dmic_client.cpp | 1 + .../spkclient/src/dspeaker_client.cpp | 1 + .../managersink/include/daudio_sink_dev.h | 1 + .../managersink/include/daudio_sink_manager.h | 1 + .../managersink/src/daudio_sink_dev.cpp | 59 ++++++++++------ .../managersink/src/daudio_sink_manager.cpp | 68 +++++++++++++++---- .../src/daudio_sink_manager_test.cpp | 17 +++-- 8 files changed, 118 insertions(+), 38 deletions(-) diff --git a/common/include/daudio_log.h b/common/include/daudio_log.h index 3d597a23..78e84148 100644 --- a/common/include/daudio_log.h +++ b/common/include/daudio_log.h @@ -54,6 +54,14 @@ void DHLog(DHLogLevel logLevel, const char *fmt, ...); return (ret); \ } \ } while (0) + +#define CHECK_AND_RETURN_LOG(cond, fmt, ...) \ + do { \ + if ((cond)) { \ + DHLOGE(fmt, ##__VA_ARGS__); \ + return; \ + } \ + } while (0) } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DAUDIO_LOG_H diff --git a/services/audioclient/micclient/src/dmic_client.cpp b/services/audioclient/micclient/src/dmic_client.cpp index 394a389b..95090e77 100644 --- a/services/audioclient/micclient/src/dmic_client.cpp +++ b/services/audioclient/micclient/src/dmic_client.cpp @@ -165,6 +165,7 @@ int32_t DMicClient::SendMessage(uint32_t type, std::string content, std::string { DHLOGI("Send message to remote."); if (type != static_cast(NOTIFY_OPEN_MIC_RESULT) && + type != static_cast(NOTIFY_OPEN_CTRL_RESULT) && type != static_cast(NOTIFY_CLOSE_MIC_RESULT) && type != static_cast(CLOSE_MIC)) { DHLOGE("event type is not NOTIFY_OPEN_MIC or NOTIFY_CLOSE_MIC or CLOSE_MIC. type: %u", type); diff --git a/services/audioclient/spkclient/src/dspeaker_client.cpp b/services/audioclient/spkclient/src/dspeaker_client.cpp index 5a800462..b13cc8d8 100644 --- a/services/audioclient/spkclient/src/dspeaker_client.cpp +++ b/services/audioclient/spkclient/src/dspeaker_client.cpp @@ -609,6 +609,7 @@ int32_t DSpeakerClient::SendMessage(uint32_t type, std::string content, std::str { DHLOGI("Send message to remote."); if (type != static_cast(NOTIFY_OPEN_SPEAKER_RESULT) && + type != static_cast(NOTIFY_OPEN_CTRL_RESULT) && type != static_cast(NOTIFY_CLOSE_SPEAKER_RESULT) && type != static_cast(VOLUME_CHANGE) && type != static_cast(AUDIO_FOCUS_CHANGE) && diff --git a/services/audiomanager/managersink/include/daudio_sink_dev.h b/services/audiomanager/managersink/include/daudio_sink_dev.h index 87841dfd..ef5c393f 100644 --- a/services/audiomanager/managersink/include/daudio_sink_dev.h +++ b/services/audiomanager/managersink/include/daudio_sink_dev.h @@ -80,6 +80,7 @@ private: void PullUpPage(); int32_t ParseDhidFromEvent(std::string args); + int32_t ParseResultFromEvent(std::string args); int32_t ConvertString2Int(std::string val); private: diff --git a/services/audiomanager/managersink/include/daudio_sink_manager.h b/services/audiomanager/managersink/include/daudio_sink_manager.h index a88bcba4..9095b7a7 100644 --- a/services/audiomanager/managersink/include/daudio_sink_manager.h +++ b/services/audiomanager/managersink/include/daudio_sink_manager.h @@ -73,6 +73,7 @@ private: int32_t GetDeviceSecurityLevel(const std::string &udid); std::string GetUdidByNetworkId(const std::string &networkId); int32_t VerifySecurityLevel(const std::string &devId); + int32_t InitAudioDevice(std::shared_ptr dev, const std::string &devId, bool isSpkOrMic); private: static constexpr const char* DEVCLEAR_THREAD = "sinkClearTh"; diff --git a/services/audiomanager/managersink/src/daudio_sink_dev.cpp b/services/audiomanager/managersink/src/daudio_sink_dev.cpp index 5096b4bb..eecb32f1 100644 --- a/services/audiomanager/managersink/src/daudio_sink_dev.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_dev.cpp @@ -201,6 +201,29 @@ int32_t DAudioSinkDev::ParseDhidFromEvent(std::string args) return dhId; } +int32_t DAudioSinkDev::ParseResultFromEvent(std::string args) +{ + DHLOGI("ParseResultFrom args : %s", args.c_str()); + cJSON *jParam = cJSON_Parse(args.c_str()); + CHECK_NULL_RETURN(jParam, ERR_DH_AUDIO_FAILED); + + if (!CJsonParamCheck(jParam, { KEY_RESULT })) { + DHLOGE("Not found the keys of result."); + cJSON_Delete(jParam); + return -1; + } + cJSON *retItem = cJSON_GetObjectItem(jParam, KEY_RESULT); + if (retItem == NULL || !cJSON_IsString(retItem)) { + DHLOGE("Not found the keys of result."); + cJSON_Delete(jParam); + return ERR_DH_AUDIO_FAILED; + } + int32_t ret = ConvertString2Int(std::string(retItem->valuestring)); + cJSON_Delete(jParam); + DHLOGI("Parsed result is: %d.", ret); + return ret; +} + int32_t DAudioSinkDev::TaskStartRender(const std::string &args) { int32_t dhId = ParseDhidFromEvent(args); @@ -472,11 +495,7 @@ void DAudioSinkDev::NotifySourceDev(const AudioEventType type, const std::string jEvent[KEY_EVENT_TYPE] = type; jEvent[KEY_RANDOM_TASK_CODE] = std::to_string(randomTaskCode); - DHLOGD("Notify source dev, new engine, random task code:%s", std::to_string(randomTaskCode).c_str()); - if (type == NOTIFY_OPEN_CTRL_RESULT || type == NOTIFY_CLOSE_CTRL_RESULT) { - DHLOGE("In new engine mode, ctrl is not allowed."); - return; - } + DHLOGI("Notify source dev, new engine, random task code:%s", std::to_string(randomTaskCode).c_str()); int32_t dhIdInt = ConvertString2Int(dhId); if (dhIdInt == -1) { DHLOGE("Parse dhId error."); @@ -564,13 +583,7 @@ void DAudioSinkDev::SinkEventHandler::ProcessEvent(const AppExecFwk::InnerEvent: void DAudioSinkDev::SinkEventHandler::NotifyCtrlOpened(const AppExecFwk::InnerEvent::Pointer &event) { - DHLOGI("Ctrl channel is opened."); - (void)event; -} - -void DAudioSinkDev::SinkEventHandler::NotifyCtrlClosed(const AppExecFwk::InnerEvent::Pointer &event) -{ - DHLOGI("Notify ctrl closed."); + DHLOGI("Ctrl channel is opened. begin to init dev, then to notify source dev."); std::string eventParam; if (GetEventParam(event, eventParam) != DH_SUCCESS) { DHLOGE("Failed to get event parameters."); @@ -578,15 +591,19 @@ void DAudioSinkDev::SinkEventHandler::NotifyCtrlClosed(const AppExecFwk::InnerEv } auto sinkDevObj = sinkDev_.lock(); CHECK_NULL_VOID(sinkDevObj); - if (sinkDevObj->TaskCloseDSpeaker(eventParam) != DH_SUCCESS) { - DHLOGE("Close speaker failed."); - return; - } - if (sinkDevObj->TaskCloseDMic(eventParam) != DH_SUCCESS) { - DHLOGE("Close mic failed."); - return; - } - sinkDevObj->JudgeDeviceStatus(); + + int32_t dhId = sinkDevObj->ParseDhidFromEvent(eventParam); + CHECK_AND_RETURN_LOG(dhId == -1, "%s", "Parse dhId error."); + int32_t ret = sinkDevObj->ParseResultFromEvent(eventParam); + sinkDevObj->NotifySourceDev(NOTIFY_OPEN_CTRL_RESULT, std::to_string(dhId), ret); + DHLOGI("Init sink device task end, notify source ret %d.", ret); + CHECK_AND_RETURN_LOG(ret != DH_SUCCESS, "%s", "Init sink device failed."); +} + +void DAudioSinkDev::SinkEventHandler::NotifyCtrlClosed(const AppExecFwk::InnerEvent::Pointer &event) +{ + DHLOGI("Notify ctrl closed."); + (void)event; } void DAudioSinkDev::SinkEventHandler::NotifyOpenSpeaker(const AppExecFwk::InnerEvent::Pointer &event) diff --git a/services/audiomanager/managersink/src/daudio_sink_manager.cpp b/services/audiomanager/managersink/src/daudio_sink_manager.cpp index c05403b3..fdb4f1e8 100644 --- a/services/audiomanager/managersink/src/daudio_sink_manager.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_manager.cpp @@ -15,6 +15,8 @@ #include "daudio_sink_manager.h" +#include "cJSON.h" + #include #include "if_system_ability_manager.h" #include "iservice_registry.h" @@ -174,30 +176,70 @@ int32_t DAudioSinkManager::CreateAudioDevice(const std::string &devId) dev = audioDevMap_[devId]; } else { dev = std::make_shared(devId, ipcSinkCallback_); - if (dev->AwakeAudioDev() != DH_SUCCESS) { - DHLOGE("Awake audio dev failed."); - return ERR_DH_AUDIO_FAILED; - } audioDevMap_.emplace(devId, dev); } } - int32_t ret = ERR_DH_AUDIO_FAILED; - if (channelState_ == ChannelState::SPK_CONTROL_OPENED) { - ret = dev->InitAVTransEngines(ChannelState::SPK_CONTROL_OPENED, rcvProviderPtr_); - } + int32_t dhId; + bool isSpkOrMic = false; if (channelState_ == ChannelState::MIC_CONTROL_OPENED) { - ret = dev->InitAVTransEngines(ChannelState::MIC_CONTROL_OPENED, sendProviderPtr_); - if (!ret) { - ret = VerifySecurityLevel(devId); + dhId = PIN_IN_MIC; + isSpkOrMic = false; + } else if (channelState_ == ChannelState::SPK_CONTROL_OPENED) { + dhId = PIN_OUT_SPEAKER; + isSpkOrMic = true; + } else { + DHLOGE("Channel state error."); + return ERR_DH_AUDIO_NOT_SUPPORT; + } + int32_t ret = InitAudioDevice(dev, devId, isSpkOrMic); + cJSON *jParam = cJSON_CreateObject(); + CHECK_NULL_RETURN(jParam, ERR_DH_AUDIO_NULLPTR); + cJSON_AddStringToObject(jParam, KEY_DH_ID, std::to_string(dhId).c_str()); + cJSON_AddStringToObject(jParam, KEY_RESULT, std::to_string(ret).c_str()); + char *jsonData = cJSON_PrintUnformatted(jParam); + if (jsonData == nullptr) { + DHLOGE("Failed to create JSON data."); + cJSON_Delete(jParam); + return ERR_DH_AUDIO_NULLPTR; + } + std::string eventContent = std::string(jsonData); + cJSON_free(jsonData); + cJSON_Delete(jParam); + NotifyEvent(devId, CTRL_OPENED, eventContent); + return DH_SUCCESS; +} + +int32_t DAudioSinkManager::InitAudioDevice(std::shared_ptr dev, const std::string &devId, + bool isSpkOrMic) +{ + DHLOGI("Init audio device."); + if (dev == nullptr) { + DHLOGE("dev is nullptr."); + return ERR_DH_AUDIO_NULLPTR; + } + int32_t ret; + if (isSpkOrMic) { + ret = dev->InitAVTransEngines(ChannelState::SPK_CONTROL_OPENED, rcvProviderPtr_); + } else { + ret = VerifySecurityLevel(devId); + if (ret != DH_SUCCESS) { + DHLOGE("Verify security level failed."); + return ERR_DH_AUDIO_FAILED; } + ret = dev->InitAVTransEngines(ChannelState::MIC_CONTROL_OPENED, sendProviderPtr_); } if (ret != DH_SUCCESS) { - DHLOGE("Init av transport sender engine failed."); + DHLOGE("Init av transport engine failed."); dev->JudgeDeviceStatus(); return ERR_DH_AUDIO_FAILED; } - return DH_SUCCESS; + ret = dev->AwakeAudioDev(); + if (ret != DH_SUCCESS) { + DHLOGE("Awake audio dev failed."); + return ERR_DH_AUDIO_FAILED; + } + return ret; } int32_t DAudioSinkManager::DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, diff --git a/services/audiomanager/test/unittest/managersink/src/daudio_sink_manager_test.cpp b/services/audiomanager/test/unittest/managersink/src/daudio_sink_manager_test.cpp index a1715fbc..a6b94fbd 100644 --- a/services/audiomanager/test/unittest/managersink/src/daudio_sink_manager_test.cpp +++ b/services/audiomanager/test/unittest/managersink/src/daudio_sink_manager_test.cpp @@ -87,14 +87,23 @@ HWTEST_F(DAudioSinkManagerTest, DAudioNotify_001, TestSize.Level1) HWTEST_F(DAudioSinkManagerTest, CreateAudioDevice_001, TestSize.Level1) { std::string devId = "devId"; - EXPECT_EQ(ERR_DH_AUDIO_FAILED, daudioSinkManager.CreateAudioDevice(devId)); + std::string params = "params"; + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + ASSERT_TRUE(samgr != nullptr); + sptr loadCallback(new DAudioSinkLoadCallback(params)); + samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_AUDIO_SINK_SA_ID, loadCallback); + sptr remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_AUDIO_SINK_SA_ID); + ASSERT_TRUE(remoteObject != nullptr); + sptr dAudioSinkIpcCallbackProxy(new DAudioSinkIpcCallbackProxy(remoteObject)); + daudioSinkManager.ipcSinkCallback_ = dAudioSinkIpcCallbackProxy; + EXPECT_EQ(ERR_DH_AUDIO_NOT_SUPPORT, daudioSinkManager.CreateAudioDevice(devId)); daudioSinkManager.audioDevMap_.emplace(devId, nullptr); - EXPECT_EQ(ERR_DH_AUDIO_FAILED, daudioSinkManager.CreateAudioDevice(devId)); + EXPECT_EQ(ERR_DH_AUDIO_NOT_SUPPORT, daudioSinkManager.CreateAudioDevice(devId)); daudioSinkManager.channelState_ = ChannelState::SPK_CONTROL_OPENED; - EXPECT_EQ(ERR_DH_AUDIO_FAILED, daudioSinkManager.CreateAudioDevice(devId)); + EXPECT_EQ(DH_SUCCESS, daudioSinkManager.CreateAudioDevice(devId)); daudioSinkManager.ClearAudioDev(devId); daudioSinkManager.channelState_ = ChannelState::MIC_CONTROL_OPENED; - EXPECT_EQ(ERR_DH_AUDIO_FAILED, daudioSinkManager.CreateAudioDevice(devId)); + EXPECT_EQ(DH_SUCCESS, daudioSinkManager.CreateAudioDevice(devId)); } /** -- Gitee