diff --git a/services/test_example/distributedaudiotest.cpp b/services/test_example/distributedaudiotest.cpp index 0f930bc3695a4be8b2fb7f45e4b3f6b1db30e026..23fa8697210ed16e7c9a32469da3cbbb7be1078a 100644 --- a/services/test_example/distributedaudiotest.cpp +++ b/services/test_example/distributedaudiotest.cpp @@ -97,10 +97,12 @@ const int32_t CMD_START_MIC = 7; const int32_t CMD_STOP_MIC = 8; const int32_t CMD_SET_VOL = 11; const int32_t CMD_GET_VOL = 12; +const int32_t CMD_LOOP_TEST = 13; const char DEV_TYPE_SPK = '1'; const char DEV_TYPE_MIC = '2'; const char SPK_FILE_PATH[128] = "/data/test.wav"; +const char LOOP_TEST_FILE_PATH[128] = "/data/bi.wav"; const char MIC_FILE_PATH[128] = "/data/mic.pcm"; constexpr int32_t TYPE_OFFSET = 12; constexpr int32_t AUDIO_SAMPLE_RATE = 48000; @@ -141,6 +143,18 @@ static std::thread g_playingThread; static std::thread g_capingThread; FILE *g_micFile = nullptr; +bool g_isLatencyTesting = false; +std::vector g_playBeepTime_; +std::vector g_captureBeepTime_; +int32_t playIndex_ = 0; +int32_t recordIndex_ = 0; +bool isFirstRender = true; +bool isFirstCapture = true; +int32_t g_usPerMs = 1000; +int32_t biHighFrameTimeMs = 900000; +int64_t playLastTime_ = 0; +int64_t recordLastTime_ = 0; + static void CloseSpk(); static void CloseMic(); @@ -151,6 +165,76 @@ static int64_t GetNowTimeUs() return nowUs.count(); } +static bool IsFrameHigh(int16_t *audioData, const int32_t size, int32_t threshold) +{ + int32_t max = 0; + for (int32_t i = 0; i < size; i++) { + int16_t f = abs(audioData[i]); + if (f > max) { + max = f; + } + } + return (max >= threshold) ? true : false; +} + +static int64_t RecordBeepTime(uint8_t *base, const int32_t &sizePerFrame, bool &status) +{ + int32_t threadhold = 8000; + if (IsFrameHigh(reinterpret_cast(base), + sizePerFrame / sizeof(int16_t), threadhold) == true && + status == true) { + status = false; + return GetNowTimeUs(); + } else if (IsFrameHigh(reinterpret_cast(base), + sizePerFrame / sizeof(int16_t), threadhold) == false) { + status = true; + } + return 0; +} + +static void BeepFramPlay(uint8_t *base, const int32_t &sizePerFrame, bool &status) +{ + if (g_isLatencyTesting) { + if (playIndex_ == 0) { + std::cout << "First play time: " << GetNowTimeUs() << std::endl; + } + int64_t bt = RecordBeepTime(base, sizePerFrame, status); + if (bt != 0) { + if (GetNowTimeUs() - playLastTime_ <= biHighFrameTimeMs) { + std::cout << "Catch high frame, but not in 900ms" << std::endl; + playIndex_++; + return; + } + g_playBeepTime_.push_back(bt); + playLastTime_ = GetNowTimeUs(); + std::cout << "Play beep frame: " << playIndex_ << "play time: " << GetNowTimeUs() << std::endl; + } + } + playIndex_++; +} + +static void BeepFramCap(uint8_t *base, const int32_t &sizePerFrame, bool &status) +{ + if (g_isLatencyTesting) { + if (recordIndex_ == 0) { + std::cout << "First record time : " << GetNowTimeUs() << std::endl; + } + + int64_t bt = RecordBeepTime(base, sizePerFrame, status); + if (bt != 0 && g_captureBeepTime_.size() < g_playBeepTime_.size()) { + if (GetNowTimeUs() - recordLastTime_ <= biHighFrameTimeMs) { + std::cout << "catch high frame, but not in 900ms" << std::endl; + recordIndex_++; + return; + } + g_captureBeepTime_.push_back(bt); + recordLastTime_ = GetNowTimeUs(); + std::cout << "Capture beep frame: " << recordIndex_ << " record time : " << GetNowTimeUs() << std::endl; + } + } + recordIndex_++; +} + static int32_t GetUserInput() { int32_t res = -1; @@ -351,6 +435,7 @@ static void Play() DHLOGE("Copy render frame failed, error code %{public}d.", ret); return; } + BeepFramPlay(reinterpret_cast(frameHal.data()), RENDER_FRAME_SIZE, isFirstRender); ret = g_render->RenderFrame(frameHal, size); if (ret != DH_SUCCESS) { std::cout<<"RenderFrame failed, index: "<< g_frameIndex << ", ret: " << ret << std::endl; @@ -364,7 +449,7 @@ static void Play() std::cout << "Playing thread stopped." << std::endl; } -static void StartRender() +static void StartRender(const char *filePath) { if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { std::cout << "Speaker device is not opened, start render failed." << std::endl; @@ -386,7 +471,7 @@ static void StartRender() } g_isInitRenderData = true; } - FILE *wavFile = fopen(SPK_FILE_PATH, "rb"); + FILE *wavFile = fopen(filePath, "rb"); fread(&wavHeader, 1, headerSize, wavFile); for (int32_t i = 0; i < g_frameNum; i++) { fread(renderData[i], 1, RENDER_FRAME_SIZE, wavFile); @@ -563,6 +648,7 @@ static void Capture() std::cout << "CaptureFrame failed, ret: " << ret << std::endl; return; } + BeepFrameCapture(reinterpret_cast(data.data()), RENDER_FRAME_SIZE, isFirstCapture); size_t writeCnt = fwrite(data.data(), 1, RENDER_FRAME_SIZE, g_micFile); if (static_cast(writeCnt) != RENDER_FRAME_SIZE) { std::cout << "fwrite data failed." << std::endl; @@ -679,6 +765,27 @@ static void SetVolume() } } +static void SetMaxVolume() +{ + if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { + std::cout << "Speaker is not opened, can not set volume." << std::endl; + return; + } + std::cout << "Please input volum to set [0,15]." << std::endl; + int32_t volInt = 14; + + 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;"; + std::string volStr = std::to_string(volInt); + if (g_adapter != nullptr) { + int32_t ret = g_adapter->SetExtraParams(key, condition, volStr); + if (ret != DH_SUCCESS) { + std::cout << "Set volume failed" << std::endl; + } + } +} + static void GetVolume() { if (g_spkStatus == DeviceStatus::DEVICE_IDLE) { @@ -698,6 +805,58 @@ static void GetVolume() std::cout << "Get volume success. volume: " << vol <