test: Add loopback functionality 46/299346/4
authorJaechul Lee <jcsing.lee@samsung.com>
Tue, 26 Sep 2023 08:15:22 +0000 (17:15 +0900)
committerJaechul Lee <jcsing.lee@samsung.com>
Tue, 10 Oct 2023 06:07:56 +0000 (15:07 +0900)
Usage:
audio_io_process_test -r 48000 -c 1 -m 0x0001 -a 0x0004 -d 1 | audio_io_process_test -p -r 48000 -c 1

[Version] 0.5.64
[Issue Type] Update

Change-Id: I106133725ae3122e8da519d2cc6424170a349436
Signed-off-by: Jaechul Lee <jcsing.lee@samsung.com>
packaging/capi-media-audio-io.spec
test/audio_io_process_test.c

index 604ae1b..c0ddac4 100644 (file)
@@ -1,6 +1,6 @@
 Name:           capi-media-audio-io
 Summary:        An Audio Input & Audio Output library in Tizen Native API
-Version:        0.5.63
+Version:        0.5.64
 Release:        0
 Group:          Multimedia/API
 License:        Apache-2.0
index 836cbdd..1ab527e 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <assert.h>
 #include <sound_manager.h>
 #include <audio_io.h>
 
@@ -42,6 +43,7 @@ static int ch_table[] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO,
                           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 void record()
 {
        int ret, size;
@@ -57,19 +59,19 @@ static void record()
 
        ret = audio_in_create(arg_samplerate, ch_table[arg_channels], AUDIO_SAMPLE_TYPE_S16_LE, &input);
        if (ret != AUDIO_IO_ERROR_NONE) {
-               printf("audio in create error = 0x%x\n", ret);
+               fprintf(stderr, "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);
+               fprintf(stderr, "fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
                goto out;
        }
 
        if (arg_reference_method_is_set) {
                if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != SOUND_MANAGER_ERROR_NONE) {
-                       printf("fail to get device list");
+                       fprintf(stderr, "fail to get device list");
                        goto out;
                }
 
@@ -84,13 +86,13 @@ static void record()
                }
 
                if (!found) {
-                       printf("Failed to find reference device");
+                       fprintf(stderr, "Failed to find reference device");
                        goto out;
                }
 
                ret = sound_manager_set_effect_method_with_reference(stream_info, arg_reference_method, device);
                if (ret != SOUND_MANAGER_ERROR_NONE) {
-                       printf("fail to sound_manager_set_audio_effect_reference_device(), ret(0x%x)\n", ret);
+                       fprintf(stderr, "fail to sound_manager_set_audio_effect_reference_device(), ret(0x%x)\n", ret);
                        goto out;
                }
 
@@ -102,17 +104,17 @@ static void record()
 
                        ret = sound_manager_get_effect_method_with_reference(stream_info, &method, &device_id);
                        if (ret != SOUND_MANAGER_ERROR_NONE) {
-                               printf("fail to sound_manager_get_audio_effect_reference_device(), ret(0x%x)\n", ret);
+                               fprintf(stderr, "fail to sound_manager_get_audio_effect_reference_device(), ret(0x%x)\n", ret);
                                goto out;
                        }
 
                        if (sound_manager_get_device_id(device, &id)) {
-                               printf("fail to sound_manager_device_id\n");
+                               fprintf(stderr, "fail to sound_manager_device_id\n");
                                goto out;
                        }
 
                        if (device_id != id || method != arg_reference_method) {
-                               printf("fail to verify aec args\n");
+                               fprintf(stderr, "fail to verify aec args\n");
                                goto out;
                        }
                }
@@ -121,7 +123,7 @@ static void record()
        if (arg_method_is_set) {
                ret = sound_manager_set_effect_method(stream_info, arg_method);
                if (ret != SOUND_MANAGER_ERROR_NONE) {
-                       printf("failed to sound_manager_set_audio_effect_method(), ret(0x%x)\n", ret);
+                       fprintf(stderr, "failed to sound_manager_set_audio_effect_method(), ret(0x%x)\n", ret);
                        goto out;
                }
 
@@ -131,12 +133,12 @@ static void record()
 
                        ret = sound_manager_get_effect_method(stream_info, &method);
                        if (ret != SOUND_MANAGER_ERROR_NONE) {
-                               printf("fail to sound_manager_get_audio_effect_reference_device(), ret(0x%x)\n", ret);
+                               fprintf(stderr, "fail to sound_manager_get_audio_effect_reference_device(), ret(0x%x)\n", ret);
                                goto out;
                        }
 
                        if (method != arg_method) {
-                               printf("fail to verify method args\n");
+                               fprintf(stderr, "fail to verify method args\n");
                                goto out;
                        }
                }
@@ -144,25 +146,29 @@ static void record()
 
        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);
