Remove 3.0 deprecated API implementation
[platform/core/api/audio-io.git] / test / audio_io_test.c
index a5a7775..61ea8eb 100644 (file)
@@ -22,9 +22,6 @@
 #include <sound_manager.h>
 #include <audio_io.h>
 
-//#define _NEW_SOUND_MANAGER_API_
-#define _SESSION_SOUND_MANAGER_API_
-
 #ifndef M_PI
 #define M_PI  (3.14159265)
 #endif
@@ -39,8 +36,25 @@ test_wav_t test_wav;
 
 static int ch_table[3] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO };
 
-void play_file(char *file, int length, int ch)
+static char *state_str[] = { "IDLE", "RUNNING", "PAUSED" };
+
+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)
 {
+       printf(">>> _audio_in_state_cb() : handle(%p), previous(%d,%s) => current(%d,%s), by_policy(%d), user_data(%p)\n",
+                       handle, previous, state_str[previous], current, state_str[current], by_policy, user_data);
+}
+
+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)
+{
+       printf(">>> _audio_out_state_cb() : handle(%p), previous(%d,%s) => current(%d,%s), by_policy(%d), user_data(%p)\n",
+                       handle, previous, state_str[previous], current, state_str[current], by_policy, user_data);
+}
+
+void play_file(char *file, int length, int num, int ch)
+{
+       int i = 0;
+       int ret = 0;
+       int total_length = length * num;
        audio_out_h output;
        FILE *fp = fopen(file, "r");
        if (fp == NULL) {
@@ -48,27 +62,43 @@ void play_file(char *file, int length, int ch)
                return;
        }
 
-       char *buf = malloc(length);
+       char *buf = malloc(total_length);
        if (buf == NULL) {
                printf("malloc failed\n");
                fclose(fp);
                return;
        }
 
-       printf("start to play [%s][%d][%d]\n", file, length, ch);
-       //audio_out_create(44100, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
+       printf("start to play [%s][%d][%d]\n", file, total_length, ch);
+       printf("create\n");
        audio_out_create_new(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
-       if (fread(buf, 1, length, fp) != length) {
+       if (fread(buf, 1, total_length, fp) != length)
                printf("error!!!!\n");
+
+       ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("audio_out_set_state_changed_cb failed. \n");
        }
 
+       printf("prepare\n");
        audio_out_prepare(output);
-       audio_out_write(output, buf, length);
+
+       char *pbuf = buf;
+       for (i = 0; i < num; i++) {
+               printf("### write = (%d/%d) ============== \n", i, num);
+               audio_out_write(output, pbuf, length);
+               pbuf += length;
+
+       }
+       printf("unprepare\n");
+       audio_out_drain(output);
        audio_out_unprepare(output);
 
+       printf("destroy\n");
        audio_out_destroy(output);
 
        fclose(fp);
+       free(buf);
 
        printf("play done\n");
 }
@@ -83,28 +113,25 @@ void play_file_sample(char *file, int frequency, int ch, int type)
        int buffer_size = 0;
        char *buf = NULL;
 
-       if (ch < 0 || ch > 2) {
+       if (ch < 0 || ch > 2)
                ch = 0;
-       }
 
        FILE *fp = fopen(file, "r");
        if (fp == NULL) {
                printf("open failed\n");
                return;
        }
-       /*Get the size */
+       /* Get the size */
        fseek(fp, 0, SEEK_END);
        file_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
 
        printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
-       if (type) {
-               //audio_out_create(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
+       if (type)
                audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
-       } else {
-               //audio_out_create(frequency, ch_table[ch] ,AUDIO_SAMPLE_TYPE_U8, SOUND_TYPE_MEDIA, &output);
+       else
                audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8, &output);
-       }
+
        audio_out_prepare(output);
        audio_out_get_buffer_size(output, &buffer_size);
 
@@ -116,7 +143,8 @@ void play_file_sample(char *file, int frequency, int ch, int type)
                fclose(fp);
                return;
        }
