Fix SVACE defects
[platform/core/api/audio-io.git] / test / audio_io_test.c
index 70e6e3a..b1f81b9 100644 (file)
 #include <string.h>
 #include <unistd.h>
 #include <math.h>
+#include <pthread.h>
 #include <sound_manager.h>
+#include <sound_manager_internal.h>
 #include <audio_io.h>
+#include <time.h>
 
 #ifndef M_PI
 #define M_PI  (3.14159265)
 #endif
 
 #define TABLE_SIZE   (200)
+
 typedef struct {
        float sine[TABLE_SIZE];
        int left_channel;
        int right_channel;
 } test_wav_t;
-test_wav_t test_wav;
 
-static int ch_table[3] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO };
+typedef struct {
+       char filename[FILENAME_MAX];
+       int rate;
+       int channels;
+       int ref_deviceid;
+} test_echo_cancel_t;
+
+test_wav_t test_wav;
 
+static int ch_table[] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO,
+                          AUDIO_CHANNEL_MULTI_3, AUDIO_CHANNEL_MULTI_4, AUDIO_CHANNEL_MULTI_5,
+                          AUDIO_CHANNEL_MULTI_6, AUDIO_CHANNEL_MULTI_7, AUDIO_CHANNEL_MULTI_8,
+                          AUDIO_CHANNEL_MULTI_9, AUDIO_CHANNEL_MULTI_10, AUDIO_CHANNEL_MULTI_11,
+                          AUDIO_CHANNEL_MULTI_12, AUDIO_CHANNEL_MULTI_13, AUDIO_CHANNEL_MULTI_14,
+                          AUDIO_CHANNEL_MULTI_15, AUDIO_CHANNEL_MULTI_16 };
 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)
+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",
+       printf(">>> _audio_in_state_cb() : handle(%p), (%d,%s) => (%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)
+static void _audio_out_state_cb(audio_out_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",
+       printf(">>> _audio_out_state_cb() : handle(%p), (%d,%s) => (%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)
+static void _play_file(char *file, int length, int num, int ch)
 {
        int i = 0;
        int ret = 0;
@@ -69,10 +87,10 @@ void play_file(char *file, int length, int num, int ch)
                return;
        }
 
-       printf("start to play [%s][%d][%d]\n", file, total_length, ch);
-       printf("create\n");
+       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, total_length, fp) != length)
+       if (fread(buf, 1, total_length, fp) != total_length)
                printf("error!!!!\n");
 
        ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
@@ -80,7 +98,7 @@ void play_file(char *file, int length, int num, int ch)
                printf("audio_out_set_state_changed_cb failed. \n");
        }
 
-       printf("prepare\n");
+       printf("  > prepare\n");
        audio_out_prepare(output);
 
        char *pbuf = buf;
@@ -90,33 +108,99 @@ void play_file(char *file, int length, int num, int ch)
                pbuf += length;
 
        }
-       printf("unprepare\n");
+       printf("  < unprepare\n");
        audio_out_drain(output);
        audio_out_unprepare(output);
 
-       printf("destroy\n");
+       printf(" < destroy\n");
        audio_out_destroy(output);
 
        fclose(fp);
        free(buf);
 
-       printf("play done\n");
+       printf("play done\n");
 }
 
 #define DUMP_FILE "/root/test.raw"
 
