blob: 5414b0873445d3226bf7eff669aa2fdd8119c925 [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)");
Benny Prijono00cae612006-07-31 15:19:36 +0000247 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
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
1491 /* Outbound proxy */
1492 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1493 pj_ansi_sprintf(line, "--outbound %.*s\n",
1494 (int)config->cfg.outbound_proxy[i].slen,
1495 config->cfg.outbound_proxy[i].ptr);
1496 pj_strcat2(&cfg, line);
1497 }
1498
1499
1500 /* UDP Transport. */
1501 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1502 pj_strcat2(&cfg, line);
1503
Benny Prijono0a5cad82006-09-26 13:21:02 +00001504 /* IP address, if any. */
1505 if (config->udp_cfg.public_addr.slen) {
1506 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1507 (int)config->udp_cfg.public_addr.slen,
1508 config->udp_cfg.public_addr.ptr);
1509 pj_strcat2(&cfg, line);
1510 }
1511
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001512 /* No TCP ? */
1513 if (config->no_tcp) {
1514 pj_strcat2(&cfg, "--no-tcp\n");
1515 }
1516
1517 /* No UDP ? */
1518 if (config->no_udp) {
1519 pj_strcat2(&cfg, "--no-udp\n");
1520 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001521
1522 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001523 if (config->cfg.stun_domain.slen) {
1524 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1525 (int)config->cfg.stun_domain.slen,
1526 config->cfg.stun_domain.ptr);
1527 pj_strcat2(&cfg, line);
1528 }
1529 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001530 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001531 (int)config->cfg.stun_host.slen,
1532 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001533 pj_strcat2(&cfg, line);
1534 }
1535
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001536 /* TLS */
1537 if (config->use_tls)
1538 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001539 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001540 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001541 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1542 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001543 pj_strcat2(&cfg, line);
1544 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001545 if (config->udp_cfg.tls_setting.cert_file.slen) {
1546 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1547 (int)config->udp_cfg.tls_setting.cert_file.slen,
1548 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001549 pj_strcat2(&cfg, line);
1550 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001551 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1552 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1553 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1554 config->udp_cfg.tls_setting.privkey_file.ptr);
1555 pj_strcat2(&cfg, line);
1556 }
1557
1558 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001559 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001560 (int)config->udp_cfg.tls_setting.password.slen,
1561 config->udp_cfg.tls_setting.password.ptr);
1562 pj_strcat2(&cfg, line);
1563 }
1564
Benny Prijonoe10db842008-07-01 15:31:59 +00001565 if (config->udp_cfg.tls_setting.server_name.slen) {
1566 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1567 (int)config->udp_cfg.tls_setting.server_name.slen,
1568 config->udp_cfg.tls_setting.server_name.ptr);
1569 pj_strcat2(&cfg, line);
1570 }
1571
Benny Prijonof3bbc132006-12-25 06:43:59 +00001572 if (config->udp_cfg.tls_setting.verify_server)
1573 pj_strcat2(&cfg, "--tls-verify-server\n");
1574
1575 if (config->udp_cfg.tls_setting.verify_client)
1576 pj_strcat2(&cfg, "--tls-verify-client\n");
1577
1578 if (config->udp_cfg.tls_setting.timeout.sec) {
1579 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001580 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001581 pj_strcat2(&cfg, line);
1582 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001583
1584 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1585
Benny Prijonof6508982008-01-25 09:02:33 +00001586 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001587#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001588 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1589 pj_ansi_sprintf(line, "--use-srtp %d\n",
1590 app_config.cfg.use_srtp);
1591 pj_strcat2(&cfg, line);
1592 }
1593 if (app_config.cfg.srtp_secure_signaling !=
1594 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1595 {
1596 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1597 app_config.cfg.srtp_secure_signaling);
1598 pj_strcat2(&cfg, line);
1599 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001600#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001601
Benny Prijonof76e1392008-06-06 14:51:48 +00001602 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001603 if (config->media_cfg.enable_ice)
1604 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001605
1606 if (config->media_cfg.enable_turn)
1607 pj_strcat2(&cfg, "--use-turn\n");
1608
1609 if (config->media_cfg.ice_no_host_cands)
1610 pj_strcat2(&cfg, "--ice-no-host\n");
1611
Benny Prijono551af422008-08-07 09:55:52 +00001612 if (config->media_cfg.ice_no_rtcp)
1613 pj_strcat2(&cfg, "--ice-no-rtcp\n");
1614
Benny Prijonof76e1392008-06-06 14:51:48 +00001615 if (config->media_cfg.turn_server.slen) {
1616 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1617 (int)config->media_cfg.turn_server.slen,
1618 config->media_cfg.turn_server.ptr);
1619 pj_strcat2(&cfg, line);
1620 }
1621
1622 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1623 pj_strcat2(&cfg, "--turn-tcp\n");
1624
1625 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1626 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1627 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1628 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1629 pj_strcat2(&cfg, line);
1630 }
1631
1632 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1633 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1634 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1635 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1636 pj_strcat2(&cfg, line);
1637 }
1638
1639 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001640 if (config->null_audio)
1641 pj_strcat2(&cfg, "--null-audio\n");
1642 if (config->auto_play)
1643 pj_strcat2(&cfg, "--auto-play\n");
1644 if (config->auto_loop)
1645 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001646 if (config->auto_conf)
1647 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001648 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001649 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001650 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001651 pj_strcat2(&cfg, line);
1652 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001653 for (i=0; i<config->tone_count; ++i) {
1654 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1655 config->tones[i].freq1, config->tones[i].freq2,
1656 config->tones[i].on_msec, config->tones[i].off_msec);
1657 pj_strcat2(&cfg, line);
1658 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001659 if (config->rec_file.slen) {
1660 pj_ansi_sprintf(line, "--rec-file %s\n",
1661 config->rec_file.ptr);
1662 pj_strcat2(&cfg, line);
1663 }
1664 if (config->auto_rec)
1665 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001666 if (config->capture_dev != PJSUA_INVALID_ID) {
1667 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1668 pj_strcat2(&cfg, line);
1669 }
1670 if (config->playback_dev != PJSUA_INVALID_ID) {
1671 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1672 pj_strcat2(&cfg, line);
1673 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001674 if (config->media_cfg.snd_auto_close_time != -1) {
1675 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1676 config->media_cfg.snd_auto_close_time);
1677 pj_strcat2(&cfg, line);
1678 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001679 if (config->no_tones) {
1680 pj_strcat2(&cfg, "--no-tones\n");
1681 }
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001682 if (config->media_cfg.jb_max != -1) {
1683 pj_ansi_sprintf(line, "--jb-max-size %d\n",
1684 config->media_cfg.jb_max);
1685 pj_strcat2(&cfg, line);
1686 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001687
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001688 /* Sound device latency */
1689 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1690 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1691 pj_strcat2(&cfg, line);
1692 }
1693 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1694 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1695 pj_strcat2(&cfg, line);
1696 }
1697
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001698 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001699 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001700 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001701 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001702 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001703 } else {
1704 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1705 config->media_cfg.clock_rate);
1706 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001707 }
Benny Prijono70972992006-08-05 11:13:58 +00001708
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001709 if (config->media_cfg.snd_clock_rate &&
1710 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1711 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001712 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1713 config->media_cfg.snd_clock_rate);
1714 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001715 }
1716
Benny Prijono7d60d052008-03-29 12:24:20 +00001717 /* Stereo mode. */
1718 if (config->media_cfg.channel_count == 2) {
1719 pj_ansi_sprintf(line, "--stereo\n");
1720 pj_strcat2(&cfg, line);
1721 }
1722
Benny Prijono70972992006-08-05 11:13:58 +00001723 /* quality */
1724 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001725 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001726 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001727 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001728 } else {
1729 pj_ansi_sprintf(line, "#using default --quality %d\n",
1730 config->media_cfg.quality);
1731 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001732 }
Benny Prijono0498d902006-06-19 14:49:14 +00001733
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001734
1735 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001736 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001737 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001738 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001739 pj_strcat2(&cfg, line);
1740 }
1741
Benny Prijono70972992006-08-05 11:13:58 +00001742 /* no-vad */
1743 if (config->media_cfg.no_vad) {
1744 pj_strcat2(&cfg, "--no-vad\n");
1745 }
1746
1747 /* ec-tail */
1748 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1749 pj_ansi_sprintf(line, "--ec-tail %d\n",
1750 config->media_cfg.ec_tail_len);
1751 pj_strcat2(&cfg, line);
1752 } else {
1753 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1754 config->media_cfg.ec_tail_len);
1755 pj_strcat2(&cfg, line);
1756 }
1757
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001758 /* ec-opt */
1759 if (config->media_cfg.ec_options != 0) {
1760 pj_ansi_sprintf(line, "--ec-opt %d\n",
1761 config->media_cfg.ec_options);
1762 pj_strcat2(&cfg, line);
1763 }
Benny Prijono70972992006-08-05 11:13:58 +00001764
1765 /* ilbc-mode */
1766 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1767 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1768 config->media_cfg.ilbc_mode);
1769 pj_strcat2(&cfg, line);
1770 } else {
1771 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1772 config->media_cfg.ilbc_mode);
1773 pj_strcat2(&cfg, line);
1774 }
1775
1776 /* RTP drop */
1777 if (config->media_cfg.tx_drop_pct) {
1778 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1779 config->media_cfg.tx_drop_pct);
1780 pj_strcat2(&cfg, line);
1781
1782 }
1783 if (config->media_cfg.rx_drop_pct) {
1784 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1785 config->media_cfg.rx_drop_pct);
1786 pj_strcat2(&cfg, line);
1787
1788 }
1789
1790
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001791 /* Start RTP port. */
1792 pj_ansi_sprintf(line, "--rtp-port %d\n",
1793 config->rtp_cfg.port);
1794 pj_strcat2(&cfg, line);
1795
1796 /* Add codec. */
1797 for (i=0; i<config->codec_cnt; ++i) {
1798 pj_ansi_sprintf(line, "--add-codec %s\n",
1799 config->codec_arg[i].ptr);
1800 pj_strcat2(&cfg, line);
1801 }
Benny Prijonofce28542007-12-09 15:41:10 +00001802 /* Disable codec */
1803 for (i=0; i<config->codec_dis_cnt; ++i) {
1804 pj_ansi_sprintf(line, "--dis-codec %s\n",
1805 config->codec_dis[i].ptr);
1806 pj_strcat2(&cfg, line);
1807 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001808
1809 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1810
1811 /* Auto-answer. */
1812 if (config->auto_answer != 0) {
1813 pj_ansi_sprintf(line, "--auto-answer %d\n",
1814 config->auto_answer);
1815 pj_strcat2(&cfg, line);
1816 }
1817
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001818 /* accept-redirect */
1819 if (config->redir_op != PJSIP_REDIRECT_ACCEPT) {
1820 pj_ansi_sprintf(line, "--accept-redirect %d\n",
1821 config->redir_op);
1822 pj_strcat2(&cfg, line);
1823 }
1824
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001825 /* Max calls. */
1826 pj_ansi_sprintf(line, "--max-calls %d\n",
1827 config->cfg.max_calls);
1828 pj_strcat2(&cfg, line);
1829
1830 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001831 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001832 pj_ansi_sprintf(line, "--duration %d\n",
1833 config->duration);
1834 pj_strcat2(&cfg, line);
1835 }
1836
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001837 /* norefersub ? */
1838 if (config->no_refersub) {
1839 pj_strcat2(&cfg, "--norefersub\n");
1840 }
1841
Benny Prijonofce28542007-12-09 15:41:10 +00001842 if (pjsip_use_compact_form)
1843 {
1844 pj_strcat2(&cfg, "--use-compact-form\n");
1845 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001846
Benny Prijono91d06b62008-09-20 12:16:56 +00001847 if (config->cfg.force_lr) {
1848 pj_strcat2(&cfg, "--no-force-lr\n");
1849 }
1850
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001851 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1852
1853 /* Add buddies. */
1854 for (i=0; i<config->buddy_cnt; ++i) {
1855 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1856 (int)config->buddy_cfg[i].uri.slen,
1857 config->buddy_cfg[i].uri.ptr);
1858 pj_strcat2(&cfg, line);
1859 }
1860
1861
1862 *(cfg.ptr + cfg.slen) = '\0';
1863 return cfg.slen;
1864}
1865
1866
1867/*
1868 * Dump application states.
1869 */
1870static void app_dump(pj_bool_t detail)
1871{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001872 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001873}
1874
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001875/*
1876 * Print log of call states. Since call states may be too long for logger,
1877 * printing it is a bit tricky, it should be printed part by part as long
1878 * as the logger can accept.
1879 */
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001880static void log_call_dump(int call_id)
1881{
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001882 unsigned call_dump_len;
1883 unsigned part_len;
1884 unsigned part_idx;
1885 unsigned log_decor;
1886
1887 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1888 sizeof(some_buf), " ");
1889 call_dump_len = strlen(some_buf);
1890
1891 log_decor = pj_log_get_decor();
1892 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1893 PJ_LOG(3,(THIS_FILE, "\n"));
1894 pj_log_set_decor(0);
1895
1896 part_idx = 0;
1897 part_len = PJ_LOG_MAX_SIZE-80;
1898 while (part_idx < call_dump_len) {
1899 char p_orig, *p;
1900
1901 p = &some_buf[part_idx];
1902 if (part_idx + part_len > call_dump_len)
1903 part_len = call_dump_len - part_idx;
1904 p_orig = p[part_len];
1905 p[part_len] = '\0';
1906 PJ_LOG(3,(THIS_FILE, "%s", p));
1907 p[part_len] = p_orig;
1908 part_idx += part_len;
1909 }
1910 pj_log_set_decor(log_decor);
1911}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001912
1913/*****************************************************************************
1914 * Console application
1915 */
1916
Benny Prijono91d20f42008-06-14 19:42:37 +00001917static void ringback_start(pjsua_call_id call_id)
1918{
1919 if (app_config.no_tones)
1920 return;
1921
1922 if (app_config.call_data[call_id].ringback_on)
1923 return;
1924
1925 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1926
1927 if (++app_config.ringback_cnt==1 &&
1928 app_config.ringback_slot!=PJSUA_INVALID_ID)
1929 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001930 pjsua_conf_connect(app_config.ringback_slot, 0);
1931 }
1932}
1933
1934static void ring_stop(pjsua_call_id call_id)
1935{
1936 if (app_config.no_tones)
1937 return;
1938
1939 if (app_config.call_data[call_id].ringback_on) {
1940 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1941
1942 pj_assert(app_config.ringback_cnt>0);
1943 if (--app_config.ringback_cnt == 0 &&
1944 app_config.ringback_slot!=PJSUA_INVALID_ID)
1945 {
1946 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001947 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001948 }
1949 }
1950
1951 if (app_config.call_data[call_id].ring_on) {
1952 app_config.call_data[call_id].ring_on = PJ_FALSE;
1953
1954 pj_assert(app_config.ring_cnt>0);
1955 if (--app_config.ring_cnt == 0 &&
1956 app_config.ring_slot!=PJSUA_INVALID_ID)
1957 {
1958 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001959 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001960 }
1961 }
1962}
1963
1964static void ring_start(pjsua_call_id call_id)
1965{
1966 if (app_config.no_tones)
1967 return;
1968
1969 if (app_config.call_data[call_id].ring_on)
1970 return;
1971
1972 app_config.call_data[call_id].ring_on = PJ_TRUE;
1973
1974 if (++app_config.ring_cnt==1 &&
1975 app_config.ring_slot!=PJSUA_INVALID_ID)
1976 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001977 pjsua_conf_connect(app_config.ring_slot, 0);
1978 }
1979}
1980
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001981/*
1982 * Find next call when current call is disconnected or when user
1983 * press ']'
1984 */
1985static pj_bool_t find_next_call(void)
1986{
1987 int i, max;
1988
1989 max = pjsua_call_get_max_count();
1990 for (i=current_call+1; i<max; ++i) {
1991 if (pjsua_call_is_active(i)) {
1992 current_call = i;
1993 return PJ_TRUE;
1994 }
1995 }
1996
1997 for (i=0; i<current_call; ++i) {
1998 if (pjsua_call_is_active(i)) {
1999 current_call = i;
2000 return PJ_TRUE;
2001 }
2002 }
2003
2004 current_call = PJSUA_INVALID_ID;
2005 return PJ_FALSE;
2006}
2007
2008
2009/*
2010 * Find previous call when user press '['
2011 */
2012static pj_bool_t find_prev_call(void)
2013{
2014 int i, max;
2015
2016 max = pjsua_call_get_max_count();
2017 for (i=current_call-1; i>=0; --i) {
2018 if (pjsua_call_is_active(i)) {
2019 current_call = i;
2020 return PJ_TRUE;
2021 }
2022 }
2023
2024 for (i=max-1; i>current_call; --i) {
2025 if (pjsua_call_is_active(i)) {
2026 current_call = i;
2027 return PJ_TRUE;
2028 }
2029 }
2030
2031 current_call = PJSUA_INVALID_ID;
2032 return PJ_FALSE;
2033}
2034
2035
Benny Prijono804ff0a2006-09-14 11:17:48 +00002036/* Callback from timer when the maximum call duration has been
2037 * exceeded.
2038 */
2039static void call_timeout_callback(pj_timer_heap_t *timer_heap,
2040 struct pj_timer_entry *entry)
2041{
2042 pjsua_call_id call_id = entry->id;
2043 pjsua_msg_data msg_data;
2044 pjsip_generic_string_hdr warn;
2045 pj_str_t hname = pj_str("Warning");
2046 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
2047
2048 PJ_UNUSED_ARG(timer_heap);
2049
Benny Prijono148c9dd2006-09-19 13:37:53 +00002050 if (call_id == PJSUA_INVALID_ID) {
2051 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
2052 return;
2053 }
2054
Benny Prijono804ff0a2006-09-14 11:17:48 +00002055 /* Add warning header */
2056 pjsua_msg_data_init(&msg_data);
2057 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
2058 pj_list_push_back(&msg_data.hdr_list, &warn);
2059
2060 /* Call duration has been exceeded; disconnect the call */
2061 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
2062 "for call %d, disconnecting the call",
2063 app_config.duration, call_id));
2064 entry->id = PJSUA_INVALID_ID;
2065 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
2066}
2067
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002068
2069/*
2070 * Handler when invite state has changed.
2071 */
2072static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
2073{
2074 pjsua_call_info call_info;
2075
2076 PJ_UNUSED_ARG(e);
2077
2078 pjsua_call_get_info(call_id, &call_info);
2079
2080 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
2081
Benny Prijono91d20f42008-06-14 19:42:37 +00002082 /* Stop all ringback for this call */
2083 ring_stop(call_id);
2084
Benny Prijono804ff0a2006-09-14 11:17:48 +00002085 /* Cancel duration timer, if any */
2086 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
2087 struct call_data *cd = &app_config.call_data[call_id];
2088 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2089
2090 cd->timer.id = PJSUA_INVALID_ID;
2091 pjsip_endpt_cancel_timer(endpt, &cd->timer);
2092 }
2093
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002094 /* Rewind play file when hangup automatically,
2095 * since file is not looped
2096 */
2097 if (app_config.auto_play_hangup)
2098 pjsua_player_set_pos(app_config.wav_id, 0);
2099
2100
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002101 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
2102 call_id,
2103 call_info.last_status,
2104 call_info.last_status_text.ptr));
2105
2106 if (call_id == current_call) {
2107 find_next_call();
2108 }
2109
Benny Prijono4be63b52006-11-25 14:50:25 +00002110 /* Dump media state upon disconnected */
2111 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002112 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002113 "Call %d disconnected, dumping media stats..",
2114 call_id));
2115 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002116 }
2117
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002118 } else {
2119
Benny Prijono804ff0a2006-09-14 11:17:48 +00002120 if (app_config.duration!=NO_LIMIT &&
2121 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2122 {
2123 /* Schedule timer to hangup call after the specified duration */
2124 struct call_data *cd = &app_config.call_data[call_id];
2125 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2126 pj_time_val delay;
2127
2128 cd->timer.id = call_id;
2129 delay.sec = app_config.duration;
2130 delay.msec = 0;
2131 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2132 }
2133
Benny Prijono4be63b52006-11-25 14:50:25 +00002134 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2135 int code;
2136 pj_str_t reason;
2137 pjsip_msg *msg;
2138
2139 /* This can only occur because of TX or RX message */
2140 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2141
2142 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2143 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2144 } else {
2145 msg = e->body.tsx_state.src.tdata->msg;
2146 }
2147
2148 code = msg->line.status.code;
2149 reason = msg->line.status.reason;
2150
Benny Prijono91d20f42008-06-14 19:42:37 +00002151 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2152 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2153 msg->body == NULL &&
2154 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2155 {
2156 ringback_start(call_id);
2157 }
2158
Benny Prijono4be63b52006-11-25 14:50:25 +00002159 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2160 call_id, call_info.state_text.ptr,
2161 code, (int)reason.slen, reason.ptr));
2162 } else {
2163 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2164 call_id,
2165 call_info.state_text.ptr));
2166 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002167
2168 if (current_call==PJSUA_INVALID_ID)
2169 current_call = call_id;
2170
2171 }
2172}
2173
2174
2175/**
2176 * Handler when there is incoming call.
2177 */
2178static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2179 pjsip_rx_data *rdata)
2180{
2181 pjsua_call_info call_info;
2182
2183 PJ_UNUSED_ARG(acc_id);
2184 PJ_UNUSED_ARG(rdata);
2185
2186 pjsua_call_get_info(call_id, &call_info);
2187
Benny Prijono91d20f42008-06-14 19:42:37 +00002188 /* Start ringback */
2189 ring_start(call_id);
2190
Benny Prijono2285e7e2008-12-17 14:28:18 +00002191 if (current_call==PJSUA_INVALID_ID)
2192 current_call = call_id;
2193
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002194 if (app_config.auto_answer > 0) {
2195 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2196 }
2197
2198 if (app_config.auto_answer < 200) {
2199 PJ_LOG(3,(THIS_FILE,
2200 "Incoming call for account %d!\n"
2201 "From: %s\n"
2202 "To: %s\n"
2203 "Press a to answer or h to reject call",
2204 acc_id,
2205 call_info.remote_info.ptr,
2206 call_info.local_info.ptr));
2207 }
2208}
2209
2210
2211/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002212 * Handler when a transaction within a call has changed state.
2213 */
2214static void on_call_tsx_state(pjsua_call_id call_id,
2215 pjsip_transaction *tsx,
2216 pjsip_event *e)
2217{
2218 const pjsip_method info_method =
2219 {
2220 PJSIP_OTHER_METHOD,
2221 { "INFO", 4 }
2222 };
2223
2224 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2225 /*
2226 * Handle INFO method.
2227 */
2228 if (tsx->role == PJSIP_ROLE_UAC &&
2229 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002230 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2231 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002232 {
2233 /* Status of outgoing INFO request */
2234 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2235 PJ_LOG(4,(THIS_FILE,
2236 "Call %d: DTMF sent successfully with INFO",
2237 call_id));
2238 } else if (tsx->status_code >= 300) {
2239 PJ_LOG(4,(THIS_FILE,
2240 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2241 call_id,
2242 tsx->status_code,
2243 (int)tsx->status_text.slen,
2244 tsx->status_text.ptr));
2245 }
2246 } else if (tsx->role == PJSIP_ROLE_UAS &&
2247 tsx->state == PJSIP_TSX_STATE_TRYING)
2248 {
2249 /* Answer incoming INFO with 200/OK */
2250 pjsip_rx_data *rdata;
2251 pjsip_tx_data *tdata;
2252 pj_status_t status;
2253
2254 rdata = e->body.tsx_state.src.rdata;
2255
2256 if (rdata->msg_info.msg->body) {
2257 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2258 200, NULL, &tdata);
2259 if (status == PJ_SUCCESS)
2260 status = pjsip_tsx_send_msg(tsx, tdata);
2261
2262 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2263 call_id,
2264 (int)rdata->msg_info.msg->body->len,
2265 rdata->msg_info.msg->body->data));
2266 } else {
2267 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2268 400, NULL, &tdata);
2269 if (status == PJ_SUCCESS)
2270 status = pjsip_tsx_send_msg(tsx, tdata);
2271 }
2272 }
2273 }
2274}
2275
2276
2277/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002278 * Callback on media state changed event.
2279 * The action may connect the call to sound device, to file, or
2280 * to loop the call.
2281 */
2282static void on_call_media_state(pjsua_call_id call_id)
2283{
2284 pjsua_call_info call_info;
2285
2286 pjsua_call_get_info(call_id, &call_info);
2287
Benny Prijono91d20f42008-06-14 19:42:37 +00002288 /* Stop ringback */
2289 ring_stop(call_id);
2290
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002291 /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
2292 * otherwise we should NOT connect the ports.
2293 */
2294 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE ||
2295 call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
2296 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002297 pj_bool_t connect_sound = PJ_TRUE;
2298
2299 /* Loopback sound, if desired */
2300 if (app_config.auto_loop) {
2301 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2302 connect_sound = PJ_FALSE;
Benny Prijonof6c77f42008-08-13 13:52:19 +00002303 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00002304
Benny Prijonof6c77f42008-08-13 13:52:19 +00002305 /* Automatically record conversation, if desired */
2306 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2307 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002308 }
2309
2310 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002311 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2312 app_config.wav_port != PJSUA_INVALID_ID)
2313 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002314 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2315 connect_sound = PJ_FALSE;
2316 }
2317
Benny Prijono7ca96da2006-08-07 12:11:40 +00002318 /* Put call in conference with other calls, if desired */
2319 if (app_config.auto_conf) {
2320 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002321 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002322 unsigned i;
2323
2324 /* Get all calls, and establish media connection between
2325 * this call and other calls.
2326 */
2327 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002328
Benny Prijono7ca96da2006-08-07 12:11:40 +00002329 for (i=0; i<call_cnt; ++i) {
2330 if (call_ids[i] == call_id)
2331 continue;
2332
2333 if (!pjsua_call_has_media(call_ids[i]))
2334 continue;
2335
2336 pjsua_conf_connect(call_info.conf_slot,
2337 pjsua_call_get_conf_port(call_ids[i]));
2338 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2339 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002340
2341 /* Automatically record conversation, if desired */
2342 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2343 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2344 app_config.rec_port);
2345 }
2346
Benny Prijono7ca96da2006-08-07 12:11:40 +00002347 }
2348
2349 /* Also connect call to local sound device */
2350 connect_sound = PJ_TRUE;
2351 }
2352
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002353 /* Otherwise connect to sound device */
2354 if (connect_sound) {
2355 pjsua_conf_connect(call_info.conf_slot, 0);
2356 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002357
2358 /* Automatically record conversation, if desired */
2359 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2360 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2361 pjsua_conf_connect(0, app_config.rec_port);
2362 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002363 }
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002364 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002365
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002366 /* Handle media status */
2367 switch (call_info.media_status) {
2368 case PJSUA_CALL_MEDIA_ACTIVE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002369 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002370 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002371
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002372 case PJSUA_CALL_MEDIA_LOCAL_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002373 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2374 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002375 break;
2376
2377 case PJSUA_CALL_MEDIA_REMOTE_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002378 PJ_LOG(3,(THIS_FILE,
2379 "Media for call %d is suspended (hold) by remote",
2380 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002381 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002382
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002383 case PJSUA_CALL_MEDIA_ERROR:
2384 PJ_LOG(3,(THIS_FILE,
Benny Prijono096c56c2007-09-15 08:30:16 +00002385 "Media has reported error, disconnecting call"));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002386 {
2387 pj_str_t reason = pj_str("ICE negotiation failed");
2388 pjsua_call_hangup(call_id, 500, &reason, NULL);
2389 }
2390 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002391
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002392 case PJSUA_CALL_MEDIA_NONE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002393 PJ_LOG(3,(THIS_FILE,
2394 "Media for call %d is inactive",
2395 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002396 break;
2397
2398 default:
2399 pj_assert(!"Unhandled media status");
2400 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002401 }
2402}
2403
Benny Prijono0875ae82006-12-26 00:11:48 +00002404/*
2405 * DTMF callback.
2406 */
2407static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2408{
2409 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2410}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002411
2412/*
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002413 * Redirection handler.
2414 */
Benny Prijono08a48b82008-11-27 12:42:07 +00002415static pjsip_redirect_op call_on_redirected(pjsua_call_id call_id,
2416 const pjsip_uri *target,
2417 const pjsip_event *e)
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002418{
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002419 PJ_UNUSED_ARG(e);
2420
Benny Prijono08a48b82008-11-27 12:42:07 +00002421 if (app_config.redir_op == PJSIP_REDIRECT_PENDING) {
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002422 char uristr[PJSIP_MAX_URL_SIZE];
2423 int len;
2424
2425 len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, target, uristr,
2426 sizeof(uristr));
2427 if (len < 1) {
2428 pj_ansi_strcpy(uristr, "--URI too long--");
2429 }
2430
2431 PJ_LOG(3,(THIS_FILE, "Call %d is being redirected to %.*s. "
2432 "Press 'Ra' to accept, 'Rr' to reject, or 'Rd' to "
2433 "disconnect.",
2434 call_id, len, uristr));
2435 }
Benny Prijono08a48b82008-11-27 12:42:07 +00002436
2437 return app_config.redir_op;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002438}
2439
2440/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002441 * Handler registration status has changed.
2442 */
2443static void on_reg_state(pjsua_acc_id acc_id)
2444{
2445 PJ_UNUSED_ARG(acc_id);
2446
2447 // Log already written.
2448}
2449
2450
2451/*
Benny Prijono563cd7a2008-08-19 20:02:15 +00002452 * Handler for incoming presence subscription request
2453 */
2454static void on_incoming_subscribe(pjsua_acc_id acc_id,
2455 pjsua_srv_pres *srv_pres,
2456 pjsua_buddy_id buddy_id,
2457 const pj_str_t *from,
2458 pjsip_rx_data *rdata,
2459 pjsip_status_code *code,
2460 pj_str_t *reason,
2461 pjsua_msg_data *msg_data)
2462{
2463 /* Just accept the request (the default behavior) */
2464 PJ_UNUSED_ARG(acc_id);
2465 PJ_UNUSED_ARG(srv_pres);
2466 PJ_UNUSED_ARG(buddy_id);
2467 PJ_UNUSED_ARG(from);
2468 PJ_UNUSED_ARG(rdata);
2469 PJ_UNUSED_ARG(code);
2470 PJ_UNUSED_ARG(reason);
2471 PJ_UNUSED_ARG(msg_data);
2472}
2473
2474
2475/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002476 * Handler on buddy state changed.
2477 */
2478static void on_buddy_state(pjsua_buddy_id buddy_id)
2479{
2480 pjsua_buddy_info info;
2481 pjsua_buddy_get_info(buddy_id, &info);
2482
2483 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2484 (int)info.uri.slen,
2485 info.uri.ptr,
2486 (int)info.status_text.slen,
2487 info.status_text.ptr));
2488}
2489
2490
2491/**
2492 * Incoming IM message (i.e. MESSAGE request)!
2493 */
2494static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2495 const pj_str_t *to, const pj_str_t *contact,
2496 const pj_str_t *mime_type, const pj_str_t *text)
2497{
2498 /* Note: call index may be -1 */
2499 PJ_UNUSED_ARG(call_id);
2500 PJ_UNUSED_ARG(to);
2501 PJ_UNUSED_ARG(contact);
2502 PJ_UNUSED_ARG(mime_type);
2503
Benny Prijonof4b538d2007-05-14 16:45:20 +00002504 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002505 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002506 (int)text->slen, text->ptr,
2507 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002508}
2509
2510
2511/**
2512 * Received typing indication
2513 */
2514static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2515 const pj_str_t *to, const pj_str_t *contact,
2516 pj_bool_t is_typing)
2517{
2518 PJ_UNUSED_ARG(call_id);
2519 PJ_UNUSED_ARG(to);
2520 PJ_UNUSED_ARG(contact);
2521
2522 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2523 (int)from->slen, from->ptr,
2524 (is_typing?"is typing..":"has stopped typing")));
2525}
2526
2527
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002528/**
2529 * Call transfer request status.
2530 */
2531static void on_call_transfer_status(pjsua_call_id call_id,
2532 int status_code,
2533 const pj_str_t *status_text,
2534 pj_bool_t final,
2535 pj_bool_t *p_cont)
2536{
2537 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2538 call_id, status_code,
2539 (int)status_text->slen, status_text->ptr,
2540 (final ? "[final]" : "")));
2541
2542 if (status_code/100 == 2) {
2543 PJ_LOG(3,(THIS_FILE,
2544 "Call %d: call transfered successfully, disconnecting call",
2545 call_id));
2546 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2547 *p_cont = PJ_FALSE;
2548 }
2549}
2550
2551
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002552/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002553 * Notification that call is being replaced.
2554 */
2555static void on_call_replaced(pjsua_call_id old_call_id,
2556 pjsua_call_id new_call_id)
2557{
2558 pjsua_call_info old_ci, new_ci;
2559
2560 pjsua_call_get_info(old_call_id, &old_ci);
2561 pjsua_call_get_info(new_call_id, &new_ci);
2562
2563 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2564 "call %d with %.*s",
2565 old_call_id,
2566 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2567 new_call_id,
2568 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2569}
2570
2571
2572/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002573 * NAT type detection callback.
2574 */
2575static void on_nat_detect(const pj_stun_nat_detect_result *res)
2576{
2577 if (res->status != PJ_SUCCESS) {
2578 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2579 } else {
2580 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2581 }
2582}
2583
2584
2585/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002586 * Print buddy list.
2587 */
2588static void print_buddy_list(void)
2589{
2590 pjsua_buddy_id ids[64];
2591 int i;
2592 unsigned count = PJ_ARRAY_SIZE(ids);
2593
2594 puts("Buddy list:");
2595
2596 pjsua_enum_buddies(ids, &count);
2597
2598 if (count == 0)
2599 puts(" -none-");
2600 else {
2601 for (i=0; i<(int)count; ++i) {
2602 pjsua_buddy_info info;
2603
2604 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2605 continue;
2606
Benny Prijono4461c7d2007-08-25 13:36:15 +00002607 printf(" [%2d] <%.*s> %.*s\n",
2608 ids[i]+1,
2609 (int)info.status_text.slen,
2610 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002611 (int)info.uri.slen,
2612 info.uri.ptr);
2613 }
2614 }
2615 puts("");
2616}
2617
2618
2619/*
2620 * Print account status.
2621 */
2622static void print_acc_status(int acc_id)
2623{
2624 char buf[80];
2625 pjsua_acc_info info;
2626
2627 pjsua_acc_get_info(acc_id, &info);
2628
2629 if (!info.has_registration) {
2630 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2631 (int)info.status_text.slen,
2632 info.status_text.ptr);
2633
2634 } else {
2635 pj_ansi_snprintf(buf, sizeof(buf),
2636 "%d/%.*s (expires=%d)",
2637 info.status,
2638 (int)info.status_text.slen,
2639 info.status_text.ptr,
2640 info.expires);
2641
2642 }
2643
2644 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2645 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002646 printf(" Online status: %.*s\n",
2647 (int)info.online_status_text.slen,
2648 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002649}
2650
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002651/* Playfile done notification, set timer to hangup calls */
2652pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2653{
2654 pj_time_val delay;
2655
2656 PJ_UNUSED_ARG(port);
2657 PJ_UNUSED_ARG(usr_data);
2658
2659 /* Just rewind WAV when it is played outside of call */
2660 if (pjsua_call_get_count() == 0) {
2661 pjsua_player_set_pos(app_config.wav_id, 0);
2662 return PJ_SUCCESS;
2663 }
2664
2665 /* Timer is already active */
2666 if (app_config.auto_hangup_timer.id == 1)
2667 return PJ_SUCCESS;
2668
2669 app_config.auto_hangup_timer.id = 1;
2670 delay.sec = 0;
2671 delay.msec = 200; /* Give 200 ms before hangup */
2672 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2673 &app_config.auto_hangup_timer,
2674 &delay);
2675
2676 return PJ_SUCCESS;
2677}
2678
2679/* Auto hangup timer callback */
2680static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2681 struct pj_timer_entry *entry)
2682{
2683 PJ_UNUSED_ARG(timer_heap);
2684 PJ_UNUSED_ARG(entry);
2685
2686 app_config.auto_hangup_timer.id = 0;
2687 pjsua_call_hangup_all();
2688}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002689
2690/*
2691 * Show a bit of help.
2692 */
2693static void keystroke_help(void)
2694{
2695 pjsua_acc_id acc_ids[16];
2696 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2697 int i;
2698
2699 printf(">>>>\n");
2700
2701 pjsua_enum_accs(acc_ids, &count);
2702
2703 printf("Account list:\n");
2704 for (i=0; i<(int)count; ++i)
2705 print_acc_status(acc_ids[i]);
2706
2707 print_buddy_list();
2708
2709 //puts("Commands:");
2710 puts("+=============================================================================+");
2711 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2712 puts("| | | |");
2713 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2714 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002715 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2716 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2717 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2718 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002719 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2720 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002721 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002722 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002723 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2724 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2725 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2726 puts("| | V Adjust audio Volume | f Save config |");
2727 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002728 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002729 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002730 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002731
2732 i = pjsua_call_get_count();
2733 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002734
2735 if (current_call != PJSUA_INVALID_ID) {
2736 pjsua_call_info ci;
2737 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2738 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2739 (int)ci.remote_info.slen, ci.remote_info.ptr,
2740 (int)ci.state_text.slen, ci.state_text.ptr);
2741 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002742}
2743
2744
2745/*
2746 * Input simple string
2747 */
2748static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2749{
2750 char *p;
2751
2752 printf("%s (empty to cancel): ", title); fflush(stdout);
Benny Prijono32d267b2009-01-01 22:08:21 +00002753 if (fgets(buf, len, stdin) == NULL)
2754 return PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002755
2756 /* Remove trailing newlines. */
2757 for (p=buf; ; ++p) {
2758 if (*p=='\r' || *p=='\n') *p='\0';
2759 else if (!*p) break;
2760 }
2761
2762 if (!*buf)
2763 return PJ_FALSE;
2764
2765 return PJ_TRUE;
2766}
2767
2768
2769#define NO_NB -2
2770struct input_result
2771{
2772 int nb_result;
2773 char *uri_result;
2774};
2775
2776
2777/*
2778 * Input URL.
2779 */
2780static void ui_input_url(const char *title, char *buf, int len,
2781 struct input_result *result)
2782{
2783 result->nb_result = NO_NB;
2784 result->uri_result = NULL;
2785
2786 print_buddy_list();
2787
2788 printf("Choices:\n"
2789 " 0 For current dialog.\n"
2790 " -1 All %d buddies in buddy list\n"
2791 " [1 -%2d] Select from buddy list\n"
2792 " URL An URL\n"
2793 " <Enter> Empty input (or 'q') to cancel\n"
2794 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2795 printf("%s: ", title);
2796
2797 fflush(stdout);
Benny Prijono32d267b2009-01-01 22:08:21 +00002798 if (fgets(buf, len, stdin) == NULL)
2799 return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002800 len = strlen(buf);
2801
2802 /* Left trim */
2803 while (pj_isspace(*buf)) {
2804 ++buf;
2805 --len;
2806 }
2807
2808 /* Remove trailing newlines */
2809 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2810 buf[--len] = '\0';
2811
2812 if (len == 0 || buf[0]=='q')
2813 return;
2814
2815 if (pj_isdigit(*buf) || *buf=='-') {
2816
2817 int i;
2818
2819 if (*buf=='-')
2820 i = 1;
2821 else
2822 i = 0;
2823
2824 for (; i<len; ++i) {
2825 if (!pj_isdigit(buf[i])) {
2826 puts("Invalid input");
2827 return;
2828 }
2829 }
2830
2831 result->nb_result = my_atoi(buf);
2832
2833 if (result->nb_result >= 0 &&
2834 result->nb_result <= (int)pjsua_get_buddy_count())
2835 {
2836 return;
2837 }
2838 if (result->nb_result == -1)
2839 return;
2840
2841 puts("Invalid input");
2842 result->nb_result = NO_NB;
2843 return;
2844
2845 } else {
2846 pj_status_t status;
2847
2848 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2849 pjsua_perror(THIS_FILE, "Invalid URL", status);
2850 return;
2851 }
2852
2853 result->uri_result = buf;
2854 }
2855}
2856
2857/*
2858 * List the ports in conference bridge
2859 */
2860static void conf_list(void)
2861{
2862 unsigned i, count;
2863 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2864
2865 printf("Conference ports:\n");
2866
2867 count = PJ_ARRAY_SIZE(id);
2868 pjsua_enum_conf_ports(id, &count);
2869
2870 for (i=0; i<count; ++i) {
2871 char txlist[PJSUA_MAX_CALLS*4+10];
2872 unsigned j;
2873 pjsua_conf_port_info info;
2874
2875 pjsua_conf_get_port_info(id[i], &info);
2876
2877 txlist[0] = '\0';
2878 for (j=0; j<info.listener_cnt; ++j) {
2879 char s[10];
2880 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2881 pj_ansi_strcat(txlist, s);
2882 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002883 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002884 info.slot_id,
2885 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002886 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002887 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002888 (int)info.name.slen,
2889 info.name.ptr,
2890 txlist);
2891
2892 }
2893 puts("");
2894}
2895
2896
2897/*
Benny Prijono56315612006-07-18 14:39:40 +00002898 * Send arbitrary request to remote host
2899 */
2900static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2901{
2902 pj_str_t str_method;
2903 pjsip_method method;
2904 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002905 pjsip_endpoint *endpt;
2906 pj_status_t status;
2907
2908 endpt = pjsua_get_pjsip_endpt();
2909
2910 str_method = pj_str(cstr_method);
2911 pjsip_method_init_np(&method, &str_method);
2912
Benny Prijonofff245c2007-04-02 11:44:47 +00002913 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002914
Benny Prijonob988d762007-12-05 04:30:04 +00002915 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002916 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002917 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002918 return;
2919 }
2920}
2921
2922
2923/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002924 * Change extended online status.
2925 */
2926static void change_online_status(void)
2927{
2928 char menuin[32];
2929 pj_bool_t online_status;
2930 pjrpid_element elem;
2931 int i, choice;
2932
2933 enum {
2934 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2935 };
2936
2937 struct opt {
2938 int id;
2939 char *name;
2940 } opts[] = {
2941 { AVAILABLE, "Available" },
2942 { BUSY, "Busy"},
2943 { OTP, "On the phone"},
2944 { IDLE, "Idle"},
2945 { AWAY, "Away"},
2946 { BRB, "Be right back"},
2947 { OFFLINE, "Offline"}
2948 };
2949
2950 printf("\n"
2951 "Choices:\n");
2952 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2953 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2954 }
2955
2956 if (!simple_input("Select status", menuin, sizeof(menuin)))
2957 return;
2958
2959 choice = atoi(menuin) - 1;
2960 if (choice < 0 || choice >= OPT_MAX) {
2961 puts("Invalid selection");
2962 return;
2963 }
2964
2965 pj_bzero(&elem, sizeof(elem));
2966 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2967
2968 online_status = PJ_TRUE;
2969
2970 switch (choice) {
2971 case AVAILABLE:
2972 break;
2973 case BUSY:
2974 elem.activity = PJRPID_ACTIVITY_BUSY;
2975 elem.note = pj_str("Busy");
2976 break;
2977 case OTP:
2978 elem.activity = PJRPID_ACTIVITY_BUSY;
2979 elem.note = pj_str("On the phone");
2980 break;
2981 case IDLE:
2982 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2983 elem.note = pj_str("Idle");
2984 break;
2985 case AWAY:
2986 elem.activity = PJRPID_ACTIVITY_AWAY;
2987 elem.note = pj_str("Away");
2988 break;
2989 case BRB:
2990 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2991 elem.note = pj_str("Be right back");
2992 break;
2993 case OFFLINE:
2994 online_status = PJ_FALSE;
2995 break;
2996 }
2997
2998 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2999}
3000
3001
3002/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00003003 * Change codec priorities.
3004 */
3005static void manage_codec_prio(void)
3006{
3007 pjsua_codec_info c[32];
3008 unsigned i, count = PJ_ARRAY_SIZE(c);
3009 char input[32];
3010 char *codec, *prio;
3011 pj_str_t id;
3012 int new_prio;
3013 pj_status_t status;
3014
3015 printf("List of codecs:\n");
3016
3017 pjsua_enum_codecs(c, &count);
3018 for (i=0; i<count; ++i) {
3019 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
3020 c[i].codec_id.ptr);
3021 }
3022
3023 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00003024 puts("Enter codec id and its new priority "
3025 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00003026
Benny Prijono88accae2008-06-26 15:48:14 +00003027 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijono32d267b2009-01-01 22:08:21 +00003028 if (fgets(input, sizeof(input), stdin) == NULL)
3029 return;
Benny Prijonoc08682e2007-10-04 06:17:58 +00003030 if (input[0]=='\r' || input[0]=='\n') {
3031 puts("Done");
3032 return;
3033 }
3034
3035 codec = strtok(input, " \t\r\n");
3036 prio = strtok(NULL, " \r\n");
3037
3038 if (!codec || !prio) {
3039 puts("Invalid input");
3040 return;
3041 }
3042
3043 new_prio = atoi(prio);
3044 if (new_prio < 0)
3045 new_prio = 0;
3046 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
3047 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
3048
3049 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
3050 (pj_uint8_t)new_prio);
3051 if (status != PJ_SUCCESS)
3052 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
3053}
3054
3055
3056/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003057 * Main "user interface" loop.
3058 */
3059void console_app_main(const pj_str_t *uri_to_call)
3060{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003061 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003062 char buf[128];
3063 char text[128];
3064 int i, count;
3065 char *uri;
3066 pj_str_t tmp;
3067 struct input_result result;
3068 pjsua_call_info call_info;
3069 pjsua_acc_info acc_info;
3070
3071
3072 /* If user specifies URI to call, then call the URI */
3073 if (uri_to_call->slen) {
3074 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
3075 }
3076
3077 keystroke_help();
3078
3079 for (;;) {
3080
3081 printf(">>> ");
3082 fflush(stdout);
3083
Benny Prijono990042e2007-01-21 19:36:00 +00003084 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
3085 /*
3086 * Be friendly to users who redirect commands into
3087 * program, when file ends, resume with kbd.
3088 * If exit is desired end script with q for quit
3089 */
3090 /* Reopen stdin/stdout/stderr to /dev/console */
3091#if defined(PJ_WIN32) && PJ_WIN32!=0
3092 if (freopen ("CONIN$", "r", stdin) == NULL) {
3093#else
3094 if (1) {
3095#endif
3096 puts("Cannot switch back to console from file redirection");
3097 menuin[0] = 'q';
3098 menuin[1] = '\0';
3099 } else {
3100 puts("Switched back to console from file redirection");
3101 continue;
3102 }
3103 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003104
Benny Prijonoebc32c32008-06-12 13:30:39 +00003105 if (cmd_echo) {
3106 printf("%s", menuin);
3107 }
3108
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003109 switch (menuin[0]) {
3110
3111 case 'm':
3112 /* Make call! : */
3113 printf("(You currently have %d calls)\n",
3114 pjsua_call_get_count());
3115
3116 uri = NULL;
3117 ui_input_url("Make call", buf, sizeof(buf), &result);
3118 if (result.nb_result != NO_NB) {
3119
3120 if (result.nb_result == -1 || result.nb_result == 0) {
3121 puts("You can't do that with make call!");
3122 continue;
3123 } else {
3124 pjsua_buddy_info binfo;
3125 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3126 uri = binfo.uri.ptr;
3127 }
3128
3129 } else if (result.uri_result) {
3130 uri = result.uri_result;
3131 }
3132
3133 tmp = pj_str(uri);
3134 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
3135 break;
3136
3137 case 'M':
3138 /* Make multiple calls! : */
3139 printf("(You currently have %d calls)\n",
3140 pjsua_call_get_count());
3141
3142 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
3143 continue;
3144
3145 count = my_atoi(menuin);
3146 if (count < 1)
3147 continue;
3148
3149 ui_input_url("Make call", buf, sizeof(buf), &result);
3150 if (result.nb_result != NO_NB) {
3151 pjsua_buddy_info binfo;
3152 if (result.nb_result == -1 || result.nb_result == 0) {
3153 puts("You can't do that with make call!");
3154 continue;
3155 }
3156 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3157 uri = binfo.uri.ptr;
3158 } else {
3159 uri = result.uri_result;
3160 }
3161
3162 for (i=0; i<my_atoi(menuin); ++i) {
3163 pj_status_t status;
3164
3165 tmp = pj_str(uri);
3166 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
3167 NULL, NULL);
3168 if (status != PJ_SUCCESS)
3169 break;
3170 }
3171 break;
3172
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003173 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00003174 i = pjsua_detect_nat_type();
3175 if (i != PJ_SUCCESS)
3176 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003177 break;
3178
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003179 case 'i':
3180 /* Send instant messaeg */
3181
3182 /* i is for call index to send message, if any */
3183 i = -1;
3184
3185 /* Make compiler happy. */
3186 uri = NULL;
3187
3188 /* Input destination. */
3189 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3190 if (result.nb_result != NO_NB) {
3191
3192 if (result.nb_result == -1) {
3193 puts("You can't send broadcast IM like that!");
3194 continue;
3195
3196 } else if (result.nb_result == 0) {
3197
3198 i = current_call;
3199
3200 } else {
3201 pjsua_buddy_info binfo;
3202 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3203 uri = binfo.uri.ptr;
3204 }
3205
3206 } else if (result.uri_result) {
3207 uri = result.uri_result;
3208 }
3209
3210
3211 /* Send typing indication. */
3212 if (i != -1)
3213 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3214 else {
3215 pj_str_t tmp_uri = pj_str(uri);
3216 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3217 }
3218
3219 /* Input the IM . */
3220 if (!simple_input("Message", text, sizeof(text))) {
3221 /*
3222 * Cancelled.
3223 * Send typing notification too, saying we're not typing.
3224 */
3225 if (i != -1)
3226 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3227 else {
3228 pj_str_t tmp_uri = pj_str(uri);
3229 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3230 }
3231 continue;
3232 }
3233
3234 tmp = pj_str(text);
3235
3236 /* Send the IM */
3237 if (i != -1)
3238 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3239 else {
3240 pj_str_t tmp_uri = pj_str(uri);
3241 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3242 }
3243
3244 break;
3245
3246 case 'a':
3247
3248 if (current_call != -1) {
3249 pjsua_call_get_info(current_call, &call_info);
3250 } else {
3251 /* Make compiler happy */
3252 call_info.role = PJSIP_ROLE_UAC;
3253 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3254 }
3255
3256 if (current_call == -1 ||
3257 call_info.role != PJSIP_ROLE_UAS ||
3258 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3259 {
3260 puts("No pending incoming call");
3261 fflush(stdout);
3262 continue;
3263
3264 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003265 int st_code;
3266 char contact[120];
3267 pj_str_t hname = { "Contact", 7 };
3268 pj_str_t hvalue;
3269 pjsip_generic_string_hdr hcontact;
3270 pjsua_msg_data msg_data;
3271
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003272 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3273 continue;
3274
Benny Prijono20d36722007-02-22 14:52:24 +00003275 st_code = my_atoi(buf);
3276 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003277 continue;
3278
Benny Prijono20d36722007-02-22 14:52:24 +00003279 pjsua_msg_data_init(&msg_data);
3280
3281 if (st_code/100 == 3) {
3282 if (!simple_input("Enter URL to be put in Contact",
3283 contact, sizeof(contact)))
3284 continue;
3285 hvalue = pj_str(contact);
3286 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3287
3288 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3289 }
3290
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003291 /*
3292 * Must check again!
3293 * Call may have been disconnected while we're waiting for
3294 * keyboard input.
3295 */
3296 if (current_call == -1) {
3297 puts("Call has been disconnected");
3298 fflush(stdout);
3299 continue;
3300 }
3301
Benny Prijono20d36722007-02-22 14:52:24 +00003302 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003303 }
3304
3305 break;
3306
3307
3308 case 'h':
3309
3310 if (current_call == -1) {
3311 puts("No current call");
3312 fflush(stdout);
3313 continue;
3314
3315 } else if (menuin[1] == 'a') {
3316
3317 /* Hangup all calls */
3318 pjsua_call_hangup_all();
3319
3320 } else {
3321
3322 /* Hangup current calls */
3323 pjsua_call_hangup(current_call, 0, NULL, NULL);
3324 }
3325 break;
3326
3327 case ']':
3328 case '[':
3329 /*
3330 * Cycle next/prev dialog.
3331 */
3332 if (menuin[0] == ']') {
3333 find_next_call();
3334
3335 } else {
3336 find_prev_call();
3337 }
3338
3339 if (current_call != -1) {
3340
3341 pjsua_call_get_info(current_call, &call_info);
3342 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3343 (int)call_info.remote_info.slen,
3344 call_info.remote_info.ptr));
3345
3346 } else {
3347 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3348 }
3349 break;
3350
3351
3352 case '>':
3353 case '<':
3354 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3355 break;
3356
3357 i = my_atoi(buf);
3358 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003359 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003360 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3361 } else {
3362 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3363 }
3364 break;
3365
3366
3367 case '+':
3368 if (menuin[1] == 'b') {
3369
3370 pjsua_buddy_config buddy_cfg;
3371 pjsua_buddy_id buddy_id;
3372 pj_status_t status;
3373
3374 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3375 break;
3376
3377 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3378 printf("Invalid SIP URI '%s'\n", buf);
3379 break;
3380 }
3381
Benny Prijonoac623b32006-07-03 15:19:31 +00003382 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003383
3384 buddy_cfg.uri = pj_str(buf);
3385 buddy_cfg.subscribe = PJ_TRUE;
3386
3387 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3388 if (status == PJ_SUCCESS) {
3389 printf("New buddy '%s' added at index %d\n",
3390 buf, buddy_id+1);
3391 }
3392
3393 } else if (menuin[1] == 'a') {
3394
Benny Prijonofb2b3652007-06-28 07:15:03 +00003395 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3396 pjsua_acc_config acc_cfg;
3397 pj_status_t status;
3398
3399 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3400 break;
3401 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3402 break;
3403 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3404 break;
3405 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3406 break;
3407 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3408 break;
3409
3410 pjsua_acc_config_default(&acc_cfg);
3411 acc_cfg.id = pj_str(id);
3412 acc_cfg.reg_uri = pj_str(registrar);
3413 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003414 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003415 acc_cfg.cred_info[0].realm = pj_str(realm);
3416 acc_cfg.cred_info[0].username = pj_str(uname);
3417 acc_cfg.cred_info[0].data_type = 0;
3418 acc_cfg.cred_info[0].data = pj_str(passwd);
3419
3420 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3421 if (status != PJ_SUCCESS) {
3422 pjsua_perror(THIS_FILE, "Error adding new account", status);
3423 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003424
3425 } else {
3426 printf("Invalid input %s\n", menuin);
3427 }
3428 break;
3429
3430 case '-':
3431 if (menuin[1] == 'b') {
3432 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3433 break;
3434
3435 i = my_atoi(buf) - 1;
3436
3437 if (!pjsua_buddy_is_valid(i)) {
3438 printf("Invalid buddy id %d\n", i);
3439 } else {
3440 pjsua_buddy_del(i);
3441 printf("Buddy %d deleted\n", i);
3442 }
3443
3444 } else if (menuin[1] == 'a') {
3445
3446 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3447 break;
3448
3449 i = my_atoi(buf);
3450
3451 if (!pjsua_acc_is_valid(i)) {
3452 printf("Invalid account id %d\n", i);
3453 } else {
3454 pjsua_acc_del(i);
3455 printf("Account %d deleted\n", i);
3456 }
3457
3458 } else {
3459 printf("Invalid input %s\n", menuin);
3460 }
3461 break;
3462
3463 case 'H':
3464 /*
3465 * Hold call.
3466 */
3467 if (current_call != -1) {
3468
3469 pjsua_call_set_hold(current_call, NULL);
3470
3471 } else {
3472 PJ_LOG(3,(THIS_FILE, "No current call"));
3473 }
3474 break;
3475
3476 case 'v':
3477 /*
3478 * Send re-INVITE (to release hold, etc).
3479 */
3480 if (current_call != -1) {
3481
3482 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3483
3484 } else {
3485 PJ_LOG(3,(THIS_FILE, "No current call"));
3486 }
3487 break;
3488
Benny Prijonoc08682e2007-10-04 06:17:58 +00003489 case 'U':
3490 /*
3491 * Send UPDATE
3492 */
3493 if (current_call != -1) {
3494
3495 pjsua_call_update(current_call, 0, NULL);
3496
3497 } else {
3498 PJ_LOG(3,(THIS_FILE, "No current call"));
3499 }
3500 break;
3501
3502 case 'C':
3503 if (menuin[1] == 'p') {
3504 manage_codec_prio();
3505 }
3506 break;
3507
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003508 case 'x':
3509 /*
3510 * Transfer call.
3511 */
3512 if (current_call == -1) {
3513
3514 PJ_LOG(3,(THIS_FILE, "No current call"));
3515
3516 } else {
3517 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003518 pjsua_msg_data msg_data;
3519 pjsip_generic_string_hdr refer_sub;
3520 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3521 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003522 pjsua_call_info ci;
3523
3524 pjsua_call_get_info(current_call, &ci);
3525 printf("Transfering current call [%d] %.*s\n",
3526 current_call,
3527 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003528
3529 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3530
3531 /* Check if call is still there. */
3532
3533 if (call != current_call) {
3534 puts("Call has been disconnected");
3535 continue;
3536 }
3537
Benny Prijonod524e822006-09-22 12:48:18 +00003538 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003539 if (app_config.no_refersub) {
3540 /* Add Refer-Sub: false in outgoing REFER request */
3541 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3542 &STR_FALSE);
3543 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3544 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003545 if (result.nb_result != NO_NB) {
3546 if (result.nb_result == -1 || result.nb_result == 0)
3547 puts("You can't do that with transfer call!");
3548 else {
3549 pjsua_buddy_info binfo;
3550 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003551 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003552 }
3553
3554 } else if (result.uri_result) {
3555 pj_str_t tmp;
3556 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003557 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003558 }
3559 }
3560 break;
3561
Benny Prijonof7b1c392006-11-11 16:46:34 +00003562 case 'X':
3563 /*
3564 * Transfer call with replaces.
3565 */
3566 if (current_call == -1) {
3567
3568 PJ_LOG(3,(THIS_FILE, "No current call"));
3569
3570 } else {
3571 int call = current_call;
3572 int dst_call;
3573 pjsua_msg_data msg_data;
3574 pjsip_generic_string_hdr refer_sub;
3575 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3576 pj_str_t STR_FALSE = { "false", 5 };
3577 pjsua_call_id ids[PJSUA_MAX_CALLS];
3578 pjsua_call_info ci;
3579 unsigned i, count;
3580
3581 count = PJ_ARRAY_SIZE(ids);
3582 pjsua_enum_calls(ids, &count);
3583
3584 if (count <= 1) {
3585 puts("There are no other calls");
3586 continue;
3587 }
3588
3589 pjsua_call_get_info(current_call, &ci);
3590 printf("Transfer call [%d] %.*s to one of the following:\n",
3591 current_call,
3592 (int)ci.remote_info.slen, ci.remote_info.ptr);
3593
3594 for (i=0; i<count; ++i) {
3595 pjsua_call_info call_info;
3596
3597 if (ids[i] == call)
3598 continue;
3599
3600 pjsua_call_get_info(ids[i], &call_info);
3601 printf("%d %.*s [%.*s]\n",
3602 ids[i],
3603 (int)call_info.remote_info.slen,
3604 call_info.remote_info.ptr,
3605 (int)call_info.state_text.slen,
3606 call_info.state_text.ptr);
3607 }
3608
3609 if (!simple_input("Enter call number to be replaced",
3610 buf, sizeof(buf)))
3611 continue;
3612
3613 dst_call = my_atoi(buf);
3614
3615 /* Check if call is still there. */
3616
3617 if (call != current_call) {
3618 puts("Call has been disconnected");
3619 continue;
3620 }
3621
3622 /* Check that destination call is valid. */
3623 if (dst_call == call) {
3624 puts("Destination call number must not be the same "
3625 "as the call being transfered");
3626 continue;
3627 }
3628 if (dst_call >= PJSUA_MAX_CALLS) {
3629 puts("Invalid destination call number");
3630 continue;
3631 }
3632 if (!pjsua_call_is_active(dst_call)) {
3633 puts("Invalid destination call number");
3634 continue;
3635 }
3636
3637 pjsua_msg_data_init(&msg_data);
3638 if (app_config.no_refersub) {
3639 /* Add Refer-Sub: false in outgoing REFER request */
3640 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3641 &STR_FALSE);
3642 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3643 }
3644
3645 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3646 }
3647 break;
3648
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003649 case '#':
3650 /*
3651 * Send DTMF strings.
3652 */
3653 if (current_call == -1) {
3654
3655 PJ_LOG(3,(THIS_FILE, "No current call"));
3656
3657 } else if (!pjsua_call_has_media(current_call)) {
3658
3659 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3660
3661 } else {
3662 pj_str_t digits;
3663 int call = current_call;
3664 pj_status_t status;
3665
3666 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3667 sizeof(buf)))
3668 {
3669 break;
3670 }
3671
3672 if (call != current_call) {
3673 puts("Call has been disconnected");
3674 continue;
3675 }
3676
3677 digits = pj_str(buf);
3678 status = pjsua_call_dial_dtmf(current_call, &digits);
3679 if (status != PJ_SUCCESS) {
3680 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3681 } else {
3682 puts("DTMF digits enqueued for transmission");
3683 }
3684 }
3685 break;
3686
Benny Prijonofeb69f42007-10-05 09:12:26 +00003687 case '*':
3688 /* Send DTMF with INFO */
3689 if (current_call == -1) {
3690
3691 PJ_LOG(3,(THIS_FILE, "No current call"));
3692
3693 } else {
3694 const pj_str_t SIP_INFO = pj_str("INFO");
3695 pj_str_t digits;
3696 int call = current_call;
3697 int i;
3698 pj_status_t status;
3699
3700 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3701 sizeof(buf)))
3702 {
3703 break;
3704 }
3705
3706 if (call != current_call) {
3707 puts("Call has been disconnected");
3708 continue;
3709 }
3710
3711 digits = pj_str(buf);
3712 for (i=0; i<digits.slen; ++i) {
3713 pjsua_msg_data msg_data;
3714 char body[80];
3715
3716 pjsua_msg_data_init(&msg_data);
3717 msg_data.content_type = pj_str("application/dtmf-relay");
3718
3719 pj_ansi_snprintf(body, sizeof(body),
3720 "Signal=%c\r\n"
3721 "Duration=160",
3722 buf[i]);
3723 msg_data.msg_body = pj_str(body);
3724
3725 status = pjsua_call_send_request(current_call, &SIP_INFO,
3726 &msg_data);
3727 if (status != PJ_SUCCESS) {
3728 break;
3729 }
3730 }
3731 }
3732 break;
3733
Benny Prijono56315612006-07-18 14:39:40 +00003734 case 'S':
3735 /*
3736 * Send arbitrary request
3737 */
3738 if (pjsua_acc_get_count() == 0) {
3739 puts("Sorry, need at least one account configured");
3740 break;
3741 }
3742
3743 puts("Send arbitrary request to remote host");
3744
3745 /* Input METHOD */
3746 if (!simple_input("Request method:",text,sizeof(text)))
3747 break;
3748
3749 /* Input destination URI */
3750 uri = NULL;
3751 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3752 if (result.nb_result != NO_NB) {
3753
3754 if (result.nb_result == -1 || result.nb_result == 0) {
3755 puts("Sorry you can't do that!");
3756 continue;
3757 } else {
3758 pjsua_buddy_info binfo;
3759 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3760 uri = binfo.uri.ptr;
3761 }
3762
3763 } else if (result.uri_result) {
3764 uri = result.uri_result;
3765 }
3766
3767 tmp = pj_str(uri);
3768
3769 send_request(text, &tmp);
3770 break;
3771
Benny Prijonoebc32c32008-06-12 13:30:39 +00003772 case 'e':
3773 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3774 pj_str_t tmp;
3775
3776 tmp.ptr = menuin+5;
3777 tmp.slen = pj_ansi_strlen(menuin)-6;
3778
3779 if (tmp.slen < 1) {
3780 puts("Usage: echo [0|1]");
3781 break;
3782 }
3783
Benny Prijonoddd02de2008-06-26 22:20:11 +00003784 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003785 }
3786 break;
3787
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003788 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003789 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3790 pj_str_t tmp;
3791 int delay;
3792
3793 tmp.ptr = menuin+6;
3794 tmp.slen = pj_ansi_strlen(menuin)-7;
3795
3796 if (tmp.slen < 1) {
3797 puts("Usage: sleep MSEC");
3798 break;
3799 }
3800
3801 delay = pj_strtoul(&tmp);
3802 if (delay < 0) delay = 0;
3803 pj_thread_sleep(delay);
3804 break;
3805 }
3806 /* Continue below */
3807
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003808 case 'u':
3809 /*
3810 * Subscribe/unsubscribe presence.
3811 */
3812 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3813 if (result.nb_result != NO_NB) {
3814 if (result.nb_result == -1) {
3815 int i, count;
3816 count = pjsua_get_buddy_count();
3817 for (i=0; i<count; ++i)
3818 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3819 } else if (result.nb_result == 0) {
3820 puts("Sorry, can only subscribe to buddy's presence, "
3821 "not from existing call");
3822 } else {
3823 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3824 }
3825
3826 } else if (result.uri_result) {
3827 puts("Sorry, can only subscribe to buddy's presence, "
3828 "not arbitrary URL (for now)");
3829 }
3830
3831 break;
3832
3833 case 'r':
3834 switch (menuin[1]) {
3835 case 'r':
3836 /*
3837 * Re-Register.
3838 */
3839 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3840 break;
3841 case 'u':
3842 /*
3843 * Unregister
3844 */
3845 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3846 break;
3847 }
3848 break;
3849
3850 case 't':
3851 pjsua_acc_get_info(current_acc, &acc_info);
3852 acc_info.online_status = !acc_info.online_status;
3853 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3854 printf("Setting %s online status to %s\n",
3855 acc_info.acc_uri.ptr,
3856 (acc_info.online_status?"online":"offline"));
3857 break;
3858
Benny Prijono4461c7d2007-08-25 13:36:15 +00003859 case 'T':
3860 change_online_status();
3861 break;
3862
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003863 case 'c':
3864 switch (menuin[1]) {
3865 case 'l':
3866 conf_list();
3867 break;
3868 case 'c':
3869 case 'd':
3870 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003871 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003872 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003873 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003874 const char *src_title, *dst_title;
3875
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003876 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003877
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003878 if (cnt != 3) {
3879 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003880
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003881 src_title = (menuin[1]=='c'?
3882 "Connect src port #":
3883 "Disconnect src port #");
3884 dst_title = (menuin[1]=='c'?
3885 "To dst port #":
3886 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003887
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003888 if (!simple_input(src_title, src_port, sizeof(src_port)))
3889 break;
3890
3891 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3892 break;
3893 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003894
3895 if (menuin[1]=='c') {
3896 status = pjsua_conf_connect(my_atoi(src_port),
3897 my_atoi(dst_port));
3898 } else {
3899 status = pjsua_conf_disconnect(my_atoi(src_port),
3900 my_atoi(dst_port));
3901 }
3902 if (status == PJ_SUCCESS) {
3903 puts("Success");
3904 } else {
3905 puts("ERROR!!");
3906 }
3907 }
3908 break;
3909 }
3910 break;
3911
Benny Prijono6dd967c2006-12-26 02:27:14 +00003912 case 'V':
3913 /* Adjust audio volume */
3914 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3915 if (simple_input(buf,text,sizeof(text))) {
3916 char *err;
3917 app_config.mic_level = (float)strtod(text, &err);
3918 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3919 }
3920 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3921 app_config.speaker_level);
3922 if (simple_input(buf,text,sizeof(text))) {
3923 char *err;
3924 app_config.speaker_level = (float)strtod(text, &err);
3925 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3926 }
3927
3928 break;
3929
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003930 case 'd':
3931 if (menuin[1] == 'c') {
3932 char settings[2000];
3933 int len;
3934
3935 len = write_settings(&app_config, settings, sizeof(settings));
3936 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003937 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003938 else
3939 PJ_LOG(3,(THIS_FILE,
3940 "Dumping configuration (%d bytes):\n%s\n",
3941 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003942
3943 } else if (menuin[1] == 'q') {
3944
3945 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00003946 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00003947 } else {
3948 PJ_LOG(3,(THIS_FILE, "No current call"));
3949 }
3950
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003951 } else {
3952 app_dump(menuin[1]=='d');
3953 }
3954 break;
3955
3956
3957 case 'f':
3958 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3959 char settings[2000];
3960 int len;
3961
3962 len = write_settings(&app_config, settings, sizeof(settings));
3963 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003964 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003965 else {
3966 pj_oshandle_t fd;
3967 pj_status_t status;
3968
3969 status = pj_file_open(app_config.pool, buf,
3970 PJ_O_WRONLY, &fd);
3971 if (status != PJ_SUCCESS) {
3972 pjsua_perror(THIS_FILE, "Unable to open file", status);
3973 } else {
3974 pj_ssize_t size = len;
3975 pj_file_write(fd, settings, &size);
3976 pj_file_close(fd);
3977
3978 printf("Settings successfully written to '%s'\n", buf);
3979 }
3980 }
3981
3982 }
3983 break;
3984
3985
3986 case 'q':
3987 goto on_exit;
3988
3989
Benny Prijono5e51a4e2008-11-27 00:06:46 +00003990 case 'R':
3991 if (!pjsua_call_is_active(current_call)) {
3992 PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
3993 } else if (menuin[1] == 'a') {
3994 pjsua_call_process_redirect(current_call,
3995 PJSIP_REDIRECT_ACCEPT);
3996 } else if (menuin[1] == 'r') {
3997 pjsua_call_process_redirect(current_call,
3998 PJSIP_REDIRECT_REJECT);
3999 } else {
4000 pjsua_call_process_redirect(current_call,
4001 PJSIP_REDIRECT_STOP);
4002 }
4003 break;
4004
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004005 default:
4006 if (menuin[0] != '\n' && menuin[0] != '\r') {
4007 printf("Invalid input %s", menuin);
4008 }
4009 keystroke_help();
4010 break;
4011 }
4012 }
4013
4014on_exit:
4015 ;
4016}
4017
4018
4019/*****************************************************************************
4020 * Public API
4021 */
4022
4023pj_status_t app_init(int argc, char *argv[])
4024{
Benny Prijonoe93e2872006-06-28 16:46:49 +00004025 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00004026 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004027 unsigned i;
4028 pj_status_t status;
4029
4030 /* Create pjsua */
4031 status = pjsua_create();
4032 if (status != PJ_SUCCESS)
4033 return status;
4034
4035 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00004036 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004037
4038 /* Initialize default config */
4039 default_config(&app_config);
4040
4041 /* Parse the arguments */
4042 status = parse_args(argc, argv, &app_config, &uri_arg);
4043 if (status != PJ_SUCCESS)
4044 return status;
4045
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004046 /* Initialize application callbacks */
4047 app_config.cfg.cb.on_call_state = &on_call_state;
4048 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
4049 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00004050 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00004051 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00004052 app_config.cfg.cb.on_call_redirected = &call_on_redirected;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004053 app_config.cfg.cb.on_reg_state = &on_reg_state;
Benny Prijono563cd7a2008-08-19 20:02:15 +00004054 app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004055 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
4056 app_config.cfg.cb.on_pager = &on_pager;
4057 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00004058 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00004059 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00004060 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004061
4062 /* Initialize pjsua */
4063 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
4064 &app_config.media_cfg);
4065 if (status != PJ_SUCCESS)
4066 return status;
4067
Benny Prijonoe909eac2006-07-27 22:04:56 +00004068#ifdef STEREO_DEMO
4069 stereo_demo();
4070#endif
4071
Benny Prijono804ff0a2006-09-14 11:17:48 +00004072 /* Initialize calls data */
4073 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
4074 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
4075 app_config.call_data[i].timer.cb = &call_timeout_callback;
4076 }
4077
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004078 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00004079 for (i=0; i<app_config.wav_count; ++i) {
4080 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004081 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00004082
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004083 if (app_config.auto_play_hangup)
4084 play_options |= PJMEDIA_FILE_NO_LOOP;
4085
4086 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00004087 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004088 if (status != PJ_SUCCESS)
4089 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00004090
Benny Prijono72c04d22007-02-17 22:20:58 +00004091 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00004092 app_config.wav_id = wav_id;
4093 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004094 if (app_config.auto_play_hangup) {
4095 pjmedia_port *port;
4096
4097 pjsua_player_get_port(app_config.wav_id, &port);
4098 status = pjmedia_wav_player_set_eof_cb(port, NULL,
4099 &on_playfile_done);
4100 if (status != PJ_SUCCESS)
4101 goto on_error;
4102
4103 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
4104 &hangup_timeout_callback);
4105 }
Benny Prijono32e4f492007-01-24 00:44:26 +00004106 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004107 }
4108
Benny Prijono4af234b2007-01-24 02:02:09 +00004109 /* Optionally registers tone players */
4110 for (i=0; i<app_config.tone_count; ++i) {
4111 pjmedia_port *tport;
4112 char name[80];
4113 pj_str_t label;
4114 pj_status_t status;
4115
4116 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
4117 app_config.tones[i].freq1,
4118 app_config.tones[i].freq2);
4119 label = pj_str(name);
4120 status = pjmedia_tonegen_create2(app_config.pool, &label,
4121 8000, 1, 160, 16,
4122 PJMEDIA_TONEGEN_LOOP, &tport);
4123 if (status != PJ_SUCCESS) {
4124 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
4125 goto on_error;
4126 }
4127
4128 status = pjsua_conf_add_port(app_config.pool, tport,
4129 &app_config.tone_slots[i]);
4130 pj_assert(status == PJ_SUCCESS);
4131
4132 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
4133 pj_assert(status == PJ_SUCCESS);
4134 }
4135
Benny Prijono1ebd6142006-10-19 15:48:02 +00004136 /* Optionally create recorder file, if any. */
4137 if (app_config.rec_file.slen) {
4138 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
4139 &app_config.rec_id);
4140 if (status != PJ_SUCCESS)
4141 goto on_error;
4142
4143 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
4144 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004145
Benny Prijonoe347cb02007-02-14 14:36:13 +00004146 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
4147
Benny Prijono91d20f42008-06-14 19:42:37 +00004148 /* Create ringback tones */
4149 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00004150 unsigned i, samples_per_frame;
4151 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00004152 pj_str_t name;
4153
Benny Prijono4f966892008-06-14 22:43:56 +00004154 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
4155 app_config.media_cfg.clock_rate *
4156 app_config.media_cfg.channel_count / 1000;
4157
Benny Prijono91d20f42008-06-14 19:42:37 +00004158 /* Ringback tone (call is ringing) */
4159 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00004160 status = pjmedia_tonegen_create2(app_config.pool, &name,
4161 app_config.media_cfg.clock_rate,
4162 app_config.media_cfg.channel_count,
4163 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004164 16, PJMEDIA_TONEGEN_LOOP,
4165 &app_config.ringback_port);
4166 if (status != PJ_SUCCESS)
4167 goto on_error;
4168
Benny Prijonoe28cec82008-06-14 20:40:53 +00004169 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00004170 for (i=0; i<RINGBACK_CNT; ++i) {
4171 tone[i].freq1 = RINGBACK_FREQ1;
4172 tone[i].freq2 = RINGBACK_FREQ2;
4173 tone[i].on_msec = RINGBACK_ON;
4174 tone[i].off_msec = RINGBACK_OFF;
4175 }
4176 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004177
Benny Prijono4f966892008-06-14 22:43:56 +00004178 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004179 PJMEDIA_TONEGEN_LOOP);
4180
4181
Benny Prijono91d20f42008-06-14 19:42:37 +00004182 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
4183 &app_config.ringback_slot);
4184 if (status != PJ_SUCCESS)
4185 goto on_error;
4186
4187 /* Ring (to alert incoming call) */
4188 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00004189 status = pjmedia_tonegen_create2(app_config.pool, &name,
4190 app_config.media_cfg.clock_rate,
4191 app_config.media_cfg.channel_count,
4192 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004193 16, PJMEDIA_TONEGEN_LOOP,
4194 &app_config.ring_port);
4195 if (status != PJ_SUCCESS)
4196 goto on_error;
4197
Benny Prijono4f966892008-06-14 22:43:56 +00004198 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00004199 tone[i].freq1 = RING_FREQ1;
4200 tone[i].freq2 = RING_FREQ2;
4201 tone[i].on_msec = RING_ON;
4202 tone[i].off_msec = RING_OFF;
4203 }
Benny Prijono4f966892008-06-14 22:43:56 +00004204 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004205
Benny Prijono4f966892008-06-14 22:43:56 +00004206 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004207 tone, PJMEDIA_TONEGEN_LOOP);
4208
Benny Prijono91d20f42008-06-14 19:42:37 +00004209 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4210 &app_config.ring_slot);
4211 if (status != PJ_SUCCESS)
4212 goto on_error;
4213
4214 }
4215
Benny Prijono87ef89a2007-01-14 00:39:45 +00004216 /* Add UDP transport unless it's disabled. */
4217 if (!app_config.no_udp) {
4218 pjsua_acc_id aid;
4219
4220 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
4221 &app_config.udp_cfg,
4222 &transport_id);
4223 if (status != PJ_SUCCESS)
4224 goto on_error;
4225
4226 /* Add local account */
4227 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4228 //pjsua_acc_set_transport(aid, transport_id);
4229 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004230
4231 if (app_config.udp_cfg.port == 0) {
4232 pjsua_transport_info ti;
4233 pj_sockaddr_in *a;
4234
4235 pjsua_transport_get_info(transport_id, &ti);
4236 a = (pj_sockaddr_in*)&ti.local_addr;
4237
4238 tcp_cfg.port = pj_ntohs(a->sin_port);
4239 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004240 }
4241
Benny Prijonoe93e2872006-06-28 16:46:49 +00004242 /* Add TCP transport unless it's disabled */
4243 if (!app_config.no_tcp) {
4244 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004245 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004246 &transport_id);
4247 if (status != PJ_SUCCESS)
4248 goto on_error;
4249
4250 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004251 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004252 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4253
4254 }
4255
Benny Prijonoe93e2872006-06-28 16:46:49 +00004256
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004257#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4258 /* Add TLS transport when application wants one */
4259 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004260
4261 pjsua_acc_id acc_id;
4262
4263 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004264 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004265 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004266 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004267 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004268 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004269 if (status != PJ_SUCCESS)
4270 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004271
4272 /* Add local account */
4273 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4274 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004275 }
4276#endif
4277
Benny Prijonoe93e2872006-06-28 16:46:49 +00004278 if (transport_id == -1) {
Benny Prijonod6e362a2008-07-19 17:53:47 +00004279 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
Benny Prijonoe93e2872006-06-28 16:46:49 +00004280 status = -1;
4281 goto on_error;
4282 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004283
4284
4285 /* Add accounts */
4286 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004287 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004288 if (status != PJ_SUCCESS)
4289 goto on_error;
4290 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4291 }
4292
4293 /* Add buddies */
4294 for (i=0; i<app_config.buddy_cnt; ++i) {
4295 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4296 if (status != PJ_SUCCESS)
4297 goto on_error;
4298 }
4299
4300 /* Optionally set codec orders */
4301 for (i=0; i<app_config.codec_cnt; ++i) {
4302 pjsua_codec_set_priority(&app_config.codec_arg[i],
4303 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4304 }
4305
Benny Prijonofce28542007-12-09 15:41:10 +00004306 /* Optionally disable some codec */
4307 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4308 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4309 }
4310
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004311 /* Add RTP transports */
Benny Prijono40860c32008-09-04 13:55:33 +00004312#ifdef TRANSPORT_ADAPTER_SAMPLE
4313 status = transport_adapter_sample();
4314
4315#else
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004316 status = pjsua_media_transports_create(&app_config.rtp_cfg);
Benny Prijono40860c32008-09-04 13:55:33 +00004317#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004318 if (status != PJ_SUCCESS)
4319 goto on_error;
4320
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004321 /* Set sound device latency */
4322 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4323
Benny Prijono22a300a2006-06-14 20:04:55 +00004324 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004325#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004326 if (app_config.null_audio) {
4327 status = pjsua_set_null_snd_dev();
4328 if (status != PJ_SUCCESS)
4329 return status;
4330 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004331#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004332
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004333 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4334 app_config.playback_dev != PJSUA_INVALID_ID)
4335 {
4336 status = pjsua_set_snd_dev(app_config.capture_dev,
4337 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004338 if (status != PJ_SUCCESS)
4339 goto on_error;
4340 }
4341
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004342 return PJ_SUCCESS;
4343
4344on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004345 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004346 return status;
4347}
4348
4349
Benny Prijonoebc32c32008-06-12 13:30:39 +00004350static int stdout_refresh_proc(void *arg)
4351{
4352 PJ_UNUSED_ARG(arg);
4353
4354 /* Set thread to lowest priority so that it doesn't clobber
4355 * stdout output
4356 */
4357 pj_thread_set_prio(pj_thread_this(),
4358 pj_thread_get_prio_min(pj_thread_this()));
4359
4360 while (!stdout_refresh_quit) {
4361 pj_thread_sleep(stdout_refresh * 1000);
4362 puts(stdout_refresh_text);
4363 fflush(stdout);
4364 }
4365
4366 return 0;
4367}
4368
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004369pj_status_t app_main(void)
4370{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004371 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004372 pj_status_t status;
4373
4374 /* Start pjsua */
4375 status = pjsua_start();
4376 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004377 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004378 return status;
4379 }
4380
Benny Prijonoebc32c32008-06-12 13:30:39 +00004381 /* Start console refresh thread */
4382 if (stdout_refresh > 0) {
4383 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4384 NULL, 0, 0, &stdout_refresh_thread);
4385 }
4386
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004387 console_app_main(&uri_arg);
4388
Benny Prijonoebc32c32008-06-12 13:30:39 +00004389 if (stdout_refresh_thread) {
4390 stdout_refresh_quit = PJ_TRUE;
4391 pj_thread_join(stdout_refresh_thread);
4392 pj_thread_destroy(stdout_refresh_thread);
4393 }
4394
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004395 return PJ_SUCCESS;
4396}
4397
4398pj_status_t app_destroy(void)
4399{
Benny Prijonof762ee72006-12-01 11:14:37 +00004400 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004401 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004402
Benny Prijonoe909eac2006-07-27 22:04:56 +00004403#ifdef STEREO_DEMO
4404 if (app_config.snd) {
4405 pjmedia_snd_port_destroy(app_config.snd);
4406 app_config.snd = NULL;
4407 }
4408#endif
4409
Benny Prijono91d20f42008-06-14 19:42:37 +00004410 /* Close ringback port */
4411 if (app_config.ringback_port &&
4412 app_config.ringback_slot != PJSUA_INVALID_ID)
4413 {
4414 pjsua_conf_remove_port(app_config.ringback_slot);
4415 app_config.ringback_slot = PJSUA_INVALID_ID;
4416 pjmedia_port_destroy(app_config.ringback_port);
4417 app_config.ringback_port = NULL;
4418 }
4419
4420 /* Close ring port */
4421 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4422 pjsua_conf_remove_port(app_config.ring_slot);
4423 app_config.ring_slot = PJSUA_INVALID_ID;
4424 pjmedia_port_destroy(app_config.ring_port);
4425 app_config.ring_port = NULL;
4426 }
4427
Benny Prijono4af234b2007-01-24 02:02:09 +00004428 /* Close tone generators */
4429 for (i=0; i<app_config.tone_count; ++i) {
4430 pjsua_conf_remove_port(app_config.tone_slots[i]);
4431 }
4432
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004433 if (app_config.pool) {
4434 pj_pool_release(app_config.pool);
4435 app_config.pool = NULL;
4436 }
4437
Benny Prijonof762ee72006-12-01 11:14:37 +00004438 status = pjsua_destroy();
4439
4440 pj_bzero(&app_config, sizeof(app_config));
4441
4442 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004443}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004444
4445
4446#ifdef STEREO_DEMO
4447static void stereo_demo()
4448{
4449 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004450 pj_status_t status;
4451
4452 /* Disable existing sound device */
4453 conf = pjsua_set_no_snd_dev();
4454
Benny Prijonoe909eac2006-07-27 22:04:56 +00004455 /* Create stereo-mono splitter/combiner */
4456 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004457 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004458 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004459 2 * conf->info.samples_per_frame,
4460 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004461 0 /* options */,
4462 &splitter);
4463 pj_assert(status == PJ_SUCCESS);
4464
4465 /* Connect channel0 (left channel?) to conference port slot0 */
4466 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4467 0 /*options*/,
4468 conf);
4469 pj_assert(status == PJ_SUCCESS);
4470
4471 /* Create reverse channel for channel1 (right channel?)... */
4472 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4473 splitter,
4474 1 /* ch1 */,
4475 0 /* options */,
4476 &ch1);
4477 pj_assert(status == PJ_SUCCESS);
4478
4479 /* .. and register it to conference bridge (it would be slot1
4480 * if there's no other devices connected to the bridge)
4481 */
4482 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4483 pj_assert(status == PJ_SUCCESS);
4484
4485 /* Create sound device */
4486 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004487 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004488 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004489 2 * conf->info.samples_per_frame,
4490 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004491 0, &app_config.snd);
4492 pj_assert(status == PJ_SUCCESS);
4493
4494
4495 /* Connect the splitter to the sound device */
4496 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4497 pj_assert(status == PJ_SUCCESS);
4498
4499}
4500#endif
4501
Benny Prijono40860c32008-09-04 13:55:33 +00004502#ifdef TRANSPORT_ADAPTER_SAMPLE
4503static pj_status_t create_transport_adapter(pjmedia_endpt *med_endpt, int port,
4504 pjmedia_transport **p_tp)
4505{
4506 pjmedia_transport *udp;
4507 pj_status_t status;
4508
4509 /* Create the UDP media transport */
4510 status = pjmedia_transport_udp_create(med_endpt, NULL, port, 0, &udp);
4511 if (status != PJ_SUCCESS)
4512 return status;
4513
4514 /* Create the adapter */
4515 status = pjmedia_tp_adapter_create(med_endpt, NULL, udp, p_tp);
4516 if (status != PJ_SUCCESS) {
4517 pjmedia_transport_close(udp);
4518 return status;
4519 }
4520
4521 return PJ_SUCCESS;
4522}
4523
4524static pj_status_t transport_adapter_sample(void)
4525{
4526 pjsua_media_transport tp[PJSUA_MAX_CALLS];
4527 pj_status_t status;
4528 int port = 7000;
4529 unsigned i;
4530
4531 for (i=0; i<app_config.cfg.max_calls; ++i) {
4532 status = create_transport_adapter(pjsua_get_pjmedia_endpt(),
4533 port + i*10,
4534 &tp[i].transport);
4535 if (status != PJ_SUCCESS)
4536 return status;
4537 }
4538
4539 return pjsua_media_transports_attach(tp, i, PJ_TRUE);
4540}
4541#endif
4542