From 33d79087fb6802256ce15322a1aa55eb84081ae9 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Thu, 20 Jul 2023 19:01:36 +0800 Subject: [PATCH 1/8] add DCTS Signed-off-by: w30042960 --- .../distributedaudiotest/Test.json | 22 + .../daudio_automat_test.cpp | 265 ++++++++ .../distributed_audio_test.cpp | 565 ++++++++++++++++++ .../distributed_audio_test.h | 116 ++++ 4 files changed, 968 insertions(+) create mode 100644 hdf_service/distributed_audio/distributedaudiotest/Test.json create mode 100644 hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp create mode 100644 hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp create mode 100644 hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h diff --git a/hdf_service/distributed_audio/distributedaudiotest/Test.json b/hdf_service/distributed_audio/distributedaudiotest/Test.json new file mode 100644 index 00000000..a8dea064 --- /dev/null +++ b/hdf_service/distributed_audio/distributedaudiotest/Test.json @@ -0,0 +1,22 @@ +{ + "description": "Config for disCamera test cases", + "driver": { + "module-name": "DctsdisAudioTest", + "native-test-timeout": "120000", + "native-test-device-path": "/data/local/tmp", + "runtime-hint": "1s", + "type": "CppTest" + }, + "kits": [ + { + "post-push" : [ + "chmod -R 777 /data/local/tmp/*" + ], + "push": [ + "DctsdisAudioTest->/data/local/tmp/DctsdisAudioTest" + ], + "type": "PushKit" + } + ] +} + diff --git a/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp b/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp new file mode 100644 index 00000000..2769cbbd --- /dev/null +++ b/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp @@ -0,0 +1,265 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "distributed_audio_test.h" + +#include +#include +#include + +using namespace testing::ext; +using namespace OHOS::DistributedHardware; + +constexpr int32_t DAUDIO_OK = 0; +constexpr int32_t DAUDIO_DALAY_TIME1 = 3; +constexpr int32_t DAUDIO_DALAY_TIME2 = 10; +constexpr int32_t RUN_TIMES = 20; +constexpr int32_t MAX_VOLUME = 15; + +class DAudioAutomatTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + DAudioAutomatTest(); +}; + +void DAudioAutomatTest::SetUpTestCase(void) +{ + if (InitTestDemo() != DAUDIO_OK) { + std::cout <<"demo test:InitTestDemo error" << std::endl; + return; + } + + if (FindAudioDevice() != DAUDIO_OK) { + return; + } +} +void DAudioAutomatTest::TearDownTestCase(void) {} +void DAudioAutomatTest::SetUp(void) {} +void DAudioAutomatTest::TearDown(void) {} +DAudioAutomatTest::DAudioAutomatTest(void) {} + + +/** + * @tc.number : DAudioTest_001 + * @tc.name : open、close speaker test + * @tc.desc : open、close speaker + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_001, TestSize.Level1) +{ + std::cout << "start run DAudioTest_001." << std::endl; + std::string retStr = "false"; + retStr = OpenSpk(); + EXPECT_EQ("true", retStr); + retStr = CloseSpk(); + EXPECT_EQ("true", retStr); +} + +/** + * @tc.number : DAudioTest_002 + * @tc.name : open、close speaker test + * @tc.desc : open、close speaker 20 times + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_002, TestSize.Level1) +{ + std::cout << "start run DAudioTest_002." << std::endl; + for (int32_t i = 0; i < RUN_TIMES; ++i) { + std::string retStr = "false"; + retStr = OpenSpk(); + EXPECT_EQ("true", retStr); + retStr = CloseSpk(); + EXPECT_EQ("true", retStr); + } +} + +/** + * @tc.number : DAudioTest_003 + * @tc.name : open、close mic test + * @tc.desc : open、close mic + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_003, TestSize.Level1) +{ + std::cout << "start run DAudioTest_003." << std::endl; + std::string retStr = "false"; + retStr = OpenMic(); + EXPECT_EQ("true", retStr); + retStr = CloseMic(); + EXPECT_EQ("true", retStr); +} + +/** + * @tc.number : DAudioTest_004 + * @tc.name : open、close mic test + * @tc.desc : open、close mic 20 times + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_004, TestSize.Level1) +{ + std::cout << "start run DAudioTest_004." << std::endl; + for (int32_t i = 0; i < RUN_TIMES; ++i) { + std::string retStr = "false"; + retStr = OpenMic(); + EXPECT_EQ("true", retStr); + retStr = CloseMic(); + EXPECT_EQ("true", retStr); + } +} + +/** + * @tc.number : DAudioTest_005 + * @tc.name : start、stop render test + * @tc.desc : start、stop render + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_005, TestSize.Level1) +{ + std::cout << "start run DAudioTest_005." << std::endl; + std::string retStr = "false"; + retStr = OpenSpk(); + EXPECT_EQ("true", retStr); + retStr = StartRender(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME2); + retStr = StopRender(); + EXPECT_EQ("true", retStr); + retStr = CloseSpk(); + EXPECT_EQ("true", retStr); +} + +/** + * @tc.number : DAudioTest_006 + * @tc.name : start、stop Capture test + * @tc.desc : start、stop Capture + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_006, TestSize.Level1) +{ + std::cout << "start run DAudioTest_006." << std::endl; + std::string retStr = "false"; + retStr = OpenMic(); + EXPECT_EQ("true", retStr); + retStr = StartCapture(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME2); + retStr = StopCapture(); + EXPECT_EQ("true", retStr); + retStr = CloseMic(); + EXPECT_EQ("true", retStr); +} + +/** + * @tc.number : DAudioTest_007 + * @tc.name : setvolume、getvolume test + * @tc.desc : setvolume、getvolume render 20 times + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_007, TestSize.Level1) +{ + std::cout << "start run DAudioTest_007." << std::endl; + std::string retStr = "false"; + retStr = OpenSpk(); + EXPECT_EQ("true", retStr); + retStr = StartRender(); + EXPECT_EQ("true", retStr); + std::srand((unsigned int)time(nullptr)); + for (int i = 0; i < RUN_TIMES; ++i) { + int volume = rand() % (MAX_VOLUME + 1); + retStr = std::to_string(volume); + retStr = SetVolume(retStr); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME2); + retStr = GetVolume(); + EXPECT_EQ("true", retStr); + } + retStr = StopRender(); + EXPECT_EQ("true", retStr); + retStr = CloseSpk(); + EXPECT_EQ("true", retStr); +} + +/** + * @tc.number : DAudioTest_008 + * @tc.name : startcapture、stopcapture test + * @tc.desc : startcapture、stopcapture render + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_008, TestSize.Level1) +{ + std::cout << "start run DAudioTest_008." << std::endl; + std::string retStr = "false"; + retStr = OpenSpk(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME1); + retStr = OpenMic(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME1); + retStr = StartRender(); + EXPECT_EQ("true", retStr); + retStr = StartCapture(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME2); + retStr = StopCapture(); + EXPECT_EQ("true", retStr); + retStr = StopRender(); + EXPECT_EQ("true", retStr); + retStr = CloseSpk(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME1); + retStr = CloseMic(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME1); +} + +/** + * @tc.number : DAudioTest_009 + * @tc.name : start、stop render test + * @tc.desc : start、stop render 20 times + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_009, TestSize.Level1) +{ + std::cout << "start run DAudioTest_009." << std::endl; + std::string retStr = "false"; + retStr = OpenSpk(); + for (int32_t i = 0; i < RUN_TIMES; ++i) { + retStr = StartRender(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME2); + retStr = StopRender(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME1); + } + retStr = CloseSpk(); + EXPECT_EQ("true", retStr); +} + +/** + * @tc.number : DAudioTest_010 + * @tc.name : start、stop Capture test + * @tc.desc : start、stop Capture + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_010, TestSize.Level1) +{ + std::cout << "start run DAudioTest_010." << std::endl; + std::string retStr = "false"; + retStr = OpenMic(); + for (int32_t i = 0; i < RUN_TIMES; ++i) { + retStr = StartCapture(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME2); + retStr = StopCapture(); + EXPECT_EQ("true", retStr); + sleep(DAUDIO_DALAY_TIME1); + } + retStr = CloseMic(); + EXPECT_EQ("true", retStr); +} \ No newline at end of file diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp new file mode 100644 index 00000000..a023da68 --- /dev/null +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp @@ -0,0 +1,565 @@ + +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include "unistd.h" +#include "./distributed_audio_test.h" + +std::string deviceId; + +struct Request { + pid_t pid; + int commandNum; + char reqString[70]; + int seqLen; +}; + +struct Response { + int seqNum; + char resString[CMD_EXECUTING_RETURN_LENGHT_MAX]; +}; + +using namespace OHOS::DistributedHardware; +static AudioManager *g_manager = nullptr; +static AudioAdapter *g_adapter = nullptr; +static AudioRender *g_render = nullptr; +static AudioCapture *g_capture = nullptr; +static AudioAdapterDescriptor *g_devices = nullptr; + +static constexpr const char* PLAY_THREAD = "playThread"; +static constexpr const char* CAPTURE_THREAD = "captureThread"; + +int32_t g_deviceNum = 0; +int32_t g_frameNum = 0; +int32_t g_frameIndex = 0; +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 std::thread g_palyingThread; +static std::thread g_capingThread; +FILE *g_micFile = nullptr; + +static int64_t GetNowTimeUs() +{ + std::chrono::microseconds nowUs = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); + return nowUs.count(); +} + +int32_t InitTestDemo() +{ + DHLOGI("**********************************************************************************"); + DHLOGI("Distributed Audio Test Demo Bin v1.3."); + DHLOGI("**********************************************************************************"); + DHLOGI("Init distributed audio hdf service."); + g_manager = GetAudioManagerFuncs(); + if (g_manager == nullptr) { + DHLOGI("Distributed audio manager is null, quit!"); + return ERR_DH_AUDIO_HDF_FAIL; + } + DHLOGI("Load audio manager success."); + return DH_SUCCESS; +} + +int32_t FindAudioDevice() +{ + if (g_manager == nullptr) { + return "false"; + } + int32_t ret = g_manager->GetAllAdapters(g_manager, &g_devices, &g_deviceNum); + if (ret != DH_SUCCESS) { + DHLOGI("Get audio devices failed!"); + return "false"; + } + std::string res = "true"; + std::cout << "Get audio devices success, adapter size: " << g_deviceNum << std::endl; + for (int32_t index = 0; index < g_deviceNum; index++) { + const AudioAdapterDescriptor &desc = g_devices[index]; + std::cout << "Device[" << index << "] ID: " << desc.adapterName << std::endl; + deviceId = deviceId + desc.adapterName; + DHLOGI("demo test: Device [%s] ID",desc.adapterName); + if (index != g_deviceNum - 1) { + } + std::cout << "pin list: "; + for (uint32_t i = 0; i < desc.portNum; i++) { + std::cout << desc.ports[i].portId << ", "; + } + std::cout << std::endl; + } + return DH_SUCCESS; +} + +static void HandleDevError(const char *condition, const char *value) +{ + if (condition[TYPE_OFFSET] == DEV_TYPE_SPK && g_spkStatus != DEVICE_IDLE) { + CloseSpk(); + } + + if (condition[TYPE_OFFSET] == DEV_TYPE_MIC && g_micStatus == DEVICE_IDLE) { + CloseMic(); + } + + DHLOGI("Receive abnormal event, Demo quit."); +} + +static int32_t ParamEventCallback(AudioExtParamKey key, const char *condition, const char *value, void *reserved, + void *cookie) +{ + std::string val(value); + std::string con(condition); + std::cout << std::endl; + DHLOGI("**********************************************************************************"); + std::cout << "Event recived: " << key << std::endl; + std::cout << "Condition: " << con << std::endl; + std::cout << "Value: " << val << std::endl; + std::cout << std::endl; + DHLOGI("**********************************************************************************"); + + if (key == AudioExtParamKey::AUDIO_EXT_PARAM_KEY_STATUS && con.rfind("ERR_EVENT", 0) == 0) { + HandleDevError(condition, value); + } + return DH_SUCCESS; +} + +static int32_t LoadSpkDev(const std::string &devId) +{ + std::cout << "Open SPK device , device Id:" << devId << std::endl; + + struct AudioAdapterDescriptor *dev = nullptr; + for (int32_t index = 0; index < g_deviceNum; index++) { + struct AudioAdapterDescriptor &desc = g_devices[index]; + if (desc.adapterName == devId) { + dev = &desc; + break; + } + } + if (dev == nullptr) { + DHLOGI("Input device id is wrong."); + FindAudioDevice(); + return ERR_DH_AUDIO_HDF_FAIL; + } + if (g_manager == nullptr) { + return ERR_DH_AUDIO_HDF_FAIL; + } + if (g_adapter == nullptr) { + int32_t ret = g_manager->LoadAdapter(g_manager, dev, &g_adapter); + if (ret != DH_SUCCESS || g_adapter == nullptr) { + std::cout << "Load audio device failed, ret: " << ret << std::endl; + return ERR_DH_AUDIO_HDF_FAIL; + } + } + DHLOGI("Load audio device success."); + return DH_SUCCESS; +} + +std::string OpenSpk() +{ + if (g_spkStatus != DEVICE_IDLE) { + DHLOGI("Speaker device is already opened."); + return "true"; + } + if (LoadSpkDev(deviceId) != DH_SUCCESS) { + return "false"; + } + ParamCallback callback = ParamEventCallback; + int32_t ret = g_adapter->RegExtraParamObserver(g_adapter, callback, nullptr); + if (ret != DH_SUCCESS) { + std::cout << "Register observer failed, ret: " << ret << std::endl; + return "false"; + } + + struct AudioDeviceDescriptor renderDesc; + renderDesc.pins = AudioPortPin::PIN_OUT_SPEAKER; + renderDesc.desc = nullptr; + AudioSampleAttributes g_rattrs = {}; + g_rattrs.type = AUDIO_IN_MEDIA; + g_rattrs.interleaved = RENDER_INTER_LEAVED; + g_rattrs.streamId = RENDER_STREAM_ID; + g_rattrs.channelCount = RENDER_CHANNEL_MASK; + g_rattrs.sampleRate = AUDIO_SAMPLE_RATE; + g_rattrs.format = AudioFormat::AUDIO_FORMAT_TYPE_PCM_16_BIT; + ret = g_adapter->CreateRender(g_adapter, &renderDesc, &g_rattrs, &g_render); + if (ret != DH_SUCCESS || g_render == nullptr) { + std::cout << "Open SPK device failed, ret: " << ret << std::endl; + return "true"; + } + g_spkStatus = DEVICE_OPEN; + DHLOGI("Open SPK device success."); + return "true"; +} + +static void WriteStreamWait(const int64_t &startTime) +{ + int64_t endTime = GetNowTimeUs(); + int64_t passTime = endTime - startTime; + + if (passTime > AUDIO_FRAME_TIME_INTERFAL_DEFAULT) { + return; + } + int64_t remainTime = AUDIO_FRAME_TIME_INTERFAL_DEFAULT - passTime; + std::this_thread::sleep_for(std::chrono::microseconds(remainTime)); +} + +static void Play() +{ + if (g_render == nullptr) { + DHLOGI("SPK device is null."); + return; + } + if (pthread_setname_np(pthread_self(), PLAY_THREAD) != DH_SUCCESS) { + DHLOGI("Play thread setname failed."); + } + DHLOGI("Playing thread started."); + g_render->control.Start((AudioHandle)g_render); + g_spkStatus = DEVICE_START; + + uint64_t size = 0; + while (g_spkStatus == 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) { + std::cout<<"RenderFrame failed, index: "<< g_frameIndex << ", ret: " << ret << std::endl; + } + g_frameIndex++; + if (g_frameNum != 0 && g_frameIndex == g_frameNum) { + g_frameIndex = 0; + } + WriteStreamWait(startTime); + } + DHLOGI("Playing thread stopped."); +} + +std::string StartRender() +{ + if (g_spkStatus == DEVICE_IDLE || g_spkStatus == DEVICE_OPEN) { + return "true"; + } + + if (g_spkStatus == DEVICE_OPEN) { + WavHdr wavHeader; + size_t headerSize = sizeof(WavHdr); + if (!g_isInitRenderData) { + struct stat statbuf; + stat(SPK_FILE_PATH, &statbuf); + int32_t size = statbuf.st_size; + g_frameNum = (size - headerSize) / RENDER_FRAME_SIZE; + std::cout << "Audio file frame num: " << g_frameNum << std::endl; + for (int32_t j = 0; j < g_frameNum; j++) { + uint8_t *frame = new uint8_t[RENDER_FRAME_SIZE](); + renderData.push_back(frame); + } + g_isInitRenderData = true; + } + FILE *wavFile = fopen(SPK_FILE_PATH, "rb"); + fread(&wavHeader, 1, headerSize, wavFile); + for (int32_t i = 0; i < g_frameNum; i++) { + fread(renderData[i], 1, RENDER_FRAME_SIZE, wavFile); + } + fclose(wavFile); + g_frameIndex = 0; + g_palyingThread = std::thread(Play); + return "true"; + } + if (g_spkStatus == DEVICE_START) { + return "Speaker device is started."; + } + if (g_spkStatus == DEVICE_STOP) { + g_palyingThread = std::thread(Play); + } + return "true"; +} + +std::string StopRender() +{ + if (g_render == nullptr) { + return "true"; + } + + if (g_spkStatus == DEVICE_IDLE) { + return "Speaker device is not opened."; + } + + if (g_spkStatus == DEVICE_OPEN) { + return "Speaker device is not started."; + } + + if (g_spkStatus == DEVICE_STOP) { + return "Speaker device is already stoped."; + } + + g_spkStatus = DEVICE_STOP; + if (g_palyingThread.joinable()) { + g_palyingThread.join(); + } + g_render->control.Stop((AudioHandle)g_render); + return "true"; +} + +std::string CloseSpk() +{ + if (g_spkStatus == DEVICE_IDLE) { + return "true"; + } + + if (g_spkStatus == DEVICE_START) { + StopRender(); + } + + int32_t ret = g_adapter->DestroyRender(g_adapter, g_render); + if (ret != DH_SUCCESS) { + return "Close speaker failed"; + } + if (g_micStatus == DEVICE_IDLE) { + g_manager->UnloadAdapter(g_manager, g_adapter); + g_adapter = nullptr; + } + g_spkStatus = DEVICE_IDLE; + + if (g_isInitRenderData) { + for (auto &p : renderData) { + delete[] p; + } + renderData.clear(); + g_isInitRenderData = false; + } + return "true"; +} + +static int32_t LoadMicDev(const std::string &devId) +{ + std::cout << "Open MIC device ,input device Id:" << devId << std::endl; + + struct AudioAdapterDescriptor *dev = nullptr; + for (int32_t index = 0; index < g_deviceNum; index++) { + struct AudioAdapterDescriptor &desc = g_devices[index]; + if (desc.adapterName == devId) { + dev = &desc; + break; + } + } + if (dev == nullptr) { + DHLOGI("Input device id is wrong."); + FindAudioDevice(); + return ERR_DH_AUDIO_HDF_FAIL; + } + if (g_manager == nullptr) { + return ERR_DH_AUDIO_HDF_FAIL; + } + if (g_adapter == nullptr) { + int32_t ret = g_manager->LoadAdapter(g_manager, dev, &g_adapter); + if (ret != DH_SUCCESS || g_adapter == nullptr) { + std::cout << "Load audio device failed, ret: " << ret << std::endl; + return ERR_DH_AUDIO_HDF_FAIL; + } + } + DHLOGI("Load audio device success."); + return DH_SUCCESS; +} + +std::string OpenMic() +{ + if (g_micStatus != DEVICE_IDLE) { + return "Mic device is already opened."; + } + if (LoadMicDev(deviceId) != DH_SUCCESS) { + return "Load audio device failed."; + } + + AudioDeviceDescriptor captureDesc; + captureDesc.pins = AudioPortPin::PIN_IN_MIC; + captureDesc.desc = nullptr; + AudioSampleAttributes captureAttr; + captureAttr.type = AUDIO_IN_MEDIA; + captureAttr.interleaved = CAPTURE_INTER_LEAVED; + captureAttr.streamId = CAPTURE_STREAM_ID; + captureAttr.channelCount = CAPTURE_CHANNEL_MASK; + captureAttr.sampleRate = AUDIO_SAMPLE_RATE; + captureAttr.format = AudioFormat::AUDIO_FORMAT_TYPE_PCM_16_BIT; + int32_t ret = g_adapter->CreateCapture(g_adapter, &captureDesc, &captureAttr, &g_capture); + if (ret != DH_SUCCESS || g_capture == nullptr) { + return "true"; + } + g_micStatus = DEVICE_OPEN; + return "true"; +} + +static void ReadStreamWait(const int64_t &startTime) +{ + int64_t endTime = GetNowTimeUs(); + int32_t passTime = endTime - startTime; + + if (passTime > AUDIO_FRAME_TIME_INTERFAL_DEFAULT) { + return; + } + int64_t remainTime = AUDIO_FRAME_TIME_INTERFAL_DEFAULT - passTime; + std::this_thread::sleep_for(std::chrono::microseconds(remainTime)); +} + +static void Capture() +{ + if (g_capture == nullptr) { + DHLOGI("MIC device is null."); + return; + } + if (pthread_setname_np(pthread_self(), CAPTURE_THREAD) != DH_SUCCESS) { + DHLOGI("Capture thread setname failed."); + } + DHLOGI("Capturing thread started."); + g_capture->control.Start((AudioHandle)g_capture); + g_micStatus = DEVICE_START; + + uint64_t size = 0; + while (g_micStatus == 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); + if (ret != DH_SUCCESS) { + std::cout << "CaptureFrame failed, ret: " << ret << std::endl; + return; + } + fwrite(data, 1, RENDER_FRAME_SIZE, g_micFile); + g_micFrameNum++; + ReadStreamWait(startTime); + } + DHLOGI("Capturing thread stopped."); +} + +std::string StartCapture() +{ + if (g_micStatus == DEVICE_IDLE) { + return "true"; + } + + if (g_micStatus == DEVICE_OPEN) { + g_micFile = fopen(MIC_FILE_PATH, "ab+"); + if (g_micFile == nullptr) { + return "Open pcm file failed."; + } + g_capingThread = std::thread(Capture); + return "true"; + } + + if (g_micStatus == DEVICE_START) { + return "Mic device is already started."; + } + + if (g_micStatus == DEVICE_STOP) { + g_capingThread = std::thread(Capture); + } + return "true"; +} + +std::string StopCapture() +{ + if (g_capture == nullptr) { + return "true"; + } + + if (g_micStatus == DEVICE_IDLE) { + return "Mic device is not opened."; + } + + if (g_micStatus == DEVICE_OPEN) { + return "Mic device is not started."; + } + + if (g_micStatus == DEVICE_STOP) { + return "Mic device is already started."; + } + + g_micStatus = DEVICE_STOP; + if (g_capingThread.joinable()) { + g_capingThread.join(); + } + g_capture->control.Stop((AudioHandle)g_capture); + return "true"; +} + +std::string CloseMic() +{ + if (g_micStatus == DEVICE_IDLE) { + return "true"; + } + + if (g_micStatus == DEVICE_START) { + StopCapture(); + } + + int32_t ret = g_adapter->DestroyCapture(g_adapter, g_capture); + if (ret != DH_SUCCESS) { + return "Close mic failed."; + } + if (g_spkStatus == DEVICE_IDLE) { + g_manager->UnloadAdapter(g_manager, g_adapter); + g_adapter = nullptr; + } + if (g_micFile != nullptr) { + fclose(g_micFile); + g_micFile = nullptr; + } + g_micStatus = DEVICE_IDLE; + return "true"; +} + +std::string SetVolume(std::string vol) +{ + if (g_spkStatus == DEVICE_IDLE) { + return "true"; + } + int32_t volInt = std::stoi(vol); + if (volInt < VOLUME_MIN || volInt > VOLUME_MAX) { + return "Volume is invalid"; + } + enum 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()); + if (ret != DH_SUCCESS) { + return "Set volume failed"; + } + return "true"; +} + +std::string GetVolume() +{ + if (g_spkStatus == DEVICE_IDLE) { + return "true"; + } + enum AudioExtParamKey key = AudioExtParamKey::AUDIO_EXT_PARAM_KEY_VOLUME; + std::string condition = "EVENT_TYPE=1;VOLUME_GROUP_ID=1;AUDIO_VOLUME_TYPE=1;"; + char vol[VOLUME_BIT]; + int32_t ret = g_adapter->GetExtraParams(g_adapter, key, condition.c_str(), vol, VOLUME_BIT); + if (ret != DH_SUCCESS) { + return "Get Volume failed."; + } + DHLOGI("demo test:GetVolume = %s", vol); + return "true"; +} \ No newline at end of file diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h new file mode 100644 index 00000000..402cae00 --- /dev/null +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISTRIBUTED_AUDIO_TEST_H +#define DISTRIBUTED_AUDIO_TEST_H + +#include +#include +#include +#include +#include +#include +#include + +#include "audio_adapter.h" +#include "audio_manager.h" +#include "audio_types.h" +#include "daudio_errcode.h" + +const std::string CMD_QUIT = "quit"; +const std::string CMD_FIND = "find"; +const std::string CMD_OPEN_SPK = "openspk"; +const std::string CMD_CLOSE_SPK = "closespk"; +const std::string CMD_START_SPK = "startspk"; +const std::string CMD_STOP_SPK = "stopspk"; +const std::string CMD_OPEN_MIC = "openmic"; +const std::string CMD_CLOSE_MIC = "closemic"; +const std::string CMD_START_MIC = "startmic"; +const std::string CMD_STOP_MIC = "stopmic"; +const std::string CMD_SET_VOL = "setvol"; +const std::string CMD_GET_VOL = "getvol"; + +const std::string CMD_QUIT_EXT = "0"; +const std::string CMD_FIND_EXT = "9"; +const std::string CMD_OPEN_SPK_EXT = "1"; +const std::string CMD_CLOSE_SPK_EXT = "2"; +const std::string CMD_START_SPK_EXT = "3"; +const std::string CMD_STOP_SPK_EXT = "4"; +const std::string CMD_OPEN_MIC_EXT = "5"; +const std::string CMD_CLOSE_MIC_EXT = "6"; +const std::string CMD_START_MIC_EXT = "7"; +const std::string CMD_STOP_MIC_EXT = "8"; +const std::string CMD_SET_VOL_EXT = "11"; +const std::string CMD_GET_VOL_EXT = "12"; + +const char DEV_TYPE_SPK = '1'; +const char DEV_TYPE_MIC = '2'; +const char SPK_FILE_PATH[128] = "/data/test.pcm"; +const char MIC_FILE_PATH[128] = "/data/mic.pcm"; +constexpr int32_t TYPE_OFFSET = 12; +constexpr int32_t AUDIO_SAMPLE_RATE = 48000; +constexpr int32_t VOLUME_MIN = 0; +constexpr int32_t VOLUME_MAX = 15; +constexpr int32_t VOLUME_BIT = 3; +constexpr int32_t RENDER_FRAME_SIZE = 4096; +constexpr int32_t RENDER_INTER_LEAVED = 1; +constexpr int32_t RENDER_STREAM_ID = 0; +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; + +typedef enum { + DEVICE_IDLE = 0, + DEVICE_OPEN = 1, + DEVICE_START = 2, + DEVICE_STOP = 3, +} DeviceStatus; + +struct WAV_HEADER { + /* RIFF Chunk Descriptor */ + uint8_t riff[4] = {'R', 'I', 'F', 'F'}; + uint32_t chunkSize = 0; + uint8_t wave[4] = {'W', 'A', 'V', 'E'}; + /* "fmt" sub-chunk */ + uint8_t fmt[4] = {'f', 'm', 't', ' '}; + uint32_t subchunk1Size = 16; + uint16_t audioFormat = 1; + uint16_t numOfChan = 2; + uint32_t samplesPerSec = 44100; + uint32_t bytesPerSec = 176400; + uint16_t blockAlign = 2; + uint16_t bitsPerSample = 16; + /* "data" sub-chunk */ + uint8_t subchunk2ID[4] = {'d', 'a', 't', 'a'}; + uint32_t subchunk2Size = 0; +}; +using WavHdr = struct WAV_HEADER; +int32_t InitTestDemo(); +int32_t FindAudioDevice(); +std::string OpenSpk(); +std::string StartRender(); +std::string StopRender(); +std::string CloseSpk(); +std::string OpenMic(); +std::string StartCapture(); +std::string StopCapture(); +std::string CloseMic(); +std::string SetVolume(std::string vol); +std::string GetVolume(); +#endif \ No newline at end of file -- Gitee From 814e85b3fe039f90b4f1afc9ee64ab052e89cc46 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Thu, 20 Jul 2023 20:36:51 +0800 Subject: [PATCH 2/8] add DCTS Signed-off-by: w30042960 --- .../distributed_audio_test.cpp | 110 +++++++++--------- .../distributed_audio_test.h | 8 +- 2 files changed, 60 insertions(+), 58 deletions(-) diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp index a023da68..e0f98602 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp @@ -28,9 +28,10 @@ #include #include "unistd.h" -#include "./distributed_audio_test.h" +#include "distributed_audio_test.h" -std::string deviceId; +static const std::string SERVER_FIFO = "/data/seqnum_sv"; +static const std::string CLIENT_FIFO_TEMPLATE = "/data/seqnum_cl.%ld"; struct Request { pid_t pid; @@ -54,6 +55,7 @@ static AudioAdapterDescriptor *g_devices = nullptr; static constexpr const char* PLAY_THREAD = "playThread"; static constexpr const char* CAPTURE_THREAD = "captureThread"; +const char *adaptName = nullptr; int32_t g_deviceNum = 0; int32_t g_frameNum = 0; int32_t g_frameIndex = 0; @@ -77,38 +79,36 @@ static int64_t GetNowTimeUs() int32_t InitTestDemo() { - DHLOGI("**********************************************************************************"); - DHLOGI("Distributed Audio Test Demo Bin v1.3."); - DHLOGI("**********************************************************************************"); - DHLOGI("Init distributed audio hdf service."); + std::cout << "**********************************************************************************" << std::endl; + std::cout << "Distributed Audio Test Demo Bin v1.3." << std::endl; + std::cout << "**********************************************************************************" << std::endl; + std::cout << std::endl; + std::cout << "Init distributed audio hdf service." << std::endl; g_manager = GetAudioManagerFuncs(); if (g_manager == nullptr) { - DHLOGI("Distributed audio manager is null, quit!"); + std::cout << "Distributed audio manager is null, quit!" << std::endl; return ERR_DH_AUDIO_HDF_FAIL; } - DHLOGI("Load audio manager success."); + std::cout << "Load audio manager success." << std::endl; return DH_SUCCESS; } int32_t FindAudioDevice() { if (g_manager == nullptr) { - return "false"; + return ERR_DH_AUDIO_HDF_NULLPTR; } int32_t ret = g_manager->GetAllAdapters(g_manager, &g_devices, &g_deviceNum); if (ret != DH_SUCCESS) { - DHLOGI("Get audio devices failed!"); - return "false"; + std::cout << "Get audio devices failed!"; + return ERR_DH_AUDIO_HDF_NULLPTR; } - std::string res = "true"; + std::string res = "true;"; std::cout << "Get audio devices success, adapter size: " << g_deviceNum << std::endl; for (int32_t index = 0; index < g_deviceNum; index++) { const AudioAdapterDescriptor &desc = g_devices[index]; std::cout << "Device[" << index << "] ID: " << desc.adapterName << std::endl; - deviceId = deviceId + desc.adapterName; - DHLOGI("demo test: Device [%s] ID",desc.adapterName); - if (index != g_deviceNum - 1) { - } + adaptName = desc.adapterName; std::cout << "pin list: "; for (uint32_t i = 0; i < desc.portNum; i++) { std::cout << desc.ports[i].portId << ", "; @@ -128,7 +128,7 @@ static void HandleDevError(const char *condition, const char *value) CloseMic(); } - DHLOGI("Receive abnormal event, Demo quit."); + std::cout << "Receive abnormal event, Demo quit." << std::endl; } static int32_t ParamEventCallback(AudioExtParamKey key, const char *condition, const char *value, void *reserved, @@ -137,12 +137,12 @@ static int32_t ParamEventCallback(AudioExtParamKey key, const char *condition, c std::string val(value); std::string con(condition); std::cout << std::endl; - DHLOGI("**********************************************************************************"); + std::cout << "**********************************************************************************" << std::endl; std::cout << "Event recived: " << key << std::endl; std::cout << "Condition: " << con << std::endl; std::cout << "Value: " << val << std::endl; + std::cout << "**********************************************************************************" << std::endl; std::cout << std::endl; - DHLOGI("**********************************************************************************"); if (key == AudioExtParamKey::AUDIO_EXT_PARAM_KEY_STATUS && con.rfind("ERR_EVENT", 0) == 0) { HandleDevError(condition, value); @@ -150,20 +150,20 @@ static int32_t ParamEventCallback(AudioExtParamKey key, const char *condition, c return DH_SUCCESS; } -static int32_t LoadSpkDev(const std::string &devId) +static int32_t LoadSpkDev() { - std::cout << "Open SPK device , device Id:" << devId << std::endl; + std::cout << "Open SPK device , device Id:" << adaptName << std::endl; struct AudioAdapterDescriptor *dev = nullptr; for (int32_t index = 0; index < g_deviceNum; index++) { struct AudioAdapterDescriptor &desc = g_devices[index]; - if (desc.adapterName == devId) { + if (strcmp(desc.adapterName, adaptName) == 0) { dev = &desc; break; } } if (dev == nullptr) { - DHLOGI("Input device id is wrong."); + std::cout << "Input device id is wrong." << std::endl; FindAudioDevice(); return ERR_DH_AUDIO_HDF_FAIL; } @@ -177,17 +177,17 @@ static int32_t LoadSpkDev(const std::string &devId) return ERR_DH_AUDIO_HDF_FAIL; } } - DHLOGI("Load audio device success."); + std::cout << "Load audio device success." << std::endl; return DH_SUCCESS; } -std::string OpenSpk() +std::string OpenSpk(const std::string &devId) { if (g_spkStatus != DEVICE_IDLE) { - DHLOGI("Speaker device is already opened."); + std::cout << "Speaker device is already opened." << std::endl; return "true"; } - if (LoadSpkDev(deviceId) != DH_SUCCESS) { + if (LoadSpkDev() != DH_SUCCESS) { return "false"; } ParamCallback callback = ParamEventCallback; @@ -210,10 +210,10 @@ std::string OpenSpk() ret = g_adapter->CreateRender(g_adapter, &renderDesc, &g_rattrs, &g_render); if (ret != DH_SUCCESS || g_render == nullptr) { std::cout << "Open SPK device failed, ret: " << ret << std::endl; - return "true"; + return "false"; } g_spkStatus = DEVICE_OPEN; - DHLOGI("Open SPK device success."); + std::cout << "Open SPK device success." << std::endl; return "true"; } @@ -232,13 +232,13 @@ static void WriteStreamWait(const int64_t &startTime) static void Play() { if (g_render == nullptr) { - DHLOGI("SPK device is null."); + std::cout << "SPK device is null." << std::endl; return; } if (pthread_setname_np(pthread_self(), PLAY_THREAD) != DH_SUCCESS) { - DHLOGI("Play thread setname failed."); + std::cout << "Play thread setname failed." << std::endl; } - DHLOGI("Playing thread started."); + std::cout << "Playing thread started." << std::endl; g_render->control.Start((AudioHandle)g_render); g_spkStatus = DEVICE_START; @@ -255,13 +255,13 @@ static void Play() } WriteStreamWait(startTime); } - DHLOGI("Playing thread stopped."); + std::cout << "Playing thread stopped." << std::endl; } std::string StartRender() { - if (g_spkStatus == DEVICE_IDLE || g_spkStatus == DEVICE_OPEN) { - return "true"; + if (g_spkStatus == DEVICE_IDLE) { + return "Speaker device is not opened, start render failed."; } if (g_spkStatus == DEVICE_OPEN) { @@ -301,7 +301,7 @@ std::string StartRender() std::string StopRender() { if (g_render == nullptr) { - return "true"; + return "SPK device is null."; } if (g_spkStatus == DEVICE_IDLE) { @@ -327,7 +327,7 @@ std::string StopRender() std::string CloseSpk() { if (g_spkStatus == DEVICE_IDLE) { - return "true"; + return "Speaker device is not opened."; } if (g_spkStatus == DEVICE_START) { @@ -354,20 +354,20 @@ std::string CloseSpk() return "true"; } -static int32_t LoadMicDev(const std::string &devId) +static int32_t LoadMicDev() { - std::cout << "Open MIC device ,input device Id:" << devId << std::endl; + std::cout << "Open MIC device ,input device Id:" << adaptName << std::endl; struct AudioAdapterDescriptor *dev = nullptr; for (int32_t index = 0; index < g_deviceNum; index++) { struct AudioAdapterDescriptor &desc = g_devices[index]; - if (desc.adapterName == devId) { + if (strcmp(desc.adapterName, adaptName) == 0) { dev = &desc; break; } } if (dev == nullptr) { - DHLOGI("Input device id is wrong."); + std::cout << "Input device id is wrong." << std::endl; FindAudioDevice(); return ERR_DH_AUDIO_HDF_FAIL; } @@ -381,7 +381,7 @@ static int32_t LoadMicDev(const std::string &devId) return ERR_DH_AUDIO_HDF_FAIL; } } - DHLOGI("Load audio device success."); + std::cout << "Load audio device success." << std::endl; return DH_SUCCESS; } @@ -390,7 +390,7 @@ std::string OpenMic() if (g_micStatus != DEVICE_IDLE) { return "Mic device is already opened."; } - if (LoadMicDev(deviceId) != DH_SUCCESS) { + if (LoadMicDev() != DH_SUCCESS) { return "Load audio device failed."; } @@ -406,7 +406,7 @@ std::string OpenMic() captureAttr.format = AudioFormat::AUDIO_FORMAT_TYPE_PCM_16_BIT; int32_t ret = g_adapter->CreateCapture(g_adapter, &captureDesc, &captureAttr, &g_capture); if (ret != DH_SUCCESS || g_capture == nullptr) { - return "true"; + return "Open MIC device failed."; } g_micStatus = DEVICE_OPEN; return "true"; @@ -427,13 +427,13 @@ static void ReadStreamWait(const int64_t &startTime) static void Capture() { if (g_capture == nullptr) { - DHLOGI("MIC device is null."); + std::cout << "MIC device is null." << std::endl; return; } if (pthread_setname_np(pthread_self(), CAPTURE_THREAD) != DH_SUCCESS) { - DHLOGI("Capture thread setname failed."); + std::cout << "Capture thread setname failed." << std::endl; } - DHLOGI("Capturing thread started."); + std::cout << "Capturing thread started." << std::endl; g_capture->control.Start((AudioHandle)g_capture); g_micStatus = DEVICE_START; @@ -450,13 +450,13 @@ static void Capture() g_micFrameNum++; ReadStreamWait(startTime); } - DHLOGI("Capturing thread stopped."); + std::cout << "Capturing thread stopped." << std::endl; } std::string StartCapture() { if (g_micStatus == DEVICE_IDLE) { - return "true"; + return "Mic device is not opened, start capture failed."; } if (g_micStatus == DEVICE_OPEN) { @@ -481,7 +481,7 @@ std::string StartCapture() std::string StopCapture() { if (g_capture == nullptr) { - return "true"; + return "MIC device is null."; } if (g_micStatus == DEVICE_IDLE) { @@ -507,7 +507,7 @@ std::string StopCapture() std::string CloseMic() { if (g_micStatus == DEVICE_IDLE) { - return "true"; + return "Mic device is not opened."; } if (g_micStatus == DEVICE_START) { @@ -533,7 +533,7 @@ std::string CloseMic() std::string SetVolume(std::string vol) { if (g_spkStatus == DEVICE_IDLE) { - return "true"; + return "Speaker is not opened, can not set volume."; } int32_t volInt = std::stoi(vol); if (volInt < VOLUME_MIN || volInt > VOLUME_MAX) { @@ -551,7 +551,7 @@ std::string SetVolume(std::string vol) std::string GetVolume() { if (g_spkStatus == DEVICE_IDLE) { - return "true"; + return "Speaker is not opened, can not get volume."; } enum AudioExtParamKey key = AudioExtParamKey::AUDIO_EXT_PARAM_KEY_VOLUME; std::string condition = "EVENT_TYPE=1;VOLUME_GROUP_ID=1;AUDIO_VOLUME_TYPE=1;"; @@ -560,6 +560,6 @@ std::string GetVolume() if (ret != DH_SUCCESS) { return "Get Volume failed."; } - DHLOGI("demo test:GetVolume = %s", vol); + std::string volStr(vol); return "true"; -} \ No newline at end of file +} diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h index 402cae00..bfc1295c 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h @@ -101,16 +101,18 @@ struct WAV_HEADER { uint32_t subchunk2Size = 0; }; using WavHdr = struct WAV_HEADER; + int32_t InitTestDemo(); int32_t FindAudioDevice(); std::string OpenSpk(); -std::string StartRender(); -std::string StopRender(); std::string CloseSpk(); std::string OpenMic(); +std::string CloseMic(); +std::string StartRender(); +std::string StopRender(); std::string StartCapture(); std::string StopCapture(); -std::string CloseMic(); std::string SetVolume(std::string vol); std::string GetVolume(); + #endif \ No newline at end of file -- Gitee From 883ab83cad76c99f6031de278ee20312adbb8cd2 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Mon, 24 Jul 2023 11:35:15 +0800 Subject: [PATCH 3/8] add DCTS Signed-off-by: w30042960 --- .../distributedaudiotest/distributed_audio_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp index e0f98602..589980b1 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp @@ -181,7 +181,7 @@ static int32_t LoadSpkDev() return DH_SUCCESS; } -std::string OpenSpk(const std::string &devId) +std::string OpenSpk() { if (g_spkStatus != DEVICE_IDLE) { std::cout << "Speaker device is already opened." << std::endl; -- Gitee From adcac887ac14c28e1bc9d3d4900ab548df43d1b4 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Tue, 25 Jul 2023 19:27:59 +0800 Subject: [PATCH 4/8] add DCTS Signed-off-by: w30042960 --- .../distributedaudiotest/BUILD.gn | 58 +++++++++++++++ .../distributedaudiotest/Test.json | 2 +- .../daudio_automat_test.cpp | 70 +++++++++++-------- 3 files changed, 99 insertions(+), 31 deletions(-) create mode 100644 hdf_service/distributed_audio/distributedaudiotest/BUILD.gn diff --git a/hdf_service/distributed_audio/distributedaudiotest/BUILD.gn b/hdf_service/distributed_audio/distributedaudiotest/BUILD.gn new file mode 100644 index 00000000..a9af3df9 --- /dev/null +++ b/hdf_service/distributed_audio/distributedaudiotest/BUILD.gn @@ -0,0 +1,58 @@ +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") +import("//drivers/peripheral/adapter/base/hidl_adapter.gni") +import("//test/xts/tools/build/suite.gni") +import("../../../distributedaudio.gni") + +module_output_path = "distributedhardware/dctsdisaudiotest" + +ohos_moduletest_suite("DctsdisAudioTest") { + module_out_path = module_output_path + include_dirs = [ + "./include", + "//drivers/peripheral/audio/interfaces/include", + "${services_path}/hdfaudioclient/include", + "${hdf_service_path}/hdi_service/common/include", + ] + + sources = [ + "daudio_automat_test.cpp", + "distributed_audio_test.cpp", + ] + + deps = [ + "$hdf_uhdf_path/utils:libhdf_utils", + "${services_path}/hdfaudioclient:daudio_client", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DctsdisAudioTest\"", + "LOG_DOMAIN=0xD004100", + ] + + install_enable = true + install_images = [ chipset_base_dir ] + subsystem_name = "distributedhardware" + part_name = "distributed_audio" +} diff --git a/hdf_service/distributed_audio/distributedaudiotest/Test.json b/hdf_service/distributed_audio/distributedaudiotest/Test.json index a8dea064..01f2723b 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/Test.json +++ b/hdf_service/distributed_audio/distributedaudiotest/Test.json @@ -1,5 +1,5 @@ { - "description": "Config for disCamera test cases", + "description": "Config for disAudio test cases", "driver": { "module-name": "DctsdisAudioTest", "native-test-timeout": "120000", diff --git a/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp b/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp index 2769cbbd..c135df94 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp +++ b/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp @@ -54,15 +54,25 @@ void DAudioAutomatTest::SetUp(void) {} void DAudioAutomatTest::TearDown(void) {} DAudioAutomatTest::DAudioAutomatTest(void) {} - /** * @tc.number : DAudioTest_001 - * @tc.name : open、close speaker test - * @tc.desc : open、close speaker + * @tc.name : find audio device test + * @tc.desc : find audio device */ HWTEST_F(DAudioAutomatTest, DAudioTest_001, TestSize.Level1) { std::cout << "start run DAudioTest_001." << std::endl; + EXPECT_EQ(DAUDIO_OK, FindAudioDevice()); +} + +/** + * @tc.number : DAudioTest_002 + * @tc.name : open、close speaker test + * @tc.desc : open、close speaker + */ +HWTEST_F(DAudioAutomatTest, DAudioTest_002, TestSize.Level1) +{ + std::cout << "start run DAudioTest_002." << std::endl; std::string retStr = "false"; retStr = OpenSpk(); EXPECT_EQ("true", retStr); @@ -71,13 +81,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_001, TestSize.Level1) } /** - * @tc.number : DAudioTest_002 + * @tc.number : DAudioTest_003 * @tc.name : open、close speaker test * @tc.desc : open、close speaker 20 times */ -HWTEST_F(DAudioAutomatTest, DAudioTest_002, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_003, TestSize.Level1) { - std::cout << "start run DAudioTest_002." << std::endl; + std::cout << "start run DAudioTest_003." << std::endl; for (int32_t i = 0; i < RUN_TIMES; ++i) { std::string retStr = "false"; retStr = OpenSpk(); @@ -88,13 +98,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_002, TestSize.Level1) } /** - * @tc.number : DAudioTest_003 + * @tc.number : DAudioTest_004 * @tc.name : open、close mic test * @tc.desc : open、close mic */ -HWTEST_F(DAudioAutomatTest, DAudioTest_003, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_004, TestSize.Level1) { - std::cout << "start run DAudioTest_003." << std::endl; + std::cout << "start run DAudioTest_004." << std::endl; std::string retStr = "false"; retStr = OpenMic(); EXPECT_EQ("true", retStr); @@ -103,13 +113,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_003, TestSize.Level1) } /** - * @tc.number : DAudioTest_004 + * @tc.number : DAudioTest_005 * @tc.name : open、close mic test * @tc.desc : open、close mic 20 times */ -HWTEST_F(DAudioAutomatTest, DAudioTest_004, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_005, TestSize.Level1) { - std::cout << "start run DAudioTest_004." << std::endl; + std::cout << "start run DAudioTest_005." << std::endl; for (int32_t i = 0; i < RUN_TIMES; ++i) { std::string retStr = "false"; retStr = OpenMic(); @@ -120,13 +130,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_004, TestSize.Level1) } /** - * @tc.number : DAudioTest_005 + * @tc.number : DAudioTest_006 * @tc.name : start、stop render test * @tc.desc : start、stop render */ -HWTEST_F(DAudioAutomatTest, DAudioTest_005, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_006, TestSize.Level1) { - std::cout << "start run DAudioTest_005." << std::endl; + std::cout << "start run DAudioTest_006." << std::endl; std::string retStr = "false"; retStr = OpenSpk(); EXPECT_EQ("true", retStr); @@ -140,13 +150,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_005, TestSize.Level1) } /** - * @tc.number : DAudioTest_006 + * @tc.number : DAudioTest_007 * @tc.name : start、stop Capture test * @tc.desc : start、stop Capture */ -HWTEST_F(DAudioAutomatTest, DAudioTest_006, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_007, TestSize.Level1) { - std::cout << "start run DAudioTest_006." << std::endl; + std::cout << "start run DAudioTest_007." << std::endl; std::string retStr = "false"; retStr = OpenMic(); EXPECT_EQ("true", retStr); @@ -160,13 +170,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_006, TestSize.Level1) } /** - * @tc.number : DAudioTest_007 + * @tc.number : DAudioTest_008 * @tc.name : setvolume、getvolume test * @tc.desc : setvolume、getvolume render 20 times */ -HWTEST_F(DAudioAutomatTest, DAudioTest_007, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_008, TestSize.Level1) { - std::cout << "start run DAudioTest_007." << std::endl; + std::cout << "start run DAudioTest_008." << std::endl; std::string retStr = "false"; retStr = OpenSpk(); EXPECT_EQ("true", retStr); @@ -189,13 +199,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_007, TestSize.Level1) } /** - * @tc.number : DAudioTest_008 + * @tc.number : DAudioTest_009 * @tc.name : startcapture、stopcapture test * @tc.desc : startcapture、stopcapture render */ -HWTEST_F(DAudioAutomatTest, DAudioTest_008, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_009, TestSize.Level1) { - std::cout << "start run DAudioTest_008." << std::endl; + std::cout << "start run DAudioTest_009." << std::endl; std::string retStr = "false"; retStr = OpenSpk(); EXPECT_EQ("true", retStr); @@ -221,13 +231,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_008, TestSize.Level1) } /** - * @tc.number : DAudioTest_009 + * @tc.number : DAudioTest_010 * @tc.name : start、stop render test * @tc.desc : start、stop render 20 times */ -HWTEST_F(DAudioAutomatTest, DAudioTest_009, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_010, TestSize.Level1) { - std::cout << "start run DAudioTest_009." << std::endl; + std::cout << "start run DAudioTest_010." << std::endl; std::string retStr = "false"; retStr = OpenSpk(); for (int32_t i = 0; i < RUN_TIMES; ++i) { @@ -243,13 +253,13 @@ HWTEST_F(DAudioAutomatTest, DAudioTest_009, TestSize.Level1) } /** - * @tc.number : DAudioTest_010 + * @tc.number : DAudioTest_011 * @tc.name : start、stop Capture test * @tc.desc : start、stop Capture */ -HWTEST_F(DAudioAutomatTest, DAudioTest_010, TestSize.Level1) +HWTEST_F(DAudioAutomatTest, DAudioTest_011, TestSize.Level1) { - std::cout << "start run DAudioTest_010." << std::endl; + std::cout << "start run DAudioTest_011." << std::endl; std::string retStr = "false"; retStr = OpenMic(); for (int32_t i = 0; i < RUN_TIMES; ++i) { -- Gitee From 9d82c65e8a7027166475652644f0f371487fa763 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Sat, 29 Jul 2023 09:58:58 +0800 Subject: [PATCH 5/8] modify DCTS Signed-off-by: w30042960 --- .../distributed_audio_test.h | 28 +------------------ 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h index bfc1295c..1c6b1f79 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h @@ -29,35 +29,9 @@ #include "audio_types.h" #include "daudio_errcode.h" -const std::string CMD_QUIT = "quit"; -const std::string CMD_FIND = "find"; -const std::string CMD_OPEN_SPK = "openspk"; -const std::string CMD_CLOSE_SPK = "closespk"; -const std::string CMD_START_SPK = "startspk"; -const std::string CMD_STOP_SPK = "stopspk"; -const std::string CMD_OPEN_MIC = "openmic"; -const std::string CMD_CLOSE_MIC = "closemic"; -const std::string CMD_START_MIC = "startmic"; -const std::string CMD_STOP_MIC = "stopmic"; -const std::string CMD_SET_VOL = "setvol"; -const std::string CMD_GET_VOL = "getvol"; - -const std::string CMD_QUIT_EXT = "0"; -const std::string CMD_FIND_EXT = "9"; -const std::string CMD_OPEN_SPK_EXT = "1"; -const std::string CMD_CLOSE_SPK_EXT = "2"; -const std::string CMD_START_SPK_EXT = "3"; -const std::string CMD_STOP_SPK_EXT = "4"; -const std::string CMD_OPEN_MIC_EXT = "5"; -const std::string CMD_CLOSE_MIC_EXT = "6"; -const std::string CMD_START_MIC_EXT = "7"; -const std::string CMD_STOP_MIC_EXT = "8"; -const std::string CMD_SET_VOL_EXT = "11"; -const std::string CMD_GET_VOL_EXT = "12"; - const char DEV_TYPE_SPK = '1'; const char DEV_TYPE_MIC = '2'; -const char SPK_FILE_PATH[128] = "/data/test.pcm"; +const char SPK_FILE_PATH[128] = "/data/test.wav"; const char MIC_FILE_PATH[128] = "/data/mic.pcm"; constexpr int32_t TYPE_OFFSET = 12; constexpr int32_t AUDIO_SAMPLE_RATE = 48000; -- Gitee From 4b56d0a45dd201a01a44c98249d15c9cead86165 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Sat, 29 Jul 2023 16:34:30 +0800 Subject: [PATCH 6/8] modify DCTS Signed-off-by: w30042960 --- hdf_service/distributed_audio/distributedaudiotest/BUILD.gn | 2 +- .../distributedaudiotest/daudio_automat_test.cpp | 2 +- .../distributedaudiotest/distributed_audio_test.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/hdf_service/distributed_audio/distributedaudiotest/BUILD.gn b/hdf_service/distributed_audio/distributedaudiotest/BUILD.gn index a9af3df9..83292ae3 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/BUILD.gn +++ b/hdf_service/distributed_audio/distributedaudiotest/BUILD.gn @@ -25,7 +25,7 @@ ohos_moduletest_suite("DctsdisAudioTest") { module_out_path = module_output_path include_dirs = [ "./include", - "//drivers/peripheral/audio/interfaces/include", + "${driver_audio_path}/include", "${services_path}/hdfaudioclient/include", "${hdf_service_path}/hdi_service/common/include", ] diff --git a/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp b/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp index c135df94..8d5c7955 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp +++ b/hdf_service/distributed_audio/distributedaudiotest/daudio_automat_test.cpp @@ -17,7 +17,7 @@ #include "distributed_audio_test.h" #include -#include +#include #include using namespace testing::ext; diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h index 1c6b1f79..323d0745 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h @@ -76,8 +76,8 @@ struct WAV_HEADER { }; using WavHdr = struct WAV_HEADER; -int32_t InitTestDemo(); -int32_t FindAudioDevice(); +int32_t InitTestDemo(void); +int32_t FindAudioDevice(void); std::string OpenSpk(); std::string CloseSpk(); std::string OpenMic(); -- Gitee From c9887cc747ace426211eb646da73044c60eb7e99 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Mon, 31 Jul 2023 19:10:56 +0800 Subject: [PATCH 7/8] modify DCTS Signed-off-by: w30042960 --- .../distributedaudiotest/distributed_audio_test.cpp | 2 +- .../distributedaudiotest/distributed_audio_test.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp index 589980b1..f8b449da 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.cpp @@ -32,6 +32,7 @@ static const std::string SERVER_FIFO = "/data/seqnum_sv"; static const std::string CLIENT_FIFO_TEMPLATE = "/data/seqnum_cl.%ld"; +using namespace OHOS::DistributedHardware; struct Request { pid_t pid; @@ -45,7 +46,6 @@ struct Response { char resString[CMD_EXECUTING_RETURN_LENGHT_MAX]; }; -using namespace OHOS::DistributedHardware; static AudioManager *g_manager = nullptr; static AudioAdapter *g_adapter = nullptr; static AudioRender *g_render = nullptr; diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h index 323d0745..a43c1b7a 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h @@ -29,6 +29,8 @@ #include "audio_types.h" #include "daudio_errcode.h" +namespace OHOS { +namespace DistributedHardware { const char DEV_TYPE_SPK = '1'; const char DEV_TYPE_MIC = '2'; const char SPK_FILE_PATH[128] = "/data/test.wav"; @@ -75,6 +77,8 @@ struct WAV_HEADER { uint32_t subchunk2Size = 0; }; using WavHdr = struct WAV_HEADER; +} // namespace DistributedHardware +} // namespace OHOS int32_t InitTestDemo(void); int32_t FindAudioDevice(void); -- Gitee From 8c14b17fe83ebe62eed94b214880df2c198a0935 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Tue, 1 Aug 2023 14:31:26 +0800 Subject: [PATCH 8/8] modify DCTS Signed-off-by: w30042960 --- .../distributedaudiotest/distributed_audio_test.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h index a43c1b7a..323d0745 100644 --- a/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h +++ b/hdf_service/distributed_audio/distributedaudiotest/distributed_audio_test.h @@ -29,8 +29,6 @@ #include "audio_types.h" #include "daudio_errcode.h" -namespace OHOS { -namespace DistributedHardware { const char DEV_TYPE_SPK = '1'; const char DEV_TYPE_MIC = '2'; const char SPK_FILE_PATH[128] = "/data/test.wav"; @@ -77,8 +75,6 @@ struct WAV_HEADER { uint32_t subchunk2Size = 0; }; using WavHdr = struct WAV_HEADER; -} // namespace DistributedHardware -} // namespace OHOS int32_t InitTestDemo(void); int32_t FindAudioDevice(void); -- Gitee