audio_io_test: Fix INTEGER_OVERFLOW defect
[platform/core/api/audio-io.git] / test / audio_io_test.c
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <math.h>
22 #include <pthread.h>
23 #include <sound_manager.h>
24 #include <sound_manager_internal.h>
25 #include <audio_io.h>
26 #include <time.h>
27
28 #ifndef M_PI
29 #define M_PI  (3.14159265)
30 #endif
31
32 #define TABLE_SIZE   (200)
33 typedef struct {
34         float sine[TABLE_SIZE];
35         int left_channel;
36         int right_channel;
37 } test_wav_t;
38 test_wav_t test_wav;
39
40 static int ch_table[] = { 0, AUDIO_CHANNEL_MONO, AUDIO_CHANNEL_STEREO,
41                           AUDIO_CHANNEL_MULTI_3, AUDIO_CHANNEL_MULTI_4, AUDIO_CHANNEL_MULTI_5,
42                           AUDIO_CHANNEL_MULTI_6, AUDIO_CHANNEL_MULTI_7, AUDIO_CHANNEL_MULTI_8,
43                           AUDIO_CHANNEL_MULTI_9, AUDIO_CHANNEL_MULTI_10, AUDIO_CHANNEL_MULTI_11,
44                           AUDIO_CHANNEL_MULTI_12, AUDIO_CHANNEL_MULTI_13, AUDIO_CHANNEL_MULTI_14,
45                           AUDIO_CHANNEL_MULTI_15, AUDIO_CHANNEL_MULTI_16 };
46 static char *state_str[] = { "IDLE", "RUNNING", "PAUSED" };
47
48 static void _audio_in_state_cb(audio_in_h handle, audio_io_state_e previous, audio_io_state_e current,
49                                                                 bool by_policy, void *user_data)
50 {
51         printf(">>> _audio_in_state_cb() : handle(%p), (%d,%s) => (%d,%s), by_policy(%d), user_data(%p)\n",
52                         handle, previous, state_str[previous], current, state_str[current], by_policy, user_data);
53 }
54
55 static void _audio_out_state_cb(audio_out_h handle, audio_io_state_e previous, audio_io_state_e current,
56                                                                 bool by_policy, void *user_data)
57 {
58         printf(">>> _audio_out_state_cb() : handle(%p), (%d,%s) => (%d,%s), by_policy(%d), user_data(%p)\n",
59                         handle, previous, state_str[previous], current, state_str[current], by_policy, user_data);
60 }
61
62 static void _play_file(char *file, int length, int num, int ch)
63 {
64         int i = 0;
65         int ret = 0;
66         int total_length = length * num;
67         audio_out_h output;
68         FILE *fp = fopen(file, "r");
69         if (fp == NULL) {
70                 printf("fopen failed\n");
71                 return;
72         }
73
74         char *buf = malloc(total_length);
75         if (buf == NULL) {
76                 printf("malloc failed\n");
77                 fclose(fp);
78                 return;
79         }
80
81         printf("# start to play [%s][%d][%d]\n", file, total_length, ch);
82         printf(" > create\n");
83         audio_out_create_new(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &output);
84         if (fread(buf, 1, total_length, fp) != total_length)
85                 printf("error!!!!\n");
86
87         ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
88         if (ret != AUDIO_IO_ERROR_NONE) {
89                 printf("audio_out_set_state_changed_cb failed. \n");
90         }
91
92         printf("  > prepare\n");
93         audio_out_prepare(output);
94
95         char *pbuf = buf;
96         for (i = 0; i < num; i++) {
97                 printf("### write = (%d/%d) ============== \n", i, num);
98                 audio_out_write(output, pbuf, length);
99                 pbuf += length;
100
101         }
102         printf("  < unprepare\n");
103         audio_out_drain(output);
104         audio_out_unprepare(output);
105
106         printf(" < destroy\n");
107         audio_out_destroy(output);
108
109         fclose(fp);
110         free(buf);
111
112         printf("# play done\n");
113 }
114
115 #define DUMP_FILE "/root/test.raw"
116
117 FILE *fp = NULL;
118
119 static void _audio_out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data)
120 {
121         char *buf = NULL;
122         int read_bytes = 0;
123         int written = 0;
124
125         if (fp == NULL) {
126                 printf("FILE is NULL\n");
127                 return;
128         }
129
130         buf = (char *)malloc(nbytes);
131         if (!buf) {
132                 printf("malloc(%zu) failed\n", nbytes);
133                 return;
134         }
135
136         read_bytes = fread(buf, 1, nbytes, fp);
137
138         written = audio_out_write(handle, buf, read_bytes);
139         printf("written : %6d/%6d (requested %zu)\n", written, read_bytes, nbytes);
140
141         if (read_bytes < nbytes) {
142                 printf("EOS!!!!\n");
143                 fclose(fp);
144                 fp = NULL;
145         }
146         free(buf);
147 }
148
149 static void _play_file_sample_async(char *file, int frequency, int ch, int type)
150 {
151         audio_out_h output;
152         int file_size = 0;
153
154         if (ch < 0 || ch > 2)
155                 ch = 0;
156
157         fp = fopen(file, "r");
158         if (fp == NULL) {
159                 printf("open failed\n");
160                 return;
161         }
162         printf("start to play [%s] of size [%d] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
163         audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
164
165         audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
166         audio_out_set_stream_cb(output, _audio_out_stream_cb, fp);
167         audio_out_prepare(output);
168
169         while (fp) {
170                 usleep(10000); /* 10ms */
171         }
172
173         printf("FINISHED!!!\n");
174
175         audio_out_unprepare(output);
176         audio_out_destroy(output);
177
178         printf("play done\n");
179 }
180
181 static void _play_file_sample_sync(char *file, int frequency, int ch, int type)
182 {
183         audio_out_h output;
184         long file_size = 0;
185         size_t read_bytes = 0;
186         int buffer_size = 0;
187         char *buf = NULL;
188         FILE *fp = NULL;
189         int i = 0;
190
191         if (ch < 0 || ch > 2)
192                 ch = 0;
193
194         fp = fopen(file, "r");
195         if (fp == NULL) {
196                 printf("open failed\n");
197                 return;
198         }
199         /* Get the size */
200         fseek(fp, 0, SEEK_END);
201         file_size = ftell(fp);
202         fseek(fp, 0, SEEK_SET);
203
204         printf("Play [%s] of size [%ld] with [%d][%d][%d]\n", file, file_size, frequency, ch, type);
205         audio_out_create_new(frequency, ch_table[ch], AUDIO_SAMPLE_TYPE_U8 + type, &output);
206
207         audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
208         audio_out_prepare(output);
209
210         audio_out_get_buffer_size(output, &buffer_size);
211         buf = (char *)calloc(buffer_size, sizeof(char));
212         if (buf == NULL) {
213                 printf("malloc failed\n");
214                 goto EXIT;
215         }
216
217         printf("Press any key to continue....\n");
218         getchar();
219         printf("Start to read[%d] from file and write : \n", buffer_size);
220
221         while (file_size > 0) {
222                 read_bytes = fread(buf, 1, buffer_size, fp);
223
224                 printf(".");
225                 if (++i % 10 == 0)
226                         printf("|");
227                 if (i % 100 == 0) {
228                         printf("\n");
229                         i = 0;
230                 }
231
232                 if (read_bytes != buffer_size) {
233                         if (feof(fp)) {
234                                 printf("\n EOS! this is a last buffer! \n");
235                         } else if (ferror(fp)) {
236                                 printf("\n Error!\n");
237                                 break;
238                         }
239                 }
240
241                 audio_out_write(output, buf, (unsigned int)read_bytes);
242
243                 file_size -= (long)read_bytes;
244                 usleep(10000);
245         }
246
247 EXIT:
248         printf("\n Cleanup now!\n");
249         audio_out_unprepare(output);
250         audio_out_destroy(output);
251
252         if (buf)
253                 free(buf);
254         if (fp)
255                 fclose(fp);
256
257         printf("Play done\n");
258 }
259
260 static sound_device_h *find_device_by_id(int id)
261 {
262         sound_device_list_h device_list;
263         sound_device_h device;
264         int _id;
265
266         if (sound_manager_get_device_list(SOUND_DEVICE_ALL_MASK, &device_list) != 0)
267                 return NULL;
268
269         while (!sound_manager_get_next_device(device_list, &device)) {
270                 if (sound_manager_get_device_id(device, &_id) != 0) {
271                         device = NULL;
272                         goto out;
273                 }
274
275                 if (_id == id)
276                         break;
277         }
278
279 out:
280         sound_manager_free_device_list(device_list);
281
282         return device;
283 }
284
285 static int _record_echocancel(char *filename, int rate, int channels, int ref_deviceid)
286 {
287         int ret, size;
288         sound_stream_info_h stream_info;
289         sound_device_h ref_dev = NULL;
290         audio_in_h input = NULL;
291         FILE *fp = NULL;
292         char *buffer = NULL;
293
294         ret = audio_in_create(rate, ch_table[channels], AUDIO_SAMPLE_TYPE_S16_LE, &input);
295         if (ret != AUDIO_IO_ERROR_NONE) {
296                 printf("audio in create error = 0x%x\n", ret);
297                 return -1;
298         }
299
300         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info);
301         if (ret) {
302                 printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
303                 goto out;
304         }
305
306         if (!(ref_dev = find_device_by_id(ref_deviceid))) {
307                 printf("fail to get ref device id\b");
308                 goto out;
309         }
310
311         ret = sound_manager_set_echo_cancel_reference_device(stream_info, ref_dev);
312         if (ret) {
313                 printf("fail to sound_manager_set_echo_cancel_reference_device(), ret(0x%x)\n", ret);
314                 goto out;
315         }
316
317         ret = audio_in_set_sound_stream_info(input, stream_info);
318         if (ret) {
319                 printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
320                 goto out;
321         }
322
323         ret = audio_in_prepare(input);
324         if (ret != AUDIO_IO_ERROR_NONE) {
325                 printf("ERROR, prepare\n");
326                 goto out;
327         }
328
329         ret = audio_in_get_buffer_size(input, &size);
330         if (ret != AUDIO_IO_ERROR_NONE) {
331                 printf("ERROR, get_buffer_size\n");
332                 goto out;
333         }
334
335         fp = fopen(filename, "wb+");
336         if (fp == NULL) {
337                 printf("ERROR, file open failed\n");
338                 goto out;
339         }
340
341         buffer = alloca(size);
342
343         while (1) {
344                 ret = audio_in_read(input, (void *)buffer, size);
345                 if (ret <= 0) {
346                         printf("FAIL, size=%d, ret=0x%x\n", size, ret);
347                         goto out;
348                 }
349
350                 fwrite(buffer, size, sizeof(char), fp);
351         }
352
353 out:
354         fclose(fp);
355         audio_in_unprepare(input);
356         audio_in_destroy(input);
357
358         return 0;
359 }
360
361 static int _record_and_play(int length, int num, int ch)
362 {
363         int ret, size, i;
364         audio_in_h input = NULL;
365         FILE *fp = NULL;
366         char *buffer = NULL;
367
368         ret = audio_in_create(44100, ch_table[ch], AUDIO_SAMPLE_TYPE_S16_LE, &input);
369         if (ret != AUDIO_IO_ERROR_NONE) {
370                 printf("audio in create error = 0x%x\n", ret);
371                 return -1;
372         }
373
374         ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
375         if (ret != AUDIO_IO_ERROR_NONE) {
376                 printf("audio_in_set_state_changed_cb failed. \n");
377                 goto error;
378         }
379
380         ret = audio_in_prepare(input);
381         if (ret != AUDIO_IO_ERROR_NONE) {
382                 printf("ERROR, prepare\n");
383                 goto error;
384         }
385
386         fp = fopen(DUMP_FILE, "wb+");
387         if (fp == NULL) {
388                 printf("ERROR, file open failed\n");
389                 goto error;
390         }
391
392         ret = audio_in_get_buffer_size(input, &size);
393         if (ret != AUDIO_IO_ERROR_NONE) {
394                 printf("ERROR, prepare\n");
395                 goto error;
396         }
397
398         size = length;
399         buffer = alloca(size);
400
401         for (i = 0; i < num; i++) {
402                 printf("### read = (%d/%d) ============== ", i, num);
403                 if ((ret = audio_in_read(input, (void *)buffer, size)) > AUDIO_IO_ERROR_NONE) {
404                         fwrite(buffer, size, sizeof(char), fp);
405                         printf("PASS, size=%d, ret=0x%x\n", size, ret);
406                 } else {
407                         printf("FAIL, size=%d, ret=0x%x\n", size, ret);
408                 }
409         }
410
411         fclose(fp);
412
413         audio_in_unprepare(input);
414         audio_in_destroy(input);
415
416         _play_file(DUMP_FILE, length, num, ch);
417
418         return 0;
419
420 error:
421         audio_in_destroy(input);
422         if (fp)
423                 fclose(fp);
424         return -1;
425 }
426
427 static int _direct_loopback()
428 {
429         int ret, size;
430         audio_in_h input = NULL;
431         audio_out_h output = NULL;
432         char *buffer = NULL;
433         int i = 0;
434
435         ret = audio_in_create(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
436         if (ret != AUDIO_IO_ERROR_NONE) {
437                 printf("audio_in_create_ex failed. \n");
438                 return ret;
439         }
440         ret = audio_out_create_new(16000, AUDIO_CHANNEL_MONO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
441         if (ret != AUDIO_IO_ERROR_NONE) {
442                 printf("audio_out_create_new failed. \n");
443                 goto exit;
444         }
445
446         ret = audio_in_prepare(input);
447         if (ret != 0) {
448                 printf("audio_in_prepare failed, err(0x%x)\n", ret);
449                 goto exit;
450         }
451
452         ret = audio_in_get_buffer_size(input, &size);
453         if (ret != AUDIO_IO_ERROR_NONE) {
454                 printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
455                 goto exit;
456         }
457
458         printf("size(%d)\n", size);
459         buffer = alloca(size);
460
461         ret = audio_out_prepare(output);
462         if (ret != 0) {
463                 printf("audio_out_prepare failed, err(0x%x)\n", ret);
464                 goto exit;
465         }
466
467         if (buffer == NULL) {
468                 printf("buffer is null\n");
469                 ret = -1;
470                 goto exit;
471         }
472
473         printf("Start to loopback(read/write) with [16kHz/MONO/S16LE]!!!\n");
474
475         while (1) {
476                 ret = audio_in_read(input, (void *)buffer, size);
477                 if (ret > AUDIO_IO_ERROR_NONE) {
478                         ret = audio_out_write(output, buffer, size);
479                         if (ret > AUDIO_IO_ERROR_NONE) {
480                                 printf(".");
481                                 i++;
482                                 if (i % 10 == 0)
483                                         printf("|");
484                                 if (i % 100 == 0) {
485                                         printf("\n");
486                                         i = 0;
487                                 }
488                         } else {
489                                 printf("audio read success, write failed. buffer(%p), size(%d)\n", buffer, size);
490                         }
491                 } else {
492                         printf("audio read/write failed. buffer(%p), size(%d)\n", buffer, size);
493                 }
494                 usleep(10000);
495         }
496
497 exit:
498         if (input)
499                 audio_in_destroy(input);
500         if (output)
501                 audio_out_destroy(output);
502
503         return ret;
504
505 }
506
507 audio_in_h input;
508 audio_out_h output;
509
510 FILE *fp_w = NULL;
511
512 sound_stream_info_h g_stream_info_read_h = NULL;
513 sound_stream_info_h g_stream_info_write_h = NULL;
514
515 static void _focus_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask,
516                                         sound_stream_focus_state_e focus_state,
517                                         sound_stream_focus_change_reason_e reason_for_change, int sound_behavior,
518                                         const char *additional_info, void *user_data)
519 {
520         printf("*** focus_callback_read is called, stream_info(%p, read(%p)/write(%p)) ***\n",
521                         stream_info, g_stream_info_read_h, g_stream_info_write_h);
522         printf(" - reason_for_change(%d), additional_info(%s), user_data(%p)\n",
523                         reason_for_change, additional_info, user_data);
524         printf(" - focus_state is :%d \n", focus_state);
525
526         return;
527 }
528
529 static void _audio_io_stream_read_cb(audio_in_h handle, size_t nbytes, void *user_data)
530 {
531         const void *buffer = NULL;
532         unsigned int len = (unsigned int)nbytes;
533
534         if (len == 0)
535                 return;
536
537         audio_in_peek(handle, &buffer, &len);
538         if (fp_w)
539                 fwrite(buffer, sizeof(char), len, fp_w);
540         audio_in_drop(handle);
541 }
542
543 static void _audio_io_stream_write_cb(audio_out_h handle, size_t nbytes, void *user_data)
544 {
545         short *buffer = NULL;
546         int i = 0;
547
548         if (nbytes == 0)
549                 return;
550
551         buffer = (short *)malloc(nbytes);
552         if (buffer == NULL) {
553                 printf("malloc failed\n");
554                 return;
555         }
556         memset(buffer, 0, nbytes);
557
558         for (i = 0; i < nbytes / 2; i += 2) {
559                 buffer[i] = (short)32768 *test_wav.sine[test_wav.left_channel]; /* left */
560                 buffer[i + 1] = (short)32768 *test_wav.sine[test_wav.right_channel]; /* right */
561                 test_wav.left_channel += 1;
562                 if (test_wav.left_channel >= TABLE_SIZE)
563                         test_wav.left_channel -= TABLE_SIZE;
564                 test_wav.right_channel += 3;
565                 if (test_wav.right_channel >= TABLE_SIZE)
566                         test_wav.right_channel -= TABLE_SIZE;
567         }
568
569         audio_out_write(handle, buffer, nbytes);
570
571         free(buffer);
572 }
573
574 int _convert_cmd_and_run(char cmd, int mode)
575 {
576         int ret = 0;
577         switch (cmd) {
578         case 'P':
579                 if (mode & 0x01)
580                         ret = audio_out_prepare(output);
581                 if (mode & 0x02)
582                         ret = audio_in_prepare(input);
583                 break;
584         case 'u':
585                 if (mode & 0x01)
586                         ret = audio_out_unprepare(output);
587                 if (mode & 0x02)
588                         ret = audio_in_unprepare(input);
589                 break;
590         case 'p':
591                 if (mode & 0x01)
592                         ret = audio_out_pause(output);
593                 if (mode & 0x02)
594                         ret = audio_in_pause(input);
595                 break;
596         case 'r':
597                 if (mode & 0x01)
598                         ret = audio_out_resume(output);
599                 if (mode & 0x02)
600                         ret = audio_in_resume(input);
601                 break;
602         case 'd':
603                 if (mode & 0x01)
604                         ret = audio_out_drain(output);
605                 break;
606         case 'f':
607                 if (mode & 0x01)
608                         ret = audio_out_flush(output);
609                 if (mode & 0x02)
610                         ret = audio_in_flush(input);
611                 break;
612         case 'i':
613                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
614                 if (ret)
615                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
616                 break;
617         case 'q': /* quit */
618                 ret = 1;
619                 break;
620         default:
621                 ret = 1;
622                 break;
623         }
624         return ret;
625 }
626
627 int audio_io_async_test(int mode)
628 {
629         int ret, size;
630         char *buffer = NULL;
631         int i = 0;
632
633         char cmd = 0;
634         int cmd_ret;
635
636         int write_mode = (mode & 0x01);
637         int read_mode = (mode & 0x02);
638
639         sound_stream_focus_state_e playback_focus_state;
640         sound_stream_focus_state_e recording_focus_state;
641
642         if ((write_mode == 0) && (read_mode == 0)) {
643                 printf("not vaild mode.\n");
644                 return 0;
645         }
646
647         if (read_mode) {
648
649                 printf("audio_in_create\n");
650                 ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
651                 if (ret != AUDIO_IO_ERROR_NONE) {
652                         printf("audio_in_create_ex failed. \n");
653                         return 0;
654                 }
655                 printf("audio_in_create success!!! [%p]\n", input);
656
657                 ret = audio_in_set_stream_cb(input, _audio_io_stream_read_cb, NULL);
658                 if (ret != AUDIO_IO_ERROR_NONE) {
659                         printf("audio_in_set_stream_cb failed. \n");
660                         goto EXIT;
661                 }
662                 printf("audio_in_set_stream_cb success!!! [%p]\n", input);
663
664                 ret = audio_in_set_state_changed_cb(input, _audio_in_state_cb, NULL);
665                 if (ret != AUDIO_IO_ERROR_NONE) {
666                         printf("audio_out_set_state_changed_cb failed. \n");
667                         goto EXIT;
668                 }
669                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", input);
670
671                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_read_h);
672                 if (ret) {
673                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
674                         goto EXIT;
675                 }
676                 ret = audio_in_set_sound_stream_info(input, g_stream_info_read_h);
677                 if (ret)
678                         printf("fail to audio_in_set_sound_stream_info(), ret(0x%x)\n", ret);
679
680                 ret = sound_manager_acquire_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
681                 if (ret) {
682                         printf("fail to sound_manager_acquire_focus() for RECORDING, ret(0x%x)\n", ret);
683                         goto EXIT;
684                 }
685
686                 fp_w = fopen("/tmp/pcm_w.raw", "w");
687         }
688
689         if (write_mode) {
690                 printf("before audio_out_create_new\n");
691                 getchar();
692
693                 printf("audio_out_create_new\n");
694                 ret = audio_out_create_new(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output);
695                 if (ret != AUDIO_IO_ERROR_NONE) {
696                         printf("audio_out_create_new failed. \n");
697                         goto EXIT;
698                 }
699                 printf("audio_out_create_new success!!! [%p]\n", output);
700
701                 ret = audio_out_set_stream_cb(output, _audio_io_stream_write_cb, NULL);
702                 if (ret != AUDIO_IO_ERROR_NONE) {
703                         printf("audio_out_set_stream_cb failed. \n");
704                         goto EXIT;
705                 }
706                 printf("audio_out_set_stream_cb success!!! [%p]\n", output);
707
708                 ret = audio_out_set_state_changed_cb(output, _audio_out_state_cb, NULL);
709                 if (ret != AUDIO_IO_ERROR_NONE) {
710                         printf("audio_out_set_state_changed_cb failed. \n");
711                         goto EXIT;
712                 }
713                 printf("audio_out_set_state_changed_cb success!!! [%p]\n", output);
714
715                 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, _focus_cb, NULL, &g_stream_info_write_h);
716                 if (ret) {
717                         printf("fail to sound_manager_create_stream_information(), ret(0x%x)\n", ret);
718                         goto EXIT;
719                 }
720
721                 ret = audio_out_set_sound_stream_info(output, g_stream_info_write_h);
722                 if (ret)
723                         printf("fail to audio_out_set_sound_stream_info(), ret(0x%x)\n", ret);
724
725                 ret = sound_manager_acquire_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
726                 if (ret) {
727                         printf("fail to sound_manager_acquire_focus() for PLAYBACK, ret(0x%x)\n", ret);
728                         goto EXIT;
729                 }
730
731                 /* generate wave data */
732                 for (i = 0; i < TABLE_SIZE; i++)
733                         test_wav.sine[i] = 0.9 * (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
734                 test_wav.left_channel = test_wav.right_channel = 0;
735         }
736
737         if (read_mode) {
738                 printf("before audio_in_prepare\n");
739                 getchar();
740                 printf("audio_in_prepare\n");
741                 ret = audio_in_prepare(input);
742                 if (ret != 0) {
743                         printf("audio_in_prepare failed, err(0x%x)\n", ret);
744                         audio_in_destroy(input);
745                         goto EXIT;
746                 } else {
747                         ret = audio_in_get_buffer_size(input, &size);
748                         if (ret != AUDIO_IO_ERROR_NONE) {
749                                 printf("audio_in_get_buffer_size failed, err(0x%x)\n", ret);
750                                 goto EXIT;
751                         } else {
752                                 printf("size(%d)\n", size);
753                                 buffer = alloca(size);
754                         }
755                 }
756
757                 if (buffer == NULL) {
758                         printf("buffer is null\n");
759                         goto EXIT;
760                 }
761         }
762
763         if (write_mode) {
764                 printf("before audio_out_prepare\n");
765                 getchar();
766                 printf("audio_out_prepare\n");
767                 ret = audio_out_prepare(output);
768                 if (ret != 0) {
769                         printf("audio_out_prepare failed, err(0x%x)\n", ret);
770                         audio_out_destroy(output);
771                         goto EXIT;
772                 }
773         }
774
775         do {
776                 printf("command(q:quit) : ");
777                 ret = getchar();
778                 if (ret == EOF)
779                         goto EXIT;
780                 cmd = (char)ret;
781                 if (cmd != '\n')
782                         getchar();
783                 cmd_ret = _convert_cmd_and_run(cmd, mode);
784                 printf("  - result code : %d\n", cmd_ret);
785         } while (cmd != 'q');
786
787 EXIT:
788         if (read_mode) {
789                 if (input) {
790                         printf("audio_in_unprepare\n");
791                         audio_in_unprepare(input);
792                         printf("audio_in_destroy\n");
793                         audio_in_destroy(input);
794                         input = NULL;
795                 }
796
797                 if (fp_w) {
798                         fclose(fp_w);
799                         fp_w = NULL;
800                 }
801
802                 if (g_stream_info_read_h) {
803                         ret = sound_manager_get_focus_state(g_stream_info_read_h, NULL, &recording_focus_state);
804                         if (recording_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
805                                 ret = sound_manager_release_focus(g_stream_info_read_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
806                                 if (ret)
807                                         printf("fail to sound_manager_release_focus() for recording, ret(0x%x)\n", ret);
808                         }
809                         ret = sound_manager_destroy_stream_information(g_stream_info_read_h);
810                         if (ret)
811                                 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
812                         g_stream_info_read_h = NULL;
813                 }
814         }
815
816         if (write_mode) {
817                 if (output) {
818                         printf("audio_out_unprepare\n");
819                         audio_out_unprepare(output);
820                         printf("audio_out_destroy\n");
821                         audio_out_destroy(output);
822                 }
823
824                 if (g_stream_info_write_h) {
825                         ret = sound_manager_get_focus_state(g_stream_info_write_h, &playback_focus_state, NULL);
826                         if (playback_focus_state == SOUND_STREAM_FOCUS_STATE_ACQUIRED) {
827                                 ret = sound_manager_release_focus(g_stream_info_write_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
828                                 if (ret)
829                                         printf("fail to sound_manager_release_focus() for playback, ret(0x%x)\n", ret);
830                         }
831                         ret = sound_manager_destroy_stream_information(g_stream_info_write_h);
832                         if (ret)
833                                 printf("fail to sound_manager_destroy_stream_information(), ret(0x%x)\n", ret);
834                         g_stream_info_write_h = NULL;
835                 }
836         }
837
838         return 0;
839 }
840
841 #define THREAD_MAX 10
842 #define TEST_COUNT 500
843 static void *thread_stress_test_audio_in(void *data)
844 {
845         int i, j;
846         int rate;
847         char *buf;
848         int buffer_size;
849         audio_channel_e ch;
850         audio_sample_type_e type;
851         unsigned int seed = (unsigned int)time(NULL);
852
853         audio_in_h input = (audio_in_h)data;
854
855         audio_in_prepare(input);
856         audio_in_get_buffer_size(input, &buffer_size);
857         buf = (char *)malloc(buffer_size);
858         if (buf == NULL) {
859                 printf("malloc failed\n");
860                 goto EXIT;
861         }
862
863         for (i=0; i<TEST_COUNT; i++) {
864                 switch (rand_r(&seed) % 8) {
865                         case 0:
866                                 audio_in_prepare(input);
867                                 break;
868                         case 1:
869                                 audio_in_read(input, buf, buffer_size);
870                                 break;
871                         case 2:
872                                 audio_in_pause(input);
873                                 break;
874                         case 3:
875                                 audio_in_resume(input);
876                                 break;
877                         case 4:
878                                 audio_in_flush(input);
879                                 break;
880                         case 5:
881                                 audio_in_unprepare(input);
882                                 break;
883                         case 6:
884                                 audio_in_prepare(input);
885                                 for  (j = 0; j < 10; j++)
886                                         audio_in_read(input, buf, buffer_size);
887                                 break;
888                         case 7:
889                                 audio_in_get_buffer_size(input, &buffer_size);
890                                 audio_in_get_sample_rate(input, &rate);
891                                 audio_in_get_channel(input, &ch);
892                                 audio_in_get_sample_type(input, &type);
893                                 break;
894                         default:
895                                 break;
896                 }
897                 if ((i % (TEST_COUNT/10)) == 0)
898                         printf("audio_in: %x thread. count(%d/%d)\n", (int)pthread_self(), i, TEST_COUNT);
899         }
900         audio_in_unprepare(input);
901
902 EXIT:
903         if (buf) {
904                 free(buf);
905                 buf = NULL;
906         }
907
908         pthread_exit(0);
909 }
910
911 static void *thread_stress_test_audio_out(void *data)
912 {
913         int i, j;
914         int rate;
915         char *buf;
916         int buffer_size;
917         audio_channel_e ch;
918         audio_sample_type_e type;
919         unsigned int seed = (unsigned int)time(NULL);
920
921         audio_out_h output = (audio_out_h)data;
922
923         srand(time(NULL));
924
925         audio_out_get_buffer_size(output, &buffer_size);
926
927         buf = (char *)malloc(buffer_size);
928         if (buf == NULL) {
929                 printf("malloc failed\n");
930                 goto EXIT;
931         }
932
933         audio_out_prepare(output);
934         for (i = 0; i < TEST_COUNT; i++) {
935                 switch (rand_r(&seed) % 9) {
936                         case 0:
937                                 audio_out_prepare(output);
938                                 break;
939                         case 1:
940                                 audio_out_write(output, buf, buffer_size);
941                                 break;
942                         case 2:
943                                 audio_out_pause(output);
944                                 break;
945                         case 3:
946                                 audio_out_resume(output);
947                                 break;
948                         case 4:
949                                 audio_out_drain(output);
950                                 break;
951                         case 5:
952                                 audio_out_flush(output);
953                                 break;
954                         case 6:
955                                 audio_out_write(output, buf, buffer_size);
956                                 break;
957                         case 7:
958                                 audio_out_prepare(output);
959                                 for  (j = 0; j < 10; j++)
960                                         audio_out_write(output, buf, buffer_size);
961                                 break;
962                         case 8:
963                                 audio_out_get_buffer_size(output, &buffer_size);
964                                 audio_out_get_sample_rate(output, &rate);
965                                 audio_out_get_channel(output, &ch);
966                                 audio_out_get_sample_type(output, &type);
967                                 break;
968                         default:
969                                 break;
970                 }
971                 if ((i % (TEST_COUNT/10)) == 0)
972                         printf("audio_out: %x thread. count(%d/%d)\n", (int)pthread_self(), i, TEST_COUNT);
973         }
974         audio_out_unprepare(output);
975
976 EXIT:
977         if (buf)
978                 free(buf);
979
980         pthread_exit(0);
981 }
982
983 #define OUT_HANDLE_MAX  1000
984 #define IN_HANDLE_MAX   10
985 void audio_io_test_handle_max()
986 {
987         audio_out_h output[OUT_HANDLE_MAX] = { 0, };
988         audio_in_h input[IN_HANDLE_MAX] = { 0, };
989         int i;
990         int success = 0;
991         int ret;
992
993         printf("==============================================\n");
994         printf("playback handle creation test. try to create %d handles\n", OUT_HANDLE_MAX);
995         for (i = 0; i < OUT_HANDLE_MAX; i++) {
996                 ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[i]);
997                 if (ret != AUDIO_IO_ERROR_NONE) {
998                         printf("audio_out_create_new failed.\n");
999                         return;
1000                 }
1001
1002                 ret = audio_out_prepare(output[i]);
1003                 if (ret == AUDIO_IO_ERROR_NONE)
1004                         success++;
1005         }
1006         printf("created handle (%d/%d)\n", success, OUT_HANDLE_MAX);
1007
1008         for (i = 0; i < OUT_HANDLE_MAX; i++)
1009                 audio_out_destroy(output[i]);
1010
1011         /* pulseaudio oom when IN_HANDLE_MAX is set over 50 */
1012         printf("capture handle creation test. try to create %d handles\n", IN_HANDLE_MAX);
1013         success = 0;
1014         for (i = 0; i < IN_HANDLE_MAX; i++) {
1015                 ret = audio_in_create(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input[i]);
1016                 if (ret != AUDIO_IO_ERROR_NONE) {
1017                         printf("audio_in_create_new failed.\n");
1018                         return;
1019                 }
1020
1021                 ret = audio_in_prepare(input[i]);
1022                 if (ret == AUDIO_IO_ERROR_NONE)
1023                         success++;
1024         }
1025         printf("created handle (%d/%d)\n", success, IN_HANDLE_MAX);
1026
1027         for (i = 0; i < IN_HANDLE_MAX; i++)
1028                 audio_in_destroy(input[i]);
1029 }
1030
1031 int audio_io_test_read_write()
1032 {
1033         int i;
1034         int ret;
1035         audio_out_h output[2];
1036         audio_in_h input;
1037
1038         int status;
1039         pthread_t t[THREAD_MAX];
1040
1041         void out_stream_cb(audio_out_h handle, size_t nbytes, void *user_data) {
1042                 char * buf = (char *)malloc(nbytes);
1043                 if (!buf) {
1044                         printf("malloc(%zu) failed\n", nbytes);
1045                         return;
1046                 }
1047                 audio_out_write(handle, buf, nbytes);
1048                 free(buf);
1049                 return;
1050         }
1051
1052         void in_stream_cb(audio_in_h handle, size_t nbytes, void *user_data) {
1053                 void* buf = NULL;
1054                 unsigned int size = nbytes;
1055
1056                 audio_in_peek(handle, (const void **)buf, &size);
1057                 audio_in_drop(handle);
1058                 return;
1059         }
1060
1061         void state_changed_cb(audio_out_h handle, audio_io_state_e previous, audio_io_state_e current, bool by_policy, void *user_data) {
1062                 return;
1063         }
1064
1065         /* audio_out sync */
1066         ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[0]);
1067         if (ret != AUDIO_IO_ERROR_NONE) {
1068                 printf("audio_out_create_new failed. \n");
1069                 return 0;
1070         }
1071         audio_out_set_state_changed_cb(output[0], state_changed_cb, NULL);
1072
1073         /* audio_out async */
1074         ret = audio_out_create_new(48000, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &output[1]);
1075         if (ret != AUDIO_IO_ERROR_NONE) {
1076                 printf("audio_out_create_new failed. \n");
1077                 return 0;
1078         }
1079         audio_out_set_stream_cb(output[1], out_stream_cb, NULL);
1080         audio_out_set_state_changed_cb(output[1], state_changed_cb, NULL);
1081
1082         /* audio_in */
1083         ret = audio_in_create(44100, AUDIO_CHANNEL_STEREO, AUDIO_SAMPLE_TYPE_S16_LE, &input);
1084         if (ret != AUDIO_IO_ERROR_NONE) {
1085                 printf("audio in create error = 0x%x\n", ret);
1086                 return -1;
1087         }
1088         audio_in_set_stream_cb(input, in_stream_cb, NULL);
1089
1090         printf("==============================================\n");
1091         printf("audio out sync test\n");
1092         for (i = 0; i < THREAD_MAX; i++)
1093                 pthread_create(&t[i], NULL, thread_stress_test_audio_out, output[0]);
1094
1095         for (i = 0; i < THREAD_MAX; i++) {
1096                 pthread_join(t[i], (void**)&status);
1097                 printf("thread %d finished\n", i);
1098         }
1099
1100         printf("==============================================\n");
1101         printf("audio out async test\n");
1102         for (i = 0; i < THREAD_MAX; i++)
1103                 pthread_create(&t[i], NULL, thread_stress_test_audio_out, output[1]);
1104
1105         for (i = 0; i < THREAD_MAX; i++) {
1106                 pthread_join(t[i], (void**)&status);
1107                 printf("thread %d finished\n", i);
1108         }
1109
1110         printf("==============================================\n");
1111         printf("audio in test\n");
1112         for (i = 0; i < THREAD_MAX; i++)
1113                 pthread_create(&t[i], NULL, thread_stress_test_audio_in, input);
1114
1115         for (i = 0; i < THREAD_MAX; i++) {
1116                 pthread_join(t[i], (void**)&status);
1117                 printf("thread %d finished\n", i);
1118         }
1119
1120         audio_out_destroy(output[0]);
1121         audio_out_destroy(output[1]);
1122         audio_in_destroy(input);
1123
1124         printf("stress test end\n");
1125
1126         return 0;
1127 }
1128
1129 int main(int argc, char **argv)
1130 {
1131         setbuf(stdout, NULL);
1132
1133         if (argc == 2 && !strcmp(argv[1], "loopback")) {
1134                 _direct_loopback();
1135         } else if (argc == 3 && !strcmp(argv[1], "async")) {
1136                 audio_io_async_test(atoi(argv[2]));
1137         } else if (argc == 2 && !strcmp(argv[1], "stress")) {
1138                 audio_io_test_handle_max();
1139                 audio_io_test_read_write();
1140         } else if (argc == 4) {
1141                 int channel_idx = atoi(argv[3]);
1142                 if (channel_idx <= 0 || channel_idx > 16) {
1143                         printf("Invalid channel\n");
1144                         return 0;
1145                 }
1146                 printf("run with [%s][%s][%s]\n", argv[1], argv[2], argv[3]);
1147                 _record_and_play(atoi(argv[1]), atoi(argv[2]), channel_idx);
1148         } else if (argc == 6) {
1149                 if (strcmp(argv[1], "play") == 0)
1150                         _play_file_sample_sync(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
1151                 else if (strcmp(argv[1], "playasync") == 0)
1152                         _play_file_sample_async(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
1153                 else if (strcmp(argv[1], "echocancel") == 0)
1154                         _record_echocancel(argv[2], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
1155         } else {
1156                 printf("- Usages :\n");
1157                 printf("- # audio_io_test loopback\n");
1158                 printf("- # audio_io_test stress\n");
1159                 printf("- # audio_io_test [length to read] [number of iteration] [channels]\n");
1160                 printf("- # audio_io_test async [write(1) | read(2)]\n");
1161                 printf("- # audio_io_test play [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
1162                 printf("- # audio_io_test playasync [filename] [sample rate] [channels] [type(0:U8,1:S16LE,2:S24LE,3:S24_32LE)]\n");
1163                 printf("- # audio_io_test echocancel [filename] [sample rate] [channels] [ref_dev]\n");
1164         }
1165         return 0;
1166 }