+               fprintf(stderr, "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");
+               fprintf(stderr, "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");
+               fprintf(stderr, "ERROR, get_buffer_size\n");
                goto out;
        }
 
-       fp = fopen(arg_filename, "wb+");
-       if (fp == NULL) {
-               printf("ERROR, file open failed\n");
+       if (strcmp(arg_filename, ""))
+               fp = fopen(arg_filename, "wb");
+       else
+               fp = fdopen(dup(STDOUT_FILENO), "wb");
+
+       if (!fp) {
+               fprintf(stderr, "failed to open file(%s)", arg_filename);
                goto out;
        }
 
@@ -171,11 +177,16 @@ static void record()
        while (1) {
                ret = audio_in_read(input, (void *)buffer, size);
                if (ret <= 0) {
-                       printf("FAIL, size=%d, ret=0x%x\n", size, ret);
+                       fprintf(stderr, "FAIL, size=%d, ret=0x%x\n", size, ret);
+                       goto out;
+               }
+
+               if (size != ret) {
+                       fprintf(stderr, "Failed to read, size=%d, ret=%d\n", size, ret);
                        goto out;
                }
 
-               fwrite(buffer, size, sizeof(char), fp);
+               fwrite(buffer, sizeof(short), size/sizeof(short), fp);
        }
 
 out:
@@ -189,32 +200,68 @@ out:
 
        if (device_list)
                if (sound_manager_free_device_list(device_list) != SOUND_MANAGER_ERROR_NONE)
-                       printf("fail to free device list\n");
+                       fprintf(stderr, "fail to free device list\n");
 
        return;
 }
 
+static void playback_stdin()
+{
+       int ret, size;
+       char *buffer;
+       audio_out_h output;
+       FILE *fp;
+
+       ret = audio_out_create_new(arg_samplerate, ch_table[arg_channels], AUDIO_SAMPLE_TYPE_S16_LE, &output);
+       assert(ret == AUDIO_IO_ERROR_NONE);
+
+       fp = fdopen(dup(STDIN_FILENO), "r");
+       assert(fp);
+
+       ret = audio_out_get_buffer_size(output, &size);
+       assert(ret == AUDIO_IO_ERROR_NONE);
+
+       buffer = (char *)malloc(size);
+       assert(buffer);
+
+       ret = audio_out_prepare(output);
+       assert(ret == AUDIO_IO_ERROR_NONE);
+
+       while (1) {
+               if (fread(buffer, size, 1, fp) <= 0)
+                       assert(0);
+
+               ret = audio_out_write(output, (void *)buffer, size);
+               assert(ret > 0);
+       }
+
+       free(buffer);
+       fclose(fp);
+       audio_out_destroy(output);
+}
+
 static void print_usage()
 {
-       printf("Usage\n\n");
-       printf("audio_io_process_test -r [samplerate] -c [channels] -a [reference_method] -d [reference_device_id] -m [methods] -o [output filename]\n");
-       printf("e.g) audio_io_process_test -r 48000 -d 1 -m 0x0001 -a 0x0004 -d 1 -o filename (method:webrtc and voicecall)\n");
-
-       printf("\n");
-       printf("Please refer to the optional arguments as following.\n\n");
-       printf("Options\n");
-       printf("-a\t\taudio-effects with reference device_id like AEC\n");
-       printf("\t\te.g) refcopy:0x0001, aec-speex:0x0002, aec-webrtc:0x0004\n");
-
-       printf("-m\t\taudio-effects like noise-suppression\n");
-       printf("\t\te.g) rnnoise:0x0001, agc-speex:0x0002, pse:0x0200, doorbel:0x0100\n");
+       fprintf(stderr, "Usage\n\n");
+       fprintf(stderr, "audio_io_process_test -r [samplerate] -c [channels] -a [reference_method] -d [reference_device_id] -m [methods] -o [output filename]\n");
+       fprintf(stderr, "e.g) audio_io_process_test -r 48000 -d 1 -m 0x0001 -a 0x0004 -d 1 -o filename (method:webrtc and voicecall)\n");
+
+       fprintf(stderr, "\n");
+       fprintf(stderr, "Please refer to the optional arguments as following.\n\n");
+       fprintf(stderr, "Options\n");
+       fprintf(stderr, "-a\t\taudio-effects with reference device_id like AEC\n");
+       fprintf(stderr, "\t\te.g) refcopy:0x0001, aec-speex:0x0002, aec-webrtc:0x0004\n");
+
+       fprintf(stderr, "-m\t\taudio-effects like noise-suppression\n");
+       fprintf(stderr, "\t\te.g) rnnoise:0x0001, agc-speex:0x0002, pse:0x0200, doorbel:0x0100\n");
 }
 
 int main(int argc, char **argv)
 {
        setbuf(stdout, NULL);
-       const char *optstring = "r:c:f:m:o:d:a:";
+       const char *optstring = "r:c:f:m:o:d:a:p";
        int option;
+       bool playback = false;
 
        arg_channels = DEFAULT_CHANNELS;
        arg_format = AUDIO_SAMPLE_TYPE_S16_LE;
@@ -224,44 +271,48 @@ int main(int argc, char **argv)
                return 0;
        }
 
