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 "native_audiostreambuilder.h"
21 #include <native_audiocapturer.h>
22 #include <thread>
23 #include <chrono>
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 namespace AudioTestConstants {
29 constexpr int32_t FIRST_ARG_IDX = 1;
30 constexpr int32_t SECOND_ARG_IDX = 2;
31 constexpr int32_t THIRD_ARG_IDX = 3;
32 constexpr int32_t RECODER_TIME = 10000;
33 constexpr int32_t COUNTDOWN_INTERVAL = 1000;
34 constexpr int32_t CONVERT_RATE = 1000;
35 }
36
37 std::string g_filePath = "/data/data/oh_test_audio.pcm";
38 FILE* g_file = nullptr;
39 int32_t g_samplingRate = 48000;
40 int32_t g_channelCount = 2;
41
AudioCapturerOnReadData(OH_AudioCapturer * capturer,void * userData,void * buffer,int32_t bufferLen)42 static int32_t AudioCapturerOnReadData(OH_AudioCapturer* capturer,
43 void* userData,
44 void* buffer,
45 int32_t bufferLen)
46 {
47 printf("Get callback buffer, bufferLen:%d \n", bufferLen);
48 size_t count = 1;
49 if (fwrite(buffer, bufferLen, count, g_file) != count) {
50 printf("buffer fwrite err");
51 }
52
53 return 0;
54 }
55
SleepWaitRecoder(bool * stop)56 void SleepWaitRecoder(bool* stop)
57 {
58 std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::RECODER_TIME));
59 *stop = true;
60 }
61
RecorderTest(char * argv[])62 void RecorderTest(char *argv[])
63 {
64 OH_AudioStream_Result ret;
65
66 // 1. create builder
67 OH_AudioStreamBuilder* builder;
68 OH_AudioStream_Type type = AUDIOSTREAM_TYPE_CAPTURER;
69 ret = OH_AudioStreamBuilder_Create(&builder, type);
70 printf("create builder: %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
76 OH_AudioCapturer_Callbacks callbacks;
77 callbacks.OH_AudioCapturer_OnReadData = AudioCapturerOnReadData;
78 ret = OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr);
79 printf("setcallback: %d \n", ret);
80
81 // 3. create OH_AudioCapturer
82 OH_AudioCapturer* audioCapturer;
83 ret = OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer);
84 printf("create capturer client, ret: %d \n", ret);
85
86 // 4. start
87 ret = OH_AudioCapturer_Start(audioCapturer);
88 printf("start ret: %d \n", ret);
89
90 bool stop = false;
91 std::thread stopthread(SleepWaitRecoder, &stop);
92 stopthread.detach();
93
94 int timeLeft = AudioTestConstants::RECODER_TIME;
95 while (!stop) {
96 printf("Recording audio is in the countdown ... %d s \n", timeLeft / AudioTestConstants::CONVERT_RATE);
97 std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::COUNTDOWN_INTERVAL));
98 timeLeft = timeLeft - AudioTestConstants::COUNTDOWN_INTERVAL;
99 }
100
101 // 5. stop and release client
102 ret = OH_AudioCapturer_Stop(audioCapturer);
103 printf("stop ret: %d \n", ret);
104 ret = OH_AudioCapturer_Release(audioCapturer);
105 printf("release ret: %d \n", ret);
106
107 // 6. destroy the builder
108 ret = OH_AudioStreamBuilder_Destroy(builder);
109 printf("destroy builder ret: %d \n", ret);
110 }
111
112
main(int argc,char * argv[])113 int main(int argc, char *argv[])
114 {
115 if ((argv == nullptr) || (argc <= AudioTestConstants::THIRD_ARG_IDX)) {
116 printf("input parms wrong. input format: samplingRate channelCount \n");
117 printf("input demo: ./oh_audio_capturer_test 48000 2 \n");
118 return 0;
119 }
120
121 printf("argc=%d ", argc);
122 printf("argv[1]=%s ", argv[AudioTestConstants::FIRST_ARG_IDX]);
123 printf("argv[2]=%s ", argv[AudioTestConstants::SECOND_ARG_IDX]);
124 printf("argv[3]=%s \n", argv[AudioTestConstants::THIRD_ARG_IDX]);
125
126 g_samplingRate = atoi(argv[AudioTestConstants::FIRST_ARG_IDX]);
127 g_channelCount = atoi(argv[AudioTestConstants::SECOND_ARG_IDX]);
128
129 g_file = fopen(g_filePath.c_str(), "wb");
130 if (g_file == nullptr) {
131 printf("OHAudioCapturerTest: Unable to open file \n");
132 return 0;
133 }
134
135 RecorderTest(argv);
136
137 fclose(g_file);
138 g_file = nullptr;
139 return 0;
140 }
141 #ifdef __cplusplus
142 }
143 #endif
144