Fix SVACE defects
[platform/core/api/audio-io.git] / test / audio_io_test.c
index 2e0f754..b1f81b9 100644 (file)
@@ -21,6 +21,7 @@
 #include <math.h>
 #include <pthread.h>
 #include <sound_manager.h>
+#include <sound_manager_internal.h>
 #include <audio_io.h>
 #include <time.h>
 
 #endif
 
 #define TABLE_SIZE   (200)
+
 typedef struct {
        float sine[TABLE_SIZE];
        int left_channel;
        int right_channel;
 } test_wav_t;
+
+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_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,
@@ -177,8 +190,8 @@ static void _play_file_sample_async(char *file, int frequency, int ch, int type)
 static void _play_file_sample_sync(char *file, int frequency, int ch, int type)
 {
        audio_out_h output;
-       int file_size = 0;
-       int read_bytes = 0;
+       long file_size = 0;
+       size_t read_bytes = 0;
        int buffer_size = 0;
        char *buf = NULL;
        FILE *fp = NULL;
@@ -197,14 +210,14 @@ static void _play_file_sample_sync(char *file, int frequency, int ch, int type)
        file_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
 
-       printf("Play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
+       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);
+       buf = (char *)calloc(buffer_size, sizeof(char));
        if (buf == NULL) {
                printf("malloc failed\n");
                goto EXIT;
@@ -216,20 +229,32 @@ static void _play_file_sample_sync(char *file, int frequency, int ch, int type)
 
        while (file_size > 0) {
                read_bytes = fread(buf, 1, buffer_size, fp);
+
                printf(".");
-               i++;
-               if (i % 10 == 0)
+               if (++i % 10 == 0)
                        printf("|");
                if (i % 100 == 0) {
                        printf("\n");
                        i = 0;
                }
-               audio_out_write(output, buf, read_bytes);
-               file_size = file_size - read_bytes;
+
+               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);
 
@@ -238,7 +263,144 @@ EXIT:
        if (fp)
                fclose(fp);
 
-       printf("\nEOS!!!! Play done\n");
+       printf("Play done\n");
+}
+
+static void *_record_echo_cancel_thread(void *userdata)
+{
+       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;
+                       }
+               }
+
+               if (!found) {
+                       printf("Failed to find reference device");
+                       goto out;
+               }
+
+               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;
+               }
+       }
+
+       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;
+       }
+
+       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);
+       }
+
+       if (device_list)
+               if (sound_manager_free_device_list(device_list) != SOUND_MANAGER_ERROR_NONE)
+                       printf("fail to free device list\n");
+
+       pthread_exit(0);
+
+       return NULL;
+}
+
+static void _test_echo_cancel(const char *filename, int rate, int channels, int ref_deviceid, bool capture)
+{
+       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);
+       }
+
+       pthread_join(thread_echo_cancel, (void**)&status);
 }
 
 static int _record_and_play(int length, int num, int ch)
@@ -426,7 +588,7 @@ 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 i = 0;
+       size_t i = 0;
 
        if (nbytes == 0)
                return;
@@ -934,7 +1096,7 @@ int audio_io_test_read_write()
 
        void in_stream_cb(audio_in_h handle, size_t nbytes, void *user_data) {
                void* buf = NULL;
-               unsigned int size = nbytes;
+               unsigned int size = (unsigned int)nbytes;
 
                audio_in_peek(handle, (const void **)buf, &size);
                audio_in_drop(handle);
@@ -1022,7 +1184,7 @@ int main(int argc, char **argv)
                audio_io_test_read_write();
        } else if (argc == 4) {
                int channel_idx = atoi(argv[3]);
-               if (channel_idx < 0 || channel_idx > 8) {
+               if (channel_idx <= 0 || channel_idx > 16) {
                        printf("Invalid channel\n");
                        return 0;
                }
@@ -1033,6 +1195,8 @@ int main(int argc, char **argv)
                        _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("- Usages :\n");
                printf("- # audio_io_test loopback\n");
@@ -1041,6 +1205,7 @@ int main(int argc, char **argv)
                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;
 }