-       //audio_out_prepare(output);
+
+       getchar();
 
        while (file_size > 0) {
                read_bytes = fread(buf, 1, buffer_size, fp);
@@ -138,26 +166,22 @@ int audio_io_test(int length, int num, int ch)
        int ret, size, i;
        audio_in_h input;
        if ((ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) {
-               ret = audio_in_ignore_session(input);
-               if (ret != 0) {
-                       printf("ERROR, set session mix\n");
-                       audio_in_destroy(input);
-                       return 0;
+               ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
+               if (ret != AUDIO_IO_ERROR_NONE) {
+                       printf("audio_in_set_state_changed_cb failed. \n");
+                       goto error;
                }
 
                ret = audio_in_prepare(input);
                if (ret != 0) {
                        printf("ERROR, prepare\n");
-                       audio_in_destroy(input);
-                       return 0;
+                       goto error;
                }
 
                FILE *fp = fopen(DUMP_FILE, "wb+");
-
                if (fp == NULL) {
                        printf("ERROR, file open failed\n");
-                       audio_in_destroy(input);
-                       return 0;
+                       goto error;
                }
 
                if ((ret = audio_in_get_buffer_size(input, &size)) == AUDIO_IO_ERROR_NONE) {
@@ -165,24 +189,28 @@ int audio_io_test(int length, int num, int ch)
                        char *buffer = alloca(size);
 
                        for (i = 0; i < num; i++) {
-                               printf("### loop = %d ============== \n", i);
+                               printf("### read = (%d/%d) ============== ", i, num);
                                if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
                                        fwrite(buffer, size, sizeof(char), fp);
-                                       printf("PASS, size=%d, ret=%d\n", size, ret);
+                                       printf("PASS, size=%d, ret=0x%x\n", size, ret);
                                } else {
-                                       printf("FAIL, size=%d, ret=%d\n", size, ret);
+                                       printf("FAIL, size=%d, ret=0x%x\n", size, ret);
                                }
                        }
                }
 
                fclose(fp);
 
+               audio_in_unprepare(input);
                audio_in_destroy(input);
        }
 
-       play_file(DUMP_FILE, length * num, ch);
+       play_file(DUMP_FILE, length, num, ch);
 
-       return 1;
+       return 0;
+error:
+       audio_in_destroy(input);
+       return -1;
 }
 
 int audio_io_loopback_in_test()
@@ -193,16 +221,10 @@ int audio_io_loopback_in_test()
 
        if (fp == NULL) {
                printf("open failed \n");
-               return 0;
+               return -1;
        }
 
        if ((ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input)) == AUDIO_IO_ERROR_NONE) {
-               ret = audio_in_ignore_session(input);
-               if (ret != 0) {
-                       printf("ERROR, set session mix\n");
-                       goto exit;
-               }
-
                ret = audio_in_prepare(input);
                if (ret != 0) {
                        printf("ERROR, prepare\n");
@@ -215,13 +237,14 @@ int audio_io_loopback_in_test()
                        goto exit;
                }
 
+               char *buffer = alloca(size);
+
                while (1) {
-                       char *buffer = alloca(size);
                        if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
                                fwrite(buffer, size, sizeof(char), fp);
-                               printf("PASS, size=%d, ret=%d\n", size, ret);
+                               printf("PASS, size=%d, ret=0x%x\n", size, ret);
                        } else {
-                               printf("FAIL, size=%d, ret=%d\n", size, ret);
+                               printf("FAIL, size=%d, ret=0x%x\n", size, ret);
                        }
                }
        }
