1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include <benchmark/benchmark.h> 17094332d3Sopenharmony_ci#include <climits> 18094332d3Sopenharmony_ci#include <gtest/gtest.h> 19094332d3Sopenharmony_ci#include <climits> 20094332d3Sopenharmony_ci#include "osal_mem.h" 21094332d3Sopenharmony_ci#include "v4_0/iaudio_capture.h" 22094332d3Sopenharmony_ci#include "v4_0/iaudio_manager.h" 23094332d3Sopenharmony_ci 24094332d3Sopenharmony_ciusing namespace std; 25094332d3Sopenharmony_ciusing namespace testing::ext; 26094332d3Sopenharmony_cinamespace { 27094332d3Sopenharmony_cistatic const uint32_t MAX_AUDIO_ADAPTER_NUM = 5; 28094332d3Sopenharmony_ciconst int32_t BUFFER_LENTH = 1024 * 16; 29094332d3Sopenharmony_ciconst int32_t DEEP_BUFFER_CAPTURE_PERIOD_SIZE = 4 * 1024; 30094332d3Sopenharmony_ciconst int32_t MMAP_SUGGUEST_REQ_SIZE = 1920; 31094332d3Sopenharmony_ciconst int32_t MOVE_LEFT_NUM = 8; 32094332d3Sopenharmony_ciconst int32_t TEST_SAMPLE_RATE_MASK_48000 = 48000; 33094332d3Sopenharmony_ciconst int32_t TEST_CHANNEL_COUNT_STERO = 2; 34094332d3Sopenharmony_ciconst int32_t ITERATION_FREQUENCY = 100; 35094332d3Sopenharmony_ciconst int32_t REPETITION_FREQUENCY = 3; 36094332d3Sopenharmony_ci 37094332d3Sopenharmony_ciclass AudioCaptureMmapBenchmarkTest : public benchmark::Fixture { 38094332d3Sopenharmony_cipublic: 39094332d3Sopenharmony_ci struct IAudioManager *manager_ = nullptr;; 40094332d3Sopenharmony_ci struct IAudioAdapter *adapter_ = nullptr; 41094332d3Sopenharmony_ci struct IAudioCapture *mmapCapture_ = nullptr; 42094332d3Sopenharmony_ci uint32_t captureId_ = 0; 43094332d3Sopenharmony_ci char *devDescriptorName_ = nullptr; 44094332d3Sopenharmony_ci struct AudioAdapterDescriptor adapterDescs_[MAX_AUDIO_ADAPTER_NUM]; 45094332d3Sopenharmony_ci uint32_t adapterSize_ = 0; 46094332d3Sopenharmony_ci virtual void SetUp(const ::benchmark::State &state); 47094332d3Sopenharmony_ci virtual void TearDown(const ::benchmark::State &state); 48094332d3Sopenharmony_ci void InitCaptureDevDesc(struct AudioDeviceDescriptor &devDesc); 49094332d3Sopenharmony_ci void InitCaptureAttrs(struct AudioSampleAttributes &attrs); 50094332d3Sopenharmony_ci void FreeAdapterElements(struct AudioAdapterDescriptor *dataBlock, bool freeSelf); 51094332d3Sopenharmony_ci void ReleaseAllAdapterDescs(struct AudioAdapterDescriptor *descs, uint32_t descsLen); 52094332d3Sopenharmony_ci}; 53094332d3Sopenharmony_ci 54094332d3Sopenharmony_civoid AudioCaptureMmapBenchmarkTest::InitCaptureDevDesc(struct AudioDeviceDescriptor &devDesc) 55094332d3Sopenharmony_ci{ 56094332d3Sopenharmony_ci ASSERT_NE(adapterDescs_, nullptr); 57094332d3Sopenharmony_ci ASSERT_NE(adapterDescs_->ports, nullptr); 58094332d3Sopenharmony_ci 59094332d3Sopenharmony_ci devDesc.pins = (enum AudioPortPin)PIN_IN_MIC; 60094332d3Sopenharmony_ci devDescriptorName_ = strdup("cardname"); 61094332d3Sopenharmony_ci devDesc.desc = devDescriptorName_; 62094332d3Sopenharmony_ci 63094332d3Sopenharmony_ci for (uint32_t index = 0; index < adapterDescs_->portsLen; index++) { 64094332d3Sopenharmony_ci if (adapterDescs_->ports[index].dir == PORT_IN) { 65094332d3Sopenharmony_ci devDesc.portId = adapterDescs_->ports[index].portId; 66094332d3Sopenharmony_ci return; 67094332d3Sopenharmony_ci } 68094332d3Sopenharmony_ci } 69094332d3Sopenharmony_ci} 70094332d3Sopenharmony_ci 71094332d3Sopenharmony_civoid AudioCaptureMmapBenchmarkTest::InitCaptureAttrs(struct AudioSampleAttributes &attrs) 72094332d3Sopenharmony_ci{ 73094332d3Sopenharmony_ci attrs.format = AUDIO_FORMAT_TYPE_PCM_16_BIT; 74094332d3Sopenharmony_ci attrs.channelCount = TEST_CHANNEL_COUNT_STERO; 75094332d3Sopenharmony_ci attrs.sampleRate = TEST_SAMPLE_RATE_MASK_48000; 76094332d3Sopenharmony_ci attrs.interleaved = 1; 77094332d3Sopenharmony_ci attrs.type = AUDIO_MMAP_NOIRQ; 78094332d3Sopenharmony_ci attrs.period = DEEP_BUFFER_CAPTURE_PERIOD_SIZE; 79094332d3Sopenharmony_ci attrs.frameSize = AUDIO_FORMAT_TYPE_PCM_16_BIT * TEST_CHANNEL_COUNT_STERO / MOVE_LEFT_NUM; 80094332d3Sopenharmony_ci attrs.isBigEndian = false; 81094332d3Sopenharmony_ci attrs.isSignedData = true; 82094332d3Sopenharmony_ci attrs.startThreshold = DEEP_BUFFER_CAPTURE_PERIOD_SIZE / (attrs.format * attrs.channelCount / MOVE_LEFT_NUM); 83094332d3Sopenharmony_ci attrs.stopThreshold = INT_MAX; 84094332d3Sopenharmony_ci attrs.silenceThreshold = BUFFER_LENTH; 85094332d3Sopenharmony_ci} 86094332d3Sopenharmony_ci 87094332d3Sopenharmony_civoid AudioCaptureMmapBenchmarkTest::FreeAdapterElements(struct AudioAdapterDescriptor *dataBlock, bool freeSelf) 88094332d3Sopenharmony_ci{ 89094332d3Sopenharmony_ci if (dataBlock == nullptr) { 90094332d3Sopenharmony_ci return; 91094332d3Sopenharmony_ci } 92094332d3Sopenharmony_ci 93094332d3Sopenharmony_ci if (dataBlock->adapterName != nullptr) { 94094332d3Sopenharmony_ci OsalMemFree(dataBlock->adapterName); 95094332d3Sopenharmony_ci dataBlock->adapterName = nullptr; 96094332d3Sopenharmony_ci } 97094332d3Sopenharmony_ci 98094332d3Sopenharmony_ci if (dataBlock->ports != nullptr) { 99094332d3Sopenharmony_ci OsalMemFree(dataBlock->ports); 100094332d3Sopenharmony_ci } 101094332d3Sopenharmony_ci} 102094332d3Sopenharmony_ci 103094332d3Sopenharmony_civoid AudioCaptureMmapBenchmarkTest::ReleaseAllAdapterDescs(struct AudioAdapterDescriptor *descs, uint32_t descsLen) 104094332d3Sopenharmony_ci{ 105094332d3Sopenharmony_ci if (descs == nullptr || descsLen == 0) { 106094332d3Sopenharmony_ci return; 107094332d3Sopenharmony_ci } 108094332d3Sopenharmony_ci 109094332d3Sopenharmony_ci for (uint32_t i = 0; i < descsLen; i++) { 110094332d3Sopenharmony_ci FreeAdapterElements(&descs[i], false); 111094332d3Sopenharmony_ci } 112094332d3Sopenharmony_ci} 113094332d3Sopenharmony_ci 114094332d3Sopenharmony_civoid AudioCaptureMmapBenchmarkTest::SetUp(const ::benchmark::State &state) 115094332d3Sopenharmony_ci{ 116094332d3Sopenharmony_ci adapterSize_ = MAX_AUDIO_ADAPTER_NUM; 117094332d3Sopenharmony_ci manager_ = IAudioManagerGet(false); 118094332d3Sopenharmony_ci ASSERT_NE(manager_, nullptr); 119094332d3Sopenharmony_ci 120094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, manager_->GetAllAdapters(manager_, adapterDescs_, &adapterSize_)); 121094332d3Sopenharmony_ci if (adapterSize_ > MAX_AUDIO_ADAPTER_NUM) { 122094332d3Sopenharmony_ci ReleaseAllAdapterDescs(adapterDescs_, adapterSize_); 123094332d3Sopenharmony_ci ASSERT_LT(adapterSize_, MAX_AUDIO_ADAPTER_NUM); 124094332d3Sopenharmony_ci } 125094332d3Sopenharmony_ci 126094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, manager_->LoadAdapter(manager_, &adapterDescs_[0], &adapter_)); 127094332d3Sopenharmony_ci if (adapter_ == nullptr) { 128094332d3Sopenharmony_ci ReleaseAllAdapterDescs(adapterDescs_, adapterSize_); 129094332d3Sopenharmony_ci EXPECT_NE(adapter_, nullptr); 130094332d3Sopenharmony_ci } 131094332d3Sopenharmony_ci 132094332d3Sopenharmony_ci struct AudioDeviceDescriptor devDesc = {}; 133094332d3Sopenharmony_ci struct AudioSampleAttributes attrs = {}; 134094332d3Sopenharmony_ci InitCaptureDevDesc(devDesc); 135094332d3Sopenharmony_ci InitCaptureAttrs(attrs); 136094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, adapter_->CreateCapture(adapter_, &devDesc, &attrs, &mmapCapture_, &captureId_)); 137094332d3Sopenharmony_ci if (mmapCapture_ == nullptr) { 138094332d3Sopenharmony_ci (void)manager_->UnloadAdapter(manager_, adapterDescs_[0].adapterName); 139094332d3Sopenharmony_ci ReleaseAllAdapterDescs(adapterDescs_, adapterSize_); 140094332d3Sopenharmony_ci } 141094332d3Sopenharmony_ci ASSERT_NE(mmapCapture_, nullptr); 142094332d3Sopenharmony_ci} 143094332d3Sopenharmony_ci 144094332d3Sopenharmony_civoid AudioCaptureMmapBenchmarkTest::TearDown(const ::benchmark::State &state) 145094332d3Sopenharmony_ci{ 146094332d3Sopenharmony_ci ASSERT_NE(devDescriptorName_, nullptr); 147094332d3Sopenharmony_ci free(devDescriptorName_); 148094332d3Sopenharmony_ci 149094332d3Sopenharmony_ci ASSERT_NE(mmapCapture_, nullptr); 150094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, adapter_->DestroyCapture(adapter_, captureId_)); 151094332d3Sopenharmony_ci 152094332d3Sopenharmony_ci ASSERT_NE(manager_, nullptr); 153094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, manager_->UnloadAdapter(manager_, adapterDescs_[0].adapterName)); 154094332d3Sopenharmony_ci ReleaseAllAdapterDescs(adapterDescs_, adapterSize_); 155094332d3Sopenharmony_ci 156094332d3Sopenharmony_ci IAudioManagerRelease(manager_, false); 157094332d3Sopenharmony_ci} 158094332d3Sopenharmony_ci 159094332d3Sopenharmony_ciBENCHMARK_F(AudioCaptureMmapBenchmarkTest, ReqMmapBuffer)(benchmark::State &state) 160094332d3Sopenharmony_ci{ 161094332d3Sopenharmony_ci ASSERT_NE(mmapCapture_, nullptr); 162094332d3Sopenharmony_ci int32_t ret; 163094332d3Sopenharmony_ci struct AudioMmapBufferDescriptor desc = {0}; 164094332d3Sopenharmony_ci uint64_t frames = 0; 165094332d3Sopenharmony_ci struct AudioTimeStamp time = {0}; 166094332d3Sopenharmony_ci 167094332d3Sopenharmony_ci for (auto _ : state) { 168094332d3Sopenharmony_ci ret = mmapCapture_->ReqMmapBuffer(mmapCapture_, MMAP_SUGGUEST_REQ_SIZE, &desc); 169094332d3Sopenharmony_ci ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT || ret == HDF_ERR_INVALID_PARAM); 170094332d3Sopenharmony_ci 171094332d3Sopenharmony_ci ret = mmapCapture_->Start(mmapCapture_); 172094332d3Sopenharmony_ci EXPECT_EQ(ret, HDF_SUCCESS); 173094332d3Sopenharmony_ci 174094332d3Sopenharmony_ci ret = mmapCapture_->GetMmapPosition(mmapCapture_, &frames, &time); 175094332d3Sopenharmony_ci ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT || ret == HDF_ERR_INVALID_PARAM); 176094332d3Sopenharmony_ci 177094332d3Sopenharmony_ci ret = mmapCapture_->Stop(mmapCapture_); 178094332d3Sopenharmony_ci ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT); 179094332d3Sopenharmony_ci } 180094332d3Sopenharmony_ci} 181094332d3Sopenharmony_ci 182094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioCaptureMmapBenchmarkTest, ReqMmapBuffer)-> 183094332d3Sopenharmony_ci Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly(); 184094332d3Sopenharmony_ci} 185