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

https://bitbucket.org/bohlooli/csipsimple · C · 6302 lines · 6201 code · 59 blank · 42 comment · 60 complexity · 500d20d136897af79cd4f7a91341bf2e MD5 · raw file

  1. /* $Id: pjsua_app.c 4312 2013-01-03 09:26:29Z ming $ */
  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 "gui.h"
  22. #define THIS_FILE "pjsua_app.c"
  23. #define NO_LIMIT (int)0x7FFFFFFF
  24. //#define STEREO_DEMO
  25. //#define TRANSPORT_ADAPTER_SAMPLE
  26. //#define HAVE_MULTIPART_TEST
  27. /* Ringtones US UK */
  28. #define RINGBACK_FREQ1 440 /* 400 */
  29. #define RINGBACK_FREQ2 480 /* 450 */
  30. #define RINGBACK_ON 2000 /* 400 */
  31. #define RINGBACK_OFF 4000 /* 200 */
  32. #define RINGBACK_CNT 1 /* 2 */
  33. #define RINGBACK_INTERVAL 4000 /* 2000 */
  34. #define RING_FREQ1 800
  35. #define RING_FREQ2 640
  36. #define RING_ON 200
  37. #define RING_OFF 100
  38. #define RING_CNT 3
  39. #define RING_INTERVAL 3000
  40. #define MAX_AVI 4
  41. /* Call specific data */
  42. struct call_data
  43. {
  44. pj_timer_entry timer;
  45. pj_bool_t ringback_on;
  46. pj_bool_t ring_on;
  47. };
  48. /* Video settings */
  49. struct app_vid
  50. {
  51. unsigned vid_cnt;
  52. int vcapture_dev;
  53. int vrender_dev;
  54. pj_bool_t in_auto_show;
  55. pj_bool_t out_auto_transmit;
  56. };
  57. /* Pjsua application data */
  58. static struct app_config
  59. {
  60. pjsua_config cfg;
  61. pjsua_logging_config log_cfg;
  62. pjsua_media_config media_cfg;
  63. pj_bool_t no_refersub;
  64. pj_bool_t ipv6;
  65. pj_bool_t enable_qos;
  66. pj_bool_t no_tcp;
  67. pj_bool_t no_udp;
  68. pj_bool_t use_tls;
  69. pjsua_transport_config udp_cfg;
  70. pjsua_transport_config rtp_cfg;
  71. pjsip_redirect_op redir_op;
  72. unsigned acc_cnt;
  73. pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
  74. unsigned buddy_cnt;
  75. pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
  76. struct call_data call_data[PJSUA_MAX_CALLS];
  77. pj_pool_t *pool;
  78. /* Compatibility with older pjsua */
  79. unsigned codec_cnt;
  80. pj_str_t codec_arg[32];
  81. unsigned codec_dis_cnt;
  82. pj_str_t codec_dis[32];
  83. pj_bool_t null_audio;
  84. unsigned wav_count;
  85. pj_str_t wav_files[32];
  86. unsigned tone_count;
  87. pjmedia_tone_desc tones[32];
  88. pjsua_conf_port_id tone_slots[32];
  89. pjsua_player_id wav_id;
  90. pjsua_conf_port_id wav_port;
  91. pj_bool_t auto_play;
  92. pj_bool_t auto_play_hangup;
  93. pj_timer_entry auto_hangup_timer;
  94. pj_bool_t auto_loop;
  95. pj_bool_t auto_conf;
  96. pj_str_t rec_file;
  97. pj_bool_t auto_rec;
  98. pjsua_recorder_id rec_id;
  99. pjsua_conf_port_id rec_port;
  100. unsigned auto_answer;
  101. unsigned duration;
  102. #ifdef STEREO_DEMO
  103. pjmedia_snd_port *snd;
  104. pjmedia_port *sc, *sc_ch1;
  105. pjsua_conf_port_id sc_ch1_slot;
  106. #endif
  107. float mic_level,
  108. speaker_level;
  109. int capture_dev, playback_dev;
  110. unsigned capture_lat, playback_lat;
  111. pj_bool_t no_tones;
  112. int ringback_slot;
  113. int ringback_cnt;
  114. pjmedia_port *ringback_port;
  115. int ring_slot;
  116. int ring_cnt;
  117. pjmedia_port *ring_port;
  118. struct app_vid vid;
  119. unsigned aud_cnt;
  120. /* AVI to play */
  121. unsigned avi_cnt;
  122. struct {
  123. pj_str_t path;
  124. pjmedia_vid_dev_index dev_id;
  125. pjsua_conf_port_id slot;
  126. } avi[MAX_AVI];
  127. pj_bool_t avi_auto_play;
  128. int avi_def_idx;
  129. } app_config;
  130. //static pjsua_acc_id current_acc;
  131. #define current_acc pjsua_acc_get_default()
  132. static pjsua_call_id current_call = PJSUA_INVALID_ID;
  133. static pj_bool_t cmd_echo;
  134. static int stdout_refresh = -1;
  135. static const char *stdout_refresh_text = "STDOUT_REFRESH";
  136. static pj_bool_t stdout_refresh_quit = PJ_FALSE;
  137. static pj_str_t uri_arg;
  138. #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
  139. # define SOME_BUF_SIZE (1024 * 10)
  140. #else
  141. # define SOME_BUF_SIZE (1024 * 3)
  142. #endif
  143. static char some_buf[SOME_BUF_SIZE];
  144. #ifdef STEREO_DEMO
  145. static void stereo_demo();
  146. #endif
  147. pj_status_t app_destroy(void);
  148. static void ringback_start(pjsua_call_id call_id);
  149. static void ring_start(pjsua_call_id call_id);
  150. static void ring_stop(pjsua_call_id call_id);
  151. pj_bool_t app_restart;
  152. pj_log_func *log_cb = NULL;
  153. /*****************************************************************************
  154. * Configuration manipulation
  155. */
  156. #if (defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
  157. PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0) || \
  158. defined(__IPHONE_4_0)
  159. void keepAliveFunction(int timeout)
  160. {
  161. int i;
  162. for (i=0; i<(int)pjsua_acc_get_count(); ++i) {
  163. if (!pjsua_acc_is_valid(i))
  164. continue;
  165. if (app_config.acc_cfg[i].reg_timeout < timeout) {
  166. app_config.acc_cfg[i].reg_timeout = timeout;
  167. pjsua_acc_modify(i, &app_config.acc_cfg[i]);
  168. } else {
  169. pjsua_acc_set_registration(i, PJ_TRUE);
  170. }
  171. }
  172. }
  173. #endif
  174. /* Show usage */
  175. static void usage(void)
  176. {
  177. puts ("Usage:");
  178. puts (" pjsua [options] [SIP URL to call]");
  179. puts ("");
  180. puts ("General options:");
  181. puts (" --config-file=file Read the config/arguments from file.");
  182. puts (" --help Display this help screen");
  183. puts (" --version Display version info");
  184. puts ("");
  185. puts ("Logging options:");
  186. puts (" --log-file=fname Log to filename (default stderr)");
  187. puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
  188. puts (" --app-log-level=N Set log max level for stdout display (default=4)");
  189. puts (" --log-append Append instead of overwrite existing log file.\n");
  190. puts (" --color Use colorful logging (default yes on Win32)");
  191. puts (" --no-color Disable colorful logging");
  192. puts (" --light-bg Use dark colors for light background (default is dark bg)");
  193. puts (" --no-stderr Disable stderr");
  194. puts ("");
  195. puts ("SIP Account options:");
  196. puts (" --registrar=url Set the URL of registrar server");
  197. puts (" --id=url Set the URL of local ID (used in From header)");
  198. puts (" --realm=string Set realm");
  199. puts (" --username=string Set authentication username");
  200. puts (" --password=string Set authentication password");
  201. puts (" --contact=url Optionally override the Contact information");
  202. puts (" --contact-params=S Append the specified parameters S in Contact header");
  203. puts (" --contact-uri-params=S Append the specified parameters S in Contact URI");
  204. puts (" --proxy=url Optional URL of proxy server to visit");
  205. puts (" May be specified multiple times");
  206. printf(" --reg-timeout=SEC Optional registration interval (default %d)\n",
  207. PJSUA_REG_INTERVAL);
  208. printf(" --rereg-delay=SEC Optional auto retry registration interval (default %d)\n",
  209. PJSUA_REG_RETRY_INTERVAL);
  210. puts (" --reg-use-proxy=N Control the use of proxy settings in REGISTER.");
  211. puts (" 0=no proxy, 1=outbound only, 2=acc only, 3=all (default)");
  212. puts (" --publish Send presence PUBLISH for this account");
  213. puts (" --mwi Subscribe to message summary/waiting indication");
  214. puts (" --use-ims Enable 3GPP/IMS related settings on this account");
  215. #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
  216. puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory,");
  217. puts (" 3:optional by duplicating media offer (def:0)");
  218. puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 2:sips (def:1)");
  219. #endif
  220. puts (" --use-100rel Require reliable provisional response (100rel)");
  221. puts (" --use-timer=N Use SIP session timers? (default=1)");
  222. puts (" 0:inactive, 1:optional, 2:mandatory, 3:always");
  223. printf(" --timer-se=N Session timers expiration period, in secs (def:%d)\n",
  224. PJSIP_SESS_TIMER_DEF_SE);
  225. puts (" --timer-min-se=N Session timers minimum expiration period, in secs (def:90)");
  226. puts (" --outb-rid=string Set SIP outbound reg-id (default:1)");
  227. puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
  228. puts (" symmetric NAT (default 1)");
  229. puts (" --disable-stun Disable STUN for this account");
  230. puts (" --next-cred Add another credentials");
  231. puts ("");
  232. puts ("SIP Account Control:");
  233. puts (" --next-account Add more account");
  234. puts ("");
  235. puts ("Transport Options:");
  236. #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
  237. puts (" --ipv6 Use IPv6 instead for SIP and media.");
  238. #endif
  239. puts (" --set-qos Enable QoS tagging for SIP and media.");
  240. puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
  241. puts (" TCP and UDP transports on the specified port, unless");
  242. puts (" if TCP or UDP is disabled.");
  243. puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
  244. puts (" (Hint: the IP may be the public IP of the NAT/router)");
  245. puts (" --bound-addr=IP Bind transports to this IP interface");
  246. puts (" --no-tcp Disable TCP transport.");
  247. puts (" --no-udp Disable UDP transport.");
  248. puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
  249. puts (" This option can be specified multiple times.");
  250. puts (" --outbound=url Set the URL of global outbound proxy server");
  251. puts (" May be specified multiple times");
  252. puts (" --stun-srv=FORMAT Set STUN server host or domain. This option may be");
  253. puts (" specified more than once. FORMAT is hostdom[:PORT]");
  254. #if defined(PJSIP_HAS_TLS_TRANSPORT) && (PJSIP_HAS_TLS_TRANSPORT != 0)
  255. puts ("");
  256. puts ("TLS Options:");
  257. puts (" --use-tls Enable TLS transport (default=no)");
  258. puts (" --tls-ca-file Specify TLS CA file (default=none)");
  259. puts (" --tls-cert-file Specify TLS certificate file (default=none)");
  260. puts (" --tls-privkey-file Specify TLS private key file (default=none)");
  261. puts (" --tls-password Specify TLS password to private key file (default=none)");
  262. puts (" --tls-verify-server Verify server's certificate (default=no)");
  263. puts (" --tls-verify-client Verify client's certificate (default=no)");
  264. puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
  265. puts (" --tls-srv-name Specify TLS server name for multihosting server");
  266. puts (" --tls-cipher Specify prefered TLS cipher (optional).");
  267. puts (" May be specified multiple times");
  268. #endif
  269. puts ("");
  270. puts ("Audio Options:");
  271. puts (" --add-codec=name Manually add codec (default is to enable all)");
  272. puts (" --dis-codec=name Disable codec (can be specified multiple times)");
  273. puts (" --clock-rate=N Override conference bridge clock rate");
  274. puts (" --snd-clock-rate=N Override sound device clock rate");
  275. puts (" --stereo Audio device and conference bridge opened in stereo mode");
  276. puts (" --null-audio Use NULL audio device");
  277. puts (" --play-file=file Register WAV file in conference bridge.");
  278. puts (" This can be specified multiple times.");
  279. puts (" --play-tone=FORMAT Register tone to the conference bridge.");
  280. puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
  281. puts (" frequencies, and ON,OFF=on/off duration in msec.");
  282. puts (" This can be specified multiple times.");
  283. puts (" --auto-play Automatically play the file (to incoming calls only)");
  284. puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
  285. puts (" --auto-conf Automatically put calls in conference with others");
  286. puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
  287. puts (" --auto-rec Automatically record conversation");
  288. puts (" --quality=N Specify media quality (0-10, default=6)");
  289. puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
  290. puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
  291. puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
  292. puts (" --ec-opt=OPT Select echo canceller algorithm (0=default, ");
  293. puts (" 1=speex, 2=suppressor)");
  294. puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 30)");
  295. puts (" --capture-dev=id Audio capture device ID (default=-1)");
  296. puts (" --playback-dev=id Audio playback device ID (default=-1)");
  297. puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
  298. puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
  299. puts (" --snd-auto-close=N Auto close audio device when idle for N secs (default=1)");
  300. puts (" Specify N=-1 to disable this feature.");
  301. puts (" Specify N=0 for instant close when unused.");
  302. puts (" --no-tones Disable audible tones");
  303. puts (" --jb-max-size Specify jitter buffer maximum size, in frames (default=-1)");
  304. puts (" --extra-audio Add one more audio stream");
  305. #if PJSUA_HAS_VIDEO
  306. puts ("");
  307. puts ("Video Options:");
  308. puts (" --video Enable video");
  309. puts (" --vcapture-dev=id Video capture device ID (default=-1)");
  310. puts (" --vrender-dev=id Video render device ID (default=-2)");
  311. puts (" --play-avi=FILE Load this AVI as virtual capture device");
  312. puts (" --auto-play-avi Automatically play the AVI media to call");
  313. #endif
  314. puts ("");
  315. puts ("Media Transport Options:");
  316. puts (" --use-ice Enable ICE (default:no)");
  317. puts (" --ice-regular Use ICE regular nomination (default: aggressive)");
  318. puts (" --ice-max-hosts=N Set maximum number of ICE host candidates");
  319. puts (" --ice-no-rtcp Disable RTCP component in ICE (default: no)");
  320. puts (" --rtp-port=N Base port to try for RTP (default=4000)");
  321. puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
  322. puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
  323. puts (" --use-turn Enable TURN relay with ICE (default:no)");
  324. puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
  325. puts (" --turn-tcp Use TCP connection to TURN server (default no)");
  326. puts (" --turn-user TURN username");
  327. puts (" --turn-passwd TURN password");
  328. puts ("");
  329. puts ("Buddy List (can be more than one):");
  330. puts (" --add-buddy url Add the specified URL to the buddy list.");
  331. puts ("");
  332. puts ("User Agent options:");
  333. puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
  334. puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
  335. puts (" --thread-cnt=N Number of worker threads (default:1)");
  336. puts (" --duration=SEC Set maximum call duration (default:no limit)");
  337. puts (" --norefersub Suppress event subscription when transfering calls");
  338. puts (" --use-compact-form Minimize SIP message size");
  339. puts (" --no-force-lr Allow strict-route to be used (i.e. do not force lr)");
  340. puts (" --accept-redirect=N Specify how to handle call redirect (3xx) response.");
  341. puts (" 0: reject, 1: follow automatically (default), 2: ask");
  342. puts ("");
  343. puts ("When URL is specified, pjsua will immediately initiate call to that URL");
  344. puts ("");
  345. fflush(stdout);
  346. }
  347. /* Set default config. */
  348. static void default_config(struct app_config *cfg)
  349. {
  350. char tmp[80];
  351. unsigned i;
  352. pjsua_config_default(&cfg->cfg);
  353. pj_ansi_sprintf(tmp, "PJSUA v%s %s", pj_get_version(),
  354. pj_get_sys_info()->info.ptr);
  355. pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
  356. pjsua_logging_config_default(&cfg->log_cfg);
  357. pjsua_media_config_default(&cfg->media_cfg);
  358. pjsua_transport_config_default(&cfg->udp_cfg);
  359. cfg->udp_cfg.port = 5060;
  360. pjsua_transport_config_default(&cfg->rtp_cfg);
  361. cfg->rtp_cfg.port = 4000;
  362. cfg->redir_op = PJSIP_REDIRECT_ACCEPT;
  363. cfg->duration = NO_LIMIT;
  364. cfg->wav_id = PJSUA_INVALID_ID;
  365. cfg->rec_id = PJSUA_INVALID_ID;
  366. cfg->wav_port = PJSUA_INVALID_ID;
  367. cfg->rec_port = PJSUA_INVALID_ID;
  368. cfg->mic_level = cfg->speaker_level = 1.0;
  369. cfg->capture_dev = PJSUA_INVALID_ID;
  370. cfg->playback_dev = PJSUA_INVALID_ID;
  371. cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
  372. cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
  373. cfg->ringback_slot = PJSUA_INVALID_ID;
  374. cfg->ring_slot = PJSUA_INVALID_ID;
  375. for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
  376. pjsua_acc_config_default(&cfg->acc_cfg[i]);
  377. for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
  378. pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
  379. cfg->vid.vcapture_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
  380. cfg->vid.vrender_dev = PJMEDIA_VID_DEFAULT_RENDER_DEV;
  381. cfg->aud_cnt = 1;
  382. cfg->avi_def_idx = PJSUA_INVALID_ID;
  383. }
  384. /*
  385. * Read command arguments from config file.
  386. */
  387. static int read_config_file(pj_pool_t *pool, const char *filename,
  388. int *app_argc, char ***app_argv)
  389. {
  390. int i;
  391. FILE *fhnd;
  392. char line[200];
  393. int argc = 0;
  394. char **argv;
  395. enum { MAX_ARGS = 128 };
  396. /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
  397. argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
  398. argv[argc++] = *app_argv[0];
  399. /* Open config file. */
  400. fhnd = fopen(filename, "rt");
  401. if (!fhnd) {
  402. PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
  403. fflush(stdout);
  404. return -1;
  405. }
  406. /* Scan tokens in the file. */
  407. while (argc < MAX_ARGS && !feof(fhnd)) {
  408. char *token;
  409. char *p;
  410. const char *whitespace = " \t\r\n";
  411. char cDelimiter;
  412. int len, token_len;
  413. if (fgets(line, sizeof(line), fhnd) == NULL) break;
  414. // Trim ending newlines
  415. len = strlen(line);
  416. if (line[len-1]=='\n')
  417. line[--len] = '\0';
  418. if (line[len-1]=='\r')
  419. line[--len] = '\0';
  420. if (len==0) continue;
  421. for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
  422. // first, scan whitespaces
  423. while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
  424. if (*p == '\0') // are we done yet?
  425. break;
  426. if (*p == '"' || *p == '\'') { // is token a quoted string
  427. cDelimiter = *p++; // save quote delimiter
  428. token = p;
  429. while (*p != '\0' && *p != cDelimiter) p++;
  430. if (*p == '\0') // found end of the line, but,
  431. cDelimiter = '\0'; // didn't find a matching quote
  432. } else { // token's not a quoted string
  433. token = p;
  434. while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
  435. cDelimiter = *p;
  436. }
  437. *p = '\0';
  438. token_len = p-token;
  439. if (token_len > 0) {
  440. if (*token == '#')
  441. break; // ignore remainder of line
  442. argv[argc] = pj_pool_alloc(pool, token_len + 1);
  443. pj_memcpy(argv[argc], token, token_len + 1);
  444. ++argc;
  445. }
  446. *p = cDelimiter;
  447. }
  448. }
  449. /* Copy arguments from command line */
  450. for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
  451. argv[argc++] = (*app_argv)[i];
  452. if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
  453. PJ_LOG(1,(THIS_FILE,
  454. "Too many arguments specified in cmd line/config file"));
  455. fflush(stdout);
  456. fclose(fhnd);
  457. return -1;
  458. }
  459. fclose(fhnd);
  460. /* Assign the new command line back to the original command line. */
  461. *app_argc = argc;
  462. *app_argv = argv;
  463. return 0;
  464. }
  465. static int my_atoi(const char *cs)
  466. {
  467. pj_str_t s;
  468. pj_cstr(&s, cs);
  469. if (cs[0] == '-') {
  470. s.ptr++, s.slen--;
  471. return 0 - (int)pj_strtoul(&s);
  472. } else if (cs[0] == '+') {
  473. s.ptr++, s.slen--;
  474. return pj_strtoul(&s);
  475. } else {
  476. return pj_strtoul(&s);
  477. }
  478. }
  479. /* Parse arguments. */
  480. static pj_status_t parse_args(int argc, char *argv[],
  481. struct app_config *cfg,
  482. pj_str_t *uri_to_call)
  483. {
  484. int c;
  485. int option_index;
  486. enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
  487. OPT_LOG_APPEND, OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG, OPT_NO_STDERR,
  488. OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
  489. OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
  490. OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
  491. OPT_BOUND_ADDR, OPT_CONTACT_PARAMS, OPT_CONTACT_URI_PARAMS,
  492. OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
  493. OPT_REG_RETRY_INTERVAL, OPT_REG_USE_PROXY,
  494. OPT_MWI, OPT_NAMESERVER, OPT_STUN_SRV, OPT_OUTB_RID,
  495. OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
  496. OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
  497. OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
  498. OPT_USE_ICE, OPT_ICE_REGULAR, OPT_USE_SRTP, OPT_SRTP_SECURE,
  499. OPT_USE_TURN, OPT_ICE_MAX_HOSTS, OPT_ICE_NO_RTCP, OPT_TURN_SRV,
  500. OPT_TURN_TCP, OPT_TURN_USER, OPT_TURN_PASSWD,
  501. OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
  502. OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
  503. OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
  504. OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL, OPT_EC_OPT,
  505. OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
  506. OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
  507. OPT_NOREFERSUB, OPT_ACCEPT_REDIRECT,
  508. OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
  509. OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
  510. OPT_TLS_NEG_TIMEOUT, OPT_TLS_CIPHER,
  511. OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
  512. OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES, OPT_JB_MAX_SIZE,
  513. OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT, OPT_IPV6, OPT_QOS,
  514. #ifdef _IONBF
  515. OPT_STDOUT_NO_BUF,
  516. #endif
  517. OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC,
  518. OPT_DISABLE_STUN, OPT_NO_FORCE_LR,
  519. OPT_TIMER, OPT_TIMER_SE, OPT_TIMER_MIN_SE,
  520. OPT_VIDEO, OPT_EXTRA_AUDIO,
  521. OPT_VCAPTURE_DEV, OPT_VRENDER_DEV, OPT_PLAY_AVI, OPT_AUTO_PLAY_AVI
  522. };
  523. struct pj_getopt_option long_options[] = {
  524. { "config-file",1, 0, OPT_CONFIG_FILE},
  525. { "log-file", 1, 0, OPT_LOG_FILE},
  526. { "log-level", 1, 0, OPT_LOG_LEVEL},
  527. { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
  528. { "log-append", 0, 0, OPT_LOG_APPEND},
  529. { "color", 0, 0, OPT_COLOR},
  530. { "no-color", 0, 0, OPT_NO_COLOR},
  531. { "light-bg", 0, 0, OPT_LIGHT_BG},
  532. { "no-stderr", 0, 0, OPT_NO_STDERR},
  533. { "help", 0, 0, OPT_HELP},
  534. { "version", 0, 0, OPT_VERSION},
  535. { "clock-rate", 1, 0, OPT_CLOCK_RATE},
  536. { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
  537. { "stereo", 0, 0, OPT_STEREO},
  538. { "null-audio", 0, 0, OPT_NULL_AUDIO},
  539. { "local-port", 1, 0, OPT_LOCAL_PORT},
  540. { "ip-addr", 1, 0, OPT_IP_ADDR},
  541. { "bound-addr", 1, 0, OPT_BOUND_ADDR},
  542. { "no-tcp", 0, 0, OPT_NO_TCP},
  543. { "no-udp", 0, 0, OPT_NO_UDP},
  544. { "norefersub", 0, 0, OPT_NOREFERSUB},
  545. { "proxy", 1, 0, OPT_PROXY},
  546. { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
  547. { "registrar", 1, 0, OPT_REGISTRAR},
  548. { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
  549. { "publish", 0, 0, OPT_PUBLISH},
  550. { "mwi", 0, 0, OPT_MWI},
  551. { "use-100rel", 0, 0, OPT_100REL},
  552. { "use-ims", 0, 0, OPT_USE_IMS},
  553. { "id", 1, 0, OPT_ID},
  554. { "contact", 1, 0, OPT_CONTACT},
  555. { "contact-params",1,0, OPT_CONTACT_PARAMS},
  556. { "contact-uri-params",1,0, OPT_CONTACT_URI_PARAMS},
  557. { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
  558. { "disable-stun",0,0, OPT_DISABLE_STUN},
  559. { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
  560. { "accept-redirect", 1, 0, OPT_ACCEPT_REDIRECT},
  561. { "no-force-lr",0, 0, OPT_NO_FORCE_LR},
  562. { "realm", 1, 0, OPT_REALM},
  563. { "username", 1, 0, OPT_USERNAME},
  564. { "password", 1, 0, OPT_PASSWORD},
  565. { "rereg-delay",1, 0, OPT_REG_RETRY_INTERVAL},
  566. { "reg-use-proxy", 1, 0, OPT_REG_USE_PROXY},
  567. { "nameserver", 1, 0, OPT_NAMESERVER},
  568. { "stun-srv", 1, 0, OPT_STUN_SRV},
  569. { "add-buddy", 1, 0, OPT_ADD_BUDDY},
  570. { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
  571. { "no-presence", 0, 0, OPT_NO_PRESENCE},
  572. { "auto-answer",1, 0, OPT_AUTO_ANSWER},
  573. { "auto-play", 0, 0, OPT_AUTO_PLAY},
  574. { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
  575. { "auto-rec", 0, 0, OPT_AUTO_REC},
  576. { "auto-loop", 0, 0, OPT_AUTO_LOOP},
  577. { "auto-conf", 0, 0, OPT_AUTO_CONF},
  578. { "play-file", 1, 0, OPT_PLAY_FILE},
  579. { "play-tone", 1, 0, OPT_PLAY_TONE},
  580. { "rec-file", 1, 0, OPT_REC_FILE},
  581. { "rtp-port", 1, 0, OPT_RTP_PORT},
  582. { "use-ice", 0, 0, OPT_USE_ICE},
  583. { "ice-regular",0, 0, OPT_ICE_REGULAR},
  584. { "use-turn", 0, 0, OPT_USE_TURN},
  585. { "ice-max-hosts",1, 0, OPT_ICE_MAX_HOSTS},
  586. { "ice-no-rtcp",0, 0, OPT_ICE_NO_RTCP},
  587. { "turn-srv", 1, 0, OPT_TURN_SRV},
  588. { "turn-tcp", 0, 0, OPT_TURN_TCP},
  589. { "turn-user", 1, 0, OPT_TURN_USER},
  590. { "turn-passwd",1, 0, OPT_TURN_PASSWD},
  591. #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
  592. { "use-srtp", 1, 0, OPT_USE_SRTP},
  593. { "srtp-secure",1, 0, OPT_SRTP_SECURE},
  594. #endif
  595. { "add-codec", 1, 0, OPT_ADD_CODEC},
  596. { "dis-codec", 1, 0, OPT_DIS_CODEC},
  597. { "complexity", 1, 0, OPT_COMPLEXITY},
  598. { "quality", 1, 0, OPT_QUALITY},
  599. { "ptime", 1, 0, OPT_PTIME},
  600. { "no-vad", 0, 0, OPT_NO_VAD},
  601. { "ec-tail", 1, 0, OPT_EC_TAIL},
  602. { "ec-opt", 1, 0, OPT_EC_OPT},
  603. { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
  604. { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
  605. { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
  606. { "next-account",0,0, OPT_NEXT_ACCOUNT},
  607. { "next-cred", 0, 0, OPT_NEXT_CRED},
  608. { "max-calls", 1, 0, OPT_MAX_CALLS},
  609. { "duration", 1, 0, OPT_DURATION},
  610. { "thread-cnt", 1, 0, OPT_THREAD_CNT},
  611. #if defined(PJSIP_HAS_TLS_TRANSPORT) && (PJSIP_HAS_TLS_TRANSPORT != 0)
  612. { "use-tls", 0, 0, OPT_USE_TLS},
  613. { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
  614. { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
  615. { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
  616. { "tls-password",1,0, OPT_TLS_PASSWORD},
  617. { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
  618. { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
  619. { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
  620. { "tls-cipher", 1, 0, OPT_TLS_CIPHER},
  621. #endif
  622. { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
  623. { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
  624. { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
  625. { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
  626. { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
  627. { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
  628. #ifdef _IONBF
  629. { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
  630. #endif
  631. { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
  632. { "no-tones", 0, 0, OPT_NO_TONES},
  633. { "jb-max-size", 1, 0, OPT_JB_MAX_SIZE},
  634. #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
  635. { "ipv6", 0, 0, OPT_IPV6},
  636. #endif
  637. { "set-qos", 0, 0, OPT_QOS},
  638. { "use-timer", 1, 0, OPT_TIMER},
  639. { "timer-se", 1, 0, OPT_TIMER_SE},
  640. { "timer-min-se", 1, 0, OPT_TIMER_MIN_SE},
  641. { "outb-rid", 1, 0, OPT_OUTB_RID},
  642. { "video", 0, 0, OPT_VIDEO},
  643. { "extra-audio",0, 0, OPT_EXTRA_AUDIO},
  644. { "vcapture-dev", 1, 0, OPT_VCAPTURE_DEV},
  645. { "vrender-dev", 1, 0, OPT_VRENDER_DEV},
  646. { "play-avi", 1, 0, OPT_PLAY_AVI},
  647. { "auto-play-avi", 0, 0, OPT_AUTO_PLAY_AVI},
  648. { NULL, 0, 0, 0}
  649. };
  650. pj_status_t status;
  651. pjsua_acc_config *cur_acc;
  652. char *config_file = NULL;
  653. unsigned i;
  654. /* Run pj_getopt once to see if user specifies config file to read. */
  655. pj_optind = 0;
  656. while ((c=pj_getopt_long(argc, argv, "", long_options,
  657. &option_index)) != -1)
  658. {
  659. switch (c) {
  660. case OPT_CONFIG_FILE:
  661. config_file = pj_optarg;
  662. break;
  663. }
  664. if (config_file)
  665. break;
  666. }
  667. if (config_file) {
  668. status = read_config_file(app_config.pool, config_file, &argc, &argv);
  669. if (status != 0)
  670. return status;
  671. }
  672. cfg->acc_cnt = 0;
  673. cur_acc = &cfg->acc_cfg[0];
  674. /* Reinitialize and re-run pj_getopt again, possibly with new arguments
  675. * read from config file.
  676. */
  677. pj_optind = 0;
  678. while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
  679. pj_str_t tmp;
  680. long lval;
  681. switch (c) {
  682. case OPT_CONFIG_FILE:
  683. /* Ignore as this has been processed before */
  684. break;
  685. case OPT_LOG_FILE:
  686. cfg->log_cfg.log_filename = pj_str(pj_optarg);
  687. break;
  688. case OPT_LOG_LEVEL:
  689. c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  690. if (c < 0 || c > 6) {
  691. PJ_LOG(1,(THIS_FILE,
  692. "Error: expecting integer value 0-6 "
  693. "for --log-level"));
  694. return PJ_EINVAL;
  695. }
  696. cfg->log_cfg.level = c;
  697. pj_log_set_level( c );
  698. break;
  699. case OPT_APP_LOG_LEVEL:
  700. cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  701. if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
  702. PJ_LOG(1,(THIS_FILE,
  703. "Error: expecting integer value 0-6 "
  704. "for --app-log-level"));
  705. return PJ_EINVAL;
  706. }
  707. break;
  708. case OPT_LOG_APPEND:
  709. cfg->log_cfg.log_file_flags |= PJ_O_APPEND;
  710. break;
  711. case OPT_COLOR:
  712. cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR;
  713. break;
  714. case OPT_NO_COLOR:
  715. cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR;
  716. break;
  717. case OPT_LIGHT_BG:
  718. pj_log_set_color(1, PJ_TERM_COLOR_R);
  719. pj_log_set_color(2, PJ_TERM_COLOR_R | PJ_TERM_COLOR_G);
  720. pj_log_set_color(3, PJ_TERM_COLOR_B | PJ_TERM_COLOR_G);
  721. pj_log_set_color(4, 0);
  722. pj_log_set_color(5, 0);
  723. pj_log_set_color(77, 0);
  724. break;
  725. case OPT_NO_STDERR:
  726. freopen("/dev/null", "w", stderr);
  727. break;
  728. case OPT_HELP:
  729. usage();
  730. return PJ_EINVAL;
  731. case OPT_VERSION: /* version */
  732. pj_dump_config();
  733. return PJ_EINVAL;
  734. case OPT_NULL_AUDIO:
  735. cfg->null_audio = PJ_TRUE;
  736. break;
  737. case OPT_CLOCK_RATE:
  738. lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  739. if (lval < 8000 || lval > 192000) {
  740. PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
  741. "8000-192000 for conference clock rate"));
  742. return PJ_EINVAL;
  743. }
  744. cfg->media_cfg.clock_rate = lval;
  745. break;
  746. case OPT_SND_CLOCK_RATE:
  747. lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  748. if (lval < 8000 || lval > 192000) {
  749. PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
  750. "8000-192000 for sound device clock rate"));
  751. return PJ_EINVAL;
  752. }
  753. cfg->media_cfg.snd_clock_rate = lval;
  754. break;
  755. case OPT_STEREO:
  756. cfg->media_cfg.channel_count = 2;
  757. break;
  758. case OPT_LOCAL_PORT: /* local-port */
  759. lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  760. if (lval < 0 || lval > 65535) {
  761. PJ_LOG(1,(THIS_FILE,
  762. "Error: expecting integer value for "
  763. "--local-port"));
  764. return PJ_EINVAL;
  765. }
  766. cfg->udp_cfg.port = (pj_uint16_t)lval;
  767. break;
  768. case OPT_IP_ADDR: /* ip-addr */
  769. cfg->udp_cfg.public_addr = pj_str(pj_optarg);
  770. cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
  771. break;
  772. case OPT_BOUND_ADDR: /* bound-addr */
  773. cfg->udp_cfg.bound_addr = pj_str(pj_optarg);
  774. cfg->rtp_cfg.bound_addr = pj_str(pj_optarg);
  775. break;
  776. case OPT_NO_UDP: /* no-udp */
  777. if (cfg->no_tcp && !cfg->use_tls) {
  778. PJ_LOG(1,(THIS_FILE,"Error: cannot disable both TCP and UDP"));
  779. return PJ_EINVAL;
  780. }
  781. cfg->no_udp = PJ_TRUE;
  782. break;
  783. case OPT_NOREFERSUB: /* norefersub */
  784. cfg->no_refersub = PJ_TRUE;
  785. break;
  786. case OPT_NO_TCP: /* no-tcp */
  787. if (cfg->no_udp && !cfg->use_tls) {
  788. PJ_LOG(1,(THIS_FILE,"Error: cannot disable both TCP and UDP"));
  789. return PJ_EINVAL;
  790. }
  791. cfg->no_tcp = PJ_TRUE;
  792. break;
  793. case OPT_PROXY: /* proxy */
  794. if (pjsua_verify_sip_url(pj_optarg) != 0) {
  795. PJ_LOG(1,(THIS_FILE,
  796. "Error: invalid SIP URL '%s' "
  797. "in proxy argument", pj_optarg));
  798. return PJ_EINVAL;
  799. }
  800. cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
  801. break;
  802. case OPT_OUTBOUND_PROXY: /* outbound proxy */
  803. if (pjsua_verify_sip_url(pj_optarg) != 0) {
  804. PJ_LOG(1,(THIS_FILE,
  805. "Error: invalid SIP URL '%s' "
  806. "in outbound proxy argument", pj_optarg));
  807. return PJ_EINVAL;
  808. }
  809. cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
  810. break;
  811. case OPT_REGISTRAR: /* registrar */
  812. if (pjsua_verify_sip_url(pj_optarg) != 0) {
  813. PJ_LOG(1,(THIS_FILE,
  814. "Error: invalid SIP URL '%s' in "
  815. "registrar argument", pj_optarg));
  816. return PJ_EINVAL;
  817. }
  818. cur_acc->reg_uri = pj_str(pj_optarg);
  819. break;
  820. case OPT_REG_TIMEOUT: /* reg-timeout */
  821. cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
  822. if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
  823. PJ_LOG(1,(THIS_FILE,
  824. "Error: invalid value for --reg-timeout "
  825. "(expecting 1-3600)"));
  826. return PJ_EINVAL;
  827. }
  828. break;
  829. case OPT_PUBLISH: /* publish */
  830. cur_acc->publish_enabled = PJ_TRUE;
  831. break;
  832. case OPT_MWI: /* mwi */
  833. cur_acc->mwi_enabled = PJ_TRUE;
  834. break;
  835. case OPT_100REL: /** 100rel */
  836. cur_acc->require_100rel = PJSUA_100REL_MANDATORY;
  837. cfg->cfg.require_100rel = PJSUA_100REL_MANDATORY;
  838. break;
  839. case OPT_TIMER: /** session timer */
  840. lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  841. if (lval < 0 || lval > 3) {
  842. PJ_LOG(1,(THIS_FILE,
  843. "Error: expecting integer value 0-3 for --use-timer"));
  844. return PJ_EINVAL;
  845. }
  846. cur_acc->use_timer = lval;
  847. cfg->cfg.use_timer = lval;
  848. break;
  849. case OPT_TIMER_SE: /** session timer session expiration */
  850. cur_acc->timer_setting.sess_expires = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  851. if (cur_acc->timer_setting.sess_expires < 90) {
  852. PJ_LOG(1,(THIS_FILE,
  853. "Error: invalid value for --timer-se "
  854. "(expecting higher than 90)"));
  855. return PJ_EINVAL;
  856. }
  857. cfg->cfg.timer_setting.sess_expires = cur_acc->timer_setting.sess_expires;
  858. break;
  859. case OPT_TIMER_MIN_SE: /** session timer minimum session expiration */
  860. cur_acc->timer_setting.min_se = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  861. if (cur_acc->timer_setting.min_se < 90) {
  862. PJ_LOG(1,(THIS_FILE,
  863. "Error: invalid value for --timer-min-se "
  864. "(expecting higher than 90)"));
  865. return PJ_EINVAL;
  866. }
  867. cfg->cfg.timer_setting.min_se = cur_acc->timer_setting.min_se;
  868. break;
  869. case OPT_OUTB_RID: /* Outbound reg-id */
  870. cur_acc->rfc5626_reg_id = pj_str(pj_optarg);
  871. break;
  872. case OPT_USE_IMS: /* Activate IMS settings */
  873. cur_acc->auth_pref.initial_auth = PJ_TRUE;
  874. break;
  875. case OPT_ID: /* id */
  876. if (pjsua_verify_url(pj_optarg) != 0) {
  877. PJ_LOG(1,(THIS_FILE,
  878. "Error: invalid SIP URL '%s' "
  879. "in local id argument", pj_optarg));
  880. return PJ_EINVAL;
  881. }
  882. cur_acc->id = pj_str(pj_optarg);
  883. break;
  884. case OPT_CONTACT: /* contact */
  885. if (pjsua_verify_sip_url(pj_optarg) != 0) {
  886. PJ_LOG(1,(THIS_FILE,
  887. "Error: invalid SIP URL '%s' "
  888. "in contact argument", pj_optarg));
  889. return PJ_EINVAL;
  890. }
  891. cur_acc->force_contact = pj_str(pj_optarg);
  892. break;
  893. case OPT_CONTACT_PARAMS:
  894. cur_acc->contact_params = pj_str(pj_optarg);
  895. break;
  896. case OPT_CONTACT_URI_PARAMS:
  897. cur_acc->contact_uri_params = pj_str(pj_optarg);
  898. break;
  899. case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
  900. cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  901. break;
  902. case OPT_DISABLE_STUN:
  903. cur_acc->sip_stun_use = PJSUA_STUN_USE_DISABLED;
  904. cur_acc->media_stun_use = PJSUA_STUN_USE_DISABLED;
  905. break;
  906. case OPT_USE_COMPACT_FORM:
  907. /* enable compact form - from Ticket #342 */
  908. {
  909. extern pj_bool_t pjsip_use_compact_form;
  910. extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
  911. extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
  912. pjsip_use_compact_form = PJ_TRUE;
  913. /* do not transmit Allow header */
  914. pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
  915. /* Do not include rtpmap for static payload types (<96) */
  916. pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
  917. }
  918. break;
  919. case OPT_ACCEPT_REDIRECT:
  920. cfg->redir_op = my_atoi(pj_optarg);
  921. if (cfg->redir_op<0 || cfg->redir_op>PJSIP_REDIRECT_STOP) {
  922. PJ_LOG(1,(THIS_FILE,
  923. "Error: accept-redirect value '%s' ", pj_optarg));
  924. return PJ_EINVAL;
  925. }
  926. break;
  927. case OPT_NO_FORCE_LR:
  928. cfg->cfg.force_lr = PJ_FALSE;
  929. break;
  930. case OPT_NEXT_ACCOUNT: /* Add more account. */
  931. cfg->acc_cnt++;
  932. cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
  933. break;
  934. case OPT_USERNAME: /* Default authentication user */
  935. cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
  936. cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
  937. break;
  938. case OPT_REALM: /* Default authentication realm. */
  939. cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
  940. break;
  941. case OPT_PASSWORD: /* authentication password */
  942. cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
  943. cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
  944. #if PJSIP_HAS_DIGEST_AKA_AUTH
  945. cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
  946. cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
  947. cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
  948. #endif
  949. break;
  950. case OPT_REG_RETRY_INTERVAL:
  951. cur_acc->reg_retry_interval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
  952. break;
  953. case OPT_REG_USE_PROXY:
  954. cur_acc->reg_use_proxy = (unsigned)pj_strtoul(pj_cstr(&tmp, pj_optarg));
  955. if (cur_acc->reg_use_proxy > 3) {
  956. PJ_LOG(1,(THIS_FILE, "Error: invalid --reg-use-proxy value '%s'",
  957. pj_optarg));
  958. return PJ_EINVAL;
  959. }
  960. break;
  961. case OPT_NEXT_CRED: /* next credential */
  962. cur_acc->cred_count++;
  963. break;
  964. case OPT_NAMESERVER: /* nameserver */
  965. cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
  966. if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
  967. PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
  968. return PJ_ETOOMANY;
  969. }
  970. break;
  971. case OPT_STUN_SRV: /* STUN server */
  972. cfg->cfg.stun_host = pj_str(pj_optarg);
  973. if (cfg->cfg.stun_srv_cnt==PJ_ARRAY_SIZE(cfg->cfg.stun_srv)) {
  974. PJ_LOG(1,(THIS_FILE, "Error: too many STUN servers"));
  975. return PJ_ETOOMANY;
  976. }
  977. cfg->cfg.stun_srv[cfg->cfg.stun_srv_cnt++] = pj_str(pj_optarg);
  978. break;
  979. case OPT_ADD_BUDDY: /* Add to buddy list. */
  980. if (pjsua_verify_url(pj_optarg) != 0) {
  981. PJ_LOG(1,(THIS_FILE,
  982. "Error: invalid URL '%s' in "
  983. "--add-buddy option", pj_optarg));
  984. return -1;
  985. }
  986. if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
  987. PJ_LOG(1,(THIS_FILE,
  988. "Error: too many buddies in buddy list."));
  989. return -1;
  990. }
  991. cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
  992. cfg->buddy_cnt++;
  993. break;
  994. case OPT_AUTO_PLAY:
  995. cfg->auto_play = 1;
  996. break;
  997. case OPT_AUTO_PLAY_HANGUP:
  998. cfg->auto_play_hangup = 1;
  999. break;
  1000. case OPT_AUTO_REC:
  1001. cfg->auto_rec = 1;
  1002. break;
  1003. case OPT_AUTO_LOOP:
  1004. cfg->auto_loop = 1;
  1005. break;
  1006. case OPT_AUTO_CONF:
  1007. cfg->auto_conf = 1;
  1008. break;
  1009. case OPT_PLAY_FILE:
  1010. cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
  1011. break;
  1012. case OPT_PLAY_TONE:
  1013. {
  1014. int f1, f2, on, off;
  1015. int n;
  1016. n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
  1017. if (n != 4) {
  1018. puts("Expecting f1,f2,on,off in --play-tone");
  1019. return -1;
  1020. }
  1021. cfg->tones[cfg->tone_count].freq1 = (short)f1;
  1022. cfg->tones[cfg->tone_count].freq2 = (short)f2;
  1023. cfg->tones[cfg->tone_count].on_msec = (short)on;
  1024. cfg->tones[cfg->tone_count].off_msec = (short)off;
  1025. ++cfg->tone_count;
  1026. }
  1027. break;
  1028. case OPT_REC_FILE:
  1029. cfg->rec_file = pj_str(pj_optarg);
  1030. break;
  1031. case OPT_USE_ICE:
  1032. cfg->media_cfg.enable_ice =
  1033. cur_acc->ice_cfg.enable_ice = PJ_TRUE;
  1034. break;
  1035. case OPT_ICE_REGULAR:
  1036. cfg->media_cfg.ice_opt.aggressive =
  1037. cur_acc->ice_cfg.ice_opt.aggressive = PJ_FALSE;
  1038. break;
  1039. case OPT_USE_TURN:
  1040. cfg->media_cfg.enable_turn =
  1041. cur_acc->turn_cfg.enable_turn = PJ_TRUE;
  1042. break;
  1043. case OPT_ICE_MAX_HOSTS:
  1044. cfg->media_cfg.ice_max_host_cands =
  1045. cur_acc->ice_cfg.ice_max_host_cands = my_atoi(pj_optarg);
  1046. break;
  1047. case OPT_ICE_NO_RTCP:
  1048. cfg->media_cfg.ice_no_rtcp =
  1049. cur_acc->ice_cfg.ice_no_rtcp = PJ_TRUE;
  1050. break;
  1051. case OPT_TURN_SRV:
  1052. cfg->media_cfg.turn_server =
  1053. cur_acc->turn_cfg.turn_server = pj_str(pj_optarg);
  1054. break;
  1055. case OPT_TURN_TCP:
  1056. cfg->media_cfg.turn_conn_type =
  1057. cur_acc->turn_cfg.turn_conn_type = PJ_TURN_TP_TCP;
  1058. break;
  1059. case OPT_TURN_USER:
  1060. cfg->media_cfg.turn_auth_cred.type =
  1061. cur_acc->turn_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
  1062. cfg->media_cfg.turn_auth_cred.data.static_cred.realm =
  1063. cur_acc->turn_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
  1064. cfg->media_cfg.turn_auth_cred.data.static_cred.username =
  1065. cur_acc->turn_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
  1066. break;
  1067. case OPT_TURN_PASSWD:
  1068. cfg->media_cfg.turn_auth_cred.data.static_cred.data_type =
  1069. cur_acc->turn_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
  1070. cfg->media_cfg.turn_auth_cred.data.static_cred.data =
  1071. cur_acc->turn_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
  1072. break;
  1073. #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
  1074. case OPT_USE_SRTP:
  1075. app_config.cfg.use_srtp = my_atoi(pj_optarg);
  1076. if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 3) {
  1077. PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
  1078. return -1;
  1079. }
  1080. if ((int)app_config.cfg.use_srtp == 3) {
  1081. /* SRTP optional mode with duplicated media offer */
  1082. app_config.cfg.use_srtp = PJMEDIA_SRTP_OPTIONAL;
  1083. app_config.cfg.srtp_optional_dup_offer = PJ_TRUE;
  1084. cur_acc->srtp_optional_dup_offer = PJ_TRUE;
  1085. }
  1086. cur_acc->use_srtp = app_config.cfg.use_srtp;
  1087. break;
  1088. case OPT_SRTP_SECURE:
  1089. app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
  1090. if (!pj_isdigit(*pj_optarg) ||
  1091. app_config.cfg.srtp_secure_signaling > 2)
  1092. {
  1093. PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
  1094. return -1;
  1095. }
  1096. cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
  1097. break;
  1098. #endif
  1099. case OPT_RTP_PORT:
  1100. cfg->rtp_cfg.port = my_atoi(pj_optarg);
  1101. if (cfg->rtp_cfg.port == 0) {
  1102. enum { START_PORT=4000 };
  1103. unsigned range;
  1104. range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
  1105. cfg->rtp_cfg.port = START_PORT +
  1106. ((pj_rand() % range) & 0xFFFE);
  1107. }
  1108. if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
  1109. PJ_LOG(1,(THIS_FILE,
  1110. "Error: rtp-port argument value "
  1111. "(expecting 1-65535"));
  1112. return -1;
  1113. }
  1114. break;
  1115. case OPT_DIS_CODEC:
  1116. cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
  1117. break;
  1118. case OPT_ADD_CODEC:
  1119. cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
  1120. break;
  1121. /* These options were no longer valid after new pjsua */
  1122. /*
  1123. case OPT_COMPLEXITY:
  1124. cfg->complexity = my_atoi(pj_optarg);
  1125. if (cfg->complexity < 0 || cfg->complexity > 10) {
  1126. PJ_LOG(1,(THIS_FILE,
  1127. "Error: invalid --complexity (expecting 0-10"));
  1128. return -1;
  1129. }
  1130. break;
  1131. */
  1132. case OPT_DURATION:
  1133. cfg->duration = my_atoi(pj_optarg);
  1134. break;
  1135. case OPT_THREAD_CNT:
  1136. cfg->cfg.thread_cnt = my_atoi(pj_optarg);
  1137. if (cfg->cfg.thread_cnt > 128) {
  1138. PJ_LOG(1,(THIS_FILE,
  1139. "Error: invalid --thread-cnt option"));
  1140. return -1;
  1141. }
  1142. break;
  1143. case OPT_PTIME:
  1144. cfg->media_cfg.ptime = my_atoi(pj_optarg);
  1145. if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
  1146. PJ_LOG(1,(THIS_FILE,
  1147. "Error: invalid --ptime option"));
  1148. return -1;
  1149. }
  1150. break;
  1151. case OPT_NO_VAD:
  1152. cfg->media_cfg.no_vad = PJ_TRUE;
  1153. break;
  1154. case OPT_EC_TAIL:
  1155. cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
  1156. if (cfg->media_cfg.ec_tail_len > 1000) {
  1157. PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
  1158. "is too big"));
  1159. return -1;
  1160. }
  1161. break;
  1162. case OPT_EC_OPT:
  1163. cfg->media_cfg.ec_options = my_atoi(pj_optarg);
  1164. break;
  1165. case OPT_QUALITY:
  1166. cfg->media_cfg.quality = my_atoi(pj_optarg);
  1167. if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
  1168. PJ_LOG(1,(THIS_FILE,
  1169. "Error: invalid --quality (expecting 0-10"));
  1170. return -1;
  1171. }
  1172. break;
  1173. case OPT_ILBC_MODE:
  1174. cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
  1175. if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
  1176. PJ_LOG(1,(THIS_FILE,
  1177. "Error: invalid --ilbc-mode (expecting 20 or 30"));
  1178. return -1;
  1179. }
  1180. break;
  1181. case OPT_RX_DROP_PCT:
  1182. cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
  1183. if (cfg->media_cfg.rx_drop_pct > 100) {
  1184. PJ_LOG(1,(THIS_FILE,
  1185. "Error: invalid --rx-drop-pct (expecting <= 100"));
  1186. return -1;
  1187. }
  1188. break;
  1189. case OPT_TX_DROP_PCT:
  1190. cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
  1191. if (cfg->media_cfg.tx_drop_pct > 100) {
  1192. PJ_LOG(1,(THIS_FILE,
  1193. "Error: invalid --tx-drop-pct (expecting <= 100"));
  1194. return -1;
  1195. }
  1196. break;
  1197. case OPT_AUTO_ANSWER:
  1198. cfg->auto_answer = my_atoi(pj_optarg);
  1199. if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
  1200. PJ_LOG(1,(THIS_FILE,
  1201. "Error: invalid code in --auto-answer "
  1202. "(expecting 100-699"));
  1203. return -1;
  1204. }
  1205. break;
  1206. case OPT_MAX_CALLS:
  1207. cfg->cfg.max_calls = my_atoi(pj_optarg);
  1208. if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
  1209. PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
  1210. "compile time limit (PJSUA_MAX_CALLS=%d)",
  1211. PJSUA_MAX_CALLS));
  1212. return -1;
  1213. }
  1214. break;
  1215. #if defined(PJSIP_HAS_TLS_TRANSPORT) && (PJSIP_HAS_TLS_TRANSPORT != 0)
  1216. case OPT_USE_TLS:
  1217. cfg->use_tls = PJ_TRUE;
  1218. break;
  1219. case OPT_TLS_CA_FILE:
  1220. cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
  1221. break;
  1222. case OPT_TLS_CERT_FILE:
  1223. cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
  1224. break;
  1225. case OPT_TLS_PRIV_FILE:
  1226. cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
  1227. break;
  1228. case OPT_TLS_PASSWORD:
  1229. cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
  1230. break;
  1231. case OPT_TLS_VERIFY_SERVER:
  1232. cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
  1233. break;
  1234. case OPT_TLS_VERIFY_CLIENT:
  1235. cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
  1236. cfg->udp_cfg.tls_setting.require_client_cert = PJ_TRUE;
  1237. break;
  1238. case OPT_TLS_NEG_TIMEOUT:
  1239. cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
  1240. break;
  1241. case OPT_TLS_CIPHER:
  1242. {
  1243. pj_ssl_cipher cipher;
  1244. if (pj_ansi_strnicmp(pj_optarg, "0x", 2) == 0) {
  1245. pj_str_t cipher_st = pj_str(pj_optarg + 2);
  1246. cipher = pj_strtoul2(&cipher_st, NULL, 16);
  1247. } else {
  1248. cipher = atoi(pj_optarg);
  1249. }
  1250. if (pj_ssl_cipher_is_supported(cipher)) {
  1251. static pj_ssl_cipher tls_ciphers[128];
  1252. tls_ciphers[cfg->udp_cfg.tls_setting.ciphers_num++] = cipher;
  1253. cfg->udp_cfg.tls_setting.ciphers = tls_ciphers;
  1254. } else {
  1255. pj_ssl_cipher ciphers[128];
  1256. unsigned j, ciphers_cnt;
  1257. ciphers_cnt = PJ_ARRAY_SIZE(ciphers);
  1258. pj_ssl_cipher_get_availables(ciphers, &ciphers_cnt);
  1259. PJ_LOG(1,(THIS_FILE, "Cipher \"%s\" is not supported by "
  1260. "TLS/SSL backend.", pj_optarg));
  1261. printf("Available TLS/SSL ciphers (%d):\n", ciphers_cnt);
  1262. for (j=0; j<ciphers_cnt; ++j)
  1263. printf("- 0x%06X: %s\n", ciphers[j], pj_ssl_cipher_name(ciphers[j]));
  1264. return -1;
  1265. }
  1266. }
  1267. break;
  1268. #endif /* PJSIP_HAS_TLS_TRANSPORT */
  1269. case OPT_CAPTURE_DEV:
  1270. cfg->capture_dev = atoi(pj_optarg);
  1271. break;
  1272. case OPT_PLAYBACK_DEV:
  1273. cfg->playback_dev = atoi(pj_optarg);
  1274. break;
  1275. case OPT_STDOUT_REFRESH:
  1276. stdout_refresh = atoi(pj_optarg);
  1277. break;
  1278. case OPT_STDOUT_REFRESH_TEXT:
  1279. stdout_refresh_text = pj_optarg;
  1280. break;
  1281. #ifdef _IONBF
  1282. case OPT_STDOUT_NO_BUF:
  1283. setvbuf(stdout, NULL, _IONBF, 0);
  1284. break;
  1285. #endif
  1286. case OPT_CAPTURE_LAT:
  1287. cfg->capture_lat = atoi(pj_optarg);
  1288. break;
  1289. case OPT_PLAYBACK_LAT:
  1290. cfg->playback_lat = atoi(pj_optarg);
  1291. break;
  1292. case OPT_SND_AUTO_CLOSE:
  1293. cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
  1294. break;
  1295. case OPT_NO_TONES:
  1296. cfg->no_tones = PJ_TRUE;
  1297. break;
  1298. case OPT_JB_MAX_SIZE:
  1299. cfg->media_cfg.jb_max = atoi(pj_optarg);
  1300. break;
  1301. #if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6
  1302. case OPT_IPV6:
  1303. cfg->ipv6 = PJ_TRUE;
  1304. break;
  1305. #endif
  1306. case OPT_QOS:
  1307. cfg->enable_qos = PJ_TRUE;
  1308. /* Set RTP traffic type to Voice */
  1309. cfg->rtp_cfg.qos_type = PJ_QOS_TYPE_VOICE;
  1310. /* Directly apply DSCP value to SIP traffic. Say lets
  1311. * set it to CS3 (DSCP 011000). Note that this will not
  1312. * work on all platforms.
  1313. */
  1314. cfg->udp_cfg.qos_params.flags = PJ_QOS_PARAM_HAS_DSCP;
  1315. cfg->udp_cfg.qos_params.dscp_val = 0x18;
  1316. break;
  1317. case OPT_VIDEO:
  1318. cfg->vid.vid_cnt = 1;
  1319. cfg->vid.in_auto_show = PJ_TRUE;
  1320. cfg->vid.out_auto_transmit = PJ_TRUE;
  1321. break;
  1322. case OPT_EXTRA_AUDIO:
  1323. cfg->aud_cnt++;
  1324. break;
  1325. case OPT_VCAPTURE_DEV:
  1326. cfg->vid.vcapture_dev = atoi(pj_optarg);
  1327. cur_acc->vid_cap_dev = cfg->vid.vcapture_dev;
  1328. break;
  1329. case OPT_VRENDER_DEV:
  1330. cfg->vid.vrender_dev = atoi(pj_optarg);
  1331. cur_acc->vid_rend_dev = cfg->vid.vrender_dev;
  1332. break;
  1333. case OPT_PLAY_AVI:
  1334. if (app_config.avi_cnt >= MAX_AVI) {
  1335. PJ_LOG(1,(THIS_FILE, "Too many AVIs"));
  1336. return -1;
  1337. }
  1338. app_config.avi[app_config.avi_cnt++].path = pj_str(pj_optarg);
  1339. break;
  1340. case OPT_AUTO_PLAY_AVI:
  1341. app_config.avi_auto_play = PJ_TRUE;
  1342. break;
  1343. default:
  1344. PJ_LOG(1,(THIS_FILE,
  1345. "Argument \"%s\" is not valid. Use --help to see help",
  1346. argv[pj_optind-1]));
  1347. return -1;
  1348. }
  1349. }
  1350. if (pj_optind != argc) {
  1351. pj_str_t uri_arg;
  1352. if (pjsua_verify_url(argv[pj_optind]) != PJ_SUCCESS) {
  1353. PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
  1354. return -1;
  1355. }
  1356. uri_arg = pj_str(argv[pj_optind]);
  1357. if (uri_to_call)
  1358. *uri_to_call = uri_arg;
  1359. pj_optind++;
  1360. /* Add URI to call to buddy list if it's not already there */
  1361. for (i=0; i<cfg->buddy_cnt; ++i) {
  1362. if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
  1363. break;
  1364. }
  1365. if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
  1366. cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
  1367. }
  1368. } else {
  1369. if (uri_to_call)
  1370. uri_to_call->slen = 0;
  1371. }
  1372. if (pj_optind != argc) {
  1373. PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
  1374. return PJ_EINVAL;
  1375. }
  1376. if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
  1377. cfg->acc_cnt++;
  1378. for (i=0; i<cfg->acc_cnt; ++i) {
  1379. pjsua_acc_config *acfg = &cfg->acc_cfg[i];
  1380. if (acfg->cred_info[acfg->cred_count].username.slen)
  1381. {
  1382. acfg->cred_count++;
  1383. }
  1384. if (acfg->ice_cfg.enable_ice) {
  1385. acfg->ice_cfg_use = PJSUA_ICE_CONFIG_USE_CUSTOM;
  1386. }
  1387. if (acfg->turn_cfg.enable_turn) {
  1388. acfg->turn_cfg_use = PJSUA_TURN_CONFIG_USE_CUSTOM;
  1389. }
  1390. /* When IMS mode is enabled for the account, verify that settings
  1391. * are okay.
  1392. */
  1393. /* For now we check if IMS mode is activated by looking if
  1394. * initial_auth is set.
  1395. */
  1396. if (acfg->auth_pref.initial_auth && acfg->cred_count) {
  1397. /* Realm must point to the real domain */
  1398. if (*acfg->cred_info[0].realm.ptr=='*') {
  1399. PJ_LOG(1,(THIS_FILE,
  1400. "Error: cannot use '*' as realm with IMS"));
  1401. return PJ_EINVAL;
  1402. }
  1403. /* Username for authentication must be in a@b format */
  1404. if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
  1405. PJ_LOG(1,(THIS_FILE,
  1406. "Error: Username for authentication must "
  1407. "be in user@domain format with IMS"));
  1408. return PJ_EINVAL;
  1409. }
  1410. }
  1411. }
  1412. return PJ_SUCCESS;
  1413. }
  1414. /*
  1415. * Save account settings
  1416. */
  1417. static void write_account_settings(int acc_index, pj_str_t *result)
  1418. {
  1419. unsigned i;
  1420. char line[128];
  1421. pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
  1422. pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
  1423. pj_strcat2(result, line);
  1424. /* Identity */
  1425. if (acc_cfg->id.slen) {
  1426. pj_ansi_sprintf(line, "--id %.*s\n",
  1427. (int)acc_cfg->id.slen,
  1428. acc_cfg->id.ptr);
  1429. pj_strcat2(result, line);
  1430. }
  1431. /* Registrar server */
  1432. if (acc_cfg->reg_uri.slen) {
  1433. pj_ansi_sprintf(line, "--registrar %.*s\n",
  1434. (int)acc_cfg->reg_uri.slen,
  1435. acc_cfg->reg_uri.ptr);
  1436. pj_strcat2(result, line);
  1437. pj_ansi_sprintf(line, "--reg-timeout %u\n",
  1438. acc_cfg->reg_timeout);
  1439. pj_strcat2(result, line);
  1440. }
  1441. /* Contact */
  1442. if (acc_cfg->force_contact.slen) {
  1443. pj_ansi_sprintf(line, "--contact %.*s\n",
  1444. (int)acc_cfg->force_contact.slen,
  1445. acc_cfg->force_contact.ptr);
  1446. pj_strcat2(result, line);
  1447. }
  1448. /* Contact header parameters */
  1449. if (acc_cfg->contact_params.slen) {
  1450. pj_ansi_sprintf(line, "--contact-params %.*s\n",
  1451. (int)acc_cfg->contact_params.slen,
  1452. acc_cfg->contact_params.ptr);
  1453. pj_strcat2(result, line);
  1454. }
  1455. /* Contact URI parameters */
  1456. if (acc_cfg->contact_uri_params.slen) {
  1457. pj_ansi_sprintf(line, "--contact-uri-params %.*s\n",
  1458. (int)acc_cfg->contact_uri_params.slen,
  1459. acc_cfg->contact_uri_params.ptr);
  1460. pj_strcat2(result, line);
  1461. }
  1462. /* */
  1463. if (acc_cfg->allow_contact_rewrite!=1)
  1464. {
  1465. pj_ansi_sprintf(line, "--auto-update-nat %i\n",
  1466. (int)acc_cfg->allow_contact_rewrite);
  1467. pj_strcat2(result, line);
  1468. }
  1469. #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
  1470. /* SRTP */
  1471. if (acc_cfg->use_srtp) {
  1472. int use_srtp = (int)acc_cfg->use_srtp;
  1473. if (use_srtp == PJMEDIA_SRTP_OPTIONAL &&
  1474. acc_cfg->srtp_optional_dup_offer)
  1475. {
  1476. use_srtp = 3;
  1477. }
  1478. pj_ansi_sprintf(line, "--use-srtp %i\n", use_srtp);
  1479. pj_strcat2(result, line);
  1480. }
  1481. if (acc_cfg->srtp_secure_signaling !=
  1482. PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
  1483. {
  1484. pj_ansi_sprintf(line, "--srtp-secure %d\n",
  1485. acc_cfg->srtp_secure_signaling);
  1486. pj_strcat2(result, line);
  1487. }
  1488. #endif
  1489. /* Proxy */
  1490. for (i=0; i<acc_cfg->proxy_cnt; ++i) {
  1491. pj_ansi_sprintf(line, "--proxy %.*s\n",
  1492. (int)acc_cfg->proxy[i].slen,
  1493. acc_cfg->proxy[i].ptr);
  1494. pj_strcat2(result, line);
  1495. }
  1496. /* Credentials */
  1497. for (i=0; i<acc_cfg->cred_count; ++i) {
  1498. if (acc_cfg->cred_info[i].realm.slen) {
  1499. pj_ansi_sprintf(line, "--realm %.*s\n",
  1500. (int)acc_cfg->cred_info[i].realm.slen,
  1501. acc_cfg->cred_info[i].realm.ptr);
  1502. pj_strcat2(result, line);
  1503. }
  1504. if (acc_cfg->cred_info[i].username.slen) {
  1505. pj_ansi_sprintf(line, "--username %.*s\n",
  1506. (int)acc_cfg->cred_info[i].username.slen,
  1507. acc_cfg->cred_info[i].username.ptr);
  1508. pj_strcat2(result, line);
  1509. }
  1510. if (acc_cfg->cred_info[i].data.slen) {
  1511. pj_ansi_sprintf(line, "--password %.*s\n",
  1512. (int)acc_cfg->cred_info[i].data.slen,
  1513. acc_cfg->cred_info[i].data.ptr);
  1514. pj_strcat2(result, line);
  1515. }
  1516. if (i != acc_cfg->cred_count - 1)
  1517. pj_strcat2(result, "--next-cred\n");
  1518. }
  1519. /* reg-use-proxy */
  1520. if (acc_cfg->reg_use_proxy != 3) {
  1521. pj_ansi_sprintf(line, "--reg-use-proxy %d\n",
  1522. acc_cfg->reg_use_proxy);
  1523. pj_strcat2(result, line);
  1524. }
  1525. /* rereg-delay */
  1526. if (acc_cfg->reg_retry_interval != PJSUA_REG_RETRY_INTERVAL) {
  1527. pj_ansi_sprintf(line, "--rereg-delay %d\n",
  1528. acc_cfg->reg_retry_interval);
  1529. pj_strcat2(result, line);
  1530. }
  1531. /* 100rel extension */
  1532. if (acc_cfg->require_100rel) {
  1533. pj_strcat2(result, "--use-100rel\n");
  1534. }
  1535. /* Session Timer extension */
  1536. if (acc_cfg->use_timer) {
  1537. pj_ansi_sprintf(line, "--use-timer %d\n",
  1538. acc_cfg->use_timer);
  1539. pj_strcat2(result, line);
  1540. }
  1541. if (acc_cfg->timer_setting.min_se != 90) {
  1542. pj_ansi_sprintf(line, "--timer-min-se %d\n",
  1543. acc_cfg->timer_setting.min_se);
  1544. pj_strcat2(result, line);
  1545. }
  1546. if (acc_cfg->timer_setting.sess_expires != PJSIP_SESS_TIMER_DEF_SE) {
  1547. pj_ansi_sprintf(line, "--timer-se %d\n",
  1548. acc_cfg->timer_setting.sess_expires);
  1549. pj_strcat2(result, line);
  1550. }
  1551. /* Publish */
  1552. if (acc_cfg->publish_enabled)
  1553. pj_strcat2(result, "--publish\n");
  1554. /* MWI */
  1555. if (acc_cfg->mwi_enabled)
  1556. pj_strcat2(result, "--mwi\n");
  1557. if (acc_cfg->sip_stun_use != PJSUA_STUN_USE_DEFAULT ||
  1558. acc_cfg->media_stun_use != PJSUA_STUN_USE_DEFAULT)
  1559. {
  1560. pj_strcat2(result, "--disable-stun\n");
  1561. }
  1562. /* Media Transport*/
  1563. if (acc_cfg->ice_cfg.enable_ice)
  1564. pj_strcat2(result, "--use-ice\n");
  1565. if (acc_cfg->ice_cfg.ice_opt.aggressive == PJ_FALSE)
  1566. pj_strcat2(result, "--ice-regular\n");
  1567. if (acc_cfg->turn_cfg.enable_turn)
  1568. pj_strcat2(result, "--use-turn\n");
  1569. if (acc_cfg->ice_cfg.ice_max_host_cands >= 0) {
  1570. pj_ansi_sprintf(line, "--ice_max_host_cands %d\n",
  1571. acc_cfg->ice_cfg.ice_max_host_cands);
  1572. pj_strcat2(result, line);
  1573. }
  1574. if (acc_cfg->ice_cfg.ice_no_rtcp)
  1575. pj_strcat2(result, "--ice-no-rtcp\n");
  1576. if (acc_cfg->turn_cfg.turn_server.slen) {
  1577. pj_ansi_sprintf(line, "--turn-srv %.*s\n",
  1578. (int)acc_cfg->turn_cfg.turn_server.slen,
  1579. acc_cfg->turn_cfg.turn_server.ptr);
  1580. pj_strcat2(result, line);
  1581. }
  1582. if (acc_cfg->turn_cfg.turn_conn_type == PJ_TURN_TP_TCP)
  1583. pj_strcat2(result, "--turn-tcp\n");
  1584. if (acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.slen) {
  1585. pj_ansi_sprintf(line, "--turn-user %.*s\n",
  1586. (int)acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.slen,
  1587. acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.ptr);
  1588. pj_strcat2(result, line);
  1589. }
  1590. if (acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.slen) {
  1591. pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
  1592. (int)acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.slen,
  1593. acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.ptr);
  1594. pj_strcat2(result, line);
  1595. }
  1596. }
  1597. /*
  1598. * Write settings.
  1599. */
  1600. static int write_settings(const struct app_config *config,
  1601. char *buf, pj_size_t max)
  1602. {
  1603. unsigned acc_index;
  1604. unsigned i;
  1605. pj_str_t cfg;
  1606. char line[128];
  1607. extern pj_bool_t pjsip_use_compact_form;
  1608. PJ_UNUSED_ARG(max);
  1609. cfg.ptr = buf;
  1610. cfg.slen = 0;
  1611. /* Logging. */
  1612. pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
  1613. pj_ansi_sprintf(line, "--log-level %d\n",
  1614. config->log_cfg.level);
  1615. pj_strcat2(&cfg, line);
  1616. pj_ansi_sprintf(line, "--app-log-level %d\n",
  1617. config->log_cfg.console_level);
  1618. pj_strcat2(&cfg, line);
  1619. if (config->log_cfg.log_filename.slen) {
  1620. pj_ansi_sprintf(line, "--log-file %.*s\n",
  1621. (int)config->log_cfg.log_filename.slen,
  1622. config->log_cfg.log_filename.ptr);
  1623. pj_strcat2(&cfg, line);
  1624. }
  1625. if (config->log_cfg.log_file_flags & PJ_O_APPEND) {
  1626. pj_strcat2(&cfg, "--log-append\n");
  1627. }
  1628. /* Save account settings. */
  1629. for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
  1630. write_account_settings(acc_index, &cfg);
  1631. if (acc_index < config->acc_cnt-1)
  1632. pj_strcat2(&cfg, "--next-account\n");
  1633. }
  1634. pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
  1635. /* Nameservers */
  1636. for (i=0; i<config->cfg.nameserver_count; ++i) {
  1637. pj_ansi_sprintf(line, "--nameserver %.*s\n",
  1638. (int)config->cfg.nameserver[i].slen,
  1639. config->cfg.nameserver[i].ptr);
  1640. pj_strcat2(&cfg, line);
  1641. }
  1642. /* Outbound proxy */
  1643. for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
  1644. pj_ansi_sprintf(line, "--outbound %.*s\n",
  1645. (int)config->cfg.outbound_proxy[i].slen,
  1646. config->cfg.outbound_proxy[i].ptr);
  1647. pj_strcat2(&cfg, line);
  1648. }
  1649. /* Transport options */
  1650. if (config->ipv6) {
  1651. pj_strcat2(&cfg, "--ipv6\n");
  1652. }
  1653. if (config->enable_qos) {
  1654. pj_strcat2(&cfg, "--set-qos\n");
  1655. }
  1656. /* UDP Transport. */
  1657. pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
  1658. pj_strcat2(&cfg, line);
  1659. /* IP address, if any. */
  1660. if (config->udp_cfg.public_addr.slen) {
  1661. pj_ansi_sprintf(line, "--ip-addr %.*s\n",
  1662. (int)config->udp_cfg.public_addr.slen,
  1663. config->udp_cfg.public_addr.ptr);
  1664. pj_strcat2(&cfg, line);
  1665. }
  1666. /* Bound IP address, if any. */
  1667. if (config->udp_cfg.bound_addr.slen) {
  1668. pj_ansi_sprintf(line, "--bound-addr %.*s\n",
  1669. (int)config->udp_cfg.bound_addr.slen,
  1670. config->udp_cfg.bound_addr.ptr);
  1671. pj_strcat2(&cfg, line);
  1672. }
  1673. /* No TCP ? */
  1674. if (config->no_tcp) {
  1675. pj_strcat2(&cfg, "--no-tcp\n");
  1676. }
  1677. /* No UDP ? */
  1678. if (config->no_udp) {
  1679. pj_strcat2(&cfg, "--no-udp\n");
  1680. }
  1681. /* STUN */
  1682. for (i=0; i<config->cfg.stun_srv_cnt; ++i) {
  1683. pj_ansi_sprintf(line, "--stun-srv %.*s\n",
  1684. (int)config->cfg.stun_srv[i].slen,
  1685. config->cfg.stun_srv[i].ptr);
  1686. pj_strcat2(&cfg, line);
  1687. }
  1688. #if defined(PJSIP_HAS_TLS_TRANSPORT) && (PJSIP_HAS_TLS_TRANSPORT != 0)
  1689. /* TLS */
  1690. if (config->use_tls)
  1691. pj_strcat2(&cfg, "--use-tls\n");
  1692. if (config->udp_cfg.tls_setting.ca_list_file.slen) {
  1693. pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
  1694. (int)config->udp_cfg.tls_setting.ca_list_file.slen,
  1695. config->udp_cfg.tls_setting.ca_list_file.ptr);
  1696. pj_strcat2(&cfg, line);
  1697. }
  1698. if (config->udp_cfg.tls_setting.cert_file.slen) {
  1699. pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
  1700. (int)config->udp_cfg.tls_setting.cert_file.slen,
  1701. config->udp_cfg.tls_setting.cert_file.ptr);
  1702. pj_strcat2(&cfg, line);
  1703. }
  1704. if (config->udp_cfg.tls_setting.privkey_file.slen) {
  1705. pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
  1706. (int)config->udp_cfg.tls_setting.privkey_file.slen,
  1707. config->udp_cfg.tls_setting.privkey_file.ptr);
  1708. pj_strcat2(&cfg, line);
  1709. }
  1710. if (config->udp_cfg.tls_setting.password.slen) {
  1711. pj_ansi_sprintf(line, "--tls-password %.*s\n",
  1712. (int)config->udp_cfg.tls_setting.password.slen,
  1713. config->udp_cfg.tls_setting.password.ptr);
  1714. pj_strcat2(&cfg, line);
  1715. }
  1716. if (config->udp_cfg.tls_setting.verify_server)
  1717. pj_strcat2(&cfg, "--tls-verify-server\n");
  1718. if (config->udp_cfg.tls_setting.verify_client)
  1719. pj_strcat2(&cfg, "--tls-verify-client\n");
  1720. if (config->udp_cfg.tls_setting.timeout.sec) {
  1721. pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
  1722. (int)config->udp_cfg.tls_setting.timeout.sec);
  1723. pj_strcat2(&cfg, line);
  1724. }
  1725. for (i=0; i<config->udp_cfg.tls_setting.ciphers_num; ++i) {
  1726. pj_ansi_sprintf(line, "--tls-cipher 0x%06X # %s\n",
  1727. config->udp_cfg.tls_setting.ciphers[i],
  1728. pj_ssl_cipher_name(config->udp_cfg.tls_setting.ciphers[i]));
  1729. pj_strcat2(&cfg, line);
  1730. }
  1731. #endif
  1732. pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
  1733. /* Video & extra audio */
  1734. for (i=0; i<config->vid.vid_cnt; ++i) {
  1735. pj_strcat2(&cfg, "--video\n");
  1736. }
  1737. for (i=1; i<config->aud_cnt; ++i) {
  1738. pj_strcat2(&cfg, "--extra-audio\n");
  1739. }
  1740. /* SRTP */
  1741. #if PJMEDIA_HAS_SRTP
  1742. if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
  1743. int use_srtp = (int)app_config.cfg.use_srtp;
  1744. if (use_srtp == PJMEDIA_SRTP_OPTIONAL &&
  1745. app_config.cfg.srtp_optional_dup_offer)
  1746. {
  1747. use_srtp = 3;
  1748. }
  1749. pj_ansi_sprintf(line, "--use-srtp %d\n", use_srtp);
  1750. pj_strcat2(&cfg, line);
  1751. }
  1752. if (app_config.cfg.srtp_secure_signaling !=
  1753. PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
  1754. {
  1755. pj_ansi_sprintf(line, "--srtp-secure %d\n",
  1756. app_config.cfg.srtp_secure_signaling);
  1757. pj_strcat2(&cfg, line);
  1758. }
  1759. #endif
  1760. /* Media */
  1761. if (config->null_audio)
  1762. pj_strcat2(&cfg, "--null-audio\n");
  1763. if (config->auto_play)
  1764. pj_strcat2(&cfg, "--auto-play\n");
  1765. if (config->auto_loop)
  1766. pj_strcat2(&cfg, "--auto-loop\n");
  1767. if (config->auto_conf)
  1768. pj_strcat2(&cfg, "--auto-conf\n");
  1769. for (i=0; i<config->wav_count; ++i) {
  1770. pj_ansi_sprintf(line, "--play-file %s\n",
  1771. config->wav_files[i].ptr);
  1772. pj_strcat2(&cfg, line);
  1773. }
  1774. for (i=0; i<config->tone_count; ++i) {
  1775. pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
  1776. config->tones[i].freq1, config->tones[i].freq2,
  1777. config->tones[i].on_msec, config->tones[i].off_msec);
  1778. pj_strcat2(&cfg, line);
  1779. }
  1780. if (config->rec_file.slen) {
  1781. pj_ansi_sprintf(line, "--rec-file %s\n",
  1782. config->rec_file.ptr);
  1783. pj_strcat2(&cfg, line);
  1784. }
  1785. if (config->auto_rec)
  1786. pj_strcat2(&cfg, "--auto-rec\n");
  1787. if (config->capture_dev != PJSUA_INVALID_ID) {
  1788. pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
  1789. pj_strcat2(&cfg, line);
  1790. }
  1791. if (config->playback_dev != PJSUA_INVALID_ID) {
  1792. pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
  1793. pj_strcat2(&cfg, line);
  1794. }
  1795. if (config->media_cfg.snd_auto_close_time != -1) {
  1796. pj_ansi_sprintf(line, "--snd-auto-close %d\n",
  1797. config->media_cfg.snd_auto_close_time);
  1798. pj_strcat2(&cfg, line);
  1799. }
  1800. if (config->no_tones) {
  1801. pj_strcat2(&cfg, "--no-tones\n");
  1802. }
  1803. if (config->media_cfg.jb_max != -1) {
  1804. pj_ansi_sprintf(line, "--jb-max-size %d\n",
  1805. config->media_cfg.jb_max);
  1806. pj_strcat2(&cfg, line);
  1807. }
  1808. /* Sound device latency */
  1809. if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
  1810. pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
  1811. pj_strcat2(&cfg, line);
  1812. }
  1813. if (config->playback_lat != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
  1814. pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
  1815. pj_strcat2(&cfg, line);
  1816. }
  1817. /* Media clock rate. */
  1818. if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
  1819. pj_ansi_sprintf(line, "--clock-rate %d\n",
  1820. config->media_cfg.clock_rate);
  1821. pj_strcat2(&cfg, line);
  1822. } else {
  1823. pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
  1824. config->media_cfg.clock_rate);
  1825. pj_strcat2(&cfg, line);
  1826. }
  1827. if (config->media_cfg.snd_clock_rate &&
  1828. config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
  1829. {
  1830. pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
  1831. config->media_cfg.snd_clock_rate);
  1832. pj_strcat2(&cfg, line);
  1833. }
  1834. /* Stereo mode. */
  1835. if (config->media_cfg.channel_count == 2) {
  1836. pj_ansi_sprintf(line, "--stereo\n");
  1837. pj_strcat2(&cfg, line);
  1838. }
  1839. /* quality */
  1840. if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
  1841. pj_ansi_sprintf(line, "--quality %d\n",
  1842. config->media_cfg.quality);
  1843. pj_strcat2(&cfg, line);
  1844. } else {
  1845. pj_ansi_sprintf(line, "#using default --quality %d\n",
  1846. config->media_cfg.quality);
  1847. pj_strcat2(&cfg, line);
  1848. }
  1849. if (config->vid.vcapture_dev != PJMEDIA_VID_DEFAULT_CAPTURE_DEV) {
  1850. pj_ansi_sprintf(line, "--vcapture-dev %d\n", config->vid.vcapture_dev);
  1851. pj_strcat2(&cfg, line);
  1852. }
  1853. if (config->vid.vrender_dev != PJMEDIA_VID_DEFAULT_RENDER_DEV) {
  1854. pj_ansi_sprintf(line, "--vrender-dev %d\n", config->vid.vrender_dev);
  1855. pj_strcat2(&cfg, line);
  1856. }
  1857. for (i=0; i<config->avi_cnt; ++i) {
  1858. pj_ansi_sprintf(line, "--play-avi %s\n", config->avi[i].path.ptr);
  1859. pj_strcat2(&cfg, line);
  1860. }
  1861. if (config->avi_auto_play) {
  1862. pj_ansi_sprintf(line, "--auto-play-avi\n");
  1863. pj_strcat2(&cfg, line);
  1864. }
  1865. /* ptime */
  1866. if (config->media_cfg.ptime) {
  1867. pj_ansi_sprintf(line, "--ptime %d\n",
  1868. config->media_cfg.ptime);
  1869. pj_strcat2(&cfg, line);
  1870. }
  1871. /* no-vad */
  1872. if (config->media_cfg.no_vad) {
  1873. pj_strcat2(&cfg, "--no-vad\n");
  1874. }
  1875. /* ec-tail */
  1876. if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
  1877. pj_ansi_sprintf(line, "--ec-tail %d\n",
  1878. config->media_cfg.ec_tail_len);
  1879. pj_strcat2(&cfg, line);
  1880. } else {
  1881. pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
  1882. config->media_cfg.ec_tail_len);
  1883. pj_strcat2(&cfg, line);
  1884. }
  1885. /* ec-opt */
  1886. if (config->media_cfg.ec_options != 0) {
  1887. pj_ansi_sprintf(line, "--ec-opt %d\n",
  1888. config->media_cfg.ec_options);
  1889. pj_strcat2(&cfg, line);
  1890. }
  1891. /* ilbc-mode */
  1892. if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
  1893. pj_ansi_sprintf(line, "--ilbc-mode %d\n",
  1894. config->media_cfg.ilbc_mode);
  1895. pj_strcat2(&cfg, line);
  1896. } else {
  1897. pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
  1898. config->media_cfg.ilbc_mode);
  1899. pj_strcat2(&cfg, line);
  1900. }
  1901. /* RTP drop */
  1902. if (config->media_cfg.tx_drop_pct) {
  1903. pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
  1904. config->media_cfg.tx_drop_pct);
  1905. pj_strcat2(&cfg, line);
  1906. }
  1907. if (config->media_cfg.rx_drop_pct) {
  1908. pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
  1909. config->media_cfg.rx_drop_pct);
  1910. pj_strcat2(&cfg, line);
  1911. }
  1912. /* Start RTP port. */
  1913. pj_ansi_sprintf(line, "--rtp-port %d\n",
  1914. config->rtp_cfg.port);
  1915. pj_strcat2(&cfg, line);
  1916. /* Disable codec */
  1917. for (i=0; i<config->codec_dis_cnt; ++i) {
  1918. pj_ansi_sprintf(line, "--dis-codec %s\n",
  1919. config->codec_dis[i].ptr);
  1920. pj_strcat2(&cfg, line);
  1921. }
  1922. /* Add codec. */
  1923. for (i=0; i<config->codec_cnt; ++i) {
  1924. pj_ansi_sprintf(line, "--add-codec %s\n",
  1925. config->codec_arg[i].ptr);
  1926. pj_strcat2(&cfg, line);
  1927. }
  1928. pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
  1929. /* Auto-answer. */
  1930. if (config->auto_answer != 0) {
  1931. pj_ansi_sprintf(line, "--auto-answer %d\n",
  1932. config->auto_answer);
  1933. pj_strcat2(&cfg, line);
  1934. }
  1935. /* accept-redirect */
  1936. if (config->redir_op != PJSIP_REDIRECT_ACCEPT) {
  1937. pj_ansi_sprintf(line, "--accept-redirect %d\n",
  1938. config->redir_op);
  1939. pj_strcat2(&cfg, line);
  1940. }
  1941. /* Max calls. */
  1942. pj_ansi_sprintf(line, "--max-calls %d\n",
  1943. config->cfg.max_calls);
  1944. pj_strcat2(&cfg, line);
  1945. /* Uas-duration. */
  1946. if (config->duration != NO_LIMIT) {
  1947. pj_ansi_sprintf(line, "--duration %d\n",
  1948. config->duration);
  1949. pj_strcat2(&cfg, line);
  1950. }
  1951. /* norefersub ? */
  1952. if (config->no_refersub) {
  1953. pj_strcat2(&cfg, "--norefersub\n");
  1954. }
  1955. if (pjsip_use_compact_form)
  1956. {
  1957. pj_strcat2(&cfg, "--use-compact-form\n");
  1958. }
  1959. if (!config->cfg.force_lr) {
  1960. pj_strcat2(&cfg, "--no-force-lr\n");
  1961. }
  1962. pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
  1963. /* Add buddies. */
  1964. for (i=0; i<config->buddy_cnt; ++i) {
  1965. pj_ansi_sprintf(line, "--add-buddy %.*s\n",
  1966. (int)config->buddy_cfg[i].uri.slen,
  1967. config->buddy_cfg[i].uri.ptr);
  1968. pj_strcat2(&cfg, line);
  1969. }
  1970. /* SIP extensions. */
  1971. pj_strcat2(&cfg, "\n#\n# SIP extensions:\n#\n");
  1972. /* 100rel extension */
  1973. if (config->cfg.require_100rel) {
  1974. pj_strcat2(&cfg, "--use-100rel\n");
  1975. }
  1976. /* Session Timer extension */
  1977. if (config->cfg.use_timer) {
  1978. pj_ansi_sprintf(line, "--use-timer %d\n",
  1979. config->cfg.use_timer);
  1980. pj_strcat2(&cfg, line);
  1981. }
  1982. if (config->cfg.timer_setting.min_se != 90) {
  1983. pj_ansi_sprintf(line, "--timer-min-se %d\n",
  1984. config->cfg.timer_setting.min_se);
  1985. pj_strcat2(&cfg, line);
  1986. }
  1987. if (config->cfg.timer_setting.sess_expires != PJSIP_SESS_TIMER_DEF_SE) {
  1988. pj_ansi_sprintf(line, "--timer-se %d\n",
  1989. config->cfg.timer_setting.sess_expires);
  1990. pj_strcat2(&cfg, line);
  1991. }
  1992. *(cfg.ptr + cfg.slen) = '\0';
  1993. return cfg.slen;
  1994. }
  1995. /*
  1996. * Dump application states.
  1997. */
  1998. static void app_dump(pj_bool_t detail)
  1999. {
  2000. pjsua_dump(detail);
  2001. }
  2002. /*
  2003. * Print log of call states. Since call states may be too long for logger,
  2004. * printing it is a bit tricky, it should be printed part by part as long
  2005. * as the logger can accept.
  2006. */
  2007. static void log_call_dump(int call_id)
  2008. {
  2009. unsigned call_dump_len;
  2010. unsigned part_len;
  2011. unsigned part_idx;
  2012. unsigned log_decor;
  2013. pjsua_call_dump(call_id, PJ_TRUE, some_buf,
  2014. sizeof(some_buf), " ");
  2015. call_dump_len = strlen(some_buf);
  2016. log_decor = pj_log_get_decor();
  2017. pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
  2018. PJ_LOG(3,(THIS_FILE, "\n"));
  2019. pj_log_set_decor(0);
  2020. part_idx = 0;
  2021. part_len = PJ_LOG_MAX_SIZE-80;
  2022. while (part_idx < call_dump_len) {
  2023. char p_orig, *p;
  2024. p = &some_buf[part_idx];
  2025. if (part_idx + part_len > call_dump_len)
  2026. part_len = call_dump_len - part_idx;
  2027. p_orig = p[part_len];
  2028. p[part_len] = '\0';
  2029. PJ_LOG(3,(THIS_FILE, "%s", p));
  2030. p[part_len] = p_orig;
  2031. part_idx += part_len;
  2032. }
  2033. pj_log_set_decor(log_decor);
  2034. }
  2035. /*****************************************************************************
  2036. * Console application
  2037. */
  2038. static void ringback_start(pjsua_call_id call_id)
  2039. {
  2040. if (app_config.no_tones)
  2041. return;
  2042. if (app_config.call_data[call_id].ringback_on)
  2043. return;
  2044. app_config.call_data[call_id].ringback_on = PJ_TRUE;
  2045. if (++app_config.ringback_cnt==1 &&
  2046. app_config.ringback_slot!=PJSUA_INVALID_ID)
  2047. {
  2048. pjsua_conf_connect(app_config.ringback_slot, 0);
  2049. }
  2050. }
  2051. static void ring_stop(pjsua_call_id call_id)
  2052. {
  2053. if (app_config.no_tones)
  2054. return;
  2055. if (app_config.call_data[call_id].ringback_on) {
  2056. app_config.call_data[call_id].ringback_on = PJ_FALSE;
  2057. pj_assert(app_config.ringback_cnt>0);
  2058. if (--app_config.ringback_cnt == 0 &&
  2059. app_config.ringback_slot!=PJSUA_INVALID_ID)
  2060. {
  2061. pjsua_conf_disconnect(app_config.ringback_slot, 0);
  2062. pjmedia_tonegen_rewind(app_config.ringback_port);
  2063. }
  2064. }
  2065. if (app_config.call_data[call_id].ring_on) {
  2066. app_config.call_data[call_id].ring_on = PJ_FALSE;
  2067. pj_assert(app_config.ring_cnt>0);
  2068. if (--app_config.ring_cnt == 0 &&
  2069. app_config.ring_slot!=PJSUA_INVALID_ID)
  2070. {
  2071. pjsua_conf_disconnect(app_config.ring_slot, 0);
  2072. pjmedia_tonegen_rewind(app_config.ring_port);
  2073. }
  2074. }
  2075. }
  2076. static void ring_start(pjsua_call_id call_id)
  2077. {
  2078. if (app_config.no_tones)
  2079. return;
  2080. if (app_config.call_data[call_id].ring_on)
  2081. return;
  2082. app_config.call_data[call_id].ring_on = PJ_TRUE;
  2083. if (++app_config.ring_cnt==1 &&
  2084. app_config.ring_slot!=PJSUA_INVALID_ID)
  2085. {
  2086. pjsua_conf_connect(app_config.ring_slot, 0);
  2087. }
  2088. }
  2089. #ifdef HAVE_MULTIPART_TEST
  2090. /*
  2091. * Enable multipart in msg_data and add a dummy body into the
  2092. * multipart bodies.
  2093. */
  2094. static void add_multipart(pjsua_msg_data *msg_data)
  2095. {
  2096. static pjsip_multipart_part *alt_part;
  2097. if (!alt_part) {
  2098. pj_str_t type, subtype, content;
  2099. alt_part = pjsip_multipart_create_part(app_config.pool);
  2100. type = pj_str("text");
  2101. subtype = pj_str("plain");
  2102. content = pj_str("Sample text body of a multipart bodies");
  2103. alt_part->body = pjsip_msg_body_create(app_config.pool, &type,
  2104. &subtype, &content);
  2105. }
  2106. msg_data->multipart_ctype.type = pj_str("multipart");
  2107. msg_data->multipart_ctype.subtype = pj_str("mixed");
  2108. pj_list_push_back(&msg_data->multipart_parts, alt_part);
  2109. }
  2110. # define TEST_MULTIPART(msg_data) add_multipart(msg_data)
  2111. #else
  2112. # define TEST_MULTIPART(msg_data)
  2113. #endif
  2114. /*
  2115. * Find next call when current call is disconnected or when user
  2116. * press ']'
  2117. */
  2118. static pj_bool_t find_next_call(void)
  2119. {
  2120. int i, max;
  2121. max = pjsua_call_get_max_count();
  2122. for (i=current_call+1; i<max; ++i) {
  2123. if (pjsua_call_is_active(i)) {
  2124. current_call = i;
  2125. return PJ_TRUE;
  2126. }
  2127. }
  2128. for (i=0; i<current_call; ++i) {
  2129. if (pjsua_call_is_active(i)) {
  2130. current_call = i;
  2131. return PJ_TRUE;
  2132. }
  2133. }
  2134. current_call = PJSUA_INVALID_ID;
  2135. return PJ_FALSE;
  2136. }
  2137. /*
  2138. * Find previous call when user press '['
  2139. */
  2140. static pj_bool_t find_prev_call(void)
  2141. {
  2142. int i, max;
  2143. max = pjsua_call_get_max_count();
  2144. for (i=current_call-1; i>=0; --i) {
  2145. if (pjsua_call_is_active(i)) {
  2146. current_call = i;
  2147. return PJ_TRUE;
  2148. }
  2149. }
  2150. for (i=max-1; i>current_call; --i) {
  2151. if (pjsua_call_is_active(i)) {
  2152. current_call = i;
  2153. return PJ_TRUE;
  2154. }
  2155. }
  2156. current_call = PJSUA_INVALID_ID;
  2157. return PJ_FALSE;
  2158. }
  2159. /* Callback from timer when the maximum call duration has been
  2160. * exceeded.
  2161. */
  2162. static void call_timeout_callback(pj_timer_heap_t *timer_heap,
  2163. struct pj_timer_entry *entry)
  2164. {
  2165. pjsua_call_id call_id = entry->id;
  2166. pjsua_msg_data msg_data;
  2167. pjsip_generic_string_hdr warn;
  2168. pj_str_t hname = pj_str("Warning");
  2169. pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
  2170. PJ_UNUSED_ARG(timer_heap);
  2171. if (call_id == PJSUA_INVALID_ID) {
  2172. PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
  2173. return;
  2174. }
  2175. /* Add warning header */
  2176. pjsua_msg_data_init(&msg_data);
  2177. pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
  2178. pj_list_push_back(&msg_data.hdr_list, &warn);
  2179. /* Call duration has been exceeded; disconnect the call */
  2180. PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
  2181. "for call %d, disconnecting the call",
  2182. app_config.duration, call_id));
  2183. entry->id = PJSUA_INVALID_ID;
  2184. pjsua_call_hangup(call_id, 200, NULL, &msg_data);
  2185. }
  2186. /*
  2187. * Handler when invite state has changed.
  2188. */
  2189. static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
  2190. {
  2191. pjsua_call_info call_info;
  2192. PJ_UNUSED_ARG(e);
  2193. pjsua_call_get_info(call_id, &call_info);
  2194. if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
  2195. /* Stop all ringback for this call */
  2196. ring_stop(call_id);
  2197. /* Cancel duration timer, if any */
  2198. if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
  2199. struct call_data *cd = &app_config.call_data[call_id];
  2200. pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
  2201. cd->timer.id = PJSUA_INVALID_ID;
  2202. pjsip_endpt_cancel_timer(endpt, &cd->timer);
  2203. }
  2204. /* Rewind play file when hangup automatically,
  2205. * since file is not looped
  2206. */
  2207. if (app_config.auto_play_hangup)
  2208. pjsua_player_set_pos(app_config.wav_id, 0);
  2209. PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
  2210. call_id,
  2211. call_info.last_status,
  2212. call_info.last_status_text.ptr));
  2213. if (call_id == current_call) {
  2214. find_next_call();
  2215. }
  2216. /* Dump media state upon disconnected */
  2217. if (1) {
  2218. PJ_LOG(5,(THIS_FILE,
  2219. "Call %d disconnected, dumping media stats..",
  2220. call_id));
  2221. log_call_dump(call_id);
  2222. }
  2223. } else {
  2224. if (app_config.duration!=NO_LIMIT &&
  2225. call_info.state == PJSIP_INV_STATE_CONFIRMED)
  2226. {
  2227. /* Schedule timer to hangup call after the specified duration */
  2228. struct call_data *cd = &app_config.call_data[call_id];
  2229. pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
  2230. pj_time_val delay;
  2231. cd->timer.id = call_id;
  2232. delay.sec = app_config.duration;
  2233. delay.msec = 0;
  2234. pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
  2235. }
  2236. if (call_info.state == PJSIP_INV_STATE_EARLY) {
  2237. int code;
  2238. pj_str_t reason;
  2239. pjsip_msg *msg;
  2240. /* This can only occur because of TX or RX message */
  2241. pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
  2242. if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
  2243. msg = e->body.tsx_state.src.rdata->msg_info.msg;
  2244. } else {
  2245. msg = e->body.tsx_state.src.tdata->msg;
  2246. }
  2247. code = msg->line.status.code;
  2248. reason = msg->line.status.reason;
  2249. /* Start ringback for 180 for UAC unless there's SDP in 180 */
  2250. if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
  2251. msg->body == NULL &&
  2252. call_info.media_status==PJSUA_CALL_MEDIA_NONE)
  2253. {
  2254. ringback_start(call_id);
  2255. }
  2256. PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
  2257. call_id, call_info.state_text.ptr,
  2258. code, (int)reason.slen, reason.ptr));
  2259. } else {
  2260. PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
  2261. call_id,
  2262. call_info.state_text.ptr));
  2263. }
  2264. if (current_call==PJSUA_INVALID_ID)
  2265. current_call = call_id;
  2266. }
  2267. }
  2268. /**
  2269. * Handler when there is incoming call.
  2270. */
  2271. static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
  2272. pjsip_rx_data *rdata)
  2273. {
  2274. pjsua_call_info call_info;
  2275. PJ_UNUSED_ARG(acc_id);
  2276. PJ_UNUSED_ARG(rdata);
  2277. pjsua_call_get_info(call_id, &call_info);
  2278. if (current_call==PJSUA_INVALID_ID)
  2279. current_call = call_id;
  2280. #ifdef USE_GUI
  2281. if (!showNotification(call_id))
  2282. return;
  2283. #endif
  2284. /* Start ringback */
  2285. ring_start(call_id);
  2286. if (app_config.auto_answer > 0) {
  2287. pjsua_call_setting call_opt;
  2288. pjsua_call_setting_default(&call_opt);
  2289. call_opt.aud_cnt = app_config.aud_cnt;
  2290. call_opt.vid_cnt = app_config.vid.vid_cnt;
  2291. pjsua_call_answer2(call_id, &call_opt, app_config.auto_answer, NULL, NULL);
  2292. }
  2293. if (app_config.auto_answer < 200) {
  2294. char notif_st[80] = {0};
  2295. #if PJSUA_HAS_VIDEO
  2296. if (call_info.rem_offerer && call_info.rem_vid_cnt) {
  2297. snprintf(notif_st, sizeof(notif_st),
  2298. "To %s the video, type \"vid %s\" first, "
  2299. "before answering the call!\n",
  2300. (app_config.vid.vid_cnt? "reject":"accept"),
  2301. (app_config.vid.vid_cnt? "disable":"enable"));
  2302. }
  2303. #endif
  2304. PJ_LOG(3,(THIS_FILE,
  2305. "Incoming call for account %d!\n"
  2306. "Media count: %d audio & %d video\n"
  2307. "%s"
  2308. "From: %s\n"
  2309. "To: %s\n"
  2310. "Press a to answer or h to reject call",
  2311. acc_id,
  2312. call_info.rem_aud_cnt,
  2313. call_info.rem_vid_cnt,
  2314. notif_st,
  2315. call_info.remote_info.ptr,
  2316. call_info.local_info.ptr));
  2317. }
  2318. }
  2319. /*
  2320. * Handler when a transaction within a call has changed state.
  2321. */
  2322. static void on_call_tsx_state(pjsua_call_id call_id,
  2323. pjsip_transaction *tsx,
  2324. pjsip_event *e)
  2325. {
  2326. const pjsip_method info_method =
  2327. {
  2328. PJSIP_OTHER_METHOD,
  2329. { "INFO", 4 }
  2330. };
  2331. if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
  2332. /*
  2333. * Handle INFO method.
  2334. */
  2335. const pj_str_t STR_APPLICATION = { "application", 11};
  2336. const pj_str_t STR_DTMF_RELAY = { "dtmf-relay", 10 };
  2337. pjsip_msg_body *body = NULL;
  2338. pj_bool_t dtmf_info = PJ_FALSE;
  2339. if (tsx->role == PJSIP_ROLE_UAC) {
  2340. if (e->body.tsx_state.type == PJSIP_EVENT_TX_MSG)
  2341. body = e->body.tsx_state.src.tdata->msg->body;
  2342. else
  2343. body = e->body.tsx_state.tsx->last_tx->msg->body;
  2344. } else {
  2345. if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
  2346. body = e->body.tsx_state.src.rdata->msg_info.msg->body;
  2347. }
  2348. /* Check DTMF content in the INFO message */
  2349. if (body && body->len &&
  2350. pj_stricmp(&body->content_type.type, &STR_APPLICATION)==0 &&
  2351. pj_stricmp(&body->content_type.subtype, &STR_DTMF_RELAY)==0)
  2352. {
  2353. dtmf_info = PJ_TRUE;
  2354. }
  2355. if (dtmf_info && tsx->role == PJSIP_ROLE_UAC &&
  2356. (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
  2357. (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
  2358. e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
  2359. {
  2360. /* Status of outgoing INFO request */
  2361. if (tsx->status_code >= 200 && tsx->status_code < 300) {
  2362. PJ_LOG(4,(THIS_FILE,
  2363. "Call %d: DTMF sent successfully with INFO",
  2364. call_id));
  2365. } else if (tsx->status_code >= 300) {
  2366. PJ_LOG(4,(THIS_FILE,
  2367. "Call %d: Failed to send DTMF with INFO: %d/%.*s",
  2368. call_id,
  2369. tsx->status_code,
  2370. (int)tsx->status_text.slen,
  2371. tsx->status_text.ptr));
  2372. }
  2373. } else if (dtmf_info && tsx->role == PJSIP_ROLE_UAS &&
  2374. tsx->state == PJSIP_TSX_STATE_TRYING)
  2375. {
  2376. /* Answer incoming INFO with 200/OK */
  2377. pjsip_rx_data *rdata;
  2378. pjsip_tx_data *tdata;
  2379. pj_status_t status;
  2380. rdata = e->body.tsx_state.src.rdata;
  2381. if (rdata->msg_info.msg->body) {
  2382. status = pjsip_endpt_create_response(tsx->endpt, rdata,
  2383. 200, NULL, &tdata);
  2384. if (status == PJ_SUCCESS)
  2385. status = pjsip_tsx_send_msg(tsx, tdata);
  2386. PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
  2387. call_id,
  2388. (int)rdata->msg_info.msg->body->len,
  2389. rdata->msg_info.msg->body->data));
  2390. } else {
  2391. status = pjsip_endpt_create_response(tsx->endpt, rdata,
  2392. 400, NULL, &tdata);
  2393. if (status == PJ_SUCCESS)
  2394. status = pjsip_tsx_send_msg(tsx, tdata);
  2395. }
  2396. }
  2397. }
  2398. }
  2399. /* General processing for media state. "mi" is the media index */
  2400. static void on_call_generic_media_state(pjsua_call_info *ci, unsigned mi,
  2401. pj_bool_t *has_error)
  2402. {
  2403. const char *status_name[] = {
  2404. "None",
  2405. "Active",
  2406. "Local hold",
  2407. "Remote hold",
  2408. "Error"
  2409. };
  2410. PJ_UNUSED_ARG(has_error);
  2411. pj_assert(ci->media[mi].status <= PJ_ARRAY_SIZE(status_name));
  2412. pj_assert(PJSUA_CALL_MEDIA_ERROR == 4);
  2413. PJ_LOG(4,(THIS_FILE, "Call %d media %d [type=%s], status is %s",
  2414. ci->id, mi, pjmedia_type_name(ci->media[mi].type),
  2415. status_name[ci->media[mi].status]));
  2416. }
  2417. /* Process audio media state. "mi" is the media index. */
  2418. static void on_call_audio_state(pjsua_call_info *ci, unsigned mi,
  2419. pj_bool_t *has_error)
  2420. {
  2421. PJ_UNUSED_ARG(has_error);
  2422. /* Stop ringback */
  2423. ring_stop(ci->id);
  2424. /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
  2425. * otherwise we should NOT connect the ports.
  2426. */
  2427. if (ci->media[mi].status == PJSUA_CALL_MEDIA_ACTIVE ||
  2428. ci->media[mi].status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
  2429. {
  2430. pj_bool_t connect_sound = PJ_TRUE;
  2431. pj_bool_t disconnect_mic = PJ_FALSE;
  2432. pjsua_conf_port_id call_conf_slot;
  2433. call_conf_slot = ci->media[mi].stream.aud.conf_slot;
  2434. /* Loopback sound, if desired */
  2435. if (app_config.auto_loop) {
  2436. pjsua_conf_connect(call_conf_slot, call_conf_slot);
  2437. connect_sound = PJ_FALSE;
  2438. }
  2439. /* Automatically record conversation, if desired */
  2440. if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
  2441. pjsua_conf_connect(call_conf_slot, app_config.rec_port);
  2442. }
  2443. /* Stream a file, if desired */
  2444. if ((app_config.auto_play || app_config.auto_play_hangup) &&
  2445. app_config.wav_port != PJSUA_INVALID_ID)
  2446. {
  2447. pjsua_conf_connect(app_config.wav_port, call_conf_slot);
  2448. connect_sound = PJ_FALSE;
  2449. }
  2450. /* Stream AVI, if desired */
  2451. if (app_config.avi_auto_play &&
  2452. app_config.avi_def_idx != PJSUA_INVALID_ID &&
  2453. app_config.avi[app_config.avi_def_idx].slot != PJSUA_INVALID_ID)
  2454. {
  2455. pjsua_conf_connect(app_config.avi[app_config.avi_def_idx].slot,
  2456. call_conf_slot);
  2457. disconnect_mic = PJ_TRUE;
  2458. }
  2459. /* Put call in conference with other calls, if desired */
  2460. if (app_config.auto_conf) {
  2461. pjsua_call_id call_ids[PJSUA_MAX_CALLS];
  2462. unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
  2463. unsigned i;
  2464. /* Get all calls, and establish media connection between
  2465. * this call and other calls.
  2466. */
  2467. pjsua_enum_calls(call_ids, &call_cnt);
  2468. for (i=0; i<call_cnt; ++i) {
  2469. if (call_ids[i] == ci->id)
  2470. continue;
  2471. if (!pjsua_call_has_media(call_ids[i]))
  2472. continue;
  2473. pjsua_conf_connect(call_conf_slot,
  2474. pjsua_call_get_conf_port(call_ids[i]));
  2475. pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
  2476. call_conf_slot);
  2477. /* Automatically record conversation, if desired */
  2478. if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
  2479. pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
  2480. app_config.rec_port);
  2481. }
  2482. }
  2483. /* Also connect call to local sound device */
  2484. connect_sound = PJ_TRUE;
  2485. }
  2486. /* Otherwise connect to sound device */
  2487. if (connect_sound) {
  2488. pjsua_conf_connect(call_conf_slot, 0);
  2489. if (!disconnect_mic)
  2490. pjsua_conf_connect(0, call_conf_slot);
  2491. /* Automatically record conversation, if desired */
  2492. if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
  2493. pjsua_conf_connect(call_conf_slot, app_config.rec_port);
  2494. pjsua_conf_connect(0, app_config.rec_port);
  2495. }
  2496. }
  2497. }
  2498. }
  2499. /* arrange windows. arg:
  2500. * -1: arrange all windows
  2501. * != -1: arrange only this window id
  2502. */
  2503. static void arrange_window(pjsua_vid_win_id wid)
  2504. {
  2505. #if PJSUA_HAS_VIDEO
  2506. pjmedia_coord pos;
  2507. int i, last;
  2508. pos.x = 0;
  2509. pos.y = 10;
  2510. last = (wid == PJSUA_INVALID_ID) ? PJSUA_MAX_VID_WINS : wid;
  2511. for (i=0; i<last; ++i) {
  2512. pjsua_vid_win_info wi;
  2513. pj_status_t status;
  2514. status = pjsua_vid_win_get_info(i, &wi);
  2515. if (status != PJ_SUCCESS)
  2516. continue;
  2517. if (wid == PJSUA_INVALID_ID)
  2518. pjsua_vid_win_set_pos(i, &pos);
  2519. if (wi.show)
  2520. pos.y += wi.size.h;
  2521. }
  2522. if (wid != PJSUA_INVALID_ID)
  2523. pjsua_vid_win_set_pos(wid, &pos);
  2524. #else
  2525. PJ_UNUSED_ARG(wid);
  2526. #endif
  2527. }
  2528. /* Process video media state. "mi" is the media index. */
  2529. static void on_call_video_state(pjsua_call_info *ci, unsigned mi,
  2530. pj_bool_t *has_error)
  2531. {
  2532. if (ci->media_status != PJSUA_CALL_MEDIA_ACTIVE)
  2533. return;
  2534. arrange_window(ci->media[mi].stream.vid.win_in);
  2535. PJ_UNUSED_ARG(has_error);
  2536. }
  2537. /*
  2538. * Callback on media state changed event.
  2539. * The action may connect the call to sound device, to file, or
  2540. * to loop the call.
  2541. */
  2542. static void on_call_media_state(pjsua_call_id call_id)
  2543. {
  2544. pjsua_call_info call_info;
  2545. unsigned mi;
  2546. pj_bool_t has_error = PJ_FALSE;
  2547. pjsua_call_get_info(call_id, &call_info);
  2548. for (mi=0; mi<call_info.media_cnt; ++mi) {
  2549. on_call_generic_media_state(&call_info, mi, &has_error);
  2550. switch (call_info.media[mi].type) {
  2551. case PJMEDIA_TYPE_AUDIO:
  2552. on_call_audio_state(&call_info, mi, &has_error);
  2553. break;
  2554. case PJMEDIA_TYPE_VIDEO:
  2555. on_call_video_state(&call_info, mi, &has_error);
  2556. break;
  2557. default:
  2558. /* Make gcc happy about enum not handled by switch/case */
  2559. break;
  2560. }
  2561. }
  2562. if (has_error) {
  2563. pj_str_t reason = pj_str("Media failed");
  2564. pjsua_call_hangup(call_id, 500, &reason, NULL);
  2565. }
  2566. #if PJSUA_HAS_VIDEO
  2567. /* Check if remote has just tried to enable video */
  2568. if (call_info.rem_offerer && call_info.rem_vid_cnt)
  2569. {
  2570. int vid_idx;
  2571. /* Check if there is active video */
  2572. vid_idx = pjsua_call_get_vid_stream_idx(call_id);
  2573. if (vid_idx == -1 || call_info.media[vid_idx].dir == PJMEDIA_DIR_NONE) {
  2574. PJ_LOG(3,(THIS_FILE,
  2575. "Just rejected incoming video offer on call %d, "
  2576. "use \"vid call enable %d\" or \"vid call add\" to enable video!",
  2577. call_id, vid_idx));
  2578. }
  2579. }
  2580. #endif
  2581. }
  2582. /*
  2583. * DTMF callback.
  2584. */
  2585. static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
  2586. {
  2587. PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
  2588. }
  2589. /*
  2590. * Redirection handler.
  2591. */
  2592. static pjsip_redirect_op call_on_redirected(pjsua_call_id call_id,
  2593. const pjsip_uri *target,
  2594. const pjsip_event *e)
  2595. {
  2596. PJ_UNUSED_ARG(e);
  2597. if (app_config.redir_op == PJSIP_REDIRECT_PENDING) {
  2598. char uristr[PJSIP_MAX_URL_SIZE];
  2599. int len;
  2600. len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, target, uristr,
  2601. sizeof(uristr));
  2602. if (len < 1) {
  2603. pj_ansi_strcpy(uristr, "--URI too long--");
  2604. }
  2605. PJ_LOG(3,(THIS_FILE, "Call %d is being redirected to %.*s. "
  2606. "Press 'Ra' to accept, 'Rr' to reject, or 'Rd' to "
  2607. "disconnect.",
  2608. call_id, len, uristr));
  2609. }
  2610. return app_config.redir_op;
  2611. }
  2612. /*
  2613. * Handler registration status has changed.
  2614. */
  2615. static void on_reg_state(pjsua_acc_id acc_id)
  2616. {
  2617. PJ_UNUSED_ARG(acc_id);
  2618. // Log already written.
  2619. }
  2620. /*
  2621. * Handler for incoming presence subscription request
  2622. */
  2623. static void on_incoming_subscribe(pjsua_acc_id acc_id,
  2624. pjsua_srv_pres *srv_pres,
  2625. pjsua_buddy_id buddy_id,
  2626. const pj_str_t *from,
  2627. pjsip_rx_data *rdata,
  2628. pjsip_status_code *code,
  2629. pj_str_t *reason,
  2630. pjsua_msg_data *msg_data)
  2631. {
  2632. /* Just accept the request (the default behavior) */
  2633. PJ_UNUSED_ARG(acc_id);
  2634. PJ_UNUSED_ARG(srv_pres);
  2635. PJ_UNUSED_ARG(buddy_id);
  2636. PJ_UNUSED_ARG(from);
  2637. PJ_UNUSED_ARG(rdata);
  2638. PJ_UNUSED_ARG(code);
  2639. PJ_UNUSED_ARG(reason);
  2640. PJ_UNUSED_ARG(msg_data);
  2641. }
  2642. /*
  2643. * Handler on buddy state changed.
  2644. */
  2645. static void on_buddy_state(pjsua_buddy_id buddy_id)
  2646. {
  2647. pjsua_buddy_info info;
  2648. pjsua_buddy_get_info(buddy_id, &info);
  2649. PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s, subscription state is %s "
  2650. "(last termination reason code=%d %.*s)",
  2651. (int)info.uri.slen,
  2652. info.uri.ptr,
  2653. (int)info.status_text.slen,
  2654. info.status_text.ptr,
  2655. info.sub_state_name,
  2656. info.sub_term_code,
  2657. (int)info.sub_term_reason.slen,
  2658. info.sub_term_reason.ptr));
  2659. }
  2660. /*
  2661. * Subscription state has changed.
  2662. */
  2663. static void on_buddy_evsub_state(pjsua_buddy_id buddy_id,
  2664. pjsip_evsub *sub,
  2665. pjsip_event *event)
  2666. {
  2667. char event_info[80];
  2668. PJ_UNUSED_ARG(sub);
  2669. event_info[0] = '\0';
  2670. if (event->type == PJSIP_EVENT_TSX_STATE &&
  2671. event->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
  2672. {
  2673. pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
  2674. snprintf(event_info, sizeof(event_info),
  2675. " (RX %s)",
  2676. pjsip_rx_data_get_info(rdata));
  2677. }
  2678. PJ_LOG(4,(THIS_FILE,
  2679. "Buddy %d: subscription state: %s (event: %s%s)",
  2680. buddy_id, pjsip_evsub_get_state_name(sub),
  2681. pjsip_event_str(event->type),
  2682. event_info));
  2683. }
  2684. /**
  2685. * Incoming IM message (i.e. MESSAGE request)!
  2686. */
  2687. static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
  2688. const pj_str_t *to, const pj_str_t *contact,
  2689. const pj_str_t *mime_type, const pj_str_t *text)
  2690. {
  2691. /* Note: call index may be -1 */
  2692. PJ_UNUSED_ARG(call_id);
  2693. PJ_UNUSED_ARG(to);
  2694. PJ_UNUSED_ARG(contact);
  2695. PJ_UNUSED_ARG(mime_type);
  2696. PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
  2697. (int)from->slen, from->ptr,
  2698. (int)text->slen, text->ptr,
  2699. (int)mime_type->slen, mime_type->ptr));
  2700. }
  2701. /**
  2702. * Received typing indication
  2703. */
  2704. static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
  2705. const pj_str_t *to, const pj_str_t *contact,
  2706. pj_bool_t is_typing)
  2707. {
  2708. PJ_UNUSED_ARG(call_id);
  2709. PJ_UNUSED_ARG(to);
  2710. PJ_UNUSED_ARG(contact);
  2711. PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
  2712. (int)from->slen, from->ptr,
  2713. (is_typing?"is typing..":"has stopped typing")));
  2714. }
  2715. /**
  2716. * Call transfer request status.
  2717. */
  2718. static void on_call_transfer_status(pjsua_call_id call_id,
  2719. int status_code,
  2720. const pj_str_t *status_text,
  2721. pj_bool_t final,
  2722. pj_bool_t *p_cont)
  2723. {
  2724. PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
  2725. call_id, status_code,
  2726. (int)status_text->slen, status_text->ptr,
  2727. (final ? "[final]" : "")));
  2728. if (status_code/100 == 2) {
  2729. PJ_LOG(3,(THIS_FILE,
  2730. "Call %d: call transfered successfully, disconnecting call",
  2731. call_id));
  2732. pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
  2733. *p_cont = PJ_FALSE;
  2734. }
  2735. }
  2736. /*
  2737. * Notification that call is being replaced.
  2738. */
  2739. static void on_call_replaced(pjsua_call_id old_call_id,
  2740. pjsua_call_id new_call_id)
  2741. {
  2742. pjsua_call_info old_ci, new_ci;
  2743. pjsua_call_get_info(old_call_id, &old_ci);
  2744. pjsua_call_get_info(new_call_id, &new_ci);
  2745. PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
  2746. "call %d with %.*s",
  2747. old_call_id,
  2748. (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
  2749. new_call_id,
  2750. (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
  2751. }
  2752. /*
  2753. * NAT type detection callback.
  2754. */
  2755. static void on_nat_detect(const pj_stun_nat_detect_result *res)
  2756. {
  2757. if (res->status != PJ_SUCCESS) {
  2758. pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
  2759. } else {
  2760. PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
  2761. }
  2762. }
  2763. /*
  2764. * MWI indication
  2765. */
  2766. static void on_mwi_info(pjsua_acc_id acc_id, pjsua_mwi_info *mwi_info)
  2767. {
  2768. pj_str_t body;
  2769. PJ_LOG(3,(THIS_FILE, "Received MWI for acc %d:", acc_id));
  2770. if (mwi_info->rdata->msg_info.ctype) {
  2771. const pjsip_ctype_hdr *ctype = mwi_info->rdata->msg_info.ctype;
  2772. PJ_LOG(3,(THIS_FILE, " Content-Type: %.*s/%.*s",
  2773. (int)ctype->media.type.slen,
  2774. ctype->media.type.ptr,
  2775. (int)ctype->media.subtype.slen,
  2776. ctype->media.subtype.ptr));
  2777. }
  2778. if (!mwi_info->rdata->msg_info.msg->body) {
  2779. PJ_LOG(3,(THIS_FILE, " no message body"));
  2780. return;
  2781. }
  2782. body.ptr = mwi_info->rdata->msg_info.msg->body->data;
  2783. body.slen = mwi_info->rdata->msg_info.msg->body->len;
  2784. PJ_LOG(3,(THIS_FILE, " Body:\n%.*s", (int)body.slen, body.ptr));
  2785. }
  2786. /*
  2787. * Transport status notification
  2788. */
  2789. static void on_transport_state(pjsip_transport *tp,
  2790. pjsip_transport_state state,
  2791. const pjsip_transport_state_info *info)
  2792. {
  2793. char host_port[128];
  2794. pj_ansi_snprintf(host_port, sizeof(host_port), "[%.*s:%d]",
  2795. (int)tp->remote_name.host.slen,
  2796. tp->remote_name.host.ptr,
  2797. tp->remote_name.port);
  2798. switch (state) {
  2799. case PJSIP_TP_STATE_CONNECTED:
  2800. {
  2801. PJ_LOG(3,(THIS_FILE, "SIP %s transport is connected to %s",
  2802. tp->type_name, host_port));
  2803. }
  2804. break;
  2805. case PJSIP_TP_STATE_DISCONNECTED:
  2806. {
  2807. char buf[100];
  2808. snprintf(buf, sizeof(buf), "SIP %s transport is disconnected from %s",
  2809. tp->type_name, host_port);
  2810. pjsua_perror(THIS_FILE, buf, info->status);
  2811. }
  2812. break;
  2813. default:
  2814. break;
  2815. }
  2816. #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
  2817. if (!pj_ansi_stricmp(tp->type_name, "tls") && info->ext_info &&
  2818. (state == PJSIP_TP_STATE_CONNECTED ||
  2819. ((pjsip_tls_state_info*)info->ext_info)->
  2820. ssl_sock_info->verify_status != PJ_SUCCESS))
  2821. {
  2822. pjsip_tls_state_info *tls_info = (pjsip_tls_state_info*)info->ext_info;
  2823. pj_ssl_sock_info *ssl_sock_info = tls_info->ssl_sock_info;
  2824. char buf[2048];
  2825. const char *verif_msgs[32];
  2826. unsigned verif_msg_cnt;
  2827. /* Dump server TLS cipher */
  2828. PJ_LOG(4,(THIS_FILE, "TLS cipher used: 0x%06X/%s",
  2829. ssl_sock_info->cipher,
  2830. pj_ssl_cipher_name(ssl_sock_info->cipher) ));
  2831. /* Dump server TLS certificate */
  2832. pj_ssl_cert_info_dump(ssl_sock_info->remote_cert_info, " ",
  2833. buf, sizeof(buf));
  2834. PJ_LOG(4,(THIS_FILE, "TLS cert info of %s:\n%s", host_port, buf));
  2835. /* Dump server TLS certificate verification result */
  2836. verif_msg_cnt = PJ_ARRAY_SIZE(verif_msgs);
  2837. pj_ssl_cert_get_verify_status_strings(ssl_sock_info->verify_status,
  2838. verif_msgs, &verif_msg_cnt);
  2839. PJ_LOG(3,(THIS_FILE, "TLS cert verification result of %s : %s",
  2840. host_port,
  2841. (verif_msg_cnt == 1? verif_msgs[0]:"")));
  2842. if (verif_msg_cnt > 1) {
  2843. unsigned i;
  2844. for (i = 0; i < verif_msg_cnt; ++i)
  2845. PJ_LOG(3,(THIS_FILE, "- %s", verif_msgs[i]));
  2846. }
  2847. if (ssl_sock_info->verify_status &&
  2848. !app_config.udp_cfg.tls_setting.verify_server)
  2849. {
  2850. PJ_LOG(3,(THIS_FILE, "PJSUA is configured to ignore TLS cert "
  2851. "verification errors"));
  2852. }
  2853. }
  2854. #endif
  2855. }
  2856. /*
  2857. * Notification on ICE error.
  2858. */
  2859. static void on_ice_transport_error(int index, pj_ice_strans_op op,
  2860. pj_status_t status, void *param)
  2861. {
  2862. PJ_UNUSED_ARG(op);
  2863. PJ_UNUSED_ARG(param);
  2864. PJ_PERROR(1,(THIS_FILE, status,
  2865. "ICE keep alive failure for transport %d", index));
  2866. }
  2867. /*
  2868. * Notification on sound device operation.
  2869. */
  2870. static pj_status_t on_snd_dev_operation(int operation)
  2871. {
  2872. PJ_LOG(3,(THIS_FILE, "Turning sound device %s", (operation? "ON":"OFF")));
  2873. return PJ_SUCCESS;
  2874. }
  2875. /* Callback on media events */
  2876. static void on_call_media_event(pjsua_call_id call_id,
  2877. unsigned med_idx,
  2878. pjmedia_event *event)
  2879. {
  2880. char event_name[5];
  2881. PJ_LOG(5,(THIS_FILE, "Event %s",
  2882. pjmedia_fourcc_name(event->type, event_name)));
  2883. #if PJSUA_HAS_VIDEO
  2884. if (event->type == PJMEDIA_EVENT_FMT_CHANGED) {
  2885. /* Adjust renderer window size to original video size */
  2886. pjsua_call_info ci;
  2887. pjsua_vid_win_id wid;
  2888. pjmedia_rect_size size;
  2889. pjsua_call_get_info(call_id, &ci);
  2890. if ((ci.media[med_idx].type == PJMEDIA_TYPE_VIDEO) &&
  2891. (ci.media[med_idx].dir & PJMEDIA_DIR_DECODING))
  2892. {
  2893. wid = ci.media[med_idx].stream.vid.win_in;
  2894. size = event->data.fmt_changed.new_fmt.det.vid.size;
  2895. pjsua_vid_win_set_size(wid, &size);
  2896. }
  2897. /* Re-arrange video windows */
  2898. arrange_window(PJSUA_INVALID_ID);
  2899. }
  2900. #else
  2901. PJ_UNUSED_ARG(call_id);
  2902. PJ_UNUSED_ARG(med_idx);
  2903. PJ_UNUSED_ARG(event);
  2904. #endif
  2905. }
  2906. #ifdef TRANSPORT_ADAPTER_SAMPLE
  2907. /*
  2908. * This callback is called when media transport needs to be created.
  2909. */
  2910. static pjmedia_transport* on_create_media_transport(pjsua_call_id call_id,
  2911. unsigned media_idx,
  2912. pjmedia_transport *base_tp,
  2913. unsigned flags)
  2914. {
  2915. pjmedia_transport *adapter;
  2916. pj_status_t status;
  2917. /* Create the adapter */
  2918. status = pjmedia_tp_adapter_create(pjsua_get_pjmedia_endpt(),
  2919. NULL, base_tp,
  2920. (flags & PJSUA_MED_TP_CLOSE_MEMBER),
  2921. &adapter);
  2922. if (status != PJ_SUCCESS) {
  2923. PJ_PERROR(1,(THIS_FILE, status, "Error creating adapter"));
  2924. return NULL;
  2925. }
  2926. PJ_LOG(3,(THIS_FILE, "Media transport is created for call %d media %d",
  2927. call_id, media_idx));
  2928. return adapter;
  2929. }
  2930. #endif
  2931. /*
  2932. * Print buddy list.
  2933. */
  2934. static void print_buddy_list(void)
  2935. {
  2936. pjsua_buddy_id ids[64];
  2937. int i;
  2938. unsigned count = PJ_ARRAY_SIZE(ids);
  2939. puts("Buddy list:");
  2940. pjsua_enum_buddies(ids, &count);
  2941. if (count == 0)
  2942. puts(" -none-");
  2943. else {
  2944. for (i=0; i<(int)count; ++i) {
  2945. pjsua_buddy_info info;
  2946. if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
  2947. continue;
  2948. printf(" [%2d] <%.*s> %.*s\n",
  2949. ids[i]+1,
  2950. (int)info.status_text.slen,
  2951. info.status_text.ptr,
  2952. (int)info.uri.slen,
  2953. info.uri.ptr);
  2954. }
  2955. }
  2956. puts("");
  2957. }
  2958. /*
  2959. * Print account status.
  2960. */
  2961. static void print_acc_status(int acc_id)
  2962. {
  2963. char buf[80];
  2964. pjsua_acc_info info;
  2965. pjsua_acc_get_info(acc_id, &info);
  2966. if (!info.has_registration) {
  2967. pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
  2968. (int)info.status_text.slen,
  2969. info.status_text.ptr);
  2970. } else {
  2971. pj_ansi_snprintf(buf, sizeof(buf),
  2972. "%d/%.*s (expires=%d)",
  2973. info.status,
  2974. (int)info.status_text.slen,
  2975. info.status_text.ptr,
  2976. info.expires);
  2977. }
  2978. printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
  2979. acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
  2980. printf(" Online status: %.*s\n",
  2981. (int)info.online_status_text.slen,
  2982. info.online_status_text.ptr);
  2983. }
  2984. /* Playfile done notification, set timer to hangup calls */
  2985. pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
  2986. {
  2987. pj_time_val delay;
  2988. PJ_UNUSED_ARG(port);
  2989. PJ_UNUSED_ARG(usr_data);
  2990. /* Just rewind WAV when it is played outside of call */
  2991. if (pjsua_call_get_count() == 0) {
  2992. pjsua_player_set_pos(app_config.wav_id, 0);
  2993. return PJ_SUCCESS;
  2994. }
  2995. /* Timer is already active */
  2996. if (app_config.auto_hangup_timer.id == 1)
  2997. return PJ_SUCCESS;
  2998. app_config.auto_hangup_timer.id = 1;
  2999. delay.sec = 0;
  3000. delay.msec = 200; /* Give 200 ms before hangup */
  3001. pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
  3002. &app_config.auto_hangup_timer,
  3003. &delay);
  3004. return PJ_SUCCESS;
  3005. }
  3006. /* Auto hangup timer callback */
  3007. static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
  3008. struct pj_timer_entry *entry)
  3009. {
  3010. PJ_UNUSED_ARG(timer_heap);
  3011. PJ_UNUSED_ARG(entry);
  3012. app_config.auto_hangup_timer.id = 0;
  3013. pjsua_call_hangup_all();
  3014. }
  3015. /*
  3016. * Show a bit of help.
  3017. */
  3018. static void keystroke_help(void)
  3019. {
  3020. pjsua_acc_id acc_ids[16];
  3021. unsigned count = PJ_ARRAY_SIZE(acc_ids);
  3022. int i;
  3023. printf(">>>>\n");
  3024. pjsua_enum_accs(acc_ids, &count);
  3025. printf("Account list:\n");
  3026. for (i=0; i<(int)count; ++i)
  3027. print_acc_status(acc_ids[i]);
  3028. print_buddy_list();
  3029. //puts("Commands:");
  3030. puts("+=============================================================================+");
  3031. puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
  3032. puts("| | | |");
  3033. puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
  3034. puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
  3035. puts("| a Answer call | i Send IM | !a Modify accnt. |");
  3036. puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
  3037. puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
  3038. puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
  3039. puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
  3040. puts("| ],[ Select next/prev call +--------------------------+-------------------+");
  3041. puts("| x Xfer call | Media Commands: | Status & Config: |");
  3042. puts("| X Xfer with Replaces | | |");
  3043. puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
  3044. puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
  3045. puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
  3046. puts("| | V Adjust audio Volume | f Save config |");
  3047. puts("| S Send arbitrary REQUEST | Cp Codec priorities | |");
  3048. puts("+-----------------------------------------------------------------------------+");
  3049. #if PJSUA_HAS_VIDEO
  3050. puts("| Video: \"vid help\" for more info |");
  3051. puts("+-----------------------------------------------------------------------------+");
  3052. #endif
  3053. puts("| q QUIT L ReLoad sleep MS echo [0|1|txt] n: detect NAT type |");
  3054. puts("+=============================================================================+");
  3055. i = pjsua_call_get_count();
  3056. printf("You have %d active call%s\n", i, (i>1?"s":""));
  3057. if (current_call != PJSUA_INVALID_ID) {
  3058. pjsua_call_info ci;
  3059. if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
  3060. printf("Current call id=%d to %.*s [%.*s]\n", current_call,
  3061. (int)ci.remote_info.slen, ci.remote_info.ptr,
  3062. (int)ci.state_text.slen, ci.state_text.ptr);
  3063. }
  3064. }
  3065. /* Help screen for video */
  3066. #if PJSUA_HAS_VIDEO
  3067. static void vid_show_help(void)
  3068. {
  3069. pj_bool_t vid_enabled = (app_config.vid.vid_cnt > 0);
  3070. puts("+=============================================================================+");
  3071. puts("| Video commands: |");
  3072. puts("| |");
  3073. puts("| vid help Show this help screen |");
  3074. puts("| vid enable|disable Enable or disable video in next offer/answer |");
  3075. puts("| vid acc show Show current account video settings |");
  3076. puts("| vid acc autorx on|off Automatically show incoming video on/off |");
  3077. puts("| vid acc autotx on|off Automatically offer video on/off |");
  3078. puts("| vid acc cap ID Set default capture device for current acc |");
  3079. puts("| vid acc rend ID Set default renderer device for current acc |");
  3080. puts("| vid call rx on|off N Enable/disable video RX for stream N in curr call |");
  3081. puts("| vid call tx on|off N Enable/disable video TX for stream N in curr call |");
  3082. puts("| vid call add Add video stream for current call |");
  3083. puts("| vid call enable|disable N Enable/disable stream #N in current call |");
  3084. puts("| vid call cap N ID Set capture dev ID for stream #N in current call |");
  3085. puts("| vid dev list List all video devices |");
  3086. puts("| vid dev refresh Refresh video device list |");
  3087. puts("| vid dev prev on|off ID Enable/disable preview for specified device ID |");
  3088. puts("| vid codec list List video codecs |");
  3089. puts("| vid codec prio ID PRIO Set codec ID priority to PRIO |");
  3090. puts("| vid codec fps ID NUM DEN Set codec ID framerate to (NUM/DEN) fps |");
  3091. puts("| vid codec bw ID AVG MAX Set codec ID bitrate to AVG & MAX kbps |");
  3092. puts("| vid codec size ID W H Set codec ID size/resolution to W x H |");
  3093. puts("| vid win list List all active video windows |");
  3094. puts("| vid win arrange Auto arrange windows |");
  3095. puts("| vid win show|hide ID Show/hide the specified video window ID |");
  3096. puts("| vid win move ID X Y Move window ID to position X,Y |");
  3097. puts("| vid win resize ID w h Resize window ID to the specified width, height |");
  3098. puts("+=============================================================================+");
  3099. printf("| Video will be %s in the next offer/answer %s |\n",
  3100. (vid_enabled? "enabled" : "disabled"), (vid_enabled? " " : ""));
  3101. puts("+=============================================================================+");
  3102. }
  3103. #endif
  3104. /*
  3105. * Input simple string
  3106. */
  3107. static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
  3108. {
  3109. char *p;
  3110. printf("%s (empty to cancel): ", title); fflush(stdout);
  3111. if (fgets(buf, len, stdin) == NULL)
  3112. return PJ_FALSE;
  3113. /* Remove trailing newlines. */
  3114. for (p=buf; ; ++p) {
  3115. if (*p=='\r' || *p=='\n') *p='\0';
  3116. else if (!*p) break;
  3117. }
  3118. if (!*buf)
  3119. return PJ_FALSE;
  3120. return PJ_TRUE;
  3121. }
  3122. #define NO_NB -2
  3123. struct input_result
  3124. {
  3125. int nb_result;
  3126. char *uri_result;
  3127. };
  3128. /*
  3129. * Input URL.
  3130. */
  3131. static void ui_input_url(const char *title, char *buf, int len,
  3132. struct input_result *result)
  3133. {
  3134. result->nb_result = NO_NB;
  3135. result->uri_result = NULL;
  3136. print_buddy_list();
  3137. printf("Choices:\n"
  3138. " 0 For current dialog.\n"
  3139. " -1 All %d buddies in buddy list\n"
  3140. " [1 -%2d] Select from buddy list\n"
  3141. " URL An URL\n"
  3142. " <Enter> Empty input (or 'q') to cancel\n"
  3143. , pjsua_get_buddy_count(), pjsua_get_buddy_count());
  3144. printf("%s: ", title);
  3145. fflush(stdout);
  3146. if (fgets(buf, len, stdin) == NULL)
  3147. return;
  3148. len = strlen(buf);
  3149. /* Left trim */
  3150. while (pj_isspace(*buf)) {
  3151. ++buf;
  3152. --len;
  3153. }
  3154. /* Remove trailing newlines */
  3155. while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
  3156. buf[--len] = '\0';
  3157. if (len == 0 || buf[0]=='q')
  3158. return;
  3159. if (pj_isdigit(*buf) || *buf=='-') {
  3160. int i;
  3161. if (*buf=='-')
  3162. i = 1;
  3163. else
  3164. i = 0;
  3165. for (; i<len; ++i) {
  3166. if (!pj_isdigit(buf[i])) {
  3167. puts("Invalid input");
  3168. return;
  3169. }
  3170. }
  3171. result->nb_result = my_atoi(buf);
  3172. if (result->nb_result >= 0 &&
  3173. result->nb_result <= (int)pjsua_get_buddy_count())
  3174. {
  3175. return;
  3176. }
  3177. if (result->nb_result == -1)
  3178. return;
  3179. puts("Invalid input");
  3180. result->nb_result = NO_NB;
  3181. return;
  3182. } else {
  3183. pj_status_t status;
  3184. if ((status=pjsua_verify_url(buf)) != PJ_SUCCESS) {
  3185. pjsua_perror(THIS_FILE, "Invalid URL", status);
  3186. return;
  3187. }
  3188. result->uri_result = buf;
  3189. }
  3190. }
  3191. /*
  3192. * List the ports in conference bridge
  3193. */
  3194. static void conf_list(void)
  3195. {
  3196. unsigned i, count;
  3197. pjsua_conf_port_id id[PJSUA_MAX_CALLS];
  3198. printf("Conference ports:\n");
  3199. count = PJ_ARRAY_SIZE(id);
  3200. pjsua_enum_conf_ports(id, &count);
  3201. for (i=0; i<count; ++i) {
  3202. char txlist[PJSUA_MAX_CALLS*4+10];
  3203. unsigned j;
  3204. pjsua_conf_port_info info;
  3205. pjsua_conf_get_port_info(id[i], &info);
  3206. txlist[0] = '\0';
  3207. for (j=0; j<info.listener_cnt; ++j) {
  3208. char s[10];
  3209. pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
  3210. pj_ansi_strcat(txlist, s);
  3211. }
  3212. printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
  3213. info.slot_id,
  3214. info.clock_rate/1000,
  3215. info.samples_per_frame*1000/info.channel_count/info.clock_rate,
  3216. info.channel_count,
  3217. (int)info.name.slen,
  3218. info.name.ptr,
  3219. txlist);
  3220. }
  3221. puts("");
  3222. }
  3223. /*
  3224. * Send arbitrary request to remote host
  3225. */
  3226. static void send_request(char *cstr_method, const pj_str_t *dst_uri)
  3227. {
  3228. pj_str_t str_method;
  3229. pjsip_method method;
  3230. pjsip_tx_data *tdata;
  3231. pjsip_endpoint *endpt;
  3232. pj_status_t status;
  3233. endpt = pjsua_get_pjsip_endpt();
  3234. str_method = pj_str(cstr_method);
  3235. pjsip_method_init_np(&method, &str_method);
  3236. status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
  3237. status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
  3238. if (status != PJ_SUCCESS) {
  3239. pjsua_perror(THIS_FILE, "Unable to send request", status);
  3240. return;
  3241. }
  3242. }
  3243. /*
  3244. * Change extended online status.
  3245. */
  3246. static void change_online_status(void)
  3247. {
  3248. char menuin[32];
  3249. pj_bool_t online_status;
  3250. pjrpid_element elem;
  3251. int i, choice;
  3252. enum {
  3253. AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
  3254. };
  3255. struct opt {
  3256. int id;
  3257. char *name;
  3258. } opts[] = {
  3259. { AVAILABLE, "Available" },
  3260. { BUSY, "Busy"},
  3261. { OTP, "On the phone"},
  3262. { IDLE, "Idle"},
  3263. { AWAY, "Away"},
  3264. { BRB, "Be right back"},
  3265. { OFFLINE, "Offline"}
  3266. };
  3267. printf("\n"
  3268. "Choices:\n");
  3269. for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
  3270. printf(" %d %s\n", opts[i].id+1, opts[i].name);
  3271. }
  3272. if (!simple_input("Select status", menuin, sizeof(menuin)))
  3273. return;
  3274. choice = atoi(menuin) - 1;
  3275. if (choice < 0 || choice >= OPT_MAX) {
  3276. puts("Invalid selection");
  3277. return;
  3278. }
  3279. pj_bzero(&elem, sizeof(elem));
  3280. elem.type = PJRPID_ELEMENT_TYPE_PERSON;
  3281. online_status = PJ_TRUE;
  3282. switch (choice) {
  3283. case AVAILABLE:
  3284. break;
  3285. case BUSY:
  3286. elem.activity = PJRPID_ACTIVITY_BUSY;
  3287. elem.note = pj_str("Busy");
  3288. break;
  3289. case OTP:
  3290. elem.activity = PJRPID_ACTIVITY_BUSY;
  3291. elem.note = pj_str("On the phone");
  3292. break;
  3293. case IDLE:
  3294. elem.activity = PJRPID_ACTIVITY_UNKNOWN;
  3295. elem.note = pj_str("Idle");
  3296. break;
  3297. case AWAY:
  3298. elem.activity = PJRPID_ACTIVITY_AWAY;
  3299. elem.note = pj_str("Away");
  3300. break;
  3301. case BRB:
  3302. elem.activity = PJRPID_ACTIVITY_UNKNOWN;
  3303. elem.note = pj_str("Be right back");
  3304. break;
  3305. case OFFLINE:
  3306. online_status = PJ_FALSE;
  3307. break;
  3308. }
  3309. pjsua_acc_set_online_status2(current_acc, online_status, &elem);
  3310. }
  3311. /*
  3312. * Change codec priorities.
  3313. */
  3314. static void manage_codec_prio(void)
  3315. {
  3316. pjsua_codec_info c[32];
  3317. unsigned i, count = PJ_ARRAY_SIZE(c);
  3318. char input[32];
  3319. char *codec, *prio;
  3320. pj_str_t id;
  3321. int new_prio;
  3322. pj_status_t status;
  3323. printf("List of audio codecs:\n");
  3324. pjsua_enum_codecs(c, &count);
  3325. for (i=0; i<count; ++i) {
  3326. printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
  3327. c[i].codec_id.ptr);
  3328. }
  3329. #if PJSUA_HAS_VIDEO
  3330. puts("");
  3331. printf("List of video codecs:\n");
  3332. pjsua_vid_enum_codecs(c, &count);
  3333. for (i=0; i<count; ++i) {
  3334. printf(" %d\t%.*s%s%.*s\n", c[i].priority,
  3335. (int)c[i].codec_id.slen,
  3336. c[i].codec_id.ptr,
  3337. c[i].desc.slen? " - ":"",
  3338. (int)c[i].desc.slen,
  3339. c[i].desc.ptr);
  3340. }
  3341. #endif
  3342. puts("");
  3343. puts("Enter codec id and its new priority (e.g. \"speex/16000 200\", ""\"H263 200\"),");
  3344. puts("or empty to cancel.");
  3345. printf("Codec name (\"*\" for all) and priority: ");
  3346. if (fgets(input, sizeof(input), stdin) == NULL)
  3347. return;
  3348. if (input[0]=='\r' || input[0]=='\n') {
  3349. puts("Done");
  3350. return;
  3351. }
  3352. codec = strtok(input, " \t\r\n");
  3353. prio = strtok(NULL, " \r\n");
  3354. if (!codec || !prio) {
  3355. puts("Invalid input");
  3356. return;
  3357. }
  3358. new_prio = atoi(prio);
  3359. if (new_prio < 0)
  3360. new_prio = 0;
  3361. else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
  3362. new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
  3363. status = pjsua_codec_set_priority(pj_cstr(&id, codec),
  3364. (pj_uint8_t)new_prio);
  3365. #if PJSUA_HAS_VIDEO
  3366. if (status != PJ_SUCCESS) {
  3367. status = pjsua_vid_codec_set_priority(pj_cstr(&id, codec),
  3368. (pj_uint8_t)new_prio);
  3369. }
  3370. #endif
  3371. if (status != PJ_SUCCESS)
  3372. pjsua_perror(THIS_FILE, "Error setting codec priority", status);
  3373. }
  3374. #if PJSUA_HAS_VIDEO
  3375. static void vid_print_dev(int id, const pjmedia_vid_dev_info *vdi,
  3376. const char *title)
  3377. {
  3378. char capnames[120];
  3379. char formats[120];
  3380. const char *dirname;
  3381. unsigned i;
  3382. if (vdi->dir == PJMEDIA_DIR_CAPTURE_RENDER) {
  3383. dirname = "capture, render";
  3384. } else if (vdi->dir == PJMEDIA_DIR_CAPTURE) {
  3385. dirname = "capture";
  3386. } else {
  3387. dirname = "render";
  3388. }
  3389. capnames[0] = '\0';
  3390. for (i=0; i<sizeof(int)*8 && (1 << i) < PJMEDIA_VID_DEV_CAP_MAX; ++i) {
  3391. if (vdi->caps & (1 << i)) {
  3392. const char *capname = pjmedia_vid_dev_cap_name(1 << i, NULL);
  3393. if (capname) {
  3394. if (*capnames)
  3395. strcat(capnames, ", ");
  3396. strncat(capnames, capname,
  3397. sizeof(capnames)-strlen(capnames)-1);
  3398. }
  3399. }
  3400. }
  3401. formats[0] = '\0';
  3402. for (i=0; i<vdi->fmt_cnt; ++i) {
  3403. const pjmedia_video_format_info *vfi =
  3404. pjmedia_get_video_format_info(NULL, vdi->fmt[i].id);
  3405. if (vfi) {
  3406. if (*formats)
  3407. strcat(formats, ", ");
  3408. strncat(formats, vfi->name, sizeof(formats)-strlen(formats)-1);
  3409. }
  3410. }
  3411. PJ_LOG(3,(THIS_FILE, "%3d %s [%s][%s] %s", id, vdi->name, vdi->driver,
  3412. dirname, title));
  3413. PJ_LOG(3,(THIS_FILE, " Supported capabilities: %s", capnames));
  3414. PJ_LOG(3,(THIS_FILE, " Supported formats: %s", formats));
  3415. }
  3416. static void vid_list_devs(void)
  3417. {
  3418. unsigned i, count;
  3419. pjmedia_vid_dev_info vdi;
  3420. pj_status_t status;
  3421. PJ_LOG(3,(THIS_FILE, "Video device list:"));
  3422. count = pjsua_vid_dev_count();
  3423. if (count == 0) {
  3424. PJ_LOG(3,(THIS_FILE, " - no device detected -"));
  3425. return;
  3426. } else {
  3427. PJ_LOG(3,(THIS_FILE, "%d device(s) detected:", count));
  3428. }
  3429. status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi);
  3430. if (status == PJ_SUCCESS)
  3431. vid_print_dev(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi,
  3432. "(default renderer device)");
  3433. status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi);
  3434. if (status == PJ_SUCCESS)
  3435. vid_print_dev(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi,
  3436. "(default capture device)");
  3437. for (i=0; i<count; ++i) {
  3438. status = pjsua_vid_dev_get_info(i, &vdi);
  3439. if (status == PJ_SUCCESS)
  3440. vid_print_dev(i, &vdi, "");
  3441. }
  3442. }
  3443. static void app_config_init_video(pjsua_acc_config *acc_cfg)
  3444. {
  3445. acc_cfg->vid_in_auto_show = app_config.vid.in_auto_show;
  3446. acc_cfg->vid_out_auto_transmit = app_config.vid.out_auto_transmit;
  3447. /* Note that normally GUI application will prefer a borderless
  3448. * window.
  3449. */
  3450. acc_cfg->vid_wnd_flags = PJMEDIA_VID_DEV_WND_BORDER |
  3451. PJMEDIA_VID_DEV_WND_RESIZABLE;
  3452. acc_cfg->vid_cap_dev = app_config.vid.vcapture_dev;
  3453. acc_cfg->vid_rend_dev = app_config.vid.vrender_dev;
  3454. if (app_config.avi_auto_play &&
  3455. app_config.avi_def_idx != PJSUA_INVALID_ID &&
  3456. app_config.avi[app_config.avi_def_idx].dev_id != PJMEDIA_VID_INVALID_DEV)
  3457. {
  3458. acc_cfg->vid_cap_dev = app_config.avi[app_config.avi_def_idx].dev_id;
  3459. }
  3460. }
  3461. static void app_config_show_video(int acc_id, const pjsua_acc_config *acc_cfg)
  3462. {
  3463. PJ_LOG(3,(THIS_FILE,
  3464. "Account %d:\n"
  3465. " RX auto show: %d\n"
  3466. " TX auto transmit: %d\n"
  3467. " Capture dev: %d\n"
  3468. " Render dev: %d",
  3469. acc_id,
  3470. acc_cfg->vid_in_auto_show,
  3471. acc_cfg->vid_out_auto_transmit,
  3472. acc_cfg->vid_cap_dev,
  3473. acc_cfg->vid_rend_dev));
  3474. }
  3475. static void vid_handle_menu(char *menuin)
  3476. {
  3477. char *argv[8];
  3478. int argc = 0;
  3479. /* Tokenize */
  3480. argv[argc] = strtok(menuin, " \t\r\n");
  3481. while (argv[argc] && *argv[argc]) {
  3482. argc++;
  3483. argv[argc] = strtok(NULL, " \t\r\n");
  3484. }
  3485. if (argc == 1 || strcmp(argv[1], "help")==0) {
  3486. vid_show_help();
  3487. } else if (argc == 2 && (strcmp(argv[1], "enable")==0 ||
  3488. strcmp(argv[1], "disable")==0))
  3489. {
  3490. pj_bool_t enabled = (strcmp(argv[1], "enable")==0);
  3491. app_config.vid.vid_cnt = (enabled ? 1 : 0);
  3492. PJ_LOG(3,(THIS_FILE, "Video will be %s in next offer/answer",
  3493. (enabled?"enabled":"disabled")));
  3494. } else if (strcmp(argv[1], "acc")==0) {
  3495. pjsua_acc_config acc_cfg;
  3496. pj_bool_t changed = PJ_FALSE;
  3497. pjsua_acc_get_config(current_acc, &acc_cfg);
  3498. if (argc == 3 && strcmp(argv[2], "show")==0) {
  3499. app_config_show_video(current_acc, &acc_cfg);
  3500. } else if (argc == 4 && strcmp(argv[2], "autorx")==0) {
  3501. int on = (strcmp(argv[3], "on")==0);
  3502. acc_cfg.vid_in_auto_show = on;
  3503. changed = PJ_TRUE;
  3504. } else if (argc == 4 && strcmp(argv[2], "autotx")==0) {
  3505. int on = (strcmp(argv[3], "on")==0);
  3506. acc_cfg.vid_out_auto_transmit = on;
  3507. changed = PJ_TRUE;
  3508. } else if (argc == 4 && strcmp(argv[2], "cap")==0) {
  3509. int dev = atoi(argv[3]);
  3510. acc_cfg.vid_cap_dev = dev;
  3511. changed = PJ_TRUE;
  3512. } else if (argc == 4 && strcmp(argv[2], "rend")==0) {
  3513. int dev = atoi(argv[3]);
  3514. acc_cfg.vid_rend_dev = dev;
  3515. changed = PJ_TRUE;
  3516. } else {
  3517. goto on_error;
  3518. }
  3519. if (changed) {
  3520. pj_status_t status = pjsua_acc_modify(current_acc, &acc_cfg);
  3521. if (status != PJ_SUCCESS)
  3522. PJ_PERROR(1,(THIS_FILE, status, "Error modifying account %d",
  3523. current_acc));
  3524. }
  3525. } else if (strcmp(argv[1], "call")==0) {
  3526. pjsua_call_vid_strm_op_param param;
  3527. pj_status_t status = PJ_SUCCESS;
  3528. pjsua_call_vid_strm_op_param_default(&param);
  3529. if (argc == 5 && strcmp(argv[2], "rx")==0) {
  3530. pjsua_stream_info si;
  3531. pj_bool_t on = (strcmp(argv[3], "on") == 0);
  3532. param.med_idx = atoi(argv[4]);
  3533. if (pjsua_call_get_stream_info(current_call, param.med_idx, &si) ||
  3534. si.type != PJMEDIA_TYPE_VIDEO)
  3535. {
  3536. PJ_PERROR(1,(THIS_FILE, PJ_EINVAL, "Invalid stream"));
  3537. return;
  3538. }
  3539. if (on) param.dir = (si.info.vid.dir | PJMEDIA_DIR_DECODING);
  3540. else param.dir = (si.info.vid.dir & PJMEDIA_DIR_ENCODING);
  3541. status = pjsua_call_set_vid_strm(current_call,
  3542. PJSUA_CALL_VID_STRM_CHANGE_DIR,
  3543. &param);
  3544. }
  3545. else if (argc == 5 && strcmp(argv[2], "tx")==0) {
  3546. pj_bool_t on = (strcmp(argv[3], "on") == 0);
  3547. pjsua_call_vid_strm_op op = on? PJSUA_CALL_VID_STRM_START_TRANSMIT :
  3548. PJSUA_CALL_VID_STRM_STOP_TRANSMIT;
  3549. param.med_idx = atoi(argv[4]);
  3550. status = pjsua_call_set_vid_strm(current_call, op, &param);
  3551. }
  3552. else if (argc == 3 && strcmp(argv[2], "add")==0) {
  3553. status = pjsua_call_set_vid_strm(current_call,
  3554. PJSUA_CALL_VID_STRM_ADD, NULL);
  3555. }
  3556. else if (argc >= 3 &&
  3557. (strcmp(argv[2], "disable")==0 || strcmp(argv[2], "enable")==0))
  3558. {
  3559. pj_bool_t enable = (strcmp(argv[2], "enable") == 0);
  3560. pjsua_call_vid_strm_op op = enable? PJSUA_CALL_VID_STRM_CHANGE_DIR :
  3561. PJSUA_CALL_VID_STRM_REMOVE;
  3562. param.med_idx = argc >= 4? atoi(argv[3]) : -1;
  3563. param.dir = PJMEDIA_DIR_ENCODING_DECODING;
  3564. status = pjsua_call_set_vid_strm(current_call, op, &param);
  3565. }
  3566. else if (argc >= 3 && strcmp(argv[2], "cap")==0) {
  3567. param.med_idx = argc >= 4? atoi(argv[3]) : -1;
  3568. param.cap_dev = argc >= 5? atoi(argv[4]) : PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
  3569. status = pjsua_call_set_vid_strm(current_call,
  3570. PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV,
  3571. &param);
  3572. } else
  3573. goto on_error;
  3574. if (status != PJ_SUCCESS) {
  3575. PJ_PERROR(1,(THIS_FILE, status, "Error modifying video stream"));
  3576. }
  3577. } else if (argc >= 3 && strcmp(argv[1], "dev")==0) {
  3578. if (strcmp(argv[2], "list")==0) {
  3579. vid_list_devs();
  3580. } else if (strcmp(argv[2], "refresh")==0) {
  3581. pjmedia_vid_dev_refresh();
  3582. } else if (strcmp(argv[2], "prev")==0) {
  3583. if (argc != 5) {
  3584. goto on_error;
  3585. } else {
  3586. pj_bool_t on = (strcmp(argv[3], "on") == 0);
  3587. int dev_id = atoi(argv[4]);
  3588. if (on) {
  3589. pjsua_vid_preview_param param;
  3590. pjsua_vid_preview_param_default(&param);
  3591. param.wnd_flags = PJMEDIA_VID_DEV_WND_BORDER |
  3592. PJMEDIA_VID_DEV_WND_RESIZABLE;
  3593. pjsua_vid_preview_start(dev_id, &param);
  3594. arrange_window(pjsua_vid_preview_get_win(dev_id));
  3595. } else {
  3596. pjsua_vid_win_id wid;
  3597. wid = pjsua_vid_preview_get_win(dev_id);
  3598. if (wid != PJSUA_INVALID_ID) {
  3599. /* Preview window hiding once it is stopped is
  3600. * responsibility of app */
  3601. pjsua_vid_win_set_show(wid, PJ_FALSE);
  3602. pjsua_vid_preview_stop(dev_id);
  3603. }
  3604. }
  3605. }
  3606. } else
  3607. goto on_error;
  3608. } else if (strcmp(argv[1], "win")==0) {
  3609. pj_status_t status = PJ_SUCCESS;
  3610. if (argc==3 && strcmp(argv[2], "list")==0) {
  3611. pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS];
  3612. unsigned i, cnt = PJ_ARRAY_SIZE(wids);
  3613. pjsua_vid_enum_wins(wids, &cnt);
  3614. PJ_LOG(3,(THIS_FILE, "Found %d video windows:", cnt));
  3615. PJ_LOG(3,(THIS_FILE, "WID show pos size"));
  3616. PJ_LOG(3,(THIS_FILE, "------------------------------"));
  3617. for (i = 0; i < cnt; ++i) {
  3618. pjsua_vid_win_info wi;
  3619. pjsua_vid_win_get_info(wids[i], &wi);
  3620. PJ_LOG(3,(THIS_FILE, "%3d %c (%d,%d) %dx%d",
  3621. wids[i], (wi.show?'Y':'N'), wi.pos.x, wi.pos.y,
  3622. wi.size.w, wi.size.h));
  3623. }
  3624. } else if (argc==4 && (strcmp(argv[2], "show")==0 ||
  3625. strcmp(argv[2], "hide")==0))
  3626. {
  3627. pj_bool_t show = (strcmp(argv[2], "show")==0);
  3628. pjsua_vid_win_id wid = atoi(argv[3]);
  3629. status = pjsua_vid_win_set_show(wid, show);
  3630. } else if (argc==6 && strcmp(argv[2], "move")==0) {
  3631. pjsua_vid_win_id wid = atoi(argv[3]);
  3632. pjmedia_coord pos;
  3633. pos.x = atoi(argv[4]);
  3634. pos.y = atoi(argv[5]);
  3635. status = pjsua_vid_win_set_pos(wid, &pos);
  3636. } else if (argc==6 && strcmp(argv[2], "resize")==0) {
  3637. pjsua_vid_win_id wid = atoi(argv[3]);
  3638. pjmedia_rect_size size;
  3639. size.w = atoi(argv[4]);
  3640. size.h = atoi(argv[5]);
  3641. status = pjsua_vid_win_set_size(wid, &size);
  3642. } else if (argc==3 && strcmp(argv[2], "arrange")==0) {
  3643. arrange_window(PJSUA_INVALID_ID);
  3644. } else
  3645. goto on_error;
  3646. if (status != PJ_SUCCESS) {
  3647. PJ_PERROR(1,(THIS_FILE, status, "Window operation error"));
  3648. }
  3649. } else if (strcmp(argv[1], "codec")==0) {
  3650. pjsua_codec_info ci[PJMEDIA_CODEC_MGR_MAX_CODECS];
  3651. unsigned count = PJ_ARRAY_SIZE(ci);
  3652. pj_status_t status;
  3653. if (argc==3 && strcmp(argv[2], "list")==0) {
  3654. status = pjsua_vid_enum_codecs(ci, &count);
  3655. if (status != PJ_SUCCESS) {
  3656. PJ_PERROR(1,(THIS_FILE, status, "Error enumerating codecs"));
  3657. } else {
  3658. unsigned i;
  3659. PJ_LOG(3,(THIS_FILE, "Found %d video codecs:", count));
  3660. PJ_LOG(3,(THIS_FILE, "codec id prio fps bw(kbps) size"));
  3661. PJ_LOG(3,(THIS_FILE, "------------------------------------------"));
  3662. for (i=0; i<count; ++i) {
  3663. pjmedia_vid_codec_param cp;
  3664. pjmedia_video_format_detail *vfd;
  3665. status = pjsua_vid_codec_get_param(&ci[i].codec_id, &cp);
  3666. if (status != PJ_SUCCESS)
  3667. continue;
  3668. vfd = pjmedia_format_get_video_format_detail(&cp.enc_fmt,
  3669. PJ_TRUE);
  3670. PJ_LOG(3,(THIS_FILE, "%.*s%.*s %3d %7.2f %4d/%4d %dx%d",
  3671. (int)ci[i].codec_id.slen, ci[i].codec_id.ptr,
  3672. 13-(int)ci[i].codec_id.slen, " ",
  3673. ci[i].priority,
  3674. (vfd->fps.num*1.0/vfd->fps.denum),
  3675. vfd->avg_bps/1000, vfd->max_bps/1000,
  3676. vfd->size.w, vfd->size.h));
  3677. }
  3678. }
  3679. } else if (argc==5 && strcmp(argv[2], "prio")==0) {
  3680. pj_str_t cid;
  3681. int prio;
  3682. cid = pj_str(argv[3]);
  3683. prio = atoi(argv[4]);
  3684. status = pjsua_vid_codec_set_priority(&cid, (pj_uint8_t)prio);
  3685. if (status != PJ_SUCCESS)
  3686. PJ_PERROR(1,(THIS_FILE, status, "Set codec priority error"));
  3687. } else if (argc==6 && strcmp(argv[2], "fps")==0) {
  3688. pjmedia_vid_codec_param cp;
  3689. pj_str_t cid;
  3690. int M, N;
  3691. cid = pj_str(argv[3]);
  3692. M = atoi(argv[4]);
  3693. N = atoi(argv[5]);
  3694. status = pjsua_vid_codec_get_param(&cid, &cp);
  3695. if (status == PJ_SUCCESS) {
  3696. cp.enc_fmt.det.vid.fps.num = M;
  3697. cp.enc_fmt.det.vid.fps.denum = N;
  3698. status = pjsua_vid_codec_set_param(&cid, &cp);
  3699. }
  3700. if (status != PJ_SUCCESS)
  3701. PJ_PERROR(1,(THIS_FILE, status, "Set codec framerate error"));
  3702. } else if (argc==6 && strcmp(argv[2], "bw")==0) {
  3703. pjmedia_vid_codec_param cp;
  3704. pj_str_t cid;
  3705. int M, N;
  3706. cid = pj_str(argv[3]);
  3707. M = atoi(argv[4]);
  3708. N = atoi(argv[5]);
  3709. status = pjsua_vid_codec_get_param(&cid, &cp);
  3710. if (status == PJ_SUCCESS) {
  3711. cp.enc_fmt.det.vid.avg_bps = M * 1000;
  3712. cp.enc_fmt.det.vid.max_bps = N * 1000;
  3713. status = pjsua_vid_codec_set_param(&cid, &cp);
  3714. }
  3715. if (status != PJ_SUCCESS)
  3716. PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error"));
  3717. } else if (argc==6 && strcmp(argv[2], "size")==0) {
  3718. pjmedia_vid_codec_param cp;
  3719. pj_str_t cid;
  3720. int M, N;
  3721. cid = pj_str(argv[3]);
  3722. M = atoi(argv[4]);
  3723. N = atoi(argv[5]);
  3724. status = pjsua_vid_codec_get_param(&cid, &cp);
  3725. if (status == PJ_SUCCESS) {
  3726. cp.enc_fmt.det.vid.size.w = M;
  3727. cp.enc_fmt.det.vid.size.h = N;
  3728. status = pjsua_vid_codec_set_param(&cid, &cp);
  3729. }
  3730. if (status != PJ_SUCCESS)
  3731. PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error"));
  3732. } else
  3733. goto on_error;
  3734. } else
  3735. goto on_error;
  3736. return;
  3737. on_error:
  3738. PJ_LOG(1,(THIS_FILE, "Invalid command, use 'vid help'"));
  3739. }
  3740. #else
  3741. static void app_config_init_video(pjsua_acc_config *acc_cfg)
  3742. {
  3743. PJ_UNUSED_ARG(acc_cfg);
  3744. }
  3745. #endif /* PJSUA_HAS_VIDEO */
  3746. /*
  3747. * Main "user interface" loop.
  3748. */
  3749. void console_app_main(const pj_str_t *uri_to_call)
  3750. {
  3751. char menuin[32];
  3752. char buf[128];
  3753. char text[128];
  3754. int i, count;
  3755. char *uri;
  3756. pj_str_t tmp;
  3757. struct input_result result;
  3758. pjsua_msg_data msg_data;
  3759. pjsua_call_info call_info;
  3760. pjsua_acc_info acc_info;
  3761. pjsua_call_setting call_opt;
  3762. pjsua_call_setting_default(&call_opt);
  3763. call_opt.aud_cnt = app_config.aud_cnt;
  3764. call_opt.vid_cnt = app_config.vid.vid_cnt;
  3765. /* If user specifies URI to call, then call the URI */
  3766. if (uri_to_call->slen) {
  3767. pjsua_call_make_call( current_acc, uri_to_call, &call_opt, NULL, NULL, NULL);
  3768. }
  3769. keystroke_help();
  3770. for (;;) {
  3771. printf(">>> ");
  3772. fflush(stdout);
  3773. if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
  3774. /*
  3775. * Be friendly to users who redirect commands into
  3776. * program, when file ends, resume with kbd.
  3777. * If exit is desired end script with q for quit
  3778. */
  3779. /* Reopen stdin/stdout/stderr to /dev/console */
  3780. #if defined(PJ_WIN32) && PJ_WIN32!=0
  3781. if (freopen ("CONIN$", "r", stdin) == NULL) {
  3782. #else
  3783. if (1) {
  3784. #endif
  3785. puts("Cannot switch back to console from file redirection");
  3786. menuin[0] = 'q';
  3787. menuin[1] = '\0';
  3788. } else {
  3789. puts("Switched back to console from file redirection");
  3790. continue;
  3791. }
  3792. }
  3793. if (cmd_echo) {
  3794. printf("%s", menuin);
  3795. }
  3796. /* Update call setting */
  3797. pjsua_call_setting_default(&call_opt);
  3798. call_opt.aud_cnt = app_config.aud_cnt;
  3799. call_opt.vid_cnt = app_config.vid.vid_cnt;
  3800. switch (menuin[0]) {
  3801. case 'm':
  3802. /* Make call! : */
  3803. printf("(You currently have %d calls)\n",
  3804. pjsua_call_get_count());
  3805. uri = NULL;
  3806. ui_input_url("Make call", buf, sizeof(buf), &result);
  3807. if (result.nb_result != NO_NB) {
  3808. if (result.nb_result == -1 || result.nb_result == 0) {
  3809. puts("You can't do that with make call!");
  3810. continue;
  3811. } else {
  3812. pjsua_buddy_info binfo;
  3813. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  3814. tmp.ptr = buf;
  3815. pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
  3816. }
  3817. } else if (result.uri_result) {
  3818. tmp = pj_str(result.uri_result);
  3819. } else {
  3820. tmp.slen = 0;
  3821. }
  3822. pjsua_msg_data_init(&msg_data);
  3823. TEST_MULTIPART(&msg_data);
  3824. pjsua_call_make_call( current_acc, &tmp, &call_opt, NULL, &msg_data, NULL);
  3825. break;
  3826. case 'M':
  3827. /* Make multiple calls! : */
  3828. printf("(You currently have %d calls)\n",
  3829. pjsua_call_get_count());
  3830. if (!simple_input("Number of calls", menuin, sizeof(menuin)))
  3831. continue;
  3832. count = my_atoi(menuin);
  3833. if (count < 1)
  3834. continue;
  3835. ui_input_url("Make call", buf, sizeof(buf), &result);
  3836. if (result.nb_result != NO_NB) {
  3837. pjsua_buddy_info binfo;
  3838. if (result.nb_result == -1 || result.nb_result == 0) {
  3839. puts("You can't do that with make call!");
  3840. continue;
  3841. }
  3842. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  3843. tmp.ptr = buf;
  3844. pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
  3845. } else {
  3846. tmp = pj_str(result.uri_result);
  3847. }
  3848. for (i=0; i<my_atoi(menuin); ++i) {
  3849. pj_status_t status;
  3850. status = pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL,
  3851. NULL, NULL);
  3852. if (status != PJ_SUCCESS)
  3853. break;
  3854. }
  3855. break;
  3856. case 'n':
  3857. i = pjsua_detect_nat_type();
  3858. if (i != PJ_SUCCESS)
  3859. pjsua_perror(THIS_FILE, "Error", i);
  3860. break;
  3861. case 'i':
  3862. /* Send instant messaeg */
  3863. /* i is for call index to send message, if any */
  3864. i = -1;
  3865. /* Make compiler happy. */
  3866. uri = NULL;
  3867. /* Input destination. */
  3868. ui_input_url("Send IM to", buf, sizeof(buf), &result);
  3869. if (result.nb_result != NO_NB) {
  3870. if (result.nb_result == -1) {
  3871. puts("You can't send broadcast IM like that!");
  3872. continue;
  3873. } else if (result.nb_result == 0) {
  3874. i = current_call;
  3875. } else {
  3876. pjsua_buddy_info binfo;
  3877. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  3878. tmp.ptr = buf;
  3879. pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
  3880. uri = buf;
  3881. }
  3882. } else if (result.uri_result) {
  3883. uri = result.uri_result;
  3884. }
  3885. /* Send typing indication. */
  3886. if (i != -1)
  3887. pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
  3888. else {
  3889. pj_str_t tmp_uri = pj_str(uri);
  3890. pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
  3891. }
  3892. /* Input the IM . */
  3893. if (!simple_input("Message", text, sizeof(text))) {
  3894. /*
  3895. * Cancelled.
  3896. * Send typing notification too, saying we're not typing.
  3897. */
  3898. if (i != -1)
  3899. pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
  3900. else {
  3901. pj_str_t tmp_uri = pj_str(uri);
  3902. pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
  3903. }
  3904. continue;
  3905. }
  3906. tmp = pj_str(text);
  3907. /* Send the IM */
  3908. if (i != -1)
  3909. pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
  3910. else {
  3911. pj_str_t tmp_uri = pj_str(uri);
  3912. pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
  3913. }
  3914. break;
  3915. case 'a':
  3916. if (current_call != -1) {
  3917. pjsua_call_get_info(current_call, &call_info);
  3918. } else {
  3919. /* Make compiler happy */
  3920. call_info.role = PJSIP_ROLE_UAC;
  3921. call_info.state = PJSIP_INV_STATE_DISCONNECTED;
  3922. }
  3923. if (current_call == -1 ||
  3924. call_info.role != PJSIP_ROLE_UAS ||
  3925. call_info.state >= PJSIP_INV_STATE_CONNECTING)
  3926. {
  3927. puts("No pending incoming call");
  3928. fflush(stdout);
  3929. continue;
  3930. } else {
  3931. int st_code;
  3932. char contact[120];
  3933. pj_str_t hname = { "Contact", 7 };
  3934. pj_str_t hvalue;
  3935. pjsip_generic_string_hdr hcontact;
  3936. if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
  3937. continue;
  3938. st_code = my_atoi(buf);
  3939. if (st_code < 100)
  3940. continue;
  3941. pjsua_msg_data_init(&msg_data);
  3942. if (st_code/100 == 3) {
  3943. if (!simple_input("Enter URL to be put in Contact",
  3944. contact, sizeof(contact)))
  3945. continue;
  3946. hvalue = pj_str(contact);
  3947. pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
  3948. pj_list_push_back(&msg_data.hdr_list, &hcontact);
  3949. }
  3950. /*
  3951. * Must check again!
  3952. * Call may have been disconnected while we're waiting for
  3953. * keyboard input.
  3954. */
  3955. if (current_call == -1) {
  3956. puts("Call has been disconnected");
  3957. fflush(stdout);
  3958. continue;
  3959. }
  3960. pjsua_call_answer2(current_call, &call_opt, st_code, NULL, &msg_data);
  3961. }
  3962. break;
  3963. case 'h':
  3964. if (current_call == -1) {
  3965. puts("No current call");
  3966. fflush(stdout);
  3967. continue;
  3968. } else if (menuin[1] == 'a') {
  3969. /* Hangup all calls */
  3970. pjsua_call_hangup_all();
  3971. } else {
  3972. /* Hangup current calls */
  3973. pjsua_call_hangup(current_call, 0, NULL, NULL);
  3974. }
  3975. break;
  3976. case ']':
  3977. case '[':
  3978. /*
  3979. * Cycle next/prev dialog.
  3980. */
  3981. if (menuin[0] == ']') {
  3982. find_next_call();
  3983. } else {
  3984. find_prev_call();
  3985. }
  3986. if (current_call != -1) {
  3987. pjsua_call_get_info(current_call, &call_info);
  3988. PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
  3989. (int)call_info.remote_info.slen,
  3990. call_info.remote_info.ptr));
  3991. } else {
  3992. PJ_LOG(3,(THIS_FILE,"No current dialog"));
  3993. }
  3994. break;
  3995. case '>':
  3996. case '<':
  3997. if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
  3998. break;
  3999. i = my_atoi(buf);
  4000. if (pjsua_acc_is_valid(i)) {
  4001. pjsua_acc_set_default(i);
  4002. PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
  4003. } else {
  4004. PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
  4005. }
  4006. break;
  4007. case '+':
  4008. if (menuin[1] == 'b') {
  4009. pjsua_buddy_config buddy_cfg;
  4010. pjsua_buddy_id buddy_id;
  4011. pj_status_t status;
  4012. if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
  4013. break;
  4014. if (pjsua_verify_url(buf) != PJ_SUCCESS) {
  4015. printf("Invalid URI '%s'\n", buf);
  4016. break;
  4017. }
  4018. pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
  4019. buddy_cfg.uri = pj_str(buf);
  4020. buddy_cfg.subscribe = PJ_TRUE;
  4021. status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
  4022. if (status == PJ_SUCCESS) {
  4023. printf("New buddy '%s' added at index %d\n",
  4024. buf, buddy_id+1);
  4025. }
  4026. } else if (menuin[1] == 'a') {
  4027. char id[80], registrar[80], realm[80], uname[80], passwd[30];
  4028. pjsua_acc_config acc_cfg;
  4029. pj_status_t status;
  4030. if (!simple_input("Your SIP URL:", id, sizeof(id)))
  4031. break;
  4032. if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
  4033. break;
  4034. if (!simple_input("Auth Realm:", realm, sizeof(realm)))
  4035. break;
  4036. if (!simple_input("Auth Username:", uname, sizeof(uname)))
  4037. break;
  4038. if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
  4039. break;
  4040. pjsua_acc_config_default(&acc_cfg);
  4041. acc_cfg.id = pj_str(id);
  4042. acc_cfg.reg_uri = pj_str(registrar);
  4043. acc_cfg.cred_count = 1;
  4044. acc_cfg.cred_info[0].scheme = pj_str("Digest");
  4045. acc_cfg.cred_info[0].realm = pj_str(realm);
  4046. acc_cfg.cred_info[0].username = pj_str(uname);
  4047. acc_cfg.cred_info[0].data_type = 0;
  4048. acc_cfg.cred_info[0].data = pj_str(passwd);
  4049. acc_cfg.rtp_cfg = app_config.rtp_cfg;
  4050. app_config_init_video(&acc_cfg);
  4051. status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
  4052. if (status != PJ_SUCCESS) {
  4053. pjsua_perror(THIS_FILE, "Error adding new account", status);
  4054. }
  4055. } else {
  4056. printf("Invalid input %s\n", menuin);
  4057. }
  4058. break;
  4059. case '-':
  4060. if (menuin[1] == 'b') {
  4061. if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
  4062. break;
  4063. i = my_atoi(buf) - 1;
  4064. if (!pjsua_buddy_is_valid(i)) {
  4065. printf("Invalid buddy id %d\n", i);
  4066. } else {
  4067. pjsua_buddy_del(i);
  4068. printf("Buddy %d deleted\n", i);
  4069. }
  4070. } else if (menuin[1] == 'a') {
  4071. if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
  4072. break;
  4073. i = my_atoi(buf);
  4074. if (!pjsua_acc_is_valid(i)) {
  4075. printf("Invalid account id %d\n", i);
  4076. } else {
  4077. pjsua_acc_del(i);
  4078. printf("Account %d deleted\n", i);
  4079. }
  4080. } else {
  4081. printf("Invalid input %s\n", menuin);
  4082. }
  4083. break;
  4084. case 'H':
  4085. /*
  4086. * Hold call.
  4087. */
  4088. if (current_call != -1) {
  4089. pjsua_call_set_hold(current_call, NULL);
  4090. } else {
  4091. PJ_LOG(3,(THIS_FILE, "No current call"));
  4092. }
  4093. break;
  4094. case 'v':
  4095. #if PJSUA_HAS_VIDEO
  4096. if (menuin[1]=='i' && menuin[2]=='d' && menuin[3]==' ') {
  4097. vid_handle_menu(menuin);
  4098. } else
  4099. #endif
  4100. if (current_call != -1) {
  4101. /*
  4102. * re-INVITE
  4103. */
  4104. call_opt.flag |= PJSUA_CALL_UNHOLD;
  4105. pjsua_call_reinvite2(current_call, &call_opt, NULL);
  4106. } else {
  4107. PJ_LOG(3,(THIS_FILE, "No current call"));
  4108. }
  4109. break;
  4110. case 'U':
  4111. /*
  4112. * Send UPDATE
  4113. */
  4114. if (current_call != -1) {
  4115. pjsua_call_update2(current_call, &call_opt, NULL);
  4116. } else {
  4117. PJ_LOG(3,(THIS_FILE, "No current call"));
  4118. }
  4119. break;
  4120. case 'C':
  4121. if (menuin[1] == 'p') {
  4122. manage_codec_prio();
  4123. }
  4124. break;
  4125. case 'x':
  4126. /*
  4127. * Transfer call.
  4128. */
  4129. if (current_call == -1) {
  4130. PJ_LOG(3,(THIS_FILE, "No current call"));
  4131. } else {
  4132. int call = current_call;
  4133. pjsip_generic_string_hdr refer_sub;
  4134. pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
  4135. pj_str_t STR_FALSE = { "false", 5 };
  4136. pjsua_call_info ci;
  4137. pjsua_call_get_info(current_call, &ci);
  4138. printf("Transfering current call [%d] %.*s\n",
  4139. current_call,
  4140. (int)ci.remote_info.slen, ci.remote_info.ptr);
  4141. ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
  4142. /* Check if call is still there. */
  4143. if (call != current_call) {
  4144. puts("Call has been disconnected");
  4145. continue;
  4146. }
  4147. pjsua_msg_data_init(&msg_data);
  4148. if (app_config.no_refersub) {
  4149. /* Add Refer-Sub: false in outgoing REFER request */
  4150. pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
  4151. &STR_FALSE);
  4152. pj_list_push_back(&msg_data.hdr_list, &refer_sub);
  4153. }
  4154. if (result.nb_result != NO_NB) {
  4155. if (result.nb_result == -1 || result.nb_result == 0)
  4156. puts("You can't do that with transfer call!");
  4157. else {
  4158. pjsua_buddy_info binfo;
  4159. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  4160. pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
  4161. }
  4162. } else if (result.uri_result) {
  4163. pj_str_t tmp;
  4164. tmp = pj_str(result.uri_result);
  4165. pjsua_call_xfer( current_call, &tmp, &msg_data);
  4166. }
  4167. }
  4168. break;
  4169. case 'X':
  4170. /*
  4171. * Transfer call with replaces.
  4172. */
  4173. if (current_call == -1) {
  4174. PJ_LOG(3,(THIS_FILE, "No current call"));
  4175. } else {
  4176. int call = current_call;
  4177. int dst_call;
  4178. pjsip_generic_string_hdr refer_sub;
  4179. pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
  4180. pj_str_t STR_FALSE = { "false", 5 };
  4181. pjsua_call_id ids[PJSUA_MAX_CALLS];
  4182. pjsua_call_info ci;
  4183. unsigned i, count;
  4184. count = PJ_ARRAY_SIZE(ids);
  4185. pjsua_enum_calls(ids, &count);
  4186. if (count <= 1) {
  4187. puts("There are no other calls");
  4188. continue;
  4189. }
  4190. pjsua_call_get_info(current_call, &ci);
  4191. printf("Transfer call [%d] %.*s to one of the following:\n",
  4192. current_call,
  4193. (int)ci.remote_info.slen, ci.remote_info.ptr);
  4194. for (i=0; i<count; ++i) {
  4195. pjsua_call_info call_info;
  4196. if (ids[i] == call)
  4197. continue;
  4198. pjsua_call_get_info(ids[i], &call_info);
  4199. printf("%d %.*s [%.*s]\n",
  4200. ids[i],
  4201. (int)call_info.remote_info.slen,
  4202. call_info.remote_info.ptr,
  4203. (int)call_info.state_text.slen,
  4204. call_info.state_text.ptr);
  4205. }
  4206. if (!simple_input("Enter call number to be replaced",
  4207. buf, sizeof(buf)))
  4208. continue;
  4209. dst_call = my_atoi(buf);
  4210. /* Check if call is still there. */
  4211. if (call != current_call) {
  4212. puts("Call has been disconnected");
  4213. continue;
  4214. }
  4215. /* Check that destination call is valid. */
  4216. if (dst_call == call) {
  4217. puts("Destination call number must not be the same "
  4218. "as the call being transfered");
  4219. continue;
  4220. }
  4221. if (dst_call >= PJSUA_MAX_CALLS) {
  4222. puts("Invalid destination call number");
  4223. continue;
  4224. }
  4225. if (!pjsua_call_is_active(dst_call)) {
  4226. puts("Invalid destination call number");
  4227. continue;
  4228. }
  4229. pjsua_msg_data_init(&msg_data);
  4230. if (app_config.no_refersub) {
  4231. /* Add Refer-Sub: false in outgoing REFER request */
  4232. pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
  4233. &STR_FALSE);
  4234. pj_list_push_back(&msg_data.hdr_list, &refer_sub);
  4235. }
  4236. pjsua_call_xfer_replaces(call, dst_call,
  4237. PJSUA_XFER_NO_REQUIRE_REPLACES,
  4238. &msg_data);
  4239. }
  4240. break;
  4241. case '#':
  4242. /*
  4243. * Send DTMF strings.
  4244. */
  4245. if (current_call == -1) {
  4246. PJ_LOG(3,(THIS_FILE, "No current call"));
  4247. } else if (!pjsua_call_has_media(current_call)) {
  4248. PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
  4249. } else {
  4250. pj_str_t digits;
  4251. int call = current_call;
  4252. pj_status_t status;
  4253. if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
  4254. sizeof(buf)))
  4255. {
  4256. break;
  4257. }
  4258. if (call != current_call) {
  4259. puts("Call has been disconnected");
  4260. continue;
  4261. }
  4262. digits = pj_str(buf);
  4263. status = pjsua_call_dial_dtmf(current_call, &digits);
  4264. if (status != PJ_SUCCESS) {
  4265. pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
  4266. } else {
  4267. puts("DTMF digits enqueued for transmission");
  4268. }
  4269. }
  4270. break;
  4271. case '*':
  4272. /* Send DTMF with INFO */
  4273. if (current_call == -1) {
  4274. PJ_LOG(3,(THIS_FILE, "No current call"));
  4275. } else {
  4276. const pj_str_t SIP_INFO = pj_str("INFO");
  4277. pj_str_t digits;
  4278. int call = current_call;
  4279. int i;
  4280. pj_status_t status;
  4281. if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
  4282. sizeof(buf)))
  4283. {
  4284. break;
  4285. }
  4286. if (call != current_call) {
  4287. puts("Call has been disconnected");
  4288. continue;
  4289. }
  4290. digits = pj_str(buf);
  4291. for (i=0; i<digits.slen; ++i) {
  4292. char body[80];
  4293. pjsua_msg_data_init(&msg_data);
  4294. msg_data.content_type = pj_str("application/dtmf-relay");
  4295. pj_ansi_snprintf(body, sizeof(body),
  4296. "Signal=%c\r\n"
  4297. "Duration=160",
  4298. buf[i]);
  4299. msg_data.msg_body = pj_str(body);
  4300. status = pjsua_call_send_request(current_call, &SIP_INFO,
  4301. &msg_data);
  4302. if (status != PJ_SUCCESS) {
  4303. break;
  4304. }
  4305. }
  4306. }
  4307. break;
  4308. case 'S':
  4309. /*
  4310. * Send arbitrary request
  4311. */
  4312. if (pjsua_acc_get_count() == 0) {
  4313. puts("Sorry, need at least one account configured");
  4314. break;
  4315. }
  4316. puts("Send arbitrary request to remote host");
  4317. /* Input METHOD */
  4318. if (!simple_input("Request method:",text,sizeof(text)))
  4319. break;
  4320. /* Input destination URI */
  4321. uri = NULL;
  4322. ui_input_url("Destination URI", buf, sizeof(buf), &result);
  4323. if (result.nb_result != NO_NB) {
  4324. if (result.nb_result == -1) {
  4325. puts("Sorry you can't do that!");
  4326. continue;
  4327. } else if (result.nb_result == 0) {
  4328. uri = NULL;
  4329. if (current_call == PJSUA_INVALID_ID) {
  4330. puts("No current call");
  4331. continue;
  4332. }
  4333. } else {
  4334. pjsua_buddy_info binfo;
  4335. pjsua_buddy_get_info(result.nb_result-1, &binfo);
  4336. tmp.ptr = buf;
  4337. pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
  4338. uri = buf;
  4339. }
  4340. } else if (result.uri_result) {
  4341. uri = result.uri_result;
  4342. } else {
  4343. continue;
  4344. }
  4345. if (uri) {
  4346. tmp = pj_str(uri);
  4347. send_request(text, &tmp);
  4348. } else {
  4349. /* If you send call control request using this method
  4350. * (such requests includes BYE, CANCEL, etc.), it will
  4351. * not go well with the call state, so don't do it
  4352. * unless it's for testing.
  4353. */
  4354. pj_str_t method = pj_str(text);
  4355. pjsua_call_send_request(current_call, &method, NULL);
  4356. }
  4357. break;
  4358. case 'e':
  4359. if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
  4360. pj_str_t tmp;
  4361. tmp.ptr = menuin+5;
  4362. tmp.slen = pj_ansi_strlen(menuin)-6;
  4363. if (tmp.slen < 1) {
  4364. puts("Usage: echo [0|1]");
  4365. break;
  4366. }
  4367. cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
  4368. }
  4369. break;
  4370. case 's':
  4371. if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
  4372. pj_str_t tmp;
  4373. int delay;
  4374. tmp.ptr = menuin+6;
  4375. tmp.slen = pj_ansi_strlen(menuin)-7;
  4376. if (tmp.slen < 1) {
  4377. puts("Usage: sleep MSEC");
  4378. break;
  4379. }
  4380. delay = pj_strtoul(&tmp);
  4381. if (delay < 0) delay = 0;
  4382. pj_thread_sleep(delay);
  4383. break;
  4384. }
  4385. /* Continue below */
  4386. case 'u':
  4387. /*
  4388. * Subscribe/unsubscribe presence.
  4389. */
  4390. ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
  4391. if (result.nb_result != NO_NB) {
  4392. if (result.nb_result == -1) {
  4393. int i, count;
  4394. count = pjsua_get_buddy_count();
  4395. for (i=0; i<count; ++i)
  4396. pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
  4397. } else if (result.nb_result == 0) {
  4398. puts("Sorry, can only subscribe to buddy's presence, "
  4399. "not from existing call");
  4400. } else {
  4401. pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
  4402. }
  4403. } else if (result.uri_result) {
  4404. puts("Sorry, can only subscribe to buddy's presence, "
  4405. "not arbitrary URL (for now)");
  4406. }
  4407. break;
  4408. case 'r':
  4409. switch (menuin[1]) {
  4410. case 'r':
  4411. /*
  4412. * Re-Register.
  4413. */
  4414. pjsua_acc_set_registration(current_acc, PJ_TRUE);
  4415. break;
  4416. case 'u':
  4417. /*
  4418. * Unregister
  4419. */
  4420. pjsua_acc_set_registration(current_acc, PJ_FALSE);
  4421. break;
  4422. }
  4423. break;
  4424. case 't':
  4425. pjsua_acc_get_info(current_acc, &acc_info);
  4426. acc_info.online_status = !acc_info.online_status;
  4427. pjsua_acc_set_online_status(current_acc, acc_info.online_status);
  4428. printf("Setting %s online status to %s\n",
  4429. acc_info.acc_uri.ptr,
  4430. (acc_info.online_status?"online":"offline"));
  4431. break;
  4432. case 'T':
  4433. change_online_status();
  4434. break;
  4435. case 'c':
  4436. switch (menuin[1]) {
  4437. case 'l':
  4438. conf_list();
  4439. break;
  4440. case 'c':
  4441. case 'd':
  4442. {
  4443. char tmp[10], src_port[10], dst_port[10];
  4444. pj_status_t status;
  4445. int cnt;
  4446. const char *src_title, *dst_title;
  4447. cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
  4448. if (cnt != 3) {
  4449. conf_list();
  4450. src_title = (menuin[1]=='c'?
  4451. "Connect src port #":
  4452. "Disconnect src port #");
  4453. dst_title = (menuin[1]=='c'?
  4454. "To dst port #":
  4455. "From dst port #");
  4456. if (!simple_input(src_title, src_port, sizeof(src_port)))
  4457. break;
  4458. if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
  4459. break;
  4460. }
  4461. if (menuin[1]=='c') {
  4462. status = pjsua_conf_connect(my_atoi(src_port),
  4463. my_atoi(dst_port));
  4464. } else {
  4465. status = pjsua_conf_disconnect(my_atoi(src_port),
  4466. my_atoi(dst_port));
  4467. }
  4468. if (status == PJ_SUCCESS) {
  4469. puts("Success");
  4470. } else {
  4471. puts("ERROR!!");
  4472. }
  4473. }
  4474. break;
  4475. }
  4476. break;
  4477. case 'V':
  4478. /* Adjust audio volume */
  4479. sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
  4480. if (simple_input(buf,text,sizeof(text))) {
  4481. char *err;
  4482. app_config.mic_level = (float)strtod(text, &err);
  4483. pjsua_conf_adjust_rx_level(0, app_config.mic_level);
  4484. }
  4485. sprintf(buf, "Adjust speaker level: [%4.1fx] ",
  4486. app_config.speaker_level);
  4487. if (simple_input(buf,text,sizeof(text))) {
  4488. char *err;
  4489. app_config.speaker_level = (float)strtod(text, &err);
  4490. pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
  4491. }
  4492. break;
  4493. case 'd':
  4494. if (menuin[1] == 'c') {
  4495. char settings[2000];
  4496. int len;
  4497. len = write_settings(&app_config, settings, sizeof(settings));
  4498. if (len < 1)
  4499. PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
  4500. else
  4501. PJ_LOG(3,(THIS_FILE,
  4502. "Dumping configuration (%d bytes):\n%s\n",
  4503. len, settings));
  4504. } else if (menuin[1] == 'q') {
  4505. if (current_call != PJSUA_INVALID_ID) {
  4506. log_call_dump(current_call);
  4507. } else {
  4508. PJ_LOG(3,(THIS_FILE, "No current call"));
  4509. }
  4510. } else {
  4511. app_dump(menuin[1]=='d');
  4512. }
  4513. break;
  4514. case 'f':
  4515. if (simple_input("Enter output filename", buf, sizeof(buf))) {
  4516. char settings[2000];
  4517. int len;
  4518. len = write_settings(&app_config, settings, sizeof(settings));
  4519. if (len < 1)
  4520. PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
  4521. else {
  4522. pj_oshandle_t fd;
  4523. pj_status_t status;
  4524. status = pj_file_open(app_config.pool, buf,
  4525. PJ_O_WRONLY, &fd);
  4526. if (status != PJ_SUCCESS) {
  4527. pjsua_perror(THIS_FILE, "Unable to open file", status);
  4528. } else {
  4529. pj_ssize_t size = len;
  4530. pj_file_write(fd, settings, &size);
  4531. pj_file_close(fd);
  4532. printf("Settings successfully written to '%s'\n", buf);
  4533. }
  4534. }
  4535. }
  4536. break;
  4537. case 'L': /* Restart */
  4538. app_restart = PJ_TRUE;
  4539. /* Continues below */
  4540. case 'q':
  4541. goto on_exit;
  4542. case 'R':
  4543. if (!pjsua_call_is_active(current_call)) {
  4544. PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
  4545. } else if (menuin[1] == 'a') {
  4546. pjsua_call_process_redirect(current_call,
  4547. PJSIP_REDIRECT_ACCEPT);
  4548. } else if (menuin[1] == 'r') {
  4549. pjsua_call_process_redirect(current_call,
  4550. PJSIP_REDIRECT_REJECT);
  4551. } else {
  4552. pjsua_call_process_redirect(current_call,
  4553. PJSIP_REDIRECT_STOP);
  4554. }
  4555. break;
  4556. default:
  4557. if (menuin[0] != '\n' && menuin[0] != '\r') {
  4558. printf("Invalid input %s", menuin);
  4559. }
  4560. keystroke_help();
  4561. break;
  4562. }
  4563. }
  4564. on_exit:
  4565. ;
  4566. }
  4567. /*
  4568. * A simple registrar, invoked by default_mod_on_rx_request()
  4569. */
  4570. static void simple_registrar(pjsip_rx_data *rdata)
  4571. {
  4572. pjsip_tx_data *tdata;
  4573. const pjsip_expires_hdr *exp;
  4574. const pjsip_hdr *h;
  4575. unsigned cnt = 0;
  4576. pjsip_generic_string_hdr *srv;
  4577. pj_status_t status;
  4578. status = pjsip_endpt_create_response(pjsua_get_pjsip_endpt(),
  4579. rdata, 200, NULL, &tdata);
  4580. if (status != PJ_SUCCESS)
  4581. return;
  4582. exp = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
  4583. h = rdata->msg_info.msg->hdr.next;
  4584. while (h != &rdata->msg_info.msg->hdr) {
  4585. if (h->type == PJSIP_H_CONTACT) {
  4586. const pjsip_contact_hdr *c = (const pjsip_contact_hdr*)h;
  4587. int e = c->expires;
  4588. if (e < 0) {
  4589. if (exp)
  4590. e = exp->ivalue;
  4591. else
  4592. e = 3600;
  4593. }
  4594. if (e > 0) {
  4595. pjsip_contact_hdr *nc = pjsip_hdr_clone(tdata->pool, h);
  4596. nc->expires = e;
  4597. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)nc);
  4598. ++cnt;
  4599. }
  4600. }
  4601. h = h->next;
  4602. }
  4603. srv = pjsip_generic_string_hdr_create(tdata->pool, NULL, NULL);
  4604. srv->name = pj_str("Server");
  4605. srv->hvalue = pj_str("pjsua simple registrar");
  4606. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)srv);
  4607. pjsip_endpt_send_response2(pjsua_get_pjsip_endpt(),
  4608. rdata, tdata, NULL, NULL);
  4609. }
  4610. /*****************************************************************************
  4611. * A simple module to handle otherwise unhandled request. We will register
  4612. * this with the lowest priority.
  4613. */
  4614. /* Notification on incoming request */
  4615. static pj_bool_t default_mod_on_rx_request(pjsip_rx_data *rdata)
  4616. {
  4617. pjsip_tx_data *tdata;
  4618. pjsip_status_code status_code;
  4619. pj_status_t status;
  4620. /* Don't respond to ACK! */
  4621. if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
  4622. &pjsip_ack_method) == 0)
  4623. return PJ_TRUE;
  4624. /* Simple registrar */
  4625. if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
  4626. &pjsip_register_method) == 0)
  4627. {
  4628. simple_registrar(rdata);
  4629. return PJ_TRUE;
  4630. }
  4631. /* Create basic response. */
  4632. if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
  4633. &pjsip_notify_method) == 0)
  4634. {
  4635. /* Unsolicited NOTIFY's, send with Bad Request */
  4636. status_code = PJSIP_SC_BAD_REQUEST;
  4637. } else {
  4638. /* Probably unknown method */
  4639. status_code = PJSIP_SC_METHOD_NOT_ALLOWED;
  4640. }
  4641. status = pjsip_endpt_create_response(pjsua_get_pjsip_endpt(),
  4642. rdata, status_code,
  4643. NULL, &tdata);
  4644. if (status != PJ_SUCCESS) {
  4645. pjsua_perror(THIS_FILE, "Unable to create response", status);
  4646. return PJ_TRUE;
  4647. }
  4648. /* Add Allow if we're responding with 405 */
  4649. if (status_code == PJSIP_SC_METHOD_NOT_ALLOWED) {
  4650. const pjsip_hdr *cap_hdr;
  4651. cap_hdr = pjsip_endpt_get_capability(pjsua_get_pjsip_endpt(),
  4652. PJSIP_H_ALLOW, NULL);
  4653. if (cap_hdr) {
  4654. pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool,
  4655. cap_hdr));
  4656. }
  4657. }
  4658. /* Add User-Agent header */
  4659. {
  4660. pj_str_t user_agent;
  4661. char tmp[80];
  4662. const pj_str_t USER_AGENT = { "User-Agent", 10};
  4663. pjsip_hdr *h;
  4664. pj_ansi_snprintf(tmp, sizeof(tmp), "PJSUA v%s/%s",
  4665. pj_get_version(), PJ_OS_NAME);
  4666. pj_strdup2_with_null(tdata->pool, &user_agent, tmp);
  4667. h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
  4668. &USER_AGENT,
  4669. &user_agent);
  4670. pjsip_msg_add_hdr(tdata->msg, h);
  4671. }
  4672. pjsip_endpt_send_response2(pjsua_get_pjsip_endpt(), rdata, tdata,
  4673. NULL, NULL);
  4674. return PJ_TRUE;
  4675. }
  4676. /* The module instance. */
  4677. static pjsip_module mod_default_handler =
  4678. {
  4679. NULL, NULL, /* prev, next. */
  4680. { "mod-default-handler", 19 }, /* Name. */
  4681. -1, /* Id */
  4682. PJSIP_MOD_PRIORITY_APPLICATION+99, /* Priority */
  4683. NULL, /* load() */
  4684. NULL, /* start() */
  4685. NULL, /* stop() */
  4686. NULL, /* unload() */
  4687. &default_mod_on_rx_request, /* on_rx_request() */
  4688. NULL, /* on_rx_response() */
  4689. NULL, /* on_tx_request. */
  4690. NULL, /* on_tx_response() */
  4691. NULL, /* on_tsx_state() */
  4692. };
  4693. /*****************************************************************************
  4694. * Public API
  4695. */
  4696. pj_status_t app_init(int argc, char *argv[])
  4697. {
  4698. pjsua_transport_id transport_id = -1;
  4699. pjsua_transport_config tcp_cfg;
  4700. unsigned i;
  4701. pj_status_t status;
  4702. app_restart = PJ_FALSE;
  4703. /* Create pjsua */
  4704. status = pjsua_create();
  4705. if (status != PJ_SUCCESS)
  4706. return status;
  4707. /* Create pool for application */
  4708. app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
  4709. /* Initialize default config */
  4710. default_config(&app_config);
  4711. /* Parse the arguments */
  4712. status = parse_args(argc, argv, &app_config, &uri_arg);
  4713. if (status != PJ_SUCCESS)
  4714. return status;
  4715. /* Initialize application callbacks */
  4716. app_config.cfg.cb.on_call_state = &on_call_state;
  4717. app_config.cfg.cb.on_call_media_state = &on_call_media_state;
  4718. app_config.cfg.cb.on_incoming_call = &on_incoming_call;
  4719. app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
  4720. app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
  4721. app_config.cfg.cb.on_call_redirected = &call_on_redirected;
  4722. app_config.cfg.cb.on_reg_state = &on_reg_state;
  4723. app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
  4724. app_config.cfg.cb.on_buddy_state = &on_buddy_state;
  4725. app_config.cfg.cb.on_buddy_evsub_state = &on_buddy_evsub_state;
  4726. app_config.cfg.cb.on_pager = &on_pager;
  4727. app_config.cfg.cb.on_typing = &on_typing;
  4728. app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
  4729. app_config.cfg.cb.on_call_replaced = &on_call_replaced;
  4730. app_config.cfg.cb.on_nat_detect = &on_nat_detect;
  4731. app_config.cfg.cb.on_mwi_info = &on_mwi_info;
  4732. app_config.cfg.cb.on_transport_state = &on_transport_state;
  4733. app_config.cfg.cb.on_ice_transport_error = &on_ice_transport_error;
  4734. app_config.cfg.cb.on_snd_dev_operation = &on_snd_dev_operation;
  4735. app_config.cfg.cb.on_call_media_event = &on_call_media_event;
  4736. #ifdef TRANSPORT_ADAPTER_SAMPLE
  4737. app_config.cfg.cb.on_create_media_transport = &on_create_media_transport;
  4738. #endif
  4739. app_config.log_cfg.cb = log_cb;
  4740. /* Set sound device latency */
  4741. if (app_config.capture_lat > 0)
  4742. app_config.media_cfg.snd_rec_latency = app_config.capture_lat;
  4743. if (app_config.playback_lat)
  4744. app_config.media_cfg.snd_play_latency = app_config.playback_lat;
  4745. /* Initialize pjsua */
  4746. status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
  4747. &app_config.media_cfg);
  4748. if (status != PJ_SUCCESS)
  4749. return status;
  4750. /* Initialize our module to handle otherwise unhandled request */
  4751. status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
  4752. &mod_default_handler);
  4753. if (status != PJ_SUCCESS)
  4754. return status;
  4755. #ifdef STEREO_DEMO
  4756. stereo_demo();
  4757. #endif
  4758. /* Initialize calls data */
  4759. for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
  4760. app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
  4761. app_config.call_data[i].timer.cb = &call_timeout_callback;
  4762. }
  4763. /* Optionally registers WAV file */
  4764. for (i=0; i<app_config.wav_count; ++i) {
  4765. pjsua_player_id wav_id;
  4766. unsigned play_options = 0;
  4767. if (app_config.auto_play_hangup)
  4768. play_options |= PJMEDIA_FILE_NO_LOOP;
  4769. status = pjsua_player_create(&app_config.wav_files[i], play_options,
  4770. &wav_id);
  4771. if (status != PJ_SUCCESS)
  4772. goto on_error;
  4773. if (app_config.wav_id == PJSUA_INVALID_ID) {
  4774. app_config.wav_id = wav_id;
  4775. app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
  4776. if (app_config.auto_play_hangup) {
  4777. pjmedia_port *port;
  4778. pjsua_player_get_port(app_config.wav_id, &port);
  4779. status = pjmedia_wav_player_set_eof_cb(port, NULL,
  4780. &on_playfile_done);
  4781. if (status != PJ_SUCCESS)
  4782. goto on_error;
  4783. pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
  4784. &hangup_timeout_callback);
  4785. }
  4786. }
  4787. }
  4788. /* Optionally registers tone players */
  4789. for (i=0; i<app_config.tone_count; ++i) {
  4790. pjmedia_port *tport;
  4791. char name[80];
  4792. pj_str_t label;
  4793. pj_status_t status;
  4794. pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
  4795. app_config.tones[i].freq1,
  4796. app_config.tones[i].freq2);
  4797. label = pj_str(name);
  4798. status = pjmedia_tonegen_create2(app_config.pool, &label,
  4799. 8000, 1, 160, 16,
  4800. PJMEDIA_TONEGEN_LOOP, &tport);
  4801. if (status != PJ_SUCCESS) {
  4802. pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
  4803. goto on_error;
  4804. }
  4805. status = pjsua_conf_add_port(app_config.pool, tport,
  4806. &app_config.tone_slots[i]);
  4807. pj_assert(status == PJ_SUCCESS);
  4808. status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
  4809. pj_assert(status == PJ_SUCCESS);
  4810. }
  4811. /* Optionally create recorder file, if any. */
  4812. if (app_config.rec_file.slen) {
  4813. status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
  4814. &app_config.rec_id);
  4815. if (status != PJ_SUCCESS)
  4816. goto on_error;
  4817. app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
  4818. }
  4819. pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
  4820. /* Create ringback tones */
  4821. if (app_config.no_tones == PJ_FALSE) {
  4822. unsigned i, samples_per_frame;
  4823. pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
  4824. pj_str_t name;
  4825. samples_per_frame = app_config.media_cfg.audio_frame_ptime *
  4826. app_config.media_cfg.clock_rate *
  4827. app_config.media_cfg.channel_count / 1000;
  4828. /* Ringback tone (call is ringing) */
  4829. name = pj_str("ringback");
  4830. status = pjmedia_tonegen_create2(app_config.pool, &name,
  4831. app_config.media_cfg.clock_rate,
  4832. app_config.media_cfg.channel_count,
  4833. samples_per_frame,
  4834. 16, PJMEDIA_TONEGEN_LOOP,
  4835. &app_config.ringback_port);
  4836. if (status != PJ_SUCCESS)
  4837. goto on_error;
  4838. pj_bzero(&tone, sizeof(tone));
  4839. for (i=0; i<RINGBACK_CNT; ++i) {
  4840. tone[i].freq1 = RINGBACK_FREQ1;
  4841. tone[i].freq2 = RINGBACK_FREQ2;
  4842. tone[i].on_msec = RINGBACK_ON;
  4843. tone[i].off_msec = RINGBACK_OFF;
  4844. }
  4845. tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
  4846. pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
  4847. PJMEDIA_TONEGEN_LOOP);
  4848. status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
  4849. &app_config.ringback_slot);
  4850. if (status != PJ_SUCCESS)
  4851. goto on_error;
  4852. /* Ring (to alert incoming call) */
  4853. name = pj_str("ring");
  4854. status = pjmedia_tonegen_create2(app_config.pool, &name,
  4855. app_config.media_cfg.clock_rate,
  4856. app_config.media_cfg.channel_count,
  4857. samples_per_frame,
  4858. 16, PJMEDIA_TONEGEN_LOOP,
  4859. &app_config.ring_port);
  4860. if (status != PJ_SUCCESS)
  4861. goto on_error;
  4862. for (i=0; i<RING_CNT; ++i) {
  4863. tone[i].freq1 = RING_FREQ1;
  4864. tone[i].freq2 = RING_FREQ2;
  4865. tone[i].on_msec = RING_ON;
  4866. tone[i].off_msec = RING_OFF;
  4867. }
  4868. tone[RING_CNT-1].off_msec = RING_INTERVAL;
  4869. pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
  4870. tone, PJMEDIA_TONEGEN_LOOP);
  4871. status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
  4872. &app_config.ring_slot);
  4873. if (status != PJ_SUCCESS)
  4874. goto on_error;
  4875. }
  4876. /* Create AVI player virtual devices */
  4877. if (app_config.avi_cnt) {
  4878. #if PJMEDIA_HAS_VIDEO && PJMEDIA_VIDEO_DEV_HAS_AVI
  4879. pjmedia_vid_dev_factory *avi_factory;
  4880. status = pjmedia_avi_dev_create_factory(pjsua_get_pool_factory(),
  4881. app_config.avi_cnt,
  4882. &avi_factory);
  4883. if (status != PJ_SUCCESS) {
  4884. PJ_PERROR(1,(THIS_FILE, status, "Error creating AVI factory"));
  4885. goto on_error;
  4886. }
  4887. for (i=0; i<app_config.avi_cnt; ++i) {
  4888. pjmedia_avi_dev_param avdp;
  4889. pjmedia_vid_dev_index avid;
  4890. unsigned strm_idx, strm_cnt;
  4891. app_config.avi[i].dev_id = PJMEDIA_VID_INVALID_DEV;
  4892. app_config.avi[i].slot = PJSUA_INVALID_ID;
  4893. pjmedia_avi_dev_param_default(&avdp);
  4894. avdp.path = app_config.avi[i].path;
  4895. status = pjmedia_avi_dev_alloc(avi_factory, &avdp, &avid);
  4896. if (status != PJ_SUCCESS) {
  4897. PJ_PERROR(1,(THIS_FILE, status,
  4898. "Error creating AVI player for %.*s",
  4899. (int)avdp.path.slen, avdp.path.ptr));
  4900. goto on_error;
  4901. }
  4902. PJ_LOG(4,(THIS_FILE, "AVI player %.*s created, dev_id=%d",
  4903. (int)avdp.title.slen, avdp.title.ptr, avid));
  4904. app_config.avi[i].dev_id = avid;
  4905. if (app_config.avi_def_idx == PJSUA_INVALID_ID)
  4906. app_config.avi_def_idx = i;
  4907. strm_cnt = pjmedia_avi_streams_get_num_streams(avdp.avi_streams);
  4908. for (strm_idx=0; strm_idx<strm_cnt; ++strm_idx) {
  4909. pjmedia_port *aud;
  4910. pjmedia_format *fmt;
  4911. pjsua_conf_port_id slot;
  4912. char fmt_name[5];
  4913. aud = pjmedia_avi_streams_get_stream(avdp.avi_streams,
  4914. strm_idx);
  4915. fmt = &aud->info.fmt;
  4916. pjmedia_fourcc_name(fmt->id, fmt_name);
  4917. if (fmt->id == PJMEDIA_FORMAT_PCM) {
  4918. status = pjsua_conf_add_port(app_config.pool, aud,
  4919. &slot);
  4920. if (status == PJ_SUCCESS) {
  4921. PJ_LOG(4,(THIS_FILE,
  4922. "AVI %.*s: audio added to slot %d",
  4923. (int)avdp.title.slen, avdp.title.ptr,
  4924. slot));
  4925. app_config.avi[i].slot = slot;
  4926. }
  4927. } else {
  4928. PJ_LOG(4,(THIS_FILE,
  4929. "AVI %.*s: audio ignored, format=%s",
  4930. (int)avdp.title.slen, avdp.title.ptr,
  4931. fmt_name));
  4932. }
  4933. }
  4934. }
  4935. #else
  4936. PJ_LOG(2,(THIS_FILE,
  4937. "Warning: --play-avi is ignored because AVI is disabled"));
  4938. #endif /* PJMEDIA_VIDEO_DEV_HAS_AVI */
  4939. }
  4940. /* Add UDP transport unless it's disabled. */
  4941. if (!app_config.no_udp) {
  4942. pjsua_acc_id aid;
  4943. pjsip_transport_type_e type = PJSIP_TRANSPORT_UDP;
  4944. status = pjsua_transport_create(type,
  4945. &app_config.udp_cfg,
  4946. &transport_id);
  4947. if (status != PJ_SUCCESS)
  4948. goto on_error;
  4949. /* Add local account */
  4950. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  4951. if (PJMEDIA_HAS_VIDEO) {
  4952. pjsua_acc_config acc_cfg;
  4953. pjsua_acc_get_config(aid, &acc_cfg);
  4954. app_config_init_video(&acc_cfg);
  4955. pjsua_acc_modify(aid, &acc_cfg);
  4956. }
  4957. //pjsua_acc_set_transport(aid, transport_id);
  4958. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  4959. if (app_config.udp_cfg.port == 0) {
  4960. pjsua_transport_info ti;
  4961. pj_sockaddr_in *a;
  4962. pjsua_transport_get_info(transport_id, &ti);
  4963. a = (pj_sockaddr_in*)&ti.local_addr;
  4964. tcp_cfg.port = pj_ntohs(a->sin_port);
  4965. }
  4966. }
  4967. /* Add UDP IPv6 transport unless it's disabled. */
  4968. if (!app_config.no_udp && app_config.ipv6) {
  4969. pjsua_acc_id aid;
  4970. pjsip_transport_type_e type = PJSIP_TRANSPORT_UDP6;
  4971. pjsua_transport_config udp_cfg;
  4972. udp_cfg = app_config.udp_cfg;
  4973. if (udp_cfg.port == 0)
  4974. udp_cfg.port = 5060;
  4975. else
  4976. udp_cfg.port += 10;
  4977. status = pjsua_transport_create(type,
  4978. &udp_cfg,
  4979. &transport_id);
  4980. if (status != PJ_SUCCESS)
  4981. goto on_error;
  4982. /* Add local account */
  4983. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  4984. if (PJMEDIA_HAS_VIDEO) {
  4985. pjsua_acc_config acc_cfg;
  4986. pjsua_acc_get_config(aid, &acc_cfg);
  4987. app_config_init_video(&acc_cfg);
  4988. if (app_config.ipv6)
  4989. acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
  4990. pjsua_acc_modify(aid, &acc_cfg);
  4991. }
  4992. //pjsua_acc_set_transport(aid, transport_id);
  4993. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  4994. if (app_config.udp_cfg.port == 0) {
  4995. pjsua_transport_info ti;
  4996. pj_sockaddr_in *a;
  4997. pjsua_transport_get_info(transport_id, &ti);
  4998. a = (pj_sockaddr_in*)&ti.local_addr;
  4999. tcp_cfg.port = pj_ntohs(a->sin_port);
  5000. }
  5001. }
  5002. /* Add TCP transport unless it's disabled */
  5003. if (!app_config.no_tcp) {
  5004. pjsua_acc_id aid;
  5005. status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
  5006. &tcp_cfg,
  5007. &transport_id);
  5008. if (status != PJ_SUCCESS)
  5009. goto on_error;
  5010. /* Add local account */
  5011. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  5012. if (PJMEDIA_HAS_VIDEO) {
  5013. pjsua_acc_config acc_cfg;
  5014. pjsua_acc_get_config(aid, &acc_cfg);
  5015. app_config_init_video(&acc_cfg);
  5016. pjsua_acc_modify(aid, &acc_cfg);
  5017. }
  5018. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  5019. }
  5020. /* Add TCP IPv6 transport unless it's disabled. */
  5021. if (!app_config.no_tcp && app_config.ipv6) {
  5022. pjsua_acc_id aid;
  5023. pjsip_transport_type_e type = PJSIP_TRANSPORT_TCP6;
  5024. tcp_cfg.port += 10;
  5025. status = pjsua_transport_create(type,
  5026. &tcp_cfg,
  5027. &transport_id);
  5028. if (status != PJ_SUCCESS)
  5029. goto on_error;
  5030. /* Add local account */
  5031. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  5032. if (PJMEDIA_HAS_VIDEO) {
  5033. pjsua_acc_config acc_cfg;
  5034. pjsua_acc_get_config(aid, &acc_cfg);
  5035. app_config_init_video(&acc_cfg);
  5036. if (app_config.ipv6)
  5037. acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
  5038. pjsua_acc_modify(aid, &acc_cfg);
  5039. }
  5040. //pjsua_acc_set_transport(aid, transport_id);
  5041. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  5042. }
  5043. #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
  5044. /* Add TLS transport when application wants one */
  5045. if (app_config.use_tls) {
  5046. pjsua_acc_id acc_id;
  5047. /* Copy the QoS settings */
  5048. tcp_cfg.tls_setting.qos_type = tcp_cfg.qos_type;
  5049. pj_memcpy(&tcp_cfg.tls_setting.qos_params, &tcp_cfg.qos_params,
  5050. sizeof(tcp_cfg.qos_params));
  5051. /* Set TLS port as TCP port+1 */
  5052. tcp_cfg.port++;
  5053. status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
  5054. &tcp_cfg,
  5055. &transport_id);
  5056. tcp_cfg.port--;
  5057. if (status != PJ_SUCCESS)
  5058. goto on_error;
  5059. /* Add local account */
  5060. pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
  5061. if (PJMEDIA_HAS_VIDEO) {
  5062. pjsua_acc_config acc_cfg;
  5063. pjsua_acc_get_config(acc_id, &acc_cfg);
  5064. app_config_init_video(&acc_cfg);
  5065. pjsua_acc_modify(acc_id, &acc_cfg);
  5066. }
  5067. pjsua_acc_set_online_status(acc_id, PJ_TRUE);
  5068. }
  5069. /* Add TLS IPv6 transport unless it's disabled. */
  5070. if (app_config.use_tls && app_config.ipv6) {
  5071. pjsua_acc_id aid;
  5072. pjsip_transport_type_e type = PJSIP_TRANSPORT_TLS6;
  5073. tcp_cfg.port += 10;
  5074. status = pjsua_transport_create(type,
  5075. &tcp_cfg,
  5076. &transport_id);
  5077. if (status != PJ_SUCCESS)
  5078. goto on_error;
  5079. /* Add local account */
  5080. pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
  5081. if (PJMEDIA_HAS_VIDEO) {
  5082. pjsua_acc_config acc_cfg;
  5083. pjsua_acc_get_config(aid, &acc_cfg);
  5084. app_config_init_video(&acc_cfg);
  5085. if (app_config.ipv6)
  5086. acc_cfg.ipv6_media_use = PJSUA_IPV6_ENABLED;
  5087. pjsua_acc_modify(aid, &acc_cfg);
  5088. }
  5089. //pjsua_acc_set_transport(aid, transport_id);
  5090. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  5091. }
  5092. #endif
  5093. if (transport_id == -1) {
  5094. PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
  5095. status = -1;
  5096. goto on_error;
  5097. }
  5098. /* Add accounts */
  5099. for (i=0; i<app_config.acc_cnt; ++i) {
  5100. app_config.acc_cfg[i].rtp_cfg = app_config.rtp_cfg;
  5101. app_config.acc_cfg[i].reg_retry_interval = 300;
  5102. app_config.acc_cfg[i].reg_first_retry_interval = 60;
  5103. app_config_init_video(&app_config.acc_cfg[i]);
  5104. status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
  5105. if (status != PJ_SUCCESS)
  5106. goto on_error;
  5107. pjsua_acc_set_online_status(current_acc, PJ_TRUE);
  5108. }
  5109. /* Add buddies */
  5110. for (i=0; i<app_config.buddy_cnt; ++i) {
  5111. status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
  5112. if (status != PJ_SUCCESS) {
  5113. PJ_PERROR(1,(THIS_FILE, status, "Error adding buddy"));
  5114. goto on_error;
  5115. }
  5116. }
  5117. /* Optionally disable some codec */
  5118. for (i=0; i<app_config.codec_dis_cnt; ++i) {
  5119. pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
  5120. #if PJSUA_HAS_VIDEO
  5121. pjsua_vid_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
  5122. #endif
  5123. }
  5124. /* Optionally set codec orders */
  5125. for (i=0; i<app_config.codec_cnt; ++i) {
  5126. pjsua_codec_set_priority(&app_config.codec_arg[i],
  5127. (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
  5128. #if PJSUA_HAS_VIDEO
  5129. pjsua_vid_codec_set_priority(&app_config.codec_arg[i],
  5130. (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
  5131. #endif
  5132. }
  5133. /* Use null sound device? */
  5134. #ifndef STEREO_DEMO
  5135. if (app_config.null_audio) {
  5136. status = pjsua_set_null_snd_dev();
  5137. if (status != PJ_SUCCESS)
  5138. return status;
  5139. }
  5140. #endif
  5141. if (app_config.capture_dev != PJSUA_INVALID_ID ||
  5142. app_config.playback_dev != PJSUA_INVALID_ID)
  5143. {
  5144. status = pjsua_set_snd_dev(app_config.capture_dev,
  5145. app_config.playback_dev);
  5146. if (status != PJ_SUCCESS)
  5147. goto on_error;
  5148. }
  5149. return PJ_SUCCESS;
  5150. on_error:
  5151. app_destroy();
  5152. return status;
  5153. }
  5154. static int stdout_refresh_proc(void *arg)
  5155. {
  5156. PJ_UNUSED_ARG(arg);
  5157. /* Set thread to lowest priority so that it doesn't clobber
  5158. * stdout output
  5159. */
  5160. pj_thread_set_prio(pj_thread_this(),
  5161. pj_thread_get_prio_min(pj_thread_this()));
  5162. while (!stdout_refresh_quit) {
  5163. pj_thread_sleep(stdout_refresh * 1000);
  5164. puts(stdout_refresh_text);
  5165. fflush(stdout);
  5166. }
  5167. return 0;
  5168. }
  5169. pj_status_t app_main(void)
  5170. {
  5171. pj_thread_t *stdout_refresh_thread = NULL;
  5172. pj_status_t status;
  5173. /* Start pjsua */
  5174. status = pjsua_start();
  5175. if (status != PJ_SUCCESS) {
  5176. app_destroy();
  5177. return status;
  5178. }
  5179. /* Start console refresh thread */
  5180. if (stdout_refresh > 0) {
  5181. pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
  5182. NULL, 0, 0, &stdout_refresh_thread);
  5183. }
  5184. console_app_main(&uri_arg);
  5185. if (stdout_refresh_thread) {
  5186. stdout_refresh_quit = PJ_TRUE;
  5187. pj_thread_join(stdout_refresh_thread);
  5188. pj_thread_destroy(stdout_refresh_thread);
  5189. }
  5190. return PJ_SUCCESS;
  5191. }
  5192. pj_status_t app_destroy(void)
  5193. {
  5194. pj_status_t status;
  5195. unsigned i;
  5196. #ifdef STEREO_DEMO
  5197. if (app_config.snd) {
  5198. pjmedia_snd_port_destroy(app_config.snd);
  5199. app_config.snd = NULL;
  5200. }
  5201. if (app_config.sc_ch1) {
  5202. pjsua_conf_remove_port(app_config.sc_ch1_slot);
  5203. app_config.sc_ch1_slot = PJSUA_INVALID_ID;
  5204. pjmedia_port_destroy(app_config.sc_ch1);
  5205. app_config.sc_ch1 = NULL;
  5206. }
  5207. if (app_config.sc) {
  5208. pjmedia_port_destroy(app_config.sc);
  5209. app_config.sc = NULL;
  5210. }
  5211. #endif
  5212. /* Close avi devs and ports */
  5213. for (i=0; i<app_config.avi_cnt; ++i) {
  5214. if (app_config.avi[i].slot != PJSUA_INVALID_ID)
  5215. pjsua_conf_remove_port(app_config.avi[i].slot);
  5216. #if PJMEDIA_HAS_VIDEO && PJMEDIA_VIDEO_DEV_HAS_AVI
  5217. if (app_config.avi[i].dev_id != PJMEDIA_VID_INVALID_DEV)
  5218. pjmedia_avi_dev_free(app_config.avi[i].dev_id);
  5219. #endif
  5220. }
  5221. /* Close ringback port */
  5222. if (app_config.ringback_port &&
  5223. app_config.ringback_slot != PJSUA_INVALID_ID)
  5224. {
  5225. pjsua_conf_remove_port(app_config.ringback_slot);
  5226. app_config.ringback_slot = PJSUA_INVALID_ID;
  5227. pjmedia_port_destroy(app_config.ringback_port);
  5228. app_config.ringback_port = NULL;
  5229. }
  5230. /* Close ring port */
  5231. if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
  5232. pjsua_conf_remove_port(app_config.ring_slot);
  5233. app_config.ring_slot = PJSUA_INVALID_ID;
  5234. pjmedia_port_destroy(app_config.ring_port);
  5235. app_config.ring_port = NULL;
  5236. }
  5237. /* Close tone generators */
  5238. for (i=0; i<app_config.tone_count; ++i) {
  5239. pjsua_conf_remove_port(app_config.tone_slots[i]);
  5240. }
  5241. if (app_config.pool) {
  5242. pj_pool_release(app_config.pool);
  5243. app_config.pool = NULL;
  5244. }
  5245. status = pjsua_destroy();
  5246. pj_bzero(&app_config, sizeof(app_config));
  5247. return status;
  5248. }
  5249. #ifdef STEREO_DEMO
  5250. /*
  5251. * In this stereo demo, we open the sound device in stereo mode and
  5252. * arrange the attachment to the PJSUA-LIB conference bridge as such
  5253. * so that channel0/left channel of the sound device corresponds to
  5254. * slot 0 in the bridge, and channel1/right channel of the sound
  5255. * device corresponds to slot 1 in the bridge. Then user can independently
  5256. * feed different media to/from the speakers/microphones channels, by
  5257. * connecting them to slot 0 or 1 respectively.
  5258. *
  5259. * Here's how the connection looks like:
  5260. *
  5261. +-----------+ stereo +-----------------+ 2x mono +-----------+
  5262. | AUDIO DEV |<------>| SPLITCOMB left|<------->|#0 BRIDGE |
  5263. +-----------+ | right|<------->|#1 |
  5264. +-----------------+ +-----------+
  5265. */
  5266. static void stereo_demo()
  5267. {
  5268. pjmedia_port *conf;
  5269. pj_status_t status;
  5270. /* Disable existing sound device */
  5271. conf = pjsua_set_no_snd_dev();
  5272. /* Create stereo-mono splitter/combiner */
  5273. status = pjmedia_splitcomb_create(app_config.pool,
  5274. conf->info.clock_rate /* clock rate */,
  5275. 2 /* stereo */,
  5276. 2 * conf->info.samples_per_frame,
  5277. conf->info.bits_per_sample,
  5278. 0 /* options */,
  5279. &app_config.sc);
  5280. pj_assert(status == PJ_SUCCESS);
  5281. /* Connect channel0 (left channel?) to conference port slot0 */
  5282. status = pjmedia_splitcomb_set_channel(app_config.sc, 0 /* ch0 */,
  5283. 0 /*options*/,
  5284. conf);
  5285. pj_assert(status == PJ_SUCCESS);
  5286. /* Create reverse channel for channel1 (right channel?)... */
  5287. status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
  5288. app_config.sc,
  5289. 1 /* ch1 */,
  5290. 0 /* options */,
  5291. &app_config.sc_ch1);
  5292. pj_assert(status == PJ_SUCCESS);
  5293. /* .. and register it to conference bridge (it would be slot1
  5294. * if there's no other devices connected to the bridge)
  5295. */
  5296. status = pjsua_conf_add_port(app_config.pool, app_config.sc_ch1,
  5297. &app_config.sc_ch1_slot);
  5298. pj_assert(status == PJ_SUCCESS);
  5299. /* Create sound device */
  5300. status = pjmedia_snd_port_create(app_config.pool, -1, -1,
  5301. conf->info.clock_rate,
  5302. 2 /* stereo */,
  5303. 2 * conf->info.samples_per_frame,
  5304. conf->info.bits_per_sample,
  5305. 0, &app_config.snd);
  5306. pj_assert(status == PJ_SUCCESS);
  5307. /* Connect the splitter to the sound device */
  5308. status = pjmedia_snd_port_connect(app_config.snd, app_config.sc);
  5309. pj_assert(status == PJ_SUCCESS);
  5310. }
  5311. #endif