diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp index 57ec5fc02095a5cf1f1fcbca1fe1095b1abc0077..06a7e8ea6b7546980dd7000d0fd1c94f65097138 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp @@ -109,7 +109,7 @@ int32_t AudioAdapterInterfaceImpl::CreateRender(const AudioDeviceDescriptor &des if (ret != DH_SUCCESS) { DHLOGE("Open render device failed."); audioRender_ = nullptr; - return HDF_FAILURE; + return ret == ERR_DH_AUDIO_HDF_WAIT_TIMEOUT ? HDF_ERR_TIMEOUT : HDF_FAILURE; } render = audioRender_; DHLOGI("Create render success."); @@ -174,7 +174,7 @@ int32_t AudioAdapterInterfaceImpl::CreateCapture(const AudioDeviceDescriptor &de if (ret != DH_SUCCESS) { DHLOGE("Open capture device failed."); audioCapture_ = nullptr; - return HDF_FAILURE; + return ret == ERR_DH_AUDIO_HDF_WAIT_TIMEOUT ? HDF_ERR_TIMEOUT : HDF_FAILURE; } capture = audioCapture_; DHLOGI("Create capture success."); @@ -349,6 +349,10 @@ int32_t AudioAdapterInterfaceImpl::AdapterLoad() int32_t AudioAdapterInterfaceImpl::AdapterUnload() { + if (audioRender_ != nullptr || audioCapture_ != nullptr) { + DHLOGE("Adapter is busy, audio render or capture is not null."); + return HDF_ERR_DEVICE_BUSY; + } status_ = AudioAdapterStatus::STATUS_UNLOAD; return HDF_SUCCESS; } @@ -737,40 +741,41 @@ int32_t AudioAdapterInterfaceImpl::HandleRenderStateChangeEvent(const DAudioEven int32_t AudioAdapterInterfaceImpl::HandleSANotifyEvent(const DAudioEvent &event) { - if (event.type == HDF_AUDIO_EVENT_OPEN_SPK_RESULT) { - DHLOGD("Notify event: OPEN_SPK_RESULT, event content: %s.", event.content.c_str()); - if (event.content == HDF_EVENT_RESULT_SUCCESS) { - isSpkOpened_ = true; - } - spkNotifyFlag_ = true; - spkWaitCond_.notify_all(); - return DH_SUCCESS; - } else if (event.type == HDF_AUDIO_EVENT_CLOSE_SPK_RESULT) { - DHLOGD("Notify event: CLOSE_SPK_RESULT, event content: %s.", event.content.c_str()); - if (event.content == HDF_EVENT_RESULT_SUCCESS) { - isSpkOpened_ = false; - } - spkNotifyFlag_ = true; - spkWaitCond_.notify_all(); - return DH_SUCCESS; - } else if (event.type == HDF_AUDIO_EVENT_OPEN_MIC_RESULT) { - DHLOGD("Notify event: OPEN_MIC_RESULT, event content: %s.", event.content.c_str()); - if (event.content == HDF_EVENT_RESULT_SUCCESS) { - isMicOpened_ = true; - } - micNotifyFlag_ = true; - micWaitCond_.notify_all(); - return DH_SUCCESS; - } else if (event.type == HDF_AUDIO_EVENT_CLOSE_MIC_RESULT) { - DHLOGD("Notify event: CLOSE_MIC_RESULT, event content: %s.", event.content.c_str()); - if (event.content == HDF_EVENT_RESULT_SUCCESS) { - isMicOpened_ = false; - } - micNotifyFlag_ = true; - micWaitCond_.notify_all(); - return DH_SUCCESS; + DHLOGD("Notify event type %d, event content: %s.", event.type, event.content.c_str()); + switch (event.type) { + case HDF_AUDIO_EVENT_OPEN_SPK_RESULT: + if (event.content == HDF_EVENT_RESULT_SUCCESS) { + isSpkOpened_ = true; + } + spkNotifyFlag_ = true; + spkWaitCond_.notify_all(); + break; + case HDF_AUDIO_EVENT_CLOSE_SPK_RESULT: + if (event.content == HDF_EVENT_RESULT_SUCCESS) { + isSpkOpened_ = false; + } + spkNotifyFlag_ = true; + spkWaitCond_.notify_all(); + break; + case HDF_AUDIO_EVENT_OPEN_MIC_RESULT: + if (event.content == HDF_EVENT_RESULT_SUCCESS) { + isMicOpened_ = true; + } + micNotifyFlag_ = true; + micWaitCond_.notify_all(); + break; + case HDF_AUDIO_EVENT_CLOSE_MIC_RESULT: + if (event.content == HDF_EVENT_RESULT_SUCCESS) { + isMicOpened_ = false; + } + micNotifyFlag_ = true; + micWaitCond_.notify_all(); + break; + default: + DHLOGE("Notify not support event type %d, event content: %s.", event.type, event.content.c_str()); + return ERR_DH_AUDIO_HDF_FAIL; } - return ERR_DH_AUDIO_HDF_FAIL; + return DH_SUCCESS; } int32_t AudioAdapterInterfaceImpl::WaitForSANotify(const AudioDeviceEvent &event) @@ -782,7 +787,7 @@ int32_t AudioAdapterInterfaceImpl::WaitForSANotify(const AudioDeviceEvent &event spkWaitCond_.wait_for(lck, std::chrono::seconds(WAIT_SECONDS), [this]() { return spkNotifyFlag_; }); if (!status) { DHLOGE("Wait spk event: %d timeout(%d)s.", event, WAIT_SECONDS); - return ERR_DH_AUDIO_HDF_FAIL; + return ERR_DH_AUDIO_HDF_WAIT_TIMEOUT; } if (event == EVENT_OPEN_SPK && isSpkOpened_ != true) { DHLOGE("Wait open render device failed."); @@ -801,7 +806,7 @@ int32_t AudioAdapterInterfaceImpl::WaitForSANotify(const AudioDeviceEvent &event micWaitCond_.wait_for(lck, std::chrono::seconds(WAIT_SECONDS), [this]() { return micNotifyFlag_; }); if (!status) { DHLOGE("Wait mic event: %d timeout(%d)s.", event, WAIT_SECONDS); - return ERR_DH_AUDIO_HDF_FAIL; + return ERR_DH_AUDIO_HDF_WAIT_TIMEOUT; } if (event == EVENT_OPEN_MIC && isMicOpened_ != true) { DHLOGE("Wait open capture device failed."); diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_manager_interface_impl.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_manager_interface_impl.cpp index 2a13a42b619f386e2b2175d0cacc22b1e699bc8f..eefc3af9201af9f88bec4b05959f6832b1f9f83c 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_manager_interface_impl.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_manager_interface_impl.cpp @@ -99,7 +99,7 @@ int32_t AudioManagerInterfaceImpl::UnloadAdapter(const std::string &adapterName) int32_t ret = adp->second->AdapterUnload(); if (ret != DH_SUCCESS) { DHLOGE("Unload audio adapter failed, adapter return: %d.", ret); - return HDF_SUCCESS; + return ret; } DHLOGI("Unload adapter success."); return HDF_SUCCESS; diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_adapter_interface/src/audio_adapter_interface_impl_test.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_adapter_interface/src/audio_adapter_interface_impl_test.cpp index c2c586a33c4e76e4404da95c4f7df715bd9dbf30..567eae96cfc8171f21976f2c3eba5bd49f0ae1bd 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_adapter_interface/src/audio_adapter_interface_impl_test.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/test/unittest/audio_adapter_interface/src/audio_adapter_interface_impl_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -418,6 +418,25 @@ HWTEST_F(AudioAdapterInterfaceImpTest, AdapterLoad_001, TestSize.Level1) HWTEST_F(AudioAdapterInterfaceImpTest, AdapterUnload_001, TestSize.Level1) { EXPECT_EQ(HDF_SUCCESS, AdapterTest_->AdapterUnload()); + + std::string adpterName = "adbcef"; + AudioDeviceDescriptor descSpk; + AudioSampleAttributes attrsSpk; + sptr callbackSpk = new MockIDAudioCallback(); + AdapterTest_->audioRender_ = new AudioRenderInterfaceImpl(adpterName, descSpk, attrsSpk, callbackSpk); + EXPECT_EQ(HDF_ERR_DEVICE_BUSY, AdapterTest_->AdapterUnload()); + + AudioDeviceDescriptor devDescMic; + AudioSampleAttributes attrsMic; + sptr callbackMic = new MockIDAudioCallback(); + AdapterTest_->audioCapture_ = new AudioCaptureInterfaceImpl(adpterName, devDescMic, attrsMic, callbackMic); + EXPECT_EQ(HDF_ERR_DEVICE_BUSY, AdapterTest_->AdapterUnload()); + + AdapterTest_->audioRender_ = nullptr; + EXPECT_EQ(HDF_ERR_DEVICE_BUSY, AdapterTest_->AdapterUnload()); + + AdapterTest_->audioCapture_ = nullptr; + EXPECT_EQ(HDF_SUCCESS, AdapterTest_->AdapterUnload()); } /** diff --git a/hdf_service/distributed_audio/hdi_service/common/include/daudio_errcode.h b/hdf_service/distributed_audio/hdi_service/common/include/daudio_errcode.h index 02b742dad91d46238f40bf0637ed8664f03163fe..c8cce97c4d0b7bdf6d97881935b485c521aac1c8 100644 --- a/hdf_service/distributed_audio/hdi_service/common/include/daudio_errcode.h +++ b/hdf_service/distributed_audio/hdi_service/common/include/daudio_errcode.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -30,7 +30,7 @@ enum DAudioErrorCode { ERR_DH_AUDIO_HDF_OPEN_DEVICE_FAIL = -46007, ERR_DH_AUDIO_HDF_CLOSE_DEVICE_FAIL = -46008, ERR_DH_AUDIO_COMMON_NOT_FOUND_KEY = -46009, - ERR_DH_AUDIO_HDF_FAILURE = -46010, + ERR_DH_AUDIO_HDF_WAIT_TIMEOUT = -46010, }; } // Distributedaudio } // OHOS diff --git a/services/hdfaudioclient/src/distributed_audio_client.cpp b/services/hdfaudioclient/src/distributed_audio_client.cpp index aee37506c77096d62108a5f1e963454d526bf35d..453a9707dc982b7434cdfbf605a1ccad4bdd71de 100644 --- a/services/hdfaudioclient/src/distributed_audio_client.cpp +++ b/services/hdfaudioclient/src/distributed_audio_client.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -230,6 +230,7 @@ static void UnloadAdapterInternal(struct AudioManager *manager, struct AudioAdap break; } } + DHLOGI("Unload adapter success."); } void AudioManagerContext::ClearDescriptors()