blob: a6d8f9204f6521819c1779d255d40d4a98183a34 [file] [log] [blame]
Benny Prijonoe3ebd552009-02-18 20:14:15 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjmedia-audiodev/audiodev.h>
21#include <pjmedia-audiodev/audiotest.h>
22#include <pjmedia.h>
23#include <pjlib.h>
24#include <pjlib-util.h>
25
26#define THIS_FILE "auddemo.c"
27#define MAX_DEVICES 64
Benny Prijono555139d2009-02-19 12:08:19 +000028#define WAV_FILE "auddemo.wav"
Benny Prijonoe3ebd552009-02-18 20:14:15 +000029
30
31static unsigned dev_count;
Nanang Izzuddin21286f22009-03-27 15:15:46 +000032static unsigned playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
33static unsigned capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
Benny Prijonoe3ebd552009-02-18 20:14:15 +000034
35static void app_perror(const char *title, pj_status_t status)
36{
37 char errmsg[PJ_ERR_MSG_SIZE];
38
39 pj_strerror(status, errmsg, sizeof(errmsg));
40 printf( "%s: %s (err=%d)\n",
41 title, errmsg, status);
42}
43
44static void list_devices(void)
45{
46 unsigned i;
47 pj_status_t status;
48
49 dev_count = pjmedia_aud_dev_count();
50 if (dev_count == 0) {
51 PJ_LOG(3,(THIS_FILE, "No devices found"));
52 return;
53 }
54
55 PJ_LOG(3,(THIS_FILE, "Found %d devices:", dev_count));
56
Benny Prijonoe3ebd552009-02-18 20:14:15 +000057 for (i=0; i<dev_count; ++i) {
58 pjmedia_aud_dev_info info;
59
Benny Prijono10454dc2009-02-21 14:21:59 +000060 status = pjmedia_aud_dev_get_info(i, &info);
Benny Prijonoe3ebd552009-02-18 20:14:15 +000061 if (status != PJ_SUCCESS)
62 continue;
63
64 PJ_LOG(3,(THIS_FILE," %2d: %s [%s] (%d/%d)",
65 i, info.driver, info.name, info.input_count, info.output_count));
66 }
67}
68
69static const char *decode_caps(unsigned caps)
70{
71 static char text[200];
Benny Prijono10454dc2009-02-21 14:21:59 +000072 unsigned i;
Benny Prijonoe3ebd552009-02-18 20:14:15 +000073
74 text[0] = '\0';
75
Benny Prijono10454dc2009-02-21 14:21:59 +000076 for (i=0; i<31; ++i) {
77 if ((1 << i) & caps) {
78 const char *capname;
79 capname = pjmedia_aud_dev_cap_name((pjmedia_aud_dev_cap)(1 << i),
80 NULL);
81 strcat(text, capname);
82 strcat(text, " ");
83 }
Benny Prijonoe3ebd552009-02-18 20:14:15 +000084 }
85
86 return text;
87}
88
89static void show_dev_info(unsigned index)
90{
91#define H "%-20s"
92 pjmedia_aud_dev_info info;
93 char formats[200];
94 pj_status_t status;
95
96 if (index >= dev_count) {
97 PJ_LOG(1,(THIS_FILE, "Error: invalid index %u", index));
98 return;
99 }
100
Benny Prijono10454dc2009-02-21 14:21:59 +0000101 status = pjmedia_aud_dev_get_info(index, &info);
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000102 if (status != PJ_SUCCESS) {
103 app_perror("pjmedia_aud_dev_get_info() error", status);
104 return;
105 }
106
107 PJ_LOG(3, (THIS_FILE, "Device at index %u:", index));
108 PJ_LOG(3, (THIS_FILE, "-------------------------"));
109
Benny Prijono10454dc2009-02-21 14:21:59 +0000110 PJ_LOG(3, (THIS_FILE, H": %u (0x%x)", "ID", index, index));
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000111 PJ_LOG(3, (THIS_FILE, H": %s", "Name", info.name));
112 PJ_LOG(3, (THIS_FILE, H": %s", "Driver", info.driver));
113 PJ_LOG(3, (THIS_FILE, H": %u", "Input channels", info.input_count));
114 PJ_LOG(3, (THIS_FILE, H": %u", "Output channels", info.output_count));
115 PJ_LOG(3, (THIS_FILE, H": %s", "Capabilities", decode_caps(info.caps)));
116
117 formats[0] = '\0';
118 if (info.caps & PJMEDIA_AUD_DEV_CAP_EXT_FORMAT) {
119 unsigned i;
120
121 for (i=0; i<info.ext_fmt_cnt; ++i) {
122 char bitrate[32];
123
Benny Prijono555139d2009-02-19 12:08:19 +0000124 switch (info.ext_fmt[i].id) {
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000125 case PJMEDIA_FORMAT_L16:
126 strcat(formats, "L16/");
127 break;
128 case PJMEDIA_FORMAT_PCMA:
129 strcat(formats, "PCMA/");
130 break;
131 case PJMEDIA_FORMAT_PCMU:
132 strcat(formats, "PCMU/");
133 break;
134 case PJMEDIA_FORMAT_AMR:
135 strcat(formats, "AMR/");
136 break;
137 case PJMEDIA_FORMAT_G729:
138 strcat(formats, "G729/");
139 break;
140 case PJMEDIA_FORMAT_ILBC:
141 strcat(formats, "ILBC/");
142 break;
143 default:
144 strcat(formats, "unknown/");
145 break;
146 }
147 sprintf(bitrate, "%u", info.ext_fmt[i].bitrate);
148 strcat(formats, bitrate);
149 strcat(formats, " ");
150 }
151 }
152 PJ_LOG(3, (THIS_FILE, H": %s", "Extended formats", formats));
153
154#undef H
155}
156
157static void test_device(pjmedia_dir dir, unsigned rec_id, unsigned play_id,
158 unsigned clock_rate, unsigned ptime,
159 unsigned chnum)
160{
Benny Prijono10454dc2009-02-21 14:21:59 +0000161 pjmedia_aud_param param;
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000162 pjmedia_aud_test_results result;
163 pj_status_t status;
164
165 if (dir & PJMEDIA_DIR_CAPTURE) {
Benny Prijono10454dc2009-02-21 14:21:59 +0000166 status = pjmedia_aud_dev_default_param(rec_id, &param);
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000167 } else {
Benny Prijono10454dc2009-02-21 14:21:59 +0000168 status = pjmedia_aud_dev_default_param(play_id, &param);
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000169 }
170
171 if (status != PJ_SUCCESS) {
172 app_perror("pjmedia_aud_dev_default_param()", status);
173 return;
174 }
175
176 param.dir = dir;
Benny Prijono10454dc2009-02-21 14:21:59 +0000177 param.rec_id = rec_id;
178 param.play_id = play_id;
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000179 param.clock_rate = clock_rate;
180 param.channel_count = chnum;
181 param.samples_per_frame = clock_rate * chnum * ptime / 1000;
182
Nanang Izzuddin21286f22009-03-27 15:15:46 +0000183 /* Latency settings */
184 param.flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
185 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
186 param.input_latency_ms = capture_lat;
187 param.output_latency_ms = playback_lat;
188
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000189 PJ_LOG(3,(THIS_FILE, "Performing test.."));
190
191 status = pjmedia_aud_test(&param, &result);
192 if (status != PJ_SUCCESS) {
193 app_perror("Test has completed with error", status);
194 return;
195 }
196
197 PJ_LOG(3,(THIS_FILE, "Done. Result:"));
198
199 if (dir & PJMEDIA_DIR_CAPTURE) {
200 if (result.rec.frame_cnt==0) {
201 PJ_LOG(1,(THIS_FILE, "Error: no frames captured!"));
202 } else {
Nanang Izzuddinfa923822009-03-11 11:28:44 +0000203 PJ_LOG(3,(THIS_FILE, " %-20s: interval (min/max/avg/dev)=%u/%u/%u/%u, burst=%u",
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000204 "Recording result",
Nanang Izzuddinfa923822009-03-11 11:28:44 +0000205 result.rec.min_interval,
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000206 result.rec.max_interval,
Nanang Izzuddinfa923822009-03-11 11:28:44 +0000207 result.rec.avg_interval,
208 result.rec.dev_interval,
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000209 result.rec.max_burst));
210 }
211 }
212
213 if (dir & PJMEDIA_DIR_PLAYBACK) {
214 if (result.play.frame_cnt==0) {
215 PJ_LOG(1,(THIS_FILE, "Error: no playback!"));
216 } else {
Nanang Izzuddinfa923822009-03-11 11:28:44 +0000217 PJ_LOG(3,(THIS_FILE, " %-20s: interval (min/max/avg/dev)=%u/%u/%u/%u, burst=%u",
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000218 "Playback result",
Nanang Izzuddinfa923822009-03-11 11:28:44 +0000219 result.play.min_interval,
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000220 result.play.max_interval,
Nanang Izzuddinfa923822009-03-11 11:28:44 +0000221 result.play.avg_interval,
222 result.play.dev_interval,
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000223 result.play.max_burst));
224 }
225 }
226
227 if (dir==PJMEDIA_DIR_CAPTURE_PLAYBACK) {
Nanang Izzuddinfa923822009-03-11 11:28:44 +0000228 if (result.rec_drift_per_sec == 0) {
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000229 PJ_LOG(3,(THIS_FILE, " No clock drift detected"));
230 } else {
231 const char *which = result.rec_drift_per_sec>=0 ? "faster" : "slower";
232 unsigned drift = result.rec_drift_per_sec>=0 ?
233 result.rec_drift_per_sec :
234 -result.rec_drift_per_sec;
235
236 PJ_LOG(3,(THIS_FILE, " Clock drifts detected. Capture device "
237 "is running %d samples per second %s "
238 "than the playback device",
239 drift, which));
240 }
241 }
242}
243
244
Benny Prijono555139d2009-02-19 12:08:19 +0000245static pj_status_t wav_rec_cb(void *user_data, pjmedia_frame *frame)
246{
247 return pjmedia_port_put_frame((pjmedia_port*)user_data, frame);
248}
249
250static void record(unsigned rec_index, const char *filename)
251{
252 pj_pool_t *pool = NULL;
253 pjmedia_port *wav = NULL;
Benny Prijono10454dc2009-02-21 14:21:59 +0000254 pjmedia_aud_param param;
Benny Prijono555139d2009-02-19 12:08:19 +0000255 pjmedia_aud_stream *strm = NULL;
Benny Prijono51fc47f2009-03-17 11:19:48 +0000256 char line[10], *dummy;
Benny Prijono555139d2009-02-19 12:08:19 +0000257 pj_status_t status;
258
259 if (filename == NULL)
260 filename = WAV_FILE;
261
262 pool = pj_pool_create(pjmedia_aud_subsys_get_pool_factory(), "wav",
263 1000, 1000, NULL);
264
265 status = pjmedia_wav_writer_port_create(pool, filename, 16000,
266 1, 320, 16, 0, 0, &wav);
267 if (status != PJ_SUCCESS) {
268 app_perror("Error creating WAV file", status);
269 goto on_return;
270 }
271
Benny Prijono10454dc2009-02-21 14:21:59 +0000272 status = pjmedia_aud_dev_default_param(rec_index, &param);
Benny Prijono555139d2009-02-19 12:08:19 +0000273 if (status != PJ_SUCCESS) {
274 app_perror("pjmedia_aud_dev_default_param()", status);
275 goto on_return;
276 }
277
278 param.dir = PJMEDIA_DIR_CAPTURE;
279 param.clock_rate = wav->info.clock_rate;
280 param.samples_per_frame = wav->info.samples_per_frame;
281 param.channel_count = wav->info.channel_count;
282 param.bits_per_sample = wav->info.bits_per_sample;
283
284 status = pjmedia_aud_stream_create(&param, &wav_rec_cb, NULL, wav,
285 &strm);
286 if (status != PJ_SUCCESS) {
287 app_perror("Error opening the sound device", status);
288 goto on_return;
289 }
290
291 status = pjmedia_aud_stream_start(strm);
292 if (status != PJ_SUCCESS) {
293 app_perror("Error starting the sound device", status);
294 goto on_return;
295 }
296
297 PJ_LOG(3,(THIS_FILE, "Recording started, press ENTER to stop"));
Benny Prijono51fc47f2009-03-17 11:19:48 +0000298 dummy = fgets(line, sizeof(line), stdin);
Benny Prijono555139d2009-02-19 12:08:19 +0000299
300on_return:
301 if (strm) {
302 pjmedia_aud_stream_stop(strm);
303 pjmedia_aud_stream_destroy(strm);
304 }
305 if (wav)
306 pjmedia_port_destroy(wav);
307 if (pool)
308 pj_pool_release(pool);
309}
310
311
312static pj_status_t wav_play_cb(void *user_data, pjmedia_frame *frame)
313{
314 return pjmedia_port_get_frame((pjmedia_port*)user_data, frame);
315}
316
317
318static void play_file(unsigned play_index, const char *filename)
319{
320 pj_pool_t *pool = NULL;
321 pjmedia_port *wav = NULL;
Benny Prijono10454dc2009-02-21 14:21:59 +0000322 pjmedia_aud_param param;
Benny Prijono555139d2009-02-19 12:08:19 +0000323 pjmedia_aud_stream *strm = NULL;
Benny Prijono51fc47f2009-03-17 11:19:48 +0000324 char line[10], *dummy;
Benny Prijono555139d2009-02-19 12:08:19 +0000325 pj_status_t status;
326
327 if (filename == NULL)
328 filename = WAV_FILE;
329
330 pool = pj_pool_create(pjmedia_aud_subsys_get_pool_factory(), "wav",
331 1000, 1000, NULL);
332
333 status = pjmedia_wav_player_port_create(pool, filename, 20, 0, 0, &wav);
334 if (status != PJ_SUCCESS) {
335 app_perror("Error opening WAV file", status);
336 goto on_return;
337 }
338
Benny Prijono10454dc2009-02-21 14:21:59 +0000339 status = pjmedia_aud_dev_default_param(play_index, &param);
Benny Prijono555139d2009-02-19 12:08:19 +0000340 if (status != PJ_SUCCESS) {
341 app_perror("pjmedia_aud_dev_default_param()", status);
342 goto on_return;
343 }
344
345 param.dir = PJMEDIA_DIR_PLAYBACK;
346 param.clock_rate = wav->info.clock_rate;
347 param.samples_per_frame = wav->info.samples_per_frame;
348 param.channel_count = wav->info.channel_count;
349 param.bits_per_sample = wav->info.bits_per_sample;
350
351 status = pjmedia_aud_stream_create(&param, NULL, &wav_play_cb, wav,
352 &strm);
353 if (status != PJ_SUCCESS) {
354 app_perror("Error opening the sound device", status);
355 goto on_return;
356 }
357
358 status = pjmedia_aud_stream_start(strm);
359 if (status != PJ_SUCCESS) {
360 app_perror("Error starting the sound device", status);
361 goto on_return;
362 }
363
364 PJ_LOG(3,(THIS_FILE, "Playback started, press ENTER to stop"));
Benny Prijono51fc47f2009-03-17 11:19:48 +0000365 dummy = fgets(line, sizeof(line), stdin);
Benny Prijono555139d2009-02-19 12:08:19 +0000366
367on_return:
368 if (strm) {
369 pjmedia_aud_stream_stop(strm);
370 pjmedia_aud_stream_destroy(strm);
371 }
372 if (wav)
373 pjmedia_port_destroy(wav);
374 if (pool)
375 pj_pool_release(pool);
376}
377
378
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000379static void print_menu(void)
380{
381 puts("");
382 puts("Audio demo menu:");
383 puts("-------------------------------");
384 puts(" l List devices");
385 puts(" i ID Show device info for device ID");
386 puts(" t RID PID CR PTIM [CH] Perform test on the device:");
387 puts(" RID: record device ID (-1 for no)");
388 puts(" PID: playback device ID (-1 for no)");
389 puts(" CR: clock rate");
390 puts(" PTIM: ptime in ms");
391 puts(" CH: # of channels");
Benny Prijono555139d2009-02-19 12:08:19 +0000392 puts(" r RID [FILE] Record capture device RID to WAV file");
393 puts(" p PID [FILE] Playback WAV file to device ID PID");
Nanang Izzuddin21286f22009-03-27 15:15:46 +0000394 puts(" d [RLAT [PLAT]] Get/set sound device latencies (in ms):");
395 puts(" Specify no param to get current latencies setting");
396 puts(" RLAT : record latency (-1 for default)");
397 puts(" PLAT : playback latency (-1 for default)");
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000398 puts(" v Toggle log verbosity");
399 puts(" q Quit");
400 puts("");
401 printf("Enter selection: ");
402 fflush(stdout);
403}
404
405int main()
406{
407 pj_caching_pool cp;
408 pj_bool_t done = PJ_FALSE;
409 pj_status_t status;
410
411 /* Init pjlib */
412 status = pj_init();
413 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
414
Nanang Izzuddina3aa6f92009-03-16 16:34:33 +0000415 pj_log_set_decor(PJ_LOG_HAS_NEWLINE);
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000416
417 /* Must create a pool factory before we can allocate any memory. */
418 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
419
420 status = pjmedia_aud_subsys_init(&cp.factory);
421 if (status != PJ_SUCCESS) {
422 app_perror("pjmedia_aud_subsys_init()", status);
423 pj_caching_pool_destroy(&cp);
424 pj_shutdown();
425 return 1;
426 }
427
428 list_devices();
429
430 while (!done) {
431 char line[80];
432
433 print_menu();
434
435 if (fgets(line, sizeof(line), stdin)==NULL)
436 break;
437
438 switch (line[0]) {
439 case 'l':
440 list_devices();
441 break;
442
443 case 'i':
444 {
445 unsigned dev_index;
446 if (sscanf(line+2, "%u", &dev_index) != 1) {
447 puts("error: device ID required");
448 break;
449 }
450 show_dev_info(dev_index);
451 }
452 break;
453
454 case 't':
455 {
456 pjmedia_dir dir;
457 int rec_id, play_id;
458 unsigned clock_rate, ptime, chnum;
459 int cnt;
460
461 cnt = sscanf(line+2, "%d %d %u %u %u", &rec_id, &play_id,
462 &clock_rate, &ptime, &chnum);
463 if (cnt < 4) {
464 puts("error: not enough parameters");
465 break;
466 }
467 if (clock_rate < 8000 || clock_rate > 128000) {
468 puts("error: invalid clock rate");
469 break;
470 }
471 if (ptime < 10 || ptime > 500) {
472 puts("error: invalid ptime");
473 break;
474 }
475 if (cnt==5) {
476 if (chnum < 1 || chnum > 4) {
477 puts("error: invalid number of channels");
478 break;
479 }
480 } else {
481 chnum = 1;
482 }
483
484 if (rec_id >= 0 && rec_id < (int)dev_count) {
485 if (play_id >= 0 && play_id < (int)dev_count)
486 dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
487 else
488 dir = PJMEDIA_DIR_CAPTURE;
489 } else if (play_id >= 0 && play_id < (int)dev_count) {
490 dir = PJMEDIA_DIR_PLAYBACK;
491 } else {
492 puts("error: at least one valid device index required");
493 break;
494 }
495
496 test_device(dir, rec_id, play_id, clock_rate, ptime, chnum);
497
498 }
499 break;
500
Benny Prijono555139d2009-02-19 12:08:19 +0000501 case 'r':
502 /* record */
503 {
504 int index;
505 char filename[80];
506 int count;
507
508 count = sscanf(line+2, "%d %s", &index, filename);
509 if (count==1)
510 record(index, NULL);
511 else if (count==2)
512 record(index, filename);
513 else
514 puts("error: invalid command syntax");
515 }
516 break;
517
518 case 'p':
519 /* playback */
520 {
521 int index;
522 char filename[80];
523 int count;
524
525 count = sscanf(line+2, "%d %s", &index, filename);
526 if (count==1)
527 play_file(index, NULL);
528 else if (count==2)
529 play_file(index, filename);
530 else
531 puts("error: invalid command syntax");
532 }
533 break;
534
Nanang Izzuddin21286f22009-03-27 15:15:46 +0000535 case 'd':
536 /* latencies */
537 {
538 int rec_lat, play_lat;
539
540 if (sscanf(line+2, "%d %d", &rec_lat, &play_lat) == 2) {
541 capture_lat = (unsigned)
542 (rec_lat>=0? rec_lat:PJMEDIA_SND_DEFAULT_REC_LATENCY);
543 playback_lat = (unsigned)
544 (play_lat >= 0? play_lat : PJMEDIA_SND_DEFAULT_PLAY_LATENCY);
545 printf("Recording latency=%ums, playback latency=%ums",
546 capture_lat, playback_lat);
547 } else {
548 printf("Current latencies: record=%ums, playback=%ums",
549 capture_lat, playback_lat);
550 }
551 puts("");
552 }
553 break;
554
Benny Prijonoe3ebd552009-02-18 20:14:15 +0000555 case 'v':
556 if (pj_log_get_level() <= 3) {
557 pj_log_set_level(5);
558 puts("Logging set to detail");
559 } else {
560 pj_log_set_level(3);
561 puts("Logging set to quiet");
562 }
563 break;
564
565 case 'q':
566 done = PJ_TRUE;
567 break;
568 }
569 }
570
571 pj_caching_pool_destroy(&cp);
572 pj_shutdown();
573 return 0;
574}
575
576