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