-void play_file_sample(char *file, int frequency, int ch, int type)
+FILE *fp = NULL;
+
+static void _audio_out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data)
+{
+       char *buf = NULL;
+       int read_bytes = 0;
+       int written = 0;
+
+       if (fp == NULL) {
+               printf("FILE is NULL\n");
+               return;
+       }
+
+       buf = (char *)malloc(nbytes);
+       if (!buf) {
+               printf("malloc(%zu) failed\n", nbytes);
+               return;
+       }
+
+       read_bytes = fread(buf, 1, nbytes, fp);
+
+       written = audio_out_write(handle, buf, read_bytes);
+       printf("written : %6d/%6d (requested %zu)\n", written, read_bytes, nbytes);
+
+       if (read_bytes < nbytes) {
+               printf("EOS!!!!\n");
+               fclose(fp);
+               fp = NULL;
+       }
+       free(buf);
+}
+
+static void _play_file_sample_async(char *file, int frequency, int ch, int type)
 {
        audio_out_h output;
        int file_size = 0;
-       int read_bytes = 0;
+
+       if (ch < 0 || ch > 2)
+               ch = 0;
+
+       fp = fopen(file, "r");
+       if (fp == NULL) {
+               printf("open failed\n");
+               return;
+       }
+       printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
+       audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
+
+       audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
+       audio_out_set_stream_cb(output, _audio_out_stream_cb, fp);
+       audio_out_prepare(output);
+
+       while (fp) {
+               usleep(10000); /* 10ms */
+       }
+
+       printf("FINISHED!!!\n");
+
+       audio_out_unprepare(output);
+       audio_out_destroy(output);
+
+       printf("play done\n");
+}
+
+static void _play_file_sample_sync(char *file, int frequency, int ch, int type)
+{
+       audio_out_h output;
+       long file_size = 0;
+       size_t read_bytes = 0;
        int buffer_size = 0;
        char *buf = NULL;
+       FILE *fp = NULL;
+       int i = 0;
 
        if (ch < 0 || ch > 2)
                ch = 0;
 
-       FILE *fp = fopen(file, "r");
+       fp = fopen(file, "r");
        if (fp == NULL) {
                printf("open failed\n");
                return;
@@ -126,149 +210,272 @@ void play_file_sample(char *file, int frequency, int ch, int type)
        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_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
-       else
-               audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8, &output);
+       printf("Play [%s] of size [%ld] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
+       audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
 
+       audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
        audio_out_prepare(output);
-       audio_out_get_buffer_size(output, &buffer_size);
 
-       buf = (char *)malloc(buffer_size);
+       audio_out_get_buffer_size(output, &buffer_size);
+       buf = (char *)calloc(buffer_size, sizeof(char));
        if (buf == NULL) {
                printf("malloc failed\n");
-               audio_out_unprepare(output);
-               audio_out_destroy(output);
-               fclose(fp);
-               return;
+               goto EXIT;
        }
 
+       printf("Press any key to continue....\n");
        getchar();
+       printf("Start to read[%d] from file and write : \n", buffer_size);
 
        while (file_size > 0) {
                read_bytes = fread(buf, 1, buffer_size, fp);
-               printf("Read %d Requested - %d\n", read_bytes, buffer_size);
-               audio_out_write(output, buf, read_bytes);
-               file_size = file_size - read_bytes;
+
+               printf(".");
+               if (++i % 10 == 0)
+                       printf("|");
+               if (i % 100 == 0) {
+                       printf("\n");
+                       i = 0;
+               }
+
+               if (read_bytes != buffer_size) {
+                       if (feof(fp)) {
+                               printf("\n EOS! this is a last buffer! \n");
+                       } else if (ferror(fp)) {
+                               printf("\n Error!\n");
+                               break;
+                       }
+               }
+
+               audio_out_write(output, buf, (unsigned int)read_bytes);
+
+               file_size -= (long)read_bytes;
+               usleep(10000);
        }
 
+EXIT:
+       printf("\n Cleanup now!\n");
        audio_out_unprepare(output);
        audio_out_destroy(output);
 
-       free(buf);
-       fclose(fp);
-       printf("play done\n");
+       if (buf)
+               free(buf);
+       if (fp)
+               fclose(fp);
+
+       printf("Play done\n");
 }
 
-int audio_io_test(int length, int num, int ch)
+static void *_record_echo_cancel_thread(void *userdata)
 {
-       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_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;
+       int ret, size;
+       sound_stream_info_h stream_info;
+       audio_in_h input = NULL;
+       FILE *fp = NULL;
+       char *buffer = NULL;
+
+       sound_device_list_h device_list = NULL;
+       sound_device_h device;
+       bool found = false;
+       int id;
+
+       test_echo_cancel_t *data = (test_echo_cancel_t *)userdata;
+
+       ret = audio_in_create(data->rate, ch_table[data->channels], AUDIO_SAMPLE_TYPE_S16_LE, &input);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("audio in create error = 0x%x\n", ret);
+               goto out;
+       }
+
+       ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info);
+       if (ret) {
+               printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
+               goto out;
+       }
+
+       if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != SOUND_MANAGER_ERROR_NONE) {
+               printf("fail to get device list");
+               goto out;
+       }
+
+       if (data->ref_deviceid != SOUND_MANAGER_STREAM_NO_REFERENCE_DEVICE) {
+               while (sound_manager_get_next_device(device_list, &device) == SOUND_MANAGER_ERROR_NONE) {
+                       if (sound_manager_get_device_id(device, &id) != SOUND_MANAGER_ERROR_NONE)
+                               goto out;
+
+                       if (data->ref_deviceid == id) {
+                               found = true;
+                               break;
+                       }
                }
 
-               ret = audio_in_prepare(input);
-               if (ret != 0) {
-                       printf("ERROR, prepare\n");
-                       goto error;
+               if (!found) {
+                       printf("Failed to find reference device");
+                       goto out;
                }
 
-               FILE *fp = fopen(DUMP_FILE, "wb+");
-               if (fp == NULL) {
-                       printf("ERROR, file open failed\n");
-                       goto error;
+               ret = sound_manager_set_echo_cancel_reference_device(stream_info, device);
+               if (ret != SOUND_MANAGER_ERROR_NONE) {
+                       printf("fail to sound_manager_set_echo_cancel_reference_device(), ret(0x%x)\n", ret);
+                       goto out;
                }
+       }
 
-               if ((ret = audio_in_get_buffer_size(input, &size)) == AUDIO_IO_ERROR_NONE) {
-                       size = length;
-                       char *buffer = alloca(size);
+       ret = audio_in_set_sound_stream_info(input, stream_info);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
+               goto out;
+       }
 
-                       for (i = 0; i < num; 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=0x%x\n", size, ret);
-                               } else {
-                                       printf("FAIL, size=%d, ret=0x%x\n", size, ret);
-                               }
-                       }
+       ret = audio_in_prepare(input);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("ERROR, prepare\n");
+               goto out;
+       }
+
+       ret = audio_in_get_buffer_size(input, &size);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("ERROR, get_buffer_size\n");
+               goto out;
+       }
+
+       fp = fopen(data->filename, "wb+");
+       if (fp == NULL) {
+               printf("ERROR, file open failed\n");
+               goto out;
+       }
+
+       buffer = alloca(size);
+
+       while (1) {
+               ret = audio_in_read(input, (void *)buffer, size);
+               if (ret <= 0) {
+                       printf("FAIL, size=%d, ret=0x%x\n", size, ret);
+                       goto out;
                }
 
+               fwrite(buffer, size, sizeof(char), fp);
+       }
+
+out:
+       if (fp)
                fclose(fp);
 
