1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cinttypes>
17 #include <thread>
18 #include <stdint.h>
19 #include <time.h>
20
21 #include "audio_log.h"
22 #include "audio_utils.h"
23 #include "fast/fast_audio_renderer_sink.h"
24 #include "pcm2wav.h"
25
26 using namespace std;
27 using namespace OHOS;
28 using namespace OHOS::AudioStandard;
29
30 class AudioHdiDeviceTest {
31 public:
RenderFrameFromFile()32 void RenderFrameFromFile()
33 {
34 if (hdiRenderSink_ == nullptr) {
35 AUDIO_ERR_LOG("RenderFrameFromFile hdiRenderSink_ null");
36 return;
37 }
38
39 size_t tempBufferSize = hdiRenderSink_->eachReadFrameSize_ * hdiRenderSink_->frameSizeInByte_;
40 char *buffer = (char *)malloc(tempBufferSize);
41 if (buffer == nullptr) {
42 AUDIO_ERR_LOG("AudioHdiDeviceTest: failed to malloc");
43 return;
44 }
45
46 uint64_t frameCount = 0;
47 int64_t timeSec = 0;
48 int64_t timeNanoSec = 0;
49
50 int64_t periodMicrTime = 5000; // 5ms
51 int64_t timeStampNext = GetNowTimeUs();
52 int64_t timeToSleep = periodMicrTime;
53
54 uint64_t written = 0;
55 int32_t ret = 0;
56 while (!stopThread && !feof(wavFile)) {
57 fread(buffer, 1, tempBufferSize, wavFile);
58 ret = hdiRenderSink_->RenderFrame(*buffer, tempBufferSize, written);
59
60 hdiRenderSink_->GetMmapHandlePosition(frameCount, timeSec, timeNanoSec);
61
62 timeToSleep = periodMicrTime - (GetNowTimeUs() - timeStampNext);
63 if (timeToSleep > 0 && timeToSleep < periodMicrTime) {
64 usleep(timeToSleep);
65 } else {
66 usleep(periodMicrTime);
67 }
68
69 timeStampNext = GetNowTimeUs();
70 }
71 free(buffer);
72 }
73
InitHdiRender()74 bool InitHdiRender()
75 {
76 hdiRenderSink_ = FastAudioRendererSink::GetInstance();
77 AudioSinkAttr attr = {};
78 attr.adapterName = "primary";
79 attr.sampleRate = 48000; // 48000hz
80 attr.channel = 2;
81 attr.format = AUDIO_FORMAT_PCM_16_BIT; // 0x2u
82
83 hdiRenderSink_->Init(attr);
84
85 return true;
86 }
87
StartHdiRender()88 void StartHdiRender()
89 {
90 if (hdiRenderSink_ == nullptr) {
91 AUDIO_ERR_LOG("StartHdiRender hdiRenderSink_ null");
92 return;
93 }
94
95 hdiRenderSink_->Start();
96 int32_t ret = hdiRenderSink_->SetVolume(0.5, 0.5);
97 AUDIO_INFO_LOG("AudioHdiDeviceTest set volume to 0.5, ret %{public}d", ret);
98
99 timeThread_ = make_unique<thread>(&AudioHdiDeviceTest::RenderFrameFromFile, this);
100
101 int32_t sleepTime = 7;
102
103 sleep(sleepTime);
104
105 stopThread = true;
106 timeThread_->join();
107 hdiRenderSink_->Stop();
108 hdiRenderSink_->DeInit();
109 }
110
TestPlayback(int argc,char * argv[])111 bool TestPlayback(int argc, char *argv[])
112 {
113 AUDIO_INFO_LOG("TestPlayback in");
114
115 wav_hdr wavHeader;
116 size_t headerSize = sizeof(wav_hdr);
117 char *inputPath = argv[1];
118 char path[PATH_MAX + 1] = {0x00};
119 if ((strlen(inputPath) > PATH_MAX) || (realpath(inputPath, path) == nullptr)) {
120 AUDIO_ERR_LOG("Invalid path");
121 return false;
122 }
123 AUDIO_INFO_LOG("AudioHdiDeviceTest: path = %{public}s", path);
124 wavFile = fopen(path, "rb");
125 if (wavFile == nullptr) {
126 AUDIO_INFO_LOG("AudioHdiDeviceTest: Unable to open wave file");
127 return false;
128 }
129 size_t bytesRead = fread(&wavHeader, 1, headerSize, wavFile);
130 AUDIO_INFO_LOG("AudioHdiDeviceTest: Header Read in bytes %{public}zu", bytesRead);
131
132 InitHdiRender();
133 StartHdiRender();
134
135 return true;
136 }
137 private:
138 FastAudioRendererSink *hdiRenderSink_ = nullptr;
139 unique_ptr<thread> timeThread_ = nullptr;
140 bool stopThread = false;
141 FILE* wavFile = nullptr;
142 };
143
main(int argc,char * argv[])144 int main(int argc, char *argv[])
145 {
146 AUDIO_INFO_LOG("AudioHdiDeviceTest: Render test in");
147
148 if (argv == nullptr) {
149 AUDIO_ERR_LOG("AudioHdiDeviceTest: argv is null");
150 return 0;
151 }
152 int32_t argsCountTwo_ = 2;
153 int32_t argsCountThree_ = 3;
154 if (argc < argsCountTwo_ || argc >= argsCountThree_) {
155 AUDIO_ERR_LOG("AudioHdiDeviceTest: incorrect argc. Enter 2 args");
156 return 0;
157 }
158
159 AUDIO_INFO_LOG("AudioHdiDeviceTest: argc=%{public}d", argc);
160 AUDIO_INFO_LOG("file path argv[1]=%{public}s", argv[1]);
161
162 AudioHdiDeviceTest testObj;
163 bool ret = testObj.TestPlayback(argc, argv);
164
165 return ret;
166 }
167