[ACR-1434] Extend channel and format parameters
[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
126         read_bytes = fread(buf, 1, nbytes, fp);
127
128         written = audio_out_write(handle, buf, read_bytes);
129         printf("written : %6d/%6d (requested %zu)\n", written, read_bytes, nbytes);
130
131         if (read_bytes < nbytes) {
132                 printf("EOS!!!!\n");
133                 fclose(fp);
134                 fp = NULL;
135         }
136         free(buf);
137 }
138
139 static void _play_file_sample_async(char *file, int frequency, int ch, int type)
140 {
141         audio_out_h output;
142         int file_size = 0;
143
144         if (ch < 0 || ch > 2)
145                 ch = 0;
146
147         fp = fopen(file, "r");
148         if (fp == NULL) {
149                 printf("open failed\n");
150                 return;
151         }
152         printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
153         audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
154
155         audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
156         audio_out_set_stream_cb(output, _audio_out_stream_cb, fp);
157         audio_out_prepare(output);
158
159         while (fp) {
160                 usleep(10000); /* 10ms */
161         }
162
163         printf("FINISHED!!!\n");
164
165         audio_out_unprepare(output);
166         audio_out_destroy(output);
167
168         printf("play done\n");
169 }
170
171 static void _play_file_sample_sync(char *file, int frequency, int ch, int type)
172 {
173         audio_out_h output;
174         int file_size = 0;
175         int read_bytes = 0;
176         int buffer_size = 0;
177         char *buf = NULL;
178         FILE *fp = NULL;
179         int i = 0;
180
181         if (ch < 0 || ch > 2)
182                 ch = 0;
183
184         fp = fopen(file, "r");
185         if (fp == NULL) {
186                 printf("open failed\n");
187                 return;
188         }
189         /* Get the size */
190         fseek(fp, 0, SEEK_END);
191         file_size = ftell(fp);
192         fseek(fp, 0, SEEK_SET);
193
194         printf("Play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
195         audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
196
197         audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
198         audio_out_prepare(output);
199
200         audio_out_get_buffer_size(output, &buffer_size);
201         buf = (char *)malloc(buffer_size);
202         if (buf == NULL) {
203                 printf("malloc failed\n");
204                 goto EXIT;
205         }
206
207         printf("Press any key to continue....\n");
208         getchar();
209         printf("Start to read[%d] from file and write : \n", buffer_size);
210
211         while (file_size > 0) {
212                 read_bytes = fread(buf, 1, buffer_size, fp);
213                 printf(".");
214                 i++;
215                 if (i % 10 == 0)
216                         printf("|");
217                 if (i % 100 == 0) {
218                         printf("\n");
219                         i = 0;
220                 }
221                 audio_out_write(output, buf, read_bytes);
222                 file_size = file_size - read_bytes;
223                 usleep(10000);
224         }
225
226 EXIT:
227         audio_out_unprepare(output);
228         audio_out_destroy(output);
229
230         if (buf)
231                 free(buf);
232         if (fp)
233                 fclose(fp);
234
235         printf("\nEOS!!!! Play done\n");
236 }
237
238 static int _record_and_play(int length, int num, int ch)
239 {
240         int ret, size, i;
241         audio_in_h input = NULL;
242         FILE *fp = NULL;
243         char *buffer = NULL;
244
245         ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input);
246         if (ret != AUDIO_IO_ERROR_NONE) {
247                 printf("audio in create error = 0x%x\n", ret);
248                 return -1;
249         }
250
251         ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
252         if (ret != AUDIO_IO_ERROR_NONE) {
253                 printf("audio_in_set_state_changed_cb failed. \n");
254                 goto error;
255         }
256
257         ret = audio_in_prepare(input);
258         if (ret != AUDIO_IO_ERROR_NONE) {
259                 printf("ERROR, prepare\n");
260                 goto error;
261         }
262
263         fp = fopen(DUMP_FILE, "wb+");
264         if (fp == NULL) {
265                 printf("ERROR, file open failed\n");
266                 goto error;
267         }
268
269         ret = audio_in_get_buffer_size(input, &size);
270         if (ret != AUDIO_IO_ERROR_NONE) {
271                 printf("ERROR, prepare\n");
272                 goto error;
273         }
274
275         size = length;
276         buffer = alloca(size);
277
278         for (i = 0; i < num; i++) {
279                 printf("### read = (%d/%d) ============== ", i, num);
280                 if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
281                         fwrite(buffer, size, sizeof(char), fp);
282                         printf("PASS, size=%d, ret=0x%x\n", size, ret);
283                 } else {
284                         printf("FAIL, size=%d, ret=0x%x\n", size, ret);
285                 }
286         }
287
288         fclose(fp);
289
290         audio_in_unprepare(input);
291         audio_in_destroy(input);
292
293         _play_file(DUMP_FILE, length, num, ch);
294
295         return 0;
296
297 error:
298         audio_in_destroy(input);
299         if (fp)
300                 fclose(fp);
301         return -1;
302 }
303
304 static int _direct_loopback()
305 {
306         int ret, size;
307         audio_in_h input = NULL;
308         audio_out_h output = NULL;
309         char *buffer = NULL;
310         int i = 0;
311
312         ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
313         if (ret != AUDIO_IO_ERROR_NONE) {
314                 printf("audio_in_create_ex failed. \n");
315                 return ret;
316         }
317         ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
318         if (ret != AUDIO_IO_ERROR_NONE) {
319                 printf("audio_out_create_new failed. \n");
320                 goto exit;
321         }
322
323         ret = audio_in_prepare(input);
324         if (ret != 0) {
325                 printf("audio_in_prepare failed, err(0x%x)\n", ret);
326                 goto exit;
327         }
328
329         ret = audio_in_get_buffer_size(input, &size);
330         if (ret != AUDIO_IO_ERROR_NONE) {
331                 printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
332                 goto exit;
333         }
334
335         printf("size(%d)\n", size);
336         buffer = alloca(size);
337
338         ret = audio_out_prepare(output);
339         if (ret != 0) {
340                 printf("audio_out_prepare failed, err(0x%x)\n", ret);
341                 goto exit;
342         }
343
344         if (buffer == NULL) {
345                 printf("buffer is null\n");
346                 ret = -1;
347                 goto exit;
348         }
349
350         printf("Start to loopback(read/write) with [16kHz/MONO/S16LE]!!!\n");
351
352         while (1) {
353                 ret = audio_in_read(input, (void *)buffer, size);
354                 if (ret > AUDIO_IO_ERROR_NONE) {
355                         ret = audio_out_write(output, buffer, size);
356                         if (ret > AUDIO_IO_ERROR_NONE) {
357                                 printf(".");
358                                 i++;
359                                 if (i % 10 == 0)
360                                         printf("|");
361                                 if (i % 100 == 0) {
362                                         printf("\n");
363                                         i = 0;
364                                 }
365                         } else {
366                                 printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
367                         }
368                 } else {
369                         printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
370                 }
371                 usleep(10000);
372         }
373
374 exit:
375         if (input)
376                 audio_in_destroy(input);
377         if (output)
378                 audio_out_destroy(output);
379
380         return ret;
381
382 }
383
384 audio_in_h input;
385 audio_out_h output;
386
387 FILE *fp_w = NULL;
388
389 sound_stream_info_h g_stream_info_read_h = NULL;
390 sound_stream_info_h g_stream_info_write_h = NULL;
391
392 static void _focus_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask,
393                                         sound_stream_focus_state_e focus_state,
394                                         sound_stream_focus_change_reason_e reason_for_change, int sound_behavior,
395                                         const char *additional_info, void *user_data)
396 {
397         printf("*** focus_callback_read is called, stream_info(%p, read(%p)/write(%p)) ***\n",
398                         stream_info, g_stream_info_read_h, g_stream_info_write_h);
399         printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n",
400                         reason_for_change, additional_info, user_data);
401         printf(" - focus_state is :%d \n", focus_state);
402
403         return;
404 }
405
406 static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *user_data)
407 {
408         const void *buffer = NULL;
409         unsigned int len = (unsigned int)nbytes;
410
411         if (len == 0)
412                 return;
413
414         audio_in_peek(handle, &buffer, &len);
415         if (fp_w)
416                 fwrite(buffer, sizeof(char), len, fp_w);
417         audio_in_drop(handle);
418 }
419
420 static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
421 {
422         short *buffer = NULL;
423         int i = 0;
424
425         if (nbytes == 0)
426                 return;
427
428         buffer = (short *)malloc(nbytes);
429         if (buffer == NULL) {
430                 printf("malloc failed\n");
431                 return;
432         }
433         memset(buffer, 0, nbytes);
434
435         for (i = 0; i < nbytes / 2; i += 2) {
436                 buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */
437                 buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel]; /* right */
438                 test_wav.left_channel += 1;
439                 if (test_wav.left_channel >= TABLE_SIZE)
440                         test_wav.left_channel -= TABLE_SIZE;
441                 test_wav.right_channel += 3;
442                 if (test_wav.right_channel >= TABLE_SIZE)
443                         test_wav.right_channel -= TABLE_SIZE;
444         }
445
446         audio_out_write(handle, buffer, nbytes);
447
448         free(buffer);
449 }
450
451 int _convert_cmd_and_run(char cmd, int mode)
452 {
453         int ret = 0;
454         switch (cmd) {
455         case 'P':
456                 if (mode & 0x01)
457                         ret = audio_out_prepare(output);
458                 if (mode & 0x02)
459                         ret = audio_in_prepare(input);
460                 break;
461         case 'u':
462                 if (mode & 0x01)
463                         ret = audio_out_unprepare(output);
464                 if (mode & 0x02)
465                         ret = audio_in_unprepare(input);
466                 break;
467         case 'p':
468                 if (mode & 0x01)
469                         ret = audio_out_pause(output);
470                 if (mode & 0x02)
471                         ret = audio_in_pause(input);
472                 break;
473         case 'r':
474                 if (mode & 0x01)
475                         ret = audio_out_resume(output);
476                 if (mode & 0x02)
477                         ret = audio_in_resume(input);
478                 break;
479         case 'd':
480                 if (mode & 0x01)
481                         ret = audio_out_drain(output);
482                 break;
483         case 'f':
484                 if (mode & 0x01)
485                         ret = audio_out_flush(output);
486                 if (mode & 0x02)
487                         ret = audio_in_flush(input);
488                 break;
489         case 'i':
490                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
491                 if (ret)
492                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
493                 break;
494         case 'q': /* quit */
495                 ret = 1;
496                 break;
497         default:
498                 ret = 1;
499                 break;
500         }
501         return ret;
502 }
503
504 int audio_io_async_test(int mode)
505 {
506         int ret, size;
507         char *buffer = NULL;
508         int i = 0;
509
510         char cmd = 0;
511         int cmd_ret;
512
513         int write_mode = (mode & 0x01);
514         int read_mode = (mode & 0x02);
515
516         sound_stream_focus_state_e playback_focus_state;
517         sound_stream_focus_state_e recording_focus_state;
518
519         if ((write_mode == 0) && (read_mode == 0)) {
520                 printf("not vaild mode.\n");
521                 return 0;
522         }
523
524         if (read_mode) {
525
526                 printf("audio_in_create\n");
527                 ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
528                 if (ret != AUDIO_IO_ERROR_NONE) {
529                         printf("audio_in_create_ex failed. \n");
530                         return 0;
531                 }
532                 printf("audio_in_create success!!! [%p]\n", input);
533
534                 ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL);
535                 if (ret != AUDIO_IO_ERROR_NONE) {
536                         printf("audio_in_set_stream_cb failed. \n");
537                         goto EXIT;
538                 }
539                 printf("audio_in_set_stream_cb success!!! [%p]\n", input);
540
541                 ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
542                 if (ret != AUDIO_IO_ERROR_NONE) {
543                         printf("audio_out_set_state_changed_cb failed. \n");
544                         goto EXIT;
545                 }
546                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", input);
547
548                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_read_h);
549                 if (ret) {
550                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
551                         goto EXIT;
552                 }
553                 ret = audio_in_set_sound_stream_info(input, g_stream_info_read_h);
554                 if (ret)
555                         printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
556
557                 ret = sound_manager_acquire_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
558                 if (ret) {
559                         printf("fail to sound_manager_acquire_focus() for RECORDING, ret(0x%x)\n", ret);
560                         goto EXIT;
561                 }
562
563                 fp_w = fopen("/tmp/pcm_w.raw", "w");
564         }
565
566         if (write_mode) {
567                 printf("before audio_out_create_new\n");
568                 getchar();
569
570                 printf("audio_out_create_new\n");
571                 ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
572                 if (ret != AUDIO_IO_ERROR_NONE) {
573                         printf("audio_out_create_new failed. \n");
574                         goto EXIT;
575                 }
576                 printf("audio_out_create_new success!!! [%p]\n", output);
577
578                 ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL);
579                 if (ret != AUDIO_IO_ERROR_NONE) {
580                         printf("audio_out_set_stream_cb failed. \n");
581                         goto EXIT;
582                 }
583                 printf("audio_out_set_stream_cb success!!! [%p]\n", output);
584
585                 ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
586                 if (ret != AUDIO_IO_ERROR_NONE) {
587                         printf("audio_out_set_state_changed_cb failed. \n");
588                         goto EXIT;
589                 }
590                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", output);
591
592                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
593                 if (ret) {
594                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
595                         goto EXIT;
596                 }
597
598                 ret = audio_out_set_sound_stream_info(output, g_stream_info_write_h);
599                 if (ret)
600                         printf("fail to audio_out_set_sound_stream_info(), ret(0x%x)\n", ret);
601
602                 ret = sound_manager_acquire_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
603                 if (ret) {
604                         printf("fail to sound_manager_acquire_focus() for PLAYBACK, ret(0x%x)\n", ret);
605                         goto EXIT;
606                 }
607
608                 /* generate wave data */
609                 for (i = 0; i < TABLE_SIZE; i++)
610                         test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
611                 test_wav.left_channel = test_wav.right_channel = 0;
612         }
613
614         if (read_mode) {
615                 printf("before audio_in_prepare\n");
616                 getchar();
617                 printf("audio_in_prepare\n");
618                 ret = audio_in_prepare(input);
619                 if (ret != 0) {
620                         printf("audio_in_prepare failed, err(0x%x)\n", ret);
621                         audio_in_destroy(input);
622                         goto EXIT;
623                 } else {
624                         ret = audio_in_get_buffer_size(input, &size);
625                         if (ret != AUDIO_IO_ERROR_NONE) {
626                                 printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
627                                 goto EXIT;
628                         } else {
629                                 printf("size(%d)\n", size);
630                                 buffer = alloca(size);
631                         }
632                 }
633
634                 if (buffer == NULL) {
635                         printf("buffer is null\n");
636                         goto EXIT;
637                 }
638         }
639
640         if (write_mode) {
641                 printf("before audio_out_prepare\n");
642                 getchar();
643                 printf("audio_out_prepare\n");
644                 ret = audio_out_prepare(output);
645                 if (ret != 0) {
646                         printf("audio_out_prepare failed, err(0x%x)\n", ret);
647                         audio_out_destroy(output);
648                         goto EXIT;
649                 }
650         }
651
652         do {
653                 printf("command(q:quit) : ");
654                 ret = getchar();
655                 if (ret == EOF)
656                         goto EXIT;
657                 cmd = (char)ret;
658                 if (cmd != '\n')
659                         getchar();
660                 cmd_ret = _convert_cmd_and_run(cmd, mode);
661                 printf("  - result code : %d\n", cmd_ret);
662         } while (cmd != 'q');
663
664 EXIT:
665         if (read_mode) {
666                 if (input) {
667                         printf("audio_in_unprepare\n");
668                         audio_in_unprepare(input);
669                         printf("audio_in_destroy\n");
670                         audio_in_destroy(input);
671                         input = NULL;
672                 }
673
674                 if (fp_w) {
675                         fclose(fp_w);
676                         fp_w = NULL;
677                 }
678
679                 if (g_stream_info_read_h) {
680                         ret = sound_manager_get_focus_state(g_stream_info_read_h, NULL, &recording_focus_state);
681                         if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
682                                 ret = sound_manager_release_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
683                                 if (ret)
684                                         printf("fail to sound_manager_release_focus() for recording, ret(0x%x)\n", ret);
685                         }
686                         ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
687                         if (ret)
688                                 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
689                         g_stream_info_read_h = NULL;
690                 }
691         }
692
693         if (write_mode) {
694                 if (output) {
695                         printf("audio_out_unprepare\n");
696                         audio_out_unprepare(output);
697                         printf("audio_out_destroy\n");
698                         audio_out_destroy(output);
699                 }
700
701                 if (g_stream_info_write_h) {
702                         ret = sound_manager_get_focus_state(g_stream_info_write_h, &playback_focus_state, NULL);
703                         if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
704                                 ret = sound_manager_release_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
705                                 if (ret)
706                                         printf("fail to sound_manager_release_focus() for playback, ret(0x%x)\n", ret);
707                         }
708                         ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
709                         if (ret)
710                                 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
711                         g_stream_info_write_h = NULL;
712                 }
713         }
714
715         return 0;
716 }
717
718 int main(int argc, char **argv)
719 {
720         setbuf(stdout, NULL);
721
722         if (argc == 2 && !strcmp(argv[1], "loopback")) {
723                 _direct_loopback();
724         } else if (argc == 3 && !strcmp(argv[1], "async")) {
725                 audio_io_async_test(atoi(argv[2]));
726         } else if (argc == 4) {
727                 int channel_idx = atoi(argv[3]);
728                 if (channel_idx < 0 || channel_idx > 8) {
729                         printf("Invalid channel\n");
730                         return 0;
731                 }
732                 printf("run with [%s][%s][%s]\n", argv[1], argv[2], argv[3]);
733                 _record_and_play(atoi(argv[1]), atoi(argv[2]), channel_idx);
734         } else if (argc == 6) {
735                 if (strcmp(argv[1], "play") == 0)
736                         _play_file_sample_sync(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
737                 else if (strcmp(argv[1], "playasync") == 0)
738                         _play_file_sample_async(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
739         } else {
740                 printf("- Usages :\n");
741                 printf("- # audio_io_test loopback\n");
742                 printf("- # audio_io_test [length to read] [number of iteration] [channels]\n");
743                 printf("- # audio_io_test async [write(1) | read(2)]\n");
744                 printf("- # audio_io_test play [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
745                 printf("- # audio_io_test playasync [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
746         }
747         return 0;
748 }