+       if (input) {
                audio_in_unprepare(input);
                audio_in_destroy(input);
        }
 
-       play_file(DUMP_FILE, length, num, ch);
+       if (device_list)
+               if (sound_manager_free_device_list(device_list) != SOUND_MANAGER_ERROR_NONE)
+                       printf("fail to free device list\n");
 
-       return 0;
-error:
-       audio_in_destroy(input);
-       return -1;
+       pthread_exit(0);
+
+       return NULL;
 }
 
-int audio_io_loopback_in_test()
+static void _test_echo_cancel(const char *filename, int rate, int channels, int ref_deviceid, bool capture)
 {
-       int ret, size;
-       audio_in_h input;
-       FILE *fp = fopen("/tmp/dump_test.raw", "wb+");
+       int status;
+       pthread_t thread_echo_cancel;
+       test_echo_cancel_t data;
+
+       snprintf(data.filename, FILENAME_MAX, "%s", filename);
+       data.rate = rate;
+       data.channels = channels;
+       data.ref_deviceid = ref_deviceid;
+       pthread_create(&thread_echo_cancel, NULL, _record_echo_cancel_thread, &data);
+
+       if (capture) {
+               pthread_t thread_capture;
+               test_echo_cancel_t data_capture;
+
+               snprintf(data_capture.filename, FILENAME_MAX, "%s_%s", filename, "norm");
+               data_capture.rate = rate;
+               data_capture.channels = channels;
+               data_capture.ref_deviceid = SOUND_MANAGER_STREAM_NO_REFERENCE_DEVICE;
+
+               pthread_create(&thread_capture, NULL, _record_echo_cancel_thread, &data_capture);
+               pthread_join(thread_capture, (void**)&status);
+       }
 
-       if (fp == NULL) {
-               printf("open failed \n");
+       pthread_join(thread_echo_cancel, (void**)&status);
+}
+
+static int _record_and_play(int length, int num, int ch)
+{
+       int ret, size, i;
+       audio_in_h input = NULL;
+       FILE *fp = NULL;
+       char *buffer = NULL;
+
+       ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("audio in create error = 0x%x\n", ret);
                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_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");
-                       goto exit;
-               }
+       ret = audio_in_prepare(input);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("ERROR, prepare\n");
+               goto error;
+       }
 
