blob: 403db74c01719816d8c25cafd183b7e23d9bba7a [file] [log] [blame]
b.liu1c1c7212023-12-22 16:35:27 +08001#include <sys/stat.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <fcntl.h>
5#include <errno.h>
b.liu62240ee2024-11-07 17:52:45 +08006#include <stdlib.h>
b.liu1c1c7212023-12-22 16:35:27 +08007
8#include "mbtk_log.h"
9#include "mbtk_audio_internal.h"
10
11#define WAV_PLAY_BUFF 2048
12
b.liub21bd8d2023-12-28 19:07:21 +080013typedef struct {
14 const unsigned char *pcm_data;
15 int data_size;
16} audio_buff_t;
17
b.liu1c1c7212023-12-22 16:35:27 +080018static int wav_play_fd = -1;
19static audio_play_state_enum play_state = AUDIO_PLAY_STATE_STOP;
20static pthread_cond_t play_cond;
21static pthread_mutex_t play_mutex;
22static pthread_t play_thread_play;
23
24static int wav_recorder_fd = -1;
25static struct wav_header recorder_header;
26static uint32 recorver_data_count = 0;
27
b.liub21bd8d2023-12-28 19:07:21 +080028static audio_buff_t audio_buff;
29
b.liu62240ee2024-11-07 17:52:45 +080030static int current_loopback_state = 0;
31static int current_loopback_device = -1;
32static mbtk_audio_service_error_cb_f service_error_cb = NULL;
33
b.liu1c1c7212023-12-22 16:35:27 +080034static void audio_play_thread(void *arg)
35{
36 int rc, len, frames = 0;
37 char buf[WAV_PLAY_BUFF];
38
39 play_state = AUDIO_PLAY_STATE_RUNNING;
40 pthread_mutex_init(&play_mutex, NULL);
41 pthread_cond_init(&play_cond, NULL);
42
b.liub21bd8d2023-12-28 19:07:21 +080043 int data_send = 0;
b.liu1c1c7212023-12-22 16:35:27 +080044 while (TRUE) {
45 /* Playback loop */
46 pthread_mutex_lock(&play_mutex);
47 if(play_state == AUDIO_PLAY_STATE_STOP) {
48 LOGD("Stop play...");
49 pthread_mutex_unlock(&play_mutex);
50 break;
51 } else if(play_state == AUDIO_PLAY_STATE_PAUSE) {
52 pthread_cond_wait(&play_cond, &play_mutex);
53 pthread_mutex_unlock(&play_mutex);
54 continue;
55 } else {
56 pthread_mutex_unlock(&play_mutex);
57 }
58
59 memset(buf, 0x00, sizeof(buf));
b.liub21bd8d2023-12-28 19:07:21 +080060 if(wav_play_fd > 0) { // Play file.
61 len = read(wav_play_fd, buf, WAV_PLAY_BUFF);
62 if (len == -1) {
63 LOGE("%s: error reading from file", __FUNCTION__);
64 goto thread_end;
65 }
b.liu1c1c7212023-12-22 16:35:27 +080066
b.liub21bd8d2023-12-28 19:07:21 +080067 if (len == 0) {
68 /* reached EOF */
69 LOGE("%s: Read wav file end.", __FUNCTION__);
70 break;
71 }
72 } else { // Play buffer.
73 if(data_send >= audio_buff.data_size) {
74 /* reached EOF */
75 LOGE("%s: Read buffer end.", __FUNCTION__);
76 break;
77 }
78 if(audio_buff.data_size - data_send >= WAV_PLAY_BUFF) {
79 memcpy(buf, audio_buff.pcm_data + data_send, WAV_PLAY_BUFF);
80 len = WAV_PLAY_BUFF;
81 } else {
82 memcpy(buf, audio_buff.pcm_data + data_send, audio_buff.data_size - data_send);
83 len = audio_buff.data_size - data_send;
84 }
85 data_send += len;
b.liu1c1c7212023-12-22 16:35:27 +080086 }
87
88 if((rc = mbtk_audio_pcm_play_data_send(buf, len)) < len) {
89 LOGE("Send data %d/%d", rc, len);
90 goto thread_end;
91 }
92
93 LOGD("%s: No.%d frame playback", __FUNCTION__, ++frames);
94 }
95
96 play_state = AUDIO_PLAY_STATE_STOP;
97
98thread_end:
99 pthread_mutex_destroy(&play_mutex);
100 pthread_cond_destroy(&play_cond);
101 mbtk_audio_pcm_play_stop();
102
b.liub21bd8d2023-12-28 19:07:21 +0800103 if(wav_play_fd > 0) {
104 if (close(wav_play_fd))
105 LOGE("%s: error closing file", __FUNCTION__);
106 }
b.liu1c1c7212023-12-22 16:35:27 +0800107
108 wav_play_fd = -1;
109 LOGD("%s: finished pcm playback.", __FUNCTION__);
110 return;
111}
112
113static void audio_recorder_cb(void *data, uint32 data_len)
114{
115 if(data_len > 0) {
116 LOGD("Recorver data:%d, count:%d", data_len, recorver_data_count);
117 if (write(wav_recorder_fd, data, data_len) < data_len) {
118 LOGE("%s: error writing to file!", __FUNCTION__);
119 }
120 recorver_data_count += data_len;
121 } else {
122 LOGD("Recorver data end.");
123 recorder_header.data_sz = recorver_data_count;
124 recorder_header.riff_sz = recorder_header.data_sz + sizeof(recorder_header) - 8;
125 lseek(wav_recorder_fd, 0, SEEK_SET);
b.liu62240ee2024-11-07 17:52:45 +0800126
127 if(sizeof(struct wav_header) == write(wav_recorder_fd, &recorder_header, sizeof(struct wav_header))) {
128 // Do nothting.
129 }
b.liu1c1c7212023-12-22 16:35:27 +0800130
131 close(wav_recorder_fd);
132 wav_recorder_fd = -1;
133 }
134}
135
b.liu62240ee2024-11-07 17:52:45 +0800136int mbtk_register_error_callback(mbtk_audio_service_error_cb_f cb)
137{
138 if (cb == NULL) {
139 LOGE("Error: Callback function is NULL.");
140 return -1;
141 }
142 service_error_cb = cb;
143 LOGD("Callback function registered successfully.");
144 return 0;
145}
146
b.liu1c1c7212023-12-22 16:35:27 +0800147int mbtk_audio_wav_init()
148{
149 //mbtk_log_init("radio", "MBTK_AUDIO");
150 return mbtk_audio_pcm_init();
151}
152
153int mbtk_audio_wav_play_start(const void *wav_file)
154{
155 struct stat st;
156 const char *path = (const char *)wav_file;
157 struct riff_wave_header riff_wave_header;
158 struct chunk_header chunk_header;
159 struct chunk_fmt chunk_fmt = {0};
160 unsigned int more_chunks = 1;
161
162 if(play_state != AUDIO_PLAY_STATE_STOP) {
163 LOGW("Audio is playing...");
164 return -1;
165 }
166
167 /* Check and open source file */
168 if (access(path, F_OK) || stat(path, &st)) {
169 LOGE("%s: error reading from file %s", __FUNCTION__, path);
170 return -1;
171 }
172
173 if (!st.st_size) {
174 LOGE("%s: empty file %s", __FUNCTION__, path);
175 return -1;
176 }
177
178 wav_play_fd = open(path, O_RDONLY);
179 if (wav_play_fd < 0) {
180 LOGE("%s: error opening file %s", __FUNCTION__, path);
181 return -1;
182 }
183
b.liu62240ee2024-11-07 17:52:45 +0800184 if(sizeof(riff_wave_header) != read(wav_play_fd, &riff_wave_header, sizeof(riff_wave_header))) {
185 LOGE("%s: read riff_wave_header fail.", __FUNCTION__);
186 return -1;
187 }
188
b.liu1c1c7212023-12-22 16:35:27 +0800189 if ((riff_wave_header.riff_id != ID_RIFF) || (riff_wave_header.wave_id != ID_WAVE)) {
190 LOGE("Error: '%s' is not a riff/wave file", path);
191 close(wav_play_fd);
192 return -1;
193 }
194
195 do {
b.liu62240ee2024-11-07 17:52:45 +0800196 if(sizeof(chunk_header) != read(wav_play_fd, &chunk_header, sizeof(chunk_header))) {
197 LOGE("%s: read chunk_header fail.", __FUNCTION__);
198 return -1;
199 }
b.liu1c1c7212023-12-22 16:35:27 +0800200
201 switch (chunk_header.id) {
202 case ID_FMT:
b.liu62240ee2024-11-07 17:52:45 +0800203 if(sizeof(chunk_fmt) != read(wav_play_fd, &chunk_fmt, sizeof(chunk_fmt))) {
204 LOGE("%s: read chunk_fmt fail.", __FUNCTION__);
205 return -1;
206 }
b.liu1c1c7212023-12-22 16:35:27 +0800207 /* If the format header is larger, skip the rest */
208 if (chunk_header.sz > sizeof(chunk_fmt))
209 lseek(wav_play_fd, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
210 break;
211 case ID_DATA:
212 /* Stop looking for chunks */
213 more_chunks = 0;
214 break;
215 default:
216 /* Unknown chunk, skip bytes */
217 lseek(wav_play_fd, chunk_header.sz, SEEK_CUR);
218 }
219 } while (more_chunks);
220
221 //Support 8k/16k & mono wave file
222 if (((chunk_fmt.sample_rate != 8000) && (chunk_fmt.sample_rate != 16000))
223 || (chunk_fmt.num_channels != 1) ) {
224 LOGD("%s: error wave file:sample_rate = %d, num_channels = %d!!",
225 __FUNCTION__,chunk_fmt.sample_rate, chunk_fmt.num_channels);
226 close(wav_play_fd);
227 return -1;
228 }
229
230 LOGE("%s: success open wave file:%s, sample_rate = %d, num_channels = %d.",
231 __FUNCTION__, path, chunk_fmt.sample_rate, chunk_fmt.num_channels);
232
233 if ((8000 == chunk_fmt.sample_rate) && (1 == chunk_fmt.num_channels)) {
234 mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_8000);
235 } else if ((16000 == chunk_fmt.sample_rate) && (1 == chunk_fmt.num_channels)) {
236 mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_16000);
237 }
238
239 if (mbtk_audio_pcm_play_start()) {
240 LOGE("%s: error opening output device.", __FUNCTION__);
241 return -1;
242 }
243
244 LOGD("Start play wav file...");
245
246 pthread_attr_t thread_attr;
247 pthread_attr_init(&thread_attr);
248 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
249 {
250 LOGE("pthread_attr_setdetachstate() fail.");
251 return -1;
252 }
253
254 if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, NULL) < 0) {
255 LOGE("%s: error creating thread_play!", __FUNCTION__);
256 return -1;
257 }
258
259 return 0;
260}
261
b.liub21bd8d2023-12-28 19:07:21 +0800262int mbtk_audio_wav_stream_play_start(const unsigned char *pcm_data, int data_size, int sample_rate, int num_channels)
263{
264 if(play_state != AUDIO_PLAY_STATE_STOP) {
265 LOGW("Audio is playing...");
266 return -1;
267 }
268
269 //Support 8k/16k & mono wave file
270 if (((sample_rate != 8000) && (sample_rate != 16000))
271 || (num_channels != 1) ) {
272 LOGD("%s: error wave file:sample_rate = %d, num_channels = %d!!",
273 __FUNCTION__,sample_rate, num_channels);
274 return -1;
275 }
276
277 LOGE("%s: success open wave stream, sample_rate = %d, num_channels = %d.",
278 __FUNCTION__, sample_rate, num_channels);
279
280 if ((8000 == sample_rate) && (1 == num_channels)) {
281 mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_8000);
282 } else if ((16000 == sample_rate) && (1 == num_channels)) {
283 mbtk_audio_pcm_sample_rate_set(MBTK_AUDIO_SAMPLE_RATE_16000);
284 }
285
286 if (mbtk_audio_pcm_play_start()) {
287 LOGE("%s: error opening output device.", __FUNCTION__);
288 return -1;
289 }
290
291 audio_buff.pcm_data = pcm_data;
292 audio_buff.data_size = data_size;
293 LOGD("Start play wav stream...");
294
295 pthread_attr_t thread_attr;
296 pthread_attr_init(&thread_attr);
297 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
298 {
299 LOGE("pthread_attr_setdetachstate() fail.");
300 return -1;
301 }
302
303 if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, NULL) < 0) {
304 LOGE("%s: error creating thread_play!", __FUNCTION__);
305 return -1;
306 }
307
308 return 0;
309}
310
311
b.liu1c1c7212023-12-22 16:35:27 +0800312int mbtk_audio_wav_play_pause()
313{
314 int result = 0;
315 pthread_mutex_lock(&play_mutex);
316 if(play_state == AUDIO_PLAY_STATE_RUNNING) {
317 play_state = AUDIO_PLAY_STATE_PAUSE;
318 } else {
b.liub21bd8d2023-12-28 19:07:21 +0800319 // result = -1;
b.liu1c1c7212023-12-22 16:35:27 +0800320 LOGW("Audio state : %d", play_state);
321 }
322 pthread_mutex_unlock(&play_mutex);
323 return result;
324}
325
326int mbtk_audio_wav_play_resume()
327{
328 int result = 0;
329 pthread_mutex_lock(&play_mutex);
330 if(play_state == AUDIO_PLAY_STATE_PAUSE) {
331 play_state = AUDIO_PLAY_STATE_RUNNING;
332 pthread_cond_signal(&play_cond);
333 } else {
b.liub21bd8d2023-12-28 19:07:21 +0800334 // result = -1;
b.liu1c1c7212023-12-22 16:35:27 +0800335 LOGW("Audio state : %d", play_state);
336 }
337 pthread_mutex_unlock(&play_mutex);
338 return result;
339}
340
341
342int mbtk_audio_wav_play_stop()
343{
344 int result = 0;
345 pthread_mutex_lock(&play_mutex);
346 if(play_state == AUDIO_PLAY_STATE_PAUSE || play_state == AUDIO_PLAY_STATE_RUNNING) {
347 if(play_state == AUDIO_PLAY_STATE_PAUSE) {
348 pthread_cond_signal(&play_cond);
349 }
350 play_state = AUDIO_PLAY_STATE_STOP;
351 pthread_mutex_unlock(&play_mutex);
352
353 LOGD("Waitting play thread exit...");
354 if (pthread_join(play_thread_play, NULL)) {
355 LOGE("error join thread_play!");
356 // abort();
357 }
358 LOGD("Play thread exit success.");
359 } else {
360 pthread_mutex_unlock(&play_mutex);
b.liub21bd8d2023-12-28 19:07:21 +0800361 // result = -1;
b.liu1c1c7212023-12-22 16:35:27 +0800362 LOGW("Audio state : %d", play_state);
363 }
364
365 return result;
366}
367
368int mbtk_audio_wav_recorder_start(const void *wav_file, mbtk_audio_sample_rate_enum sample_rate)
369{
b.liu1c1c7212023-12-22 16:35:27 +0800370 const char *path = (const char *)wav_file;
371
372 LOGD("wav_file is %s.", path);
373 if(wav_recorder_fd > 0) {
374 LOGW("Audio is recorder...");
375 }
376
377 memset(&recorder_header, 0x0, sizeof(struct wav_header));
378 recorder_header.riff_id = ID_RIFF;
379 recorder_header.riff_sz = 0;
380 recorder_header.riff_fmt = ID_WAVE;
381 recorder_header.fmt_id = ID_FMT;
382 recorder_header.fmt_sz = 16;
383 recorder_header.audio_format = 1; //FORMAT_PCM;
384 recorder_header.num_channels = 1; //Modem ONLY support mono recording
385 recorder_header.sample_rate = (sample_rate == MBTK_AUDIO_SAMPLE_RATE_8000 ? 8000 : 16000);
386 recorder_header.bits_per_sample = 16; //PCM_SAMPLEBITS_S16_LE;
387 recorder_header.byte_rate = (recorder_header.bits_per_sample / 8) * recorder_header.num_channels * recorder_header.sample_rate;
388 recorder_header.block_align = recorder_header.num_channels * (recorder_header.bits_per_sample / 8);
389 recorder_header.data_id = ID_DATA;
390
391 mbtk_audio_pcm_sample_rate_set(sample_rate);
392
b.liub3b923a2024-06-06 15:15:49 +0800393 wav_recorder_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
b.liu1c1c7212023-12-22 16:35:27 +0800394 if (wav_recorder_fd < 0) {
395 LOGE("%s: error opening file %s!", __FUNCTION__, path);
396 return -1;
397 }
398
399 //leave enough room for header
400 lseek(wav_recorder_fd, sizeof(struct wav_header), SEEK_SET);
401
402 recorver_data_count = 0;
403
404 return mbtk_audio_pcm_recorder_start(audio_recorder_cb);
405}
406
407int mbtk_audio_wav_recorder_pause()
408{
409 return mbtk_audio_pcm_recorder_pause();
410}
411
412int mbtk_audio_wav_recorder_resume()
413{
414 return mbtk_audio_pcm_recorder_resume();
415}
416
417
418int mbtk_audio_wav_recorder_stop()
419{
420 return mbtk_audio_pcm_recorder_stop();
421}
422
423int mbtk_audio_wav_deinit()
424{
425 if(play_state != AUDIO_PLAY_STATE_STOP) {
426 if(mbtk_audio_wav_play_stop()) {
427 LOGE("mbtk_audio_wav_play_stop() fail.");
428 }
429 }
430
431 if(wav_recorder_fd > 0) {
432 mbtk_audio_wav_recorder_stop();
433 }
434
435 return mbtk_audio_pcm_deinit();
436}
437
b.liu62240ee2024-11-07 17:52:45 +0800438int mbtk_audio_set_loopback_enable_state(int device, int enable_state)
439{
440 char command[128];
441
442 if (device < 0 || device > 2 || (enable_state != 0 && enable_state != 1)) {
443 LOGE("Invalid device or enable_state");
444 return -1;
445 }
446
447 snprintf(command, sizeof(command), "ubus call audio_if audio_mode_set '{\"param0\":0}'");
448 if (system(command) != 0) {
449 LOGE("Failed to set audio mode");
450 return -1;
451 }
452
453 if (enable_state == 1) {
454 snprintf(command, sizeof(command), "ubus call audio_if loopback_enable '{\"param0\":%d}'", device);
455 if (system(command) != 0) {
456 LOGE("Failed to enable loopback");
457 return -1;
458 }
459 } else {
460 if (system("ubus call audio_if loopback_disable") != 0) {
461 LOGE("Failed to disable loopback");
462 return -1;
463 }
464 }
465
466 current_loopback_device = device;
467 current_loopback_state = enable_state;
468
469 return 0;
470}
471
472int mbtk_audio_get_loopback_enable_state(int *device, int *enable_state)
473{
474 if (device == NULL || enable_state == NULL) {
475 LOGE("Null pointer provided for device or enable_state");
476 return -1;
477 }
478
479 *device = current_loopback_device;
480 *enable_state = current_loopback_state;
481
482 return 0;
483}