blob: 87cb749fe67b3ca3d21bc9944741f1befd264d94 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00005 *
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
22
Benny Prijono4ab9fbb2007-10-12 12:14:27 +000023#define THIS_FILE "pjsua_app.c"
Benny Prijono804ff0a2006-09-14 11:17:48 +000024#define NO_LIMIT (int)0x7FFFFFFF
Benny Prijonoeebe9af2006-06-13 22:57:13 +000025
Benny Prijonoe909eac2006-07-27 22:04:56 +000026//#define STEREO_DEMO
Benny Prijono40860c32008-09-04 13:55:33 +000027//#define TRANSPORT_ADAPTER_SAMPLE
Benny Prijonoeebe9af2006-06-13 22:57:13 +000028
Benny Prijono4f966892008-06-14 22:43:56 +000029/* Ringtones US UK */
30#define RINGBACK_FREQ1 440 /* 400 */
31#define RINGBACK_FREQ2 480 /* 450 */
32#define RINGBACK_ON 2000 /* 400 */
33#define RINGBACK_OFF 4000 /* 200 */
34#define RINGBACK_CNT 1 /* 2 */
35#define RINGBACK_INTERVAL 4000 /* 2000 */
Benny Prijono91d20f42008-06-14 19:42:37 +000036
Benny Prijono4f966892008-06-14 22:43:56 +000037#define RING_FREQ1 800
38#define RING_FREQ2 640
39#define RING_ON 200
40#define RING_OFF 100
41#define RING_CNT 3
42#define RING_INTERVAL 3000
Benny Prijono91d20f42008-06-14 19:42:37 +000043
44
Benny Prijono804ff0a2006-09-14 11:17:48 +000045/* Call specific data */
46struct call_data
47{
48 pj_timer_entry timer;
Benny Prijono91d20f42008-06-14 19:42:37 +000049 pj_bool_t ringback_on;
50 pj_bool_t ring_on;
Benny Prijono804ff0a2006-09-14 11:17:48 +000051};
52
Benny Prijonoeebe9af2006-06-13 22:57:13 +000053
54/* Pjsua application data */
55static struct app_config
56{
57 pjsua_config cfg;
58 pjsua_logging_config log_cfg;
59 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000060 pj_bool_t no_refersub;
Benny Prijonoe93e2872006-06-28 16:46:49 +000061 pj_bool_t no_tcp;
62 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000063 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000064 pjsua_transport_config udp_cfg;
65 pjsua_transport_config rtp_cfg;
Benny Prijono5e51a4e2008-11-27 00:06:46 +000066 pjsip_redirect_op redir_op;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000067
68 unsigned acc_cnt;
69 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
70
71 unsigned buddy_cnt;
72 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
73
Benny Prijono804ff0a2006-09-14 11:17:48 +000074 struct call_data call_data[PJSUA_MAX_CALLS];
75
Benny Prijonoeebe9af2006-06-13 22:57:13 +000076 pj_pool_t *pool;
77 /* Compatibility with older pjsua */
78
79 unsigned codec_cnt;
80 pj_str_t codec_arg[32];
Benny Prijonofce28542007-12-09 15:41:10 +000081 unsigned codec_dis_cnt;
82 pj_str_t codec_dis[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000083 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000084 unsigned wav_count;
85 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000086 unsigned tone_count;
87 pjmedia_tone_desc tones[32];
88 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000089 pjsua_player_id wav_id;
90 pjsua_conf_port_id wav_port;
91 pj_bool_t auto_play;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +000092 pj_bool_t auto_play_hangup;
93 pj_timer_entry auto_hangup_timer;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000094 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000095 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000096 pj_str_t rec_file;
97 pj_bool_t auto_rec;
98 pjsua_recorder_id rec_id;
99 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000100 unsigned auto_answer;
101 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +0000102
103#ifdef STEREO_DEMO
104 pjmedia_snd_port *snd;
105#endif
106
Benny Prijono6dd967c2006-12-26 02:27:14 +0000107 float mic_level,
108 speaker_level;
109
Benny Prijono4e5d5512007-03-06 18:11:30 +0000110 int capture_dev, playback_dev;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000111 unsigned capture_lat, playback_lat;
Benny Prijono91d20f42008-06-14 19:42:37 +0000112
113 pj_bool_t no_tones;
114 int ringback_slot;
115 int ringback_cnt;
116 pjmedia_port *ringback_port;
117 int ring_slot;
118 int ring_cnt;
119 pjmedia_port *ring_port;
120
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000121} app_config;
122
123
Benny Prijono21b9ad92006-08-15 13:11:22 +0000124//static pjsua_acc_id current_acc;
125#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +0000126static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoebc32c32008-06-12 13:30:39 +0000127static pj_bool_t cmd_echo;
128static int stdout_refresh = -1;
129static const char *stdout_refresh_text = "STDOUT_REFRESH";
130static pj_bool_t stdout_refresh_quit = PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000131static pj_str_t uri_arg;
132
Nanang Izzuddin660eee82008-07-17 14:54:03 +0000133static char some_buf[1024 * 3];
Benny Prijono6eddd872008-03-21 13:46:08 +0000134
Benny Prijono594e4c52006-09-14 18:51:01 +0000135#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +0000136static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +0000137#endif
Benny Prijono40860c32008-09-04 13:55:33 +0000138#ifdef TRANSPORT_ADAPTER_SAMPLE
139static pj_status_t transport_adapter_sample(void);
140#endif
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000141pj_status_t app_destroy(void);
Benny Prijonoe909eac2006-07-27 22:04:56 +0000142
Benny Prijono91d20f42008-06-14 19:42:37 +0000143static void ringback_start(pjsua_call_id call_id);
144static void ring_start(pjsua_call_id call_id);
145static void ring_stop(pjsua_call_id call_id);
146
147
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000148/*****************************************************************************
149 * Configuration manipulation
150 */
151
152/* Show usage */
153static void usage(void)
154{
155 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000156 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000157 puts ("");
158 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000159 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000160 puts (" --help Display this help screen");
161 puts (" --version Display version info");
162 puts ("");
163 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000164 puts (" --log-file=fname Log to filename (default stderr)");
165 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
166 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000167 puts (" --color Use colorful logging (default yes on Win32)");
168 puts (" --no-color Disable colorful logging");
Benny Prijonob7944862008-07-19 20:53:49 +0000169 puts (" --light-bg Use dark colors for light background (default is dark bg)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000170
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000171 puts ("");
172 puts ("SIP Account options:");
Benny Prijono48ab2b72007-11-08 09:24:30 +0000173 puts (" --use-ims Enable 3GPP/IMS related settings on this account");
Benny Prijonod8179652008-01-23 20:39:07 +0000174#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijonof6508982008-01-25 09:02:33 +0000175 puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory (def:0)");
176 puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 1:sips (def:1)");
Benny Prijonod8179652008-01-23 20:39:07 +0000177#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178 puts (" --registrar=url Set the URL of registrar server");
179 puts (" --id=url Set the URL of local ID (used in From header)");
180 puts (" --contact=url Optionally override the Contact information");
Benny Prijono30fe4852008-12-10 16:54:16 +0000181 puts (" --contact-params=S Append the specified parameters S in Contact URI");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182 puts (" --proxy=url Optional URL of proxy server to visit");
183 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000184 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000185 puts (" --realm=string Set realm");
186 puts (" --username=string Set authentication username");
187 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000188 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000189 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000190 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
191 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000192 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000193 puts ("");
194 puts ("SIP Account Control:");
195 puts (" --next-account Add more account");
196 puts ("");
197 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000198 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
199 puts (" TCP and UDP transports on the specified port, unless");
200 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000201 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
202 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000203 puts (" --no-tcp Disable TCP transport.");
204 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000205 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
206 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000207 puts (" --outbound=url Set the URL of global outbound proxy server");
208 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000209 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000210 puts ("");
211 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000212 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000213 puts (" --tls-ca-file Specify TLS CA file (default=none)");
214 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
215 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
216 puts (" --tls-password Specify TLS password to private key file (default=none)");
217 puts (" --tls-verify-server Verify server's certificate (default=no)");
218 puts (" --tls-verify-client Verify client's certificate (default=no)");
219 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
Benny Prijonoe10db842008-07-01 15:31:59 +0000220 puts (" --tls-srv-name Specify TLS server name for multi-hosting server (optional)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000221
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000222 puts ("");
223 puts ("Media Options:");
224 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000225 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonof3758ee2008-02-26 15:32:16 +0000226 puts (" --clock-rate=N Override conference bridge clock rate");
227 puts (" --snd-clock-rate=N Override sound device clock rate");
Benny Prijono7d60d052008-03-29 12:24:20 +0000228 puts (" --stereo Audio device and conference bridge opened in stereo mode");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000229 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000230 puts (" --play-file=file Register WAV file in conference bridge.");
231 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000232 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
233 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
234 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000235 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000236 puts (" --auto-play Automatically play the file (to incoming calls only)");
237 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000238 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000239 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
240 puts (" --auto-rec Automatically record conversation");
Benny Prijono00cae612006-07-31 15:19:36 +0000241 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000242 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000243 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000244 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000245 puts (" --ec-opt=OPT Select echo canceller algorithm (0=default, ");
246 puts (" 1=speex, 2=suppressor)");
Nanang Izzuddin11951c32009-03-12 23:04:21 +0000247 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 30)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000248 puts (" --capture-dev=id Audio capture device ID (default=-1)");
249 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +0000250 puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000251 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000252 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
253 puts (" Specify N=-1 (default) to disable this feature.");
254 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000255 puts (" --no-tones Disable audible tones");
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000256 puts (" --jb-max-size Specify jitter buffer maximum size, in frames (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000257
Benny Prijonof76e1392008-06-06 14:51:48 +0000258 puts ("");
259 puts ("Media Transport Options:");
260 puts (" --use-ice Enable ICE (default:no)");
Benny Prijono551af422008-08-07 09:55:52 +0000261 puts (" --ice-no-host Disable ICE host candidates (default: no)");
262 puts (" --ice-no-rtcp Disable RTCP component in ICE (default: no)");
Benny Prijonof76e1392008-06-06 14:51:48 +0000263 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
264 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
265 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
266 puts (" --use-turn Enable TURN relay with ICE (default:no)");
267 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
268 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
269 puts (" --turn-user TURN username");
270 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000271
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000272 puts ("");
273 puts ("Buddy List (can be more than one):");
274 puts (" --add-buddy url Add the specified URL to the buddy list.");
275 puts ("");
276 puts ("User Agent options:");
277 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
278 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000279 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000280 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000281 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000282 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono91d06b62008-09-20 12:16:56 +0000283 puts (" --no-force-lr Allow strict-route to be used (i.e. do not force lr)");
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000284 puts (" --accept-redirect=N Specify how to handle call redirect (3xx) response.");
285 puts (" 0: reject, 1: follow automatically (default), 2: ask");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000286
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000287 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000288 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
289 puts ("");
290
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000291 fflush(stdout);
292}
293
294
295/* Set default config. */
296static void default_config(struct app_config *cfg)
297{
Benny Prijono56315612006-07-18 14:39:40 +0000298 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000299 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000300
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000301 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000302 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000303 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
304
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000305 pjsua_logging_config_default(&cfg->log_cfg);
306 pjsua_media_config_default(&cfg->media_cfg);
307 pjsua_transport_config_default(&cfg->udp_cfg);
308 cfg->udp_cfg.port = 5060;
309 pjsua_transport_config_default(&cfg->rtp_cfg);
310 cfg->rtp_cfg.port = 4000;
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000311 cfg->redir_op = PJSIP_REDIRECT_ACCEPT;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000312 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000313 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000314 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000315 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000316 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000317 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000318 cfg->capture_dev = PJSUA_INVALID_ID;
319 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000320 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
321 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000322 cfg->ringback_slot = PJSUA_INVALID_ID;
323 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000324
325 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
326 pjsua_acc_config_default(&cfg->acc_cfg[i]);
327
328 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
329 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000330}
331
332
333/*
334 * Read command arguments from config file.
335 */
336static int read_config_file(pj_pool_t *pool, const char *filename,
337 int *app_argc, char ***app_argv)
338{
339 int i;
340 FILE *fhnd;
341 char line[200];
342 int argc = 0;
343 char **argv;
344 enum { MAX_ARGS = 64 };
345
346 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
347 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
348 argv[argc++] = *app_argv[0];
349
350 /* Open config file. */
351 fhnd = fopen(filename, "rt");
352 if (!fhnd) {
353 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
354 fflush(stdout);
355 return -1;
356 }
357
358 /* Scan tokens in the file. */
359 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000360 char *token;
361 char *p;
362 const char *whitespace = " \t\r\n";
363 char cDelimiter;
364 int len, token_len;
365
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000366 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000367
368 // Trim ending newlines
369 len = strlen(line);
370 if (line[len-1]=='\n')
371 line[--len] = '\0';
372 if (line[len-1]=='\r')
373 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000374
Benny Prijonobf5b4692007-06-28 03:20:17 +0000375 if (len==0) continue;
376
377 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
378 // first, scan whitespaces
379 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
380
381 if (*p == '\0') // are we done yet?
382 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000383
Benny Prijonobf5b4692007-06-28 03:20:17 +0000384 if (*p == '"' || *p == '\'') { // is token a quoted string
385 cDelimiter = *p++; // save quote delimiter
386 token = p;
387
388 while (*p != '\0' && *p != cDelimiter) p++;
389
390 if (*p == '\0') // found end of the line, but,
391 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000392
Benny Prijonobf5b4692007-06-28 03:20:17 +0000393 } else { // token's not a quoted string
394 token = p;
395
396 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
397
398 cDelimiter = *p;
399 }
400
401 *p = '\0';
402 token_len = p-token;
403
404 if (token_len > 0) {
405 if (*token == '#')
406 break; // ignore remainder of line
407
408 argv[argc] = pj_pool_alloc(pool, token_len + 1);
409 pj_memcpy(argv[argc], token, token_len + 1);
410 ++argc;
411 }
412
413 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000414 }
415 }
416
417 /* Copy arguments from command line */
418 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
419 argv[argc++] = (*app_argv)[i];
420
421 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
422 PJ_LOG(1,(THIS_FILE,
423 "Too many arguments specified in cmd line/config file"));
424 fflush(stdout);
425 fclose(fhnd);
426 return -1;
427 }
428
429 fclose(fhnd);
430
431 /* Assign the new command line back to the original command line. */
432 *app_argc = argc;
433 *app_argv = argv;
434 return 0;
435
436}
437
438static int my_atoi(const char *cs)
439{
440 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000441
442 pj_cstr(&s, cs);
443 if (cs[0] == '-') {
444 s.ptr++, s.slen--;
445 return 0 - (int)pj_strtoul(&s);
446 } else if (cs[0] == '+') {
447 s.ptr++, s.slen--;
448 return pj_strtoul(&s);
449 } else {
450 return pj_strtoul(&s);
451 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000452}
453
454
455/* Parse arguments. */
456static pj_status_t parse_args(int argc, char *argv[],
457 struct app_config *cfg,
458 pj_str_t *uri_to_call)
459{
460 int c;
461 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000462 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonob7944862008-07-19 20:53:49 +0000463 OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000464 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000465 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
466 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono30fe4852008-12-10 16:54:16 +0000467 OPT_CONTACT_PARAMS,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000468 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000469 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000470 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000471 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000472 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
473 OPT_USE_ICE, OPT_USE_SRTP, OPT_SRTP_SECURE,
Benny Prijono551af422008-08-07 09:55:52 +0000474 OPT_USE_TURN, OPT_ICE_NO_HOST, OPT_ICE_NO_RTCP, OPT_TURN_SRV,
475 OPT_TURN_TCP, OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000476 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
477 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000478 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000479 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL, OPT_EC_OPT,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000480 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000481 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000482 OPT_NOREFERSUB, OPT_ACCEPT_REDIRECT,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000483 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
484 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
Benny Prijonoe10db842008-07-01 15:31:59 +0000485 OPT_TLS_NEG_TIMEOUT, OPT_TLS_SRV_NAME,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000486 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000487 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES, OPT_JB_MAX_SIZE,
Benny Prijonoebc32c32008-06-12 13:30:39 +0000488 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000489#ifdef _IONBF
490 OPT_STDOUT_NO_BUF,
491#endif
Benny Prijono91d06b62008-09-20 12:16:56 +0000492 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC,
493 OPT_NO_FORCE_LR
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000494 };
495 struct pj_getopt_option long_options[] = {
496 { "config-file",1, 0, OPT_CONFIG_FILE},
497 { "log-file", 1, 0, OPT_LOG_FILE},
498 { "log-level", 1, 0, OPT_LOG_LEVEL},
499 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
Benny Prijonod6e362a2008-07-19 17:53:47 +0000500 { "color", 0, 0, OPT_COLOR},
501 { "no-color", 0, 0, OPT_NO_COLOR},
Benny Prijonob7944862008-07-19 20:53:49 +0000502 { "light-bg", 0, 0, OPT_LIGHT_BG},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000503 { "help", 0, 0, OPT_HELP},
504 { "version", 0, 0, OPT_VERSION},
505 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000506 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000507 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000508 { "null-audio", 0, 0, OPT_NULL_AUDIO},
509 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000510 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000511 { "no-tcp", 0, 0, OPT_NO_TCP},
512 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000513 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514 { "proxy", 1, 0, OPT_PROXY},
515 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
516 { "registrar", 1, 0, OPT_REGISTRAR},
517 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000518 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000519 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000520 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000521 { "id", 1, 0, OPT_ID},
522 { "contact", 1, 0, OPT_CONTACT},
Benny Prijono30fe4852008-12-10 16:54:16 +0000523 { "contact-params",1,0, OPT_CONTACT_PARAMS},
Benny Prijonofce28542007-12-09 15:41:10 +0000524 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
525 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000526 { "accept-redirect", 1, 0, OPT_ACCEPT_REDIRECT},
Benny Prijono91d06b62008-09-20 12:16:56 +0000527 { "no-force-lr",0, 0, OPT_NO_FORCE_LR},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000528 { "realm", 1, 0, OPT_REALM},
529 { "username", 1, 0, OPT_USERNAME},
530 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000531 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000532 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000533 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
535 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
536 { "no-presence", 0, 0, OPT_NO_PRESENCE},
537 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000539 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000540 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000541 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
542 { "auto-conf", 0, 0, OPT_AUTO_CONF},
543 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000544 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000545 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000546 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000547
Benny Prijonoc97608e2007-03-23 16:34:20 +0000548 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000549 { "use-turn", 0, 0, OPT_USE_TURN},
550 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
Benny Prijono551af422008-08-07 09:55:52 +0000551 { "ice-no-rtcp",0, 0, OPT_ICE_NO_RTCP},
Benny Prijonof76e1392008-06-06 14:51:48 +0000552 { "turn-srv", 1, 0, OPT_TURN_SRV},
553 { "turn-tcp", 0, 0, OPT_TURN_TCP},
554 { "turn-user", 1, 0, OPT_TURN_USER},
555 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
556
Benny Prijonod8179652008-01-23 20:39:07 +0000557#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
558 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000559 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000560#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000561 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000562 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000563 { "complexity", 1, 0, OPT_COMPLEXITY},
564 { "quality", 1, 0, OPT_QUALITY},
565 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000566 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000567 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000568 { "ec-opt", 1, 0, OPT_EC_OPT},
Benny Prijono00cae612006-07-31 15:19:36 +0000569 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
570 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
571 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000572 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000573 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000574 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000575 { "duration", 1, 0, OPT_DURATION},
576 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000577 { "use-tls", 0, 0, OPT_USE_TLS},
578 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000579 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
580 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000581 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000582 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
583 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
584 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijonoe10db842008-07-01 15:31:59 +0000585 { "tls-srv-name", 1, 0, OPT_TLS_SRV_NAME},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000586 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
587 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000588 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
589 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000590 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
591 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000592#ifdef _IONBF
593 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
594#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000595 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000596 { "no-tones", 0, 0, OPT_NO_TONES},
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000597 { "jb-max-size", 1, 0, OPT_JB_MAX_SIZE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000598 { NULL, 0, 0, 0}
599 };
600 pj_status_t status;
601 pjsua_acc_config *cur_acc;
602 char *config_file = NULL;
603 unsigned i;
604
605 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000606 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000607 while ((c=pj_getopt_long(argc, argv, "", long_options,
608 &option_index)) != -1)
609 {
610 switch (c) {
611 case OPT_CONFIG_FILE:
612 config_file = pj_optarg;
613 break;
614 }
615 if (config_file)
616 break;
617 }
618
619 if (config_file) {
620 status = read_config_file(app_config.pool, config_file, &argc, &argv);
621 if (status != 0)
622 return status;
623 }
624
625 cfg->acc_cnt = 0;
626 cur_acc = &cfg->acc_cfg[0];
627
628
629 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
630 * read from config file.
631 */
632 pj_optind = 0;
633 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000634 pj_str_t tmp;
635 long lval;
636
637 switch (c) {
638
Benny Prijono6f137482006-06-15 11:31:36 +0000639 case OPT_CONFIG_FILE:
640 /* Ignore as this has been processed before */
641 break;
642
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000643 case OPT_LOG_FILE:
644 cfg->log_cfg.log_filename = pj_str(pj_optarg);
645 break;
646
647 case OPT_LOG_LEVEL:
648 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
649 if (c < 0 || c > 6) {
650 PJ_LOG(1,(THIS_FILE,
651 "Error: expecting integer value 0-6 "
652 "for --log-level"));
653 return PJ_EINVAL;
654 }
655 cfg->log_cfg.level = c;
656 pj_log_set_level( c );
657 break;
658
659 case OPT_APP_LOG_LEVEL:
660 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
661 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
662 PJ_LOG(1,(THIS_FILE,
663 "Error: expecting integer value 0-6 "
664 "for --app-log-level"));
665 return PJ_EINVAL;
666 }
667 break;
668
Benny Prijonod6e362a2008-07-19 17:53:47 +0000669 case OPT_COLOR:
670 cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR;
671 break;
672
673 case OPT_NO_COLOR:
674 cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR;
675 break;
676
Benny Prijonob7944862008-07-19 20:53:49 +0000677 case OPT_LIGHT_BG:
678 pj_log_set_color(1, PJ_TERM_COLOR_R);
679 pj_log_set_color(2, PJ_TERM_COLOR_R | PJ_TERM_COLOR_G);
680 pj_log_set_color(3, PJ_TERM_COLOR_B | PJ_TERM_COLOR_G);
681 pj_log_set_color(4, 0);
682 pj_log_set_color(5, 0);
683 pj_log_set_color(77, 0);
684 break;
685
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000686 case OPT_HELP:
687 usage();
688 return PJ_EINVAL;
689
690 case OPT_VERSION: /* version */
691 pj_dump_config();
692 return PJ_EINVAL;
693
694 case OPT_NULL_AUDIO:
695 cfg->null_audio = PJ_TRUE;
696 break;
697
698 case OPT_CLOCK_RATE:
699 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000700 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000701 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000702 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000703 return PJ_EINVAL;
704 }
705 cfg->media_cfg.clock_rate = lval;
706 break;
707
Benny Prijonof3758ee2008-02-26 15:32:16 +0000708 case OPT_SND_CLOCK_RATE:
709 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000710 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000711 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000712 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000713 return PJ_EINVAL;
714 }
715 cfg->media_cfg.snd_clock_rate = lval;
716 break;
717
Benny Prijono7d60d052008-03-29 12:24:20 +0000718 case OPT_STEREO:
719 cfg->media_cfg.channel_count = 2;
720 break;
721
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000722 case OPT_LOCAL_PORT: /* local-port */
723 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000724 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000725 PJ_LOG(1,(THIS_FILE,
726 "Error: expecting integer value for "
727 "--local-port"));
728 return PJ_EINVAL;
729 }
730 cfg->udp_cfg.port = (pj_uint16_t)lval;
731 break;
732
Benny Prijono0a5cad82006-09-26 13:21:02 +0000733 case OPT_IP_ADDR: /* ip-addr */
734 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
735 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
736 break;
737
Benny Prijonoe93e2872006-06-28 16:46:49 +0000738 case OPT_NO_UDP: /* no-udp */
739 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000740 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
741 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000742 }
743
744 cfg->no_udp = PJ_TRUE;
745 break;
746
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000747 case OPT_NOREFERSUB: /* norefersub */
748 cfg->no_refersub = PJ_TRUE;
749 break;
750
Benny Prijonoe93e2872006-06-28 16:46:49 +0000751 case OPT_NO_TCP: /* no-tcp */
752 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000753 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
754 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000755 }
756
757 cfg->no_tcp = PJ_TRUE;
758 break;
759
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000760 case OPT_PROXY: /* proxy */
761 if (pjsua_verify_sip_url(pj_optarg) != 0) {
762 PJ_LOG(1,(THIS_FILE,
763 "Error: invalid SIP URL '%s' "
764 "in proxy argument", pj_optarg));
765 return PJ_EINVAL;
766 }
767 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
768 break;
769
770 case OPT_OUTBOUND_PROXY: /* outbound proxy */
771 if (pjsua_verify_sip_url(pj_optarg) != 0) {
772 PJ_LOG(1,(THIS_FILE,
773 "Error: invalid SIP URL '%s' "
774 "in outbound proxy argument", pj_optarg));
775 return PJ_EINVAL;
776 }
777 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
778 break;
779
780 case OPT_REGISTRAR: /* registrar */
781 if (pjsua_verify_sip_url(pj_optarg) != 0) {
782 PJ_LOG(1,(THIS_FILE,
783 "Error: invalid SIP URL '%s' in "
784 "registrar argument", pj_optarg));
785 return PJ_EINVAL;
786 }
787 cur_acc->reg_uri = pj_str(pj_optarg);
788 break;
789
790 case OPT_REG_TIMEOUT: /* reg-timeout */
791 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
792 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
793 PJ_LOG(1,(THIS_FILE,
794 "Error: invalid value for --reg-timeout "
795 "(expecting 1-3600)"));
796 return PJ_EINVAL;
797 }
798 break;
799
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000800 case OPT_PUBLISH: /* publish */
801 cur_acc->publish_enabled = PJ_TRUE;
802 break;
803
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000804 case OPT_100REL: /** 100rel */
805 cur_acc->require_100rel = PJ_TRUE;
806 cfg->cfg.require_100rel = PJ_TRUE;
807 break;
808
Benny Prijono48ab2b72007-11-08 09:24:30 +0000809 case OPT_USE_IMS: /* Activate IMS settings */
810 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000811 break;
812
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000813 case OPT_ID: /* id */
814 if (pjsua_verify_sip_url(pj_optarg) != 0) {
815 PJ_LOG(1,(THIS_FILE,
816 "Error: invalid SIP URL '%s' "
817 "in local id argument", pj_optarg));
818 return PJ_EINVAL;
819 }
820 cur_acc->id = pj_str(pj_optarg);
821 break;
822
823 case OPT_CONTACT: /* contact */
824 if (pjsua_verify_sip_url(pj_optarg) != 0) {
825 PJ_LOG(1,(THIS_FILE,
826 "Error: invalid SIP URL '%s' "
827 "in contact argument", pj_optarg));
828 return PJ_EINVAL;
829 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000830 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000831 break;
832
Benny Prijono30fe4852008-12-10 16:54:16 +0000833 case OPT_CONTACT_PARAMS:
834 cur_acc->contact_params = pj_str(pj_optarg);
835 break;
836
Benny Prijonofce28542007-12-09 15:41:10 +0000837 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000838 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000839 break;
840
841 case OPT_USE_COMPACT_FORM:
842 /* enable compact form - from Ticket #342 */
843 {
844 extern pj_bool_t pjsip_use_compact_form;
845 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
846 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
847
848 pjsip_use_compact_form = PJ_TRUE;
849 /* do not transmit Allow header */
850 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
851 /* Do not include rtpmap for static payload types (<96) */
852 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
853 }
854 break;
855
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000856 case OPT_ACCEPT_REDIRECT:
857 cfg->redir_op = my_atoi(pj_optarg);
858 if (cfg->redir_op<0 || cfg->redir_op>PJSIP_REDIRECT_STOP) {
859 PJ_LOG(1,(THIS_FILE,
860 "Error: accept-redirect value '%s' ", pj_optarg));
861 return PJ_EINVAL;
862 }
863 break;
864
Benny Prijono91d06b62008-09-20 12:16:56 +0000865 case OPT_NO_FORCE_LR:
866 cfg->cfg.force_lr = PJ_FALSE;
867 break;
868
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000869 case OPT_NEXT_ACCOUNT: /* Add more account. */
870 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000871 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000872 break;
873
874 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000875 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000876 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000877 break;
878
879 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000880 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000881 break;
882
883 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000884 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
885 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000886#if PJSIP_HAS_DIGEST_AKA_AUTH
887 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
888 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
889 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
890#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000891 break;
892
893 case OPT_NEXT_CRED: /* next credential */
894 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000895 break;
896
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000897 case OPT_NAMESERVER: /* nameserver */
898 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
899 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
900 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
901 return PJ_ETOOMANY;
902 }
903 break;
904
Benny Prijonoebbf6892007-03-24 17:37:25 +0000905 case OPT_STUN_DOMAIN: /* STUN domain */
906 cfg->cfg.stun_domain = pj_str(pj_optarg);
907 break;
908
Benny Prijonoc97608e2007-03-23 16:34:20 +0000909 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000910 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000911 break;
912
913 case OPT_ADD_BUDDY: /* Add to buddy list. */
914 if (pjsua_verify_sip_url(pj_optarg) != 0) {
915 PJ_LOG(1,(THIS_FILE,
916 "Error: invalid URL '%s' in "
917 "--add-buddy option", pj_optarg));
918 return -1;
919 }
920 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
921 PJ_LOG(1,(THIS_FILE,
922 "Error: too many buddies in buddy list."));
923 return -1;
924 }
925 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
926 cfg->buddy_cnt++;
927 break;
928
929 case OPT_AUTO_PLAY:
930 cfg->auto_play = 1;
931 break;
932
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000933 case OPT_AUTO_PLAY_HANGUP:
934 cfg->auto_play_hangup = 1;
935 break;
936
Benny Prijono1ebd6142006-10-19 15:48:02 +0000937 case OPT_AUTO_REC:
938 cfg->auto_rec = 1;
939 break;
940
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000941 case OPT_AUTO_LOOP:
942 cfg->auto_loop = 1;
943 break;
944
Benny Prijono7ca96da2006-08-07 12:11:40 +0000945 case OPT_AUTO_CONF:
946 cfg->auto_conf = 1;
947 break;
948
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000949 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000950 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000951 break;
952
Benny Prijono4af234b2007-01-24 02:02:09 +0000953 case OPT_PLAY_TONE:
954 {
955 int f1, f2, on, off;
956 int n;
957
958 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
959 if (n != 4) {
960 puts("Expecting f1,f2,on,off in --play-tone");
961 return -1;
962 }
963
964 cfg->tones[cfg->tone_count].freq1 = (short)f1;
965 cfg->tones[cfg->tone_count].freq2 = (short)f2;
966 cfg->tones[cfg->tone_count].on_msec = (short)on;
967 cfg->tones[cfg->tone_count].off_msec = (short)off;
968 ++cfg->tone_count;
969 }
970 break;
971
Benny Prijono1ebd6142006-10-19 15:48:02 +0000972 case OPT_REC_FILE:
973 cfg->rec_file = pj_str(pj_optarg);
974 break;
975
Benny Prijonoc97608e2007-03-23 16:34:20 +0000976 case OPT_USE_ICE:
977 cfg->media_cfg.enable_ice = PJ_TRUE;
978 break;
979
Benny Prijonof76e1392008-06-06 14:51:48 +0000980 case OPT_USE_TURN:
981 cfg->media_cfg.enable_turn = PJ_TRUE;
982 break;
983
984 case OPT_ICE_NO_HOST:
985 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
986 break;
987
Benny Prijono551af422008-08-07 09:55:52 +0000988 case OPT_ICE_NO_RTCP:
989 cfg->media_cfg.ice_no_rtcp = PJ_TRUE;
990 break;
991
Benny Prijonof76e1392008-06-06 14:51:48 +0000992 case OPT_TURN_SRV:
993 cfg->media_cfg.turn_server = pj_str(pj_optarg);
994 break;
995
996 case OPT_TURN_TCP:
997 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
998 break;
999
1000 case OPT_TURN_USER:
1001 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
1002 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
1003 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
1004 break;
1005
1006 case OPT_TURN_PASSWD:
1007 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1008 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
1009 break;
1010
Benny Prijonod8179652008-01-23 20:39:07 +00001011#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1012 case OPT_USE_SRTP:
1013 app_config.cfg.use_srtp = my_atoi(pj_optarg);
1014 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
1015 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
1016 return -1;
1017 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001018 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +00001019 break;
Benny Prijonof6508982008-01-25 09:02:33 +00001020 case OPT_SRTP_SECURE:
1021 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
1022 if (!pj_isdigit(*pj_optarg) ||
1023 app_config.cfg.srtp_secure_signaling > 2)
1024 {
1025 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
1026 return -1;
1027 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001028 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +00001029 break;
Benny Prijonod8179652008-01-23 20:39:07 +00001030#endif
1031
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001032 case OPT_RTP_PORT:
1033 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +00001034 if (cfg->rtp_cfg.port == 0) {
1035 enum { START_PORT=4000 };
1036 unsigned range;
1037
1038 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
1039 cfg->rtp_cfg.port = START_PORT +
1040 ((pj_rand() % range) & 0xFFFE);
1041 }
1042
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001043 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
1044 PJ_LOG(1,(THIS_FILE,
1045 "Error: rtp-port argument value "
1046 "(expecting 1-65535"));
1047 return -1;
1048 }
1049 break;
1050
Benny Prijonofce28542007-12-09 15:41:10 +00001051 case OPT_DIS_CODEC:
1052 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
1053 break;
1054
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001055 case OPT_ADD_CODEC:
1056 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
1057 break;
1058
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001059 /* These options were no longer valid after new pjsua */
1060 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001061 case OPT_COMPLEXITY:
1062 cfg->complexity = my_atoi(pj_optarg);
1063 if (cfg->complexity < 0 || cfg->complexity > 10) {
1064 PJ_LOG(1,(THIS_FILE,
1065 "Error: invalid --complexity (expecting 0-10"));
1066 return -1;
1067 }
1068 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +00001069 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001070
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001071 case OPT_DURATION:
1072 cfg->duration = my_atoi(pj_optarg);
1073 break;
1074
Benny Prijonof521eb02006-08-06 23:07:25 +00001075 case OPT_THREAD_CNT:
1076 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1077 if (cfg->cfg.thread_cnt > 128) {
1078 PJ_LOG(1,(THIS_FILE,
1079 "Error: invalid --thread-cnt option"));
1080 return -1;
1081 }
1082 break;
1083
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001084 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001085 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1086 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001087 PJ_LOG(1,(THIS_FILE,
1088 "Error: invalid --ptime option"));
1089 return -1;
1090 }
1091 break;
1092
Benny Prijono0a12f002006-07-26 17:05:39 +00001093 case OPT_NO_VAD:
1094 cfg->media_cfg.no_vad = PJ_TRUE;
1095 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001096
Benny Prijonod79f25c2006-08-02 19:41:37 +00001097 case OPT_EC_TAIL:
1098 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1099 if (cfg->media_cfg.ec_tail_len > 1000) {
1100 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1101 "is too big"));
1102 return -1;
1103 }
1104 break;
1105
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001106 case OPT_EC_OPT:
1107 cfg->media_cfg.ec_options = my_atoi(pj_optarg);
1108 break;
1109
Benny Prijono0498d902006-06-19 14:49:14 +00001110 case OPT_QUALITY:
1111 cfg->media_cfg.quality = my_atoi(pj_optarg);
1112 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1113 PJ_LOG(1,(THIS_FILE,
1114 "Error: invalid --quality (expecting 0-10"));
1115 return -1;
1116 }
1117 break;
1118
Benny Prijono00cae612006-07-31 15:19:36 +00001119 case OPT_ILBC_MODE:
1120 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1121 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1122 PJ_LOG(1,(THIS_FILE,
1123 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1124 return -1;
1125 }
1126 break;
1127
1128 case OPT_RX_DROP_PCT:
1129 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1130 if (cfg->media_cfg.rx_drop_pct > 100) {
1131 PJ_LOG(1,(THIS_FILE,
1132 "Error: invalid --rx-drop-pct (expecting <= 100"));
1133 return -1;
1134 }
1135 break;
1136
1137 case OPT_TX_DROP_PCT:
1138 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1139 if (cfg->media_cfg.tx_drop_pct > 100) {
1140 PJ_LOG(1,(THIS_FILE,
1141 "Error: invalid --tx-drop-pct (expecting <= 100"));
1142 return -1;
1143 }
1144 break;
1145
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001146 case OPT_AUTO_ANSWER:
1147 cfg->auto_answer = my_atoi(pj_optarg);
1148 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1149 PJ_LOG(1,(THIS_FILE,
1150 "Error: invalid code in --auto-answer "
1151 "(expecting 100-699"));
1152 return -1;
1153 }
1154 break;
1155
1156 case OPT_MAX_CALLS:
1157 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001158 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001159 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1160 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001161 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001162 return -1;
1163 }
1164 break;
1165
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001166 case OPT_USE_TLS:
1167 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001168#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1169 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1170 return -1;
1171#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001172 break;
1173
1174 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001175 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1176#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1177 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1178 return -1;
1179#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001180 break;
1181
Benny Prijonof3bbc132006-12-25 06:43:59 +00001182 case OPT_TLS_CERT_FILE:
1183 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1184#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1185 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1186 return -1;
1187#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001188 break;
1189
Benny Prijonof3bbc132006-12-25 06:43:59 +00001190 case OPT_TLS_PRIV_FILE:
1191 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1192 break;
1193
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001194 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001195 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1196#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1197 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1198 return -1;
1199#endif
1200 break;
1201
1202 case OPT_TLS_VERIFY_SERVER:
1203 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1204 break;
1205
1206 case OPT_TLS_VERIFY_CLIENT:
1207 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1208 break;
1209
1210 case OPT_TLS_NEG_TIMEOUT:
1211 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001212 break;
1213
Benny Prijonoe10db842008-07-01 15:31:59 +00001214 case OPT_TLS_SRV_NAME:
1215 cfg->udp_cfg.tls_setting.server_name = pj_str(pj_optarg);
1216 break;
1217
Benny Prijono4e5d5512007-03-06 18:11:30 +00001218 case OPT_CAPTURE_DEV:
1219 cfg->capture_dev = atoi(pj_optarg);
1220 break;
1221
1222 case OPT_PLAYBACK_DEV:
1223 cfg->playback_dev = atoi(pj_optarg);
1224 break;
1225
Benny Prijonoebc32c32008-06-12 13:30:39 +00001226 case OPT_STDOUT_REFRESH:
1227 stdout_refresh = atoi(pj_optarg);
1228 break;
1229
1230 case OPT_STDOUT_REFRESH_TEXT:
1231 stdout_refresh_text = pj_optarg;
1232 break;
1233
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001234#ifdef _IONBF
1235 case OPT_STDOUT_NO_BUF:
1236 setvbuf(stdout, NULL, _IONBF, 0);
1237 break;
1238#endif
1239
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001240 case OPT_CAPTURE_LAT:
1241 cfg->capture_lat = atoi(pj_optarg);
1242 break;
1243
1244 case OPT_PLAYBACK_LAT:
1245 cfg->playback_lat = atoi(pj_optarg);
1246 break;
1247
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001248 case OPT_SND_AUTO_CLOSE:
1249 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1250 break;
1251
Benny Prijono91d20f42008-06-14 19:42:37 +00001252 case OPT_NO_TONES:
1253 cfg->no_tones = PJ_TRUE;
1254 break;
1255
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001256 case OPT_JB_MAX_SIZE:
1257 cfg->media_cfg.jb_max = atoi(pj_optarg);
1258 break;
1259
Benny Prijono22a300a2006-06-14 20:04:55 +00001260 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001261 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001262 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001263 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001264 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001265 }
1266 }
1267
1268 if (pj_optind != argc) {
1269 pj_str_t uri_arg;
1270
1271 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1272 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1273 return -1;
1274 }
1275 uri_arg = pj_str(argv[pj_optind]);
1276 if (uri_to_call)
1277 *uri_to_call = uri_arg;
1278 pj_optind++;
1279
1280 /* Add URI to call to buddy list if it's not already there */
1281 for (i=0; i<cfg->buddy_cnt; ++i) {
1282 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1283 break;
1284 }
1285 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1286 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1287 }
1288
1289 } else {
1290 if (uri_to_call)
1291 uri_to_call->slen = 0;
1292 }
1293
1294 if (pj_optind != argc) {
1295 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1296 return PJ_EINVAL;
1297 }
1298
Benny Prijono56315612006-07-18 14:39:40 +00001299 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1300 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001301
1302 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001303 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1304
1305 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001306 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001307 acfg->cred_count++;
1308 }
1309
1310 /* When IMS mode is enabled for the account, verify that settings
1311 * are okay.
1312 */
1313 /* For now we check if IMS mode is activated by looking if
1314 * initial_auth is set.
1315 */
1316 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1317 /* Realm must point to the real domain */
1318 if (*acfg->cred_info[0].realm.ptr=='*') {
1319 PJ_LOG(1,(THIS_FILE,
1320 "Error: cannot use '*' as realm with IMS"));
1321 return PJ_EINVAL;
1322 }
1323
1324 /* Username for authentication must be in a@b format */
1325 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1326 PJ_LOG(1,(THIS_FILE,
1327 "Error: Username for authentication must "
1328 "be in user@domain format with IMS"));
1329 return PJ_EINVAL;
1330 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001331 }
1332 }
1333
1334
1335 return PJ_SUCCESS;
1336}
1337
1338
1339/*
1340 * Save account settings
1341 */
1342static void write_account_settings(int acc_index, pj_str_t *result)
1343{
1344 unsigned i;
1345 char line[128];
1346 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1347
1348
1349 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1350 pj_strcat2(result, line);
1351
1352
1353 /* Identity */
1354 if (acc_cfg->id.slen) {
1355 pj_ansi_sprintf(line, "--id %.*s\n",
1356 (int)acc_cfg->id.slen,
1357 acc_cfg->id.ptr);
1358 pj_strcat2(result, line);
1359 }
1360
1361 /* Registrar server */
1362 if (acc_cfg->reg_uri.slen) {
1363 pj_ansi_sprintf(line, "--registrar %.*s\n",
1364 (int)acc_cfg->reg_uri.slen,
1365 acc_cfg->reg_uri.ptr);
1366 pj_strcat2(result, line);
1367
1368 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1369 acc_cfg->reg_timeout);
1370 pj_strcat2(result, line);
1371 }
1372
1373 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001374 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001375 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001376 (int)acc_cfg->force_contact.slen,
1377 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001378 pj_strcat2(result, line);
1379 }
1380
Benny Prijono30fe4852008-12-10 16:54:16 +00001381 /* Contact parameters */
1382 if (acc_cfg->contact_params.slen) {
1383 pj_ansi_sprintf(line, "--contact-params %.*s\n",
1384 (int)acc_cfg->contact_params.slen,
1385 acc_cfg->contact_params.ptr);
1386 pj_strcat2(result, line);
1387 }
1388
Benny Prijonofce28542007-12-09 15:41:10 +00001389 /* */
Benny Prijono7f630432008-09-24 16:52:41 +00001390 if (acc_cfg->allow_contact_rewrite!=1)
Benny Prijonofce28542007-12-09 15:41:10 +00001391 {
Benny Prijono251e02a2008-07-25 10:05:55 +00001392 pj_ansi_sprintf(line, "--auto-update-nat %i\n",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001393 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001394 pj_strcat2(result, line);
1395 }
1396
Benny Prijonod8179652008-01-23 20:39:07 +00001397#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1398 /* SRTP */
1399 if (acc_cfg->use_srtp) {
1400 pj_ansi_sprintf(line, "--use-srtp %i\n",
1401 (int)acc_cfg->use_srtp);
1402 pj_strcat2(result, line);
1403 }
1404#endif
1405
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001406 /* Proxy */
1407 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1408 pj_ansi_sprintf(line, "--proxy %.*s\n",
1409 (int)acc_cfg->proxy[i].slen,
1410 acc_cfg->proxy[i].ptr);
1411 pj_strcat2(result, line);
1412 }
1413
1414 /* Credentials */
1415 for (i=0; i<acc_cfg->cred_count; ++i) {
1416 if (acc_cfg->cred_info[i].realm.slen) {
1417 pj_ansi_sprintf(line, "--realm %.*s\n",
1418 (int)acc_cfg->cred_info[i].realm.slen,
1419 acc_cfg->cred_info[i].realm.ptr);
1420 pj_strcat2(result, line);
1421 }
1422
1423 if (acc_cfg->cred_info[i].username.slen) {
1424 pj_ansi_sprintf(line, "--username %.*s\n",
1425 (int)acc_cfg->cred_info[i].username.slen,
1426 acc_cfg->cred_info[i].username.ptr);
1427 pj_strcat2(result, line);
1428 }
1429
1430 if (acc_cfg->cred_info[i].data.slen) {
1431 pj_ansi_sprintf(line, "--password %.*s\n",
1432 (int)acc_cfg->cred_info[i].data.slen,
1433 acc_cfg->cred_info[i].data.ptr);
1434 pj_strcat2(result, line);
1435 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001436
1437 if (i != acc_cfg->cred_count - 1)
1438 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001439 }
1440
1441}
1442
1443
1444/*
1445 * Write settings.
1446 */
1447static int write_settings(const struct app_config *config,
1448 char *buf, pj_size_t max)
1449{
1450 unsigned acc_index;
1451 unsigned i;
1452 pj_str_t cfg;
1453 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001454 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001455
1456 PJ_UNUSED_ARG(max);
1457
1458 cfg.ptr = buf;
1459 cfg.slen = 0;
1460
1461 /* Logging. */
1462 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1463 pj_ansi_sprintf(line, "--log-level %d\n",
1464 config->log_cfg.level);
1465 pj_strcat2(&cfg, line);
1466
1467 pj_ansi_sprintf(line, "--app-log-level %d\n",
1468 config->log_cfg.console_level);
1469 pj_strcat2(&cfg, line);
1470
1471 if (config->log_cfg.log_filename.slen) {
1472 pj_ansi_sprintf(line, "--log-file %.*s\n",
1473 (int)config->log_cfg.log_filename.slen,
1474 config->log_cfg.log_filename.ptr);
1475 pj_strcat2(&cfg, line);
1476 }
1477
1478
1479 /* Save account settings. */
1480 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1481
1482 write_account_settings(acc_index, &cfg);
1483
1484 if (acc_index < config->acc_cnt-1)
1485 pj_strcat2(&cfg, "--next-account\n");
1486 }
1487
1488
1489 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1490
Benny Prijono98d8c952009-02-06 08:13:43 +00001491 /* Nameservers */
1492 for (i=0; i<config->cfg.nameserver_count; ++i) {
1493 pj_ansi_sprintf(line, "--nameserver %.*s\n",
1494 (int)config->cfg.nameserver[i].slen,
1495 config->cfg.nameserver[i].ptr);
1496 pj_strcat2(&cfg, line);
1497 }
1498
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001499 /* Outbound proxy */
1500 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1501 pj_ansi_sprintf(line, "--outbound %.*s\n",
1502 (int)config->cfg.outbound_proxy[i].slen,
1503 config->cfg.outbound_proxy[i].ptr);
1504 pj_strcat2(&cfg, line);
1505 }
1506
1507
1508 /* UDP Transport. */
1509 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1510 pj_strcat2(&cfg, line);
1511
Benny Prijono0a5cad82006-09-26 13:21:02 +00001512 /* IP address, if any. */
1513 if (config->udp_cfg.public_addr.slen) {
1514 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1515 (int)config->udp_cfg.public_addr.slen,
1516 config->udp_cfg.public_addr.ptr);
1517 pj_strcat2(&cfg, line);
1518 }
1519
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001520 /* No TCP ? */
1521 if (config->no_tcp) {
1522 pj_strcat2(&cfg, "--no-tcp\n");
1523 }
1524
1525 /* No UDP ? */
1526 if (config->no_udp) {
1527 pj_strcat2(&cfg, "--no-udp\n");
1528 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001529
1530 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001531 if (config->cfg.stun_domain.slen) {
1532 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1533 (int)config->cfg.stun_domain.slen,
1534 config->cfg.stun_domain.ptr);
1535 pj_strcat2(&cfg, line);
1536 }
1537 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001538 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001539 (int)config->cfg.stun_host.slen,
1540 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001541 pj_strcat2(&cfg, line);
1542 }
1543
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001544 /* TLS */
1545 if (config->use_tls)
1546 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001547 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001548 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001549 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1550 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001551 pj_strcat2(&cfg, line);
1552 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001553 if (config->udp_cfg.tls_setting.cert_file.slen) {
1554 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1555 (int)config->udp_cfg.tls_setting.cert_file.slen,
1556 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001557 pj_strcat2(&cfg, line);
1558 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001559 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1560 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1561 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1562 config->udp_cfg.tls_setting.privkey_file.ptr);
1563 pj_strcat2(&cfg, line);
1564 }
1565
1566 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001567 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001568 (int)config->udp_cfg.tls_setting.password.slen,
1569 config->udp_cfg.tls_setting.password.ptr);
1570 pj_strcat2(&cfg, line);
1571 }
1572
Benny Prijonoe10db842008-07-01 15:31:59 +00001573 if (config->udp_cfg.tls_setting.server_name.slen) {
1574 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1575 (int)config->udp_cfg.tls_setting.server_name.slen,
1576 config->udp_cfg.tls_setting.server_name.ptr);
1577 pj_strcat2(&cfg, line);
1578 }
1579
Benny Prijonof3bbc132006-12-25 06:43:59 +00001580 if (config->udp_cfg.tls_setting.verify_server)
1581 pj_strcat2(&cfg, "--tls-verify-server\n");
1582
1583 if (config->udp_cfg.tls_setting.verify_client)
1584 pj_strcat2(&cfg, "--tls-verify-client\n");
1585
1586 if (config->udp_cfg.tls_setting.timeout.sec) {
1587 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001588 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001589 pj_strcat2(&cfg, line);
1590 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001591
1592 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1593
Benny Prijonof6508982008-01-25 09:02:33 +00001594 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001595#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001596 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1597 pj_ansi_sprintf(line, "--use-srtp %d\n",
1598 app_config.cfg.use_srtp);
1599 pj_strcat2(&cfg, line);
1600 }
1601 if (app_config.cfg.srtp_secure_signaling !=
1602 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1603 {
1604 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1605 app_config.cfg.srtp_secure_signaling);
1606 pj_strcat2(&cfg, line);
1607 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001608#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001609
Benny Prijonof76e1392008-06-06 14:51:48 +00001610 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001611 if (config->media_cfg.enable_ice)
1612 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001613
1614 if (config->media_cfg.enable_turn)
1615 pj_strcat2(&cfg, "--use-turn\n");
1616
1617 if (config->media_cfg.ice_no_host_cands)
1618 pj_strcat2(&cfg, "--ice-no-host\n");
1619
Benny Prijono551af422008-08-07 09:55:52 +00001620 if (config->media_cfg.ice_no_rtcp)
1621 pj_strcat2(&cfg, "--ice-no-rtcp\n");
1622
Benny Prijonof76e1392008-06-06 14:51:48 +00001623 if (config->media_cfg.turn_server.slen) {
1624 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1625 (int)config->media_cfg.turn_server.slen,
1626 config->media_cfg.turn_server.ptr);
1627 pj_strcat2(&cfg, line);
1628 }
1629
1630 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1631 pj_strcat2(&cfg, "--turn-tcp\n");
1632
1633 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1634 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1635 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1636 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1637 pj_strcat2(&cfg, line);
1638 }
1639
1640 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1641 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1642 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1643 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1644 pj_strcat2(&cfg, line);
1645 }
1646
1647 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001648 if (config->null_audio)
1649 pj_strcat2(&cfg, "--null-audio\n");
1650 if (config->auto_play)
1651 pj_strcat2(&cfg, "--auto-play\n");
1652 if (config->auto_loop)
1653 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001654 if (config->auto_conf)
1655 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001656 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001657 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001658 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001659 pj_strcat2(&cfg, line);
1660 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001661 for (i=0; i<config->tone_count; ++i) {
1662 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1663 config->tones[i].freq1, config->tones[i].freq2,
1664 config->tones[i].on_msec, config->tones[i].off_msec);
1665 pj_strcat2(&cfg, line);
1666 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001667 if (config->rec_file.slen) {
1668 pj_ansi_sprintf(line, "--rec-file %s\n",
1669 config->rec_file.ptr);
1670 pj_strcat2(&cfg, line);
1671 }
1672 if (config->auto_rec)
1673 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001674 if (config->capture_dev != PJSUA_INVALID_ID) {
1675 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1676 pj_strcat2(&cfg, line);
1677 }
1678 if (config->playback_dev != PJSUA_INVALID_ID) {
1679 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1680 pj_strcat2(&cfg, line);
1681 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001682 if (config->media_cfg.snd_auto_close_time != -1) {
1683 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1684 config->media_cfg.snd_auto_close_time);
1685 pj_strcat2(&cfg, line);
1686 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001687 if (config->no_tones) {
1688 pj_strcat2(&cfg, "--no-tones\n");
1689 }
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001690 if (config->media_cfg.jb_max != -1) {
1691 pj_ansi_sprintf(line, "--jb-max-size %d\n",
1692 config->media_cfg.jb_max);
1693 pj_strcat2(&cfg, line);
1694 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001695
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001696 /* Sound device latency */
1697 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1698 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1699 pj_strcat2(&cfg, line);
1700 }
1701 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1702 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1703 pj_strcat2(&cfg, line);
1704 }
1705
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001706 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001707 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001708 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001709 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001710 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001711 } else {
1712 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1713 config->media_cfg.clock_rate);
1714 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001715 }
Benny Prijono70972992006-08-05 11:13:58 +00001716
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001717 if (config->media_cfg.snd_clock_rate &&
1718 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1719 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001720 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1721 config->media_cfg.snd_clock_rate);
1722 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001723 }
1724
Benny Prijono7d60d052008-03-29 12:24:20 +00001725 /* Stereo mode. */
1726 if (config->media_cfg.channel_count == 2) {
1727 pj_ansi_sprintf(line, "--stereo\n");
1728 pj_strcat2(&cfg, line);
1729 }
1730
Benny Prijono70972992006-08-05 11:13:58 +00001731 /* quality */
1732 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001733 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001734 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001735 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001736 } else {
1737 pj_ansi_sprintf(line, "#using default --quality %d\n",
1738 config->media_cfg.quality);
1739 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001740 }
Benny Prijono0498d902006-06-19 14:49:14 +00001741
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001742
1743 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001744 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001745 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001746 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001747 pj_strcat2(&cfg, line);
1748 }
1749
Benny Prijono70972992006-08-05 11:13:58 +00001750 /* no-vad */
1751 if (config->media_cfg.no_vad) {
1752 pj_strcat2(&cfg, "--no-vad\n");
1753 }
1754
1755 /* ec-tail */
1756 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1757 pj_ansi_sprintf(line, "--ec-tail %d\n",
1758 config->media_cfg.ec_tail_len);
1759 pj_strcat2(&cfg, line);
1760 } else {
1761 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1762 config->media_cfg.ec_tail_len);
1763 pj_strcat2(&cfg, line);
1764 }
1765
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001766 /* ec-opt */
1767 if (config->media_cfg.ec_options != 0) {
1768 pj_ansi_sprintf(line, "--ec-opt %d\n",
1769 config->media_cfg.ec_options);
1770 pj_strcat2(&cfg, line);
1771 }
Benny Prijono70972992006-08-05 11:13:58 +00001772
1773 /* ilbc-mode */
1774 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1775 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1776 config->media_cfg.ilbc_mode);
1777 pj_strcat2(&cfg, line);
1778 } else {
1779 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1780 config->media_cfg.ilbc_mode);
1781 pj_strcat2(&cfg, line);
1782 }
1783
1784 /* RTP drop */
1785 if (config->media_cfg.tx_drop_pct) {
1786 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1787 config->media_cfg.tx_drop_pct);
1788 pj_strcat2(&cfg, line);
1789
1790 }
1791 if (config->media_cfg.rx_drop_pct) {
1792 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1793 config->media_cfg.rx_drop_pct);
1794 pj_strcat2(&cfg, line);
1795
1796 }
1797
1798
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001799 /* Start RTP port. */
1800 pj_ansi_sprintf(line, "--rtp-port %d\n",
1801 config->rtp_cfg.port);
1802 pj_strcat2(&cfg, line);
1803
1804 /* Add codec. */
1805 for (i=0; i<config->codec_cnt; ++i) {
1806 pj_ansi_sprintf(line, "--add-codec %s\n",
1807 config->codec_arg[i].ptr);
1808 pj_strcat2(&cfg, line);
1809 }
Benny Prijonofce28542007-12-09 15:41:10 +00001810 /* Disable codec */
1811 for (i=0; i<config->codec_dis_cnt; ++i) {
1812 pj_ansi_sprintf(line, "--dis-codec %s\n",
1813 config->codec_dis[i].ptr);
1814 pj_strcat2(&cfg, line);
1815 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001816
1817 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1818
1819 /* Auto-answer. */
1820 if (config->auto_answer != 0) {
1821 pj_ansi_sprintf(line, "--auto-answer %d\n",
1822 config->auto_answer);
1823 pj_strcat2(&cfg, line);
1824 }
1825
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001826 /* accept-redirect */
1827 if (config->redir_op != PJSIP_REDIRECT_ACCEPT) {
1828 pj_ansi_sprintf(line, "--accept-redirect %d\n",
1829 config->redir_op);
1830 pj_strcat2(&cfg, line);
1831 }
1832
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001833 /* Max calls. */
1834 pj_ansi_sprintf(line, "--max-calls %d\n",
1835 config->cfg.max_calls);
1836 pj_strcat2(&cfg, line);
1837
1838 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001839 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001840 pj_ansi_sprintf(line, "--duration %d\n",
1841 config->duration);
1842 pj_strcat2(&cfg, line);
1843 }
1844
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001845 /* norefersub ? */
1846 if (config->no_refersub) {
1847 pj_strcat2(&cfg, "--norefersub\n");
1848 }
1849
Benny Prijonofce28542007-12-09 15:41:10 +00001850 if (pjsip_use_compact_form)
1851 {
1852 pj_strcat2(&cfg, "--use-compact-form\n");
1853 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001854
Benny Prijono91d06b62008-09-20 12:16:56 +00001855 if (config->cfg.force_lr) {
1856 pj_strcat2(&cfg, "--no-force-lr\n");
1857 }
1858
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001859 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1860
1861 /* Add buddies. */
1862 for (i=0; i<config->buddy_cnt; ++i) {
1863 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1864 (int)config->buddy_cfg[i].uri.slen,
1865 config->buddy_cfg[i].uri.ptr);
1866 pj_strcat2(&cfg, line);
1867 }
1868
1869
1870 *(cfg.ptr + cfg.slen) = '\0';
1871 return cfg.slen;
1872}
1873
1874
1875/*
1876 * Dump application states.
1877 */
1878static void app_dump(pj_bool_t detail)
1879{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001880 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001881}
1882
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001883/*
1884 * Print log of call states. Since call states may be too long for logger,
1885 * printing it is a bit tricky, it should be printed part by part as long
1886 * as the logger can accept.
1887 */
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001888static void log_call_dump(int call_id)
1889{
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001890 unsigned call_dump_len;
1891 unsigned part_len;
1892 unsigned part_idx;
1893 unsigned log_decor;
1894
1895 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1896 sizeof(some_buf), " ");
1897 call_dump_len = strlen(some_buf);
1898
1899 log_decor = pj_log_get_decor();
1900 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1901 PJ_LOG(3,(THIS_FILE, "\n"));
1902 pj_log_set_decor(0);
1903
1904 part_idx = 0;
1905 part_len = PJ_LOG_MAX_SIZE-80;
1906 while (part_idx < call_dump_len) {
1907 char p_orig, *p;
1908
1909 p = &some_buf[part_idx];
1910 if (part_idx + part_len > call_dump_len)
1911 part_len = call_dump_len - part_idx;
1912 p_orig = p[part_len];
1913 p[part_len] = '\0';
1914 PJ_LOG(3,(THIS_FILE, "%s", p));
1915 p[part_len] = p_orig;
1916 part_idx += part_len;
1917 }
1918 pj_log_set_decor(log_decor);
1919}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001920
1921/*****************************************************************************
1922 * Console application
1923 */
1924
Benny Prijono91d20f42008-06-14 19:42:37 +00001925static void ringback_start(pjsua_call_id call_id)
1926{
1927 if (app_config.no_tones)
1928 return;
1929
1930 if (app_config.call_data[call_id].ringback_on)
1931 return;
1932
1933 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1934
1935 if (++app_config.ringback_cnt==1 &&
1936 app_config.ringback_slot!=PJSUA_INVALID_ID)
1937 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001938 pjsua_conf_connect(app_config.ringback_slot, 0);
1939 }
1940}
1941
1942static void ring_stop(pjsua_call_id call_id)
1943{
1944 if (app_config.no_tones)
1945 return;
1946
1947 if (app_config.call_data[call_id].ringback_on) {
1948 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1949
1950 pj_assert(app_config.ringback_cnt>0);
1951 if (--app_config.ringback_cnt == 0 &&
1952 app_config.ringback_slot!=PJSUA_INVALID_ID)
1953 {
1954 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001955 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001956 }
1957 }
1958
1959 if (app_config.call_data[call_id].ring_on) {
1960 app_config.call_data[call_id].ring_on = PJ_FALSE;
1961
1962 pj_assert(app_config.ring_cnt>0);
1963 if (--app_config.ring_cnt == 0 &&
1964 app_config.ring_slot!=PJSUA_INVALID_ID)
1965 {
1966 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001967 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001968 }
1969 }
1970}
1971
1972static void ring_start(pjsua_call_id call_id)
1973{
1974 if (app_config.no_tones)
1975 return;
1976
1977 if (app_config.call_data[call_id].ring_on)
1978 return;
1979
1980 app_config.call_data[call_id].ring_on = PJ_TRUE;
1981
1982 if (++app_config.ring_cnt==1 &&
1983 app_config.ring_slot!=PJSUA_INVALID_ID)
1984 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001985 pjsua_conf_connect(app_config.ring_slot, 0);
1986 }
1987}
1988
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001989/*
1990 * Find next call when current call is disconnected or when user
1991 * press ']'
1992 */
1993static pj_bool_t find_next_call(void)
1994{
1995 int i, max;
1996
1997 max = pjsua_call_get_max_count();
1998 for (i=current_call+1; i<max; ++i) {
1999 if (pjsua_call_is_active(i)) {
2000 current_call = i;
2001 return PJ_TRUE;
2002 }
2003 }
2004
2005 for (i=0; i<current_call; ++i) {
2006 if (pjsua_call_is_active(i)) {
2007 current_call = i;
2008 return PJ_TRUE;
2009 }
2010 }
2011
2012 current_call = PJSUA_INVALID_ID;
2013 return PJ_FALSE;
2014}
2015
2016
2017/*
2018 * Find previous call when user press '['
2019 */
2020static pj_bool_t find_prev_call(void)
2021{
2022 int i, max;
2023
2024 max = pjsua_call_get_max_count();
2025 for (i=current_call-1; i>=0; --i) {
2026 if (pjsua_call_is_active(i)) {
2027 current_call = i;
2028 return PJ_TRUE;
2029 }
2030 }
2031
2032 for (i=max-1; i>current_call; --i) {
2033 if (pjsua_call_is_active(i)) {
2034 current_call = i;
2035 return PJ_TRUE;
2036 }
2037 }
2038
2039 current_call = PJSUA_INVALID_ID;
2040 return PJ_FALSE;
2041}
2042
2043
Benny Prijono804ff0a2006-09-14 11:17:48 +00002044/* Callback from timer when the maximum call duration has been
2045 * exceeded.
2046 */
2047static void call_timeout_callback(pj_timer_heap_t *timer_heap,
2048 struct pj_timer_entry *entry)
2049{
2050 pjsua_call_id call_id = entry->id;
2051 pjsua_msg_data msg_data;
2052 pjsip_generic_string_hdr warn;
2053 pj_str_t hname = pj_str("Warning");
2054 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
2055
2056 PJ_UNUSED_ARG(timer_heap);
2057
Benny Prijono148c9dd2006-09-19 13:37:53 +00002058 if (call_id == PJSUA_INVALID_ID) {
2059 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
2060 return;
2061 }
2062
Benny Prijono804ff0a2006-09-14 11:17:48 +00002063 /* Add warning header */
2064 pjsua_msg_data_init(&msg_data);
2065 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
2066 pj_list_push_back(&msg_data.hdr_list, &warn);
2067
2068 /* Call duration has been exceeded; disconnect the call */
2069 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
2070 "for call %d, disconnecting the call",
2071 app_config.duration, call_id));
2072 entry->id = PJSUA_INVALID_ID;
2073 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
2074}
2075
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002076
2077/*
2078 * Handler when invite state has changed.
2079 */
2080static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
2081{
2082 pjsua_call_info call_info;
2083
2084 PJ_UNUSED_ARG(e);
2085
2086 pjsua_call_get_info(call_id, &call_info);
2087
2088 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
2089
Benny Prijono91d20f42008-06-14 19:42:37 +00002090 /* Stop all ringback for this call */
2091 ring_stop(call_id);
2092
Benny Prijono804ff0a2006-09-14 11:17:48 +00002093 /* Cancel duration timer, if any */
2094 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
2095 struct call_data *cd = &app_config.call_data[call_id];
2096 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2097
2098 cd->timer.id = PJSUA_INVALID_ID;
2099 pjsip_endpt_cancel_timer(endpt, &cd->timer);
2100 }
2101
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002102 /* Rewind play file when hangup automatically,
2103 * since file is not looped
2104 */
2105 if (app_config.auto_play_hangup)
2106 pjsua_player_set_pos(app_config.wav_id, 0);
2107
2108
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002109 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
2110 call_id,
2111 call_info.last_status,
2112 call_info.last_status_text.ptr));
2113
2114 if (call_id == current_call) {
2115 find_next_call();
2116 }
2117
Benny Prijono4be63b52006-11-25 14:50:25 +00002118 /* Dump media state upon disconnected */
2119 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002120 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002121 "Call %d disconnected, dumping media stats..",
2122 call_id));
2123 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002124 }
2125
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002126 } else {
2127
Benny Prijono804ff0a2006-09-14 11:17:48 +00002128 if (app_config.duration!=NO_LIMIT &&
2129 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2130 {
2131 /* Schedule timer to hangup call after the specified duration */
2132 struct call_data *cd = &app_config.call_data[call_id];
2133 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2134 pj_time_val delay;
2135
2136 cd->timer.id = call_id;
2137 delay.sec = app_config.duration;
2138 delay.msec = 0;
2139 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2140 }
2141
Benny Prijono4be63b52006-11-25 14:50:25 +00002142 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2143 int code;
2144 pj_str_t reason;
2145 pjsip_msg *msg;
2146
2147 /* This can only occur because of TX or RX message */
2148 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2149
2150 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2151 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2152 } else {
2153 msg = e->body.tsx_state.src.tdata->msg;
2154 }
2155
2156 code = msg->line.status.code;
2157 reason = msg->line.status.reason;
2158
Benny Prijono91d20f42008-06-14 19:42:37 +00002159 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2160 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2161 msg->body == NULL &&
2162 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2163 {
2164 ringback_start(call_id);
2165 }
2166
Benny Prijono4be63b52006-11-25 14:50:25 +00002167 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2168 call_id, call_info.state_text.ptr,
2169 code, (int)reason.slen, reason.ptr));
2170 } else {
2171 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2172 call_id,
2173 call_info.state_text.ptr));
2174 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002175
2176 if (current_call==PJSUA_INVALID_ID)
2177 current_call = call_id;
2178
2179 }
2180}
2181
2182
2183/**
2184 * Handler when there is incoming call.
2185 */
2186static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2187 pjsip_rx_data *rdata)
2188{
2189 pjsua_call_info call_info;
2190
2191 PJ_UNUSED_ARG(acc_id);
2192 PJ_UNUSED_ARG(rdata);
2193
2194 pjsua_call_get_info(call_id, &call_info);
2195
Benny Prijono91d20f42008-06-14 19:42:37 +00002196 /* Start ringback */
2197 ring_start(call_id);
2198
Benny Prijono2285e7e2008-12-17 14:28:18 +00002199 if (current_call==PJSUA_INVALID_ID)
2200 current_call = call_id;
2201
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002202 if (app_config.auto_answer > 0) {
2203 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2204 }
2205
2206 if (app_config.auto_answer < 200) {
2207 PJ_LOG(3,(THIS_FILE,
2208 "Incoming call for account %d!\n"
2209 "From: %s\n"
2210 "To: %s\n"
2211 "Press a to answer or h to reject call",
2212 acc_id,
2213 call_info.remote_info.ptr,
2214 call_info.local_info.ptr));
2215 }
2216}
2217
2218
2219/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002220 * Handler when a transaction within a call has changed state.
2221 */
2222static void on_call_tsx_state(pjsua_call_id call_id,
2223 pjsip_transaction *tsx,
2224 pjsip_event *e)
2225{
2226 const pjsip_method info_method =
2227 {
2228 PJSIP_OTHER_METHOD,
2229 { "INFO", 4 }
2230 };
2231
2232 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2233 /*
2234 * Handle INFO method.
2235 */
2236 if (tsx->role == PJSIP_ROLE_UAC &&
2237 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002238 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2239 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002240 {
2241 /* Status of outgoing INFO request */
2242 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2243 PJ_LOG(4,(THIS_FILE,
2244 "Call %d: DTMF sent successfully with INFO",
2245 call_id));
2246 } else if (tsx->status_code >= 300) {
2247 PJ_LOG(4,(THIS_FILE,
2248 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2249 call_id,
2250 tsx->status_code,
2251 (int)tsx->status_text.slen,
2252 tsx->status_text.ptr));
2253 }
2254 } else if (tsx->role == PJSIP_ROLE_UAS &&
2255 tsx->state == PJSIP_TSX_STATE_TRYING)
2256 {
2257 /* Answer incoming INFO with 200/OK */
2258 pjsip_rx_data *rdata;
2259 pjsip_tx_data *tdata;
2260 pj_status_t status;
2261
2262 rdata = e->body.tsx_state.src.rdata;
2263
2264 if (rdata->msg_info.msg->body) {
2265 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2266 200, NULL, &tdata);
2267 if (status == PJ_SUCCESS)
2268 status = pjsip_tsx_send_msg(tsx, tdata);
2269
2270 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2271 call_id,
2272 (int)rdata->msg_info.msg->body->len,
2273 rdata->msg_info.msg->body->data));
2274 } else {
2275 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2276 400, NULL, &tdata);
2277 if (status == PJ_SUCCESS)
2278 status = pjsip_tsx_send_msg(tsx, tdata);
2279 }
2280 }
2281 }
2282}
2283
2284
2285/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002286 * Callback on media state changed event.
2287 * The action may connect the call to sound device, to file, or
2288 * to loop the call.
2289 */
2290static void on_call_media_state(pjsua_call_id call_id)
2291{
2292 pjsua_call_info call_info;
2293
2294 pjsua_call_get_info(call_id, &call_info);
2295
Benny Prijono91d20f42008-06-14 19:42:37 +00002296 /* Stop ringback */
2297 ring_stop(call_id);
2298
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002299 /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
2300 * otherwise we should NOT connect the ports.
2301 */
2302 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE ||
2303 call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
2304 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002305 pj_bool_t connect_sound = PJ_TRUE;
2306
2307 /* Loopback sound, if desired */
2308 if (app_config.auto_loop) {
2309 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2310 connect_sound = PJ_FALSE;
Benny Prijonof6c77f42008-08-13 13:52:19 +00002311 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00002312
Benny Prijonof6c77f42008-08-13 13:52:19 +00002313 /* Automatically record conversation, if desired */
2314 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2315 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002316 }
2317
2318 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002319 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2320 app_config.wav_port != PJSUA_INVALID_ID)
2321 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002322 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2323 connect_sound = PJ_FALSE;
2324 }
2325
Benny Prijono7ca96da2006-08-07 12:11:40 +00002326 /* Put call in conference with other calls, if desired */
2327 if (app_config.auto_conf) {
2328 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002329 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002330 unsigned i;
2331
2332 /* Get all calls, and establish media connection between
2333 * this call and other calls.
2334 */
2335 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002336
Benny Prijono7ca96da2006-08-07 12:11:40 +00002337 for (i=0; i<call_cnt; ++i) {
2338 if (call_ids[i] == call_id)
2339 continue;
2340
2341 if (!pjsua_call_has_media(call_ids[i]))
2342 continue;
2343
2344 pjsua_conf_connect(call_info.conf_slot,
2345 pjsua_call_get_conf_port(call_ids[i]));
2346 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2347 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002348
2349 /* Automatically record conversation, if desired */
2350 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2351 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2352 app_config.rec_port);
2353 }
2354
Benny Prijono7ca96da2006-08-07 12:11:40 +00002355 }
2356
2357 /* Also connect call to local sound device */
2358 connect_sound = PJ_TRUE;
2359 }
2360
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002361 /* Otherwise connect to sound device */
2362 if (connect_sound) {
2363 pjsua_conf_connect(call_info.conf_slot, 0);
2364 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002365
2366 /* Automatically record conversation, if desired */
2367 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2368 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2369 pjsua_conf_connect(0, app_config.rec_port);
2370 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002371 }
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002372 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002373
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002374 /* Handle media status */
2375 switch (call_info.media_status) {
2376 case PJSUA_CALL_MEDIA_ACTIVE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002377 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002378 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002379
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002380 case PJSUA_CALL_MEDIA_LOCAL_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002381 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2382 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002383 break;
2384
2385 case PJSUA_CALL_MEDIA_REMOTE_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002386 PJ_LOG(3,(THIS_FILE,
2387 "Media for call %d is suspended (hold) by remote",
2388 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002389 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002390
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002391 case PJSUA_CALL_MEDIA_ERROR:
2392 PJ_LOG(3,(THIS_FILE,
Benny Prijono096c56c2007-09-15 08:30:16 +00002393 "Media has reported error, disconnecting call"));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002394 {
2395 pj_str_t reason = pj_str("ICE negotiation failed");
2396 pjsua_call_hangup(call_id, 500, &reason, NULL);
2397 }
2398 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002399
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002400 case PJSUA_CALL_MEDIA_NONE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002401 PJ_LOG(3,(THIS_FILE,
2402 "Media for call %d is inactive",
2403 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002404 break;
2405
2406 default:
2407 pj_assert(!"Unhandled media status");
2408 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002409 }
2410}
2411
Benny Prijono0875ae82006-12-26 00:11:48 +00002412/*
2413 * DTMF callback.
2414 */
2415static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2416{
2417 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2418}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002419
2420/*
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002421 * Redirection handler.
2422 */
Benny Prijono08a48b82008-11-27 12:42:07 +00002423static pjsip_redirect_op call_on_redirected(pjsua_call_id call_id,
2424 const pjsip_uri *target,
2425 const pjsip_event *e)
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002426{
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002427 PJ_UNUSED_ARG(e);
2428
Benny Prijono08a48b82008-11-27 12:42:07 +00002429 if (app_config.redir_op == PJSIP_REDIRECT_PENDING) {
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002430 char uristr[PJSIP_MAX_URL_SIZE];
2431 int len;
2432
2433 len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, target, uristr,
2434 sizeof(uristr));
2435 if (len < 1) {
2436 pj_ansi_strcpy(uristr, "--URI too long--");
2437 }
2438
2439 PJ_LOG(3,(THIS_FILE, "Call %d is being redirected to %.*s. "
2440 "Press 'Ra' to accept, 'Rr' to reject, or 'Rd' to "
2441 "disconnect.",
2442 call_id, len, uristr));
2443 }
Benny Prijono08a48b82008-11-27 12:42:07 +00002444
2445 return app_config.redir_op;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002446}
2447
2448/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002449 * Handler registration status has changed.
2450 */
2451static void on_reg_state(pjsua_acc_id acc_id)
2452{
2453 PJ_UNUSED_ARG(acc_id);
2454
2455 // Log already written.
2456}
2457
2458
2459/*
Benny Prijono563cd7a2008-08-19 20:02:15 +00002460 * Handler for incoming presence subscription request
2461 */
2462static void on_incoming_subscribe(pjsua_acc_id acc_id,
2463 pjsua_srv_pres *srv_pres,
2464 pjsua_buddy_id buddy_id,
2465 const pj_str_t *from,
2466 pjsip_rx_data *rdata,
2467 pjsip_status_code *code,
2468 pj_str_t *reason,
2469 pjsua_msg_data *msg_data)
2470{
2471 /* Just accept the request (the default behavior) */
2472 PJ_UNUSED_ARG(acc_id);
2473 PJ_UNUSED_ARG(srv_pres);
2474 PJ_UNUSED_ARG(buddy_id);
2475 PJ_UNUSED_ARG(from);
2476 PJ_UNUSED_ARG(rdata);
2477 PJ_UNUSED_ARG(code);
2478 PJ_UNUSED_ARG(reason);
2479 PJ_UNUSED_ARG(msg_data);
2480}
2481
2482
2483/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002484 * Handler on buddy state changed.
2485 */
2486static void on_buddy_state(pjsua_buddy_id buddy_id)
2487{
2488 pjsua_buddy_info info;
2489 pjsua_buddy_get_info(buddy_id, &info);
2490
2491 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2492 (int)info.uri.slen,
2493 info.uri.ptr,
2494 (int)info.status_text.slen,
2495 info.status_text.ptr));
2496}
2497
2498
2499/**
2500 * Incoming IM message (i.e. MESSAGE request)!
2501 */
2502static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2503 const pj_str_t *to, const pj_str_t *contact,
2504 const pj_str_t *mime_type, const pj_str_t *text)
2505{
2506 /* Note: call index may be -1 */
2507 PJ_UNUSED_ARG(call_id);
2508 PJ_UNUSED_ARG(to);
2509 PJ_UNUSED_ARG(contact);
2510 PJ_UNUSED_ARG(mime_type);
2511
Benny Prijonof4b538d2007-05-14 16:45:20 +00002512 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002513 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002514 (int)text->slen, text->ptr,
2515 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002516}
2517
2518
2519/**
2520 * Received typing indication
2521 */
2522static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2523 const pj_str_t *to, const pj_str_t *contact,
2524 pj_bool_t is_typing)
2525{
2526 PJ_UNUSED_ARG(call_id);
2527 PJ_UNUSED_ARG(to);
2528 PJ_UNUSED_ARG(contact);
2529
2530 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2531 (int)from->slen, from->ptr,
2532 (is_typing?"is typing..":"has stopped typing")));
2533}
2534
2535
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002536/**
2537 * Call transfer request status.
2538 */
2539static void on_call_transfer_status(pjsua_call_id call_id,
2540 int status_code,
2541 const pj_str_t *status_text,
2542 pj_bool_t final,
2543 pj_bool_t *p_cont)
2544{
2545 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2546 call_id, status_code,
2547 (int)status_text->slen, status_text->ptr,
2548 (final ? "[final]" : "")));
2549
2550 if (status_code/100 == 2) {
2551 PJ_LOG(3,(THIS_FILE,
2552 "Call %d: call transfered successfully, disconnecting call",
2553 call_id));
2554 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2555 *p_cont = PJ_FALSE;
2556 }
2557}
2558
2559
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002560/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002561 * Notification that call is being replaced.
2562 */
2563static void on_call_replaced(pjsua_call_id old_call_id,
2564 pjsua_call_id new_call_id)
2565{
2566 pjsua_call_info old_ci, new_ci;
2567
2568 pjsua_call_get_info(old_call_id, &old_ci);
2569 pjsua_call_get_info(new_call_id, &new_ci);
2570
2571 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2572 "call %d with %.*s",
2573 old_call_id,
2574 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2575 new_call_id,
2576 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2577}
2578
2579
2580/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002581 * NAT type detection callback.
2582 */
2583static void on_nat_detect(const pj_stun_nat_detect_result *res)
2584{
2585 if (res->status != PJ_SUCCESS) {
2586 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2587 } else {
2588 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2589 }
2590}
2591
2592
2593/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002594 * Print buddy list.
2595 */
2596static void print_buddy_list(void)
2597{
2598 pjsua_buddy_id ids[64];
2599 int i;
2600 unsigned count = PJ_ARRAY_SIZE(ids);
2601
2602 puts("Buddy list:");
2603
2604 pjsua_enum_buddies(ids, &count);
2605
2606 if (count == 0)
2607 puts(" -none-");
2608 else {
2609 for (i=0; i<(int)count; ++i) {
2610 pjsua_buddy_info info;
2611
2612 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2613 continue;
2614
Benny Prijono4461c7d2007-08-25 13:36:15 +00002615 printf(" [%2d] <%.*s> %.*s\n",
2616 ids[i]+1,
2617 (int)info.status_text.slen,
2618 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002619 (int)info.uri.slen,
2620 info.uri.ptr);
2621 }
2622 }
2623 puts("");
2624}
2625
2626
2627/*
2628 * Print account status.
2629 */
2630static void print_acc_status(int acc_id)
2631{
2632 char buf[80];
2633 pjsua_acc_info info;
2634
2635 pjsua_acc_get_info(acc_id, &info);
2636
2637 if (!info.has_registration) {
2638 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2639 (int)info.status_text.slen,
2640 info.status_text.ptr);
2641
2642 } else {
2643 pj_ansi_snprintf(buf, sizeof(buf),
2644 "%d/%.*s (expires=%d)",
2645 info.status,
2646 (int)info.status_text.slen,
2647 info.status_text.ptr,
2648 info.expires);
2649
2650 }
2651
2652 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2653 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002654 printf(" Online status: %.*s\n",
2655 (int)info.online_status_text.slen,
2656 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002657}
2658
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002659/* Playfile done notification, set timer to hangup calls */
2660pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2661{
2662 pj_time_val delay;
2663
2664 PJ_UNUSED_ARG(port);
2665 PJ_UNUSED_ARG(usr_data);
2666
2667 /* Just rewind WAV when it is played outside of call */
2668 if (pjsua_call_get_count() == 0) {
2669 pjsua_player_set_pos(app_config.wav_id, 0);
2670 return PJ_SUCCESS;
2671 }
2672
2673 /* Timer is already active */
2674 if (app_config.auto_hangup_timer.id == 1)
2675 return PJ_SUCCESS;
2676
2677 app_config.auto_hangup_timer.id = 1;
2678 delay.sec = 0;
2679 delay.msec = 200; /* Give 200 ms before hangup */
2680 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2681 &app_config.auto_hangup_timer,
2682 &delay);
2683
2684 return PJ_SUCCESS;
2685}
2686
2687/* Auto hangup timer callback */
2688static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2689 struct pj_timer_entry *entry)
2690{
2691 PJ_UNUSED_ARG(timer_heap);
2692 PJ_UNUSED_ARG(entry);
2693
2694 app_config.auto_hangup_timer.id = 0;
2695 pjsua_call_hangup_all();
2696}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002697
2698/*
2699 * Show a bit of help.
2700 */
2701static void keystroke_help(void)
2702{
2703 pjsua_acc_id acc_ids[16];
2704 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2705 int i;
2706
2707 printf(">>>>\n");
2708
2709 pjsua_enum_accs(acc_ids, &count);
2710
2711 printf("Account list:\n");
2712 for (i=0; i<(int)count; ++i)
2713 print_acc_status(acc_ids[i]);
2714
2715 print_buddy_list();
2716
2717 //puts("Commands:");
2718 puts("+=============================================================================+");
2719 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2720 puts("| | | |");
2721 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2722 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002723 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2724 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2725 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2726 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002727 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2728 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002729 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002730 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002731 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2732 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2733 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2734 puts("| | V Adjust audio Volume | f Save config |");
2735 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002736 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002737 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002738 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002739
2740 i = pjsua_call_get_count();
2741 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002742
2743 if (current_call != PJSUA_INVALID_ID) {
2744 pjsua_call_info ci;
2745 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2746 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2747 (int)ci.remote_info.slen, ci.remote_info.ptr,
2748 (int)ci.state_text.slen, ci.state_text.ptr);
2749 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002750}
2751
2752
2753/*
2754 * Input simple string
2755 */
2756static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2757{
2758 char *p;
2759
2760 printf("%s (empty to cancel): ", title); fflush(stdout);
Benny Prijono32d267b2009-01-01 22:08:21 +00002761 if (fgets(buf, len, stdin) == NULL)
2762 return PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002763
2764 /* Remove trailing newlines. */
2765 for (p=buf; ; ++p) {
2766 if (*p=='\r' || *p=='\n') *p='\0';
2767 else if (!*p) break;
2768 }
2769
2770 if (!*buf)
2771 return PJ_FALSE;
2772
2773 return PJ_TRUE;
2774}
2775
2776
2777#define NO_NB -2
2778struct input_result
2779{
2780 int nb_result;
2781 char *uri_result;
2782};
2783
2784
2785/*
2786 * Input URL.
2787 */
2788static void ui_input_url(const char *title, char *buf, int len,
2789 struct input_result *result)
2790{
2791 result->nb_result = NO_NB;
2792 result->uri_result = NULL;
2793
2794 print_buddy_list();
2795
2796 printf("Choices:\n"
2797 " 0 For current dialog.\n"
2798 " -1 All %d buddies in buddy list\n"
2799 " [1 -%2d] Select from buddy list\n"
2800 " URL An URL\n"
2801 " <Enter> Empty input (or 'q') to cancel\n"
2802 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2803 printf("%s: ", title);
2804
2805 fflush(stdout);
Benny Prijono32d267b2009-01-01 22:08:21 +00002806 if (fgets(buf, len, stdin) == NULL)
2807 return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002808 len = strlen(buf);
2809
2810 /* Left trim */
2811 while (pj_isspace(*buf)) {
2812 ++buf;
2813 --len;
2814 }
2815
2816 /* Remove trailing newlines */
2817 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2818 buf[--len] = '\0';
2819
2820 if (len == 0 || buf[0]=='q')
2821 return;
2822
2823 if (pj_isdigit(*buf) || *buf=='-') {
2824
2825 int i;
2826
2827 if (*buf=='-')
2828 i = 1;
2829 else
2830 i = 0;
2831
2832 for (; i<len; ++i) {
2833 if (!pj_isdigit(buf[i])) {
2834 puts("Invalid input");
2835 return;
2836 }
2837 }
2838
2839 result->nb_result = my_atoi(buf);
2840
2841 if (result->nb_result >= 0 &&
2842 result->nb_result <= (int)pjsua_get_buddy_count())
2843 {
2844 return;
2845 }
2846 if (result->nb_result == -1)
2847 return;
2848
2849 puts("Invalid input");
2850 result->nb_result = NO_NB;
2851 return;
2852
2853 } else {
2854 pj_status_t status;
2855
2856 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2857 pjsua_perror(THIS_FILE, "Invalid URL", status);
2858 return;
2859 }
2860
2861 result->uri_result = buf;
2862 }
2863}
2864
2865/*
2866 * List the ports in conference bridge
2867 */
2868static void conf_list(void)
2869{
2870 unsigned i, count;
2871 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2872
2873 printf("Conference ports:\n");
2874
2875 count = PJ_ARRAY_SIZE(id);
2876 pjsua_enum_conf_ports(id, &count);
2877
2878 for (i=0; i<count; ++i) {
2879 char txlist[PJSUA_MAX_CALLS*4+10];
2880 unsigned j;
2881 pjsua_conf_port_info info;
2882
2883 pjsua_conf_get_port_info(id[i], &info);
2884
2885 txlist[0] = '\0';
2886 for (j=0; j<info.listener_cnt; ++j) {
2887 char s[10];
2888 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2889 pj_ansi_strcat(txlist, s);
2890 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002891 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002892 info.slot_id,
2893 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002894 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002895 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002896 (int)info.name.slen,
2897 info.name.ptr,
2898 txlist);
2899
2900 }
2901 puts("");
2902}
2903
2904
2905/*
Benny Prijono56315612006-07-18 14:39:40 +00002906 * Send arbitrary request to remote host
2907 */
2908static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2909{
2910 pj_str_t str_method;
2911 pjsip_method method;
2912 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002913 pjsip_endpoint *endpt;
2914 pj_status_t status;
2915
2916 endpt = pjsua_get_pjsip_endpt();
2917
2918 str_method = pj_str(cstr_method);
2919 pjsip_method_init_np(&method, &str_method);
2920
Benny Prijonofff245c2007-04-02 11:44:47 +00002921 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002922
Benny Prijonob988d762007-12-05 04:30:04 +00002923 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002924 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002925 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002926 return;
2927 }
2928}
2929
2930
2931/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002932 * Change extended online status.
2933 */
2934static void change_online_status(void)
2935{
2936 char menuin[32];
2937 pj_bool_t online_status;
2938 pjrpid_element elem;
2939 int i, choice;
2940
2941 enum {
2942 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2943 };
2944
2945 struct opt {
2946 int id;
2947 char *name;
2948 } opts[] = {
2949 { AVAILABLE, "Available" },
2950 { BUSY, "Busy"},
2951 { OTP, "On the phone"},
2952 { IDLE, "Idle"},
2953 { AWAY, "Away"},
2954 { BRB, "Be right back"},
2955 { OFFLINE, "Offline"}
2956 };
2957
2958 printf("\n"
2959 "Choices:\n");
2960 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2961 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2962 }
2963
2964 if (!simple_input("Select status", menuin, sizeof(menuin)))
2965 return;
2966
2967 choice = atoi(menuin) - 1;
2968 if (choice < 0 || choice >= OPT_MAX) {
2969 puts("Invalid selection");
2970 return;
2971 }
2972
2973 pj_bzero(&elem, sizeof(elem));
2974 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2975
2976 online_status = PJ_TRUE;
2977
2978 switch (choice) {
2979 case AVAILABLE:
2980 break;
2981 case BUSY:
2982 elem.activity = PJRPID_ACTIVITY_BUSY;
2983 elem.note = pj_str("Busy");
2984 break;
2985 case OTP:
2986 elem.activity = PJRPID_ACTIVITY_BUSY;
2987 elem.note = pj_str("On the phone");
2988 break;
2989 case IDLE:
2990 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2991 elem.note = pj_str("Idle");
2992 break;
2993 case AWAY:
2994 elem.activity = PJRPID_ACTIVITY_AWAY;
2995 elem.note = pj_str("Away");
2996 break;
2997 case BRB:
2998 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2999 elem.note = pj_str("Be right back");
3000 break;
3001 case OFFLINE:
3002 online_status = PJ_FALSE;
3003 break;
3004 }
3005
3006 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
3007}
3008
3009
3010/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00003011 * Change codec priorities.
3012 */
3013static void manage_codec_prio(void)
3014{
3015 pjsua_codec_info c[32];
3016 unsigned i, count = PJ_ARRAY_SIZE(c);
3017 char input[32];
3018 char *codec, *prio;
3019 pj_str_t id;
3020 int new_prio;
3021 pj_status_t status;
3022
3023 printf("List of codecs:\n");
3024
3025 pjsua_enum_codecs(c, &count);
3026 for (i=0; i<count; ++i) {
3027 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
3028 c[i].codec_id.ptr);
3029 }
3030
3031 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00003032 puts("Enter codec id and its new priority "
3033 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00003034
Benny Prijono88accae2008-06-26 15:48:14 +00003035 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijono32d267b2009-01-01 22:08:21 +00003036 if (fgets(input, sizeof(input), stdin) == NULL)
3037 return;
Benny Prijonoc08682e2007-10-04 06:17:58 +00003038 if (input[0]=='\r' || input[0]=='\n') {
3039 puts("Done");
3040 return;
3041 }
3042
3043 codec = strtok(input, " \t\r\n");
3044 prio = strtok(NULL, " \r\n");
3045
3046 if (!codec || !prio) {
3047 puts("Invalid input");
3048 return;
3049 }
3050
3051 new_prio = atoi(prio);
3052 if (new_prio < 0)
3053 new_prio = 0;
3054 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
3055 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
3056
3057 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
3058 (pj_uint8_t)new_prio);
3059 if (status != PJ_SUCCESS)
3060 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
3061}
3062
3063
3064/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003065 * Main "user interface" loop.
3066 */
3067void console_app_main(const pj_str_t *uri_to_call)
3068{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003069 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003070 char buf[128];
3071 char text[128];
3072 int i, count;
3073 char *uri;
3074 pj_str_t tmp;
3075 struct input_result result;
3076 pjsua_call_info call_info;
3077 pjsua_acc_info acc_info;
3078
3079
3080 /* If user specifies URI to call, then call the URI */
3081 if (uri_to_call->slen) {
3082 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
3083 }
3084
3085 keystroke_help();
3086
3087 for (;;) {
3088
3089 printf(">>> ");
3090 fflush(stdout);
3091
Benny Prijono990042e2007-01-21 19:36:00 +00003092 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
3093 /*
3094 * Be friendly to users who redirect commands into
3095 * program, when file ends, resume with kbd.
3096 * If exit is desired end script with q for quit
3097 */
3098 /* Reopen stdin/stdout/stderr to /dev/console */
3099#if defined(PJ_WIN32) && PJ_WIN32!=0
3100 if (freopen ("CONIN$", "r", stdin) == NULL) {
3101#else
3102 if (1) {
3103#endif
3104 puts("Cannot switch back to console from file redirection");
3105 menuin[0] = 'q';
3106 menuin[1] = '\0';
3107 } else {
3108 puts("Switched back to console from file redirection");
3109 continue;
3110 }
3111 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003112
Benny Prijonoebc32c32008-06-12 13:30:39 +00003113 if (cmd_echo) {
3114 printf("%s", menuin);
3115 }
3116
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003117 switch (menuin[0]) {
3118
3119 case 'm':
3120 /* Make call! : */
3121 printf("(You currently have %d calls)\n",
3122 pjsua_call_get_count());
3123
3124 uri = NULL;
3125 ui_input_url("Make call", buf, sizeof(buf), &result);
3126 if (result.nb_result != NO_NB) {
3127
3128 if (result.nb_result == -1 || result.nb_result == 0) {
3129 puts("You can't do that with make call!");
3130 continue;
3131 } else {
3132 pjsua_buddy_info binfo;
3133 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijono45f2f352009-04-02 12:15:23 +00003134 tmp.ptr = buf;
3135 pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003136 }
3137
3138 } else if (result.uri_result) {
Benny Prijono45f2f352009-04-02 12:15:23 +00003139 tmp = pj_str(result.uri_result);
3140 } else {
3141 tmp.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003142 }
3143
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003144 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
3145 break;
3146
3147 case 'M':
3148 /* Make multiple calls! : */
3149 printf("(You currently have %d calls)\n",
3150 pjsua_call_get_count());
3151
3152 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
3153 continue;
3154
3155 count = my_atoi(menuin);
3156 if (count < 1)
3157 continue;
3158
3159 ui_input_url("Make call", buf, sizeof(buf), &result);
3160 if (result.nb_result != NO_NB) {
3161 pjsua_buddy_info binfo;
3162 if (result.nb_result == -1 || result.nb_result == 0) {
3163 puts("You can't do that with make call!");
3164 continue;
3165 }
3166 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijono45f2f352009-04-02 12:15:23 +00003167 tmp.ptr = buf;
3168 pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003169 } else {
Benny Prijono45f2f352009-04-02 12:15:23 +00003170 tmp = pj_str(result.uri_result);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003171 }
3172
3173 for (i=0; i<my_atoi(menuin); ++i) {
3174 pj_status_t status;
3175
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003176 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
3177 NULL, NULL);
3178 if (status != PJ_SUCCESS)
3179 break;
3180 }
3181 break;
3182
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003183 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00003184 i = pjsua_detect_nat_type();
3185 if (i != PJ_SUCCESS)
3186 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003187 break;
3188
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003189 case 'i':
3190 /* Send instant messaeg */
3191
3192 /* i is for call index to send message, if any */
3193 i = -1;
3194
3195 /* Make compiler happy. */
3196 uri = NULL;
3197
3198 /* Input destination. */
3199 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3200 if (result.nb_result != NO_NB) {
3201
3202 if (result.nb_result == -1) {
3203 puts("You can't send broadcast IM like that!");
3204 continue;
3205
3206 } else if (result.nb_result == 0) {
3207
3208 i = current_call;
3209
3210 } else {
3211 pjsua_buddy_info binfo;
3212 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijono45f2f352009-04-02 12:15:23 +00003213 tmp.ptr = buf;
3214 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
3215 uri = buf;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003216 }
3217
3218 } else if (result.uri_result) {
3219 uri = result.uri_result;
3220 }
3221
3222
3223 /* Send typing indication. */
3224 if (i != -1)
3225 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3226 else {
3227 pj_str_t tmp_uri = pj_str(uri);
3228 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3229 }
3230
3231 /* Input the IM . */
3232 if (!simple_input("Message", text, sizeof(text))) {
3233 /*
3234 * Cancelled.
3235 * Send typing notification too, saying we're not typing.
3236 */
3237 if (i != -1)
3238 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3239 else {
3240 pj_str_t tmp_uri = pj_str(uri);
3241 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3242 }
3243 continue;
3244 }
3245
3246 tmp = pj_str(text);
3247
3248 /* Send the IM */
3249 if (i != -1)
3250 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3251 else {
3252 pj_str_t tmp_uri = pj_str(uri);
3253 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3254 }
3255
3256 break;
3257
3258 case 'a':
3259
3260 if (current_call != -1) {
3261 pjsua_call_get_info(current_call, &call_info);
3262 } else {
3263 /* Make compiler happy */
3264 call_info.role = PJSIP_ROLE_UAC;
3265 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3266 }
3267
3268 if (current_call == -1 ||
3269 call_info.role != PJSIP_ROLE_UAS ||
3270 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3271 {
3272 puts("No pending incoming call");
3273 fflush(stdout);
3274 continue;
3275
3276 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003277 int st_code;
3278 char contact[120];
3279 pj_str_t hname = { "Contact", 7 };
3280 pj_str_t hvalue;
3281 pjsip_generic_string_hdr hcontact;
3282 pjsua_msg_data msg_data;
3283
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003284 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3285 continue;
3286
Benny Prijono20d36722007-02-22 14:52:24 +00003287 st_code = my_atoi(buf);
3288 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003289 continue;
3290
Benny Prijono20d36722007-02-22 14:52:24 +00003291 pjsua_msg_data_init(&msg_data);
3292
3293 if (st_code/100 == 3) {
3294 if (!simple_input("Enter URL to be put in Contact",
3295 contact, sizeof(contact)))
3296 continue;
3297 hvalue = pj_str(contact);
3298 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3299
3300 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3301 }
3302
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003303 /*
3304 * Must check again!
3305 * Call may have been disconnected while we're waiting for
3306 * keyboard input.
3307 */
3308 if (current_call == -1) {
3309 puts("Call has been disconnected");
3310 fflush(stdout);
3311 continue;
3312 }
3313
Benny Prijono20d36722007-02-22 14:52:24 +00003314 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003315 }
3316
3317 break;
3318
3319
3320 case 'h':
3321
3322 if (current_call == -1) {
3323 puts("No current call");
3324 fflush(stdout);
3325 continue;
3326
3327 } else if (menuin[1] == 'a') {
3328
3329 /* Hangup all calls */
3330 pjsua_call_hangup_all();
3331
3332 } else {
3333
3334 /* Hangup current calls */
3335 pjsua_call_hangup(current_call, 0, NULL, NULL);
3336 }
3337 break;
3338
3339 case ']':
3340 case '[':
3341 /*
3342 * Cycle next/prev dialog.
3343 */
3344 if (menuin[0] == ']') {
3345 find_next_call();
3346
3347 } else {
3348 find_prev_call();
3349 }
3350
3351 if (current_call != -1) {
3352
3353 pjsua_call_get_info(current_call, &call_info);
3354 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3355 (int)call_info.remote_info.slen,
3356 call_info.remote_info.ptr));
3357
3358 } else {
3359 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3360 }
3361 break;
3362
3363
3364 case '>':
3365 case '<':
3366 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3367 break;
3368
3369 i = my_atoi(buf);
3370 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003371 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003372 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3373 } else {
3374 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3375 }
3376 break;
3377
3378
3379 case '+':
3380 if (menuin[1] == 'b') {
3381
3382 pjsua_buddy_config buddy_cfg;
3383 pjsua_buddy_id buddy_id;
3384 pj_status_t status;
3385
3386 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3387 break;
3388
3389 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3390 printf("Invalid SIP URI '%s'\n", buf);
3391 break;
3392 }
3393
Benny Prijonoac623b32006-07-03 15:19:31 +00003394 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003395
3396 buddy_cfg.uri = pj_str(buf);
3397 buddy_cfg.subscribe = PJ_TRUE;
3398
3399 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3400 if (status == PJ_SUCCESS) {
3401 printf("New buddy '%s' added at index %d\n",
3402 buf, buddy_id+1);
3403 }
3404
3405 } else if (menuin[1] == 'a') {
3406
Benny Prijonofb2b3652007-06-28 07:15:03 +00003407 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3408 pjsua_acc_config acc_cfg;
3409 pj_status_t status;
3410
3411 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3412 break;
3413 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3414 break;
3415 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3416 break;
3417 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3418 break;
3419 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3420 break;
3421
3422 pjsua_acc_config_default(&acc_cfg);
3423 acc_cfg.id = pj_str(id);
3424 acc_cfg.reg_uri = pj_str(registrar);
3425 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003426 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003427 acc_cfg.cred_info[0].realm = pj_str(realm);
3428 acc_cfg.cred_info[0].username = pj_str(uname);
3429 acc_cfg.cred_info[0].data_type = 0;
3430 acc_cfg.cred_info[0].data = pj_str(passwd);
3431
3432 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3433 if (status != PJ_SUCCESS) {
3434 pjsua_perror(THIS_FILE, "Error adding new account", status);
3435 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003436
3437 } else {
3438 printf("Invalid input %s\n", menuin);
3439 }
3440 break;
3441
3442 case '-':
3443 if (menuin[1] == 'b') {
3444 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3445 break;
3446
3447 i = my_atoi(buf) - 1;
3448
3449 if (!pjsua_buddy_is_valid(i)) {
3450 printf("Invalid buddy id %d\n", i);
3451 } else {
3452 pjsua_buddy_del(i);
3453 printf("Buddy %d deleted\n", i);
3454 }
3455
3456 } else if (menuin[1] == 'a') {
3457
3458 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3459 break;
3460
3461 i = my_atoi(buf);
3462
3463 if (!pjsua_acc_is_valid(i)) {
3464 printf("Invalid account id %d\n", i);
3465 } else {
3466 pjsua_acc_del(i);
3467 printf("Account %d deleted\n", i);
3468 }
3469
3470 } else {
3471 printf("Invalid input %s\n", menuin);
3472 }
3473 break;
3474
3475 case 'H':
3476 /*
3477 * Hold call.
3478 */
3479 if (current_call != -1) {
3480
3481 pjsua_call_set_hold(current_call, NULL);
3482
3483 } else {
3484 PJ_LOG(3,(THIS_FILE, "No current call"));
3485 }
3486 break;
3487
3488 case 'v':
3489 /*
3490 * Send re-INVITE (to release hold, etc).
3491 */
3492 if (current_call != -1) {
3493
3494 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3495
3496 } else {
3497 PJ_LOG(3,(THIS_FILE, "No current call"));
3498 }
3499 break;
3500
Benny Prijonoc08682e2007-10-04 06:17:58 +00003501 case 'U':
3502 /*
3503 * Send UPDATE
3504 */
3505 if (current_call != -1) {
3506
3507 pjsua_call_update(current_call, 0, NULL);
3508
3509 } else {
3510 PJ_LOG(3,(THIS_FILE, "No current call"));
3511 }
3512 break;
3513
3514 case 'C':
3515 if (menuin[1] == 'p') {
3516 manage_codec_prio();
3517 }
3518 break;
3519
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003520 case 'x':
3521 /*
3522 * Transfer call.
3523 */
3524 if (current_call == -1) {
3525
3526 PJ_LOG(3,(THIS_FILE, "No current call"));
3527
3528 } else {
3529 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003530 pjsua_msg_data msg_data;
3531 pjsip_generic_string_hdr refer_sub;
3532 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3533 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003534 pjsua_call_info ci;
3535
3536 pjsua_call_get_info(current_call, &ci);
3537 printf("Transfering current call [%d] %.*s\n",
3538 current_call,
3539 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003540
3541 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3542
3543 /* Check if call is still there. */
3544
3545 if (call != current_call) {
3546 puts("Call has been disconnected");
3547 continue;
3548 }
3549
Benny Prijonod524e822006-09-22 12:48:18 +00003550 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003551 if (app_config.no_refersub) {
3552 /* Add Refer-Sub: false in outgoing REFER request */
3553 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3554 &STR_FALSE);
3555 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3556 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003557 if (result.nb_result != NO_NB) {
3558 if (result.nb_result == -1 || result.nb_result == 0)
3559 puts("You can't do that with transfer call!");
3560 else {
3561 pjsua_buddy_info binfo;
3562 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003563 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003564 }
3565
3566 } else if (result.uri_result) {
3567 pj_str_t tmp;
3568 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003569 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003570 }
3571 }
3572 break;
3573
Benny Prijonof7b1c392006-11-11 16:46:34 +00003574 case 'X':
3575 /*
3576 * Transfer call with replaces.
3577 */
3578 if (current_call == -1) {
3579
3580 PJ_LOG(3,(THIS_FILE, "No current call"));
3581
3582 } else {
3583 int call = current_call;
3584 int dst_call;
3585 pjsua_msg_data msg_data;
3586 pjsip_generic_string_hdr refer_sub;
3587 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3588 pj_str_t STR_FALSE = { "false", 5 };
3589 pjsua_call_id ids[PJSUA_MAX_CALLS];
3590 pjsua_call_info ci;
3591 unsigned i, count;
3592
3593 count = PJ_ARRAY_SIZE(ids);
3594 pjsua_enum_calls(ids, &count);
3595
3596 if (count <= 1) {
3597 puts("There are no other calls");
3598 continue;
3599 }
3600
3601 pjsua_call_get_info(current_call, &ci);
3602 printf("Transfer call [%d] %.*s to one of the following:\n",
3603 current_call,
3604 (int)ci.remote_info.slen, ci.remote_info.ptr);
3605
3606 for (i=0; i<count; ++i) {
3607 pjsua_call_info call_info;
3608
3609 if (ids[i] == call)
3610 continue;
3611
3612 pjsua_call_get_info(ids[i], &call_info);
3613 printf("%d %.*s [%.*s]\n",
3614 ids[i],
3615 (int)call_info.remote_info.slen,
3616 call_info.remote_info.ptr,
3617 (int)call_info.state_text.slen,
3618 call_info.state_text.ptr);
3619 }
3620
3621 if (!simple_input("Enter call number to be replaced",
3622 buf, sizeof(buf)))
3623 continue;
3624
3625 dst_call = my_atoi(buf);
3626
3627 /* Check if call is still there. */
3628
3629 if (call != current_call) {
3630 puts("Call has been disconnected");
3631 continue;
3632 }
3633
3634 /* Check that destination call is valid. */
3635 if (dst_call == call) {
3636 puts("Destination call number must not be the same "
3637 "as the call being transfered");
3638 continue;
3639 }
3640 if (dst_call >= PJSUA_MAX_CALLS) {
3641 puts("Invalid destination call number");
3642 continue;
3643 }
3644 if (!pjsua_call_is_active(dst_call)) {
3645 puts("Invalid destination call number");
3646 continue;
3647 }
3648
3649 pjsua_msg_data_init(&msg_data);
3650 if (app_config.no_refersub) {
3651 /* Add Refer-Sub: false in outgoing REFER request */
3652 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3653 &STR_FALSE);
3654 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3655 }
3656
3657 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3658 }
3659 break;
3660
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003661 case '#':
3662 /*
3663 * Send DTMF strings.
3664 */
3665 if (current_call == -1) {
3666
3667 PJ_LOG(3,(THIS_FILE, "No current call"));
3668
3669 } else if (!pjsua_call_has_media(current_call)) {
3670
3671 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3672
3673 } else {
3674 pj_str_t digits;
3675 int call = current_call;
3676 pj_status_t status;
3677
3678 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3679 sizeof(buf)))
3680 {
3681 break;
3682 }
3683
3684 if (call != current_call) {
3685 puts("Call has been disconnected");
3686 continue;
3687 }
3688
3689 digits = pj_str(buf);
3690 status = pjsua_call_dial_dtmf(current_call, &digits);
3691 if (status != PJ_SUCCESS) {
3692 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3693 } else {
3694 puts("DTMF digits enqueued for transmission");
3695 }
3696 }
3697 break;
3698
Benny Prijonofeb69f42007-10-05 09:12:26 +00003699 case '*':
3700 /* Send DTMF with INFO */
3701 if (current_call == -1) {
3702
3703 PJ_LOG(3,(THIS_FILE, "No current call"));
3704
3705 } else {
3706 const pj_str_t SIP_INFO = pj_str("INFO");
3707 pj_str_t digits;
3708 int call = current_call;
3709 int i;
3710 pj_status_t status;
3711
3712 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3713 sizeof(buf)))
3714 {
3715 break;
3716 }
3717
3718 if (call != current_call) {
3719 puts("Call has been disconnected");
3720 continue;
3721 }
3722
3723 digits = pj_str(buf);
3724 for (i=0; i<digits.slen; ++i) {
3725 pjsua_msg_data msg_data;
3726 char body[80];
3727
3728 pjsua_msg_data_init(&msg_data);
3729 msg_data.content_type = pj_str("application/dtmf-relay");
3730
3731 pj_ansi_snprintf(body, sizeof(body),
3732 "Signal=%c\r\n"
3733 "Duration=160",
3734 buf[i]);
3735 msg_data.msg_body = pj_str(body);
3736
3737 status = pjsua_call_send_request(current_call, &SIP_INFO,
3738 &msg_data);
3739 if (status != PJ_SUCCESS) {
3740 break;
3741 }
3742 }
3743 }
3744 break;
3745
Benny Prijono56315612006-07-18 14:39:40 +00003746 case 'S':
3747 /*
3748 * Send arbitrary request
3749 */
3750 if (pjsua_acc_get_count() == 0) {
3751 puts("Sorry, need at least one account configured");
3752 break;
3753 }
3754
3755 puts("Send arbitrary request to remote host");
3756
3757 /* Input METHOD */
3758 if (!simple_input("Request method:",text,sizeof(text)))
3759 break;
3760
3761 /* Input destination URI */
3762 uri = NULL;
3763 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3764 if (result.nb_result != NO_NB) {
3765
3766 if (result.nb_result == -1 || result.nb_result == 0) {
3767 puts("Sorry you can't do that!");
3768 continue;
3769 } else {
3770 pjsua_buddy_info binfo;
3771 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijono45f2f352009-04-02 12:15:23 +00003772 tmp.ptr = buf;
3773 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
3774 uri = buf;
Benny Prijono56315612006-07-18 14:39:40 +00003775 }
3776
3777 } else if (result.uri_result) {
3778 uri = result.uri_result;
3779 }
3780
3781 tmp = pj_str(uri);
3782
3783 send_request(text, &tmp);
3784 break;
3785
Benny Prijonoebc32c32008-06-12 13:30:39 +00003786 case 'e':
3787 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3788 pj_str_t tmp;
3789
3790 tmp.ptr = menuin+5;
3791 tmp.slen = pj_ansi_strlen(menuin)-6;
3792
3793 if (tmp.slen < 1) {
3794 puts("Usage: echo [0|1]");
3795 break;
3796 }
3797
Benny Prijonoddd02de2008-06-26 22:20:11 +00003798 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003799 }
3800 break;
3801
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003802 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003803 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3804 pj_str_t tmp;
3805 int delay;
3806
3807 tmp.ptr = menuin+6;
3808 tmp.slen = pj_ansi_strlen(menuin)-7;
3809
3810 if (tmp.slen < 1) {
3811 puts("Usage: sleep MSEC");
3812 break;
3813 }
3814
3815 delay = pj_strtoul(&tmp);
3816 if (delay < 0) delay = 0;
3817 pj_thread_sleep(delay);
3818 break;
3819 }
3820 /* Continue below */
3821
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003822 case 'u':
3823 /*
3824 * Subscribe/unsubscribe presence.
3825 */
3826 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3827 if (result.nb_result != NO_NB) {
3828 if (result.nb_result == -1) {
3829 int i, count;
3830 count = pjsua_get_buddy_count();
3831 for (i=0; i<count; ++i)
3832 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3833 } else if (result.nb_result == 0) {
3834 puts("Sorry, can only subscribe to buddy's presence, "
3835 "not from existing call");
3836 } else {
3837 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3838 }
3839
3840 } else if (result.uri_result) {
3841 puts("Sorry, can only subscribe to buddy's presence, "
3842 "not arbitrary URL (for now)");
3843 }
3844
3845 break;
3846
3847 case 'r':
3848 switch (menuin[1]) {
3849 case 'r':
3850 /*
3851 * Re-Register.
3852 */
3853 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3854 break;
3855 case 'u':
3856 /*
3857 * Unregister
3858 */
3859 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3860 break;
3861 }
3862 break;
3863
3864 case 't':
3865 pjsua_acc_get_info(current_acc, &acc_info);
3866 acc_info.online_status = !acc_info.online_status;
3867 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3868 printf("Setting %s online status to %s\n",
3869 acc_info.acc_uri.ptr,
3870 (acc_info.online_status?"online":"offline"));
3871 break;
3872
Benny Prijono4461c7d2007-08-25 13:36:15 +00003873 case 'T':
3874 change_online_status();
3875 break;
3876
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003877 case 'c':
3878 switch (menuin[1]) {
3879 case 'l':
3880 conf_list();
3881 break;
3882 case 'c':
3883 case 'd':
3884 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003885 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003886 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003887 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003888 const char *src_title, *dst_title;
3889
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003890 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003891
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003892 if (cnt != 3) {
3893 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003894
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003895 src_title = (menuin[1]=='c'?
3896 "Connect src port #":
3897 "Disconnect src port #");
3898 dst_title = (menuin[1]=='c'?
3899 "To dst port #":
3900 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003901
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003902 if (!simple_input(src_title, src_port, sizeof(src_port)))
3903 break;
3904
3905 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3906 break;
3907 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003908
3909 if (menuin[1]=='c') {
3910 status = pjsua_conf_connect(my_atoi(src_port),
3911 my_atoi(dst_port));
3912 } else {
3913 status = pjsua_conf_disconnect(my_atoi(src_port),
3914 my_atoi(dst_port));
3915 }
3916 if (status == PJ_SUCCESS) {
3917 puts("Success");
3918 } else {
3919 puts("ERROR!!");
3920 }
3921 }
3922 break;
3923 }
3924 break;
3925
Benny Prijono6dd967c2006-12-26 02:27:14 +00003926 case 'V':
3927 /* Adjust audio volume */
3928 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3929 if (simple_input(buf,text,sizeof(text))) {
3930 char *err;
3931 app_config.mic_level = (float)strtod(text, &err);
3932 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3933 }
3934 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3935 app_config.speaker_level);
3936 if (simple_input(buf,text,sizeof(text))) {
3937 char *err;
3938 app_config.speaker_level = (float)strtod(text, &err);
3939 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3940 }
3941
3942 break;
3943
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003944 case 'd':
3945 if (menuin[1] == 'c') {
3946 char settings[2000];
3947 int len;
3948
3949 len = write_settings(&app_config, settings, sizeof(settings));
3950 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003951 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003952 else
3953 PJ_LOG(3,(THIS_FILE,
3954 "Dumping configuration (%d bytes):\n%s\n",
3955 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003956
3957 } else if (menuin[1] == 'q') {
3958
3959 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00003960 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00003961 } else {
3962 PJ_LOG(3,(THIS_FILE, "No current call"));
3963 }
3964
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003965 } else {
3966 app_dump(menuin[1]=='d');
3967 }
3968 break;
3969
3970
3971 case 'f':
3972 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3973 char settings[2000];
3974 int len;
3975
3976 len = write_settings(&app_config, settings, sizeof(settings));
3977 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003978 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003979 else {
3980 pj_oshandle_t fd;
3981 pj_status_t status;
3982
3983 status = pj_file_open(app_config.pool, buf,
3984 PJ_O_WRONLY, &fd);
3985 if (status != PJ_SUCCESS) {
3986 pjsua_perror(THIS_FILE, "Unable to open file", status);
3987 } else {
3988 pj_ssize_t size = len;
3989 pj_file_write(fd, settings, &size);
3990 pj_file_close(fd);
3991
3992 printf("Settings successfully written to '%s'\n", buf);
3993 }
3994 }
3995
3996 }
3997 break;
3998
3999
4000 case 'q':
4001 goto on_exit;
4002
4003
Benny Prijono5e51a4e2008-11-27 00:06:46 +00004004 case 'R':
4005 if (!pjsua_call_is_active(current_call)) {
4006 PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
4007 } else if (menuin[1] == 'a') {
4008 pjsua_call_process_redirect(current_call,
4009 PJSIP_REDIRECT_ACCEPT);
4010 } else if (menuin[1] == 'r') {
4011 pjsua_call_process_redirect(current_call,
4012 PJSIP_REDIRECT_REJECT);
4013 } else {
4014 pjsua_call_process_redirect(current_call,
4015 PJSIP_REDIRECT_STOP);
4016 }
4017 break;
4018
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004019 default:
4020 if (menuin[0] != '\n' && menuin[0] != '\r') {
4021 printf("Invalid input %s", menuin);
4022 }
4023 keystroke_help();
4024 break;
4025 }
4026 }
4027
4028on_exit:
4029 ;
4030}
4031
Benny Prijonod9e8dfe2009-03-12 11:25:11 +00004032/*****************************************************************************
4033 * A simple module to handle otherwise unhandled request. We will register
4034 * this with the lowest priority.
4035 */
4036
4037/* Notification on incoming request */
4038static pj_bool_t default_mod_on_rx_request(pjsip_rx_data *rdata)
4039{
4040 pjsip_tx_data *tdata;
4041 pjsip_status_code status_code;
4042 pj_status_t status;
4043
4044 /* Don't respond to ACK! */
4045 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
4046 &pjsip_ack_method) == 0)
4047 return PJ_TRUE;
4048
4049 /* Create basic response. */
4050 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
4051 &pjsip_notify_method) == 0)
4052 {
4053 /* Unsolicited NOTIFY's, send with Bad Request */
4054 status_code = PJSIP_SC_BAD_REQUEST;
4055 } else {
4056 /* Probably unknown method */
4057 status_code = PJSIP_SC_METHOD_NOT_ALLOWED;
4058 }
4059 status = pjsip_endpt_create_response(pjsua_get_pjsip_endpt(),
4060 rdata, status_code,
4061 NULL, &tdata);
4062 if (status != PJ_SUCCESS) {
4063 pjsua_perror(THIS_FILE, "Unable to create response", status);
4064 return PJ_TRUE;
4065 }
4066
4067 /* Add Allow if we're responding with 405 */
4068 if (status_code == PJSIP_SC_METHOD_NOT_ALLOWED) {
4069 const pjsip_hdr *cap_hdr;
4070 cap_hdr = pjsip_endpt_get_capability(pjsua_get_pjsip_endpt(),
4071 PJSIP_H_ALLOW, NULL);
4072 if (cap_hdr) {
4073 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool,
4074 cap_hdr));
4075 }
4076 }
4077
4078 /* Add User-Agent header */
4079 {
4080 pj_str_t user_agent;
4081 char tmp[80];
4082 const pj_str_t USER_AGENT = { "User-Agent", 10};
4083 pjsip_hdr *h;
4084
4085 pj_ansi_snprintf(tmp, sizeof(tmp), "PJSUA v%s/%s",
4086 pj_get_version(), PJ_OS_NAME);
4087 pj_strdup2_with_null(tdata->pool, &user_agent, tmp);
4088
4089 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
4090 &USER_AGENT,
4091 &user_agent);
4092 pjsip_msg_add_hdr(tdata->msg, h);
4093 }
4094
4095 pjsip_endpt_send_response2(pjsua_get_pjsip_endpt(), rdata, tdata,
4096 NULL, NULL);
4097
4098 return PJ_TRUE;
4099}
4100
4101
4102/* The module instance. */
4103static pjsip_module mod_default_handler =
4104{
4105 NULL, NULL, /* prev, next. */
4106 { "mod-default-handler", 19 }, /* Name. */
4107 -1, /* Id */
4108 PJSIP_MOD_PRIORITY_APPLICATION+99, /* Priority */
4109 NULL, /* load() */
4110 NULL, /* start() */
4111 NULL, /* stop() */
4112 NULL, /* unload() */
4113 &default_mod_on_rx_request, /* on_rx_request() */
4114 NULL, /* on_rx_response() */
4115 NULL, /* on_tx_request. */
4116 NULL, /* on_tx_response() */
4117 NULL, /* on_tsx_state() */
4118
4119};
4120
4121
4122
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004123
4124/*****************************************************************************
4125 * Public API
4126 */
4127
4128pj_status_t app_init(int argc, char *argv[])
4129{
Benny Prijonoe93e2872006-06-28 16:46:49 +00004130 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00004131 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004132 unsigned i;
4133 pj_status_t status;
4134
4135 /* Create pjsua */
4136 status = pjsua_create();
4137 if (status != PJ_SUCCESS)
4138 return status;
4139
4140 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00004141 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004142
4143 /* Initialize default config */
4144 default_config(&app_config);
4145
4146 /* Parse the arguments */
4147 status = parse_args(argc, argv, &app_config, &uri_arg);
4148 if (status != PJ_SUCCESS)
4149 return status;
4150
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004151 /* Initialize application callbacks */
4152 app_config.cfg.cb.on_call_state = &on_call_state;
4153 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
4154 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00004155 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00004156 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00004157 app_config.cfg.cb.on_call_redirected = &call_on_redirected;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004158 app_config.cfg.cb.on_reg_state = &on_reg_state;
Benny Prijono563cd7a2008-08-19 20:02:15 +00004159 app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004160 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
4161 app_config.cfg.cb.on_pager = &on_pager;
4162 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00004163 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00004164 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00004165 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004166
Benny Prijono10454dc2009-02-21 14:21:59 +00004167 /* Set sound device latency */
4168 if (app_config.capture_lat > 0)
4169 app_config.media_cfg.snd_rec_latency = app_config.capture_lat;
4170 if (app_config.playback_lat)
4171 app_config.media_cfg.snd_play_latency = app_config.playback_lat;
4172
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004173 /* Initialize pjsua */
4174 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
4175 &app_config.media_cfg);
4176 if (status != PJ_SUCCESS)
4177 return status;
4178
Benny Prijonod9e8dfe2009-03-12 11:25:11 +00004179 /* Initialize our module to handle otherwise unhandled request */
4180 status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
4181 &mod_default_handler);
4182 if (status != PJ_SUCCESS)
4183 return status;
4184
Benny Prijonoe909eac2006-07-27 22:04:56 +00004185#ifdef STEREO_DEMO
4186 stereo_demo();
4187#endif
4188
Benny Prijono804ff0a2006-09-14 11:17:48 +00004189 /* Initialize calls data */
4190 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
4191 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
4192 app_config.call_data[i].timer.cb = &call_timeout_callback;
4193 }
4194
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004195 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00004196 for (i=0; i<app_config.wav_count; ++i) {
4197 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004198 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00004199
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004200 if (app_config.auto_play_hangup)
4201 play_options |= PJMEDIA_FILE_NO_LOOP;
4202
4203 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00004204 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004205 if (status != PJ_SUCCESS)
4206 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00004207
Benny Prijono72c04d22007-02-17 22:20:58 +00004208 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00004209 app_config.wav_id = wav_id;
4210 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004211 if (app_config.auto_play_hangup) {
4212 pjmedia_port *port;
4213
4214 pjsua_player_get_port(app_config.wav_id, &port);
4215 status = pjmedia_wav_player_set_eof_cb(port, NULL,
4216 &on_playfile_done);
4217 if (status != PJ_SUCCESS)
4218 goto on_error;
4219
4220 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
4221 &hangup_timeout_callback);
4222 }
Benny Prijono32e4f492007-01-24 00:44:26 +00004223 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004224 }
4225
Benny Prijono4af234b2007-01-24 02:02:09 +00004226 /* Optionally registers tone players */
4227 for (i=0; i<app_config.tone_count; ++i) {
4228 pjmedia_port *tport;
4229 char name[80];
4230 pj_str_t label;
4231 pj_status_t status;
4232
4233 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
4234 app_config.tones[i].freq1,
4235 app_config.tones[i].freq2);
4236 label = pj_str(name);
4237 status = pjmedia_tonegen_create2(app_config.pool, &label,
4238 8000, 1, 160, 16,
4239 PJMEDIA_TONEGEN_LOOP, &tport);
4240 if (status != PJ_SUCCESS) {
4241 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
4242 goto on_error;
4243 }
4244
4245 status = pjsua_conf_add_port(app_config.pool, tport,
4246 &app_config.tone_slots[i]);
4247 pj_assert(status == PJ_SUCCESS);
4248
4249 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
4250 pj_assert(status == PJ_SUCCESS);
4251 }
4252
Benny Prijono1ebd6142006-10-19 15:48:02 +00004253 /* Optionally create recorder file, if any. */
4254 if (app_config.rec_file.slen) {
4255 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
4256 &app_config.rec_id);
4257 if (status != PJ_SUCCESS)
4258 goto on_error;
4259
4260 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
4261 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004262
Benny Prijonoe347cb02007-02-14 14:36:13 +00004263 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
4264
Benny Prijono91d20f42008-06-14 19:42:37 +00004265 /* Create ringback tones */
4266 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00004267 unsigned i, samples_per_frame;
4268 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00004269 pj_str_t name;
4270
Benny Prijono4f966892008-06-14 22:43:56 +00004271 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
4272 app_config.media_cfg.clock_rate *
4273 app_config.media_cfg.channel_count / 1000;
4274
Benny Prijono91d20f42008-06-14 19:42:37 +00004275 /* Ringback tone (call is ringing) */
4276 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00004277 status = pjmedia_tonegen_create2(app_config.pool, &name,
4278 app_config.media_cfg.clock_rate,
4279 app_config.media_cfg.channel_count,
4280 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004281 16, PJMEDIA_TONEGEN_LOOP,
4282 &app_config.ringback_port);
4283 if (status != PJ_SUCCESS)
4284 goto on_error;
4285
Benny Prijonoe28cec82008-06-14 20:40:53 +00004286 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00004287 for (i=0; i<RINGBACK_CNT; ++i) {
4288 tone[i].freq1 = RINGBACK_FREQ1;
4289 tone[i].freq2 = RINGBACK_FREQ2;
4290 tone[i].on_msec = RINGBACK_ON;
4291 tone[i].off_msec = RINGBACK_OFF;
4292 }
4293 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004294
Benny Prijono4f966892008-06-14 22:43:56 +00004295 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004296 PJMEDIA_TONEGEN_LOOP);
4297
4298
Benny Prijono91d20f42008-06-14 19:42:37 +00004299 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
4300 &app_config.ringback_slot);
4301 if (status != PJ_SUCCESS)
4302 goto on_error;
4303
4304 /* Ring (to alert incoming call) */
4305 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00004306 status = pjmedia_tonegen_create2(app_config.pool, &name,
4307 app_config.media_cfg.clock_rate,
4308 app_config.media_cfg.channel_count,
4309 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004310 16, PJMEDIA_TONEGEN_LOOP,
4311 &app_config.ring_port);
4312 if (status != PJ_SUCCESS)
4313 goto on_error;
4314
Benny Prijono4f966892008-06-14 22:43:56 +00004315 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00004316 tone[i].freq1 = RING_FREQ1;
4317 tone[i].freq2 = RING_FREQ2;
4318 tone[i].on_msec = RING_ON;
4319 tone[i].off_msec = RING_OFF;
4320 }
Benny Prijono4f966892008-06-14 22:43:56 +00004321 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004322
Benny Prijono4f966892008-06-14 22:43:56 +00004323 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004324 tone, PJMEDIA_TONEGEN_LOOP);
4325
Benny Prijono91d20f42008-06-14 19:42:37 +00004326 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4327 &app_config.ring_slot);
4328 if (status != PJ_SUCCESS)
4329 goto on_error;
4330
4331 }
4332
Benny Prijono87ef89a2007-01-14 00:39:45 +00004333 /* Add UDP transport unless it's disabled. */
4334 if (!app_config.no_udp) {
4335 pjsua_acc_id aid;
4336
4337 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
4338 &app_config.udp_cfg,
4339 &transport_id);
4340 if (status != PJ_SUCCESS)
4341 goto on_error;
4342
4343 /* Add local account */
4344 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4345 //pjsua_acc_set_transport(aid, transport_id);
4346 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004347
4348 if (app_config.udp_cfg.port == 0) {
4349 pjsua_transport_info ti;
4350 pj_sockaddr_in *a;
4351
4352 pjsua_transport_get_info(transport_id, &ti);
4353 a = (pj_sockaddr_in*)&ti.local_addr;
4354
4355 tcp_cfg.port = pj_ntohs(a->sin_port);
4356 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004357 }
4358
Benny Prijonoe93e2872006-06-28 16:46:49 +00004359 /* Add TCP transport unless it's disabled */
4360 if (!app_config.no_tcp) {
4361 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004362 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004363 &transport_id);
4364 if (status != PJ_SUCCESS)
4365 goto on_error;
4366
4367 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004368 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004369 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4370
4371 }
4372
Benny Prijonoe93e2872006-06-28 16:46:49 +00004373
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004374#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4375 /* Add TLS transport when application wants one */
4376 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004377
4378 pjsua_acc_id acc_id;
4379
4380 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004381 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004382 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004383 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004384 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004385 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004386 if (status != PJ_SUCCESS)
4387 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004388
4389 /* Add local account */
4390 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4391 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004392 }
4393#endif
4394
Benny Prijonoe93e2872006-06-28 16:46:49 +00004395 if (transport_id == -1) {
Benny Prijonod6e362a2008-07-19 17:53:47 +00004396 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
Benny Prijonoe93e2872006-06-28 16:46:49 +00004397 status = -1;
4398 goto on_error;
4399 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004400
4401
4402 /* Add accounts */
4403 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004404 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004405 if (status != PJ_SUCCESS)
4406 goto on_error;
4407 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4408 }
4409
4410 /* Add buddies */
4411 for (i=0; i<app_config.buddy_cnt; ++i) {
4412 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4413 if (status != PJ_SUCCESS)
4414 goto on_error;
4415 }
4416
4417 /* Optionally set codec orders */
4418 for (i=0; i<app_config.codec_cnt; ++i) {
4419 pjsua_codec_set_priority(&app_config.codec_arg[i],
4420 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4421 }
4422
Benny Prijonofce28542007-12-09 15:41:10 +00004423 /* Optionally disable some codec */
4424 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4425 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4426 }
4427
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004428 /* Add RTP transports */
Benny Prijono40860c32008-09-04 13:55:33 +00004429#ifdef TRANSPORT_ADAPTER_SAMPLE
4430 status = transport_adapter_sample();
4431
4432#else
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004433 status = pjsua_media_transports_create(&app_config.rtp_cfg);
Benny Prijono40860c32008-09-04 13:55:33 +00004434#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004435 if (status != PJ_SUCCESS)
4436 goto on_error;
4437
Benny Prijono22a300a2006-06-14 20:04:55 +00004438 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004439#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004440 if (app_config.null_audio) {
4441 status = pjsua_set_null_snd_dev();
4442 if (status != PJ_SUCCESS)
4443 return status;
4444 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004445#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004446
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004447 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4448 app_config.playback_dev != PJSUA_INVALID_ID)
4449 {
4450 status = pjsua_set_snd_dev(app_config.capture_dev,
4451 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004452 if (status != PJ_SUCCESS)
4453 goto on_error;
4454 }
4455
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004456 return PJ_SUCCESS;
4457
4458on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004459 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004460 return status;
4461}
4462
4463
Benny Prijonoebc32c32008-06-12 13:30:39 +00004464static int stdout_refresh_proc(void *arg)
4465{
4466 PJ_UNUSED_ARG(arg);
4467
4468 /* Set thread to lowest priority so that it doesn't clobber
4469 * stdout output
4470 */
4471 pj_thread_set_prio(pj_thread_this(),
4472 pj_thread_get_prio_min(pj_thread_this()));
4473
4474 while (!stdout_refresh_quit) {
4475 pj_thread_sleep(stdout_refresh * 1000);
4476 puts(stdout_refresh_text);
4477 fflush(stdout);
4478 }
4479
4480 return 0;
4481}
4482
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004483pj_status_t app_main(void)
4484{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004485 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004486 pj_status_t status;
4487
4488 /* Start pjsua */
4489 status = pjsua_start();
4490 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004491 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004492 return status;
4493 }
4494
Benny Prijonoebc32c32008-06-12 13:30:39 +00004495 /* Start console refresh thread */
4496 if (stdout_refresh > 0) {
4497 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4498 NULL, 0, 0, &stdout_refresh_thread);
4499 }
4500
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004501 console_app_main(&uri_arg);
4502
Benny Prijonoebc32c32008-06-12 13:30:39 +00004503 if (stdout_refresh_thread) {
4504 stdout_refresh_quit = PJ_TRUE;
4505 pj_thread_join(stdout_refresh_thread);
4506 pj_thread_destroy(stdout_refresh_thread);
4507 }
4508
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004509 return PJ_SUCCESS;
4510}
4511
4512pj_status_t app_destroy(void)
4513{
Benny Prijonof762ee72006-12-01 11:14:37 +00004514 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004515 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004516
Benny Prijonoe909eac2006-07-27 22:04:56 +00004517#ifdef STEREO_DEMO
4518 if (app_config.snd) {
4519 pjmedia_snd_port_destroy(app_config.snd);
4520 app_config.snd = NULL;
4521 }
4522#endif
4523
Benny Prijono91d20f42008-06-14 19:42:37 +00004524 /* Close ringback port */
4525 if (app_config.ringback_port &&
4526 app_config.ringback_slot != PJSUA_INVALID_ID)
4527 {
4528 pjsua_conf_remove_port(app_config.ringback_slot);
4529 app_config.ringback_slot = PJSUA_INVALID_ID;
4530 pjmedia_port_destroy(app_config.ringback_port);
4531 app_config.ringback_port = NULL;
4532 }
4533
4534 /* Close ring port */
4535 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4536 pjsua_conf_remove_port(app_config.ring_slot);
4537 app_config.ring_slot = PJSUA_INVALID_ID;
4538 pjmedia_port_destroy(app_config.ring_port);
4539 app_config.ring_port = NULL;
4540 }
4541
Benny Prijono4af234b2007-01-24 02:02:09 +00004542 /* Close tone generators */
4543 for (i=0; i<app_config.tone_count; ++i) {
4544 pjsua_conf_remove_port(app_config.tone_slots[i]);
4545 }
4546
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004547 if (app_config.pool) {
4548 pj_pool_release(app_config.pool);
4549 app_config.pool = NULL;
4550 }
4551
Benny Prijonof762ee72006-12-01 11:14:37 +00004552 status = pjsua_destroy();
4553
4554 pj_bzero(&app_config, sizeof(app_config));
4555
4556 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004557}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004558
4559
4560#ifdef STEREO_DEMO
4561static void stereo_demo()
4562{
4563 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004564 pj_status_t status;
4565
4566 /* Disable existing sound device */
4567 conf = pjsua_set_no_snd_dev();
4568
Benny Prijonoe909eac2006-07-27 22:04:56 +00004569 /* Create stereo-mono splitter/combiner */
4570 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004571 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004572 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004573 2 * conf->info.samples_per_frame,
4574 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004575 0 /* options */,
4576 &splitter);
4577 pj_assert(status == PJ_SUCCESS);
4578
4579 /* Connect channel0 (left channel?) to conference port slot0 */
4580 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4581 0 /*options*/,
4582 conf);
4583 pj_assert(status == PJ_SUCCESS);
4584
4585 /* Create reverse channel for channel1 (right channel?)... */
4586 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4587 splitter,
4588 1 /* ch1 */,
4589 0 /* options */,
4590 &ch1);
4591 pj_assert(status == PJ_SUCCESS);
4592
4593 /* .. and register it to conference bridge (it would be slot1
4594 * if there's no other devices connected to the bridge)
4595 */
4596 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4597 pj_assert(status == PJ_SUCCESS);
4598
4599 /* Create sound device */
4600 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004601 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004602 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004603 2 * conf->info.samples_per_frame,
4604 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004605 0, &app_config.snd);
4606 pj_assert(status == PJ_SUCCESS);
4607
4608
4609 /* Connect the splitter to the sound device */
4610 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4611 pj_assert(status == PJ_SUCCESS);
4612
4613}
4614#endif
4615
Benny Prijono40860c32008-09-04 13:55:33 +00004616#ifdef TRANSPORT_ADAPTER_SAMPLE
4617static pj_status_t create_transport_adapter(pjmedia_endpt *med_endpt, int port,
4618 pjmedia_transport **p_tp)
4619{
4620 pjmedia_transport *udp;
4621 pj_status_t status;
4622
4623 /* Create the UDP media transport */
4624 status = pjmedia_transport_udp_create(med_endpt, NULL, port, 0, &udp);
4625 if (status != PJ_SUCCESS)
4626 return status;
4627
4628 /* Create the adapter */
4629 status = pjmedia_tp_adapter_create(med_endpt, NULL, udp, p_tp);
4630 if (status != PJ_SUCCESS) {
4631 pjmedia_transport_close(udp);
4632 return status;
4633 }
4634
4635 return PJ_SUCCESS;
4636}
4637
4638static pj_status_t transport_adapter_sample(void)
4639{
4640 pjsua_media_transport tp[PJSUA_MAX_CALLS];
4641 pj_status_t status;
4642 int port = 7000;
4643 unsigned i;
4644
4645 for (i=0; i<app_config.cfg.max_calls; ++i) {
4646 status = create_transport_adapter(pjsua_get_pjmedia_endpt(),
4647 port + i*10,
4648 &tp[i].transport);
4649 if (status != PJ_SUCCESS)
4650 return status;
4651 }
4652
4653 return pjsua_media_transports_attach(tp, i, PJ_TRUE);
4654}
4655#endif
4656