pjsua_app_legacy.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pjsua-lib/pjsua.h>
  20. #include "pjsua_app_common.h"
  21. #define THIS_FILE "pjsua_app_legacy.c"
  22. /* An attempt to avoid stdout buffering for python tests:
  23. * - call 'fflush(stdout)' after each call to 'printf()/puts()'
  24. * - apply 'setbuf(stdout, 0)', but it is not guaranteed by the standard:
  25. * http://stackoverflow.com/questions/1716296
  26. */
  27. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  28. (defined (_MSC_VER) && _MSC_VER >= 1400)
  29. /* Variadic macro is introduced in C99; MSVC supports it in since 2005. */
  30. # define printf(...) {printf(__VA_ARGS__);fflush(stdout);}
  31. # define puts(s) {puts(s);fflush(stdout);}
  32. #endif
  33. static pj_bool_t cmd_echo;
  34. /*
  35. * Print buddy list.
  36. */
  37. static void print_buddy_list()
  38. {
  39. pjsua_buddy_id ids[64];
  40. int i;
  41. unsigned count = PJ_ARRAY_SIZE(ids);
  42. puts("Buddy list:");
  43. pjsua_enum_buddies(ids, &count);
  44. if (count == 0) {
  45. puts(" -none-");
  46. } else {
  47. for (i=0; i<(int)count; ++i) {
  48. pjsua_buddy_info info;
  49. if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
  50. continue;
  51. printf(" [%2d] <%.*s> %.*s\n",
  52. ids[i]+1,
  53. (int)info.status_text.slen,
  54. info.status_text.ptr,
  55. (int)info.uri.slen,
  56. info.uri.ptr);
  57. }
  58. }
  59. puts("");
  60. }
  61. /*
  62. * Input URL.
  63. */
  64. static void ui_input_url(const char *title, char *buf, pj_size_t len,
  65. input_result *result, pj_bool_t allow_all)
  66. {
  67. result->nb_result = PJSUA_APP_NO_NB;
  68. result->uri_result = NULL;
  69. print_buddy_list();
  70. printf("Choices:\n"
  71. " 0 For current dialog.\n");
  72. if (pjsua_get_buddy_count() > 0) {
  73. if (allow_all) {
  74. printf(" -1 All %d buddies in buddy list\n",
  75. pjsua_get_buddy_count());
  76. }
  77. printf(" [1 -%2d] Select from buddy list\n",
  78. pjsua_get_buddy_count());
  79. }
  80. printf(" URL An URL\n"
  81. " <Enter> Empty input (or 'q') to cancel\n");
  82. printf("%s: ", title);
  83. fflush(stdout);
  84. if (fgets(buf, (int)len, stdin) == NULL)
  85. return;
  86. len = strlen(buf);
  87. /* Left trim */
  88. while (pj_isspace(*buf)) {
  89. ++buf;
  90. --len;
  91. }
  92. /* Remove trailing newlines */
  93. while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
  94. buf[--len] = '\0';
  95. if (len == 0 || buf[0]=='q')
  96. return;
  97. if (pj_isdigit(*buf) || *buf=='-') {
  98. unsigned i;
  99. if (*buf=='-')
  100. i = 1;
  101. else
  102. i = 0;
  103. for (; i<len; ++i) {
  104. if (!pj_isdigit(buf[i])) {
  105. puts("Invalid input");
  106. return;
  107. }
  108. }
  109. result->nb_result = my_atoi(buf);
  110. if (result->nb_result >= 0 &&
  111. result->nb_result <= (int)pjsua_get_buddy_count())
  112. {
  113. return;
  114. }
  115. if (result->nb_result == -1)
  116. return;
  117. puts("Invalid input");
  118. result->nb_result = PJSUA_APP_NO_NB;
  119. return;
  120. } else {
  121. pj_status_t status;
  122. if ((status=pjsua_verify_url(buf)) != PJ_SUCCESS) {
  123. pjsua_perror(THIS_FILE, "Invalid URL", status);
  124. return;
  125. }
  126. result->uri_result = buf;
  127. }
  128. }
  129. static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
  130. {
  131. char *p;
  132. printf("%s (empty to cancel): ", title); fflush(stdout);
  133. if (fgets(buf, (int)len, stdin) == NULL)
  134. return PJ_FALSE;
  135. /* Remove trailing newlines. */
  136. for (p=buf; ; ++p) {
  137. if (*p=='\r' || *p=='\n') *p='\0';
  138. else if (!*p) break;
  139. }
  140. if (!*buf)
  141. return PJ_FALSE;
  142. return PJ_TRUE;
  143. }
  144. /*
  145. * Print account status.
  146. */
  147. static void print_acc_status(int acc_id)
  148. {
  149. char buf[80];
  150. pjsua_acc_info info;
  151. pjsua_acc_get_info(acc_id, &info);
  152. if (!info.has_registration) {
  153. pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
  154. (int)info.status_text.slen,
  155. info.status_text.ptr);
  156. } else {
  157. pj_ansi_snprintf(buf, sizeof(buf),
  158. "%d/%.*s (expires=%d)",
  159. info.status,
  160. (int)info.status_text.slen,
  161. info.status_text.ptr,
  162. info.expires);
  163. }
  164. printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
  165. acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
  166. printf(" Online status: %.*s\n",
  167. (int)info.online_status_text.slen,
  168. info.online_status_text.ptr);
  169. }
  170. /*
  171. * Show a bit of help.
  172. */
  173. static void keystroke_help()
  174. {
  175. pjsua_acc_id acc_ids[16];
  176. unsigned count = PJ_ARRAY_SIZE(acc_ids);
  177. int i;
  178. printf(">>>>\n");
  179. pjsua_enum_accs(acc_ids, &count);
  180. printf("Account list:\n");
  181. for (i=0; i<(int)count; ++i)
  182. print_acc_status(acc_ids[i]);
  183. print_buddy_list();
  184. //puts("Commands:");
  185. puts("+=============================================================================+");
  186. puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
  187. puts("| | | |");
  188. puts("| m Make new call | +b Add new buddy | +a Add new accnt.|");
  189. puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
  190. puts("| a Answer call | i Send IM | !a Modify accnt. |");
  191. puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
  192. puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
  193. puts("| o Toggle call SDP offer | D Subscribe dlg event | |");
  194. puts("| | Du Unsub dlg event | |");
  195. puts("| v re-inVite (release hold) | t Toggle online status | > Cycle next ac.|");
  196. puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
  197. puts("| ],[ Select next/prev call +--------------------------+-------------------+");
  198. puts("| x Xfer call | Media Commands: | Status & Config: |");
  199. puts("| X Xfer with Replaces | | |");
  200. puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
  201. puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
  202. puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
  203. puts("| | V Adjust audio Volume | f Save config |");
  204. puts("| S Send arbitrary REQUEST | Cp Codec priorities | |");
  205. puts("+-----------------------------------------------------------------------------+");
  206. #if PJSUA_HAS_VIDEO
  207. puts("| Video: \"vid help\" for more info |");
  208. puts("+-----------------------------------------------------------------------------+");
  209. #endif
  210. puts("| q QUIT L ReLoad I IP change n detect NAT type |");
  211. puts("| sleep MS echo [0|1|txt] |");
  212. puts("+=============================================================================+");
  213. i = pjsua_call_get_count();
  214. printf("You have %d active call%s\n", i, (i>1?"s":""));
  215. if (current_call != PJSUA_INVALID_ID) {
  216. pjsua_call_info ci;
  217. if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
  218. printf("Current call id=%d to %.*s [%.*s]\n", current_call,
  219. (int)ci.remote_info.slen, ci.remote_info.ptr,
  220. (int)ci.state_text.slen, ci.state_text.ptr);
  221. }
  222. }
  223. /* Help screen for video */
  224. #if PJSUA_HAS_VIDEO
  225. static void vid_show_help()
  226. {
  227. pj_bool_t vid_enabled = (app_config.vid.vid_cnt > 0);
  228. puts("+=============================================================================+");
  229. puts("| Video commands: |");
  230. puts("| |");
  231. puts("| vid help Show this help screen |");
  232. puts("| vid enable|disable Enable or disable video in next offer/answer |");
  233. puts("| vid acc show Show current account video settings |");
  234. puts("| vid acc autorx on|off Automatically show incoming video on/off |");
  235. puts("| vid acc autotx on|off Automatically offer video on/off |");
  236. puts("| vid acc cap ID Set default capture device for current acc |");
  237. puts("| vid acc rend ID Set default renderer device for current acc |");
  238. puts("| vid call rx on|off N Enable/disable video RX for stream N in curr call |");
  239. puts("| vid call tx on|off N Enable/disable video TX for stream N in curr call |");
  240. puts("| vid call add Add video stream for current call |");
  241. puts("| vid call enable|disable N Enable/disable stream #N in current call |");
  242. puts("| vid call cap N ID Set capture dev ID for stream #N in current call |");
  243. puts("| vid dev list List all video devices |");
  244. puts("| vid dev refresh Refresh video device list |");
  245. puts("| vid dev prev on|off ID Enable/disable preview for specified device ID |");
  246. puts("| vid codec list List video codecs |");
  247. puts("| vid codec prio ID PRIO Set codec ID priority to PRIO |");
  248. puts("| vid codec fps ID NUM DEN Set codec ID framerate to (NUM/DEN) fps |");
  249. puts("| vid codec bw ID AVG MAX Set codec ID bitrate to AVG & MAX kbps |");
  250. puts("| vid codec size ID W H Set codec ID size/resolution to W x H |");
  251. puts("| vid win list List all active video windows |");
  252. puts("| vid win arrange Auto arrange windows |");
  253. puts("| vid win show|hide ID Show/hide the specified video window ID |");
  254. puts("| vid win move ID X Y Move window ID to position X,Y |");
  255. puts("| vid win resize ID w h Resize window ID to the specified width, height |");
  256. puts("| vid win full off|on|dt ID Set fullscreen off/on/desktop for window ID |");
  257. puts("| vid conf list List all video ports in video conference bridge |");
  258. puts("| vid conf cc P Q Connect port P to Q in the video conf bridge |");
  259. puts("| vid conf cd P Q Disconnect port P to Q in the video conf bridge |");
  260. puts("+=============================================================================+");
  261. printf("| Video will be %s in the next offer/answer %s |\n",
  262. (vid_enabled? "enabled" : "disabled"), (vid_enabled? " " : ""));
  263. puts("+=============================================================================+");
  264. }
  265. static void vid_handle_menu(char *menuin)
  266. {
  267. char *argv[8] = {NULL};
  268. int argc = 0;
  269. /* Tokenize */
  270. argv[argc] = strtok(menuin, " \t\r\n");
  271. while (argv[argc] && *argv[argc]) {
  272. argc++;
  273. argv[argc] = strtok(NULL, " \t\r\n");
  274. }
  275. if (argc == 1 || strcmp(argv[1], "help")==0) {
  276. vid_show_help();
  277. } else if (argc == 2 && (strcmp(argv[1], "enable")==0 ||
  278. strcmp(argv[1], "disable")==0))
  279. {
  280. pj_bool_t enabled = (strcmp(argv[1], "enable")==0);
  281. app_config.vid.vid_cnt = (enabled ? 1 : 0);
  282. PJ_LOG(3,(THIS_FILE, "Video will be %s in next offer/answer",
  283. (enabled?"enabled":"disabled")));
  284. } else if (strcmp(argv[1], "acc")==0) {
  285. pjsua_acc_config acc_cfg;
  286. pj_bool_t changed = PJ_FALSE;
  287. pj_pool_t *tmp_pool = pjsua_pool_create("tmp-pjsua", 1000, 1000);
  288. pjsua_acc_get_config(current_acc, tmp_pool, &acc_cfg);
  289. if (argc == 3 && strcmp(argv[2], "show")==0) {
  290. app_config_show_video(current_acc, &acc_cfg);
  291. } else if (argc == 4 && strcmp(argv[2], "autorx")==0) {
  292. int on = (strcmp(argv[3], "on")==0);
  293. acc_cfg.vid_in_auto_show = on;
  294. changed = PJ_TRUE;
  295. } else if (argc == 4 && strcmp(argv[2], "autotx")==0) {
  296. int on = (strcmp(argv[3], "on")==0);
  297. acc_cfg.vid_out_auto_transmit = on;
  298. changed = PJ_TRUE;
  299. } else if (argc == 4 && strcmp(argv[2], "cap")==0) {
  300. int dev = atoi(argv[3]);
  301. acc_cfg.vid_cap_dev = dev;
  302. changed = PJ_TRUE;
  303. } else if (argc == 4 && strcmp(argv[2], "rend")==0) {
  304. int dev = atoi(argv[3]);
  305. acc_cfg.vid_rend_dev = dev;
  306. changed = PJ_TRUE;
  307. } else {
  308. pj_pool_release(tmp_pool);
  309. goto on_error;
  310. }
  311. if (changed) {
  312. pj_status_t status = pjsua_acc_modify(current_acc, &acc_cfg);
  313. if (status != PJ_SUCCESS)
  314. PJ_PERROR(1,(THIS_FILE, status, "Error modifying account %d",
  315. current_acc));
  316. }
  317. pj_pool_release(tmp_pool);
  318. } else if (strcmp(argv[1], "call")==0) {
  319. pjsua_call_vid_strm_op_param param;
  320. pj_status_t status = PJ_SUCCESS;
  321. pjsua_call_vid_strm_op_param_default(&param);
  322. if (argc == 5 && strcmp(argv[2], "rx")==0) {
  323. pjsua_stream_info si;
  324. pj_bool_t on = (strcmp(argv[3], "on") == 0);
  325. param.med_idx = atoi(argv[4]);
  326. if (pjsua_call_get_stream_info(current_call, param.med_idx, &si) ||
  327. si.type != PJMEDIA_TYPE_VIDEO)
  328. {
  329. PJ_PERROR(1,(THIS_FILE, PJ_EINVAL, "Invalid stream"));
  330. return;
  331. }
  332. if (on) param.dir = (si.info.vid.dir | PJMEDIA_DIR_DECODING);
  333. else param.dir = (si.info.vid.dir & PJMEDIA_DIR_ENCODING);
  334. status = pjsua_call_set_vid_strm(current_call,
  335. PJSUA_CALL_VID_STRM_CHANGE_DIR,
  336. &param);
  337. }
  338. else if (argc == 5 && strcmp(argv[2], "tx")==0) {
  339. pj_bool_t on = (strcmp(argv[3], "on") == 0);
  340. pjsua_call_vid_strm_op op = on? PJSUA_CALL_VID_STRM_START_TRANSMIT :
  341. PJSUA_CALL_VID_STRM_STOP_TRANSMIT;
  342. param.med_idx = atoi(argv[4]);
  343. status = pjsua_call_set_vid_strm(current_call, op, &param);
  344. }
  345. else if (argc == 3 && strcmp(argv[2], "add")==0) {
  346. status = pjsua_call_set_vid_strm(current_call,
  347. PJSUA_CALL_VID_STRM_ADD, NULL);
  348. }
  349. else if (argc >= 3 &&
  350. (strcmp(argv[2], "disable")==0 || strcmp(argv[2], "enable")==0))
  351. {
  352. pj_bool_t enable = (strcmp(argv[2], "enable") == 0);
  353. pjsua_call_vid_strm_op op = enable? PJSUA_CALL_VID_STRM_CHANGE_DIR :
  354. PJSUA_CALL_VID_STRM_REMOVE;
  355. param.med_idx = argc >= 4? atoi(argv[3]) : -1;
  356. param.dir = PJMEDIA_DIR_ENCODING_DECODING;
  357. status = pjsua_call_set_vid_strm(current_call, op, &param);
  358. }
  359. else if (argc >= 3 && strcmp(argv[2], "cap")==0) {
  360. param.med_idx = argc >= 4? atoi(argv[3]) : -1;
  361. param.cap_dev = argc >= 5? atoi(argv[4]) : PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
  362. status = pjsua_call_set_vid_strm(current_call,
  363. PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV,
  364. &param);
  365. } else
  366. goto on_error;
  367. if (status != PJ_SUCCESS) {
  368. PJ_PERROR(1,(THIS_FILE, status, "Error modifying video stream"));
  369. }
  370. } else if (argc >= 3 && strcmp(argv[1], "dev")==0) {
  371. if (strcmp(argv[2], "list")==0) {
  372. vid_list_devs();
  373. } else if (strcmp(argv[2], "refresh")==0) {
  374. pjmedia_vid_dev_refresh();
  375. } else if (strcmp(argv[2], "prev")==0) {
  376. if (argc != 5) {
  377. goto on_error;
  378. } else {
  379. pj_bool_t on = (strcmp(argv[3], "on") == 0);
  380. int dev_id = atoi(argv[4]);
  381. if (on) {
  382. pjsua_vid_preview_param param;
  383. pjsua_vid_preview_param_default(&param);
  384. param.wnd_flags = PJMEDIA_VID_DEV_WND_BORDER |
  385. PJMEDIA_VID_DEV_WND_RESIZABLE;
  386. pjsua_vid_preview_start(dev_id, &param);
  387. arrange_window(pjsua_vid_preview_get_win(dev_id));
  388. } else {
  389. pjsua_vid_win_id wid;
  390. wid = pjsua_vid_preview_get_win(dev_id);
  391. if (wid != PJSUA_INVALID_ID) {
  392. /* Preview window hiding once it is stopped is
  393. * responsibility of app */
  394. pjsua_vid_win_set_show(wid, PJ_FALSE);
  395. pjsua_vid_preview_stop(dev_id);
  396. }
  397. }
  398. }
  399. } else
  400. goto on_error;
  401. } else if (strcmp(argv[1], "win")==0) {
  402. pj_status_t status = PJ_SUCCESS;
  403. if (argc==3 && strcmp(argv[2], "list")==0) {
  404. pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS];
  405. unsigned i, cnt = PJ_ARRAY_SIZE(wids);
  406. pjsua_vid_enum_wins(wids, &cnt);
  407. PJ_LOG(3,(THIS_FILE, "Found %d video windows:", cnt));
  408. PJ_LOG(3,(THIS_FILE, "WID show pos size"));
  409. PJ_LOG(3,(THIS_FILE, "------------------------------"));
  410. for (i = 0; i < cnt; ++i) {
  411. pjsua_vid_win_info wi;
  412. pjsua_vid_win_get_info(wids[i], &wi);
  413. PJ_LOG(3,(THIS_FILE, "%3d %c (%d,%d) %dx%d",
  414. wids[i], (wi.show?'Y':'N'), wi.pos.x, wi.pos.y,
  415. wi.size.w, wi.size.h));
  416. }
  417. } else if (argc==4 && (strcmp(argv[2], "show")==0 ||
  418. strcmp(argv[2], "hide")==0))
  419. {
  420. pj_bool_t show = (strcmp(argv[2], "show")==0);
  421. pjsua_vid_win_id wid = atoi(argv[3]);
  422. status = pjsua_vid_win_set_show(wid, show);
  423. } else if (argc==6 && strcmp(argv[2], "move")==0) {
  424. pjsua_vid_win_id wid = atoi(argv[3]);
  425. pjmedia_coord pos;
  426. pos.x = atoi(argv[4]);
  427. pos.y = atoi(argv[5]);
  428. status = pjsua_vid_win_set_pos(wid, &pos);
  429. } else if (argc==6 && strcmp(argv[2], "resize")==0) {
  430. pjsua_vid_win_id wid = atoi(argv[3]);
  431. pjmedia_rect_size size;
  432. size.w = atoi(argv[4]);
  433. size.h = atoi(argv[5]);
  434. status = pjsua_vid_win_set_size(wid, &size);
  435. } else if (argc==3 && strcmp(argv[2], "arrange")==0) {
  436. arrange_window(PJSUA_INVALID_ID);
  437. } else if (argc==5 && (strcmp(argv[2], "full")==0))
  438. {
  439. pjsua_vid_win_id wid = atoi(argv[4]);
  440. pjmedia_vid_dev_fullscreen_flag mode = PJMEDIA_VID_DEV_WINDOWED;
  441. if (strcmp(argv[3], "on")==0)
  442. mode = PJMEDIA_VID_DEV_FULLSCREEN;
  443. else if (strcmp(argv[3], "dt")==0)
  444. mode = PJMEDIA_VID_DEV_FULLSCREEN_DESKTOP;
  445. status = pjsua_vid_win_set_fullscreen(wid, mode);
  446. } else
  447. goto on_error;
  448. if (status != PJ_SUCCESS) {
  449. PJ_PERROR(1,(THIS_FILE, status, "Window operation error"));
  450. }
  451. } else if (strcmp(argv[1], "codec")==0) {
  452. pjsua_codec_info ci[PJMEDIA_CODEC_MGR_MAX_CODECS];
  453. unsigned count = PJ_ARRAY_SIZE(ci);
  454. pj_status_t status;
  455. if (argc==3 && strcmp(argv[2], "list")==0) {
  456. status = pjsua_vid_enum_codecs(ci, &count);
  457. if (status != PJ_SUCCESS) {
  458. PJ_PERROR(1,(THIS_FILE, status, "Error enumerating codecs"));
  459. } else {
  460. unsigned i;
  461. PJ_LOG(3,(THIS_FILE, "Found %d video codecs:", count));
  462. PJ_LOG(3,(THIS_FILE, "codec id prio fps bw(kbps) size"));
  463. PJ_LOG(3,(THIS_FILE, "------------------------------------------"));
  464. for (i=0; i<count; ++i) {
  465. pjmedia_vid_codec_param cp;
  466. pjmedia_video_format_detail *vfd;
  467. status = pjsua_vid_codec_get_param(&ci[i].codec_id, &cp);
  468. if (status != PJ_SUCCESS)
  469. continue;
  470. vfd = pjmedia_format_get_video_format_detail(&cp.enc_fmt,
  471. PJ_TRUE);
  472. PJ_LOG(3,(THIS_FILE, "%.*s%.*s %3d %7.2f %4d/%4d %dx%d",
  473. (int)ci[i].codec_id.slen, ci[i].codec_id.ptr,
  474. 13-(int)ci[i].codec_id.slen, " ",
  475. ci[i].priority,
  476. (vfd->fps.num*1.0/vfd->fps.denum),
  477. vfd->avg_bps/1000, vfd->max_bps/1000,
  478. vfd->size.w, vfd->size.h));
  479. }
  480. }
  481. } else if (argc==5 && strcmp(argv[2], "prio")==0) {
  482. pj_str_t cid;
  483. int prio;
  484. cid = pj_str(argv[3]);
  485. prio = atoi(argv[4]);
  486. status = pjsua_vid_codec_set_priority(&cid, (pj_uint8_t)prio);
  487. if (status != PJ_SUCCESS)
  488. PJ_PERROR(1,(THIS_FILE, status, "Set codec priority error"));
  489. } else if (argc==6 && strcmp(argv[2], "fps")==0) {
  490. pjmedia_vid_codec_param cp;
  491. pj_str_t cid;
  492. int M, N;
  493. cid = pj_str(argv[3]);
  494. M = atoi(argv[4]);
  495. N = atoi(argv[5]);
  496. status = pjsua_vid_codec_get_param(&cid, &cp);
  497. if (status == PJ_SUCCESS) {
  498. cp.enc_fmt.det.vid.fps.num = M;
  499. cp.enc_fmt.det.vid.fps.denum = N;
  500. status = pjsua_vid_codec_set_param(&cid, &cp);
  501. }
  502. if (status != PJ_SUCCESS)
  503. PJ_PERROR(1,(THIS_FILE, status, "Set codec framerate error"));
  504. } else if (argc==6 && strcmp(argv[2], "bw")==0) {
  505. pjmedia_vid_codec_param cp;
  506. pj_str_t cid;
  507. int M, N;
  508. cid = pj_str(argv[3]);
  509. M = atoi(argv[4]);
  510. N = atoi(argv[5]);
  511. status = pjsua_vid_codec_get_param(&cid, &cp);
  512. if (status == PJ_SUCCESS) {
  513. cp.enc_fmt.det.vid.avg_bps = M * 1000;
  514. cp.enc_fmt.det.vid.max_bps = N * 1000;
  515. status = pjsua_vid_codec_set_param(&cid, &cp);
  516. }
  517. if (status != PJ_SUCCESS)
  518. PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error"));
  519. } else if (argc==6 && strcmp(argv[2], "size")==0) {
  520. pjmedia_vid_codec_param cp;
  521. pj_str_t cid;
  522. int M, N;
  523. cid = pj_str(argv[3]);
  524. M = atoi(argv[4]);
  525. N = atoi(argv[5]);
  526. status = pjsua_vid_codec_get_param(&cid, &cp);
  527. if (status == PJ_SUCCESS) {
  528. cp.enc_fmt.det.vid.size.w = M;
  529. cp.enc_fmt.det.vid.size.h = N;
  530. status = pjsua_vid_codec_set_param(&cid, &cp);
  531. }
  532. if (status != PJ_SUCCESS)
  533. PJ_PERROR(1,(THIS_FILE, status, "Set codec size error"));
  534. } else
  535. goto on_error;
  536. } else if (strcmp(argv[1], "conf")==0) {
  537. pj_status_t status;
  538. if (argc==3 && strcmp(argv[2], "list")==0) {
  539. pjsua_conf_port_id id[100];
  540. unsigned count = PJ_ARRAY_SIZE(id);
  541. status = pjsua_vid_conf_enum_ports(id, &count);
  542. if (status != PJ_SUCCESS) {
  543. PJ_PERROR(1,(THIS_FILE, status,
  544. "Failed enumerating video conf bridge ports"));
  545. } else {
  546. unsigned i;
  547. printf(" Video conference has %d ports:\n", count);
  548. printf(" id name format rx-from tx-to \n");
  549. printf(" ------------------------------------------------------------------\n");
  550. for (i=0; i<count; ++i) {
  551. char li_list[PJSUA_MAX_CALLS*4];
  552. char tr_list[PJSUA_MAX_CALLS*4];
  553. char s[32];
  554. unsigned j;
  555. pjsua_vid_conf_port_info info;
  556. pjmedia_rect_size *size;
  557. pjmedia_ratio *fps;
  558. pjsua_vid_conf_get_port_info(id[i], &info);
  559. size = &info.format.det.vid.size;
  560. fps = &info.format.det.vid.fps;
  561. li_list[0] = '\0';
  562. for (j=0; j<info.listener_cnt; ++j) {
  563. char str_info[10];
  564. pj_ansi_snprintf(str_info, sizeof(str_info), "%d%s",
  565. info.listeners[j],
  566. (j==info.listener_cnt-1)?"":",");
  567. pj_ansi_strxcat(li_list, str_info, sizeof(li_list));
  568. }
  569. tr_list[0] = '\0';
  570. for (j=0; j<info.transmitter_cnt; ++j) {
  571. char str_info[10];
  572. pj_ansi_snprintf(str_info, sizeof(str_info), "%d%s",
  573. info.transmitters[j],
  574. (j==info.transmitter_cnt-1)?"":",");
  575. pj_ansi_strxcat(tr_list, str_info, sizeof(tr_list));
  576. }
  577. pjmedia_fourcc_name(info.format.id, s);
  578. s[4] = ' ';
  579. pj_ansi_snprintf(s+5, sizeof(s)-5, "%dx%d@%.1f",
  580. size->w, size->h,
  581. (float)(fps->num*1.0/fps->denum));
  582. printf("%3d %.*s%.*s %s%.*s %s%.*s %s\n",
  583. id[i],
  584. (int)info.name.slen, info.name.ptr,
  585. 22-(int)info.name.slen, " ",
  586. s,
  587. 20-(int)pj_ansi_strlen(s), " ",
  588. tr_list,
  589. 12-(int)pj_ansi_strlen(tr_list), " ",
  590. li_list);
  591. }
  592. }
  593. } else if (argc==5 && strcmp(argv[2], "cc")==0) {
  594. int P, Q;
  595. P = atoi(argv[3]);
  596. Q = atoi(argv[4]);
  597. pjsua_vid_conf_connect(P, Q, NULL);
  598. } else if (argc==5 && strcmp(argv[2], "cd")==0) {
  599. int P, Q;
  600. P = atoi(argv[3]);
  601. Q = atoi(argv[4]);
  602. pjsua_vid_conf_disconnect(P, Q);
  603. } else {
  604. goto on_error;
  605. }
  606. } else
  607. goto on_error;
  608. return;
  609. on_error:
  610. PJ_LOG(1,(THIS_FILE, "Invalid command, use 'vid help'"));
  611. }
  612. #endif /* PJSUA_HAS_VIDEO */
  613. /** UI Command **/
  614. static void ui_make_new_call()
  615. {
  616. char buf[128];
  617. pjsua_msg_data msg_data_;
  618. input_result result;
  619. pj_str_t tmp;
  620. pj_bool_t loop = PJ_FALSE;
  621. printf("(You currently have %d calls)\n", pjsua_call_get_count());
  622. ui_input_url("Make call", buf, sizeof(buf), &result, PJ_TRUE);
  623. do {
  624. if (result.nb_result != PJSUA_APP_NO_NB) {
  625. if (result.nb_result == -1) {
  626. loop = PJ_TRUE;
  627. result.nb_result = 1;
  628. }
  629. if (result.nb_result > (int)pjsua_get_buddy_count()) break;
  630. if (result.nb_result == 0) {
  631. puts("You can't do that with make call!");
  632. return;
  633. } else {
  634. pjsua_buddy_info binfo;
  635. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  636. tmp.ptr = buf;
  637. pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
  638. }
  639. } else if (result.uri_result) {
  640. tmp = pj_str(result.uri_result);
  641. } else {
  642. tmp.slen = 0;
  643. }
  644. pjsua_msg_data_init(&msg_data_);
  645. TEST_MULTIPART(&msg_data_);
  646. if (app_config.enable_loam) {
  647. call_opt.flag |= PJSUA_CALL_NO_SDP_OFFER;
  648. }
  649. pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL,
  650. &msg_data_, &current_call);
  651. result.nb_result++;
  652. } while (loop);
  653. }
  654. static void ui_make_multi_call()
  655. {
  656. char menuin[32];
  657. int count;
  658. char buf[128];
  659. input_result result;
  660. pj_str_t tmp;
  661. int i;
  662. printf("(You currently have %d calls)\n", pjsua_call_get_count());
  663. if (!simple_input("Number of calls", menuin, sizeof(menuin)))
  664. return;
  665. count = my_atoi(menuin);
  666. if (count < 1)
  667. return;
  668. ui_input_url("Make call", buf, sizeof(buf), &result, PJ_FALSE);
  669. if (result.nb_result != PJSUA_APP_NO_NB) {
  670. pjsua_buddy_info binfo;
  671. if (result.nb_result == -1 || result.nb_result == 0) {
  672. puts("You can't do that with make call!");
  673. return;
  674. }
  675. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  676. tmp.ptr = buf;
  677. pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
  678. } else {
  679. tmp = pj_str(result.uri_result);
  680. }
  681. if (app_config.enable_loam) {
  682. call_opt.flag |= PJSUA_CALL_NO_SDP_OFFER;
  683. }
  684. for (i=0; i<my_atoi(menuin); ++i) {
  685. pj_status_t status;
  686. status = pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL,
  687. NULL, NULL);
  688. if (status != PJ_SUCCESS)
  689. break;
  690. }
  691. }
  692. static void ui_detect_nat_type()
  693. {
  694. int i = pjsua_detect_nat_type();
  695. if (i != PJ_SUCCESS)
  696. pjsua_perror(THIS_FILE, "Error", i);
  697. }
  698. static void ui_send_instant_message()
  699. {
  700. char *uri = NULL;
  701. /* i is for call index to send message, if any */
  702. int i = -1;
  703. input_result result;
  704. char buf[128];
  705. char text[128];
  706. pj_str_t tmp;
  707. /* Input destination. */
  708. ui_input_url("Send IM to", buf, sizeof(buf), &result, PJ_FALSE);
  709. if (result.nb_result != PJSUA_APP_NO_NB) {
  710. if (result.nb_result == -1) {
  711. puts("You can't send broadcast IM like that!");
  712. return;
  713. } else if (result.nb_result == 0) {
  714. i = current_call;
  715. } else {
  716. pjsua_buddy_info binfo;
  717. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  718. tmp.ptr = buf;
  719. pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
  720. uri = buf;
  721. }
  722. } else if (result.uri_result) {
  723. uri = result.uri_result;
  724. }
  725. /* Send typing indication. */
  726. if (!app_config.no_mci) {
  727. if (i != -1)
  728. pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
  729. else {
  730. pj_str_t tmp_uri = pj_str(uri);
  731. pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
  732. }
  733. }
  734. /* Input the IM . */
  735. if (!simple_input("Message", text, sizeof(text))) {
  736. if (!app_config.no_mci) {
  737. /*
  738. * Cancelled.
  739. * Send typing notification too, saying we're not typing.
  740. */
  741. if (i != -1)
  742. pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
  743. else {
  744. pj_str_t tmp_uri = pj_str(uri);
  745. pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
  746. }
  747. return;
  748. }
  749. }
  750. tmp = pj_str(text);
  751. /* Send the IM */
  752. if (i != -1)
  753. pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
  754. else {
  755. pj_str_t tmp_uri = pj_str(uri);
  756. pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
  757. }
  758. }
  759. static void ui_answer_call()
  760. {
  761. pjsua_call_info call_info;
  762. char buf[128];
  763. pjsua_msg_data msg_data_;
  764. if (current_call != -1) {
  765. pjsua_call_get_info(current_call, &call_info);
  766. } else {
  767. /* Make compiler happy */
  768. call_info.role = PJSIP_ROLE_UAC;
  769. call_info.state = PJSIP_INV_STATE_DISCONNECTED;
  770. }
  771. if (current_call == -1 ||
  772. call_info.role != PJSIP_ROLE_UAS ||
  773. call_info.state >= PJSIP_INV_STATE_CONNECTING)
  774. {
  775. puts("No pending incoming call");
  776. fflush(stdout);
  777. return;
  778. } else {
  779. int st_code;
  780. char contact[120];
  781. pj_str_t hname = { "Contact", 7 };
  782. pj_str_t hvalue;
  783. pjsip_generic_string_hdr hcontact;
  784. if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
  785. return;
  786. st_code = my_atoi(buf);
  787. if (st_code < 100)
  788. return;
  789. pjsua_msg_data_init(&msg_data_);
  790. if (st_code/100 == 3) {
  791. if (!simple_input("Enter URL to be put in Contact",
  792. contact, sizeof(contact)))
  793. return;
  794. hvalue = pj_str(contact);
  795. pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
  796. pj_list_push_back(&msg_data_.hdr_list, &hcontact);
  797. }
  798. /*
  799. * Must check again!
  800. * Call may have been disconnected while we're waiting for
  801. * keyboard input.
  802. */
  803. if (current_call == -1) {
  804. puts("Call has been disconnected");
  805. fflush(stdout);
  806. return;
  807. }
  808. pjsua_call_answer2(current_call, &call_opt, st_code, NULL, &msg_data_);
  809. }
  810. }
  811. static void ui_hangup_call(char menuin[])
  812. {
  813. if (current_call == -1) {
  814. puts("No current call");
  815. fflush(stdout);
  816. return;
  817. } else if (menuin[1] == 'a') {
  818. /* Hangup all calls */
  819. pjsua_call_hangup_all();
  820. } else {
  821. /* Hangup current calls */
  822. pjsua_call_hangup(current_call, 0, NULL, NULL);
  823. }
  824. }
  825. static void ui_cycle_dialog(char menuin[])
  826. {
  827. if (menuin[0] == ']') {
  828. find_next_call();
  829. } else {
  830. find_prev_call();
  831. }
  832. if (current_call != -1) {
  833. pjsua_call_info call_info;
  834. pjsua_call_get_info(current_call, &call_info);
  835. PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
  836. (int)call_info.remote_info.slen,
  837. call_info.remote_info.ptr));
  838. } else {
  839. PJ_LOG(3,(THIS_FILE,"No current dialog"));
  840. }
  841. }
  842. static void ui_cycle_account()
  843. {
  844. int i;
  845. char buf[128];
  846. if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
  847. return;
  848. i = my_atoi(buf);
  849. if (pjsua_acc_is_valid(i)) {
  850. pjsua_acc_set_default(i);
  851. PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
  852. } else {
  853. PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
  854. }
  855. }
  856. static void ui_add_buddy()
  857. {
  858. char buf[128];
  859. pjsua_buddy_config buddy_cfg;
  860. pjsua_buddy_id buddy_id;
  861. pj_status_t status;
  862. if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
  863. return;
  864. if (pjsua_verify_url(buf) != PJ_SUCCESS) {
  865. printf("Invalid URI '%s'\n", buf);
  866. return;
  867. }
  868. pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
  869. buddy_cfg.uri = pj_str(buf);
  870. /* Only one subscription can be active, so we need to disable this
  871. * to allow user to choose between presence or dialog event.
  872. */
  873. // buddy_cfg.subscribe = PJ_TRUE;
  874. status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
  875. if (status == PJ_SUCCESS) {
  876. printf("New buddy '%s' added at index %d\n",
  877. buf, buddy_id+1);
  878. }
  879. }
  880. static void ui_add_account(pjsua_transport_config *rtp_cfg)
  881. {
  882. char id[80], registrar[80], realm[80], uname[80], passwd[30];
  883. pjsua_acc_config acc_cfg;
  884. pj_status_t status;
  885. if (!simple_input("Your SIP URL:", id, sizeof(id)))
  886. return;
  887. if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
  888. return;
  889. if (!simple_input("Auth Realm:", realm, sizeof(realm)))
  890. return;
  891. if (!simple_input("Auth Username:", uname, sizeof(uname)))
  892. return;
  893. if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
  894. return;
  895. pjsua_acc_config_default(&acc_cfg);
  896. acc_cfg.id = pj_str(id);
  897. acc_cfg.reg_uri = pj_str(registrar);
  898. acc_cfg.cred_count = 1;
  899. acc_cfg.cred_info[0].scheme = pjsip_DIGEST_STR;
  900. acc_cfg.cred_info[0].realm = pj_str(realm);
  901. acc_cfg.cred_info[0].username = pj_str(uname);
  902. acc_cfg.cred_info[0].data_type = 0;
  903. acc_cfg.cred_info[0].data = pj_str(passwd);
  904. acc_cfg.rtp_cfg = *rtp_cfg;
  905. app_config_init_video(&acc_cfg);
  906. status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
  907. if (status != PJ_SUCCESS) {
  908. pjsua_perror(THIS_FILE, "Error adding new account", status);
  909. }
  910. }
  911. static void ui_delete_buddy()
  912. {
  913. char buf[128];
  914. int i;
  915. if (!simple_input("Enter buddy ID to delete", buf, sizeof(buf)))
  916. return;
  917. i = my_atoi(buf) - 1;
  918. if (!pjsua_buddy_is_valid(i)) {
  919. printf("Invalid buddy id %d\n", i);
  920. } else {
  921. pjsua_buddy_del(i);
  922. printf("Buddy %d deleted\n", i);
  923. }
  924. }
  925. static void ui_delete_account()
  926. {
  927. char buf[128];
  928. int i;
  929. if (!simple_input("Enter account ID to delete", buf, sizeof(buf)))
  930. return;
  931. i = my_atoi(buf);
  932. if (!pjsua_acc_is_valid(i)) {
  933. printf("Invalid account id %d\n", i);
  934. } else {
  935. pjsua_acc_del(i);
  936. printf("Account %d deleted\n", i);
  937. }
  938. }
  939. static void ui_unset_loam_mode()
  940. {
  941. app_config.enable_loam = PJ_FALSE;
  942. }
  943. static void ui_call_hold()
  944. {
  945. if (current_call != -1) {
  946. pjsua_call_set_hold(current_call, NULL);
  947. } else {
  948. PJ_LOG(3,(THIS_FILE, "No current call"));
  949. }
  950. }
  951. static void ui_call_reinvite()
  952. {
  953. call_opt.flag |= PJSUA_CALL_UNHOLD;
  954. pjsua_call_reinvite2(current_call, &call_opt, NULL);
  955. }
  956. static void ui_send_update()
  957. {
  958. if (current_call != -1) {
  959. if (app_config.enable_loam) {
  960. call_opt.flag |= PJSUA_CALL_NO_SDP_OFFER;
  961. }
  962. pjsua_call_update2(current_call, &call_opt, NULL);
  963. } else {
  964. PJ_LOG(3,(THIS_FILE, "No current call"));
  965. }
  966. }
  967. /*
  968. * Change codec priorities.
  969. */
  970. static void ui_manage_codec_prio()
  971. {
  972. pjsua_codec_info c[32];
  973. unsigned i, count = PJ_ARRAY_SIZE(c);
  974. char input[32];
  975. char *codec, *prio;
  976. pj_str_t id;
  977. int new_prio;
  978. pj_status_t status;
  979. printf("List of audio codecs:\n");
  980. pjsua_enum_codecs(c, &count);
  981. for (i=0; i<count; ++i) {
  982. printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
  983. c[i].codec_id.ptr);
  984. }
  985. #if PJSUA_HAS_VIDEO
  986. puts("");
  987. printf("List of video codecs:\n");
  988. pjsua_vid_enum_codecs(c, &count);
  989. for (i=0; i<count; ++i) {
  990. printf(" %d\t%.*s%s%.*s\n", c[i].priority,
  991. (int)c[i].codec_id.slen,
  992. c[i].codec_id.ptr,
  993. c[i].desc.slen? " - ":"",
  994. (int)c[i].desc.slen,
  995. c[i].desc.ptr);
  996. }
  997. #endif
  998. puts("");
  999. puts("Enter codec id and its new priority (e.g. \"speex/16000 200\", "
  1000. """\"H263 200\"),");
  1001. puts("or empty to cancel.");
  1002. printf("Codec name (\"*\" for all) and priority: ");
  1003. if (fgets(input, sizeof(input), stdin) == NULL)
  1004. return;
  1005. if (input[0]=='\r' || input[0]=='\n') {
  1006. puts("Done");
  1007. return;
  1008. }
  1009. codec = strtok(input, " \t\r\n");
  1010. prio = strtok(NULL, " \r\n");
  1011. if (!codec || !prio) {
  1012. puts("Invalid input");
  1013. return;
  1014. }
  1015. new_prio = atoi(prio);
  1016. if (new_prio < 0)
  1017. new_prio = 0;
  1018. else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
  1019. new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
  1020. status = pjsua_codec_set_priority(pj_cstr(&id, codec),
  1021. (pj_uint8_t)new_prio);
  1022. #if PJSUA_HAS_VIDEO
  1023. if (status != PJ_SUCCESS) {
  1024. status = pjsua_vid_codec_set_priority(pj_cstr(&id, codec),
  1025. (pj_uint8_t)new_prio);
  1026. }
  1027. #endif
  1028. if (status != PJ_SUCCESS)
  1029. pjsua_perror(THIS_FILE, "Error setting codec priority", status);
  1030. }
  1031. static void ui_call_transfer(pj_bool_t no_refersub)
  1032. {
  1033. if (current_call == -1) {
  1034. PJ_LOG(3,(THIS_FILE, "No current call"));
  1035. } else {
  1036. int call = current_call;
  1037. char buf[128];
  1038. pjsip_generic_string_hdr refer_sub;
  1039. pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
  1040. pj_str_t STR_FALSE = { "false", 5 };
  1041. pjsua_call_info ci;
  1042. input_result result;
  1043. pjsua_msg_data msg_data_;
  1044. pjsua_call_get_info(current_call, &ci);
  1045. printf("Transferring current call [%d] %.*s\n", current_call,
  1046. (int)ci.remote_info.slen, ci.remote_info.ptr);
  1047. ui_input_url("Transfer to URL", buf, sizeof(buf), &result, PJ_FALSE);
  1048. /* Check if call is still there. */
  1049. if (call != current_call) {
  1050. puts("Call has been disconnected");
  1051. return;
  1052. }
  1053. pjsua_msg_data_init(&msg_data_);
  1054. if (no_refersub) {
  1055. /* Add Refer-Sub: false in outgoing REFER request */
  1056. pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
  1057. &STR_FALSE);
  1058. pj_list_push_back(&msg_data_.hdr_list, &refer_sub);
  1059. }
  1060. if (result.nb_result != PJSUA_APP_NO_NB) {
  1061. if (result.nb_result == -1 || result.nb_result == 0) {
  1062. puts("You can't do that with transfer call!");
  1063. } else {
  1064. pjsua_buddy_info binfo;
  1065. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  1066. pjsua_call_xfer( current_call, &binfo.uri, &msg_data_);
  1067. }
  1068. } else if (result.uri_result) {
  1069. pj_str_t tmp;
  1070. tmp = pj_str(result.uri_result);
  1071. pjsua_call_xfer( current_call, &tmp, &msg_data_);
  1072. }
  1073. }
  1074. }
  1075. static void ui_call_transfer_replaces(pj_bool_t no_refersub)
  1076. {
  1077. if (current_call == -1) {
  1078. PJ_LOG(3,(THIS_FILE, "No current call"));
  1079. } else {
  1080. int call = current_call;
  1081. int dst_call;
  1082. pjsip_generic_string_hdr refer_sub;
  1083. pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
  1084. pj_str_t STR_FALSE = { "false", 5 };
  1085. pjsua_call_id ids[PJSUA_MAX_CALLS];
  1086. pjsua_call_info ci;
  1087. pjsua_msg_data msg_data_;
  1088. char buf[128];
  1089. unsigned i, count;
  1090. count = PJ_ARRAY_SIZE(ids);
  1091. pjsua_enum_calls(ids, &count);
  1092. if (count <= 1) {
  1093. puts("There are no other calls");
  1094. return;
  1095. }
  1096. pjsua_call_get_info(current_call, &ci);
  1097. printf("Transfer call [%d] %.*s to one of the following:\n",
  1098. current_call,
  1099. (int)ci.remote_info.slen, ci.remote_info.ptr);
  1100. for (i=0; i<count; ++i) {
  1101. pjsua_call_info call_info;
  1102. if (ids[i] == call)
  1103. continue;
  1104. pjsua_call_get_info(ids[i], &call_info);
  1105. printf("%d %.*s [%.*s]\n",
  1106. ids[i],
  1107. (int)call_info.remote_info.slen,
  1108. call_info.remote_info.ptr,
  1109. (int)call_info.state_text.slen,
  1110. call_info.state_text.ptr);
  1111. }
  1112. if (!simple_input("Enter call number to be replaced", buf, sizeof(buf)))
  1113. return;
  1114. dst_call = my_atoi(buf);
  1115. /* Check if call is still there. */
  1116. if (call != current_call) {
  1117. puts("Call has been disconnected");
  1118. return;
  1119. }
  1120. /* Check that destination call is valid. */
  1121. if (dst_call == call) {
  1122. puts("Destination call number must not be the same "
  1123. "as the call being transferred");
  1124. return;
  1125. }
  1126. if (dst_call >= PJSUA_MAX_CALLS) {
  1127. puts("Invalid destination call number");
  1128. return;
  1129. }
  1130. if (!pjsua_call_is_active(dst_call)) {
  1131. puts("Invalid destination call number");
  1132. return;
  1133. }
  1134. pjsua_msg_data_init(&msg_data_);
  1135. if (no_refersub) {
  1136. /* Add Refer-Sub: false in outgoing REFER request */
  1137. pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
  1138. &STR_FALSE);
  1139. pj_list_push_back(&msg_data_.hdr_list, &refer_sub);
  1140. }
  1141. pjsua_call_xfer_replaces(call, dst_call,
  1142. PJSUA_XFER_NO_REQUIRE_REPLACES,
  1143. &msg_data_);
  1144. }
  1145. }
  1146. static void ui_send_dtmf_2833()
  1147. {
  1148. if (current_call == -1) {
  1149. PJ_LOG(3,(THIS_FILE, "No current call"));
  1150. } else if (!pjsua_call_has_media(current_call)) {
  1151. PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
  1152. } else {
  1153. pj_str_t digits;
  1154. int call = current_call;
  1155. pj_status_t status;
  1156. char buf[128];
  1157. #if defined(PJMEDIA_HAS_DTMF_FLASH) && PJMEDIA_HAS_DTMF_FLASH!= 0
  1158. if (!simple_input("DTMF strings to send (0-9*R#A-B)", buf,
  1159. sizeof(buf)))
  1160. #else
  1161. if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
  1162. sizeof(buf)))
  1163. #endif
  1164. {
  1165. return;
  1166. }
  1167. if (call != current_call) {
  1168. puts("Call has been disconnected");
  1169. return;
  1170. }
  1171. digits = pj_str(buf);
  1172. status = pjsua_call_dial_dtmf(current_call, &digits);
  1173. if (status != PJ_SUCCESS) {
  1174. pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
  1175. } else {
  1176. puts("DTMF digits enqueued for transmission");
  1177. }
  1178. }
  1179. }
  1180. static void ui_send_dtmf_info()
  1181. {
  1182. if (current_call == -1) {
  1183. PJ_LOG(3,(THIS_FILE, "No current call"));
  1184. } else {
  1185. int call = current_call;
  1186. pj_status_t status;
  1187. char buf[128];
  1188. pjsua_call_send_dtmf_param param;
  1189. if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
  1190. sizeof(buf)))
  1191. {
  1192. return;
  1193. }
  1194. if (call != current_call) {
  1195. puts("Call has been disconnected");
  1196. return;
  1197. }
  1198. pjsua_call_send_dtmf_param_default(&param);
  1199. param.digits = pj_str(buf);
  1200. param.method = PJSUA_DTMF_METHOD_SIP_INFO;
  1201. status = pjsua_call_send_dtmf(current_call, &param);
  1202. if (status != PJ_SUCCESS) {
  1203. pjsua_perror(THIS_FILE, "Error sending DTMF", status);
  1204. }
  1205. }
  1206. }
  1207. static void ui_send_arbitrary_request()
  1208. {
  1209. char text[128];
  1210. char buf[128];
  1211. char *uri;
  1212. input_result result;
  1213. pj_str_t tmp;
  1214. if (pjsua_acc_get_count() == 0) {
  1215. puts("Sorry, need at least one account configured");
  1216. return;
  1217. }
  1218. puts("Send arbitrary request to remote host");
  1219. /* Input METHOD */
  1220. if (!simple_input("Request method:",text,sizeof(text)))
  1221. return;
  1222. /* Input destination URI */
  1223. uri = NULL;
  1224. ui_input_url("Destination URI", buf, sizeof(buf), &result, PJ_FALSE);
  1225. if (result.nb_result != PJSUA_APP_NO_NB) {
  1226. if (result.nb_result == -1) {
  1227. puts("Sorry you can't do that!");
  1228. return;
  1229. } else if (result.nb_result == 0) {
  1230. uri = NULL;
  1231. if (current_call == PJSUA_INVALID_ID) {
  1232. puts("No current call");
  1233. return;
  1234. }
  1235. } else {
  1236. pjsua_buddy_info binfo;
  1237. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  1238. tmp.ptr = buf;
  1239. pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
  1240. uri = buf;
  1241. }
  1242. } else if (result.uri_result) {
  1243. uri = result.uri_result;
  1244. } else {
  1245. return;
  1246. }
  1247. if (uri) {
  1248. tmp = pj_str(uri);
  1249. send_request(text, &tmp);
  1250. } else {
  1251. /* If you send call control request using this method
  1252. * (such requests includes BYE, CANCEL, etc.), it will
  1253. * not go well with the call state, so don't do it
  1254. * unless it's for testing.
  1255. */
  1256. pj_str_t method = pj_str(text);
  1257. pjsua_call_send_request(current_call, &method, NULL);
  1258. }
  1259. }
  1260. static void ui_toggle_call_sdp_offer()
  1261. {
  1262. app_config.enable_loam = !app_config.enable_loam;
  1263. printf("Subsequent calls and UPDATEs will contain SDP offer: ");
  1264. if (app_config.enable_loam) {
  1265. printf("NO.\n");
  1266. } else {
  1267. printf("YES.\n");
  1268. }
  1269. }
  1270. static void ui_echo(char menuin[])
  1271. {
  1272. if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
  1273. pj_str_t tmp;
  1274. tmp.ptr = menuin+5;
  1275. tmp.slen = pj_ansi_strlen(menuin)-6;
  1276. if (tmp.slen < 1) {
  1277. puts("Usage: echo [0|1]");
  1278. return;
  1279. }
  1280. cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
  1281. }
  1282. }
  1283. static void ui_sleep(char menuin[])
  1284. {
  1285. if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
  1286. pj_str_t tmp;
  1287. int delay;
  1288. tmp.ptr = menuin+6;
  1289. tmp.slen = pj_ansi_strlen(menuin)-7;
  1290. if (tmp.slen < 1) {
  1291. puts("Usage: sleep MSEC");
  1292. return;
  1293. }
  1294. delay = (int)pj_strtoul(&tmp);
  1295. if (delay < 0) delay = 0;
  1296. pj_thread_sleep(delay);
  1297. }
  1298. }
  1299. static void ui_subscribe(char menuin[])
  1300. {
  1301. char buf[128];
  1302. input_result result;
  1303. ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result,
  1304. PJ_TRUE);
  1305. if (result.nb_result != PJSUA_APP_NO_NB) {
  1306. if (result.nb_result == -1) {
  1307. int i, count;
  1308. count = pjsua_get_buddy_count();
  1309. for (i=0; i<count; ++i)
  1310. pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
  1311. } else if (result.nb_result == 0) {
  1312. puts("Sorry, can only subscribe to buddy's presence, "
  1313. "not from existing call");
  1314. } else {
  1315. pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
  1316. }
  1317. } else if (result.uri_result) {
  1318. puts("Sorry, can only subscribe to buddy's presence, "
  1319. "not arbitrary URL (for now)");
  1320. }
  1321. }
  1322. static void ui_subscribe_dlg_event(pj_bool_t sub)
  1323. {
  1324. char buf[128];
  1325. input_result result;
  1326. ui_input_url("(un)Subscribe dialog event of", buf, sizeof(buf), &result,
  1327. PJ_TRUE);
  1328. if (result.nb_result != PJSUA_APP_NO_NB) {
  1329. if (result.nb_result == -1) {
  1330. int i, count;
  1331. count = pjsua_get_buddy_count();
  1332. for (i=0; i<count; ++i)
  1333. pjsua_buddy_subscribe_dlg_event(i, sub);
  1334. } else if (result.nb_result == 0) {
  1335. puts("Sorry, can only subscribe to buddy's dialog event, "
  1336. "not from existing call");
  1337. } else {
  1338. pjsua_buddy_subscribe_dlg_event(result.nb_result-1, sub);
  1339. }
  1340. } else if (result.uri_result) {
  1341. puts("Sorry, can only subscribe to buddy's dialog event, "
  1342. "not arbitrary URL (for now)");
  1343. }
  1344. }
  1345. static void ui_register(char menuin[])
  1346. {
  1347. switch (menuin[1]) {
  1348. case 'r':
  1349. /*
  1350. * Re-Register.
  1351. */
  1352. pjsua_acc_set_registration(current_acc, PJ_TRUE);
  1353. break;
  1354. case 'u':
  1355. /*
  1356. * Unregister
  1357. */
  1358. pjsua_acc_set_registration(current_acc, PJ_FALSE);
  1359. break;
  1360. }
  1361. }
  1362. static void ui_toggle_state()
  1363. {
  1364. pjsua_acc_info acc_info;
  1365. pjsua_acc_get_info(current_acc, &acc_info);
  1366. acc_info.online_status = !acc_info.online_status;
  1367. pjsua_acc_set_online_status(current_acc, acc_info.online_status);
  1368. printf("Setting %s online status to %s\n",
  1369. acc_info.acc_uri.ptr,
  1370. (acc_info.online_status?"online":"offline"));
  1371. }
  1372. /*
  1373. * Change extended online status.
  1374. */
  1375. static void ui_change_online_status()
  1376. {
  1377. char menuin[32];
  1378. pj_bool_t online_status;
  1379. pjrpid_element elem;
  1380. int choice;
  1381. unsigned i;
  1382. enum {
  1383. AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
  1384. };
  1385. struct opt {
  1386. int id;
  1387. char *name;
  1388. } opts[] = {
  1389. { AVAILABLE, "Available" },
  1390. { BUSY, "Busy"},
  1391. { OTP, "On the phone"},
  1392. { IDLE, "Idle"},
  1393. { AWAY, "Away"},
  1394. { BRB, "Be right back"},
  1395. { OFFLINE, "Offline"}
  1396. };
  1397. printf("\n"
  1398. "Choices:\n");
  1399. for (i=0; i<(unsigned)PJ_ARRAY_SIZE(opts); ++i) {
  1400. printf(" %d %s\n", opts[i].id+1, opts[i].name);
  1401. }
  1402. if (!simple_input("Select status", menuin, sizeof(menuin)))
  1403. return;
  1404. choice = atoi(menuin) - 1;
  1405. if (choice < 0 || choice >= OPT_MAX) {
  1406. puts("Invalid selection");
  1407. return;
  1408. }
  1409. pj_bzero(&elem, sizeof(elem));
  1410. elem.type = PJRPID_ELEMENT_TYPE_PERSON;
  1411. online_status = PJ_TRUE;
  1412. switch (choice) {
  1413. case AVAILABLE:
  1414. break;
  1415. case BUSY:
  1416. elem.activity = PJRPID_ACTIVITY_BUSY;
  1417. elem.note = pj_str("Busy");
  1418. break;
  1419. case OTP:
  1420. elem.activity = PJRPID_ACTIVITY_BUSY;
  1421. elem.note = pj_str("On the phone");
  1422. break;
  1423. case IDLE:
  1424. elem.activity = PJRPID_ACTIVITY_UNKNOWN;
  1425. elem.note = pj_str("Idle");
  1426. break;
  1427. case AWAY:
  1428. elem.activity = PJRPID_ACTIVITY_AWAY;
  1429. elem.note = pj_str("Away");
  1430. break;
  1431. case BRB:
  1432. elem.activity = PJRPID_ACTIVITY_UNKNOWN;
  1433. elem.note = pj_str("Be right back");
  1434. break;
  1435. case OFFLINE:
  1436. online_status = PJ_FALSE;
  1437. break;
  1438. }
  1439. pjsua_acc_set_online_status2(current_acc, online_status, &elem);
  1440. }
  1441. /*
  1442. * List the ports in conference bridge
  1443. */
  1444. static void ui_conf_list()
  1445. {
  1446. unsigned i, count;
  1447. pjsua_conf_port_id id[PJSUA_MAX_CONF_PORTS];
  1448. printf("Conference ports:\n");
  1449. count = PJ_ARRAY_SIZE(id);
  1450. pjsua_enum_conf_ports(id, &count);
  1451. for (i=0; i<count; ++i) {
  1452. char txlist[PJSUA_MAX_CALLS*4+10];
  1453. unsigned j;
  1454. pjsua_conf_port_info info;
  1455. pjsua_conf_get_port_info(id[i], &info);
  1456. txlist[0] = '\0';
  1457. for (j=0; j<info.listener_cnt; ++j) {
  1458. char s[10];
  1459. pj_ansi_snprintf(s, sizeof(s), "#%d ", info.listeners[j]);
  1460. pj_ansi_strxcat(txlist, s, sizeof(txlist));
  1461. }
  1462. printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
  1463. info.slot_id,
  1464. info.clock_rate/1000,
  1465. info.samples_per_frame*1000/info.channel_count/info.clock_rate,
  1466. info.channel_count,
  1467. (int)info.name.slen,
  1468. info.name.ptr,
  1469. txlist);
  1470. }
  1471. puts("");
  1472. }
  1473. static void ui_conf_connect(char menuin[])
  1474. {
  1475. char tmp[10], src_port[10], dst_port[10];
  1476. pj_status_t status;
  1477. int cnt;
  1478. const char *src_title, *dst_title;
  1479. cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
  1480. if (cnt != 3) {
  1481. ui_conf_list();
  1482. src_title = (menuin[1]=='c'? "Connect src port #":
  1483. "Disconnect src port #");
  1484. dst_title = (menuin[1]=='c'? "To dst port #":"From dst port #");
  1485. if (!simple_input(src_title, src_port, sizeof(src_port)))
  1486. return;
  1487. if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
  1488. return;
  1489. }
  1490. if (menuin[1]=='c') {
  1491. status = pjsua_conf_connect(my_atoi(src_port), my_atoi(dst_port));
  1492. } else {
  1493. status = pjsua_conf_disconnect(my_atoi(src_port), my_atoi(dst_port));
  1494. }
  1495. if (status == PJ_SUCCESS) {
  1496. puts("Success");
  1497. } else {
  1498. puts("ERROR!!");
  1499. }
  1500. }
  1501. static void ui_adjust_volume()
  1502. {
  1503. char buf[128];
  1504. char text[128];
  1505. snprintf(buf, sizeof(buf), "Adjust mic level: [%4.1fx] ",
  1506. app_config.mic_level);
  1507. if (simple_input(buf,text,sizeof(text))) {
  1508. char *err;
  1509. app_config.mic_level = (float)strtod(text, &err);
  1510. pjsua_conf_adjust_rx_level(0, app_config.mic_level);
  1511. }
  1512. snprintf(buf, sizeof(buf), "Adjust speaker level: [%4.1fx] ",
  1513. app_config.speaker_level);
  1514. if (simple_input(buf,text,sizeof(text))) {
  1515. char *err;
  1516. app_config.speaker_level = (float)strtod(text, &err);
  1517. pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
  1518. }
  1519. }
  1520. static void ui_dump_call_quality()
  1521. {
  1522. if (current_call != PJSUA_INVALID_ID) {
  1523. log_call_dump(current_call);
  1524. } else {
  1525. PJ_LOG(3,(THIS_FILE, "No current call"));
  1526. }
  1527. }
  1528. static void ui_dump_configuration()
  1529. {
  1530. char settings[2000];
  1531. int len;
  1532. len = write_settings(&app_config, settings, sizeof(settings));
  1533. if (len < 1)
  1534. PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
  1535. else
  1536. PJ_LOG(3,(THIS_FILE, "Dumping configuration (%d bytes):\n%s\n",
  1537. len, settings));
  1538. }
  1539. static void ui_write_settings(const char *filename)
  1540. {
  1541. char settings[2000];
  1542. int len;
  1543. len = write_settings(&app_config, settings, sizeof(settings));
  1544. if (len < 1)
  1545. PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
  1546. else {
  1547. pj_oshandle_t fd;
  1548. pj_status_t status;
  1549. status = pj_file_open(app_config.pool, filename, PJ_O_WRONLY, &fd);
  1550. if (status != PJ_SUCCESS) {
  1551. pjsua_perror(THIS_FILE, "Unable to open file", status);
  1552. } else {
  1553. pj_ssize_t size = len;
  1554. pj_file_write(fd, settings, &size);
  1555. pj_file_close(fd);
  1556. printf("Settings successfully written to '%s'\n", filename);
  1557. }
  1558. }
  1559. }
  1560. /*
  1561. * Dump application states.
  1562. */
  1563. static void ui_app_dump(pj_bool_t detail)
  1564. {
  1565. pjsua_dump(detail);
  1566. }
  1567. static void ui_call_redirect(char menuin[])
  1568. {
  1569. if (current_call == PJSUA_INVALID_ID) {
  1570. PJ_LOG(3,(THIS_FILE, "No current call"));
  1571. } else {
  1572. if (!pjsua_call_is_active(current_call)) {
  1573. PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
  1574. } else if (menuin[1] == 'a') {
  1575. pjsua_call_process_redirect(current_call,
  1576. PJSIP_REDIRECT_ACCEPT_REPLACE);
  1577. } else if (menuin[1] == 'A') {
  1578. pjsua_call_process_redirect(current_call,
  1579. PJSIP_REDIRECT_ACCEPT);
  1580. } else if (menuin[1] == 'r') {
  1581. pjsua_call_process_redirect(current_call,
  1582. PJSIP_REDIRECT_REJECT);
  1583. } else {
  1584. pjsua_call_process_redirect(current_call,
  1585. PJSIP_REDIRECT_STOP);
  1586. }
  1587. }
  1588. }
  1589. static void ui_handle_ip_change()
  1590. {
  1591. pjsua_ip_change_param param;
  1592. pj_status_t status;
  1593. pjsua_ip_change_param_default(&param);
  1594. status = pjsua_handle_ip_change(&param);
  1595. if (status != PJ_SUCCESS) {
  1596. pjsua_perror(THIS_FILE, "IP change failed", status);
  1597. }
  1598. }
  1599. /*
  1600. * Main "user interface" loop.
  1601. */
  1602. void legacy_main(void)
  1603. {
  1604. char menuin[80];
  1605. char buf[128];
  1606. keystroke_help();
  1607. for (;;) {
  1608. printf(">>> ");
  1609. fflush(stdout);
  1610. if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
  1611. /*
  1612. * Be friendly to users who redirect commands into
  1613. * program, when file ends, resume with kbd.
  1614. * If exit is desired end script with q for quit
  1615. */
  1616. /* Reopen stdin/stdout/stderr to /dev/console */
  1617. #if ((defined(PJ_WIN32) && PJ_WIN32!=0) || \
  1618. (defined(PJ_WIN64) && PJ_WIN64!=0)) && \
  1619. (!defined(PJ_WIN32_WINCE) || PJ_WIN32_WINCE==0)
  1620. if (freopen ("CONIN$", "r", stdin) == NULL) {
  1621. #else
  1622. if (1) {
  1623. #endif
  1624. puts("Cannot switch back to console from file redirection");
  1625. menuin[0] = 'q';
  1626. menuin[1] = '\0';
  1627. } else {
  1628. puts("Switched back to console from file redirection");
  1629. continue;
  1630. }
  1631. }
  1632. if (cmd_echo) {
  1633. printf("%s", menuin);
  1634. }
  1635. /* Update call setting */
  1636. pjsua_call_setting_default(&call_opt);
  1637. call_opt.aud_cnt = app_config.aud_cnt;
  1638. call_opt.vid_cnt = app_config.vid.vid_cnt;
  1639. switch (menuin[0]) {
  1640. case 'm':
  1641. /* Make call! : */
  1642. ui_make_new_call();
  1643. break;
  1644. case 'M':
  1645. /* Make multiple calls! : */
  1646. ui_make_multi_call();
  1647. break;
  1648. case 'n':
  1649. ui_detect_nat_type();
  1650. break;
  1651. case 'i':
  1652. /* Send instant messaeg */
  1653. ui_send_instant_message();
  1654. break;
  1655. case 'a':
  1656. ui_answer_call();
  1657. break;
  1658. case 'h':
  1659. ui_hangup_call(menuin);
  1660. break;
  1661. case ']':
  1662. case '[':
  1663. /*
  1664. * Cycle next/prev dialog.
  1665. */
  1666. ui_cycle_dialog(menuin);
  1667. break;
  1668. case '>':
  1669. case '<':
  1670. ui_cycle_account();
  1671. break;
  1672. case '+':
  1673. if (menuin[1] == 'b') {
  1674. ui_add_buddy();
  1675. } else if (menuin[1] == 'a') {
  1676. ui_add_account(&app_config.rtp_cfg);
  1677. } else {
  1678. printf("Invalid input %s\n", menuin);
  1679. }
  1680. break;
  1681. case '-':
  1682. if (menuin[1] == 'b') {
  1683. ui_delete_buddy();
  1684. } else if (menuin[1] == 'a') {
  1685. ui_delete_account();
  1686. } else {
  1687. printf("Invalid input %s\n", menuin);
  1688. }
  1689. break;
  1690. case 'H':
  1691. /*
  1692. * Hold call.
  1693. */
  1694. ui_call_hold();
  1695. break;
  1696. case 'o':
  1697. /*
  1698. * Toggle call SDP offer
  1699. */
  1700. ui_toggle_call_sdp_offer();
  1701. break;
  1702. case 'v':
  1703. #if PJSUA_HAS_VIDEO
  1704. if (menuin[1]=='i' && menuin[2]=='d' && menuin[3]==' ') {
  1705. vid_handle_menu(menuin);
  1706. } else
  1707. #endif
  1708. if (current_call != -1) {
  1709. /*
  1710. * re-INVITE
  1711. */
  1712. ui_call_reinvite();
  1713. } else {
  1714. PJ_LOG(3,(THIS_FILE, "No current call"));
  1715. }
  1716. break;
  1717. case 'U':
  1718. /*
  1719. * Send UPDATE
  1720. */
  1721. ui_send_update();
  1722. break;
  1723. case 'C':
  1724. if (menuin[1] == 'p') {
  1725. ui_manage_codec_prio();
  1726. }
  1727. break;
  1728. case 'x':
  1729. /*
  1730. * Transfer call.
  1731. */
  1732. ui_call_transfer(app_config.no_refersub);
  1733. break;
  1734. case 'X':
  1735. /*
  1736. * Transfer call with replaces.
  1737. */
  1738. ui_call_transfer_replaces(app_config.no_refersub);
  1739. break;
  1740. case '#':
  1741. /*
  1742. * Send DTMF strings.
  1743. */
  1744. ui_send_dtmf_2833();
  1745. break;
  1746. case '*':
  1747. /* Send DTMF with INFO */
  1748. ui_send_dtmf_info();
  1749. break;
  1750. case 'S':
  1751. /*
  1752. * Send arbitrary request
  1753. */
  1754. ui_send_arbitrary_request();
  1755. break;
  1756. case 'e':
  1757. ui_echo(menuin);
  1758. break;
  1759. case 's':
  1760. if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
  1761. ui_sleep(menuin);
  1762. break;
  1763. }
  1764. /* Continue below */
  1765. case 'u':
  1766. /*
  1767. * Subscribe/unsubscribe presence.
  1768. */
  1769. ui_subscribe(menuin);
  1770. break;
  1771. case 'D':
  1772. /* Subscribe/unsubscribe dialog event */
  1773. ui_subscribe_dlg_event(menuin[1] != 'u');
  1774. break;
  1775. case 'r':
  1776. ui_register(menuin);
  1777. break;
  1778. case 't':
  1779. ui_toggle_state();
  1780. break;
  1781. case 'T':
  1782. ui_change_online_status();
  1783. break;
  1784. case 'c':
  1785. switch (menuin[1]) {
  1786. case 'l':
  1787. ui_conf_list();
  1788. break;
  1789. case 'c':
  1790. case 'd':
  1791. ui_conf_connect(menuin);
  1792. break;
  1793. }
  1794. break;
  1795. case 'V':
  1796. /* Adjust audio volume */
  1797. ui_adjust_volume();
  1798. break;
  1799. case 'd':
  1800. if (menuin[1] == 'c') {
  1801. ui_dump_configuration();
  1802. } else if (menuin[1] == 'q') {
  1803. ui_dump_call_quality();
  1804. } else {
  1805. ui_app_dump(menuin[1]=='d');
  1806. }
  1807. break;
  1808. case 'f':
  1809. if (simple_input("Enter output filename", buf, sizeof(buf))) {
  1810. ui_write_settings(buf);
  1811. }
  1812. break;
  1813. case 'L': /* Restart */
  1814. case 'q':
  1815. legacy_on_stopped(menuin[0]=='L');
  1816. goto on_exit;
  1817. case 'R':
  1818. ui_call_redirect(menuin);
  1819. break;
  1820. case 'I': /* Handle IP change. */
  1821. ui_handle_ip_change();
  1822. break;
  1823. default:
  1824. if (menuin[0] != '\n' && menuin[0] != '\r') {
  1825. printf("Invalid input %s", menuin);
  1826. }
  1827. keystroke_help();
  1828. break;
  1829. }
  1830. }
  1831. on_exit:
  1832. ;
  1833. }