-               ret = audio_in_get_buffer_size(input, &size);
-               if (ret != AUDIO_IO_ERROR_NONE) {
-                       printf("audio_in_get_buffer_size failed.\n");
-                       goto exit;
-               }
+       fp = fopen(DUMP_FILE, "wb+");
+       if (fp == NULL) {
+               printf("ERROR, file open failed\n");
+               goto error;
+       }
 
-               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=0x%x\n", size, ret);
-                       } else {
-                               printf("FAIL, size=%d, ret=0x%x\n", size, ret);
-                       }
+       ret = audio_in_get_buffer_size(input, &size);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("ERROR, prepare\n");
+               goto error;
+       }
+
+       size = length;
+       buffer = alloca(size);
+
+       for (i = 0; i < num; 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=0x%x\n", size, ret);
+               } else {
+                       printf("FAIL, size=%d, ret=0x%x\n", size, ret);
                }
        }
 
- exit:
+       fclose(fp);
+
+       audio_in_unprepare(input);
        audio_in_destroy(input);
 
-       fclose(fp);
+       _play_file(DUMP_FILE, length, num, ch);
 
-       return ret;
+       return 0;
 
+error:
+       audio_in_destroy(input);
+       if (fp)
+               fclose(fp);
+       return -1;
 }
 
-int audio_io_loopback_test()
+static int _direct_loopback()
 {
        int ret, size;
-       audio_in_h input;
-       audio_out_h output;
+       audio_in_h input = NULL;
+       audio_out_h output = NULL;
        char *buffer = NULL;
+       int i = 0;
 
        ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
        if (ret != AUDIO_IO_ERROR_NONE) {
@@ -277,50 +484,69 @@ int audio_io_loopback_test()
        }
        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 ret;
+               printf("audio_out_create_new failed. \n");
+               goto exit;
        }
 
        ret = audio_in_prepare(input);
        if (ret != 0) {
                printf("audio_in_prepare failed, err(0x%x)\n", ret);
-               audio_in_destroy(input);
-               return ret;
-       } else {
-               ret = audio_in_get_buffer_size(input, &size);
-               if (ret != AUDIO_IO_ERROR_NONE) {
-                       printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
-                       return 0;
-               } else {
-                       printf("size(%d)\n", size);
-                       buffer = alloca(size);
-               }
+               goto exit;
        }
 
+       ret = audio_in_get_buffer_size(input, &size);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
+               goto exit;
+       }
+
+       printf("size(%d)\n", size);
+       buffer = alloca(size);
+
        ret = audio_out_prepare(output);
        if (ret != 0) {
                printf("audio_out_prepare failed, err(0x%x)\n", ret);
-               audio_out_destroy(output);
-               return ret;
+               goto exit;
        }
 
        if (buffer == NULL) {
                printf("buffer is null\n");
-               return -1;
+               ret = -1;
+               goto exit;
        }
 
+       printf("Start to loopback(read/write) with [16kHz/MONO/S16LE]!!!\n");
+
        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)
