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