From 8468aa15ebefde143a9a875f11b470bb41fa55b2 Mon Sep 17 00:00:00 2001 From: hoperun_wangyupan Date: Wed, 12 Aug 2026 18:25:51 +0800 Subject: [PATCH] add start event MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hoperun_wangyupan 🤖 AI[0%] 🔧 Human Fixed[0%] 🧑 Human[100%] 👌 AI Adopted[0%] --- .../include/audio_render_interface_impl.h | 1 + .../v1_0/src/audio_render_interface_impl.cpp | 63 +++++++++++-------- .../src/audio_render_interface_impl_test.cpp | 54 +++++++++++++++- .../common/include/daudio_constants.h | 1 + 4 files changed, 92 insertions(+), 27 deletions(-) diff --git a/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl.h b/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl.h index 18334b79ac..a509633a07 100644 --- a/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl.h +++ b/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl.h @@ -111,6 +111,7 @@ private: float GetFadeRate(uint32_t currentIndex, const uint32_t durationIndex); int32_t FadeInProcess(const uint32_t durationFrame, int8_t* frameData, const size_t frameLength); int32_t SetOffloadParamsChange(const std::string &offloadParams); + int32_t NotifyPlayStatusChange(const std::string &changeType); #ifdef DAUDIO_SUPPORT_SHARED_BUFFER int32_t CreateAshmem(int32_t ashmemLength); int32_t WriteToShmem(const AudioData &data); diff --git a/distributed_audio/hdi_service/audio/v1_0/src/audio_render_interface_impl.cpp b/distributed_audio/hdi_service/audio/v1_0/src/audio_render_interface_impl.cpp index 7faae93e71..cec7a25770 100644 --- a/distributed_audio/hdi_service/audio/v1_0/src/audio_render_interface_impl.cpp +++ b/distributed_audio/hdi_service/audio/v1_0/src/audio_render_interface_impl.cpp @@ -251,6 +251,37 @@ int32_t AudioRenderInterfaceImpl::IsSupportsDrain(bool &support) return HDF_SUCCESS; } +int32_t AudioRenderInterfaceImpl::NotifyPlayStatusChange(const std::string &changeType) +{ + cJSON *jParam = cJSON_CreateObject(); + if (jParam == nullptr) { + DHLOGE("Failed to create cJSON object."); + return HDF_FAILURE; + } + cJSON_AddStringToObject(jParam, KEY_DH_ID, std::to_string(devDesc_.pins).c_str()); + cJSON_AddStringToObject(jParam, "ChangeType", changeType.c_str()); + char *jsonData = cJSON_PrintUnformatted(jParam); + if (jsonData == nullptr) { + DHLOGE("Failed to create JSON data."); + cJSON_Delete(jParam); + return HDF_FAILURE; + } + std::string content(jsonData); + cJSON_Delete(jParam); + cJSON_free(jsonData); + + DAudioEvent event = { HDF_AUDIO_EVENT_CHANGE_PLAY_STATUS, content }; + if (audioExtCallback_ == nullptr) { + DHLOGE("Callback is nullptr."); + return HDF_FAILURE; + } + int32_t ret = audioExtCallback_->NotifyEvent(renderId_, event); + if (ret != HDF_SUCCESS) { + DHLOGE("Notify play status change failed, type: %{public}s.", changeType.c_str()); + } + return HDF_SUCCESS; +} + int32_t AudioRenderInterfaceImpl::Start() { DHLOGI("Start render."); @@ -258,35 +289,15 @@ int32_t AudioRenderInterfaceImpl::Start() DHLOGI("Render status wrong, return false."); return HDF_FAILURE; } + + std::string changeType = firstOpenFlag_ ? HDF_EVENT_START : HDF_EVENT_RESTART; if (firstOpenFlag_) { firstOpenFlag_ = false; - } else { - cJSON *jParam = cJSON_CreateObject(); - if (jParam == nullptr) { - DHLOGE("Failed to create cJSON object."); - return HDF_FAILURE; - } - cJSON_AddStringToObject(jParam, KEY_DH_ID, std::to_string(devDesc_.pins).c_str()); - cJSON_AddStringToObject(jParam, "ChangeType", HDF_EVENT_RESTART.c_str()); - char *jsonData = cJSON_PrintUnformatted(jParam); - if (jsonData == nullptr) { - DHLOGE("Failed to create JSON data."); - cJSON_Delete(jParam); - return HDF_FAILURE; - } - std::string content(jsonData); - cJSON_Delete(jParam); - cJSON_free(jsonData); - DAudioEvent event = { HDF_AUDIO_EVENT_CHANGE_PLAY_STATUS, content}; - if (audioExtCallback_ == nullptr) { - DHLOGE("Callback is nullptr."); - return HDF_FAILURE; - } - int32_t ret = audioExtCallback_->NotifyEvent(renderId_, event); - if (ret != HDF_SUCCESS) { - DHLOGE("Restart failed."); - } } + if (NotifyPlayStatusChange(changeType) != HDF_SUCCESS) { + return HDF_FAILURE; + } + #ifdef DAUDIO_SUPPORT_SHARED_BUFFER writeIndex_ = 0; writeNum_ = 0; diff --git a/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_render_interface/src/audio_render_interface_impl_test.cpp b/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_render_interface/src/audio_render_interface_impl_test.cpp index 00e78d12da..36288f0e81 100644 --- a/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_render_interface/src/audio_render_interface_impl_test.cpp +++ b/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_render_interface/src/audio_render_interface_impl_test.cpp @@ -352,7 +352,7 @@ HWTEST_F(AudioRenderInterfaceImplTest, Start_003, TestSize.Level1) EXPECT_EQ(HDF_FAILURE, audioRenderInterfaceImpl_->Start()); audioRenderInterfaceImpl_->SetRenderStatus(RENDER_STATUS_OPEN); audioRenderInterfaceImpl_->audioExtCallback_ = sptr(new MockRevertIDAudioCallback()); - EXPECT_EQ(HDF_SUCCESS, audioRenderInterfaceImpl_->Start()); + EXPECT_EQ(HDF_FAILURE, audioRenderInterfaceImpl_->Start()); } /** @@ -971,6 +971,58 @@ HWTEST_F(AudioRenderInterfaceImplTest, FadeInProcess_001, TestSize.Level1) EXPECT_EQ(HDF_ERR_INVALID_PARAM, audioRenderInterfaceImpl_->FadeInProcess(durationFrame, frameData, errLength)); delete[] frameData; } + +/** + * @tc.name: NotifyPlayStatusChange_001 + * @tc.desc: Verify the NotifyPlayStatusChange function when callback is nullptr. + * @tc.type: FUNC + * @tc.require: AR000H0E6H + */ +HWTEST_F(AudioRenderInterfaceImplTest, NotifyPlayStatusChange_001, TestSize.Level1) +{ + ASSERT_NE(nullptr, audioRenderInterfaceImpl_); + audioRenderInterfaceImpl_->audioExtCallback_ = nullptr; + EXPECT_EQ(HDF_FAILURE, audioRenderInterfaceImpl_->NotifyPlayStatusChange(DistributedHardware::HDF_EVENT_START)); +} + +/** + * @tc.name: NotifyPlayStatusChange_002 + * @tc.desc: Verify the NotifyPlayStatusChange function when NotifyEvent fails. + * @tc.type: FUNC + * @tc.require: AR000H0E6H + */ +HWTEST_F(AudioRenderInterfaceImplTest, NotifyPlayStatusChange_002, TestSize.Level1) +{ + ASSERT_NE(nullptr, audioRenderInterfaceImpl_); + audioRenderInterfaceImpl_->audioExtCallback_ = sptr(new MockRevertIDAudioCallback()); + EXPECT_EQ(HDF_SUCCESS, audioRenderInterfaceImpl_->NotifyPlayStatusChange(DistributedHardware::HDF_EVENT_START)); +} + +/** + * @tc.name: NotifyPlayStatusChange_003 + * @tc.desc: Verify the NotifyPlayStatusChange function with HDF_EVENT_START. + * @tc.type: FUNC + * @tc.require: AR000H0E6H + */ +HWTEST_F(AudioRenderInterfaceImplTest, NotifyPlayStatusChange_003, TestSize.Level1) +{ + ASSERT_NE(nullptr, audioRenderInterfaceImpl_); + audioRenderInterfaceImpl_->audioExtCallback_ = sptr(new MockIDAudioCallback()); + EXPECT_EQ(HDF_SUCCESS, audioRenderInterfaceImpl_->NotifyPlayStatusChange(DistributedHardware::HDF_EVENT_START)); +} + +/** + * @tc.name: NotifyPlayStatusChange_004 + * @tc.desc: Verify the NotifyPlayStatusChange function with HDF_EVENT_RESTART. + * @tc.type: FUNC + * @tc.require: AR000H0E6H + */ +HWTEST_F(AudioRenderInterfaceImplTest, NotifyPlayStatusChange_004, TestSize.Level1) +{ + ASSERT_NE(nullptr, audioRenderInterfaceImpl_); + audioRenderInterfaceImpl_->audioExtCallback_ = sptr(new MockIDAudioCallback()); + EXPECT_EQ(HDF_SUCCESS, audioRenderInterfaceImpl_->NotifyPlayStatusChange(DistributedHardware::HDF_EVENT_RESTART)); +} } // V2_0 } // Audio } // Distributedaudio diff --git a/distributed_audio/hdi_service/common/include/daudio_constants.h b/distributed_audio/hdi_service/common/include/daudio_constants.h index f46d935a67..b2d0ba5ee7 100644 --- a/distributed_audio/hdi_service/common/include/daudio_constants.h +++ b/distributed_audio/hdi_service/common/include/daudio_constants.h @@ -53,6 +53,7 @@ const std::string HDF_EVENT_RESULT_FAILED = "DH_FAILED"; const std::string HDF_EVENT_RESTART = "restart"; const std::string HDF_EVENT_PAUSE = "pause"; +const std::string HDF_EVENT_START = "start"; const std::string PRINT_SPK = "spk"; const std::string PRINT_MIC = "mic";