Apply tizen coding convention
[platform/core/api/audio-io.git] / test / audio_io_test.c
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <math.h>
22 #include <sound_manager.h>
23 #include <audio_io.h>
24
25 #ifndef M_PI
26 #define M_PI  (3.14159265)
27 #endif
28
29 #define TABLE_SIZE   (200)
30 typedef struct {
31         float sine[TABLE_SIZE];
32         int left_channel;
33         int right_channel;
34 } test_wav_t;
35 test_wav_t test_wav;
36
37 static int ch_table[3] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO };
38
39 void play_file(char *file, int length, int ch)
40 {
41         audio_out_h output;
42         FILE *fp = fopen(file, "r");
43         if (fp == NULL) {
44                 printf("fopen failed\n");
45                 return;
46         }
47
48         char *buf = malloc(length);
49         if (buf == NULL) {
50                 printf("malloc failed\n");
51                 fclose(fp);
52                 return;
53         }
54
55         printf("start to play [%s][%d][%d]\n", file, length, ch);
56         audio_out_create_new(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
57         if (fread(buf, 1, length, fp) != length)
58                 printf("error!!!!\n");
59
60         audio_out_prepare(output);
61         audio_out_write(output, buf, length);
62         audio_out_unprepare(output);
63
64         audio_out_destroy(output);
65
66         fclose(fp);
67
68         printf("play done\n");
69 }
70
71 #define DUMP_FILE "/root/test.raw"
72
73 void play_file_sample(char *file, int frequency, int ch, int type)
74 {
75         audio_out_h output;
76         int file_size = 0;
77         int read_bytes = 0;
78         int buffer_size = 0;
79         char *buf = NULL;
80
81         if (ch < 0 || ch > 2)
82                 ch = 0;
83
84         FILE *fp = fopen(file, "r");
85         if (fp == NULL) {
86                 printf("open failed\n");
87                 return;
88         }
89         /* Get the size */
90         fseek(fp, 0, SEEK_END);
91         file_size = ftell(fp);
92         fseek(fp, 0, SEEK_SET);
93
94         printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
95         if (type)
96                 audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
97         else
98                 audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8, &output);
99
100         audio_out_prepare(output);
101         audio_out_get_buffer_size(output, &buffer_size);
102
103         buf = (char *)malloc(buffer_size);
104         if (buf == NULL) {
105                 printf("malloc failed\n");
106                 audio_out_unprepare(output);
107                 audio_out_destroy(output);
108                 fclose(fp);
109                 return;
110         }
111
112         while (file_size > 0) {
113                 read_bytes = fread(buf, 1, buffer_size, fp);
114                 printf("Read %d Requested - %d\n", read_bytes, buffer_size);
115                 audio_out_write(output, buf, read_bytes);
116                 file_size = file_size - read_bytes;
117         }
118
119         audio_out_unprepare(output);
120         audio_out_destroy(output);
121
122         free(buf);
123         fclose(fp);
124         printf("play done\n");
125 }
126
127 int audio_io_test(int length, int num, int ch)
128 {
129         int ret, size, i;
130         audio_in_h input;
131         if ((ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) {
132                 ret = audio_in_ignore_session(input);
133                 if (ret != 0) {
134                         printf("ERROR, set session mix\n");
135                         audio_in_destroy(input);
136                         return 0;
137                 }
138
139                 ret = audio_in_prepare(input);
140                 if (ret != 0) {
141                         printf("ERROR, prepare\n");
142                         audio_in_destroy(input);
143                         return 0;
144                 }
145
146                 FILE *fp = fopen(DUMP_FILE, "wb+");
147
148                 if (fp == NULL) {
149                         printf("ERROR, file open failed\n");
150                         audio_in_destroy(input);
151                         return 0;
152                 }
153
154                 if ((ret = audio_in_get_buffer_size(input, &size)) == AUDIO_IO_ERROR_NONE) {
155                         size = length;
156                         char *buffer = alloca(size);
157
158                         for (i = 0; i < num; i++) {
159                                 printf("### loop = %d ============== \n", i);
160                                 if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
161                                         fwrite(buffer, size, sizeof(char), fp);
162                                         printf("PASS, size=%d, ret=%d\n", size, ret);
163                                 } else {
164                                         printf("FAIL, size=%d, ret=%d\n", size, ret);
165                                 }
166                         }
167                 }
168
169                 fclose(fp);
170
171                 audio_in_destroy(input);
172         }
173
174         play_file(DUMP_FILE, length * num, ch);
175
176         return 1;
177 }
178
179 int audio_io_loopback_in_test()
180 {
181         int ret, size;
182         audio_in_h input;
183         FILE *fp = fopen("/tmp/dump_test.raw", "wb+");
184
185         if (fp == NULL) {
186                 printf("open failed \n");
187                 return 0;
188         }
189
190         if ((ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) {
191                 ret = audio_in_ignore_session(input);
192                 if (ret != 0) {
193                         printf("ERROR, set session mix\n");
194                         goto exit;
195                 }
196
197                 ret = audio_in_prepare(input);
198                 if (ret != 0) {
199                         printf("ERROR, prepare\n");
200                         goto exit;
201                 }
202
203                 ret = audio_in_get_buffer_size(input, &size);
204                 if (ret != AUDIO_IO_ERROR_NONE) {
205                         printf("audio_in_get_buffer_size failed.\n");
206                         goto exit;
207                 }
208
209                 while (1) {
210                         char *buffer = alloca(size);
211                         if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
212                                 fwrite(buffer, size, sizeof(char), fp);
213                                 printf("PASS, size=%d, ret=%d\n", size, ret);
214                         } else {
215                                 printf("FAIL, size=%d, ret=%d\n", size, ret);
216                         }
217                 }
218         }
219
220  exit:
221         audio_in_destroy(input);
222
223         fclose(fp);
224
225         return ret;
226
227 }
228
229 int audio_io_loopback_test()
230 {
231         int ret, size;
232         audio_in_h input;
233         audio_out_h output;
234         char *buffer = NULL;
235
236         ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
237         if (ret != AUDIO_IO_ERROR_NONE) {
238                 printf("audio_in_create_ex failed. \n");
239                 return 0;
240         }
241         ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
242         if (ret != AUDIO_IO_ERROR_NONE) {
243                 printf("audio_out_create failed. \n");
244                 return 0;
245         }
246
247         ret = audio_in_prepare(input);
248         if (ret != 0) {
249                 printf("audio_in_prepare failed.\n");
250                 audio_in_destroy(input);
251                 return 0;
252         } else {
253                 ret = audio_in_get_buffer_size(input, &size);
254                 if (ret != AUDIO_IO_ERROR_NONE) {
255                         printf("audio_in_get_buffer_size failed.\n");
256                         return 0;
257                 } else {
258                         printf("size(%d)\n", size);
259                         buffer = alloca(size);
260                 }
261         }
262
263         ret = audio_out_prepare(output);
264         if (ret != 0) {
265                 printf("audio_out_prepare failed.\n");
266                 audio_out_destroy(output);
267                 return 0;
268         }
269
270         if (buffer == NULL) {
271                 printf("buffer is null\n");
272                 return 0;
273         }
274
275         while (1) {
276                 ret = audio_in_read(input, (void *)buffer, size);
277                 if (ret > AUDIO_IO_ERROR_NONE) {
278                         ret = audio_out_write(output, buffer, size);
279                         if (ret > AUDIO_IO_ERROR_NONE)
280                                 printf("audio read/write success. buffer(%p), size(%d)\n", buffer, size);
281                         else
282                                 printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
283                 } else
284                         printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
285         }
286
287 }
288
289 audio_in_h input;
290 audio_out_h output;
291
292 FILE *fp_w = NULL;
293
294 sound_stream_info_h g_stream_info_read_h = NULL;
295 sound_stream_info_h g_stream_info_write_h = NULL;
296
297 static void focus_callback(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason_for_change, const char *additional_info, void *user_data)
298 {
299         int ret = 0;
300         sound_stream_focus_state_e playback_focus_state;
301         sound_stream_focus_state_e recording_focus_state;
302         printf("*** focus_callback_read is called, stream_info(%p, read(%p)/write(%p)) ***\n", stream_info, g_stream_info_read_h, g_stream_info_write_h);
303         printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data);
304         ret = sound_manager_get_focus_state(stream_info, &playback_focus_state, &recording_focus_state);
305         if (!ret)
306                 printf(" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state);
307
308         return;
309 }
310
311 static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *user_data)
312 {
313         const void *buffer = NULL;
314         unsigned int len = (unsigned int)nbytes;
315
316         if (len > 0) {
317                 audio_in_peek(handle, &buffer, &len);
318                 if (fp_w)
319                         fwrite(buffer, sizeof(char), len, fp_w);
320                 audio_in_drop(handle);
321         }
322 }
323
324 static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
325 {
326         short *buffer = NULL;
327         int ret = 0;
328         int i = 0;
329
330         if (nbytes > 0) {
331                 buffer = (short *)malloc(nbytes);
332                 if (buffer == NULL) {
333                         printf("malloc failed\n");
334                         return;
335                 }
336                 memset(buffer, 0, nbytes);
337
338                 for (i = 0; i < nbytes / 2; i += 2) {
339                         buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */
340                         buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel];    /* right */
341                         test_wav.left_channel += 1;
342                         if (test_wav.left_channel >= TABLE_SIZE)
343                                 test_wav.left_channel -= TABLE_SIZE;
344                         test_wav.right_channel += 3;
345                         if (test_wav.right_channel >= TABLE_SIZE)
346                                 test_wav.right_channel -= TABLE_SIZE;
347                 }
348
349                 ret = audio_out_write(handle, buffer, nbytes);
350
351                 free(buffer);
352         }
353 }
354
355 static void _audio_in_state_cb(audio_in_h handle, audio_io_state_e previous, audio_io_state_e current, bool by_policy, void *user_data)
356 {
357         printf(">>> _audio_in_state_cb() : handle(%p), current(%d), previous(%d), by_policy(%d), user_data(%p)\n", handle, current, previous, by_policy, user_data);
358 }
359
360 static void _audio_out_state_cb(audio_in_h handle, audio_io_state_e previous, audio_io_state_e current, bool by_policy, void *user_data)
361 {
362         printf(">>> _audio_out_state_cb() : handle(%p), current(%d), previous(%d), by_policy(%d), user_data(%p)\n", handle, current, previous, by_policy, user_data);
363 }
364
365 int _convert_cmd_and_run(char cmd, int mode)
366 {
367         int ret = 0;
368         switch (cmd) {
369         case 'P':
370                 if (mode & 0x01)
371                         ret = audio_out_prepare(output);
372                 if (mode & 0x02)
373                         ret = audio_in_prepare(input);
374                 break;
375         case 'u':
376                 if (mode & 0x01)
377                         ret = audio_out_unprepare(output);
378                 if (mode & 0x02)
379                         ret = audio_in_unprepare(input);
380                 break;
381         case 'p':
382                 if (mode & 0x01)
383                         ret = audio_out_pause(output);
384                 if (mode & 0x02)
385                         ret = audio_in_pause(input);
386                 break;
387         case 'r':
388                 if (mode & 0x01)
389                         ret = audio_out_resume(output);
390                 if (mode & 0x02)
391                         ret = audio_in_resume(input);
392                 break;
393         case 'd':
394                 if (mode & 0x01)
395                         ret = audio_out_drain(output);
396                 break;
397         case 'f':
398                 if (mode & 0x01)
399                         ret = audio_out_flush(output);
400                 if (mode & 0x02)
401                         ret = audio_in_flush(input);
402                 break;
403         case 'i':
404                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_write_h);
405                 if (ret)
406                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
407                 break;
408         case 'q':                                       /* quit */
409                 ret = 1;
410                 break;
411         default:
412                 ret = 1;
413                 break;
414         }
415         return ret;
416 }
417
418 int audio_io_async_test(int mode)
419 {
420         int ret, size;
421         char *buffer = NULL;
422         int i = 0;
423
424         char cmd = 0;
425         int cmd_ret;
426
427         int write_mode = (mode & 0x01);
428         int read_mode = (mode & 0x02);
429
430         sound_stream_focus_state_e playback_focus_state;
431         sound_stream_focus_state_e recording_focus_state;
432
433         if ((write_mode == 0) && (read_mode == 0)) {
434                 printf("not vaild mode.\n");
435                 return 0;
436         }
437
438         if (read_mode) {
439
440                 printf("audio_in_create\n");
441                 ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
442                 if (ret != AUDIO_IO_ERROR_NONE) {
443                         printf("audio_in_create_ex failed. \n");
444                         return 0;
445                 }
446                 printf("audio_in_create success!!! [%p]\n", input);
447
448                 ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL);
449                 if (ret != AUDIO_IO_ERROR_NONE) {
450                         printf("audio_in_set_stream_cb failed. \n");
451                         goto EXIT;
452                 }
453                 printf("audio_in_set_stream_cb success!!! [%p]\n", input);
454
455                 ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
456                 if (ret != AUDIO_IO_ERROR_NONE) {
457                         printf("audio_out_set_state_changed_cb failed. \n");
458                         goto EXIT;
459                 }
460                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", input);
461
462                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_read_h);
463                 if (ret) {
464                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
465                         goto EXIT;
466                 }
467                 ret = audio_in_set_stream_info(input, g_stream_info_read_h);
468                 if (ret)
469                         printf("fail to audio_in_set_stream_info(), ret(0x%x)\n", ret);
470
471                 ret = sound_manager_acquire_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
472                 if (ret) {
473                         printf("fail to sound_manager_acquire_focus() for RECORDING, ret(0x%x)\n", ret);
474                         goto EXIT;
475                 }
476
477                 fp_w = fopen("/tmp/pcm_w.raw", "w");
478         }
479
480         if (write_mode) {
481                 printf("before audio_out_create\n");
482                 getchar();
483
484                 printf("audio_out_create\n");
485                 ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
486                 if (ret != AUDIO_IO_ERROR_NONE) {
487                         printf("audio_out_create failed. \n");
488                         goto EXIT;
489                 }
490                 printf("audio_out_create success!!! [%p]\n", output);
491
492                 ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL);
493                 if (ret != AUDIO_IO_ERROR_NONE) {
494                         printf("audio_out_set_stream_cb failed. \n");
495                         goto EXIT;
496                 }
497                 printf("audio_out_set_stream_cb success!!! [%p]\n", output);
498
499                 ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
500                 if (ret != AUDIO_IO_ERROR_NONE) {
501                         printf("audio_out_set_state_changed_cb failed. \n");
502                         goto EXIT;
503                 }
504                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", output);
505
506                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_write_h);
507                 if (ret) {
508                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
509                         goto EXIT;
510                 }
511                 ret = audio_out_set_stream_info(output, g_stream_info_write_h);
512                 if (ret)
513                         printf("fail to audio_out_set_stream_info(), ret(0x%x)\n", ret);
514
515                 ret = sound_manager_acquire_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
516                 if (ret) {
517                         printf("fail to sound_manager_acquire_focus() for PLAYBACK, ret(0x%x)\n", ret);
518                         goto EXIT;
519                 }
520
521                 /* generate wave data */
522                 for (i = 0; i < TABLE_SIZE; i++)
523                         test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
524                 test_wav.left_channel = test_wav.right_channel = 0;
525         }
526
527         if (read_mode) {
528                 printf("before audio_in_prepare\n");
529                 getchar();
530                 printf("audio_in_prepare\n");
531                 ret = audio_in_prepare(input);
532                 if (ret != 0) {
533                         printf("audio_in_prepare failed.\n");
534                         audio_in_destroy(input);
535                         goto EXIT;
536                 } else {
537                         ret = audio_in_get_buffer_size(input, &size);
538                         if (ret != AUDIO_IO_ERROR_NONE) {
539                                 printf("audio_in_get_buffer_size failed.\n");
540                                 goto EXIT;
541                         } else {
542                                 printf("size(%d)\n", size);
543                                 buffer = alloca(size);
544                         }
545                 }
546
547                 if (buffer == NULL) {
548                         printf("buffer is null\n");
549                         goto EXIT;
550                 }
551         }
552
553         if (write_mode) {
554                 printf("before audio_out_prepare\n");
555                 getchar();
556                 printf("audio_out_prepare\n");
557                 ret = audio_out_prepare(output);
558                 if (ret != 0) {
559                         printf("audio_out_prepare failed.\n");
560                         audio_out_destroy(output);
561                         goto EXIT;
562                 }
563         }
564
565         do {
566                 printf("command(q:quit) : ");
567                 cmd = (char)getchar();
568                 if (cmd != '\n')
569                         getchar();
570                 cmd_ret = _convert_cmd_and_run(cmd, mode);
571                 printf("  - result code : %d\n", cmd_ret);
572         } while (cmd != 'q');
573
574 EXIT:
575         if (read_mode) {
576                 if (input) {
577                         printf("audio_in_unprepare\n");
578                         audio_in_unprepare(input);
579                         printf("audio_in_destroy\n");
580                         audio_in_destroy(input);
581                         input = NULL;
582                 }
583
584                 if (fp_w) {
585                         fclose(fp_w);
586                         fp_w = NULL;
587                 }
588
589                 if (g_stream_info_read_h) {
590                         ret = sound_manager_get_focus_state(g_stream_info_read_h, NULL, &recording_focus_state);
591                         if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
592                                 ret = sound_manager_release_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
593                                 if (ret)
594                                         printf("fail to sound_manager_release_focus() for recording, ret(0x%x)\n", ret);
595                         }
596                         ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
597                         if (ret)
598                                 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
599                         g_stream_info_read_h = NULL;
600                 }
601         }
602
603         if (write_mode) {
604                 if (output) {
605                         printf("audio_out_unprepare\n");
606                         audio_out_unprepare(output);
607                         printf("audio_out_destroy\n");
608                         audio_out_destroy(output);
609                 }
610
611                 if (g_stream_info_write_h) {
612                         ret = sound_manager_get_focus_state(g_stream_info_write_h, &playback_focus_state, NULL);
613                         if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
614                                 ret = sound_manager_release_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
615                                 if (ret)
616                                         printf("fail to sound_manager_release_focus() for playback, ret(0x%x)\n", ret);
617                         }
618                         ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
619                         if (ret)
620                                 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
621                         g_stream_info_write_h = NULL;
622                 }
623         }
624
625         return 0;
626 }
627
628 int main(int argc, char **argv)
629 {
630         if (argc == 2 && !strcmp(argv[1], "call-forwarding-loop")) {
631                 audio_io_loopback_test();
632         } else if (argc == 2 && !strcmp(argv[1], "call-forwarding-in")) {
633                 audio_io_loopback_in_test();
634         } else if (argc == 3 && !strcmp(argv[1], "async")) {
635                 audio_io_async_test(atoi(argv[2]));
636         } else if (argc == 4) {
637                 printf("run with [%s][%s][%s]\n", argv[1], argv[2], argv[3]);
638                 audio_io_test(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
639         } else if (argc == 6) {
640                 play_file_sample(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
641         } else {
642                 printf("1. usage : audio_io_test call-forwarding-loop\n");
643                 printf("2. usage : audio_io_test call-forwarding-in\n");
644                 printf("3. usage : audio_io_test [length to read] [number of iteration] [channels]\n");
645                 printf("4. usage : audio_io_test async [write(1) | read(2)]\n");
646                 printf("5. Uasge : audio_io_test play [filename] [sample rate] [channels] [type(0:U8)]\n");
647         }
648         return 0;
649 }