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