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