@@ -238,32 +261,30 @@ int audio_io_loopback_in_test()
 int audio_io_loopback_test()
 {
        int ret, size;
-       audio_in_h input;
-       audio_out_h output;
+       audio_in_h input = NULL;
+       audio_out_h output = NULL;
        char *buffer = NULL;
 
        ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
        if (ret != AUDIO_IO_ERROR_NONE) {
                printf("audio_in_create_ex failed. \n");
-               return 0;
+               return ret;
        }
-       //ret = audio_out_create(16000, AUDIO_CHANNEL_MONO , AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_CALL, &output);
        ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
        if (ret != AUDIO_IO_ERROR_NONE) {
-               printf("audio_out_create failed. \n");
-               return 0;
+               printf("audio_out_create_new failed. \n");
+               goto exit;
        }
 
        ret = audio_in_prepare(input);
        if (ret != 0) {
-               printf("audio_in_prepare failed.\n");
-               audio_in_destroy(input);
-               return 0;
+               printf("audio_in_prepare failed, err(0x%x)\n", ret);
+               goto exit;
        } else {
                ret = audio_in_get_buffer_size(input, &size);
                if (ret != AUDIO_IO_ERROR_NONE) {
-                       printf("audio_in_get_buffer_size failed.\n");
-                       return 0;
+                       printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
+                       goto exit;
                } else {
                        printf("size(%d)\n", size);
                        buffer = alloca(size);
@@ -272,29 +293,36 @@ int audio_io_loopback_test()
 
        ret = audio_out_prepare(output);
        if (ret != 0) {
-               printf("audio_out_prepare failed.\n");
-               audio_out_destroy(output);
-               return 0;
+               printf("audio_out_prepare failed, err(0x%x)\n", ret);
+               goto exit;
        }
 
        if (buffer == NULL) {
                printf("buffer is null\n");
-               return 0;
+               ret = -1;
+               goto exit;
        }
 
        while (1) {
                ret = audio_in_read(input, (void *)buffer, size);
                if (ret > AUDIO_IO_ERROR_NONE) {
                        ret = audio_out_write(output, buffer, size);
-                       if (ret > AUDIO_IO_ERROR_NONE) {
+                       if (ret > AUDIO_IO_ERROR_NONE)
                                printf("audio read/write success. buffer(%p), size(%d)\n", buffer, size);
-                       } else {
+                       else
                                printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
-                       }
                } else
                        printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
        }
 
+exit:
+       if (input)
+               audio_in_destroy(input);
+       if (output)
+               audio_out_destroy(output);
+
+       return ret;
+
 }
 
 audio_in_h input;
@@ -302,73 +330,28 @@ audio_out_h output;
 
 FILE *fp_w = NULL;
 
-#ifdef _NEW_SOUND_MANAGER_API_
 sound_stream_info_h g_stream_info_read_h = NULL;
 sound_stream_info_h g_stream_info_write_h = NULL;
 
-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)
+static void focus_callback(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
+                               sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *additional_info, void *user_data)
 {
-       int ret = 0;
-       sound_stream_focus_state_e playback_focus_state;
-       sound_stream_focus_state_e recording_focus_state;
-       printf("*** focus_callback_read is called, stream_info(%p) ***\n", stream_info);
+       printf("*** focus_callback_read is called, stream_info(%p, read(%p)/write(%p)) ***\n", stream_info, g_stream_info_read_h, g_stream_info_write_h);
        printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data);
-       ret = sound_manager_get_focus_state(stream_info, &playback_focus_state, &recording_focus_state);
-       if (!ret)
-               printf(" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state);
-       if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
-               printf(" -- PLAYBACK_FOCUS acquired\n");
-       }
-       if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
-               printf(" -- FOCUS_RECORDING acquired\n");
-       }
-       printf("*** focus_callback_read is ended, stream_info(%p) ****\n", stream_info);
-       return;
-}
+       printf(" - focus_state is :%d \n", focus_state);
 
-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)
-{
-       int ret = 0;
-       sound_stream_focus_state_e playback_focus_state;
-       sound_stream_focus_state_e recording_focus_state;
-       printf("*** focus_callback_write is called, stream_info(%p) ***\n", stream_info);
-       printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n", reason_for_change, additional_info, user_data);
-       ret = sound_manager_get_focus_state(stream_info, &playback_focus_state, &recording_focus_state);
-       if (!ret)
-               printf(" - focus_state(playback_focus:%d, recording_focus:%d)\n", playback_focus_state, recording_focus_state);
-       if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
-               printf(" -- PLAYBACK_FOCUS acquired\n");
-       }
-       if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
-               printf(" -- FOCUS_RECORDING acquired\n");
-       }
-       printf("*** focus_callback_write is ended, stream_info(%p) ****\n", stream_info);
        return;
 }
-#endif
-
-static void interrupted_callback_read(audio_io_interrupted_code_e code, void *user_data)
-{
-       printf("*** interrupted_callback_read is called, code(%d), user_data(%p) ***\n", code, user_data);
-}
-
-static void interrupted_callback_write(audio_io_interrupted_code_e code, void *user_data)
-{
-       printf("*** interrupted_callback_write is called, code(%d), user_data(%p) ***\n", code, user_data);
-}
 
 static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *user_data)
 {
        const void *buffer = NULL;
        unsigned int len = (unsigned int)nbytes;
 
-       //printf("_audio_io_stream_read_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data);
-
        if (len > 0) {
                audio_in_peek(handle, &buffer, &len);
-               if (fp_w) {
+               if (fp_w)
                        fwrite(buffer, sizeof(char), len, fp_w);
-               }
                audio_in_drop(handle);
        }
 }
