audio_io_test: Release device list device list after using it
[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 <pthread.h>
23 #include <sound_manager.h>
24 #include <sound_manager_internal.h>
25 #include <audio_io.h>
26 #include <time.h>
27
28 #ifndef M_PI
29 #define M_PI  (3.14159265)
30 #endif
31
32 #define TABLE_SIZE   (200)
33
34 typedef struct {
35         float sine[TABLE_SIZE];
36         int left_channel;
37         int right_channel;
38 } test_wav_t;
39
40 typedef struct {
41         char filename[FILENAME_MAX];
42         int rate;
43         int channels;
44         int ref_deviceid;
45 } test_echo_cancel_t;
46
47 test_wav_t test_wav;
48
49 static int ch_table[] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO,
50                           AUDIO_CHANNEL_MULTI_3, AUDIO_CHANNEL_MULTI_4, AUDIO_CHANNEL_MULTI_5,
51                           AUDIO_CHANNEL_MULTI_6, AUDIO_CHANNEL_MULTI_7, AUDIO_CHANNEL_MULTI_8,
52                           AUDIO_CHANNEL_MULTI_9, AUDIO_CHANNEL_MULTI_10, AUDIO_CHANNEL_MULTI_11,
53                           AUDIO_CHANNEL_MULTI_12, AUDIO_CHANNEL_MULTI_13, AUDIO_CHANNEL_MULTI_14,
54                           AUDIO_CHANNEL_MULTI_15, AUDIO_CHANNEL_MULTI_16 };
55 static char *state_str[] = { "IDLE", "RUNNING", "PAUSED" };
56
57 static void _audio_in_state_cb(audio_in_h handle, audio_io_state_e previous, audio_io_state_e current,
58                                                                 bool by_policy, void *user_data)
59 {
60         printf(">>> _audio_in_state_cb() : handle(%p), (%d,%s) => (%d,%s), by_policy(%d), user_data(%p)\n",
61                         handle, previous, state_str[previous], current, state_str[current], by_policy, user_data);
62 }
63
64 static void _audio_out_state_cb(audio_out_h handle, audio_io_state_e previous, audio_io_state_e current,
65                                                                 bool by_policy, void *user_data)
66 {
67         printf(">>> _audio_out_state_cb() : handle(%p), (%d,%s) => (%d,%s), by_policy(%d), user_data(%p)\n",
68                         handle, previous, state_str[previous], current, state_str[current], by_policy, user_data);
69 }
70
71 static void _play_file(char *file, int length, int num, int ch)
72 {
73         int i = 0;
74         int ret = 0;
75         int total_length = length * num;
76         audio_out_h output;
77         FILE *fp = fopen(file, "r");
78         if (fp == NULL) {
79                 printf("fopen failed\n");
80                 return;
81         }
82
83         char *buf = malloc(total_length);
84         if (buf == NULL) {
85                 printf("malloc failed\n");
86                 fclose(fp);
87                 return;
88         }
89
90         printf("# start to play [%s][%d][%d]\n", file, total_length, ch);
91         printf(" > create\n");
92         audio_out_create_new(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
93         if (fread(buf, 1, total_length, fp) != total_length)
94                 printf("error!!!!\n");
95
96         ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
97         if (ret != AUDIO_IO_ERROR_NONE) {
98                 printf("audio_out_set_state_changed_cb failed. \n");
99         }
100
101         printf("  > prepare\n");
102         audio_out_prepare(output);
103
104         char *pbuf = buf;
105         for (i = 0; i < num; i++) {
106                 printf("### write = (%d/%d) ============== \n", i, num);
107                 audio_out_write(output, pbuf, length);
108                 pbuf += length;
109
110         }
111         printf("  < unprepare\n");
112         audio_out_drain(output);
113         audio_out_unprepare(output);
114
115         printf(" < destroy\n");
116         audio_out_destroy(output);
117
118         fclose(fp);
119         free(buf);
120
121         printf("# play done\n");
122 }
123
124 #define DUMP_FILE "/root/test.raw"
125
126 FILE *fp = NULL;
127
128 static void _audio_out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data)
129 {
130         char *buf = NULL;
131         int read_bytes = 0;
132         int written = 0;
133
134         if (fp == NULL) {
135                 printf("FILE is NULL\n");
136                 return;
137         }
138
139         buf = (char *)malloc(nbytes);
140         if (!buf) {
141                 printf("malloc(%zu) failed\n", nbytes);
142                 return;
143         }
144
145         read_bytes = fread(buf, 1, nbytes, fp);
146
147         written = audio_out_write(handle, buf, read_bytes);
148         printf("written : %6d/%6d (requested %zu)\n", written, read_bytes, nbytes);
149
150         if (read_bytes < nbytes) {
151                 printf("EOS!!!!\n");
152                 fclose(fp);
153                 fp = NULL;
154         }
155         free(buf);
156 }
157
158 static void _play_file_sample_async(char *file, int frequency, int ch, int type)
159 {
160         audio_out_h output;
161         int file_size = 0;
162
163         if (ch < 0 || ch > 2)
164                 ch = 0;
165
166         fp = fopen(file, "r");
167         if (fp == NULL) {
168                 printf("open failed\n");
169                 return;
170         }
171         printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
172         audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
173
174         audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
175         audio_out_set_stream_cb(output, _audio_out_stream_cb, fp);
176         audio_out_prepare(output);
177
178         while (fp) {
179                 usleep(10000); /* 10ms */
180         }
181
182         printf("FINISHED!!!\n");
183
184         audio_out_unprepare(output);
185         audio_out_destroy(output);
186
187         printf("play done\n");
188 }
189
190 static void _play_file_sample_sync(char *file, int frequency, int ch, int type)
191 {
192         audio_out_h output;
193         long file_size = 0;
194         size_t read_bytes = 0;
195         int buffer_size = 0;
196         char *buf = NULL;
197         FILE *fp = NULL;
198         int i = 0;
199
200         if (ch < 0 || ch > 2)
201                 ch = 0;
202
203         fp = fopen(file, "r");
204         if (fp == NULL) {
205                 printf("open failed\n");
206                 return;
207         }
208         /* Get the size */
209         fseek(fp, 0, SEEK_END);
210         file_size = ftell(fp);
211         fseek(fp, 0, SEEK_SET);
212
213         printf("Play [%s] of size [%ld] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
214         audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
215
216         audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
217         audio_out_prepare(output);
218
219         audio_out_get_buffer_size(output, &buffer_size);
220         buf = (char *)calloc(buffer_size, sizeof(char));
221         if (buf == NULL) {
222                 printf("malloc failed\n");
223                 goto EXIT;
224         }
225
226         printf("Press any key to continue....\n");
227         getchar();
228         printf("Start to read[%d] from file and write : \n", buffer_size);
229
230         while (file_size > 0) {
231                 read_bytes = fread(buf, 1, buffer_size, fp);
232
233                 printf(".");
234                 if (++i % 10 == 0)
235                         printf("|");
236                 if (i % 100 == 0) {
237                         printf("\n");
238                         i = 0;
239                 }
240
241                 if (read_bytes != buffer_size) {
242                         if (feof(fp)) {
243                                 printf("\n EOS! this is a last buffer! \n");
244                         } else if (ferror(fp)) {
245                                 printf("\n Error!\n");
246                                 break;
247                         }
248                 }
249
250                 audio_out_write(output, buf, (unsigned int)read_bytes);
251
252                 file_size -= (long)read_bytes;
253                 usleep(10000);
254         }
255
256 EXIT:
257         printf("\n Cleanup now!\n");
258         audio_out_unprepare(output);
259         audio_out_destroy(output);
260
261         if (buf)
262                 free(buf);
263         if (fp)
264                 fclose(fp);
265
266         printf("Play done\n");
267 }
268
269 static void *_record_echo_cancel_thread(void *userdata)
270 {
271         int ret, size;
272         sound_stream_info_h stream_info;
273         audio_in_h input = NULL;
274         FILE *fp = NULL;
275         char *buffer = NULL;
276
277         sound_device_list_h device_list = NULL;
278         sound_device_h device;
279         bool found = false;
280         int id;
281
282         test_echo_cancel_t *data = (test_echo_cancel_t *)userdata;
283
284         ret = audio_in_create(data->rate, ch_table[data->channels], AUDIO_SAMPLE_TYPE_S16_LE, &input);
285         if (ret != AUDIO_IO_ERROR_NONE) {
286                 printf("audio in create error = 0x%x\n", ret);
287                 goto out;
288         }
289
290         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info);
291         if (ret) {
292                 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
293                 goto out;
294         }
295
296         if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != SOUND_MANAGER_ERROR_NONE) {
297                 printf("fail to get device list");
298                 goto out;
299         }
300
301         if (data->ref_deviceid != SOUND_MANAGER_STREAM_NO_REFERENCE_DEVICE) {
302                 while (sound_manager_get_next_device(device_list, &device) == SOUND_MANAGER_ERROR_NONE) {
303                         if (sound_manager_get_device_id(device, &id) != SOUND_MANAGER_ERROR_NONE)
304                                 goto out;
305
306                         if (data->ref_deviceid == id) {
307                                 found = true;
308                                 break;
309                         }
310                 }
311
312                 if (!found) {
313                         printf("Failed to find reference device");
314                         goto out;
315                 }
316
317                 ret = sound_manager_set_echo_cancel_reference_device(stream_info, device);
318                 if (ret != SOUND_MANAGER_ERROR_NONE) {
319                         printf("fail to sound_manager_set_echo_cancel_reference_device(), ret(0x%x)\n", ret);
320                         goto out;
321                 }
322         }
323
324         ret = audio_in_set_sound_stream_info(input, stream_info);
325         if (ret != AUDIO_IO_ERROR_NONE) {
326                 printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
327                 goto out;
328         }
329
330         ret = audio_in_prepare(input);
331         if (ret != AUDIO_IO_ERROR_NONE) {
332                 printf("ERROR, prepare\n");
333                 goto out;
334         }
335
336         ret = audio_in_get_buffer_size(input, &size);
337         if (ret != AUDIO_IO_ERROR_NONE) {
338                 printf("ERROR, get_buffer_size\n");
339                 goto out;
340         }
341
342         fp = fopen(data->filename, "wb+");
343         if (fp == NULL) {
344                 printf("ERROR, file open failed\n");
345                 goto out;
346         }
347
348         buffer = alloca(size);
349
350         while (1) {
351                 ret = audio_in_read(input, (void *)buffer, size);
352                 if (ret <= 0) {
353                         printf("FAIL, size=%d, ret=0x%x\n", size, ret);
354                         goto out;
355                 }
356
357                 fwrite(buffer, size, sizeof(char), fp);
358         }
359
360 out:
361         if (fp)
362                 fclose(fp);
363
364         if (input) {
365                 audio_in_unprepare(input);
366                 audio_in_destroy(input);
367         }
368
369         if (device_list)
370                 sound_manager_free_device_list(device_list);
371
372         pthread_exit(0);
373
374         return NULL;
375 }
376
377 static void _test_echo_cancel(const char *filename, int rate, int channels, int ref_deviceid, bool capture)
378 {
379         int status;
380         pthread_t thread_echo_cancel;
381         test_echo_cancel_t data;
382
383         snprintf(data.filename, FILENAME_MAX, "%s", filename);
384         data.rate = rate;
385         data.channels = channels;
386         data.ref_deviceid = ref_deviceid;
387         pthread_create(&thread_echo_cancel, NULL, _record_echo_cancel_thread, &data);
388
389         if (capture) {
390                 pthread_t thread_capture;
391                 test_echo_cancel_t data_capture;
392
393                 snprintf(data_capture.filename, FILENAME_MAX, "%s_%s", filename, "norm");
394                 data_capture.rate = rate;
395                 data_capture.channels = channels;
396                 data_capture.ref_deviceid = SOUND_MANAGER_STREAM_NO_REFERENCE_DEVICE;
397
398                 pthread_create(&thread_capture, NULL, _record_echo_cancel_thread, &data_capture);
399                 pthread_join(thread_capture, (void**)&status);
400         }
401
402         pthread_join(thread_echo_cancel, (void**)&status);
403 }
404
405 static int _record_and_play(int length, int num, int ch)
406 {
407         int ret, size, i;
408         audio_in_h input = NULL;
409         FILE *fp = NULL;
410         char *buffer = NULL;
411
412         ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input);
413         if (ret != AUDIO_IO_ERROR_NONE) {
414                 printf("audio in create error = 0x%x\n", ret);
415                 return -1;
416         }
417
418         ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
419         if (ret != AUDIO_IO_ERROR_NONE) {
420                 printf("audio_in_set_state_changed_cb failed. \n");
421                 goto error;
422         }
423
424         ret = audio_in_prepare(input);
425         if (ret != AUDIO_IO_ERROR_NONE) {
426                 printf("ERROR, prepare\n");
427                 goto error;
428         }
429
430         fp = fopen(DUMP_FILE, "wb+");
431         if (fp == NULL) {
432                 printf("ERROR, file open failed\n");
433                 goto error;
434         }
435
436         ret = audio_in_get_buffer_size(input, &size);
437         if (ret != AUDIO_IO_ERROR_NONE) {
438                 printf("ERROR, prepare\n");
439                 goto error;
440         }
441
442         size = length;
443         buffer = alloca(size);
444
445         for (i = 0; i < num; i++) {
446                 printf("### read = (%d/%d) ============== ", i, num);
447                 if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
448                         fwrite(buffer, size, sizeof(char), fp);
449                         printf("PASS, size=%d, ret=0x%x\n", size, ret);
450                 } else {
451                         printf("FAIL, size=%d, ret=0x%x\n", size, ret);
452                 }
453         }
454
455         fclose(fp);
456
457         audio_in_unprepare(input);
458         audio_in_destroy(input);
459
460         _play_file(DUMP_FILE, length, num, ch);
461
462         return 0;
463
464 error:
465         audio_in_destroy(input);
466         if (fp)
467                 fclose(fp);
468         return -1;
469 }
470
471 static int _direct_loopback()
472 {
473         int ret, size;
474         audio_in_h input = NULL;
475         audio_out_h output = NULL;
476         char *buffer = NULL;
477         int i = 0;
478
479         ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
480         if (ret != AUDIO_IO_ERROR_NONE) {
481                 printf("audio_in_create_ex failed. \n");
482                 return ret;
483         }
484         ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
485         if (ret != AUDIO_IO_ERROR_NONE) {
486                 printf("audio_out_create_new failed. \n");
487                 goto exit;
488         }
489
490         ret = audio_in_prepare(input);
491         if (ret != 0) {
492                 printf("audio_in_prepare failed, err(0x%x)\n", ret);
493                 goto exit;
494         }
495
496         ret = audio_in_get_buffer_size(input, &size);
497         if (ret != AUDIO_IO_ERROR_NONE) {
498                 printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
499                 goto exit;
500         }
501
502         printf("size(%d)\n", size);
503         buffer = alloca(size);
504
505         ret = audio_out_prepare(output);
506         if (ret != 0) {
507                 printf("audio_out_prepare failed, err(0x%x)\n", ret);
508                 goto exit;
509         }
510
511         if (buffer == NULL) {
512                 printf("buffer is null\n");
513                 ret = -1;
514                 goto exit;
515         }
516
517         printf("Start to loopback(read/write) with [16kHz/MONO/S16LE]!!!\n");
518
519         while (1) {
520                 ret = audio_in_read(input, (void *)buffer, size);
521                 if (ret > AUDIO_IO_ERROR_NONE) {
522                         ret = audio_out_write(output, buffer, size);
523                         if (ret > AUDIO_IO_ERROR_NONE) {
524                                 printf(".");
525                                 i++;
526                                 if (i % 10 == 0)
527                                         printf("|");
528                                 if (i % 100 == 0) {
529                                         printf("\n");
530                                         i = 0;
531                                 }
532                         } else {
533                                 printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
534                         }
535                 } else {
536                         printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
537                 }
538                 usleep(10000);
539         }
540
541 exit:
542         if (input)
543                 audio_in_destroy(input);
544         if (output)
545                 audio_out_destroy(output);
546
547         return ret;
548
549 }
550
551 audio_in_h input;
552 audio_out_h output;
553
554 FILE *fp_w = NULL;
555
556 sound_stream_info_h g_stream_info_read_h = NULL;
557 sound_stream_info_h g_stream_info_write_h = NULL;
558
559 static void _focus_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask,
560                                         sound_stream_focus_state_e focus_state,
561                                         sound_stream_focus_change_reason_e reason_for_change, int sound_behavior,
562                                         const char *additional_info, void *user_data)
563 {
564         printf("*** focus_callback_read is called, stream_info(%p, read(%p)/write(%p)) ***\n",
565                         stream_info, g_stream_info_read_h, g_stream_info_write_h);
566         printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n",
567                         reason_for_change, additional_info, user_data);
568         printf(" - focus_state is :%d \n", focus_state);
569
570         return;
571 }
572
573 static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *user_data)
574 {
575         const void *buffer = NULL;
576         unsigned int len = (unsigned int)nbytes;
577
578         if (len == 0)
579                 return;
580
581         audio_in_peek(handle, &buffer, &len);
582         if (fp_w)
583                 fwrite(buffer, sizeof(char), len, fp_w);
584         audio_in_drop(handle);
585 }
586
587 static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
588 {
589         short *buffer = NULL;
590         int i = 0;
591
592         if (nbytes == 0)
593                 return;
594
595         buffer = (short *)malloc(nbytes);
596         if (buffer == NULL) {
597                 printf("malloc failed\n");
598                 return;
599         }
600         memset(buffer, 0, nbytes);
601
602         for (i = 0; i < nbytes / 2; i += 2) {
603                 buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */
604                 buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel]; /* right */
605                 test_wav.left_channel += 1;
606                 if (test_wav.left_channel >= TABLE_SIZE)
607                         test_wav.left_channel -= TABLE_SIZE;
608                 test_wav.right_channel += 3;
609                 if (test_wav.right_channel >= TABLE_SIZE)
610                         test_wav.right_channel -= TABLE_SIZE;
611         }
612
613         audio_out_write(handle, buffer, nbytes);
614
615         free(buffer);
616 }
617
618 int _convert_cmd_and_run(char cmd, int mode)
619 {
620         int ret = 0;
621         switch (cmd) {
622         case 'P':
623                 if (mode & 0x01)
624                         ret = audio_out_prepare(output);
625                 if (mode & 0x02)
626                         ret = audio_in_prepare(input);
627                 break;
628         case 'u':
629                 if (mode & 0x01)
630                         ret = audio_out_unprepare(output);
631                 if (mode & 0x02)
632                         ret = audio_in_unprepare(input);
633                 break;
634         case 'p':
635                 if (mode & 0x01)
636                         ret = audio_out_pause(output);
637                 if (mode & 0x02)
638                         ret = audio_in_pause(input);
639                 break;
640         case 'r':
641                 if (mode & 0x01)
642                         ret = audio_out_resume(output);
643                 if (mode & 0x02)
644                         ret = audio_in_resume(input);
645                 break;
646         case 'd':
647                 if (mode & 0x01)
648                         ret = audio_out_drain(output);
649                 break;
650         case 'f':
651                 if (mode & 0x01)
652                         ret = audio_out_flush(output);
653                 if (mode & 0x02)
654                         ret = audio_in_flush(input);
655                 break;
656         case 'i':
657                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
658                 if (ret)
659                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
660                 break;
661         case 'q': /* quit */
662                 ret = 1;
663                 break;
664         default:
665                 ret = 1;
666                 break;
667         }
668         return ret;
669 }
670
671 int audio_io_async_test(int mode)
672 {
673         int ret, size;
674         char *buffer = NULL;
675         int i = 0;
676
677         char cmd = 0;
678         int cmd_ret;
679
680         int write_mode = (mode & 0x01);
681         int read_mode = (mode & 0x02);
682
683         sound_stream_focus_state_e playback_focus_state;
684         sound_stream_focus_state_e recording_focus_state;
685
686         if ((write_mode == 0) && (read_mode == 0)) {
687                 printf("not vaild mode.\n");
688                 return 0;
689         }
690
691         if (read_mode) {
692
693                 printf("audio_in_create\n");
694                 ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
695                 if (ret != AUDIO_IO_ERROR_NONE) {
696                         printf("audio_in_create_ex failed. \n");
697                         return 0;
698                 }
699                 printf("audio_in_create success!!! [%p]\n", input);
700
701                 ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL);
702                 if (ret != AUDIO_IO_ERROR_NONE) {
703                         printf("audio_in_set_stream_cb failed. \n");
704                         goto EXIT;
705                 }
706                 printf("audio_in_set_stream_cb success!!! [%p]\n", input);
707
708                 ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
709                 if (ret != AUDIO_IO_ERROR_NONE) {
710                         printf("audio_out_set_state_changed_cb failed. \n");
711                         goto EXIT;
712                 }
713                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", input);
714
715                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_read_h);
716                 if (ret) {
717                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
718                         goto EXIT;
719                 }
720                 ret = audio_in_set_sound_stream_info(input, g_stream_info_read_h);
721                 if (ret)
722                         printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
723
724                 ret = sound_manager_acquire_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
725                 if (ret) {
726                         printf("fail to sound_manager_acquire_focus() for RECORDING, ret(0x%x)\n", ret);
727                         goto EXIT;
728                 }
729
730                 fp_w = fopen("/tmp/pcm_w.raw", "w");
731         }
732
733         if (write_mode) {
734                 printf("before audio_out_create_new\n");
735                 getchar();
736
737                 printf("audio_out_create_new\n");
738                 ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
739                 if (ret != AUDIO_IO_ERROR_NONE) {
740                         printf("audio_out_create_new failed. \n");
741                         goto EXIT;
742                 }
743                 printf("audio_out_create_new success!!! [%p]\n", output);
744
745                 ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL);
746                 if (ret != AUDIO_IO_ERROR_NONE) {
747                         printf("audio_out_set_stream_cb failed. \n");
748                         goto EXIT;
749                 }
750                 printf("audio_out_set_stream_cb success!!! [%p]\n", output);
751
752                 ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
753                 if (ret != AUDIO_IO_ERROR_NONE) {
754                         printf("audio_out_set_state_changed_cb failed. \n");
755                         goto EXIT;
756                 }
757                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", output);
758
759                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
760                 if (ret) {
761                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
762                         goto EXIT;
763                 }
764
765                 ret = audio_out_set_sound_stream_info(output, g_stream_info_write_h);
766                 if (ret)
767                         printf("fail to audio_out_set_sound_stream_info(), ret(0x%x)\n", ret);
768
769                 ret = sound_manager_acquire_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
770                 if (ret) {
771                         printf("fail to sound_manager_acquire_focus() for PLAYBACK, ret(0x%x)\n", ret);
772                         goto EXIT;
773                 }
774
775                 /* generate wave data */
776                 for (i = 0; i < TABLE_SIZE; i++)
777                         test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
778                 test_wav.left_channel = test_wav.right_channel = 0;
779         }
780
781         if (read_mode) {
782                 printf("before audio_in_prepare\n");
783                 getchar();
784                 printf("audio_in_prepare\n");
785                 ret = audio_in_prepare(input);
786                 if (ret != 0) {
787                         printf("audio_in_prepare failed, err(0x%x)\n", ret);
788                         audio_in_destroy(input);
789                         goto EXIT;
790                 } else {
791                         ret = audio_in_get_buffer_size(input, &size);
792                         if (ret != AUDIO_IO_ERROR_NONE) {
793                                 printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
794                                 goto EXIT;
795                         } else {
796                                 printf("size(%d)\n", size);
797                                 buffer = alloca(size);
798                         }
799                 }
800
801                 if (buffer == NULL) {
802                         printf("buffer is null\n");
803                         goto EXIT;
804                 }
805         }
806
807         if (write_mode) {
808                 printf("before audio_out_prepare\n");
809                 getchar();
810                 printf("audio_out_prepare\n");
811                 ret = audio_out_prepare(output);
812                 if (ret != 0) {
813                         printf("audio_out_prepare failed, err(0x%x)\n", ret);
814                         audio_out_destroy(output);
815                         goto EXIT;
816                 }
817         }
818
819         do {
820                 printf("command(q:quit) : ");
821                 ret = getchar();
822                 if (ret == EOF)
823                         goto EXIT;
824                 cmd = (char)ret;
825                 if (cmd != '\n')
826                         getchar();
827                 cmd_ret = _convert_cmd_and_run(cmd, mode);
828                 printf("  - result code : %d\n", cmd_ret);
829         } while (cmd != 'q');
830
831 EXIT:
832         if (read_mode) {
833                 if (input) {
834                         printf("audio_in_unprepare\n");
835                         audio_in_unprepare(input);
836                         printf("audio_in_destroy\n");
837                         audio_in_destroy(input);
838                         input = NULL;
839                 }
840
841                 if (fp_w) {
842                         fclose(fp_w);
843                         fp_w = NULL;
844                 }
845
846                 if (g_stream_info_read_h) {
847                         ret = sound_manager_get_focus_state(g_stream_info_read_h, NULL, &recording_focus_state);
848                         if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
849                                 ret = sound_manager_release_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
850                                 if (ret)
851                                         printf("fail to sound_manager_release_focus() for recording, ret(0x%x)\n", ret);
852                         }
853                         ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
854                         if (ret)
855                                 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
856                         g_stream_info_read_h = NULL;
857                 }
858         }
859
860         if (write_mode) {
861                 if (output) {
862                         printf("audio_out_unprepare\n");
863                         audio_out_unprepare(output);
864                         printf("audio_out_destroy\n");
865                         audio_out_destroy(output);
866                 }
867
868                 if (g_stream_info_write_h) {
869                         ret = sound_manager_get_focus_state(g_stream_info_write_h, &playback_focus_state, NULL);
870                         if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
871                                 ret = sound_manager_release_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
872                                 if (ret)
873                                         printf("fail to sound_manager_release_focus() for playback, ret(0x%x)\n", ret);
874                         }
875                         ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
876                         if (ret)
877                                 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
878                         g_stream_info_write_h = NULL;
879                 }
880         }
881
882         return 0;
883 }
884
885 #define THREAD_MAX 10
886 #define TEST_COUNT 500
887 static void *thread_stress_test_audio_in(void *data)
888 {
889         int i, j;
890         int rate;
891         char *buf;
892         int buffer_size;
893         audio_channel_e ch;
894         audio_sample_type_e type;
895         unsigned int seed = (unsigned int)time(NULL);
896
897         audio_in_h input = (audio_in_h)data;
898
899         audio_in_prepare(input);
900         audio_in_get_buffer_size(input, &buffer_size);
901         buf = (char *)malloc(buffer_size);
902         if (buf == NULL) {
903                 printf("malloc failed\n");
904                 goto EXIT;
905         }
906
907         for (i=0; i<TEST_COUNT; i++) {
908                 switch (rand_r(&seed) % 8) {
909                         case 0:
910                                 audio_in_prepare(input);
911                                 break;
912                         case 1:
913                                 audio_in_read(input, buf, buffer_size);
914                                 break;
915                         case 2:
916                                 audio_in_pause(input);
917                                 break;
918                         case 3:
919                                 audio_in_resume(input);
920                                 break;
921                         case 4:
922                                 audio_in_flush(input);
923                                 break;
924                         case 5:
925                                 audio_in_unprepare(input);
926                                 break;
927                         case 6:
928                                 audio_in_prepare(input);
929                                 for  (j = 0; j < 10; j++)
930                                         audio_in_read(input, buf, buffer_size);
931                                 break;
932                         case 7:
933                                 audio_in_get_buffer_size(input, &buffer_size);
934                                 audio_in_get_sample_rate(input, &rate);
935                                 audio_in_get_channel(input, &ch);
936                                 audio_in_get_sample_type(input, &type);
937                                 break;
938                         default:
939                                 break;
940                 }
941                 if ((i % (TEST_COUNT/10)) == 0)
942                         printf("audio_in: %x thread. count(%d/%d)\n", (int)pthread_self(), i, TEST_COUNT);
943         }
944         audio_in_unprepare(input);
945
946 EXIT:
947         if (buf) {
948                 free(buf);
949                 buf = NULL;
950         }
951
952         pthread_exit(0);
953 }
954
955 static void *thread_stress_test_audio_out(void *data)
956 {
957         int i, j;
958         int rate;
959         char *buf;
960         int buffer_size;
961         audio_channel_e ch;
962         audio_sample_type_e type;
963         unsigned int seed = (unsigned int)time(NULL);
964
965         audio_out_h output = (audio_out_h)data;
966
967         srand(time(NULL));
968
969         audio_out_get_buffer_size(output, &buffer_size);
970
971         buf = (char *)malloc(buffer_size);
972         if (buf == NULL) {
973                 printf("malloc failed\n");
974                 goto EXIT;
975         }
976
977         audio_out_prepare(output);
978         for (i = 0; i < TEST_COUNT; i++) {
979                 switch (rand_r(&seed) % 9) {
980                         case 0:
981                                 audio_out_prepare(output);
982                                 break;
983                         case 1:
984                                 audio_out_write(output, buf, buffer_size);
985                                 break;
986                         case 2:
987                                 audio_out_pause(output);
988                                 break;
989                         case 3:
990                                 audio_out_resume(output);
991                                 break;
992                         case 4:
993                                 audio_out_drain(output);
994                                 break;
995                         case 5:
996                                 audio_out_flush(output);
997                                 break;
998                         case 6:
999                                 audio_out_write(output, buf, buffer_size);
1000                                 break;
1001                         case 7:
1002                                 audio_out_prepare(output);
1003                                 for  (j = 0; j < 10; j++)
1004                                         audio_out_write(output, buf, buffer_size);
1005                                 break;
1006                         case 8:
1007                                 audio_out_get_buffer_size(output, &buffer_size);
1008                                 audio_out_get_sample_rate(output, &rate);
1009                                 audio_out_get_channel(output, &ch);
1010                                 audio_out_get_sample_type(output, &type);
1011                                 break;
1012                         default:
1013                                 break;
1014                 }
1015                 if ((i % (TEST_COUNT/10)) == 0)
1016                         printf("audio_out: %x thread. count(%d/%d)\n", (int)pthread_self(), i, TEST_COUNT);
1017         }
1018         audio_out_unprepare(output);
1019
1020 EXIT:
1021         if (buf)
1022                 free(buf);
1023
1024         pthread_exit(0);
1025 }
1026
1027 #define OUT_HANDLE_MAX  1000
1028 #define IN_HANDLE_MAX   10
1029 void audio_io_test_handle_max()
1030 {
1031         audio_out_h output[OUT_HANDLE_MAX] = { 0, };
1032         audio_in_h input[IN_HANDLE_MAX] = { 0, };
1033         int i;
1034         int success = 0;
1035         int ret;
1036
1037         printf("==============================================\n");
1038         printf("playback handle creation test. try to create %d handles\n", OUT_HANDLE_MAX);
1039         for (i = 0; i < OUT_HANDLE_MAX; i++) {
1040                 ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[i]);
1041                 if (ret != AUDIO_IO_ERROR_NONE) {
1042                         printf("audio_out_create_new failed.\n");
1043                         return;
1044                 }
1045
1046                 ret = audio_out_prepare(output[i]);
1047                 if (ret == AUDIO_IO_ERROR_NONE)
1048                         success++;
1049         }
1050         printf("created handle (%d/%d)\n", success, OUT_HANDLE_MAX);
1051
1052         for (i = 0; i < OUT_HANDLE_MAX; i++)
1053                 audio_out_destroy(output[i]);
1054
1055         /* pulseaudio oom when IN_HANDLE_MAX is set over 50 */
1056         printf("capture handle creation test. try to create %d handles\n", IN_HANDLE_MAX);
1057         success = 0;
1058         for (i = 0; i < IN_HANDLE_MAX; i++) {
1059                 ret = audio_in_create(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input[i]);
1060                 if (ret != AUDIO_IO_ERROR_NONE) {
1061                         printf("audio_in_create_new failed.\n");
1062                         return;
1063                 }
1064
1065                 ret = audio_in_prepare(input[i]);
1066                 if (ret == AUDIO_IO_ERROR_NONE)
1067                         success++;
1068         }
1069         printf("created handle (%d/%d)\n", success, IN_HANDLE_MAX);
1070
1071         for (i = 0; i < IN_HANDLE_MAX; i++)
1072                 audio_in_destroy(input[i]);
1073 }
1074
1075 int audio_io_test_read_write()
1076 {
1077         int i;
1078         int ret;
1079         audio_out_h output[2];
1080         audio_in_h input;
1081
1082         int status;
1083         pthread_t t[THREAD_MAX];
1084
1085         void out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data) {
1086                 char * buf = (char *)malloc(nbytes);
1087                 if (!buf) {
1088                         printf("malloc(%zu) failed\n", nbytes);
1089                         return;
1090                 }
1091                 audio_out_write(handle, buf, nbytes);
1092                 free(buf);
1093                 return;
1094         }
1095
1096         void in_stream_cb(audio_in_h handle, size_t nbytes, void *user_data) {
1097                 void* buf = NULL;
1098                 unsigned int size = nbytes;
1099
1100                 audio_in_peek(handle, (const void **)buf, &size);
1101                 audio_in_drop(handle);
1102                 return;
1103         }
1104
1105         void state_changed_cb(audio_out_h handle, audio_io_state_e previous, audio_io_state_e current, bool by_policy, void *user_data) {
1106                 return;
1107         }
1108
1109         /* audio_out sync */
1110         ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[0]);
1111         if (ret != AUDIO_IO_ERROR_NONE) {
1112                 printf("audio_out_create_new failed. \n");
1113                 return 0;
1114         }
1115         audio_out_set_state_changed_cb(output[0], state_changed_cb, NULL);
1116
1117         /* audio_out async */
1118         ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[1]);
1119         if (ret != AUDIO_IO_ERROR_NONE) {
1120                 printf("audio_out_create_new failed. \n");
1121                 return 0;
1122         }
1123         audio_out_set_stream_cb(output[1], out_stream_cb, NULL);
1124         audio_out_set_state_changed_cb(output[1], state_changed_cb, NULL);
1125
1126         /* audio_in */
1127         ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
1128         if (ret != AUDIO_IO_ERROR_NONE) {
1129                 printf("audio in create error = 0x%x\n", ret);
1130                 return -1;
1131         }
1132         audio_in_set_stream_cb(input, in_stream_cb, NULL);
1133
1134         printf("==============================================\n");
1135         printf("audio out sync test\n");
1136         for (i = 0; i < THREAD_MAX; i++)
1137                 pthread_create(&t[i], NULL, thread_stress_test_audio_out, output[0]);
1138
1139         for (i = 0; i < THREAD_MAX; i++) {
1140                 pthread_join(t[i], (void**)&status);
1141                 printf("thread %d finished\n", i);
1142         }
1143
1144         printf("==============================================\n");
1145         printf("audio out async test\n");
1146         for (i = 0; i < THREAD_MAX; i++)
1147                 pthread_create(&t[i], NULL, thread_stress_test_audio_out, output[1]);
1148
1149         for (i = 0; i < THREAD_MAX; i++) {
1150                 pthread_join(t[i], (void**)&status);
1151                 printf("thread %d finished\n", i);
1152         }
1153
1154         printf("==============================================\n");
1155         printf("audio in test\n");
1156         for (i = 0; i < THREAD_MAX; i++)
1157                 pthread_create(&t[i], NULL, thread_stress_test_audio_in, input);
1158
1159         for (i = 0; i < THREAD_MAX; i++) {
1160                 pthread_join(t[i], (void**)&status);
1161                 printf("thread %d finished\n", i);
1162         }
1163
1164         audio_out_destroy(output[0]);
1165         audio_out_destroy(output[1]);
1166         audio_in_destroy(input);
1167
1168         printf("stress test end\n");
1169
1170         return 0;
1171 }
1172
1173 int main(int argc, char **argv)
1174 {
1175         setbuf(stdout, NULL);
1176
1177         if (argc == 2 && !strcmp(argv[1], "loopback")) {
1178                 _direct_loopback();
1179         } else if (argc == 3 && !strcmp(argv[1], "async")) {
1180                 audio_io_async_test(atoi(argv[2]));
1181         } else if (argc == 2 && !strcmp(argv[1], "stress")) {
1182                 audio_io_test_handle_max();
1183                 audio_io_test_read_write();
1184         } else if (argc == 4) {
1185                 int channel_idx = atoi(argv[3]);
1186                 if (channel_idx <= 0 || channel_idx > 16) {
1187                         printf("Invalid channel\n");
1188                         return 0;
1189                 }
1190                 printf("run with [%s][%s][%s]\n", argv[1], argv[2], argv[3]);
1191                 _record_and_play(atoi(argv[1]), atoi(argv[2]), channel_idx);
1192         } else if (argc == 6) {
1193                 if (strcmp(argv[1], "play") == 0)
1194                         _play_file_sample_sync(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
1195                 else if (strcmp(argv[1], "playasync") == 0)
1196                         _play_file_sample_async(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
1197         } else if (argc == 7 && strcmp(argv[1], "echocancel") == 0) {
1198                         _test_echo_cancel(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), !!atoi(argv[6]));
1199         } else {
1200                 printf("- Usages :\n");
1201                 printf("- # audio_io_test loopback\n");
1202                 printf("- # audio_io_test stress\n");
1203                 printf("- # audio_io_test [length to read] [number of iteration] [channels]\n");
1204                 printf("- # audio_io_test async [write(1) | read(2)]\n");
1205                 printf("- # audio_io_test play [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
1206                 printf("- # audio_io_test playasync [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
1207                 printf("- # audio_io_test echocancel [filename] [sample rate] [channels] [ref_dev] [capture without EC]\n");
1208         }
1209         return 0;
1210 }