#include <string.h>
#include <unistd.h>
#include <math.h>
+#include <pthread.h>
#include <sound_manager.h>
#include <audio_io.h>
+#include <time.h>
#ifndef M_PI
#define M_PI (3.14159265)
}
buf = (char *)malloc(nbytes);
+ if (!buf) {
+ printf("malloc(%zu) failed\n", nbytes);
+ return;
+ }
read_bytes = fread(buf, 1, nbytes, fp);
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);
+}
+
+int audio_io_stress_test()
+{
+ 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 = 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("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("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("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)
{
setbuf(stdout, NULL);
_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_stress_test();
} else if (argc == 4) {
int channel_idx = atoi(argv[3]);
if (channel_idx < 0 || channel_idx > 8) {
} else {
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");