@@ -376,11 +359,8 @@ static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *use
 static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
 {
        short *buffer = NULL;
-       int ret = 0;
        int i = 0;
 
-       //printf("_audio_io_stream_write_cb : handle=%p, nbytes=%d, user_data=%p\n", handle, nbytes, user_data);
-
        if (nbytes > 0) {
                buffer = (short *)malloc(nbytes);
                if (buffer == NULL) {
@@ -400,25 +380,12 @@ static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *u
                                test_wav.right_channel -= TABLE_SIZE;
                }
 
-               ret = audio_out_write(handle, buffer, nbytes);
-               if (ret > AUDIO_IO_ERROR_NONE) {
-                       //printf("audio write success. buffer(%p), nbytes(%d)\n", buffer, nbytes);
-               }
+               audio_out_write(handle, buffer, nbytes);
 
                free(buffer);
        }
 }
 
-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)
-{
-       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);
-}
-
-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)
-{
-       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);
-}
-
 int _convert_cmd_and_run(char cmd, int mode)
 {
        int ret = 0;
@@ -450,7 +417,6 @@ int _convert_cmd_and_run(char cmd, int mode)
        case 'd':
                if (mode & 0x01)
                        ret = audio_out_drain(output);
-               //if (mode & 0x02)  ret = audio_in_drain(input);
                break;
        case 'f':
                if (mode & 0x01)
@@ -459,22 +425,9 @@ int _convert_cmd_and_run(char cmd, int mode)
                        ret = audio_in_flush(input);
                break;
        case 'i':
-#ifdef _NEW_SOUND_MANAGER_API_
-               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_write, NULL, &g_stream_info_write_h);
-               if (ret) {
+               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_write_h);
+               if (ret)
                        printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