-       printf("===== Arguments information =====\n");
+       fprintf(stderr, "===== Arguments information =====\n");
        while (-1 != (option = getopt(argc, argv, optstring))) {
                switch (option) {
                        case 'r': /* rate */
                                arg_samplerate = atoi(optarg);
-                               printf("Samplerate : %d\n", arg_samplerate);
+                               fprintf(stderr, "Samplerate : %d\n", arg_samplerate);
                                break;
                        case 'a': /* sound_effect_method_with_reference_e */
                                arg_reference_method_is_set = true;
                                arg_reference_method = (int)strtol(optarg, NULL, 16);
-                               printf("reference method : 0x%x, optarg(%s)\n",
+                               fprintf(stderr, "reference method : 0x%x, optarg(%s)\n",
                                                arg_reference_method, optarg);
                                break;
                        case 'd': /* reference device */
                                arg_ref_dev_is_set = true;
                                arg_ref_dev = atoi(optarg);
-                               printf("reference device : %d\n", arg_ref_dev);
+                               fprintf(stderr, "reference device : %d\n", arg_ref_dev);
                                break;
                        case 'm': /* method */
                                arg_method_is_set = true;
                                arg_method = (int)strtol(optarg, NULL, 16);
-                               printf("method : 0x%x, optarg(%s)\n", arg_method, optarg);
+                               fprintf(stderr, "method : 0x%x, optarg(%s)\n", arg_method, optarg);
                                break;
                        case 'o': /* output file */
                                snprintf(arg_filename, sizeof(arg_filename), "%s", optarg);
-                               printf("Output filename : %s\n", arg_filename);
+                               fprintf(stderr, "Output filename : %s\n", arg_filename);
                                break;
                        case 'c': /* channels */
                                arg_channels = atoi(optarg);
-                               printf("Channels : %d\n", arg_channels);
+                               fprintf(stderr, "Channels : %d\n", arg_channels);
                                if (arg_channels < 1 || arg_channels > 16) {
-                                       printf("Invalid channels : %d\n", arg_channels);
+                                       fprintf(stderr, "Invalid channels : %d\n", arg_channels);
                                        return -1;
                                }
                                break;
                        case 'f': /* format */
                                arg_format = atoi(optarg);
-                               printf("Format : %d\n", arg_format);
+                               fprintf(stderr, "Format : %d\n", arg_format);
+                               break;
+                       case 'p': /* playback for loopback test */
+                               fprintf(stderr, "Playback : Yes\n");
+                               playback = true;
                                break;
                        case 'h':
                        default:
@@ -269,16 +320,20 @@ int main(int argc, char **argv)
                                return 0;
                }
        }
-       printf("=================================\n");
+       fprintf(stderr, "=================================\n");
+
+       if (playback) {
+               playback_stdin();
+       }
 
        if ((arg_reference_method_is_set && !arg_ref_dev_is_set) ||
                        (!arg_reference_method_is_set && arg_ref_dev_is_set)) {
-               printf("need to set reference devices and method both\n");
+               fprintf(stderr, "need to set reference devices and method both\n");
                return -1;
        }
 
        record();
-       printf("END\n");
+       fprintf(stderr, "END\n");
 
        return 0;
 }