systest.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. #define THIS_FILE "systest.c"
  21. unsigned test_item_count;
  22. test_item_t test_items[SYSTEST_MAX_TEST];
  23. char doc_path[PATH_LENGTH] = {0};
  24. char res_path[PATH_LENGTH] = {0};
  25. char fpath[PATH_LENGTH];
  26. #define USER_ERROR "User used said not okay"
  27. static void systest_wizard(void);
  28. static void systest_list_audio_devs(void);
  29. static void systest_display_settings(void);
  30. static void systest_play_tone(void);
  31. static void systest_play_wav1(void);
  32. static void systest_play_wav2(void);
  33. static void systest_rec_audio(void);
  34. static void systest_audio_test(void);
  35. static void systest_latency_test(void);
  36. static void systest_aec_test(void);
  37. static void exit_app(void);
  38. /* Menus */
  39. static gui_menu menu_exit = { "Exit", &exit_app };
  40. static gui_menu menu_wizard = { "Run test wizard", &systest_wizard };
  41. static gui_menu menu_playtn = { "Play Tone", &systest_play_tone };
  42. static gui_menu menu_playwv1 = { "Play WAV File1", &systest_play_wav1 };
  43. static gui_menu menu_playwv2 = { "Play WAV File2", &systest_play_wav2 };
  44. static gui_menu menu_recaud = { "Record Audio", &systest_rec_audio };
  45. static gui_menu menu_audtest = { "Device Test", &systest_audio_test };
  46. static gui_menu menu_calclat = { "Latency Test", &systest_latency_test };
  47. static gui_menu menu_sndaec = { "AEC/AES Test", &systest_aec_test };
  48. static gui_menu menu_listdev = { "View Devices", &systest_list_audio_devs };
  49. static gui_menu menu_getsets = { "View Settings", &systest_display_settings };
  50. static gui_menu menu_tests = {
  51. "Tests", NULL,
  52. 10,
  53. {
  54. &menu_wizard,
  55. &menu_audtest,
  56. &menu_playtn,
  57. &menu_playwv1,
  58. &menu_playwv2,
  59. &menu_recaud,
  60. &menu_calclat,
  61. &menu_sndaec,
  62. NULL,
  63. &menu_exit
  64. }
  65. };
  66. static gui_menu menu_options = {
  67. "Options", NULL,
  68. 2,
  69. {
  70. &menu_listdev,
  71. &menu_getsets,
  72. }
  73. };
  74. static gui_menu root_menu = {
  75. "Root", NULL, 2, {&menu_tests, &menu_options}
  76. };
  77. /*****************************************************************/
  78. #if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
  79. PJ_INLINE(char *) add_path(const char *path, const char *fname)
  80. {
  81. pj_ansi_strxcpy(fpath, path, PATH_LENGTH);
  82. pj_ansi_strxcat(fpath, fname, PATH_LENGTH);
  83. return fpath;
  84. }
  85. #else
  86. # define add_path(path, fname) fname
  87. #endif
  88. static void exit_app(void)
  89. {
  90. systest_save_result(add_path(doc_path, RESULT_OUT_PATH));
  91. gui_destroy();
  92. }
  93. #include <pjsua-lib/pjsua.h>
  94. #include <pjmedia_audiodev.h>
  95. typedef struct systest_t
  96. {
  97. pjsua_config ua_cfg;
  98. pjsua_media_config media_cfg;
  99. pjmedia_aud_dev_index rec_id;
  100. pjmedia_aud_dev_index play_id;
  101. } systest_t;
  102. static systest_t systest;
  103. static char textbuf[600];
  104. /* Device ID to test */
  105. int systest_cap_dev_id = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
  106. int systest_play_dev_id = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
  107. static void systest_perror(const char *title, pj_status_t status)
  108. {
  109. char errmsg[PJ_ERR_MSG_SIZE];
  110. char themsg[PJ_ERR_MSG_SIZE + 100];
  111. if (status != PJ_SUCCESS)
  112. pj_strerror(status, errmsg, sizeof(errmsg));
  113. else
  114. pj_ansi_strxcpy(errmsg, "No error", sizeof(errmsg));
  115. pj_ansi_strxcpy(themsg, title, sizeof(themsg));
  116. pj_ansi_strxcat(themsg, ": ", sizeof(themsg));
  117. pj_ansi_strxcat(themsg, errmsg, sizeof(themsg));
  118. gui_msgbox("Error", themsg, WITH_OK);
  119. }
  120. test_item_t *systest_alloc_test_item(const char *title)
  121. {
  122. test_item_t *ti;
  123. if (test_item_count == SYSTEST_MAX_TEST) {
  124. gui_msgbox("Error", "You have done too many tests", WITH_OK);
  125. return NULL;
  126. }
  127. ti = &test_items[test_item_count++];
  128. pj_bzero(ti, sizeof(*ti));
  129. pj_ansi_strxcpy(ti->title, title, sizeof(ti->title));
  130. return ti;
  131. }
  132. /*****************************************************************************
  133. * test: play simple ringback tone and hear it
  134. */
  135. static void systest_play_tone(void)
  136. {
  137. /* Ringtones */
  138. #define RINGBACK_FREQ1 440 /* 400 */
  139. #define RINGBACK_FREQ2 480 /* 450 */
  140. #define RINGBACK_ON 3000 /* 400 */
  141. #define RINGBACK_OFF 4000 /* 200 */
  142. #define RINGBACK_CNT 1 /* 2 */
  143. #define RINGBACK_INTERVAL 4000 /* 2000 */
  144. unsigned i, samples_per_frame;
  145. pjmedia_tone_desc tone[RINGBACK_CNT];
  146. pj_pool_t *pool = NULL;
  147. pjmedia_port *ringback_port = NULL;
  148. enum gui_key key;
  149. int ringback_slot = -1;
  150. test_item_t *ti;
  151. pj_str_t name;
  152. const char *title = "Audio Tone Playback Test";
  153. pj_status_t status;
  154. ti = systest_alloc_test_item(title);
  155. if (!ti)
  156. return;
  157. key = gui_msgbox(title,
  158. "This test will play simple ringback tone to "
  159. "the speaker. Please listen carefully for audio "
  160. "impairments such as stutter. You may need "
  161. "to let this test running for a while to "
  162. "make sure that everything is okay. Press "
  163. "OK to start, CANCEL to skip",
  164. WITH_OKCANCEL);
  165. if (key != KEY_OK) {
  166. ti->skipped = PJ_TRUE;
  167. return;
  168. }
  169. PJ_LOG(3,(THIS_FILE, "Running %s", title));
  170. pool = pjsua_pool_create("ringback", 512, 512);
  171. samples_per_frame = systest.media_cfg.audio_frame_ptime *
  172. systest.media_cfg.clock_rate *
  173. systest.media_cfg.channel_count / 1000;
  174. /* Ringback tone (call is ringing) */
  175. name = pj_str("ringback");
  176. status = pjmedia_tonegen_create2(pool, &name,
  177. systest.media_cfg.clock_rate,
  178. systest.media_cfg.channel_count,
  179. samples_per_frame,
  180. 16, PJMEDIA_TONEGEN_LOOP,
  181. &ringback_port);
  182. if (status != PJ_SUCCESS)
  183. goto on_return;
  184. pj_bzero(&tone, sizeof(tone));
  185. for (i=0; i<RINGBACK_CNT; ++i) {
  186. tone[i].freq1 = RINGBACK_FREQ1;
  187. tone[i].freq2 = RINGBACK_FREQ2;
  188. tone[i].on_msec = RINGBACK_ON;
  189. tone[i].off_msec = RINGBACK_OFF;
  190. }
  191. tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
  192. status = pjmedia_tonegen_play(ringback_port, RINGBACK_CNT, tone,
  193. PJMEDIA_TONEGEN_LOOP);
  194. if (status != PJ_SUCCESS)
  195. goto on_return;
  196. status = pjsua_conf_add_port(pool, ringback_port, &ringback_slot);
  197. if (status != PJ_SUCCESS)
  198. goto on_return;
  199. status = pjsua_conf_connect(ringback_slot, 0);
  200. if (status != PJ_SUCCESS)
  201. goto on_return;
  202. key = gui_msgbox(title,
  203. "Ringback tone should be playing now in the "
  204. "speaker. Press OK to stop. ", WITH_OK);
  205. PJ_UNUSED_ARG(key);
  206. status = PJ_SUCCESS;
  207. on_return:
  208. if (ringback_slot != -1)
  209. pjsua_conf_remove_port(ringback_slot);
  210. if (ringback_port)
  211. pjmedia_port_destroy(ringback_port);
  212. if (pool)
  213. pj_pool_release(pool);
  214. if (status != PJ_SUCCESS) {
  215. systest_perror("Sorry we encounter error when initializing "
  216. "the tone generator", status);
  217. ti->success = PJ_FALSE;
  218. pj_strerror(status, ti->reason, sizeof(ti->reason));
  219. } else {
  220. key = gui_msgbox(title, "Is the audio okay?", WITH_YESNO);
  221. ti->success = (key == KEY_YES);
  222. if (!ti->success)
  223. pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
  224. }
  225. return;
  226. }
  227. /* Util: create file player, each time trying different paths until we get
  228. * the file.
  229. */
  230. static pj_status_t create_player(unsigned path_cnt, const char *paths[],
  231. pjsua_player_id *p_id)
  232. {
  233. pj_str_t name;
  234. pj_status_t status = PJ_ENOTFOUND;
  235. unsigned i;
  236. for (i=0; i<path_cnt; ++i) {
  237. status = pjsua_player_create(pj_cstr(&name, paths[i]), 0, p_id);
  238. if (status == PJ_SUCCESS)
  239. return PJ_SUCCESS;
  240. }
  241. return status;
  242. }
  243. /*****************************************************************************
  244. * test: play WAV file
  245. */
  246. static void systest_play_wav(unsigned path_cnt, const char *paths[])
  247. {
  248. pjsua_player_id play_id = PJSUA_INVALID_ID;
  249. enum gui_key key;
  250. test_item_t *ti;
  251. const char *title = "WAV File Playback Test";
  252. pj_status_t status;
  253. ti = systest_alloc_test_item(title);
  254. if (!ti)
  255. return;
  256. pj_ansi_snprintf(textbuf, sizeof(textbuf),
  257. "This test will play %s file to "
  258. "the speaker. Please listen carefully for audio "
  259. "impairments such as stutter. Let this test run "
  260. "for a while to make sure that everything is okay."
  261. " Press OK to start, CANCEL to skip",
  262. paths[0]);
  263. key = gui_msgbox(title, textbuf,
  264. WITH_OKCANCEL);
  265. if (key != KEY_OK) {
  266. ti->skipped = PJ_TRUE;
  267. return;
  268. }
  269. PJ_LOG(3,(THIS_FILE, "Running %s", title));
  270. /* WAV port */
  271. status = create_player(path_cnt, paths, &play_id);
  272. if (status != PJ_SUCCESS)
  273. goto on_return;
  274. status = pjsua_conf_connect(pjsua_player_get_conf_port(play_id), 0);
  275. if (status != PJ_SUCCESS)
  276. goto on_return;
  277. key = gui_msgbox(title,
  278. "WAV file should be playing now in the "
  279. "speaker. Press OK to stop. ", WITH_OK);
  280. PJ_UNUSED_ARG(key);
  281. status = PJ_SUCCESS;
  282. on_return:
  283. if (play_id != -1)
  284. pjsua_player_destroy(play_id);
  285. if (status != PJ_SUCCESS) {
  286. systest_perror("Sorry we've encountered error", status);
  287. ti->success = PJ_FALSE;
  288. pj_strerror(status, ti->reason, sizeof(ti->reason));
  289. } else {
  290. key = gui_msgbox(title, "Is the audio okay?", WITH_YESNO);
  291. ti->success = (key == KEY_YES);
  292. if (!ti->success)
  293. pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
  294. }
  295. return;
  296. }
  297. static void systest_play_wav1(void)
  298. {
  299. const char *paths[] = { add_path(res_path, WAV_PLAYBACK_PATH),
  300. ALT_PATH1 WAV_PLAYBACK_PATH };
  301. systest_play_wav(PJ_ARRAY_SIZE(paths), paths);
  302. }
  303. static void systest_play_wav2(void)
  304. {
  305. const char *paths[] = { add_path(res_path, WAV_TOCK8_PATH),
  306. ALT_PATH1 WAV_TOCK8_PATH};
  307. systest_play_wav(PJ_ARRAY_SIZE(paths), paths);
  308. }
  309. /*****************************************************************************
  310. * test: record audio
  311. */
  312. static void systest_rec_audio(void)
  313. {
  314. const pj_str_t filename = pj_str(add_path(doc_path, WAV_REC_OUT_PATH));
  315. pj_pool_t *pool = NULL;
  316. enum gui_key key;
  317. pjsua_recorder_id rec_id = PJSUA_INVALID_ID;
  318. pjsua_player_id play_id = PJSUA_INVALID_ID;
  319. pjsua_conf_port_id rec_slot = PJSUA_INVALID_ID;
  320. pjsua_conf_port_id play_slot = PJSUA_INVALID_ID;
  321. pj_status_t status = PJ_SUCCESS;
  322. const char *title = "Audio Recording";
  323. test_item_t *ti;
  324. ti = systest_alloc_test_item(title);
  325. if (!ti)
  326. return;
  327. key = gui_msgbox(title,
  328. "This test will allow you to record audio "
  329. "from the microphone, and playback the "
  330. "audio to the speaker. Press OK to start recording, "
  331. "CANCEL to skip.",
  332. WITH_OKCANCEL);
  333. if (key != KEY_OK) {
  334. ti->skipped = PJ_TRUE;
  335. return;
  336. }
  337. PJ_LOG(3,(THIS_FILE, "Running %s", title));
  338. pool = pjsua_pool_create("rectest", 512, 512);
  339. status = pjsua_recorder_create(&filename, 0, NULL, -1, 0, &rec_id);
  340. if (status != PJ_SUCCESS)
  341. goto on_return;
  342. rec_slot = pjsua_recorder_get_conf_port(rec_id);
  343. status = pjsua_conf_connect(0, rec_slot);
  344. if (status != PJ_SUCCESS)
  345. goto on_return;
  346. key = gui_msgbox(title,
  347. "Recording is in progress now, please say "
  348. "something in the microphone. Press OK "
  349. "to stop recording", WITH_OK);
  350. pjsua_conf_disconnect(0, rec_slot);
  351. rec_slot = PJSUA_INVALID_ID;
  352. pjsua_recorder_destroy(rec_id);
  353. rec_id = PJSUA_INVALID_ID;
  354. status = pjsua_player_create(&filename, 0, &play_id);
  355. if (status != PJ_SUCCESS)
  356. goto on_return;
  357. play_slot = pjsua_player_get_conf_port(play_id);
  358. status = pjsua_conf_connect(play_slot, 0);
  359. if (status != PJ_SUCCESS)
  360. goto on_return;
  361. key = gui_msgbox(title,
  362. "Recording has been stopped. "
  363. "The recorded audio is being played now to "
  364. "the speaker device, in a loop. Listen for "
  365. "any audio impairments. Press OK to stop.",
  366. WITH_OK);
  367. PJ_UNUSED_ARG(key);
  368. on_return:
  369. if (rec_slot != PJSUA_INVALID_ID)
  370. pjsua_conf_disconnect(0, rec_slot);
  371. if (rec_id != PJSUA_INVALID_ID)
  372. pjsua_recorder_destroy(rec_id);
  373. if (play_slot != PJSUA_INVALID_ID)
  374. pjsua_conf_disconnect(play_slot, 0);
  375. if (play_id != PJSUA_INVALID_ID)
  376. pjsua_player_destroy(play_id);
  377. if (pool)
  378. pj_pool_release(pool);
  379. if (status != PJ_SUCCESS) {
  380. systest_perror("Sorry we encountered an error", status);
  381. ti->success = PJ_FALSE;
  382. pj_strerror(status, ti->reason, sizeof(ti->reason));
  383. } else {
  384. key = gui_msgbox(title, "Is the audio okay?", WITH_YESNO);
  385. ti->success = (key == KEY_YES);
  386. if (!ti->success) {
  387. pj_ansi_snprintf(textbuf, sizeof(textbuf),
  388. "You will probably need to copy the recorded "
  389. "WAV file %s to a desktop computer and analyze "
  390. "it, to find out whether it's a recording "
  391. "or playback problem.",
  392. WAV_REC_OUT_PATH);
  393. gui_msgbox(title, textbuf, WITH_OK);
  394. pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
  395. }
  396. }
  397. }
  398. /****************************************************************************
  399. * test: audio system test
  400. */
  401. static void systest_audio_test(void)
  402. {
  403. enum {
  404. GOOD_MAX_INTERVAL = 5,
  405. };
  406. const pjmedia_dir dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
  407. pjmedia_aud_param param;
  408. pjmedia_aud_test_results result;
  409. pj_size_t textbufpos;
  410. enum gui_key key;
  411. unsigned problem_count = 0;
  412. const char *problems[16];
  413. char drifttext[120];
  414. test_item_t *ti;
  415. const char *title = "Audio Device Test";
  416. pj_status_t status;
  417. ti = systest_alloc_test_item(title);
  418. if (!ti)
  419. return;
  420. key = gui_msgbox(title,
  421. "This will run an automated test for about "
  422. "ten seconds or so, and display some "
  423. "statistics about your sound device. "
  424. "Please don't do anything until the test completes. "
  425. "Press OK to start, or CANCEL to skip this test.",
  426. WITH_OKCANCEL);
  427. if (key != KEY_OK) {
  428. ti->skipped = PJ_TRUE;
  429. return;
  430. }
  431. PJ_LOG(3,(THIS_FILE, "Running %s", title));
  432. /* Disable sound device in pjsua first */
  433. pjsua_set_no_snd_dev();
  434. /* Setup parameters */
  435. status = pjmedia_aud_dev_default_param(systest.play_id, &param);
  436. if (status != PJ_SUCCESS) {
  437. systest_perror("Sorry we had error in pjmedia_aud_dev_default_param()", status);
  438. pjsua_set_snd_dev(systest.rec_id, systest.play_id);
  439. ti->success = PJ_FALSE;
  440. pj_strerror(status, ti->reason, sizeof(ti->reason));
  441. ti->reason[sizeof(ti->reason)-1] = '\0';
  442. return;
  443. }
  444. param.dir = dir;
  445. param.rec_id = systest.rec_id;
  446. param.play_id = systest.play_id;
  447. param.clock_rate = systest.media_cfg.snd_clock_rate;
  448. param.channel_count = systest.media_cfg.channel_count;
  449. param.samples_per_frame = param.clock_rate * param.channel_count *
  450. systest.media_cfg.audio_frame_ptime / 1000;
  451. /* Latency settings */
  452. param.flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
  453. PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
  454. param.input_latency_ms = systest.media_cfg.snd_rec_latency;
  455. param.output_latency_ms = systest.media_cfg.snd_play_latency;
  456. /* Run the test */
  457. status = pjmedia_aud_test(&param, &result);
  458. if (status != PJ_SUCCESS) {
  459. systest_perror("Sorry we encountered error with the test", status);
  460. pjsua_set_snd_dev(systest.rec_id, systest.play_id);
  461. ti->success = PJ_FALSE;
  462. pj_strerror(status, ti->reason, sizeof(ti->reason));
  463. ti->reason[sizeof(ti->reason)-1] = '\0';
  464. return;
  465. }
  466. /* Restore pjsua sound device */
  467. pjsua_set_snd_dev(systest.rec_id, systest.play_id);
  468. /* Analyze the result! */
  469. pj_ansi_strxcpy(textbuf, "Here are the audio statistics:\r\n",
  470. sizeof(textbuf));
  471. textbufpos = strlen(textbuf);
  472. if (result.rec.frame_cnt==0) {
  473. problems[problem_count++] =
  474. "No audio frames were captured from the microphone. "
  475. "This means the audio device is not working properly.";
  476. } else {
  477. pj_ansi_snprintf(textbuf+textbufpos,
  478. sizeof(textbuf)-textbufpos,
  479. "Rec : interval (min/max/avg/dev)=\r\n"
  480. " %u/%u/%u/%u (ms)\r\n"
  481. " max burst=%u\r\n",
  482. result.rec.min_interval,
  483. result.rec.max_interval,
  484. result.rec.avg_interval,
  485. result.rec.dev_interval,
  486. result.rec.max_burst);
  487. textbufpos = strlen(textbuf);
  488. if (result.rec.max_burst > GOOD_MAX_INTERVAL) {
  489. problems[problem_count++] =
  490. "Recording max burst is quite high";
  491. }
  492. }
  493. if (result.play.frame_cnt==0) {
  494. problems[problem_count++] =
  495. "No audio frames were played to the speaker. "
  496. "This means the audio device is not working properly.";
  497. } else {
  498. pj_ansi_snprintf(textbuf+textbufpos,
  499. sizeof(textbuf)-textbufpos,
  500. "Play: interval (min/max/avg/dev)=\r\n"
  501. " %u/%u/%u/%u (ms)\r\n"
  502. " burst=%u\r\n",
  503. result.play.min_interval,
  504. result.play.max_interval,
  505. result.play.avg_interval,
  506. result.play.dev_interval,
  507. result.play.max_burst);
  508. textbufpos = strlen(textbuf);
  509. if (result.play.max_burst > GOOD_MAX_INTERVAL) {
  510. problems[problem_count++] =
  511. "Playback max burst is quite high";
  512. }
  513. }
  514. if (result.rec_drift_per_sec) {
  515. const char *which = result.rec_drift_per_sec>=0 ? "faster" : "slower";
  516. unsigned drift = result.rec_drift_per_sec>=0 ?
  517. result.rec_drift_per_sec :
  518. -result.rec_drift_per_sec;
  519. pj_ansi_snprintf(drifttext, sizeof(drifttext),
  520. "Clock drifts detected. Capture "
  521. "is %d samples/sec %s "
  522. "than the playback device",
  523. drift, which);
  524. problems[problem_count++] = drifttext;
  525. }
  526. if (problem_count == 0) {
  527. pj_ansi_snprintf(textbuf+textbufpos,
  528. sizeof(textbuf)-textbufpos,
  529. "\r\nThe sound device seems to be okay!");
  530. textbufpos = strlen(textbuf);
  531. key = gui_msgbox("Audio Device Test", textbuf, WITH_OK);
  532. } else {
  533. unsigned i;
  534. pj_ansi_snprintf(textbuf+textbufpos,
  535. sizeof(textbuf)-textbufpos,
  536. "There could be %d problem(s) with the "
  537. "sound device:\r\n",
  538. problem_count);
  539. textbufpos = strlen(textbuf);
  540. for (i=0; i<problem_count; ++i) {
  541. pj_ansi_snprintf(textbuf+textbufpos,
  542. sizeof(textbuf)-textbufpos,
  543. " %d: %s\r\n", i+1, problems[i]);
  544. textbufpos = strlen(textbuf);
  545. }
  546. key = gui_msgbox(title, textbuf, WITH_OK);
  547. }
  548. ti->success = PJ_TRUE;
  549. pj_ansi_strxcpy(ti->reason, textbuf, sizeof(ti->reason));
  550. }
  551. /****************************************************************************
  552. * sound latency test
  553. */
  554. static int calculate_latency(pj_pool_t *pool, pjmedia_port *wav,
  555. unsigned *lat_sum, unsigned *lat_cnt,
  556. unsigned *lat_min, unsigned *lat_max)
  557. {
  558. pjmedia_frame frm;
  559. short *buf;
  560. unsigned i, clock_rate, samples_per_frame;
  561. pj_size_t read, len;
  562. unsigned start_pos;
  563. pj_bool_t first;
  564. pj_status_t status;
  565. *lat_sum = 0;
  566. *lat_cnt = 0;
  567. *lat_min = 10000;
  568. *lat_max = 0;
  569. samples_per_frame = PJMEDIA_PIA_SPF(&wav->info);
  570. clock_rate = PJMEDIA_PIA_SRATE(&wav->info);
  571. PJ_ASSERT_ON_FAIL(samples_per_frame && clock_rate,
  572. {
  573. systest_perror("Invalid WAV file", PJ_SUCCESS);
  574. return -1;
  575. });
  576. frm.buf = pj_pool_alloc(pool, samples_per_frame * 2);
  577. frm.size = samples_per_frame * 2;
  578. len = pjmedia_wav_player_get_len(wav);
  579. buf = pj_pool_alloc(pool, len + samples_per_frame);
  580. /* Read the whole file */
  581. read = 0;
  582. while (read < len/2) {
  583. status = pjmedia_port_get_frame(wav, &frm);
  584. if (status != PJ_SUCCESS)
  585. break;
  586. pjmedia_copy_samples(buf+read, (short*)frm.buf, samples_per_frame);
  587. read += samples_per_frame;
  588. }
  589. if (read < 2 * clock_rate) {
  590. systest_perror("The WAV file is too short", PJ_SUCCESS);
  591. return -1;
  592. }
  593. /* Zero the first 500ms to remove loud click noises
  594. * (keypad press, etc.)
  595. */
  596. pjmedia_zero_samples(buf, clock_rate / 2);
  597. /* Loop to calculate latency */
  598. start_pos = 0;
  599. first = PJ_TRUE;
  600. while (start_pos < len/2 - clock_rate) {
  601. int max_signal = 0;
  602. unsigned max_signal_pos = start_pos;
  603. unsigned max_echo_pos = 0;
  604. unsigned pos;
  605. unsigned lat;
  606. /* Get the largest signal in the next 0.7s */
  607. for (i=start_pos; i<start_pos + clock_rate * 700 / 1000; ++i) {
  608. if (abs(buf[i]) > max_signal) {
  609. max_signal = abs(buf[i]);
  610. max_signal_pos = i;
  611. }
  612. }
  613. /* Advance 10ms from max_signal_pos */
  614. pos = max_signal_pos + 10 * clock_rate / 1000;
  615. /* Get the largest signal in the next 800ms */
  616. max_signal = 0;
  617. max_echo_pos = pos;
  618. for (i=pos; i<pos+clock_rate * 8 / 10; ++i) {
  619. if (abs(buf[i]) > max_signal) {
  620. max_signal = abs(buf[i]);
  621. max_echo_pos = i;
  622. }
  623. }
  624. lat = (max_echo_pos - max_signal_pos) * 1000 / clock_rate;
  625. #if 0
  626. PJ_LOG(4,(THIS_FILE, "Signal at %dms, echo at %d ms, latency %d ms",
  627. max_signal_pos * 1000 / clock_rate,
  628. max_echo_pos * 1000 / clock_rate,
  629. lat));
  630. #endif
  631. *lat_sum += lat;
  632. (*lat_cnt)++;
  633. if (lat < *lat_min)
  634. *lat_min = lat;
  635. if (lat > *lat_max)
  636. *lat_max = lat;
  637. /* Advance next loop */
  638. if (first) {
  639. start_pos = max_signal_pos + clock_rate * 9 / 10;
  640. first = PJ_FALSE;
  641. } else {
  642. start_pos += clock_rate;
  643. }
  644. }
  645. return 0;
  646. }
  647. static void systest_latency_test(void)
  648. {
  649. const char *ref_wav_paths[] = { add_path(res_path, WAV_TOCK8_PATH), ALT_PATH1 WAV_TOCK8_PATH };
  650. pj_str_t rec_wav_file;
  651. pjsua_player_id play_id = PJSUA_INVALID_ID;
  652. pjsua_conf_port_id play_slot = PJSUA_INVALID_ID;
  653. pjsua_recorder_id rec_id = PJSUA_INVALID_ID;
  654. pjsua_conf_port_id rec_slot = PJSUA_INVALID_ID;
  655. pj_pool_t *pool = NULL;
  656. pjmedia_port *wav_port = NULL;
  657. unsigned lat_sum=0, lat_cnt=0, lat_min=0, lat_max=0;
  658. enum gui_key key;
  659. test_item_t *ti;
  660. const char *title = "Audio Latency Test";
  661. pj_status_t status;
  662. ti = systest_alloc_test_item(title);
  663. if (!ti)
  664. return;
  665. key = gui_msgbox(title,
  666. "This test will try to find the audio device's "
  667. "latency. We will play a special WAV file to the "
  668. "speaker for ten seconds, then at the end "
  669. "calculate the latency. Please don't do anything "
  670. "until the test is done.", WITH_OKCANCEL);
  671. if (key != KEY_OK) {
  672. ti->skipped = PJ_TRUE;
  673. return;
  674. }
  675. key = gui_msgbox(title,
  676. "For this test to work, we must be able to capture "
  677. "the audio played in the speaker (the echo), and only"
  678. " that audio (i.e. you must be in relatively quiet "
  679. "place to run this test). "
  680. "Press OK to start, or CANCEL to skip.",
  681. WITH_OKCANCEL);
  682. if (key != KEY_OK) {
  683. ti->skipped = PJ_TRUE;
  684. return;
  685. }
  686. PJ_LOG(3,(THIS_FILE, "Running %s", title));
  687. status = create_player(PJ_ARRAY_SIZE(ref_wav_paths), ref_wav_paths,
  688. &play_id);
  689. if (status != PJ_SUCCESS)
  690. goto on_return;
  691. play_slot = pjsua_player_get_conf_port(play_id);
  692. rec_wav_file = pj_str(add_path(doc_path, WAV_LATENCY_OUT_PATH));
  693. status = pjsua_recorder_create(&rec_wav_file, 0, NULL, -1, 0, &rec_id);
  694. if (status != PJ_SUCCESS)
  695. goto on_return;
  696. rec_slot = pjsua_recorder_get_conf_port(rec_id);
  697. /* Setup the test */
  698. //status = pjsua_conf_connect(0, 0);
  699. status = pjsua_conf_connect(play_slot, 0);
  700. status = pjsua_conf_connect(0, rec_slot);
  701. status = pjsua_conf_connect(play_slot, rec_slot);
  702. /* We're running */
  703. PJ_LOG(3,(THIS_FILE, "Please wait while test is running (~10 sec)"));
  704. gui_sleep(10);
  705. /* Done with the test */
  706. //status = pjsua_conf_disconnect(0, 0);
  707. status = pjsua_conf_disconnect(play_slot, rec_slot);
  708. status = pjsua_conf_disconnect(0, rec_slot);
  709. status = pjsua_conf_disconnect(play_slot, 0);
  710. pjsua_recorder_destroy(rec_id);
  711. rec_id = PJSUA_INVALID_ID;
  712. pjsua_player_destroy(play_id);
  713. play_id = PJSUA_INVALID_ID;
  714. /* Confirm that echo is heard */
  715. gui_msgbox(title,
  716. "Test is done. Now we need to confirm that we indeed "
  717. "captured the echo. We will play the captured audio "
  718. "and please confirm that you can hear the 'tock' echo.",
  719. WITH_OK);
  720. status = pjsua_player_create(&rec_wav_file, 0, &play_id);
  721. if (status != PJ_SUCCESS)
  722. goto on_return;
  723. play_slot = pjsua_player_get_conf_port(play_id);
  724. status = pjsua_conf_connect(play_slot, 0);
  725. if (status != PJ_SUCCESS)
  726. goto on_return;
  727. key = gui_msgbox(title,
  728. "The captured audio is being played back now. "
  729. "Can you hear the 'tock' echo?",
  730. WITH_YESNO);
  731. pjsua_player_destroy(play_id);
  732. play_id = PJSUA_INVALID_ID;
  733. if (key != KEY_YES)
  734. goto on_return;
  735. /* Now analyze the latency */
  736. pool = pjsua_pool_create("latency", 512, 512);
  737. status = pjmedia_wav_player_port_create(pool, rec_wav_file.ptr, 0, 0, 0, &wav_port);
  738. if (status != PJ_SUCCESS)
  739. goto on_return;
  740. status = calculate_latency(pool, wav_port, &lat_sum, &lat_cnt,
  741. &lat_min, &lat_max);
  742. if (status != PJ_SUCCESS)
  743. goto on_return;
  744. on_return:
  745. if (wav_port)
  746. pjmedia_port_destroy(wav_port);
  747. if (pool)
  748. pj_pool_release(pool);
  749. if (play_id != PJSUA_INVALID_ID)
  750. pjsua_player_destroy(play_id);
  751. if (rec_id != PJSUA_INVALID_ID)
  752. pjsua_recorder_destroy(rec_id);
  753. if (status != PJ_SUCCESS) {
  754. systest_perror("Sorry we encountered an error", status);
  755. ti->success = PJ_FALSE;
  756. pj_strerror(status, ti->reason, sizeof(ti->reason));
  757. } else if (key != KEY_YES) {
  758. ti->success = PJ_FALSE;
  759. if (!ti->success) {
  760. pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
  761. }
  762. } else {
  763. char msg[200];
  764. pj_size_t msglen;
  765. if (lat_cnt == 0)
  766. lat_cnt = 1;
  767. pj_ansi_snprintf(msg, sizeof(msg),
  768. "The sound device latency:\r\n"
  769. " Min=%u, Max=%u, Avg=%u\r\n",
  770. lat_min, lat_max, lat_sum/lat_cnt);
  771. msglen = strlen(msg);
  772. if (lat_sum/lat_cnt > 500) {
  773. pj_ansi_snprintf(msg+msglen, sizeof(msg)-msglen,
  774. "The latency is huge!\r\n");
  775. msglen = strlen(msg);
  776. } else if (lat_sum/lat_cnt > 200) {
  777. pj_ansi_snprintf(msg+msglen, sizeof(msg)-msglen,
  778. "The latency is quite high\r\n");
  779. msglen = strlen(msg);
  780. }
  781. key = gui_msgbox(title, msg, WITH_OK);
  782. ti->success = PJ_TRUE;
  783. pj_ansi_strxcpy(ti->reason, msg, sizeof(ti->reason));
  784. }
  785. }
  786. static void systest_aec_test(void)
  787. {
  788. const char *ref_wav_paths[] = { add_path(res_path, WAV_PLAYBACK_PATH),
  789. ALT_PATH1 WAV_PLAYBACK_PATH };
  790. pjsua_player_id player_id = PJSUA_INVALID_ID;
  791. pjsua_recorder_id writer_id = PJSUA_INVALID_ID;
  792. enum gui_key key;
  793. test_item_t *ti;
  794. const char *title = "AEC/AES Test";
  795. unsigned last_ec_tail = 0;
  796. pj_status_t status;
  797. pj_str_t tmp;
  798. ti = systest_alloc_test_item(title);
  799. if (!ti)
  800. return;
  801. key = gui_msgbox(title,
  802. "This test will try to find whether the AEC/AES "
  803. "works good on this system. Test will play a file "
  804. "while recording from mic. The recording will be "
  805. "played back later so you can check if echo is there. "
  806. "Press OK to start.",
  807. WITH_OKCANCEL);
  808. if (key != KEY_OK) {
  809. ti->skipped = PJ_TRUE;
  810. return;
  811. }
  812. /* Save current EC tail */
  813. status = pjsua_get_ec_tail(&last_ec_tail);
  814. if (status != PJ_SUCCESS)
  815. goto on_return;
  816. /* Set EC tail setting to default */
  817. status = pjsua_set_ec(PJSUA_DEFAULT_EC_TAIL_LEN, 0);
  818. if (status != PJ_SUCCESS)
  819. goto on_return;
  820. /*
  821. * Create player and recorder
  822. */
  823. status = create_player(PJ_ARRAY_SIZE(ref_wav_paths), ref_wav_paths,
  824. &player_id);
  825. if (status != PJ_SUCCESS) {
  826. PJ_PERROR(1,(THIS_FILE, status, "Error opening WAV file %s",
  827. WAV_PLAYBACK_PATH));
  828. goto on_return;
  829. }
  830. status = pjsua_recorder_create(
  831. pj_cstr(&tmp, add_path(doc_path, AEC_REC_PATH)), 0, 0, -1,
  832. 0, &writer_id);
  833. if (status != PJ_SUCCESS) {
  834. PJ_PERROR(1,(THIS_FILE, status, "Error writing WAV file %s",
  835. AEC_REC_PATH));
  836. goto on_return;
  837. }
  838. /*
  839. * Start playback and recording.
  840. */
  841. pjsua_conf_connect(pjsua_player_get_conf_port(player_id), 0);
  842. pj_thread_sleep(100);
  843. pjsua_conf_connect(0, pjsua_recorder_get_conf_port(writer_id));
  844. /* Wait user signal */
  845. gui_msgbox(title, "AEC/AES test is running. Press OK to stop this test.",
  846. WITH_OK);
  847. /*
  848. * Stop and close playback and recorder
  849. */
  850. pjsua_conf_disconnect(0, pjsua_recorder_get_conf_port(writer_id));
  851. pjsua_conf_disconnect(pjsua_player_get_conf_port(player_id), 0);
  852. pjsua_recorder_destroy(writer_id);
  853. pjsua_player_destroy(player_id);
  854. player_id = PJSUA_INVALID_ID;
  855. writer_id = PJSUA_INVALID_ID;
  856. /*
  857. * Play the result.
  858. */
  859. status = pjsua_player_create(
  860. pj_cstr(&tmp, add_path(doc_path, AEC_REC_PATH)),
  861. 0, &player_id);
  862. if (status != PJ_SUCCESS) {
  863. PJ_PERROR(1,(THIS_FILE, status, "Error opening WAV file %s", AEC_REC_PATH));
  864. goto on_return;
  865. }
  866. pjsua_conf_connect(pjsua_player_get_conf_port(player_id), 0);
  867. /* Wait user signal */
  868. gui_msgbox(title, "We are now playing the captured audio from the mic. "
  869. "Check if echo (of the audio played back previously) is "
  870. "present in the audio. The recording is stored in "
  871. AEC_REC_PATH " for offline analysis. "
  872. "Press OK to stop.",
  873. WITH_OK);
  874. pjsua_conf_disconnect(pjsua_player_get_conf_port(player_id), 0);
  875. key = gui_msgbox(title,
  876. "Did you notice any echo in the recording?",
  877. WITH_YESNO);
  878. on_return:
  879. if (player_id != PJSUA_INVALID_ID)
  880. pjsua_player_destroy(player_id);
  881. if (writer_id != PJSUA_INVALID_ID)
  882. pjsua_recorder_destroy(writer_id);
  883. /* Wait until sound device closed before restoring back EC tail setting */
  884. while (pjsua_snd_is_active())
  885. pj_thread_sleep(10);
  886. pjsua_set_ec(last_ec_tail, 0);
  887. if (status != PJ_SUCCESS) {
  888. systest_perror("Sorry we encountered an error", status);
  889. ti->success = PJ_FALSE;
  890. pj_strerror(status, ti->reason, sizeof(ti->reason));
  891. } else if (key == KEY_YES) {
  892. ti->success = PJ_FALSE;
  893. if (!ti->success) {
  894. pj_ansi_strxcpy(ti->reason, USER_ERROR, sizeof(ti->reason));
  895. }
  896. } else {
  897. char msg[200];
  898. pj_ansi_snprintf(msg, sizeof(msg), "Test succeeded.\r\n");
  899. ti->success = PJ_TRUE;
  900. pj_ansi_strxcpy(ti->reason, msg, sizeof(ti->reason));
  901. }
  902. }
  903. /****************************************************************************
  904. * configurations
  905. */
  906. static void systest_list_audio_devs()
  907. {
  908. unsigned i, dev_count;
  909. pj_size_t len=0;
  910. pj_status_t status;
  911. test_item_t *ti;
  912. enum gui_key key;
  913. const char *title = "Audio Device List";
  914. ti = systest_alloc_test_item(title);
  915. if (!ti)
  916. return;
  917. PJ_LOG(3,(THIS_FILE, "Running %s", title));
  918. dev_count = pjmedia_aud_dev_count();
  919. if (dev_count == 0) {
  920. key = gui_msgbox(title,
  921. "No audio devices are found", WITH_OK);
  922. ti->success = PJ_FALSE;
  923. pj_ansi_strxcpy(ti->reason, "No device found", sizeof(ti->reason));
  924. return;
  925. }
  926. pj_ansi_snprintf(ti->reason+len, sizeof(ti->reason)-len,
  927. "Found %u devices\r\n", dev_count);
  928. len = strlen(ti->reason);
  929. for (i=0; i<dev_count; ++i) {
  930. pjmedia_aud_dev_info info;
  931. status = pjmedia_aud_dev_get_info(i, &info);
  932. if (status != PJ_SUCCESS) {
  933. systest_perror("Error retrieving device info", status);
  934. ti->success = PJ_FALSE;
  935. pj_strerror(status, ti->reason, sizeof(ti->reason));
  936. return;
  937. }
  938. pj_ansi_snprintf(ti->reason+len, sizeof(ti->reason)-len,
  939. " %2d: %s [%s] (%d/%d)\r\n",
  940. i, info.driver, info.name,
  941. info.input_count, info.output_count);
  942. len = strlen(ti->reason);
  943. }
  944. ti->reason[len] = '\0';
  945. key = gui_msgbox(title, ti->reason, WITH_OK);
  946. PJ_UNUSED_ARG(key);
  947. ti->success = PJ_TRUE;
  948. }
  949. static void systest_display_settings(void)
  950. {
  951. pjmedia_aud_dev_info di;
  952. pj_size_t len = 0;
  953. enum gui_key key;
  954. test_item_t *ti;
  955. const char *title = "Audio Settings";
  956. pj_status_t status;
  957. ti = systest_alloc_test_item(title);
  958. if (!ti)
  959. return;
  960. PJ_LOG(3,(THIS_FILE, "Running %s", title));
  961. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Version: %s\r\n",
  962. pj_get_version());
  963. len = strlen(textbuf);
  964. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Test clock rate: %d\r\n",
  965. systest.media_cfg.clock_rate);
  966. len = strlen(textbuf);
  967. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Device clock rate: %d\r\n",
  968. systest.media_cfg.snd_clock_rate);
  969. len = strlen(textbuf);
  970. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Aud frame ptime: %d\r\n",
  971. systest.media_cfg.audio_frame_ptime);
  972. len = strlen(textbuf);
  973. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Channel count: %d\r\n",
  974. systest.media_cfg.channel_count);
  975. len = strlen(textbuf);
  976. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Audio switching: %s\r\n",
  977. (PJMEDIA_CONF_USE_SWITCH_BOARD ? "Switchboard" : "Conf bridge"));
  978. len = strlen(textbuf);
  979. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len, "Snd buff count: %d\r\n",
  980. PJMEDIA_SOUND_BUFFER_COUNT);
  981. len = strlen(textbuf);
  982. /* Capture device */
  983. status = pjmedia_aud_dev_get_info(systest.rec_id, &di);
  984. if (status != PJ_SUCCESS) {
  985. systest_perror("Error querying device info", status);
  986. ti->success = PJ_FALSE;
  987. pj_strerror(status, ti->reason, sizeof(ti->reason));
  988. return;
  989. }
  990. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len,
  991. "Rec dev : %d (%s) [%s]\r\n",
  992. systest.rec_id,
  993. di.name,
  994. di.driver);
  995. len = strlen(textbuf);
  996. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len,
  997. "Rec buf : %d msec\r\n",
  998. systest.media_cfg.snd_rec_latency);
  999. len = strlen(textbuf);
  1000. /* Playback device */
  1001. status = pjmedia_aud_dev_get_info(systest.play_id, &di);
  1002. if (status != PJ_SUCCESS) {
  1003. systest_perror("Error querying device info", status);
  1004. return;
  1005. }
  1006. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len,
  1007. "Play dev: %d (%s) [%s]\r\n",
  1008. systest.play_id,
  1009. di.name,
  1010. di.driver);
  1011. len = strlen(textbuf);
  1012. pj_ansi_snprintf(textbuf+len, sizeof(textbuf)-len,
  1013. "Play buf: %d msec\r\n",
  1014. systest.media_cfg.snd_play_latency);
  1015. len = strlen(textbuf);
  1016. ti->success = PJ_TRUE;
  1017. pj_ansi_strxcpy(ti->reason, textbuf, sizeof(ti->reason));
  1018. key = gui_msgbox(title, textbuf, WITH_OK);
  1019. PJ_UNUSED_ARG(key); /* Warning about unused var */
  1020. }
  1021. /*****************************************************************/
  1022. int systest_init(void)
  1023. {
  1024. pjsua_logging_config log_cfg;
  1025. pj_status_t status = PJ_SUCCESS;
  1026. status = pjsua_create();
  1027. if (status != PJ_SUCCESS) {
  1028. systest_perror("Sorry we've had error in pjsua_create()", status);
  1029. return status;
  1030. }
  1031. pjsua_logging_config_default(&log_cfg);
  1032. log_cfg.log_filename = pj_str(add_path(doc_path, LOG_OUT_PATH));
  1033. pjsua_config_default(&systest.ua_cfg);
  1034. pjsua_media_config_default(&systest.media_cfg);
  1035. systest.media_cfg.clock_rate = TEST_CLOCK_RATE;
  1036. systest.media_cfg.snd_clock_rate = DEV_CLOCK_RATE;
  1037. if (OVERRIDE_AUD_FRAME_PTIME)
  1038. systest.media_cfg.audio_frame_ptime = OVERRIDE_AUD_FRAME_PTIME;
  1039. systest.media_cfg.channel_count = CHANNEL_COUNT;
  1040. systest.rec_id = REC_DEV_ID;
  1041. systest.play_id = PLAY_DEV_ID;
  1042. systest.media_cfg.ec_tail_len = 0;
  1043. systest.media_cfg.snd_auto_close_time = 0;
  1044. #if defined(OVERRIDE_AUDDEV_PLAY_LAT) && OVERRIDE_AUDDEV_PLAY_LAT!=0
  1045. systest.media_cfg.snd_play_latency = OVERRIDE_AUDDEV_PLAY_LAT;
  1046. #endif
  1047. #if defined(OVERRIDE_AUDDEV_REC_LAT) && OVERRIDE_AUDDEV_REC_LAT!=0
  1048. systest.media_cfg.snd_rec_latency = OVERRIDE_AUDDEV_REC_LAT;
  1049. #endif
  1050. status = pjsua_init(&systest.ua_cfg, &log_cfg, &systest.media_cfg);
  1051. if (status != PJ_SUCCESS) {
  1052. pjsua_destroy();
  1053. systest_perror("Sorry we've had error in pjsua_init()", status);
  1054. return status;
  1055. }
  1056. status = pjsua_start();
  1057. if (status != PJ_SUCCESS) {
  1058. pjsua_destroy();
  1059. systest_perror("Sorry we've had error in pjsua_start()", status);
  1060. return status;
  1061. }
  1062. status = gui_init(&root_menu);
  1063. if (status != 0)
  1064. goto on_return;
  1065. return 0;
  1066. on_return:
  1067. gui_destroy();
  1068. return status;
  1069. }
  1070. int systest_set_dev(int cap_dev, int play_dev)
  1071. {
  1072. systest.rec_id = systest_cap_dev_id = cap_dev;
  1073. systest.play_id = systest_play_dev_id = play_dev;
  1074. return pjsua_set_snd_dev(cap_dev, play_dev);
  1075. }
  1076. static void systest_wizard(void)
  1077. {
  1078. PJ_LOG(3,(THIS_FILE, "Running test wizard"));
  1079. systest_list_audio_devs();
  1080. systest_display_settings();
  1081. systest_play_tone();
  1082. systest_play_wav1();
  1083. systest_rec_audio();
  1084. systest_audio_test();
  1085. systest_latency_test();
  1086. systest_aec_test();
  1087. gui_msgbox("Test wizard", "Test wizard complete.", WITH_OK);
  1088. }
  1089. int systest_run(void)
  1090. {
  1091. gui_start(&root_menu);
  1092. return 0;
  1093. }
  1094. void systest_save_result(const char *filename)
  1095. {
  1096. unsigned i;
  1097. pj_oshandle_t fd;
  1098. pj_time_val tv;
  1099. pj_parsed_time pt;
  1100. pj_ssize_t size;
  1101. const char *text;
  1102. pj_status_t status;
  1103. status = pj_file_open(NULL, filename, PJ_O_WRONLY | PJ_O_APPEND, &fd);
  1104. if (status != PJ_SUCCESS) {
  1105. pj_ansi_snprintf(textbuf, sizeof(textbuf),
  1106. "Error opening file %s",
  1107. filename);
  1108. systest_perror(textbuf, status);
  1109. return;
  1110. }
  1111. text = "\r\n\r\nPJSYSTEST Report\r\n";
  1112. size = strlen(text);
  1113. pj_file_write(fd, text, &size);
  1114. /* Put timestamp */
  1115. pj_gettimeofday(&tv);
  1116. if (pj_time_decode(&tv, &pt) == PJ_SUCCESS) {
  1117. pj_ansi_snprintf(textbuf, sizeof(textbuf),
  1118. "Time: %04d/%02d/%02d %02d:%02d:%02d\r\n",
  1119. pt.year, pt.mon+1, pt.day,
  1120. pt.hour, pt.min, pt.sec);
  1121. size = strlen(textbuf);
  1122. pj_file_write(fd, textbuf, &size);
  1123. }
  1124. pj_ansi_snprintf(textbuf, sizeof(textbuf),
  1125. "Tests invoked: %u\r\n"
  1126. "-----------------------------------------------\r\n",
  1127. test_item_count);
  1128. size = strlen(textbuf);
  1129. pj_file_write(fd, textbuf, &size);
  1130. for (i=0; i<test_item_count; ++i) {
  1131. int len;
  1132. test_item_t *ti = &test_items[i];
  1133. len = pj_ansi_snprintf(textbuf, sizeof(textbuf),
  1134. "\r\nTEST %d: %s %s\r\n",
  1135. i, ti->title,
  1136. (ti->skipped? "Skipped" : (ti->success ? "Success" : "Failed")));
  1137. PJ_CHECK_TRUNC_STR(len, textbuf, sizeof(textbuf));
  1138. size = strlen(textbuf);
  1139. pj_file_write(fd, textbuf, &size);
  1140. size = strlen(ti->reason);
  1141. pj_file_write(fd, ti->reason, &size);
  1142. size = 2;
  1143. pj_file_write(fd, "\r\n", &size);
  1144. }
  1145. pj_file_close(fd);
  1146. pj_ansi_snprintf(textbuf, sizeof(textbuf),
  1147. "Test result successfully appended to file %s",
  1148. filename);
  1149. gui_msgbox("Test result saved", textbuf, WITH_OK);
  1150. }
  1151. void systest_deinit(void)
  1152. {
  1153. gui_destroy();
  1154. pjsua_destroy();
  1155. }