diff --git a/bundle.json b/bundle.json index e963b31144610cee38e07f68025ab8fda703eed4..cdde5b0cfc44d1cabddefe2779d2fcd201869ef8 100755 --- a/bundle.json +++ b/bundle.json @@ -70,7 +70,8 @@ "//foundation/distributedhardware/distributed_audio/services/audiotransport/audiochannel/audiodatachannel:distributed_audio_data_channel", "//foundation/distributedhardware/distributed_audio/services/audiotransport/audiotransportstatus:distributed_audio_transport_status", "//foundation/distributedhardware/distributed_audio/services/audiotransport/decodetransport:distributed_audio_decode_transport", - "//foundation/distributedhardware/distributed_audio/services/audiotransport/encodetransport:distributed_audio_encode_transport" + "//foundation/distributedhardware/distributed_audio/services/audiotransport/encodetransport:distributed_audio_encode_transport", + "//foundation/distributedhardware/distributed_audio/services/test_example:audio_distributed_test" ], "inner_kits": [{ "type": "so", diff --git a/services/audioclient/micclient/include/dmic_client.h b/services/audioclient/micclient/include/dmic_client.h index d483980102b49148b062f00b2332ca64d35f3725..6b9536b87cbefe0efad9d8eceb7c62a0e76e3f7a 100644 --- a/services/audioclient/micclient/include/dmic_client.h +++ b/services/audioclient/micclient/include/dmic_client.h @@ -40,9 +40,6 @@ #include "iaudio_event_callback.h" #include "imic_client.h" -#include "v1_0/audio_types.h" -#include "v1_0/iaudio_manager.h" - namespace OHOS { namespace DistributedHardware { class DMicClient : public IAudioDataTransCallback, @@ -65,29 +62,13 @@ public: void SetAttrs(const std::string &devId, const std::shared_ptr &callback) override; int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override; - void AdapterDescFree(struct AudioAdapterDescriptor *dataBlock, bool freeSelf); - void ReleaseAdapterDescs(struct AudioAdapterDescriptor **descs, uint32_t descsLen); - int32_t GetAudioManager(); - int32_t GetAdapter(); - int32_t GetPrimaryDesc(struct AudioAdapterDescriptor *descs, struct AudioAdapterDescriptor - **primaryDesc); - void ReleaseHDFAudioDevice(); - int32_t InitHDFAudioDevice(); - void GenerateAttr(const AudioParam ¶m); - int32_t AudioFwkClientSetUp(); - int32_t HdfClientSetUp(); - int32_t TransSetUp(); - int32_t HdfClientRelease(); - int32_t TransRelease(); - int32_t HdfClientStartCapture(); - void HdfCaptureAudioData(uint32_t lengthPerCapture, const uint32_t lengthPerTrans, - const uint32_t len); - void AudioFwkCaptureData(); - void OnReadData(size_t length) override; private: void CaptureThreadRunning(); + int32_t AudioFwkClientSetUp(); + int32_t TransSetUp(); + void AudioFwkCaptureData(); private: constexpr static uint8_t CHANNEL_WAIT_SECONDS = 5; static constexpr const char* CAPTURETHREAD = "captureThread"; @@ -106,27 +87,6 @@ private: std::shared_ptr micTrans_ = nullptr; int64_t lastCaptureStartTime_ = 0; int64_t lastTransStartTime_ = 0; - - constexpr static uint8_t PORT_ID = 11; - constexpr static size_t FRAME_PER_SECOND = 50; - constexpr static size_t MAX_AUDIO_ADAPTER_DESC = 5; - constexpr static size_t PATH_LEN = 256; - constexpr static size_t INT_32_MAX = 0x7fffffff; - constexpr static size_t BUFFER_PERIOD_SIZE = 4096; - constexpr static size_t AUDIO_BUFFER_SIZE = 16384; - constexpr static size_t FORMATNUM = 2; - static constexpr const char* ADAPTERNAME = "primary"; - static constexpr const char* DEVNAME = "devName"; - - char adapterName_[PATH_LEN] = {0}; - struct IAudioManager *audioManager_ = nullptr; - struct IAudioAdapter *audioAdapter_ = nullptr; - struct IAudioCapture *hdfCapture_ = nullptr; - uint32_t captureId_ = 0; - int64_t frameIndex_ = 0; - bool micInUse_ = false; - struct AudioDeviceDescriptor captureDesc_; - struct AudioSampleAttributes captureAttr_; }; } // DistributedHardware } // OHOS diff --git a/services/audioclient/micclient/src/dmic_client.cpp b/services/audioclient/micclient/src/dmic_client.cpp index 99d89382854ccae115b04067b9bbade19d94f627..348daeeb0120e745d1dcbb342e4313baae4f4883 100644 --- a/services/audioclient/micclient/src/dmic_client.cpp +++ b/services/audioclient/micclient/src/dmic_client.cpp @@ -79,11 +79,10 @@ int32_t DMicClient::OnStateChange(const AudioEventType type) switch (type) { case AudioEventType::DATA_OPENED: { isBlocking_.store(true); - isCaptureReady_.store(true); - if (audioParam_.captureOpts.capturerFlags == MMAP_MODE) { - frameIndex_ = 0; + if (audioParam_.captureOpts.capturerFlags != MMAP_MODE) { + isCaptureReady_.store(true); + captureDataThread_ = std::thread(&DMicClient::CaptureThreadRunning, this); } - captureDataThread_ = std::thread(&DMicClient::CaptureThreadRunning, this); event.type = AudioEventType::MIC_OPENED; break; } @@ -105,164 +104,6 @@ int32_t DMicClient::OnStateChange(const AudioEventType type) return DH_SUCCESS; } -void DMicClient::AdapterDescFree(struct AudioAdapterDescriptor *dataBlock, bool freeSelf) -{ - if (dataBlock == nullptr) { - return; - } - if (dataBlock->adapterName != nullptr) { - free(dataBlock->adapterName); - dataBlock->adapterName = nullptr; - } - if (dataBlock->ports != nullptr) { - free(dataBlock->ports); - dataBlock->ports = nullptr; - } - if (freeSelf) { - free(dataBlock); - dataBlock = nullptr; - } -} - -void DMicClient::ReleaseAdapterDescs(struct AudioAdapterDescriptor **descs, uint32_t descsLen) -{ - if (descsLen > 0 && descs != nullptr && (*descs) != nullptr) { - for (uint32_t i = 0; i < descsLen; i++) { - AdapterDescFree(&(*descs)[i], false); - } - free(*descs); - *descs = nullptr; - } -} - -int32_t DMicClient::GetAudioManager() -{ - audioManager_ = IAudioManagerGet(false); - if (audioManager_ == nullptr) { - DHLOGE("Get audio manager fail"); - return ERR_DH_AUDIO_FAILED; - } - return DH_SUCCESS; -} - -int32_t DMicClient::GetAdapter() -{ - size_t adapterSize = sizeof(struct AudioAdapterDescriptor) * (MAX_AUDIO_ADAPTER_DESC); - struct AudioAdapterDescriptor *descs = reinterpret_cast(malloc(adapterSize)); - if (descs == nullptr) { - DHLOGE("malloc for descs failed"); - return ERR_DH_AUDIO_FAILED; - } - if (memset_s(descs, adapterSize, 0, adapterSize) != EOK) { - DHLOGE("memset for descs failed"); - ReleaseAdapterDescs(&descs, MAX_AUDIO_ADAPTER_DESC); - return ERR_DH_AUDIO_FAILED; - } - uint32_t size = MAX_AUDIO_ADAPTER_DESC; - if (audioManager_ == nullptr) { - DHLOGE("AudioManager is nullptr"); - ReleaseAdapterDescs(&descs, MAX_AUDIO_ADAPTER_DESC); - return ERR_DH_AUDIO_FAILED; - } - int32_t ret = audioManager_->GetAllAdapters(audioManager_, descs, &size); - if (size == 0 || descs == nullptr || ret != 0) { - DHLOGE("Get audio adapters failed. ret : %d.", ret); - ReleaseAdapterDescs(&descs, MAX_AUDIO_ADAPTER_DESC); - return ERR_DH_AUDIO_FAILED; - } - struct AudioAdapterDescriptor *primaryDesc = nullptr; - if (GetPrimaryDesc(descs, &primaryDesc) != DH_SUCCESS) { - return ERR_DH_AUDIO_FAILED; - } - ret = audioManager_->LoadAdapter(audioManager_, primaryDesc, &audioAdapter_); - if (ret != DH_SUCCESS || audioAdapter_ == nullptr) { - DHLOGE("Load primary adapter failed."); - ReleaseAdapterDescs(&descs, MAX_AUDIO_ADAPTER_DESC); - return ERR_DH_AUDIO_FAILED; - } - ReleaseAdapterDescs(&descs, MAX_AUDIO_ADAPTER_DESC); - (void)audioAdapter_->InitAllPorts(audioAdapter_); - return DH_SUCCESS; -} - -int32_t DMicClient::GetPrimaryDesc(struct AudioAdapterDescriptor *descs, struct AudioAdapterDescriptor - **primaryDesc) -{ - uint32_t size = MAX_AUDIO_ADAPTER_DESC; - for (uint32_t index = 0; index < size; index++) { - auto desc = &descs[index]; - if (desc == nullptr || desc->adapterName == nullptr) { - continue; - } - if (!strcmp(desc->adapterName, ADAPTERNAME)) { - *primaryDesc = desc; - break; - } - } - if (*primaryDesc == nullptr) { - DHLOGE("Find primary adapter failed."); - ReleaseAdapterDescs(&descs, MAX_AUDIO_ADAPTER_DESC); - return ERR_DH_AUDIO_FAILED; - } - if (strcpy_s(adapterName_, PATH_LEN, (*primaryDesc)->adapterName) < 0) { - DHLOGE("Strcpy adapter name failed."); - ReleaseAdapterDescs(&descs, MAX_AUDIO_ADAPTER_DESC); - return ERR_DH_AUDIO_FAILED; - } - return DH_SUCCESS; -} - -void DMicClient::ReleaseHDFAudioDevice() -{ - if (micInUse_) { - return; - } - if (audioManager_ != nullptr && audioManager_->UnloadAdapter != nullptr) { - audioManager_->UnloadAdapter(audioManager_, adapterName_); - IAudioAdapterRelease(audioAdapter_, false); - audioAdapter_ = nullptr; - IAudioManagerRelease(audioManager_, false); - audioManager_ = nullptr; - } -} - -int32_t DMicClient::InitHDFAudioDevice() -{ - if (micInUse_) { - return DH_SUCCESS; - } - if (GetAudioManager() != DH_SUCCESS) { - DHLOGE("Get audio manager failed"); - ReleaseHDFAudioDevice(); - return ERR_DH_AUDIO_FAILED; - } - - if (GetAdapter() != DH_SUCCESS) { - DHLOGE("Get audio adapter failed"); - ReleaseHDFAudioDevice(); - return ERR_DH_AUDIO_FAILED; - } - return DH_SUCCESS; -} - -void DMicClient::GenerateAttr(const AudioParam ¶m) -{ - captureDesc_.portId = PORT_ID; - captureDesc_.pins = AudioPortPin::PIN_IN_MIC; - captureDesc_.desc = strdup(DEVNAME); - captureAttr_.format = AUDIO_FORMAT_TYPE_PCM_16_BIT; - captureAttr_.interleaved = 0; - captureAttr_.type = AUDIO_IN_MEDIA; - captureAttr_.period = BUFFER_PERIOD_SIZE; - captureAttr_.isBigEndian = false; - captureAttr_.isSignedData = true; - captureAttr_.stopThreshold = INT_32_MAX; - captureAttr_.silenceThreshold = AUDIO_BUFFER_SIZE; - captureAttr_.channelCount = param.comParam.channelMask; - captureAttr_.sampleRate = param.comParam.sampleRate; - captureAttr_.sourceType = 1; -} - int32_t DMicClient::AudioFwkClientSetUp() { AudioStandard::AudioCapturerOptions capturerOptions = { @@ -274,7 +115,7 @@ int32_t DMicClient::AudioFwkClientSetUp() }, { static_cast(audioParam_.captureOpts.sourceType), - 0, + audioParam_.captureOpts.capturerFlags == MMAP_MODE ? AudioStandard::STREAM_FLAG_FAST : 0, } }; std::lock_guard lck(devMtx_); @@ -293,29 +134,6 @@ int32_t DMicClient::AudioFwkClientSetUp() return TransSetUp(); } -int32_t DMicClient::HdfClientSetUp() -{ - if (InitHDFAudioDevice() != DH_SUCCESS) { - DHLOGE("Init hdf audio device failed."); - return ERR_DH_AUDIO_FAILED; - } - GenerateAttr(audioParam_); - - if (audioAdapter_ == nullptr) { - DHLOGE("audio adapter is null"); - return ERR_DH_AUDIO_FAILED; - } - int32_t ret = audioAdapter_->CreateCapture(audioAdapter_, &captureDesc_, &captureAttr_, - &hdfCapture_, &captureId_); - if (ret != DH_SUCCESS || hdfCapture_ == nullptr) { - DHLOGE("CreateCapture failed, ret: %d.", ret); - return ERR_DH_AUDIO_FAILED; - } - micInUse_ = true; - DHLOGI("Create hdf audio capture success"); - return TransSetUp(); -} - int32_t DMicClient::TransSetUp() { if (micTrans_ == nullptr) { @@ -331,71 +149,6 @@ int32_t DMicClient::TransSetUp() return DH_SUCCESS; } -int32_t DMicClient::HdfClientRelease() -{ - DHLOGI("Release hdf mic client."); - if (audioAdapter_ != nullptr) { - audioAdapter_->DestroyCapture(audioAdapter_, captureId_); - } - IAudioCaptureRelease(hdfCapture_, false); - hdfCapture_ = nullptr; - micInUse_ = false; - ReleaseHDFAudioDevice(); - DHLOGI("Release hdf audio capture success."); - return TransRelease(); -} - -int32_t DMicClient::TransRelease() -{ - if (micTrans_ == nullptr) { - DHLOGE("Mic trans is nullptr."); - return ERR_DH_AUDIO_FAILED; - } - if (micTrans_->Release() != DH_SUCCESS) { - DHLOGE("Mic trans release failed."); - } - micTrans_ = nullptr; - clientStatus_ = AudioStatus::STATUS_IDLE; - return DH_SUCCESS; -} - -int32_t DMicClient::HdfClientStartCapture() -{ - if (hdfCapture_ == nullptr) { - DHLOGE("Audio capturer is nullptr, can not start."); - return ERR_DH_AUDIO_FAILED; - } - hdfCapture_->Start(hdfCapture_); - DHLOGI("Start hdf capture success."); - return DH_SUCCESS; -} - -void DMicClient::HdfCaptureAudioData(uint32_t lengthPerCapture, const uint32_t lengthPerTrans, - const uint32_t len) -{ - auto audioData = std::make_shared(lengthPerCapture); - uint64_t size = 0; - if (hdfCapture_ == nullptr) { - DHLOGE("hdf capture is nullptr."); - return; - } - hdfCapture_->CaptureFrame(hdfCapture_, reinterpret_cast(audioData->Data()), - &lengthPerCapture, &size); - DHLOGD("CaptureFrame success, framesize: %d", lengthPerCapture); - - for (uint32_t i = 0; i < len; i++) { - std::shared_ptr data = std::make_shared(lengthPerTrans); - if (memcpy_s(data->Data(), lengthPerTrans, audioData->Data() + lengthPerTrans * i, - lengthPerTrans) != EOK) { - DHLOGE("Copy audio data %d failed.", i); - } - int32_t ret = micTrans_->FeedAudioData(data); - if (ret != DH_SUCCESS) { - DHLOGE("Failed to send data %d.", i); - } - } -} - int32_t DMicClient::SetUp(const AudioParam ¶m) { DHLOGI("Set up mic client, param: {sampleRate: %d, bitFormat: %d," + @@ -403,9 +156,6 @@ int32_t DMicClient::SetUp(const AudioParam ¶m) param.comParam.sampleRate, param.comParam.bitFormat, param.comParam.channelMask, param.captureOpts.sourceType, param.captureOpts.capturerFlags, param.comParam.frameSize); audioParam_ = param; - if (audioParam_.captureOpts.capturerFlags == MMAP_MODE) { - return HdfClientSetUp(); - } return AudioFwkClientSetUp(); } @@ -434,13 +184,22 @@ int32_t DMicClient::Release() DHLOGE("Mic status is wrong or mic trans is null, %d.", (int32_t)clientStatus_); return ERR_DH_AUDIO_SA_STATUS_ERR; } - if (audioParam_.captureOpts.capturerFlags == MMAP_MODE) { - return HdfClientRelease(); - } + bool isReleaseError = false; if (audioCapturer_ == nullptr || !audioCapturer_->Release()) { DHLOGE("Audio capturer release failed."); + isReleaseError = true; + } + int32_t ret = micTrans_->Release(); + if (ret != DH_SUCCESS) { + DHLOGE("Mic trans release failed."); + isReleaseError = true; + } + micTrans_ = nullptr; + clientStatus_ = AudioStatus::STATUS_IDLE; + if (isReleaseError) { + return ERR_DH_AUDIO_FAILED; } - return TransRelease(); + return DH_SUCCESS; } int32_t DMicClient::StartCapture() @@ -453,20 +212,16 @@ int32_t DMicClient::StartCapture() "daudio init failed or mic status wrong."); return ERR_DH_AUDIO_SA_STATUS_ERR; } - if (audioParam_.captureOpts.capturerFlags == MMAP_MODE) { - HdfClientStartCapture(); - } else { - if (audioCapturer_ == nullptr) { - DHLOGE("audio capturer is nullptr."); - return ERR_DH_AUDIO_CLIENT_CAPTURER_START_FAILED; - } - if (!audioCapturer_->Start()) { - DHLOGE("Audio capturer start failed."); - audioCapturer_->Release(); - DAudioHisysevent::GetInstance().SysEventWriteFault(DAUDIO_OPT_FAIL, - ERR_DH_AUDIO_CLIENT_CAPTURER_START_FAILED, "daudio capturer start failed."); - return ERR_DH_AUDIO_CLIENT_CAPTURER_START_FAILED; - } + if (audioCapturer_ == nullptr) { + DHLOGE("audio capturer is nullptr."); + return ERR_DH_AUDIO_CLIENT_CAPTURER_START_FAILED; + } + if (!audioCapturer_->Start()) { + DHLOGE("Audio capturer start failed."); + audioCapturer_->Release(); + DAudioHisysevent::GetInstance().SysEventWriteFault(DAUDIO_OPT_FAIL, + ERR_DH_AUDIO_CLIENT_CAPTURER_START_FAILED, "daudio capturer start failed."); + return ERR_DH_AUDIO_CLIENT_CAPTURER_START_FAILED; } int32_t ret = micTrans_->Start(); if (ret != DH_SUCCESS) { @@ -533,23 +288,8 @@ void DMicClient::CaptureThreadRunning() if (pthread_setname_np(pthread_self(), CAPTURETHREAD) != DH_SUCCESS) { DHLOGE("Capture data thread setname failed."); } - uint32_t lengthPerCapture; - uint32_t lengthPerTrans; - uint32_t len; - if (audioParam_.captureOpts.capturerFlags == MMAP_MODE) { - lengthPerCapture = (audioParam_.comParam.sampleRate * audioParam_.comParam.bitFormat * - FORMATNUM * audioParam_.comParam.channelMask) / FRAME_PER_SECOND; - lengthPerTrans = audioParam_.comParam.frameSize; - len = lengthPerCapture / lengthPerTrans; - } while (isCaptureReady_.load()) { - if (audioParam_.captureOpts.capturerFlags == MMAP_MODE) { - DHLOGD("Capture frameIndex: %lld.", frameIndex_); - HdfCaptureAudioData(lengthPerCapture, lengthPerTrans, len); - ++frameIndex_; - } else { - AudioFwkCaptureData(); - } + AudioFwkCaptureData(); } } @@ -607,9 +347,11 @@ int32_t DMicClient::StopCapture() } isBlocking_.store(false); - isCaptureReady_.store(false); - if (captureDataThread_.joinable()) { - captureDataThread_.join(); + if (audioParam_.captureOpts.capturerFlags != MMAP_MODE) { + isCaptureReady_.store(false); + if (captureDataThread_.joinable()) { + captureDataThread_.join(); + } } bool status = true; @@ -618,15 +360,9 @@ int32_t DMicClient::StopCapture() DHLOGE("Mic trans stop failed."); status = false; } - if (audioParam_.captureOpts.capturerFlags == MMAP_MODE) { - if (hdfCapture_ != nullptr) { - hdfCapture_->Stop(hdfCapture_); - } - } else { - if (audioCapturer_ == nullptr || !audioCapturer_->Stop()) { - DHLOGE("Audio capturer stop failed."); - status = false; - } + if (audioCapturer_ == nullptr || !audioCapturer_->Stop()) { + DHLOGE("Audio capturer stop failed."); + status = false; } clientStatus_ = AudioStatus::STATUS_STOP; if (!status) { diff --git a/services/audioclient/test/unittest/micclient/BUILD.gn b/services/audioclient/test/unittest/micclient/BUILD.gn index 9d0c2f90a0aea2c6a26065da2eb57a7074b75def..aeec741c8fb5ba1f644588a8cbeed5c6372e33bd 100644 --- a/services/audioclient/test/unittest/micclient/BUILD.gn +++ b/services/audioclient/test/unittest/micclient/BUILD.gn @@ -67,7 +67,6 @@ ohos_unittest("MicClientTest") { "audio_framework:audio_capturer", "distributed_hardware_fwk:distributed_av_receiver", "distributed_hardware_fwk:distributed_av_sender", - "drivers_interface_audio:libaudio_proxy_1.0", ] } diff --git a/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp b/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp index f844ca24048cbd029b1fef1a24eba16c69e8e81b..1b32590c359ffe73263862bcc17f91a1e6064f5a 100644 --- a/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp +++ b/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp @@ -210,36 +210,6 @@ HWTEST_F(DMicClientTest, SendMessage_001, TestSize.Level1) EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, micClient_->SendMessage(NOTIFY_OPEN_MIC_RESULT, content, dstDevId)); } -/** - * @tc.name: GetAdapter_001 - * @tc.desc: Verify the GetAdapter function. - * @tc.type: FUNC - * @tc.require: AR000H0E6G - */ -HWTEST_F(DMicClientTest, GetAdapter_001, TestSize.Level1) -{ - struct IAudioManager *audioManager_; - audioManager_ = nullptr; - int32_t actual = micClient_->GetAdapter(); - EXPECT_EQ(ERR_DH_AUDIO_FAILED, actual); -} - -/** - * @tc.name: InitHDFAudioDevice_001 - * @tc.desc: Verify the InitHDFAudioDevice function. - * @tc.type: FUNC - * @tc.require: AR000H0E6G - */ -HWTEST_F(DMicClientTest, InitHDFAudioDevice_001, TestSize.Level1) -{ - bool micInUse_ = true; - int32_t actual = micClient_->InitHDFAudioDevice(); - EXPECT_EQ(DH_SUCCESS, actual); - micInUse_ = false; - actual = micClient_->InitHDFAudioDevice(); - EXPECT_EQ(DH_SUCCESS, actual); -} - /** * @tc.name: AudioFwkClientSetUp_001 * @tc.desc: Verify the AudioFwkClientSetUp function. @@ -256,24 +226,6 @@ HWTEST_F(DMicClientTest, AudioFwkClientSetUp_001, TestSize.Level1) EXPECT_NE(DH_SUCCESS, actual); } -/** - * @tc.name: HdfClientSetUp_001 - * @tc.desc: Verify the HdfClientSetUp function. - * @tc.type: FUNC - * @tc.require: AR000H0E6G - */ -HWTEST_F(DMicClientTest, HdfClientSetUp_001, TestSize.Level1) -{ - bool micInUse_; - micInUse_ = true; - int32_t actual = micClient_->HdfClientSetUp(); - EXPECT_EQ(DH_SUCCESS, actual); - micInUse_ = false; - micClient_->micTrans_ = nullptr; - actual = micClient_->HdfClientSetUp(); - EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, actual); -} - /** * @tc.name: TransSetUp_001 * @tc.desc: Verify the TransSetUp function. @@ -288,18 +240,5 @@ HWTEST_F(DMicClientTest, TransSetUp_001, TestSize.Level1) actual = micClient_->TransSetUp(); EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, actual); } - -/** - * @tc.name: HdfClientRelease_001 - * @tc.desc: Verify the HdfClientRelease function. - * @tc.type: FUNC - * @tc.require: AR000H0E6G - */ -HWTEST_F(DMicClientTest, HdfClientRelease_001, TestSize.Level1) -{ - std::shared_ptr audioAdapter_ = std::make_shared(); - int32_t actual = micClient_->HdfClientRelease(); - EXPECT_EQ(DH_SUCCESS, actual); -} } // DistributedHardware } // OHOS diff --git a/services/test_example/BUILD.gn b/services/test_example/BUILD.gn index 6035c81cf2fb6347acb86657dab50e0ea0f4345b..c9a35f263e5a69394acddb6689c5f1982258bf32 100644 --- a/services/test_example/BUILD.gn +++ b/services/test_example/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") +import("../../distributedaudio.gni") ohos_executable("audio_distributed_test") { include_dirs = [ diff --git a/services/test_example/distributedaudiotest.cpp b/services/test_example/distributedaudiotest.cpp index a527fcc1447017ec9c8f9363e23d662ab8b8af84..cf5dfbac655787d32b4546e99df19b08e779f6fe 100644 --- a/services/test_example/distributedaudiotest.cpp +++ b/services/test_example/distributedaudiotest.cpp @@ -60,9 +60,7 @@ constexpr int32_t RENDER_CHANNEL_MASK = 2; constexpr int32_t CAPTURE_INTER_LEAVED = 1; constexpr int32_t CAPTURE_STREAM_ID = 2; constexpr int32_t CAPTURE_CHANNEL_MASK = 2; -constexpr int32_t MILLISECOND_PER_SECOND = 1000; constexpr int64_t AUDIO_FRAME_TIME_INTERFAL_DEFAULT = 21333; -constexpr int32_t CMD_EXECUTING_RETURN_LENGHT_MAX = 500; static AudioManager *g_manager = nullptr; static AudioAdapter *g_adapter = nullptr; @@ -82,15 +80,15 @@ int32_t g_micFrameNum = 0; bool g_isInitRenderData = false; static std::vector renderData; -static DeviceStatus g_spkStatus = DEVICE_IDLE; -static DeviceStatus g_micStatus = DEVICE_IDLE; +static DeviceStatus g_spkStatus = DeviceStatus::DEVICE_IDLE; +static DeviceStatus g_micStatus = DeviceStatus::DEVICE_IDLE; static std::thread g_playingThread; static std::thread g_capingThread; FILE *g_micFile = nullptr; -static std::string CloseSpk(); -static std::string CloseMic(); +static void CloseSpk(); +static void CloseMic(); static int64_t GetNowTimeUs() { @@ -159,11 +157,11 @@ static int32_t InitTestDemo() static void HandleDevError(const char *condition, const char *value) { - if (condition[TYPE_OFFSET] == DEV_TYPE_SPK && g_spkStatus != DEVICE_IDLE) { + if (condition[TYPE_OFFSET] == DEV_TYPE_SPK && g_spkStatus != DeviceStatus::DEVICE_IDLE) { CloseSpk(); } - if (condition[TYPE_OFFSET] == DEV_TYPE_MIC && g_micStatus == DEVICE_IDLE) { + if (condition[TYPE_OFFSET] == DEV_TYPE_MIC && g_micStatus == DeviceStatus::DEVICE_IDLE) { CloseMic(); } @@ -219,7 +217,7 @@ static int32_t LoadSpkDev(const std::string &devId) static void OpenSpk(const std::string &devId) { - if (g_spkStatus != DEVICE_IDLE) { + if (g_spkStatus != DeviceStatus::DEVICE_IDLE) { std::cout << "Speaker device is already opened." << std::endl; return; } @@ -249,7 +247,7 @@ static void OpenSpk(const std::string &devId) std::cout << "Open SPK device failed, ret: " << ret << std::endl; return; } - g_spkStatus = DEVICE_OPEN; + g_spkStatus = DeviceStatus::DEVICE_OPEN; std::cout << "Open SPK device success." << std::endl; } @@ -276,10 +274,10 @@ static void Play() } std::cout << "Playing thread started." << std::endl; g_render->control.Start((AudioHandle)g_render); - g_spkStatus = DEVICE_START; + g_spkStatus = DeviceStatus::DEVICE_START; uint64_t size = 0; - while (g_spkStatus == DEVICE_START) { + while (g_spkStatus == DeviceStatus::DEVICE_START) { int64_t startTime = GetNowTimeUs(); int32_t ret = g_render->RenderFrame(g_render, renderData[g_frameIndex], RENDER_FRAME_SIZE, &size); if (ret != DH_SUCCESS) { @@ -296,12 +294,12 @@ static void Play() static void StartRender() { - if (g_spkStatus == DEVICE_IDLE) { + if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Speaker device is not opened, start render failed." << std::endl; return; } - if (g_spkStatus == DEVICE_OPEN) { + if (g_spkStatus == DeviceStatus::DEVICE_OPEN) { WavHdr wavHeader; size_t headerSize = sizeof(WavHdr); if (!g_isInitRenderData) { @@ -326,11 +324,11 @@ static void StartRender() g_playingThread = std::thread(Play); return; } - if (g_spkStatus == DEVICE_START) { + if (g_spkStatus == DeviceStatus::DEVICE_START) { std::cout << "Speaker device is started." << std::endl; return; } - if (g_spkStatus == DEVICE_STOP) { + if (g_spkStatus == DeviceStatus::DEVICE_STOP) { g_playingThread = std::thread(Play); } } @@ -342,22 +340,22 @@ static void StopRender() return; } - if (g_spkStatus == DEVICE_IDLE) { + if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Speaker device is not opened." << std::endl; return; } - if (g_spkStatus == DEVICE_OPEN) { + if (g_spkStatus == DeviceStatus::DEVICE_OPEN) { std::cout << "Speaker device is not started." << std::endl; return; } - if (g_spkStatus == DEVICE_STOP) { + if (g_spkStatus == DeviceStatus::DEVICE_STOP) { std::cout << "Speaker device is already stoped." << std::endl; return; } - g_spkStatus = DEVICE_STOP; + g_spkStatus = DeviceStatus::DEVICE_STOP; if (g_playingThread.joinable()) { g_playingThread.join(); } @@ -366,12 +364,12 @@ static void StopRender() static void CloseSpk() { - if (g_spkStatus == DEVICE_IDLE) { + if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Speaker device is not opened." << std::endl; return; } - if (g_spkStatus == DEVICE_START) { + if (g_spkStatus == DeviceStatus::DEVICE_START) { StopRender(); } @@ -380,11 +378,11 @@ static void CloseSpk() std::cout << "Close speaker failed" << std::endl; return; } - if (g_micStatus == DEVICE_IDLE) { + if (g_micStatus == DeviceStatus::DEVICE_IDLE) { g_manager->UnloadAdapter(g_manager, g_adapter); g_adapter = nullptr; } - g_spkStatus = DEVICE_IDLE; + g_spkStatus = DeviceStatus::DEVICE_IDLE; if (g_isInitRenderData) { for (auto &p : renderData) { @@ -426,7 +424,7 @@ static int32_t LoadMicDev(const std::string &devId) static void OpenMic(const std::string &devId) { - if (g_micStatus != DEVICE_IDLE) { + if (g_micStatus != DeviceStatus::DEVICE_IDLE) { std::cout << "Mic device is already opened." << std::endl; return; } @@ -450,7 +448,7 @@ static void OpenMic(const std::string &devId) std::cout << "Open MIC device failed." << std::endl; return; } - g_micStatus = DEVICE_OPEN; + g_micStatus = DeviceStatus::DEVICE_OPEN; std::cout << "Open MIC device success." << std::endl; } @@ -477,10 +475,10 @@ static void Capture() } std::cout << "Capturing thread started." << std::endl; g_capture->control.Start((AudioHandle)g_capture); - g_micStatus = DEVICE_START; + g_micStatus = DeviceStatus::DEVICE_START; uint64_t size = 0; - while (g_micStatus == DEVICE_START) { + while (g_micStatus == DeviceStatus::DEVICE_START) { uint8_t *data[RENDER_FRAME_SIZE]; int64_t startTime = GetNowTimeUs(); int32_t ret = g_capture->CaptureFrame(g_capture, data, RENDER_FRAME_SIZE, &size); @@ -500,12 +498,12 @@ static void Capture() static void StartCapture() { - if (g_micStatus == DEVICE_IDLE) { + if (g_micStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Mic device is not opened, start capture failed." << std::endl; return; } - if (g_micStatus == DEVICE_OPEN) { + if (g_micStatus == DeviceStatus::DEVICE_OPEN) { g_micFile = fopen(MIC_FILE_PATH, "ab+"); if (g_micFile == nullptr) { std::cout << "Open pcm file failed." << std::endl; @@ -515,12 +513,12 @@ static void StartCapture() return; } - if (g_micStatus == DEVICE_START) { + if (g_micStatus == DeviceStatus::DEVICE_START) { std::cout << "Mic device is already started." << std::endl; return; } - if (g_micStatus == DEVICE_STOP) { + if (g_micStatus == DeviceStatus::DEVICE_STOP) { g_capingThread = std::thread(Capture); } } @@ -531,19 +529,19 @@ static void StopCapture() std::cout << "MIC device is null." << std::endl; return; } - if (g_micStatus == DEVICE_IDLE) { + if (g_micStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Mic device is not opened." << std::endl; return; } - if (g_micStatus == DEVICE_OPEN) { + if (g_micStatus == DeviceStatus::DEVICE_OPEN) { std::cout << "Mic device is not started." << std::endl; return; } - if (g_micStatus == DEVICE_STOP) { + if (g_micStatus == DeviceStatus::DEVICE_STOP) { std::cout << "Mic device is already started." << std::endl; return; } - g_micStatus = DEVICE_STOP; + g_micStatus = DeviceStatus::DEVICE_STOP; if (g_capingThread.joinable()) { g_capingThread.join(); } @@ -552,12 +550,12 @@ static void StopCapture() static void CloseMic() { - if (g_micStatus == DEVICE_IDLE) { + if (g_micStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Mic device is not opened." << std::endl; return; } - if (g_micStatus == DEVICE_START) { + if (g_micStatus == DeviceStatus::DEVICE_START) { StopCapture(); } @@ -566,7 +564,7 @@ static void CloseMic() std::cout << "Close mic failed." << std::endl; return; } - if (g_spkStatus == DEVICE_IDLE) { + if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { g_manager->UnloadAdapter(g_manager, g_adapter); g_adapter = nullptr; } @@ -574,13 +572,13 @@ static void CloseMic() fclose(g_micFile); g_micFile = nullptr; } - g_micStatus = DEVICE_IDLE; + g_micStatus = DeviceStatus::DEVICE_IDLE; std::cout << "Close MIC device success." << std::endl; } static void SetVolume() { - if (g_spkStatus == DEVICE_IDLE) { + if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Speaker is not opened, can not set volume." << std::endl; return; } @@ -593,7 +591,8 @@ static void SetVolume() std::cout << "Set volume: " << volInt << std::endl; AudioExtParamKey key = AudioExtParamKey::AUDIO_EXT_PARAM_KEY_VOLUME; std::string condition = "EVENT_TYPE=1;VOLUME_GROUP_ID=1;AUDIO_VOLUME_TYPE=1;"; - int32_t ret = g_adapter->SetExtraParams(g_adapter, key, condition.c_str(), vol.c_str()); + std::string volStr = std::to_string(volInt); + int32_t ret = g_adapter->SetExtraParams(g_adapter, key, condition.c_str(), volStr.c_str()); if (ret != DH_SUCCESS) { std::cout << "Set volume failed" << std::endl; } @@ -601,7 +600,7 @@ static void SetVolume() static void GetVolume() { - if (g_spkStatus == DEVICE_IDLE) { + if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Speaker is not opened, can not get volume." << std::endl; return; }