-                               printf("audio read/write success. buffer(%p), size(%d)\n", buffer, size);
-                       else
+                       if (ret > AUDIO_IO_ERROR_NONE) {
+                               printf(".");
+                               i++;
+                               if (i % 10 == 0)
+                                       printf("|");
+                               if (i % 100 == 0) {
+                                       printf("\n");
+                                       i = 0;
+                               }
+                       } else {
                                printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
-               } else
+                       }
+               } else {
                        printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
+               }
+               usleep(10000);
        }
 
+exit:
+       if (input)
+               audio_in_destroy(input);
+       if (output)
+               audio_out_destroy(output);
+
+       return ret;
+
 }
 
 audio_in_h input;
@@ -331,11 +557,15 @@ FILE *fp_w = NULL;
 sound_stream_info_h g_stream_info_read_h = NULL;
 sound_stream_info_h g_stream_info_write_h = NULL;
 
-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)
+static void _focus_cb(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)
 {
-       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);
+       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);
        printf(" - focus_state is :%d \n", focus_state);
 
        return;
@@ -346,42 +576,44 @@ static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *use
        const void *buffer = NULL;
        unsigned int len = (unsigned int)nbytes;
 
-       if (len > 0) {
-               audio_in_peek(handle, &buffer, &len);
-               if (fp_w)
-                       fwrite(buffer, sizeof(char), len, fp_w);
-               audio_in_drop(handle);
-       }
+       if (len == 0)
+               return;
+
+       audio_in_peek(handle, &buffer, &len);
+       if (fp_w)
+               fwrite(buffer, sizeof(char), len, fp_w);
+       audio_in_drop(handle);
 }
 
 static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
 {
        short *buffer = NULL;
-       int i = 0;
+       size_t i = 0;
 
-       if (nbytes > 0) {
-               buffer = (short *)malloc(nbytes);
-               if (buffer == NULL) {
-                       printf("malloc failed\n");
-                       return;
-               }
-               memset(buffer, 0, nbytes);
+       if (nbytes == 0)
+               return;
 
-               for (i = 0; i < nbytes / 2; i += 2) {
-                       buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */
-                       buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel];    /* right */
-                       test_wav.left_channel += 1;
-                       if (test_wav.left_channel >= TABLE_SIZE)
-                               test_wav.left_channel -= TABLE_SIZE;
-                       test_wav.right_channel += 3;
-                       if (test_wav.right_channel >= TABLE_SIZE)
-                               test_wav.right_channel -= TABLE_SIZE;
-               }
+       buffer = (short *)malloc(nbytes);
+       if (buffer == NULL) {
+               printf("malloc failed\n");
+               return;
+       }
+       memset(buffer, 0, nbytes);
+
+       for (i = 0; i < nbytes / 2; i += 2) {
+               buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */
+               buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel]; /* right */
+               test_wav.left_channel += 1;
+               if (test_wav.left_channel >= TABLE_SIZE)
+                       test_wav.left_channel -= TABLE_SIZE;
+               test_wav.right_channel += 3;
+               if (test_wav.right_channel >= TABLE_SIZE)
+                       test_wav.right_channel -= TABLE_SIZE;
+       }
 
-               audio_out_write(handle, buffer, nbytes);
+       audio_out_write(handle, buffer, nbytes);
 
-               free(buffer);
-       }
+       free(buffer);
 }
 
 int _convert_cmd_and_run(char cmd, int mode)
@@ -423,11 +655,11 @@ int _convert_cmd_and_run(char cmd, int mode)
                        ret = audio_in_flush(input);
                break;
        case 'i':
-               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_write_h);
+               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
                if (ret)
                        printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
                break;
-       case 'q':                                       /* quit */
+       case 'q': /* quit */
                ret = 1;
                break;
        default:
@@ -481,7 +713,7 @@ int audio_io_async_test(int mode)
                }
                printf("audio_out_set_state_changed_cb success!!! [%p]\n", input);
 
