• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <iostream>
17 #include <cstdint>
18 #include <cstdio>
19 #include <cstring>
20 #include <thread>
21 #include <chrono>
22 #include <ctime>
23 #include "common/native_audiostreambuilder.h"
24 #include "native_audiorenderer.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 namespace AudioTestConstants {
30     constexpr int32_t FIRST_ARG_IDX = 1;
31     constexpr int32_t SECOND_ARG_IDX = 2;
32     constexpr int32_t THIRD_ARG_IDX = 3;
33     constexpr int32_t FOUR_ARG_IDX = 4;
34     constexpr int32_t WAIT_INTERVAL = 1000;
35 }
36 
37 std::string g_filePath = "/data/data/oh_test_audio.pcm";
38 FILE* g_file = nullptr;
39 bool g_readEnd = false;
40 int32_t g_samplingRate = 48000;
41 int32_t g_channelCount = 2;
42 int32_t g_latencyMode = 0;
43 
AudioRendererOnWriteData(OH_AudioRenderer * capturer,void * userData,void * buffer,int32_t bufferLen)44 static int32_t AudioRendererOnWriteData(OH_AudioRenderer* capturer,
45     void* userData,
46     void* buffer,
47     int32_t bufferLen)
48 {
49     size_t readCount = fread(buffer, bufferLen, 1, g_file);
50     if (!readCount) {
51         if (ferror(g_file)) {
52             printf("Error reading myfile");
53         } else if (feof(g_file)) {
54             printf("EOF found");
55             g_readEnd = true;
56         }
57     }
58 
59     return 0;
60 }
61 
PlayerTest(char * argv[])62 void PlayerTest(char *argv[])
63 {
64     OH_AudioStream_Result ret;
65 
66     // 1. create builder
67     OH_AudioStreamBuilder* builder;
68     OH_AudioStream_Type type = AUDIOSTREAM_TYPE_RENDERER;
69     ret = OH_AudioStreamBuilder_Create(&builder, type);
70     printf("createcallback ret: %d \n", ret);
71 
72     // 2. set params and callbacks
73     OH_AudioStreamBuilder_SetSamplingRate(builder, g_samplingRate);
74     OH_AudioStreamBuilder_SetChannelCount(builder, g_channelCount);
75     OH_AudioStreamBuilder_SetLatencyMode(builder, (OH_AudioStream_LatencyMode)g_latencyMode);
76 
77     OH_AudioRenderer_Callbacks callbacks;
78     callbacks.OH_AudioRenderer_OnWriteData = AudioRendererOnWriteData;
79     ret = OH_AudioStreamBuilder_SetRendererCallback(builder, callbacks, nullptr);
80     printf("setcallback ret: %d \n", ret);
81 
82     // 3. create OH_AudioRenderer
83     OH_AudioRenderer* audioRenderer;
84     ret = OH_AudioStreamBuilder_GenerateRenderer(builder, &audioRenderer);
85     printf("create renderer client, ret: %d \n", ret);
86 
87     // 4. start
88     ret = OH_AudioRenderer_Start(audioRenderer);
89     printf("start ret: %d \n", ret);
90     int32_t frameSize;
91     OH_AudioRenderer_GetFrameSizeInCallback(audioRenderer, &frameSize);
92     printf("framesize: %d \n", frameSize);
93 
94     int timer = 0;
95     while (!g_readEnd) {
96         std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::WAIT_INTERVAL));
97         int64_t frames;
98         OH_AudioRenderer_GetFramesWritten(audioRenderer, &frames);
99         printf("Wait for the audio to finish playing.(..%d s) frames:%ld\n", ++timer, frames);
100         int64_t framePosition;
101         int64_t timestamp;
102         OH_AudioRenderer_GetTimestamp(audioRenderer, CLOCK_MONOTONIC, &framePosition, &timestamp);
103         printf("framePosition %ld timestamp:%ld\n", framePosition, timestamp);
104     }
105     // 5. stop and release client
106     ret = OH_AudioRenderer_Stop(audioRenderer);
107     printf("stop ret: %d \n", ret);
108     ret = OH_AudioRenderer_Release(audioRenderer);
109     printf("release ret: %d \n", ret);
110 
111     // 6. destroy the builder
112     ret = OH_AudioStreamBuilder_Destroy(builder);
113     printf("destroy builder ret: %d \n", ret);
114 }
115 
main(int argc,char * argv[])116 int main(int argc, char *argv[])
117 {
118     printf("start \n");
119     if ((argv == nullptr) || (argc < AudioTestConstants::THIRD_ARG_IDX)) {
120         printf("input parms wrong. input format: filePath samplingRate channelCount latencyMode\n");
121         printf("input demo: ./oh_audio_renderer_test ./oh_test_audio.pcm 48000 2 1 \n");
122         return 0;
123     }
124     printf("argc=%d ", argc);
125     printf("file path =%s ", argv[AudioTestConstants::FIRST_ARG_IDX]);
126     printf("sample rate =%s ", argv[AudioTestConstants::SECOND_ARG_IDX]);
127     printf("channel count =%s \n", argv[AudioTestConstants::THIRD_ARG_IDX]);
128     printf("latency mode =%s \n", argv[AudioTestConstants::FOUR_ARG_IDX]);
129 
130     g_filePath = argv[AudioTestConstants::FIRST_ARG_IDX];
131     g_samplingRate = atoi(argv[AudioTestConstants::SECOND_ARG_IDX]);
132     g_channelCount = atoi(argv[AudioTestConstants::THIRD_ARG_IDX]);
133     g_latencyMode = atoi(argv[AudioTestConstants::FOUR_ARG_IDX]);
134 
135     printf("filePATH: %s \n", g_filePath.c_str());
136 
137     g_file = fopen(g_filePath.c_str(), "rb");
138     if (g_file == nullptr) {
139         printf("OHAudioRendererTest: Unable to open file \n");
140         return 0;
141     }
142 
143     PlayerTest(argv);
144 
145     fclose(g_file);
146     g_file = nullptr;
147     return 0;
148 }
149 
150 #ifdef __cplusplus
151 }
152 #endif
153