Debian Codegraph @

Xpjproject/pjsip-apps/src/pjsystest/systest.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include "systest.h"
19 #include "gui.h"
20
21 #define THIS_FILE "systest.c"
22
23 unsigned test_item_count;
24 test_item_t test_items[SYSTEST_MAX_TEST];
25 char doc_path[PATH_LENGTH] = {0};
26 char res_path[PATH_LENGTH] = {0};
27 char fpath[PATH_LENGTH];
28
29 #define USER_ERROR "User used said not okay"
30
31 static void systest_wizard(void);
32 static void systest_list_audio_devs(void);
33 static void systest_display_settings(void);
34 static void systest_play_tone(void);
35 static void systest_play_wav1(void);
36 static void systest_play_wav2(void);
37 static void systest_play_wav_conf(void);
38 static void systest_rec_audio(void);
39 static void systest_audio_test(void);
40 static void systest_latency_test(void);
41 static void systest_aec_test(void);
42 static void exit_app(void);
43
44 static void systest_enable_sound(void);
45 static void systest_disable_sound(void);
46 static void systest_mute_sound(void);
47
48 static void systest_configure_player1(void);
49 static void systest_connect_player1(void);
50 static void systest_disconnect_player1(void);
51
52 static void systest_configure_player2(void);
53 static void systest_connect_player2(void);
54 static void systest_disconnect_player2(void);
55
56 /* Menus */
57 static gui_menu menu_exit = { "Exit", &exit_app };
58 static gui_menu submenu_exit = { "Return", &gui_destroy };
59
60 static gui_menu menu_wizard = { "Run test wizard", &systest_wizard };
61 static gui_menu menu_playtn = { "Play Tone", &systest_play_tone };
62 static gui_menu menu_playwv1 = { "Play WAV File1", &systest_play_wav1 };
63 static gui_menu menu_playwv2 = { "Play WAV File2", &systest_play_wav2 };
64 static gui_menu menu_playwv_conf = { "Play WAV Files 1+2 (conf)", &systest_play_wav_conf };
65 static gui_menu menu_recaud = { "Record Audio", &systest_rec_audio };
66 static gui_menu menu_audtest = { "Device Test", &systest_audio_test };
67 static gui_menu menu_calclat = { "Latency Test", &systest_latency_test };
68 static gui_menu menu_sndaec = { "AEC/AES Test", &systest_aec_test };
69
70 static gui_menu menu_listdev = { "View Devices", &systest_list_audio_devs };
71 static gui_menu menu_getsets = { "View Settings", &systest_display_settings };
72
73 static gui_menu menu_enable_sound = { "Enable", &systest_enable_sound };
74 static gui_menu menu_disable_sound = { "Disable", &systest_disable_sound };
75 static gui_menu menu_mute_sound = { "Mute", &systest_mute_sound };
76
77 static gui_menu menu_configure1 = { "Disable", &systest_configure_player1 };
78 static gui_menu menu_connect1 = { "Connect", &systest_connect_player1 };
79 static gui_menu menu_disconnect1 = { "Disconnect", &systest_disconnect_player1 };
80
81 static gui_menu menu_configure2 = { "Disable", &systest_configure_player2 };
82 static gui_menu menu_connect2 = { "Connect", &systest_connect_player2 };
83 static gui_menu menu_disconnect2 = { "Disconnect", &systest_disconnect_player2 };
84
85 static gui_menu menu_tests = {
86 "Tests", NULL,
87 10,
88 {
89 &menu_wizard,
90 &menu_audtest,
91 &menu_playtn,
92 &menu_playwv1,
93 &menu_playwv2,
94 &menu_recaud,
95 &menu_calclat,
96 &menu_sndaec,
97 NULL,
98 &menu_exit
99 }
100 };
101
102 static gui_menu menu_tests_conf = {
103 "Tests", NULL,
104 10,
105 {
106 &menu_wizard,
107 &menu_audtest,
108 &menu_playtn,
109 &menu_playwv1,
110 &menu_playwv2,
111 &menu_playwv_conf,
112 &menu_recaud,
113 &menu_calclat,
114 &menu_sndaec,
115 //NULL,
116 //&menu_exit
117 }
118 };
119
120 static gui_menu menu_options = {
121 "Options", NULL,
122 2,
123 {
124 &menu_listdev,
125 &menu_getsets,
126 }
127 };
128
129 static gui_menu menu_sound_conf = {
130 "Sound", NULL,
131 2,
132 {
133 //&menu_enable_sound,
134 &menu_disable_sound,
135 &menu_mute_sound
136 }
137 };
138
139 static gui_menu menu_player1_conf = {
140 "Voice", NULL,
141 2,
142 {
143 &menu_configure1,
144 &menu_disconnect1
145 }
146 };
147
148 static gui_menu menu_player2_conf = {
149 "Metronome", NULL,
150 2,
151 {
152 &menu_configure2,
153 &menu_disconnect2
154 }
155 };
156
157 static gui_menu submenu_tests_conf = {
158 "Conference", NULL,
159 4,
160 {
161 &menu_sound_conf,
162 &menu_player1_conf,
163 &menu_player2_conf,
164 &submenu_exit
165 }
166 };
167
168 static gui_menu root_menu = {
169 "Root", NULL, 2, {&menu_tests, &menu_options, &menu_exit, &menu_tests_conf}
170 };
171
172 /*****************************************************************/
173
174 #if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
175 PJ_INLINE(char *) add_path(const char *path, const char *fname)
176 {
177 pj_ansi_strxcpy(fpath, path, PATH_LENGTH);
178 pj_ansi_strxcat(fpath, fname, PATH_LENGTH);
179 return fpath;
180 }
181 #else
182 # define add_path(path, fname) fname
183 #endif
184
185 static void exit_app(void)
186 {
187 systest_save_result(add_path(doc_path, RESULT_OUT_PATH));
188 gui_destroy();
189 }
190
191
192 #include <pjsua-lib/pjsua.h>
193 #include <pjmedia_audiodev.h>
194
195 typedef struct systest_t
196 {
197 pjsua_config ua_cfg;
198 pjsua_media_config media_cfg;
199 pjmedia_aud_dev_index rec_id;
200 pjmedia_aud_dev_index play_id;
201 } systest_t;
202
203 static systest_t systest;
204 static char textbuf[600];
205
206 /* Device ID to test */
207 int systest_cap_dev_id = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
208 int systest_play_dev_id = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
209
210 pjsua_player_id play_id1 = PJSUA_INVALID_ID;
211 pjsua_player_id play_id2 = PJSUA_INVALID_ID;
212 pjmedia_port_op player1_active = PJMEDIA_PORT_ENABLE;
213 pjmedia_port_op player2_active = PJMEDIA_PORT_ENABLE;
214 pjmedia_port_op sound_active = PJMEDIA_PORT_ENABLE;
215
216
217 static void systest_perror(const char *title, pj_status_t status)
218 {
219 char errmsg[PJ_ERR_MSG_SIZE];
220 char themsg[PJ_ERR_MSG_SIZE + 100];
221
222 if (status != PJ_SUCCESS)
223 pj_strerror(status, errmsg, sizeof(errmsg));
224 else
225 pj_ansi_strxcpy(errmsg, "No error", sizeof(errmsg));
226
227 pj_ansi_strxcpy(themsg, title, sizeof(themsg));
228 pj_ansi_strxcat(themsg, ": ", sizeof(themsg));
229 pj_ansi_strxcat(themsg, errmsg, sizeof(themsg));
230 gui_msgbox("Error", themsg, WITH_OK);
231 }
232
233 test_item_t *systest_alloc_test_item(const char *title)
234 {
235 test_item_t *ti;
236
237 if (test_item_count == SYSTEST_MAX_TEST) {
238 gui_msgbox("Error", "You have done too many tests", WITH_OK);
239 return NULL;
240 }
241
242 ti = &test_items[test_item_count++];
243 pj_bzero(ti, sizeof(*ti));
244 pj_ansi_strxcpy(ti->title, title, sizeof(ti->title));
245
246 return ti;
247 }
248
249 /*****************************************************************************
250 * test: play simple ringback tone and hear it
251 */
252 static void systest_play_tone(void)
253 {
254 /* Ringtones */
255 #define RINGBACK_FREQ1 440 /* 400 */
256 #define RINGBACK_FREQ2 480 /* 450 */
257 #define RINGBACK_ON 3000 /* 400 */
258 #define RINGBACK_OFF 4000 /* 200 */
259 #define RINGBACK_CNT 1 /* 2 */
260 #define RINGBACK_INTERVAL 4000 /* 2000 */
261
262 unsigned i, samples_per_frame;
263 pjmedia_tone_desc tone[RINGBACK_CNT];
264 pj_pool_t *pool = NULL;
265 pjmedia_port *ringback_port = NULL;
266 enum gui_key key;
267 int ringback_slot = -1;
268 test_item_t *ti;
269 pj_str_t name;
270 const char *title = "Audio Tone Playback Test";
271 pj_status_t status;
272
273 ti = systest_alloc_test_item(title);
274 if (!ti)
275 return;
276
277 key = gui_msgbox(title,
278 "This test will play simple ringback tone to "
279 "the speaker. Please listen carefully for audio "
280 "impairments such as stutter. You may need "
281 "to let this test running for a while to "
282 "make sure that everything is okay. Press "
283 "OK to start, CANCEL to skip",
284 WITH_OKCANCEL);
285 if (key != KEY_OK) {
286 ti->skipped = PJ_TRUE;
287 return;
288 }
289
290 PJ_LOG(3,(THIS_FILE, "Running %s", title));
291
292 pool = pjsua_pool_create("ringback", 512, 512);
293 samples_per_frame = systest.media_cfg.audio_frame_ptime *
294 systest.media_cfg.clock_rate *
295 systest.media_cfg.channel_count / 1000;
296
297 /* Ringback tone (call is ringing) */
298 name = pj_str("ringback");
299 status = pjmedia_tonegen_create2(pool, &name,
300 systest.media_cfg.clock_rate,
301 systest.media_cfg.channel_count,
302 samples_per_frame,
303 16, PJMEDIA_TONEGEN_LOOP,
304 &ringback_port);
305 if (status != PJ_SUCCESS)
306 goto on_return;
307
308 pj_bzero(&tone, sizeof(tone));
309 for (i=0; i<RINGBACK_CNT; ++i) {
310 tone[i].freq1 = RINGBACK_FREQ1;
311 tone[i].freq2 = RINGBACK_FREQ2;
312 tone[i].on_msec = RINGBACK_ON;
313 tone[i].off_msec = RINGBACK_OFF;
314 }
315 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
316
317 status = pjmedia_tonegen_play(ringback_port, RINGBACK_CNT, tone,
318 PJMEDIA_TONEGEN_LOOP);
319 if (status != PJ_SUCCESS)
320 goto on_return;
321
322 status = pjsua_conf_add_port(pool, ringback_port, &ringback_slot);
323 if (status != PJ_SUCCESS)
324 goto on_return;
325
326 status = pjsua_conf_connect(ringback_slot, 0);
327 if (status != PJ_SUCCESS)
328 goto on_return;
329
330 key = gui_msgbox(title,
331 "Ringback tone should be playing now in the "
332 "speaker. Press OK to stop. ", WITH_OK);
333 PJ_UNUSED_ARG(key);
334 status = PJ_SUCCESS;
335
336 on_return:
337 if (ringback_slot != -1)
338 pjsua_conf_remove_port(ringback_slot);
339 if (ringback_port)
340 pjmedia_port_destroy(ringback_port);
341 if (pool)
342 pj_pool_release(pool);
343
344 if (status != PJ_SUCCESS) {
345 systest_perror("Sorry we encounter error when initializing "
346 "the tone generator", status);
347 ti->success = PJ_FALSE;
348 pj_strerror(status, ti->reason, sizeof(ti->reason));
349 } else {
350 key = gui_msgbox(title, "Is the audio okay?", WITH_YESNO);
351 ti->success = (key == KEY_YES);
352 if (!ti->success)
353 pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
354 }
355 return;
356 }
357
358 /* Util: create file player, each time trying different paths until we get
359 * the file.
360 */
361 static pj_status_t create_player(unsigned path_cnt, const char *paths[],
362 pjsua_player_id *p_id)
363 {
364 pj_str_t name;
365 pj_status_t status = PJ_ENOTFOUND;
366 unsigned i;
367
368 for (i=0; i<path_cnt; ++i) {
369 status = pjsua_player_create(pj_cstr(&name, paths[i]), 0, p_id);
370 if (status == PJ_SUCCESS)
371 return PJ_SUCCESS;
372 }
373 return status;
374 }
375
376 /*****************************************************************************
377 * test: play WAV file
378 */
379
380 static void rebuild_sound_menu(void)
381 {
382 int i = 0;
383 if (sound_active != PJMEDIA_PORT_ENABLE)
384 menu_sound_conf.submenus[i++] = &menu_enable_sound;
385 if (sound_active != PJMEDIA_PORT_DISABLE)
386 menu_sound_conf.submenus[i++] = &menu_disable_sound;
387 if (sound_active != PJMEDIA_PORT_MUTE)
388 menu_sound_conf.submenus[i++] = &menu_mute_sound;
389 }
390
391 static void rebuild_player1_menu(void)
392 {
393 pj_ansi_strxcpy(menu_configure1.title,
394 player1_active != PJMEDIA_PORT_DISABLE ? "Disable" : "Enable",
395 sizeof(menu_configure1.title));
396 }
397
398 static void rebuild_player2_menu(void)
399 {
400 pj_ansi_strxcpy(menu_configure2.title,
401 player2_active != PJMEDIA_PORT_DISABLE ? "Disable" : "Enable",
402 sizeof(menu_configure2.title));
403 }
404
405 static void systest_enable_sound(void)
406 {
407 pj_status_t status;
408 status = pjsua_conf_configure_port(0,
409 PJMEDIA_PORT_ENABLE,
410 PJMEDIA_PORT_NO_CHANGE);
411 if (status != PJ_SUCCESS)
412 gui_destroy();
413 else {
414 sound_active = PJMEDIA_PORT_ENABLE;
415 rebuild_sound_menu();
416 }
417 }
418
419 static void systest_disable_sound(void)
420 {
421 pj_status_t status;
422 status = pjsua_conf_configure_port(0,
423 PJMEDIA_PORT_DISABLE,
424 PJMEDIA_PORT_NO_CHANGE);
425 if (status != PJ_SUCCESS)
426 gui_destroy();
427 else {
428 sound_active = PJMEDIA_PORT_DISABLE;
429 rebuild_sound_menu();
430 }
431 }
432
433 static void systest_mute_sound(void)
434 {
435 pj_status_t status;
436 status = pjsua_conf_configure_port(0,
437 PJMEDIA_PORT_MUTE,
438 PJMEDIA_PORT_NO_CHANGE);
439 if (status != PJ_SUCCESS)
440 gui_destroy();
441 else {
442 sound_active = PJMEDIA_PORT_MUTE;
443 rebuild_sound_menu();
444 }
445 }
446
447 static void systest_configure_player1(void)
448 {
449 pj_status_t status;
450 player1_active = player1_active==PJMEDIA_PORT_DISABLE ? PJMEDIA_PORT_ENABLE : PJMEDIA_PORT_DISABLE;
451 status = pjsua_conf_configure_port(pjsua_player_get_conf_port(play_id1),
452 PJMEDIA_PORT_NO_CHANGE,
453 player1_active);
454 if (status != PJ_SUCCESS)
455 gui_destroy();
456 else
457 rebuild_player1_menu();
458 }
459
460 static void systest_connect_player1(void)
461 {
462 pj_status_t status;
463 status = pjsua_conf_connect(pjsua_player_get_conf_port(play_id1), 0);
464 if (status != PJ_SUCCESS)
465 gui_destroy();
466 else
467 menu_player1_conf.submenus[1] = &menu_disconnect1;
468 }
469
470 static void systest_disconnect_player1(void)
471 {
472 pj_status_t status;
473 status = pjsua_conf_disconnect(pjsua_player_get_conf_port(play_id1), 0);
474 if (status != PJ_SUCCESS)
475 gui_destroy();
476 else
477 menu_player1_conf.submenus[1] = &menu_connect1;
478 }
479
480 static void systest_configure_player2(void)
481 {
482 pj_status_t status;
483 player2_active = player2_active==PJMEDIA_PORT_DISABLE ? PJMEDIA_PORT_ENABLE : PJMEDIA_PORT_DISABLE;
484 status = pjsua_conf_configure_port(pjsua_player_get_conf_port(play_id2),
485 PJMEDIA_PORT_NO_CHANGE,
486 player2_active);
487 if (status != PJ_SUCCESS)
488 gui_destroy();
489 else
490 rebuild_player2_menu();
491 }
492
493 static void systest_connect_player2(void)
494 {
495 pj_status_t status;
496 status = pjsua_conf_connect(pjsua_player_get_conf_port(play_id2), 0);
497 if (status != PJ_SUCCESS)
498 gui_destroy();
499 else
500 menu_player2_conf.submenus[1] = &menu_disconnect2;
501 }
502
503 static void systest_disconnect_player2(void)
504 {
505 pj_status_t status;
506 status = pjsua_conf_disconnect(pjsua_player_get_conf_port(play_id2), 0);
507 if (status != PJ_SUCCESS)
508 gui_destroy();
509 else
510 menu_player2_conf.submenus[1] = &menu_connect2;
511 }
512
513
514 static void systest_play_wav_impl(unsigned path_cnt, const char *paths[],
515 unsigned path_cnt2, const char *paths2[])
516 {
517 enum gui_key key;
518 test_item_t *ti;
519 const char *title = "WAV File Playback Test";
520 pj_status_t status;
521
522 play_id1 = PJSUA_INVALID_ID;
523 play_id2 = PJSUA_INVALID_ID;
524
525 ti = systest_alloc_test_item(title);
526 if (!ti)
527 return;
528
529 if (path_cnt2)
530 pj_ansi_snprintf(textbuf, sizeof(textbuf),
531 "This test will play the sum of %s (voice) "
532 "and %s (metronome) files to "
533 "the speaker. Please listen carefully for audio "
534 "impairments such as stutter. Let this test run "
535 "for a while to make sure that everything is okay."
536 " Press OK to start, CANCEL to skip",
537 paths[0], paths2[0]);
538 else
539 pj_ansi_snprintf(textbuf, sizeof(textbuf),
540 "This test will play %s file to "
541 "the speaker. Please listen carefully for audio "
542 "impairments such as stutter. Let this test run "
543 "for a while to make sure that everything is okay."
544 " Press OK to start, CANCEL to skip",
545 paths[0]);
546
547 key = gui_msgbox(title, textbuf,
548 WITH_OKCANCEL);
549 if (key != KEY_OK) {
550 ti->skipped = PJ_TRUE;
551 return;
552 }
553
554 PJ_LOG(3,(THIS_FILE, "Running %s", title));
555
556 /* WAV port */
557 status = create_player(path_cnt, paths, &play_id1);
558 if (status != PJ_SUCCESS)
559 goto on_return;
560 player1_active = PJMEDIA_PORT_ENABLE;
561
562 if (path_cnt2) {
563 status = create_player(path_cnt2, paths2, &play_id2);
564 if (status != PJ_SUCCESS)
565 goto on_return;
566 player2_active = PJMEDIA_PORT_ENABLE;
567 }
568
569 status = pjsua_conf_connect(pjsua_player_get_conf_port(play_id1), 0);
570 if (status != PJ_SUCCESS)
571 goto on_return;
572
573 if (path_cnt2) {
574 /* Connect the two players */
575 status = pjsua_conf_connect(pjsua_player_get_conf_port(play_id2), 0);
576 if (status != PJ_SUCCESS)
577 goto on_return;
578
579 /* Show the conference menu, so that user can switch between the two players. */
580 rebuild_sound_menu();
581 rebuild_player1_menu();
582 menu_player1_conf.submenus[1] = &menu_disconnect1;
583 rebuild_player2_menu();
584 menu_player2_conf.submenus[1] = &menu_disconnect2;
585 gui_start(&submenu_tests_conf);
586 systest_enable_sound();
587
588 } else {
589 key = gui_msgbox(title,
590 "WAV file should be playing now in the "
591 "speaker. Press OK to stop. ", WITH_OK);
592 //PJ_UNUSED_ARG(key);
593 }
594
595 status = PJ_SUCCESS;
596
597 on_return:
598 if (play_id1 != PJSUA_INVALID_ID)
599 pjsua_player_destroy(play_id1);
600
601 if (play_id2 != PJSUA_INVALID_ID)
602 pjsua_player_destroy(play_id2);
603
604 if (status != PJ_SUCCESS) {
605 systest_perror("Sorry we've encountered error", status);
606 ti->success = PJ_FALSE;
607 pj_strerror(status, ti->reason, sizeof(ti->reason));
608 } else {
609 key = gui_msgbox(title, "Is the audio okay?", WITH_YESNO);
610 ti->success = (key == KEY_YES);
611 if (!ti->success)
612 pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
613 }
614 return;
615 }
616
617 static void systest_play_wav(unsigned path_cnt, const char *paths[])
618 {
619 systest_play_wav_impl(path_cnt, paths, 0, NULL);
620 }
621
622 static void systest_play_wav1(void)
623 {
624 const char *paths[] = { add_path(res_path, WAV_PLAYBACK_PATH),
625 ALT_PATH1 WAV_PLAYBACK_PATH };
626 systest_play_wav(PJ_ARRAY_SIZE(paths), paths);
627 }
628
629 static void systest_play_wav2(void)
630 {
631 const char *paths[] = { add_path(res_path, WAV_TOCK8_PATH),
632 ALT_PATH1 WAV_TOCK8_PATH};
633 systest_play_wav(PJ_ARRAY_SIZE(paths), paths);
634 }
635
636 static void systest_play_wav_conf(void)
637 {
638 const char *paths[] = { add_path(res_path, WAV_PLAYBACK_PATH),
639 ALT_PATH1 WAV_PLAYBACK_PATH };
640 const char *paths2[] = { add_path(res_path, WAV_TOCK8_PATH),
641 ALT_PATH1 WAV_TOCK8_PATH};
642 systest_play_wav_impl(PJ_ARRAY_SIZE(paths), paths,
643 PJ_ARRAY_SIZE(paths2), paths2);
644 }
645
646 /*****************************************************************************
647 * test: record audio
648 */
649 static void systest_rec_audio(void)
650 {
651 const pj_str_t filename = pj_str(add_path(doc_path, WAV_REC_OUT_PATH));
652 pj_pool_t *pool = NULL;
653 enum gui_key key;
654 pjsua_recorder_id rec_id = PJSUA_INVALID_ID;
655 pjsua_player_id play_id = PJSUA_INVALID_ID;
656 pjsua_conf_port_id rec_slot = PJSUA_INVALID_ID;
657 pjsua_conf_port_id play_slot = PJSUA_INVALID_ID;
658 pj_status_t status = PJ_SUCCESS;
659 const char *title = "Audio Recording";
660 test_item_t *ti;
661
662 ti = systest_alloc_test_item(title);
663 if (!ti)
664 return;
665
666 key = gui_msgbox(title,
667 "This test will allow you to record audio "
668 "from the microphone, and playback the "
669 "audio to the speaker. Press OK to start recording, "
670 "CANCEL to skip.",
671 WITH_OKCANCEL);
672 if (key != KEY_OK) {
673 ti->skipped = PJ_TRUE;
674 return;
675 }
676
677 PJ_LOG(3,(THIS_FILE, "Running %s", title));
678
679 pool = pjsua_pool_create("rectest", 512, 512);
680
681 status = pjsua_recorder_create(&filename, 0, NULL, -1, 0, &rec_id);
682 if (status != PJ_SUCCESS)
683 goto on_return;
684
685 rec_slot = pjsua_recorder_get_conf_port(rec_id);
686
687 status = pjsua_conf_connect(0, rec_slot);
688 if (status != PJ_SUCCESS)
689 goto on_return;
690
691 key = gui_msgbox(title,
692 "Recording is in progress now, please say "
693 "something in the microphone. Press OK "
694 "to stop recording", WITH_OK);
695
696 pjsua_conf_disconnect(0, rec_slot);
697 rec_slot = PJSUA_INVALID_ID;
698 pjsua_recorder_destroy(rec_id);
699 rec_id = PJSUA_INVALID_ID;
700
701 gui_sleep(1); //HACK! waiting for the recorder to close!
702
703 status = pjsua_player_create(&filename, 0, &play_id);
704 if (status != PJ_SUCCESS)
705 goto on_return;
706
707 play_slot = pjsua_player_get_conf_port(play_id);
708
709 status = pjsua_conf_connect(play_slot, 0);
710 if (status != PJ_SUCCESS)
711 goto on_return;
712
713 key = gui_msgbox(title,
714 "Recording has been stopped. "
715 "The recorded audio is being played now to "
716 "the speaker device, in a loop. Listen for "
717 "any audio impairments. Press OK to stop.",
718 WITH_OK);
719 PJ_UNUSED_ARG(key);
720
721 on_return:
722 if (rec_slot != PJSUA_INVALID_ID)
723 pjsua_conf_disconnect(0, rec_slot);
724 if (rec_id != PJSUA_INVALID_ID)
725 pjsua_recorder_destroy(rec_id);
726 if (play_slot != PJSUA_INVALID_ID)
727 pjsua_conf_disconnect(play_slot, 0);
728 if (play_id != PJSUA_INVALID_ID)
729 pjsua_player_destroy(play_id);
730 if (pool)
731 pj_pool_release(pool);
732
733 if (status != PJ_SUCCESS) {
734 systest_perror("Sorry we encountered an error", status);
735 ti->success = PJ_FALSE;
736 pj_strerror(status, ti->reason, sizeof(ti->reason));
737 } else {
738 key = gui_msgbox(title, "Is the audio okay?", WITH_YESNO);
739 ti->success = (key == KEY_YES);
740 if (!ti->success) {
741 pj_ansi_snprintf(textbuf, sizeof(textbuf),
742 "You will probably need to copy the recorded "
743 "WAV file %s to a desktop computer and analyze "
744 "it, to find out whether it's a recording "
745 "or playback problem.",
746 WAV_REC_OUT_PATH);
747 gui_msgbox(title, textbuf, WITH_OK);
748 pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
749 }
750 }
751 }
752
753
754 /****************************************************************************
755 * test: audio system test
756 */
757 static void systest_audio_test(void)
758 {
759 enum {
760 GOOD_MAX_INTERVAL = 5,
761 };
762 const pjmedia_dir dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
763 pjmedia_aud_param param;
764 pjmedia_aud_test_results result;
765 pj_size_t textbufpos;
766 enum gui_key key;
767 unsigned problem_count = 0;
768 const char *problems[16];
769 char drifttext[120];
770 test_item_t *ti;
771 const char *title = "Audio Device Test";
772 pj_status_t status;
773
774 ti = systest_alloc_test_item(title);
775 if (!ti)
776 return;
777
778 key = gui_msgbox(title,
779 "This will run an automated test for about "
780 "ten seconds or so, and display some "
781 "statistics about your sound device. "
782 "Please don't do anything until the test completes. "
783 "Press OK to start, or CANCEL to skip this test.",
784 WITH_OKCANCEL);
785 if (key != KEY_OK) {
786 ti->skipped = PJ_TRUE;
787 return;
788 }
789
790 PJ_LOG(3,(THIS_FILE, "Running %s", title));
791
792 /* Disable sound device in pjsua first */
793 pjsua_set_no_snd_dev();
794
795 /* Setup parameters */
796 status = pjmedia_aud_dev_default_param(systest.play_id, &param);
797 if (status != PJ_SUCCESS) {
798 systest_perror("Sorry we had error in pjmedia_aud_dev_default_param()", status);
799 pjsua_set_snd_dev(systest.rec_id, systest.play_id);
800 ti->success = PJ_FALSE;
801 pj_strerror(status, ti->reason, sizeof(ti->reason));
802 ti->reason[sizeof(ti->reason)-1] = '\0';
803 return;
804 }
805
806 param.dir = dir;
807 param.rec_id = systest.rec_id;
808 param.play_id = systest.play_id;
809 param.clock_rate = systest.media_cfg.snd_clock_rate;
810 param.channel_count = systest.media_cfg.channel_count;
811 param.samples_per_frame = param.clock_rate * param.channel_count *
812 systest.media_cfg.audio_frame_ptime / 1000;
813
814 /* Latency settings */
815 param.flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
816 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
817 param.input_latency_ms = systest.media_cfg.snd_rec_latency;
818 param.output_latency_ms = systest.media_cfg.snd_play_latency;
819
820 /* Run the test */
821 status = pjmedia_aud_test(&param, &result);
822 if (status != PJ_SUCCESS) {
823 systest_perror("Sorry we encountered error with the test", status);
824 pjsua_set_snd_dev(systest.rec_id, systest.play_id);
825 ti->success = PJ_FALSE;
826 pj_strerror(status, ti->reason, sizeof(ti->reason));
827 ti->reason[sizeof(ti->reason)-1] = '\0';
828 return;
829 }
830
831 /* Restore pjsua sound device */
832 pjsua_set_snd_dev(systest.rec_id, systest.play_id);
833
834 /* Analyze the result! */
835 pj_ansi_strxcpy(textbuf, "Here are the audio statistics:\r\n",
836 sizeof(textbuf));
837 textbufpos = strlen(textbuf);
838
839 if (result.rec.frame_cnt==0) {
840 problems[problem_count++] =
841 "No audio frames were captured from the microphone. "
842 "This means the audio device is not working properly.";
843 } else {
844 pj_ansi_snprintf(textbuf+textbufpos,
845 sizeof(textbuf)-textbufpos,
846 "Rec : interval (min/max/avg/dev)=\r\n"
847 " %u/%u/%u/%u (ms)\r\n"
848 " max burst=%u\r\n",
849 result.rec.min_interval,
850 result.rec.max_interval,
851 result.rec.avg_interval,
852 result.rec.dev_interval,
853 result.rec.max_burst);
854 textbufpos = strlen(textbuf);
855
856 if (result.rec.max_burst > GOOD_MAX_INTERVAL) {
857 problems[problem_count++] =
858 "Recording max burst is quite high";
859 }
860 }
861
862 if (result.play.frame_cnt==0) {
863 problems[problem_count++] =
864 "No audio frames were played to the speaker. "
865 "This means the audio device is not working properly.";
866 } else {
867 pj_ansi_snprintf(textbuf+textbufpos,
868 sizeof(textbuf)-textbufpos,
869 "Play: interval (min/max/avg/dev)=\r\n"
870 " %u/%u/%u/%u (ms)\r\n"
871 " burst=%u\r\n",
872 result.play.min_interval,
873 result.play.max_interval,
874 result.play.avg_interval,
875 result.play.dev_interval,
876 result.play.max_burst);
877 textbufpos = strlen(textbuf);
878
879 if (result.play.max_burst > GOOD_MAX_INTERVAL) {
880 problems[problem_count++] =
881 "Playback max burst is quite high";
882 }
883 }
884
885 if (result.rec_drift_per_sec) {
886 const char *which = result.rec_drift_per_sec>=0 ? "faster" : "slower";
887 unsigned drift = result.rec_drift_per_sec>=0 ?
888 result.rec_drift_per_sec :
889 -result.rec_drift_per_sec;
890
891 pj_ansi_snprintf(drifttext, sizeof(drifttext),
892 "Clock drifts detected. Capture "
893 "is %d samples/sec %s "
894 "than the playback device",
895 drift, which);
896 problems[problem_count++] = drifttext;
897 }
898
899 if (problem_count == 0) {
900 pj_ansi_snprintf(textbuf+textbufpos,
901 sizeof(textbuf)-textbufpos,
902 "\r\nThe sound device seems to be okay!");
903 textbufpos = strlen(textbuf);
904
905 key = gui_msgbox("Audio Device Test", textbuf, WITH_OK);
906 } else {
907 unsigned i;
908
909 pj_ansi_snprintf(textbuf+textbufpos,
910 sizeof(textbuf)-textbufpos,
911 "There could be %d problem(s) with the "
912 "sound device:\r\n",
913 problem_count);
914 textbufpos = strlen(textbuf);
915
916 for (i=0; i<problem_count; ++i) {
917 pj_ansi_snprintf(textbuf+textbufpos,
918 sizeof(textbuf)-textbufpos,
919 " %d: %s\r\n", i+1, problems[i]);
920 textbufpos = strlen(textbuf);
921 }
922
923 key = gui_msgbox(title, textbuf, WITH_OK);
924 }
925
926 ti->success = PJ_TRUE;
927 pj_ansi_strxcpy(ti->reason, textbuf, sizeof(ti->reason));
928 }
929
930
931 /****************************************************************************
932 * sound latency test
933 */
934 static int calculate_latency(pj_pool_t *pool, pjmedia_port *wav,
935 unsigned *lat_sum, unsigned *lat_cnt,
936 unsigned *lat_min, unsigned *lat_max)
937 {
938 pjmedia_frame frm;
939 short *buf;
940 unsigned i, clock_rate, samples_per_frame;
941 pj_size_t read, len;
942 unsigned start_pos;
943 pj_bool_t first;
944 pj_status_t status;
945
946 *lat_sum = 0;
947 *lat_cnt = 0;
948 *lat_min = 10000;
949 *lat_max = 0;
950
951 samples_per_frame = PJMEDIA_PIA_SPF(&wav->info);
952 clock_rate = PJMEDIA_PIA_SRATE(&wav->info);
953 PJ_ASSERT_ON_FAIL(samples_per_frame && clock_rate,
954 {
955 systest_perror("Invalid WAV file", PJ_SUCCESS);
956 return -1;
957 });
958 frm.buf = pj_pool_alloc(pool, samples_per_frame * 2);
959 frm.size = samples_per_frame * 2;
960 len = pjmedia_wav_player_get_len(wav);
961 buf = pj_pool_alloc(pool, len + samples_per_frame);
962
963 /* Read the whole file */
964 read = 0;
965 while (read < len/2) {
966 status = pjmedia_port_get_frame(wav, &frm);
967 if (status != PJ_SUCCESS)
968 break;
969
970 pjmedia_copy_samples(buf+read, (short*)frm.buf, samples_per_frame);
971 read += samples_per_frame;
972 }
973
974 if (read < 2 * clock_rate) {
975 systest_perror("The WAV file is too short", PJ_SUCCESS);
976 return -1;
977 }
978
979 /* Zero the first 500ms to remove loud click noises
980 * (keypad press, etc.)
981 */
982 pjmedia_zero_samples(buf, clock_rate / 2);
983
984 /* Loop to calculate latency */
985 start_pos = 0;
986 first = PJ_TRUE;
987 while (start_pos < len/2 - clock_rate) {
988 int max_signal = 0;
989 unsigned max_signal_pos = start_pos;
990 unsigned max_echo_pos = 0;
991 unsigned pos;
992 unsigned lat;
993
994 /* Get the largest signal in the next 0.7s */
995 for (i=start_pos; i<start_pos + clock_rate * 700 / 1000; ++i) {
996 if (abs(buf[i]) > max_signal) {
997 max_signal = abs(buf[i]);
998 max_signal_pos = i;
999 }
1000 }
1001
1002 /* Advance 10ms from max_signal_pos */
1003 pos = max_signal_pos + 10 * clock_rate / 1000;
1004
1005 /* Get the largest signal in the next 800ms */
1006 max_signal = 0;
1007 max_echo_pos = pos;
1008 for (i=pos; i<pos+clock_rate * 8 / 10; ++i) {
1009 if (abs(buf[i]) > max_signal) {
1010 max_signal = abs(buf[i]);
1011 max_echo_pos = i;
1012 }
1013 }
1014
1015 lat = (max_echo_pos - max_signal_pos) * 1000 / clock_rate;
1016
1017 #if 0
1018 PJ_LOG(4,(THIS_FILE, "Signal at %dms, echo at %d ms, latency %d ms",
1019 max_signal_pos * 1000 / clock_rate,
1020 max_echo_pos * 1000 / clock_rate,
1021 lat));
1022 #endif
1023
1024 *lat_sum += lat;
1025 (*lat_cnt)++;
1026 if (lat < *lat_min)
1027 *lat_min = lat;
1028 if (lat > *lat_max)
1029 *lat_max = lat;
1030
1031 /* Advance next loop */
1032 if (first) {
1033 start_pos = max_signal_pos + clock_rate * 9 / 10;
1034 first = PJ_FALSE;
1035 } else {
1036 start_pos += clock_rate;
1037 }
1038 }
1039
1040 return 0;
1041 }
1042
1043
1044 static void systest_latency_test(void)
1045 {
1046 const char *ref_wav_paths[] = { add_path(res_path, WAV_TOCK8_PATH), ALT_PATH1 WAV_TOCK8_PATH };
1047 pj_str_t rec_wav_file;
1048 pjsua_player_id play_id = PJSUA_INVALID_ID;
1049 pjsua_conf_port_id play_slot = PJSUA_INVALID_ID;
1050 pjsua_recorder_id rec_id = PJSUA_INVALID_ID;
1051 pjsua_conf_port_id rec_slot = PJSUA_INVALID_ID;
1052 pj_pool_t *pool = NULL;
1053 pjmedia_port *wav_port = NULL;
1054 unsigned lat_sum=0, lat_cnt=0, lat_min=0, lat_max=0;
1055 enum gui_key key;
1056 test_item_t *ti;
1057 const char *title = "Audio Latency Test";
1058 pj_status_t status;
1059
1060 ti = systest_alloc_test_item(title);
1061 if (!ti)
1062 return;
1063
1064 key = gui_msgbox(title,
1065 "This test will try to find the audio device's "
1066 "latency. We will play a special WAV file to the "
1067 "speaker for ten seconds, then at the end "
1068 "calculate the latency. Please don't do anything "
1069 "until the test is done.", WITH_OKCANCEL);
1070 if (key != KEY_OK) {
1071 ti->skipped = PJ_TRUE;
1072 return;
1073 }
1074 key = gui_msgbox(title,
1075 "For this test to work, we must be able to capture "
1076 "the audio played in the speaker (the echo), and only"
1077 " that audio (i.e. you must be in relatively quiet "
1078 "place to run this test). "
1079 "Press OK to start, or CANCEL to skip.",
1080 WITH_OKCANCEL);
1081 if (key != KEY_OK) {
1082 ti->skipped = PJ_TRUE;
1083 return;
1084 }
1085
1086 PJ_LOG(3,(THIS_FILE, "Running %s", title));
1087
1088 status = create_player(PJ_ARRAY_SIZE(ref_wav_paths), ref_wav_paths,
1089 &play_id);
1090 if (status != PJ_SUCCESS)
1091 goto on_return;
1092
1093 play_slot = pjsua_player_get_conf_port(play_id);
1094
1095 rec_wav_file = pj_str(add_path(doc_path, WAV_LATENCY_OUT_PATH));
1096 status = pjsua_recorder_create(&rec_wav_file, 0, NULL, -1, 0, &rec_id);
1097 if (status != PJ_SUCCESS)
1098 goto on_return;
1099
1100 rec_slot = pjsua_recorder_get_conf_port(rec_id);
1101
1102 /* Setup the test */
1103 //status = pjsua_conf_connect(0, 0);
1104 status = pjsua_conf_connect(play_slot, 0);
1105 status = pjsua_conf_connect(0, rec_slot);
1106 status = pjsua_conf_connect(play_slot, rec_slot);
1107
1108
1109 /* We're running */
1110 PJ_LOG(3,(THIS_FILE, "Please wait while test is running (~10 sec)"));
1111 gui_sleep(10);
1112
1113 /* Done with the test */
1114 //status = pjsua_conf_disconnect(0, 0);
1115 status = pjsua_conf_disconnect(play_slot, rec_slot);
1116 status = pjsua_conf_disconnect(0, rec_slot);
1117 status = pjsua_conf_disconnect(play_slot, 0);
1118
1119 pjsua_recorder_destroy(rec_id);
1120 rec_id = PJSUA_INVALID_ID;
1121
1122 pjsua_player_destroy(play_id);
1123 play_id = PJSUA_INVALID_ID;
1124
1125 /* Confirm that echo is heard */
1126 gui_msgbox(title,
1127 "Test is done. Now we need to confirm that we indeed "
1128 "captured the echo. We will play the captured audio "
1129 "and please confirm that you can hear the 'tock' echo.",
1130 WITH_OK);
1131
1132 status = pjsua_player_create(&rec_wav_file, 0, &play_id);
1133 if (status != PJ_SUCCESS)
1134 goto on_return;
1135
1136 play_slot = pjsua_player_get_conf_port(play_id);
1137
1138 status = pjsua_conf_connect(play_slot, 0);
1139 if (status != PJ_SUCCESS)
1140 goto on_return;
1141
1142 key = gui_msgbox(title,
1143 "The captured audio is being played back now. "
1144 "Can you hear the 'tock' echo?",
1145 WITH_YESNO);
1146
1147 pjsua_player_destroy(play_id);
1148 play_id = PJSUA_INVALID_ID;
1149
1150 if (key != KEY_YES)
1151 goto on_return;
1152
1153 /* Now analyze the latency */
1154 pool = pjsua_pool_create("latency", 512, 512);
1155
1156 status = pjmedia_wav_player_port_create(pool, rec_wav_file.ptr, 0, 0, 0, &wav_port);
1157 if (status != PJ_SUCCESS)
1158 goto on_return;
1159
1160 status = calculate_latency(pool, wav_port, &lat_sum, &lat_cnt,
1161 &lat_min, &lat_max);
1162 if (status != PJ_SUCCESS)
1163 goto on_return;
1164
1165 on_return:
1166 if (wav_port)
1167 pjmedia_port_destroy(wav_port);
1168 if (pool)
1169 pj_pool_release(pool);
1170 if (play_id != PJSUA_INVALID_ID)
1171 pjsua_player_destroy(play_id);
1172 if (rec_id != PJSUA_INVALID_ID)
1173 pjsua_recorder_destroy(rec_id);
1174
1175 if (status != PJ_SUCCESS) {
1176 systest_perror("Sorry we encountered an error", status);
1177 ti->success = PJ_FALSE;
1178 pj_strerror(status, ti->reason, sizeof(ti->reason));
1179 } else if (key != KEY_YES) {
1180 ti->success = PJ_FALSE;
1181 if (!ti->success) {
1182 pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
1183 }
1184 } else {
1185 char msg[200];
1186 pj_size_t msglen;
1187
1188 if (lat_cnt == 0)
1189 lat_cnt = 1;
1190 pj_ansi_snprintf(msg, sizeof(msg),
1191 "The sound device latency:\r\n"
1192 " Min=%u, Max=%u, Avg=%u\r\n",
1193 lat_min, lat_max, lat_sum/lat_cnt);
1194 msglen = strlen(msg);
1195
1196 if (lat_sum/lat_cnt > 500) {
1197 pj_ansi_snprintf(msg+msglen, sizeof(msg)-msglen,
1198 "The latency is huge!\r\n");
1199 msglen = strlen(msg);
1200 } else if (lat_sum/lat_cnt > 200) {
1201 pj_ansi_snprintf(msg+msglen, sizeof(msg)-msglen,
1202 "The latency is quite high\r\n");
1203 msglen = strlen(msg);
1204 }
1205
1206 key = gui_msgbox(title, msg, WITH_OK);
1207
1208 ti->success = PJ_TRUE;
1209 pj_ansi_strxcpy(ti->reason, msg, sizeof(ti->reason));
1210 }
1211 }
1212
1213
1214 static void systest_aec_test(void)
1215 {
1216 const char *ref_wav_paths[] = { add_path(res_path, WAV_PLAYBACK_PATH),
1217 ALT_PATH1 WAV_PLAYBACK_PATH };
1218 pjsua_player_id player_id = PJSUA_INVALID_ID;
1219 pjsua_recorder_id writer_id = PJSUA_INVALID_ID;
1220 enum gui_key key;
1221 test_item_t *ti;
1222 const char *title = "AEC/AES Test";
1223 unsigned last_ec_tail = 0;
1224 pj_status_t status;
1225 pj_str_t tmp;
1226
1227 ti = systest_alloc_test_item(title);
1228 if (!ti)
1229 return;
1230
1231 key = gui_msgbox(title,
1232 "This test will try to find whether the AEC/AES "
1233 "works good on this system. Test will play a file "
1234 "while recording from mic. The recording will be "
1235 "played back later so you can check if echo is there. "
1236 "Press OK to start.",
1237 WITH_OKCANCEL);
1238 if (key != KEY_OK) {
1239 ti->skipped = PJ_TRUE;
1240 return;
1241 }
1242
1243 /* Save current EC tail */
1244 status = pjsua_get_ec_tail(&last_ec_tail);
1245 if (status != PJ_SUCCESS)
1246 goto on_return;
1247
1248 /* Set EC tail setting to default */
1249 status = pjsua_set_ec(PJSUA_DEFAULT_EC_TAIL_LEN, 0);
1250 if (status != PJ_SUCCESS)
1251 goto on_return;
1252
1253 /*
1254 * Create player and recorder
1255 */
1256 status = create_player(PJ_ARRAY_SIZE(ref_wav_paths), ref_wav_paths,
1257 &player_id);
1258 if (status != PJ_SUCCESS) {
1259 PJ_PERROR(1,(THIS_FILE, status, "Error opening WAV file %s",
1260 WAV_PLAYBACK_PATH));
1261 goto on_return;
1262 }
1263
1264 status = pjsua_recorder_create(
1265 pj_cstr(&tmp, add_path(doc_path, AEC_REC_PATH)), 0, 0, -1,
1266 0, &writer_id);
1267 if (status != PJ_SUCCESS) {
1268 PJ_PERROR(1,(THIS_FILE, status, "Error writing WAV file %s",
1269 AEC_REC_PATH));
1270 goto on_return;
1271 }
1272
1273 /*
1274 * Start playback and recording.
1275 */
1276 pjsua_conf_connect(pjsua_player_get_conf_port(player_id), 0);
1277 pj_thread_sleep(100);
1278 pjsua_conf_connect(0, pjsua_recorder_get_conf_port(writer_id));
1279
1280 /* Wait user signal */
1281 gui_msgbox(title, "AEC/AES test is running. Press OK to stop this test.",
1282 WITH_OK);
1283
1284 /*
1285 * Stop and close playback and recorder
1286 */
1287 pjsua_conf_disconnect(0, pjsua_recorder_get_conf_port(writer_id));
1288 pjsua_conf_disconnect(pjsua_player_get_conf_port(player_id), 0);
1289 pjsua_recorder_destroy(writer_id);
1290 pjsua_player_destroy(player_id);
1291 player_id = PJSUA_INVALID_ID;
1292 writer_id = PJSUA_INVALID_ID;
1293
1294 /*
1295 * Play the result.
1296 */
1297 status = pjsua_player_create(
1298 pj_cstr(&tmp, add_path(doc_path, AEC_REC_PATH)),
1299 0, &player_id);
1300 if (status != PJ_SUCCESS) {
1301 PJ_PERROR(1,(THIS_FILE, status, "Error opening WAV file %s", AEC_REC_PATH));
1302 goto on_return;
1303 }
1304 pjsua_conf_connect(pjsua_player_get_conf_port(player_id), 0);
1305
1306 /* Wait user signal */
1307 gui_msgbox(title, "We are now playing the captured audio from the mic. "
1308 "Check if echo (of the audio played back previously) is "
1309 "present in the audio. The recording is stored in "
1310 AEC_REC_PATH " for offline analysis. "
1311 "Press OK to stop.",
1312 WITH_OK);
1313
1314 pjsua_conf_disconnect(pjsua_player_get_conf_port(player_id), 0);
1315
1316 key = gui_msgbox(title,
1317 "Did you notice any echo in the recording?",
1318 WITH_YESNO);
1319
1320
1321 on_return:
1322 if (player_id != PJSUA_INVALID_ID)
1323 pjsua_player_destroy(player_id);
1324 if (writer_id != PJSUA_INVALID_ID)
1325 pjsua_recorder_destroy(writer_id);
1326
1327 /* Wait until sound device closed before restoring back EC tail setting */
1328 while (pjsua_snd_is_active())
1329 pj_thread_sleep(10);
1330 pjsua_set_ec(last_ec_tail, 0);
1331
1332
1333 if (status != PJ_SUCCESS) {
1334 systest_perror("Sorry we encountered an error", status);
1335 ti->success = PJ_FALSE;
1336 pj_strerror(status, ti->reason, sizeof(ti->reason));
1337 } else if (key == KEY_YES) {
1338 ti->success = PJ_FALSE;
1339 if (!ti->success) {
1340 pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
1341 }
1342 } else {
1343 char msg[200];
1344
1345 pj_ansi_snprintf(msg, sizeof(msg), "Test succeeded.\r\n");
1346
1347 ti->success = PJ_TRUE;
1348 pj_ansi_strxcpy(ti->reason, msg, sizeof(ti->reason));
1349 }
1350 }
1351
1352
1353 /****************************************************************************
1354 * configurations
1355 */
1356 static void systest_list_audio_devs()
1357 {
1358 unsigned i, dev_count;
1359 pj_size_t len=0;
1360 pj_status_t status;
1361 test_item_t *ti;
1362 enum gui_key key;
1363 const char *title = "Audio Device List";
1364
1365 ti = systest_alloc_test_item(title);
1366 if (!ti)
1367 return;
1368
1369 PJ_LOG(3,(THIS_FILE, "Running %s", title));
1370
1371 dev_count = pjmedia_aud_dev_count();
1372 if (dev_count == 0) {
1373 key = gui_msgbox(title,
1374 "No audio devices are found", WITH_OK);
1375 ti->success = PJ_FALSE;
1376 pj_ansi_strxcpy(ti->reason, "No device found", sizeof(ti->reason));
1377 return;
1378 }
1379
1380 pj_ansi_snprintf(ti->reason+len, sizeof(ti->reason)-len,
1381 "Found %u devices\r\n", dev_count);
1382 len = strlen(ti->reason);
1383
1384 for (i=0; i<dev_count; ++i) {
1385 pjmedia_aud_dev_info info;
1386
1387 status = pjmedia_aud_dev_get_info(i, &info);
1388 if (status != PJ_SUCCESS) {
1389 systest_perror("Error retrieving device info", status);
1390 ti->success = PJ_FALSE;
1391 pj_strerror(status, ti->reason, sizeof(ti->reason));
1392 return;
1393 }
1394
1395 pj_ansi_snprintf(ti->reason+len, sizeof(ti->reason)-len,
1396 " %2d: %s [%s] (%d/%d)\r\n",
1397 i, info.driver, info.name,
1398 info.input_count, info.output_count);
1399 len = strlen(ti->reason);
1400 }
1401
1402 ti->reason[len] = '\0';
1403 key = gui_msgbox(title, ti->reason, WITH_OK);
1404 PJ_UNUSED_ARG(key);
1405
1406 ti->success = PJ_TRUE;
1407 }
1408
1409 static void systest_display_settings(void)
1410 {
1411 pjmedia_aud_dev_info di;
1412 pj_size_t len = 0;
1413 enum gui_key key;
1414 test_item_t *ti;
1415 const char *title = "Audio Settings";
1416 pj_status_t status;
1417
1418 ti = systest_alloc_test_item(title);
1419 if (!ti)
1420 return;
1421
1422 PJ_LOG(3,(THIS_FILE, "Running %s", title));
1423
1424 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Version: %s\r\n",
1425 pj_get_version());
1426 len = strlen(textbuf);
1427
1428 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Test clock rate: %d\r\n",
1429 systest.media_cfg.clock_rate);
1430 len = strlen(textbuf);
1431
1432 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Device clock rate: %d\r\n",
1433 systest.media_cfg.snd_clock_rate);
1434 len = strlen(textbuf);
1435
1436 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Aud frame ptime: %d\r\n",
1437 systest.media_cfg.audio_frame_ptime);
1438 len = strlen(textbuf);
1439
1440 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Channel count: %d\r\n",
1441 systest.media_cfg.channel_count);
1442 len = strlen(textbuf);
1443
1444 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Audio switching: %s\r\n",
1445 (PJMEDIA_CONF_BACKEND == PJMEDIA_CONF_SWITCH_BOARD_BACKEND ?
1446 "Switchboard" :
1447 PJMEDIA_CONF_BACKEND == PJMEDIA_CONF_SERIAL_BRIDGE_BACKEND ?
1448 "Conf bridge" : "Parallel conf bridge"));
1449 len = strlen(textbuf);
1450
1451 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Snd buff count: %d\r\n",
1452 PJMEDIA_SOUND_BUFFER_COUNT);
1453 len = strlen(textbuf);
1454
1455 /* Capture device */
1456 status = pjmedia_aud_dev_get_info(systest.rec_id, &di);
1457 if (status != PJ_SUCCESS) {
1458 systest_perror("Error querying device info", status);
1459 ti->success = PJ_FALSE;
1460 pj_strerror(status, ti->reason, sizeof(ti->reason));
1461 return;
1462 }
1463
1464 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len,
1465 "Rec dev : %d (%s) [%s]\r\n",
1466 systest.rec_id,
1467 di.name,
1468 di.driver);
1469 len = strlen(textbuf);
1470
1471 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len,
1472 "Rec buf : %d msec\r\n",
1473 systest.media_cfg.snd_rec_latency);
1474 len = strlen(textbuf);
1475
1476 /* Playback device */
1477 status = pjmedia_aud_dev_get_info(systest.play_id, &di);
1478 if (status != PJ_SUCCESS) {
1479 systest_perror("Error querying device info", status);
1480 return;
1481 }
1482
1483 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len,
1484 "Play dev: %d (%s) [%s]\r\n",
1485 systest.play_id,
1486 di.name,
1487 di.driver);
1488 len = strlen(textbuf);
1489
1490 pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len,
1491 "Play buf: %d msec\r\n",
1492 systest.media_cfg.snd_play_latency);
1493 len = strlen(textbuf);
1494
1495 ti->success = PJ_TRUE;
1496 pj_ansi_strxcpy(ti->reason, textbuf, sizeof(ti->reason));
1497 key = gui_msgbox(title, textbuf, WITH_OK);
1498 PJ_UNUSED_ARG(key); /* Warning about unused var */
1499 }
1500
1501 /*****************************************************************/
1502
1503 int systest_init(void)
1504 {
1505 pjsua_logging_config log_cfg;
1506 pj_status_t status = PJ_SUCCESS;
1507
1508 status = pjsua_create();
1509 if (status != PJ_SUCCESS) {
1510 systest_perror("Sorry we've had error in pjsua_create()", status);
1511 return status;
1512 }
1513
1514 pjsua_logging_config_default(&log_cfg);
1515 log_cfg.log_filename = pj_str(add_path(doc_path, LOG_OUT_PATH));
1516
1517 pjsua_config_default(&systest.ua_cfg);
1518 pjsua_media_config_default(&systest.media_cfg);
1519 systest.media_cfg.clock_rate = TEST_CLOCK_RATE;
1520 systest.media_cfg.snd_clock_rate = DEV_CLOCK_RATE;
1521 if (OVERRIDE_AUD_FRAME_PTIME)
1522 systest.media_cfg.audio_frame_ptime = OVERRIDE_AUD_FRAME_PTIME;
1523 systest.media_cfg.channel_count = CHANNEL_COUNT;
1524 systest.rec_id = REC_DEV_ID;
1525 systest.play_id = PLAY_DEV_ID;
1526 systest.media_cfg.ec_tail_len = 0;
1527 systest.media_cfg.snd_auto_close_time = 0;
1528
1529 #if defined(OVERRIDE_AUDDEV_PLAY_LAT) && OVERRIDE_AUDDEV_PLAY_LAT!=0
1530 systest.media_cfg.snd_play_latency = OVERRIDE_AUDDEV_PLAY_LAT;
1531 #endif
1532
1533 #if defined(OVERRIDE_AUDDEV_REC_LAT) && OVERRIDE_AUDDEV_REC_LAT!=0
1534 systest.media_cfg.snd_rec_latency = OVERRIDE_AUDDEV_REC_LAT;
1535 #endif
1536
1537 status = pjsua_init(&systest.ua_cfg, &log_cfg, &systest.media_cfg);
1538 if (status != PJ_SUCCESS) {
1539 pjsua_destroy();
1540 systest_perror("Sorry we've had error in pjsua_init()", status);
1541 return status;
1542 }
1543
1544 status = pjsua_start();
1545 if (status != PJ_SUCCESS) {
1546 pjsua_destroy();
1547 systest_perror("Sorry we've had error in pjsua_start()", status);
1548 return status;
1549 }
1550
1551 status = gui_init(&root_menu);
1552 if (status != 0)
1553 goto on_return;
1554
1555 return 0;
1556
1557 on_return:
1558 gui_destroy();
1559 return status;
1560 }
1561
1562
1563 int systest_set_dev(int cap_dev, int play_dev)
1564 {
1565 systest.rec_id = systest_cap_dev_id = cap_dev;
1566 systest.play_id = systest_play_dev_id = play_dev;
1567 return pjsua_set_snd_dev(cap_dev, play_dev);
1568 }
1569
1570 static void systest_wizard(void)
1571 {
1572 PJ_LOG(3,(THIS_FILE, "Running test wizard"));
1573 systest_list_audio_devs();
1574 systest_display_settings();
1575 systest_play_tone();
1576 systest_play_wav1();
1577 systest_rec_audio();
1578 systest_audio_test();
1579 systest_latency_test();
1580 systest_aec_test();
1581 gui_msgbox("Test wizard", "Test wizard complete.", WITH_OK);
1582 }
1583
1584
1585 int systest_run(void)
1586 {
1587 gui_start(&root_menu);
1588 return 0;
1589 }
1590
1591 void systest_save_result(const char *filename)
1592 {
1593 unsigned i;
1594 pj_oshandle_t fd;
1595 pj_time_val tv;
1596 pj_parsed_time pt;
1597 pj_ssize_t size;
1598 const char *text;
1599 pj_status_t status;
1600
1601 status = pj_file_open(NULL, filename, PJ_O_WRONLY | PJ_O_APPEND, &fd);
1602 if (status != PJ_SUCCESS) {
1603 pj_ansi_snprintf(textbuf, sizeof(textbuf),
1604 "Error opening file %s",
1605 filename);
1606 systest_perror(textbuf, status);
1607 return;
1608 }
1609
1610 text = "\r\n\r\nPJSYSTEST Report\r\n";
1611 size = strlen(text);
1612 pj_file_write(fd, text, &size);
1613
1614 /* Put timestamp */
1615 pj_gettimeofday(&tv);
1616 if (pj_time_decode(&tv, &pt) == PJ_SUCCESS) {
1617 pj_ansi_snprintf(textbuf, sizeof(textbuf),
1618 "Time: %04d/%02d/%02d %02d:%02d:%02d\r\n",
1619 pt.year, pt.mon+1, pt.day,
1620 pt.hour, pt.min, pt.sec);
1621 size = strlen(textbuf);
1622 pj_file_write(fd, textbuf, &size);
1623 }
1624
1625 pj_ansi_snprintf(textbuf, sizeof(textbuf),
1626 "Tests invoked: %u\r\n"
1627 "-----------------------------------------------\r\n",
1628 test_item_count);
1629 size = strlen(textbuf);
1630 pj_file_write(fd, textbuf, &size);
1631
1632 for (i=0; i<test_item_count; ++i) {
1633 int len;
1634 test_item_t *ti = &test_items[i];
1635
1636 len = pj_ansi_snprintf(textbuf, sizeof(textbuf),
1637 "\r\nTEST %d: %s %s\r\n",
1638 i, ti->title,
1639 (ti->skipped? "Skipped" : (ti->success ? "Success" : "Failed")));
1640 PJ_CHECK_TRUNC_STR(len, textbuf, sizeof(textbuf));
1641 size = strlen(textbuf);
1642 pj_file_write(fd, textbuf, &size);
1643
1644 size = strlen(ti->reason);
1645 pj_file_write(fd, ti->reason, &size);
1646
1647 size = 2;
1648 pj_file_write(fd, "\r\n", &size);
1649 }
1650
1651
1652 pj_file_close(fd);
1653
1654 pj_ansi_snprintf(textbuf, sizeof(textbuf),
1655 "Test result successfully appended to file %s",
1656 filename);
1657 gui_msgbox("Test result saved", textbuf, WITH_OK);
1658 }
1659
1660 void systest_deinit(void)
1661 {
1662 gui_destroy();
1663 pjsua_destroy();
1664 }
1665