-               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_read_h);
+               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_read_h);
                if (ret) {
                        printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
                        goto EXIT;
@@ -500,16 +732,16 @@ int audio_io_async_test(int mode)
        }
 
        if (write_mode) {
-               printf("before audio_out_create\n");
+               printf("before audio_out_create_new\n");
                getchar();
 
-               printf("audio_out_create\n");
+               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");
+                       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) {
@@ -525,7 +757,7 @@ int audio_io_async_test(int mode)
                }
                printf("audio_out_set_state_changed_cb success!!! [%p]\n", output);
 
-               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, focus_callback, NULL, &g_stream_info_write_h);
+               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
                if (ret) {
                        printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
                        goto EXIT;
@@ -651,30 +883,329 @@ EXIT:
        return 0;
 }
 
+#define THREAD_MAX 10
+#define TEST_COUNT 500
+static void *thread_stress_test_audio_in(void *data)
+{
+       int i, j;
+       int rate;
+       char *buf;
+       int buffer_size;
+       audio_channel_e ch;
+       audio_sample_type_e type;
+       unsigned int seed = (unsigned int)time(NULL);
+
+       audio_in_h input = (audio_in_h)data;
+
+       audio_in_prepare(input);
+       audio_in_get_buffer_size(input, &buffer_size);
+       buf = (char *)malloc(buffer_size);
+       if (buf == NULL) {
+               printf("malloc failed\n");
+               goto EXIT;
+       }
+
+       for (i=0; i<TEST_COUNT; i++) {
+               switch (rand_r(&seed) % 8) {
+                       case 0:
+                               audio_in_prepare(input);
+                               break;
+                       case 1:
+                               audio_in_read(input, buf, buffer_size);
+                               break;
+                       case 2:
+                               audio_in_pause(input);
+                               break;
+                       case 3:
+                               audio_in_resume(input);
+                               break;
+                       case 4:
+                               audio_in_flush(input);
+                               break;
+                       case 5:
+                               audio_in_unprepare(input);
+                               break;
+                       case 6:
+                               audio_in_prepare(input);
+                               for  (j = 0; j < 10; j++)
+                                       audio_in_read(input, buf, buffer_size);
+                               break;
+                       case 7:
+                               audio_in_get_buffer_size(input, &buffer_size);
+                               audio_in_get_sample_rate(input, &rate);
+                               audio_in_get_channel(input, &ch);
+                               audio_in_get_sample_type(input, &type);
+                               break;
+                       default:
+                               break;
+               }
+               if ((i % (TEST_COUNT/10)) == 0)
+                       printf("audio_in: %x thread. count(%d/%d)\n", (int)pthread_self(), i, TEST_COUNT);
+       }
+       audio_in_unprepare(input);
+
+EXIT:
+       if (buf) {
+               free(buf);
+               buf = NULL;
+       }
+
+       pthread_exit(0);
+}
+
+static void *thread_stress_test_audio_out(void *data)
+{
+       int i, j;
+       int rate;
+       char *buf;
+       int buffer_size;
+       audio_channel_e ch;
+       audio_sample_type_e type;
+       unsigned int seed = (unsigned int)time(NULL);
+
+       audio_out_h output = (audio_out_h)data;
+
+       srand(time(NULL));
+
+       audio_out_get_buffer_size(output, &buffer_size);
+
+       buf = (char *)malloc(buffer_size);
+       if (buf == NULL) {
+               printf("malloc failed\n");
+               goto EXIT;
+       }
+
+       audio_out_prepare(output);
+       for (i = 0; i < TEST_COUNT; i++) {
+               switch (rand_r(&seed) % 9) {
+                       case 0:
+                               audio_out_prepare(output);
+                               break;
+                       case 1:
+                               audio_out_write(output, buf, buffer_size);
+                               break;
+                       case 2:
+                               audio_out_pause(output);
+                               break;
+                       case 3:
+                               audio_out_resume(output);
+                               break;
+                       case 4:
+                               audio_out_drain(output);
+                               break;
+                       case 5:
+                               audio_out_flush(output);
+                               break;
+                       case 6:
+                               audio_out_write(output, buf, buffer_size);
+                               break;
+                       case 7:
+                               audio_out_prepare(output);
+                               for  (j = 0; j < 10; j++)
+                                       audio_out_write(output, buf, buffer_size);
+                               break;
+                       case 8:
+                               audio_out_get_buffer_size(output, &buffer_size);
+                               audio_out_get_sample_rate(output, &rate);
+                               audio_out_get_channel(output, &ch);
+                               audio_out_get_sample_type(output, &type);
+                               break;
+                       default:
+                               break;
+               }
+               if ((i % (TEST_COUNT/10)) == 0)
+                       printf("audio_out: %x thread. count(%d/%d)\n", (int)pthread_self(), i, TEST_COUNT);
+       }
+       audio_out_unprepare(output);
+
+EXIT:
+       if (buf)
+               free(buf);
+
+       pthread_exit(0);
+}
+
+#define OUT_HANDLE_MAX 1000
+#define IN_HANDLE_MAX  10
+void audio_io_test_handle_max()
+{
+       audio_out_h output[OUT_HANDLE_MAX] = { 0, };
+       audio_in_h input[IN_HANDLE_MAX] = { 0, };
+       int i;
+       int success = 0;
+       int ret;
+
+       printf("==============================================\n");
+       printf("playback handle creation test. try to create %d handles\n", OUT_HANDLE_MAX);
+       for (i = 0; i < OUT_HANDLE_MAX; i++) {
+               ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[i]);
+               if (ret != AUDIO_IO_ERROR_NONE) {
+                       printf("audio_out_create_new failed.\n");
+                       return;
+               }
+
+               ret = audio_out_prepare(output[i]);
+               if (ret == AUDIO_IO_ERROR_NONE)
+                       success++;
+       }
+       printf("created handle (%d/%d)\n", success, OUT_HANDLE_MAX);
+
+       for (i = 0; i < OUT_HANDLE_MAX; i++)
+               audio_out_destroy(output[i]);
+
+       /* pulseaudio oom when IN_HANDLE_MAX is set over 50 */
+       printf("capture handle creation test. try to create %d handles\n", IN_HANDLE_MAX);
+       success = 0;
+       for (i = 0; i < IN_HANDLE_MAX; i++) {
+               ret = audio_in_create(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input[i]);
+               if (ret != AUDIO_IO_ERROR_NONE) {
+                       printf("audio_in_create_new failed.\n");
+                       return;
+               }
+
+               ret = audio_in_prepare(input[i]);
+               if (ret == AUDIO_IO_ERROR_NONE)
+                       success++;
+       }
+       printf("created handle (%d/%d)\n", success, IN_HANDLE_MAX);
+
+       for (i = 0; i < IN_HANDLE_MAX; i++)
+               audio_in_destroy(input[i]);
+}
+
+int audio_io_test_read_write()
+{
+       int i;
+       int ret;
+       audio_out_h output[2];
+       audio_in_h input;
+
+       int status;
+       pthread_t t[THREAD_MAX];
+
+       void out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data) {
+               char * buf = (char *)malloc(nbytes);
+               if (!buf) {
+                       printf("malloc(%zu) failed\n", nbytes);
+                       return;
+               }
+               audio_out_write(handle, buf, nbytes);
+               free(buf);
+               return;
+       }
+
+       void in_stream_cb(audio_in_h handle, size_t nbytes, void *user_data) {
+               void* buf = NULL;
+               unsigned int size = (unsigned int)nbytes;
+
+               audio_in_peek(handle, (const void **)buf, &size);
+               audio_in_drop(handle);
+               return;
+       }
+
+       void state_changed_cb(audio_out_h handle, audio_io_state_e previous, audio_io_state_e current, bool by_policy, void *user_data) {
+               return;
+       }
+
+       /* audio_out sync */
+       ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[0]);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("audio_out_create_new failed. \n");
+               return 0;
+       }
+       audio_out_set_state_changed_cb(output[0], state_changed_cb, NULL);
+
+       /* audio_out async */
+       ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[1]);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("audio_out_create_new failed. \n");
+               return 0;
+       }
+       audio_out_set_stream_cb(output[1], out_stream_cb, NULL);
+       audio_out_set_state_changed_cb(output[1], state_changed_cb, NULL);
+
+       /* audio_in */
+       ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
+       if (ret != AUDIO_IO_ERROR_NONE) {
+               printf("audio in create error = 0x%x\n", ret);
+               return -1;
+       }
+       audio_in_set_stream_cb(input, in_stream_cb, NULL);
+
+       printf("==============================================\n");
+       printf("audio out sync test\n");
+       for (i = 0; i < THREAD_MAX; i++)
+               pthread_create(&t[i], NULL, thread_stress_test_audio_out, output[0]);
+
+       for (i = 0; i < THREAD_MAX; i++) {
+               pthread_join(t[i], (void**)&status);
+               printf("thread %d finished\n", i);
+       }
+
+       printf("==============================================\n");
+       printf("audio out async test\n");
+       for (i = 0; i < THREAD_MAX; i++)
+               pthread_create(&t[i], NULL, thread_stress_test_audio_out, output[1]);
+
+       for (i = 0; i < THREAD_MAX; i++) {
+               pthread_join(t[i], (void**)&status);
+               printf("thread %d finished\n", i);
+       }
+
+       printf("==============================================\n");
+       printf("audio in test\n");
+       for (i = 0; i < THREAD_MAX; i++)
+               pthread_create(&t[i], NULL, thread_stress_test_audio_in, input);
+
+       for (i = 0; i < THREAD_MAX; i++) {
+               pthread_join(t[i], (void**)&status);
+               printf("thread %d finished\n", i);
+       }
+
+       audio_out_destroy(output[0]);
+       audio_out_destroy(output[1]);
+       audio_in_destroy(input);
+
+       printf("stress test end\n");
+
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
-       if (argc == 2 && !strcmp(argv[1], "call-forwarding-loop")) {
-               audio_io_loopback_test();
-       } else if (argc == 2 && !strcmp(argv[1], "call-forwarding-in")) {
-               audio_io_loopback_in_test();
+       setbuf(stdout, NULL);
+
+       if (argc == 2 && !strcmp(argv[1], "loopback")) {
+               _direct_loopback();
        } else if (argc == 3 && !strcmp(argv[1], "async")) {
                audio_io_async_test(atoi(argv[2]));
+       } else if (argc == 2 && !strcmp(argv[1], "stress")) {
+               audio_io_test_handle_max();
+               audio_io_test_read_write();
        } else if (argc == 4) {
                int channel_idx = atoi(argv[3]);
-               if (channel_idx < 0 || channel_idx > 2) {
+               if (channel_idx <= 0 || channel_idx > 16) {
                        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]), channel_idx);
+               _record_and_play(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]));
+               if (strcmp(argv[1], "play") == 0)
+                       _play_file_sample_sync(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
+               else if (strcmp(argv[1], "playasync") == 0)
+                       _play_file_sample_async(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
+       } else if (argc == 7 && strcmp(argv[1], "echocancel") == 0) {
+                       _test_echo_cancel(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), !!atoi(argv[6]));
        } else {
-               printf("1. usage : audio_io_test call-forwarding-loop\n");
-               printf("2. usage : audio_io_test call-forwarding-in\n");
-               printf("3. usage : audio_io_test [length to read] [number of iteration] [channels]\n");
-               printf("4. usage : audio_io_test async [write(1) | read(2)]\n");
-               printf("5. Uasge : audio_io_test play [filename] [sample rate] [channels] [type(0:U8)]\n");
+               printf("- Usages :\n");
+               printf("- # audio_io_test loopback\n");
+               printf("- # audio_io_test stress\n");
+               printf("- # audio_io_test [length to read] [number of iteration] [channels]\n");
+               printf("- # audio_io_test async [write(1) | read(2)]\n");
+               printf("- # audio_io_test play [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
+               printf("- # audio_io_test playasync [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
+               printf("- # audio_io_test echocancel [filename] [sample rate] [channels] [ref_dev] [capture without EC]\n");
        }
        return 0;
 }