-               }
-#endif
-#ifdef _SESSION_SOUND_MANAGER_API_
-               ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
-               if (ret) {
-                       printf("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
-               }
-               ret = sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
-               if (ret) {
-                       printf("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
-               }
-#endif
                break;
        case 'q':                                       /* quit */
                ret = 1;
@@ -498,6 +451,9 @@ int audio_io_async_test(int mode)
        int write_mode = (mode & 0x01);
        int read_mode = (mode & 0x02);
 
+       sound_stream_focus_state_e playback_focus_state;
+       sound_stream_focus_state_e recording_focus_state;
+
        if ((write_mode == 0) && (read_mode == 0)) {
                printf("not vaild mode.\n");
                return 0;
@@ -505,20 +461,6 @@ int audio_io_async_test(int mode)
 
        if (read_mode) {
 
-#ifdef _SESSION_SOUND_MANAGER_API_
-               printf("set session for capture.\n");
-
-               ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
-               if (ret) {
-                       printf("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
-               }
-
-               ret = sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
-               if (ret) {
-                       printf("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
-               }
-#endif
-
                printf("audio_in_create\n");
                ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
                if (ret != AUDIO_IO_ERROR_NONE) {
@@ -530,87 +472,80 @@ int audio_io_async_test(int mode)
                ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL);
                if (ret != AUDIO_IO_ERROR_NONE) {
                        printf("audio_in_set_stream_cb failed. \n");
-                       return 0;
+                       goto EXIT;
                }
                printf("audio_in_set_stream_cb success!!! [%p]\n", input);
 
                ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
                if (ret != AUDIO_IO_ERROR_NONE) {
                        printf("audio_out_set_state_changed_cb failed. \n");
-                       return 0;
+                       goto EXIT;
                }
                printf("audio_out_set_state_changed_cb success!!! [%p]\n", input);
 
-               fp_w = fopen("/tmp/pcm_w.raw", "w");
-
-#ifdef _NEW_SOUND_MANAGER_API_
-               //set stream type as SOUND_STREAM_TYPE_MEDIA
-               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_read, NULL, &g_stream_info_read_h);
+               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_read_h);
                if (ret) {
                        printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
+                       goto EXIT;
                }
-               ret = audio_in_set_stream_info(input, g_stream_info_read_h);
-#endif
+               ret = audio_in_set_sound_stream_info(input, g_stream_info_read_h);
+               if (ret)
+                       printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
 
-               ret = audio_in_set_interrupted_cb(input, interrupted_callback_read, NULL);
+               ret = sound_manager_acquire_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
+               if (ret) {
+                       printf("fail to sound_manager_acquire_focus() for RECORDING, ret(0x%x)\n", ret);
+                       goto EXIT;
+               }
+
+               fp_w = fopen("/tmp/pcm_w.raw", "w");
        }
 
        if (write_mode) {
-               printf("before audio_out_create\n");
+               printf("before audio_out_create_new\n");
                getchar();
 
-#ifdef _SESSION_SOUND_MANAGER_API_
-               printf("set session for playback.\n");
-
-               ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
-               if (ret) {
-                       printf("fail to sound_manager_set_session_type(), ret(0x%x)\n", ret);
-               }
-
-               ret = sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
-               if (ret) {
-                       printf("fail to sound_manager_set_media_session_option(), ret(0x%x)\n", ret);
-               }
-#endif
-
-               printf("audio_out_create\n");
-               //ret = audio_out_create(44100, AUDIO_CHANNEL_STEREO , AUDIO_SAMPLE_TYPE_S16_LE, SOUND_TYPE_MEDIA, &output);
+               printf("audio_out_create_new\n");
                ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
                if (ret != AUDIO_IO_ERROR_NONE) {
-                       printf("audio_out_create failed. \n");
-                       return 0;
+                       printf("audio_out_create_new failed. \n");
+                       goto EXIT;
                }
-               printf("audio_out_create success!!! [%p]\n", output);
+               printf("audio_out_create_new success!!! [%p]\n", output);
 
                ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL);
                if (ret != AUDIO_IO_ERROR_NONE) {
                        printf("audio_out_set_stream_cb failed. \n");
-                       return 0;
+                       goto EXIT;
                }
                printf("audio_out_set_stream_cb success!!! [%p]\n", output);
 
                ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
                if (ret != AUDIO_IO_ERROR_NONE) {
                        printf("audio_out_set_state_changed_cb failed. \n");
-                       return 0;
+                       goto EXIT;
                }
                printf("audio_out_set_state_changed_cb success!!! [%p]\n", output);
 
-#ifdef _NEW_SOUND_MANAGER_API_
-               //set stream type as SOUND_STREAM_TYPE_MEDIA
-               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback_write, NULL, &g_stream_info_write_h);
+               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_write_h);
                if (ret) {
                        printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
+                       goto EXIT;
                }
-               ret = audio_out_set_stream_info(output, g_stream_info_write_h);
-#endif
 
-               ret = audio_out_set_interrupted_cb(output, interrupted_callback_write, NULL);
+               ret = audio_out_set_sound_stream_info(output, g_stream_info_write_h);
+               if (ret)
+                       printf("fail to audio_out_set_sound_stream_info(), ret(0x%x)\n", ret);
 
-               //generate wave data
-               for (i = 0; i < TABLE_SIZE; i++) {
-                       test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
+               ret = sound_manager_acquire_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
+               if (ret) {
+                       printf("fail to sound_manager_acquire_focus() for PLAYBACK, ret(0x%x)\n", ret);
+                       goto EXIT;
                }
+
+               /* generate wave data */
+               for (i = 0; i < TABLE_SIZE; i++)
+                       test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
                test_wav.left_channel = test_wav.right_channel = 0;
        }
 
@@ -620,14 +555,14 @@ int audio_io_async_test(int mode)
                printf("audio_in_prepare\n");
                ret = audio_in_prepare(input);
                if (ret != 0) {
-                       printf("audio_in_prepare failed.\n");
+                       printf("audio_in_prepare failed, err(0x%x)\n", ret);
                        audio_in_destroy(input);
-                       return 0;
+                       goto EXIT;
                } else {
                        ret = audio_in_get_buffer_size(input, &size);
                        if (ret != AUDIO_IO_ERROR_NONE) {
-                               printf("audio_in_get_buffer_size failed.\n");
-                               return 0;
+                               printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
+                               goto EXIT;
                        } else {
                                printf("size(%d)\n", size);
                                buffer = alloca(size);
@@ -636,7 +571,7 @@ int audio_io_async_test(int mode)
 
                if (buffer == NULL) {
                        printf("buffer is null\n");
-                       return 0;
+                       goto EXIT;
                }
        }
 
@@ -646,63 +581,73 @@ int audio_io_async_test(int mode)
                printf("audio_out_prepare\n");
                ret = audio_out_prepare(output);
                if (ret != 0) {
-                       printf("audio_out_prepare failed.\n");
+                       printf("audio_out_prepare failed, err(0x%x)\n", ret);
                        audio_out_destroy(output);
-                       return 0;
+                       goto EXIT;
                }
        }
 
        do {
                printf("command(q:quit) : ");
-               cmd = (char)getchar();
+               ret = getchar();
+               if (ret == EOF)
+                       goto EXIT;
+               cmd = (char)ret;
                if (cmd != '\n')
                        getchar();
                cmd_ret = _convert_cmd_and_run(cmd, mode);
                printf("  - result code : %d\n", cmd_ret);
        } while (cmd != 'q');
 
-       //printf ("loop start\n");
-       //for (i=0; i<10; i++) {
-       //    printf ("-------- %d -------\n",i);
-       //    usleep (1000000);
-       //}
-
-       //getchar();
-
+EXIT:
        if (read_mode) {
-               printf("audio_in_unprepare\n");
-               audio_in_unprepare(input);
-               printf("audio_in_destroy\n");
-               audio_in_destroy(input);
+               if (input) {
+                       printf("audio_in_unprepare\n");
+                       audio_in_unprepare(input);
+                       printf("audio_in_destroy\n");
+                       audio_in_destroy(input);
+                       input = NULL;
+               }
 
-               fclose(fp_w);
-               fp_w = NULL;
+               if (fp_w) {
+                       fclose(fp_w);
+                       fp_w = NULL;
+               }
 
-#ifdef _NEW_SOUND_MANAGER_API_
-               ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
-               if (ret) {
-                       printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
-               } else {
+               if (g_stream_info_read_h) {
+                       ret = sound_manager_get_focus_state(g_stream_info_read_h, NULL, &recording_focus_state);
+                       if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
+                               ret = sound_manager_release_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
+                               if (ret)
+                                       printf("fail to sound_manager_release_focus() for recording, ret(0x%x)\n", ret);
+                       }
+                       ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
+                       if (ret)
+                               printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
                        g_stream_info_read_h = NULL;
                }
-#endif
        }
-       //getchar();
 
        if (write_mode) {
-               printf("audio_out_unprepare\n");
-               audio_out_unprepare(output);
-               printf("audio_out_destroy\n");
-               audio_out_destroy(output);
+               if (output) {
+                       printf("audio_out_unprepare\n");
+                       audio_out_unprepare(output);
+                       printf("audio_out_destroy\n");
+                       audio_out_destroy(output);
+               }
 
-#ifdef _NEW_SOUND_MANAGER_API_
-               ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
-               if (ret) {
-                       printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
-               } else {
+               if (g_stream_info_write_h) {
+                       ret = sound_manager_get_focus_state(g_stream_info_write_h, &playback_focus_state, NULL);
+                       if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
+                               ret = sound_manager_release_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
+                               if (ret)
+                                       printf("fail to sound_manager_release_focus() for playback, ret(0x%x)\n", ret);
+                       }
+                       ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
+                       if (ret)
+                               printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
                        g_stream_info_write_h = NULL;
                }
-#endif
        }
 
        return 0;
@@ -717,8 +662,13 @@ int main(int argc, char **argv)
        } else if (argc == 3 && !strcmp(argv[1], "async")) {
                audio_io_async_test(atoi(argv[2]));
        } else if (argc == 4) {
+               int channel_idx = atoi(argv[3]);
+               if (channel_idx < 0 || channel_idx > 2) {
+                       printf("Invalid channel\n");
+                       return 0;
+               }
                printf("run with [%s][%s][%s]\n", argv[1], argv[2], argv[3]);
-               audio_io_test(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
+               audio_io_test(atoi(argv[1]), atoi(argv[2]), channel_idx);
        } else if (argc == 6) {
                play_file_sample(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
        } else {