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