Revise exception handling
[platform/core/api/audio-io.git] / test / audio_io_test.c
index 08c800a..3b5d98f 100644 (file)
@@ -36,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) {
@@ -45,25 +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);
+       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");
 }
@@ -109,6 +144,8 @@ void play_file_sample(char *file, int frequency, int ch, int type)
                return;
        }
 
+       getchar();
+
        while (file_size > 0) {
                read_bytes = fread(buf, 1, buffer_size, fp);
                printf("Read %d Requested - %d\n", read_bytes, buffer_size);
@@ -129,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) {
@@ -156,12 +189,12 @@ 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);
                                }
                        }
                }
@@ -172,9 +205,12 @@ int audio_io_test(int length, int num, int ch)
                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()
@@ -185,7 +221,7 @@ 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) {
@@ -207,13 +243,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);
                        }
                }
        }
@@ -230,31 +267,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_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;
+               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 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, err(0x%x)\n", ret);
-                       return 0;
+                       goto exit;
                } else {
                        printf("size(%d)\n", size);
                        buffer = alloca(size);
@@ -264,13 +300,13 @@ int audio_io_loopback_test()
        ret = audio_out_prepare(output);
        if (ret != 0) {
                printf("audio_out_prepare failed, err(0x%x)\n", ret);
-               audio_out_destroy(output);
-               return 0;
+               goto exit;
        }
 
        if (buffer == NULL) {
                printf("buffer is null\n");
-               return 0;
+               ret = -1;
+               goto exit;
        }
 
        while (1) {
@@ -285,6 +321,14 @@ int audio_io_loopback_test()
                        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;
@@ -348,16 +392,6 @@ static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *u
        }
 }
 
-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;
@@ -460,9 +494,9 @@ int audio_io_async_test(int mode)
                        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);
+               ret = audio_in_set_sound_stream_info(input, g_stream_info_read_h);
                if (ret)
-                       printf("fail to audio_in_set_stream_info(), ret(0x%x)\n", ret);
+                       printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
 
                ret = sound_manager_acquire_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
                if (ret) {
@@ -504,9 +538,10 @@ int audio_io_async_test(int mode)
                        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);
+
+               ret = audio_out_set_sound_stream_info(output, g_stream_info_write_h);
                if (ret)
-                       printf("fail to audio_out_set_stream_info(), ret(0x%x)\n", ret);
+                       printf("fail to audio_out_set_sound_stream_info(), ret(0x%x)\n", ret);
 
                ret = sound_manager_acquire_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
                if (ret) {
@@ -559,11 +594,11 @@ int audio_io_async_test(int mode)
        }
 
        do {
-               int gotchar;
                printf("command(q:quit) : ");
-               gotchar = getchar();
-               if (gotchar == EOF)
+               ret = getchar();
+               if (ret == EOF)
                        goto EXIT;
+               cmd = (char)ret;
                if (cmd != '\n')
                        getchar();
                cmd_ret = _convert_cmd_and_run(cmd, mode);