PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/CSipSimple/jni/pjsip/sources/pjsip-apps/src/pjsua/pjsua_app_legacy.c

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