• 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 <cinttypes>
21 #include "native_audiostreambuilder.h"
22 #include <native_audiocapturer.h>
23 #include <thread>
24 #include <chrono>
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 RECODER_TIME = 10000;
34     constexpr int32_t COUNTDOWN_INTERVAL = 1000;
35     constexpr int32_t CONVERT_RATE = 1000;
36 }
37 
38 std::string g_filePath = "/data/data/oh_test_audio.pcm";
39 FILE* g_file = nullptr;
40 int32_t g_samplingRate = 48000;
41 int32_t g_channelCount = 2;
42 
AudioCapturerOnReadData(OH_AudioCapturer * capturer,void * userData,void * buffer,int32_t bufferLen)43 static int32_t AudioCapturerOnReadData(OH_AudioCapturer* capturer,
44     void* userData,
45     void* buffer,
46     int32_t bufferLen)
47 {
48     printf("Get callback buffer, bufferLen:%d  \n", bufferLen);
49     size_t count = 1;
50     if (fwrite(buffer, bufferLen, count, g_file) != count) {
51         printf("buffer fwrite err");
52     }
53 
54     return 0;
55 }
56 
AudioErrCallback(OH_AudioCapturer * renderer,void * userData,OH_AudioStream_Result error)57 static int32_t AudioErrCallback(OH_AudioCapturer* renderer,
58     void* userData,
59     OH_AudioStream_Result error)
60 {
61     printf("recv err : code %d \n", error);
62     return 0;
63 }
64 
AudioInterruptCallback(OH_AudioCapturer * renderer,void * userData,OH_AudioInterrupt_ForceType type,OH_AudioInterrupt_Hint hint)65 static int32_t AudioInterruptCallback(OH_AudioCapturer* renderer,
66     void* userData,
67     OH_AudioInterrupt_ForceType type,
68     OH_AudioInterrupt_Hint hint)
69 {
70     printf("recv interrupt event : type: %d hint: %d \n", type, hint);
71     return 0;
72 }
73 
AudioEventCallback(OH_AudioCapturer * renderer,void * userData,OH_AudioStream_Event event)74 static int32_t AudioEventCallback(OH_AudioCapturer* renderer,
75     void* userData,
76     OH_AudioStream_Event event)
77 {
78     printf("recv event : event: %d \n", event);
79     return 0;
80 }
81 
SleepWaitRecoder(bool * stop)82 void SleepWaitRecoder(bool* stop)
83 {
84     std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::RECODER_TIME));
85     *stop = true;
86 }
87 
RecorderTest(char * argv[])88 void RecorderTest(char *argv[])
89 {
90     OH_AudioStream_Result ret;
91 
92     // 1. create builder
93     OH_AudioStreamBuilder* builder;
94     OH_AudioStream_Type type = AUDIOSTREAM_TYPE_CAPTURER;
95     ret = OH_AudioStreamBuilder_Create(&builder, type);
96     printf("create builder: %d \n", ret);
97 
98     // 2. set params and callbacks
99     OH_AudioStreamBuilder_SetSamplingRate(builder, g_samplingRate);
100     OH_AudioStreamBuilder_SetChannelCount(builder, g_channelCount);
101 
102     OH_AudioCapturer_Callbacks callbacks;
103     callbacks.OH_AudioCapturer_OnReadData = AudioCapturerOnReadData;
104     callbacks.OH_AudioCapturer_OnError = AudioErrCallback;
105     callbacks.OH_AudioCapturer_OnInterruptEvent = AudioInterruptCallback;
106     callbacks.OH_AudioCapturer_OnStreamEvent = AudioEventCallback;
107     ret = OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr);
108     printf("setcallback: %d \n", ret);
109 
110     // 3. create OH_AudioCapturer
111     OH_AudioCapturer* audioCapturer;
112     ret = OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer);
113     printf("create capturer client, ret: %d \n", ret);
114 
115     // 4. start
116     ret = OH_AudioCapturer_Start(audioCapturer);
117     printf("start ret: %d \n", ret);
118 
119     bool stop = false;
120     std::thread stopthread(SleepWaitRecoder, &stop);
121     stopthread.detach();
122 
123     int timeLeft = AudioTestConstants::RECODER_TIME;
124     int64_t framePosition = 0;
125     int64_t timestamp = 0;
126     while (!stop) {
127         OH_AudioCapturer_GetTimestamp(audioCapturer, CLOCK_MONOTONIC, &framePosition, &timestamp);
128         printf("Recording audio is in the countdown ... %d s (framePosition: %" PRId64 ")((timestamp: %" PRId64 "))\n",
129             timeLeft / AudioTestConstants::CONVERT_RATE, framePosition, timestamp);
130         std::this_thread::sleep_for(std::chrono::milliseconds(AudioTestConstants::COUNTDOWN_INTERVAL));
131         timeLeft  = timeLeft - AudioTestConstants::COUNTDOWN_INTERVAL;
132     }
133 
134     // 5. stop and release client
135     ret = OH_AudioCapturer_Stop(audioCapturer);
136     printf("stop ret: %d \n", ret);
137     ret = OH_AudioCapturer_Release(audioCapturer);
138     printf("release ret: %d \n", ret);
139 
140     // 6. destroy the builder
141     ret = OH_AudioStreamBuilder_Destroy(builder);
142     printf("destroy builder ret: %d \n", ret);
143 }
144 
145 
main(int argc,char * argv[])146 int main(int argc, char *argv[])
147 {
148     if ((argv == nullptr) || (argc <= AudioTestConstants::THIRD_ARG_IDX)) {
149         printf("input parms wrong. input format: samplingRate channelCount \n");
150         printf("input demo: ./oh_audio_capturer_test 48000 2 \n");
151         return 0;
152     }
153 
154     printf("argc=%d ", argc);
155     printf("argv[1]=%s ", argv[AudioTestConstants::FIRST_ARG_IDX]);
156     printf("argv[2]=%s ", argv[AudioTestConstants::SECOND_ARG_IDX]);
157     printf("argv[3]=%s \n", argv[AudioTestConstants::THIRD_ARG_IDX]);
158 
159     g_samplingRate = atoi(argv[AudioTestConstants::FIRST_ARG_IDX]);
160     g_channelCount = atoi(argv[AudioTestConstants::SECOND_ARG_IDX]);
161 
162     g_file = fopen(g_filePath.c_str(), "wb");
163     if (g_file == nullptr) {
164         printf("OHAudioCapturerTest: Unable to open file \n");
165         return 0;
166     }
167 
168     RecorderTest(argv);
169 
170     fclose(g_file);
171     g_file = nullptr;
172     return 0;
173 }
174 #ifdef __cplusplus
175 }
176 #endif
177