blob: b3b5a98d1ce0188a26928f3f4cbf9708c8503a50 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20
21
Benny Prijono4ab9fbb2007-10-12 12:14:27 +000022#define THIS_FILE "pjsua_app.c"
Benny Prijono804ff0a2006-09-14 11:17:48 +000023#define NO_LIMIT (int)0x7FFFFFFF
Benny Prijonoeebe9af2006-06-13 22:57:13 +000024
Benny Prijonoe909eac2006-07-27 22:04:56 +000025//#define STEREO_DEMO
Benny Prijono40860c32008-09-04 13:55:33 +000026//#define TRANSPORT_ADAPTER_SAMPLE
Benny Prijonoeebe9af2006-06-13 22:57:13 +000027
Benny Prijono4f966892008-06-14 22:43:56 +000028/* Ringtones US UK */
29#define RINGBACK_FREQ1 440 /* 400 */
30#define RINGBACK_FREQ2 480 /* 450 */
31#define RINGBACK_ON 2000 /* 400 */
32#define RINGBACK_OFF 4000 /* 200 */
33#define RINGBACK_CNT 1 /* 2 */
34#define RINGBACK_INTERVAL 4000 /* 2000 */
Benny Prijono91d20f42008-06-14 19:42:37 +000035
Benny Prijono4f966892008-06-14 22:43:56 +000036#define RING_FREQ1 800
37#define RING_FREQ2 640
38#define RING_ON 200
39#define RING_OFF 100
40#define RING_CNT 3
41#define RING_INTERVAL 3000
Benny Prijono91d20f42008-06-14 19:42:37 +000042
43
Benny Prijono804ff0a2006-09-14 11:17:48 +000044/* Call specific data */
45struct call_data
46{
47 pj_timer_entry timer;
Benny Prijono91d20f42008-06-14 19:42:37 +000048 pj_bool_t ringback_on;
49 pj_bool_t ring_on;
Benny Prijono804ff0a2006-09-14 11:17:48 +000050};
51
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052
53/* Pjsua application data */
54static struct app_config
55{
56 pjsua_config cfg;
57 pjsua_logging_config log_cfg;
58 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000059 pj_bool_t no_refersub;
Benny Prijonoe93e2872006-06-28 16:46:49 +000060 pj_bool_t no_tcp;
61 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000062 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000063 pjsua_transport_config udp_cfg;
64 pjsua_transport_config rtp_cfg;
Benny Prijono5e51a4e2008-11-27 00:06:46 +000065 pjsip_redirect_op redir_op;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000066
67 unsigned acc_cnt;
68 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
69
70 unsigned buddy_cnt;
71 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
72
Benny Prijono804ff0a2006-09-14 11:17:48 +000073 struct call_data call_data[PJSUA_MAX_CALLS];
74
Benny Prijonoeebe9af2006-06-13 22:57:13 +000075 pj_pool_t *pool;
76 /* Compatibility with older pjsua */
77
78 unsigned codec_cnt;
79 pj_str_t codec_arg[32];
Benny Prijonofce28542007-12-09 15:41:10 +000080 unsigned codec_dis_cnt;
81 pj_str_t codec_dis[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000082 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000083 unsigned wav_count;
84 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000085 unsigned tone_count;
86 pjmedia_tone_desc tones[32];
87 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000088 pjsua_player_id wav_id;
89 pjsua_conf_port_id wav_port;
90 pj_bool_t auto_play;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +000091 pj_bool_t auto_play_hangup;
92 pj_timer_entry auto_hangup_timer;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000093 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000094 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000095 pj_str_t rec_file;
96 pj_bool_t auto_rec;
97 pjsua_recorder_id rec_id;
98 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000099 unsigned auto_answer;
100 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +0000101
102#ifdef STEREO_DEMO
103 pjmedia_snd_port *snd;
104#endif
105
Benny Prijono6dd967c2006-12-26 02:27:14 +0000106 float mic_level,
107 speaker_level;
108
Benny Prijono4e5d5512007-03-06 18:11:30 +0000109 int capture_dev, playback_dev;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000110 unsigned capture_lat, playback_lat;
Benny Prijono91d20f42008-06-14 19:42:37 +0000111
112 pj_bool_t no_tones;
113 int ringback_slot;
114 int ringback_cnt;
115 pjmedia_port *ringback_port;
116 int ring_slot;
117 int ring_cnt;
118 pjmedia_port *ring_port;
119
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000120} app_config;
121
122
Benny Prijono21b9ad92006-08-15 13:11:22 +0000123//static pjsua_acc_id current_acc;
124#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +0000125static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoebc32c32008-06-12 13:30:39 +0000126static pj_bool_t cmd_echo;
127static int stdout_refresh = -1;
128static const char *stdout_refresh_text = "STDOUT_REFRESH";
129static pj_bool_t stdout_refresh_quit = PJ_FALSE;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000130static pj_str_t uri_arg;
131
Nanang Izzuddin660eee82008-07-17 14:54:03 +0000132static char some_buf[1024 * 3];
Benny Prijono6eddd872008-03-21 13:46:08 +0000133
Benny Prijono594e4c52006-09-14 18:51:01 +0000134#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +0000135static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +0000136#endif
Benny Prijono40860c32008-09-04 13:55:33 +0000137#ifdef TRANSPORT_ADAPTER_SAMPLE
138static pj_status_t transport_adapter_sample(void);
139#endif
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000140pj_status_t app_destroy(void);
Benny Prijonoe909eac2006-07-27 22:04:56 +0000141
Benny Prijono91d20f42008-06-14 19:42:37 +0000142static void ringback_start(pjsua_call_id call_id);
143static void ring_start(pjsua_call_id call_id);
144static void ring_stop(pjsua_call_id call_id);
145
146
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000147/*****************************************************************************
148 * Configuration manipulation
149 */
150
151/* Show usage */
152static void usage(void)
153{
154 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000155 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000156 puts ("");
157 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000158 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000159 puts (" --help Display this help screen");
160 puts (" --version Display version info");
161 puts ("");
162 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000163 puts (" --log-file=fname Log to filename (default stderr)");
164 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
165 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000166 puts (" --color Use colorful logging (default yes on Win32)");
167 puts (" --no-color Disable colorful logging");
Benny Prijonob7944862008-07-19 20:53:49 +0000168 puts (" --light-bg Use dark colors for light background (default is dark bg)");
Benny Prijonod6e362a2008-07-19 17:53:47 +0000169
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000170 puts ("");
171 puts ("SIP Account options:");
Benny Prijono48ab2b72007-11-08 09:24:30 +0000172 puts (" --use-ims Enable 3GPP/IMS related settings on this account");
Benny Prijonod8179652008-01-23 20:39:07 +0000173#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijonof6508982008-01-25 09:02:33 +0000174 puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory (def:0)");
175 puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 1:sips (def:1)");
Benny Prijonod8179652008-01-23 20:39:07 +0000176#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000177 puts (" --registrar=url Set the URL of registrar server");
178 puts (" --id=url Set the URL of local ID (used in From header)");
179 puts (" --contact=url Optionally override the Contact information");
180 puts (" --proxy=url Optional URL of proxy server to visit");
181 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000182 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000183 puts (" --realm=string Set realm");
184 puts (" --username=string Set authentication username");
185 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000186 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000187 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000188 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
189 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000190 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000191 puts ("");
192 puts ("SIP Account Control:");
193 puts (" --next-account Add more account");
194 puts ("");
195 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000196 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
197 puts (" TCP and UDP transports on the specified port, unless");
198 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000199 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
200 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000201 puts (" --no-tcp Disable TCP transport.");
202 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000203 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
204 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000205 puts (" --outbound=url Set the URL of global outbound proxy server");
206 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000207 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000208 puts ("");
209 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000210 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000211 puts (" --tls-ca-file Specify TLS CA file (default=none)");
212 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
213 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
214 puts (" --tls-password Specify TLS password to private key file (default=none)");
215 puts (" --tls-verify-server Verify server's certificate (default=no)");
216 puts (" --tls-verify-client Verify client's certificate (default=no)");
217 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
Benny Prijonoe10db842008-07-01 15:31:59 +0000218 puts (" --tls-srv-name Specify TLS server name for multi-hosting server (optional)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000219
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000220 puts ("");
221 puts ("Media Options:");
222 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000223 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonof3758ee2008-02-26 15:32:16 +0000224 puts (" --clock-rate=N Override conference bridge clock rate");
225 puts (" --snd-clock-rate=N Override sound device clock rate");
Benny Prijono7d60d052008-03-29 12:24:20 +0000226 puts (" --stereo Audio device and conference bridge opened in stereo mode");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000227 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000228 puts (" --play-file=file Register WAV file in conference bridge.");
229 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000230 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
231 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
232 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000233 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000234 puts (" --auto-play Automatically play the file (to incoming calls only)");
235 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000236 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000237 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
238 puts (" --auto-rec Automatically record conversation");
Benny Prijono00cae612006-07-31 15:19:36 +0000239 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000241 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000242 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000243 puts (" --ec-opt=OPT Select echo canceller algorithm (0=default, ");
244 puts (" 1=speex, 2=suppressor)");
Benny Prijono00cae612006-07-31 15:19:36 +0000245 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000246 puts (" --capture-dev=id Audio capture device ID (default=-1)");
247 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +0000248 puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000249 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000250 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
251 puts (" Specify N=-1 (default) to disable this feature.");
252 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000253 puts (" --no-tones Disable audible tones");
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000254 puts (" --jb-max-size Specify jitter buffer maximum size, in frames (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000255
Benny Prijonof76e1392008-06-06 14:51:48 +0000256 puts ("");
257 puts ("Media Transport Options:");
258 puts (" --use-ice Enable ICE (default:no)");
Benny Prijono551af422008-08-07 09:55:52 +0000259 puts (" --ice-no-host Disable ICE host candidates (default: no)");
260 puts (" --ice-no-rtcp Disable RTCP component in ICE (default: no)");
Benny Prijonof76e1392008-06-06 14:51:48 +0000261 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
262 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
263 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
264 puts (" --use-turn Enable TURN relay with ICE (default:no)");
265 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
266 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
267 puts (" --turn-user TURN username");
268 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000269
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270 puts ("");
271 puts ("Buddy List (can be more than one):");
272 puts (" --add-buddy url Add the specified URL to the buddy list.");
273 puts ("");
274 puts ("User Agent options:");
275 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
276 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000277 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000278 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000279 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000280 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono91d06b62008-09-20 12:16:56 +0000281 puts (" --no-force-lr Allow strict-route to be used (i.e. do not force lr)");
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000282 puts (" --accept-redirect=N Specify how to handle call redirect (3xx) response.");
283 puts (" 0: reject, 1: follow automatically (default), 2: ask");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000284
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000285 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000286 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
287 puts ("");
288
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000289 fflush(stdout);
290}
291
292
293/* Set default config. */
294static void default_config(struct app_config *cfg)
295{
Benny Prijono56315612006-07-18 14:39:40 +0000296 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000297 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000298
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000299 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000300 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000301 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
302
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000303 pjsua_logging_config_default(&cfg->log_cfg);
304 pjsua_media_config_default(&cfg->media_cfg);
305 pjsua_transport_config_default(&cfg->udp_cfg);
306 cfg->udp_cfg.port = 5060;
307 pjsua_transport_config_default(&cfg->rtp_cfg);
308 cfg->rtp_cfg.port = 4000;
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000309 cfg->redir_op = PJSIP_REDIRECT_ACCEPT;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000310 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000311 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000312 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000313 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000314 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000315 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000316 cfg->capture_dev = PJSUA_INVALID_ID;
317 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000318 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
319 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000320 cfg->ringback_slot = PJSUA_INVALID_ID;
321 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000322
323 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
324 pjsua_acc_config_default(&cfg->acc_cfg[i]);
325
326 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
327 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000328}
329
330
331/*
332 * Read command arguments from config file.
333 */
334static int read_config_file(pj_pool_t *pool, const char *filename,
335 int *app_argc, char ***app_argv)
336{
337 int i;
338 FILE *fhnd;
339 char line[200];
340 int argc = 0;
341 char **argv;
342 enum { MAX_ARGS = 64 };
343
344 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
345 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
346 argv[argc++] = *app_argv[0];
347
348 /* Open config file. */
349 fhnd = fopen(filename, "rt");
350 if (!fhnd) {
351 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
352 fflush(stdout);
353 return -1;
354 }
355
356 /* Scan tokens in the file. */
357 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000358 char *token;
359 char *p;
360 const char *whitespace = " \t\r\n";
361 char cDelimiter;
362 int len, token_len;
363
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000364 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000365
366 // Trim ending newlines
367 len = strlen(line);
368 if (line[len-1]=='\n')
369 line[--len] = '\0';
370 if (line[len-1]=='\r')
371 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372
Benny Prijonobf5b4692007-06-28 03:20:17 +0000373 if (len==0) continue;
374
375 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
376 // first, scan whitespaces
377 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
378
379 if (*p == '\0') // are we done yet?
380 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381
Benny Prijonobf5b4692007-06-28 03:20:17 +0000382 if (*p == '"' || *p == '\'') { // is token a quoted string
383 cDelimiter = *p++; // save quote delimiter
384 token = p;
385
386 while (*p != '\0' && *p != cDelimiter) p++;
387
388 if (*p == '\0') // found end of the line, but,
389 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390
Benny Prijonobf5b4692007-06-28 03:20:17 +0000391 } else { // token's not a quoted string
392 token = p;
393
394 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
395
396 cDelimiter = *p;
397 }
398
399 *p = '\0';
400 token_len = p-token;
401
402 if (token_len > 0) {
403 if (*token == '#')
404 break; // ignore remainder of line
405
406 argv[argc] = pj_pool_alloc(pool, token_len + 1);
407 pj_memcpy(argv[argc], token, token_len + 1);
408 ++argc;
409 }
410
411 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000412 }
413 }
414
415 /* Copy arguments from command line */
416 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
417 argv[argc++] = (*app_argv)[i];
418
419 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
420 PJ_LOG(1,(THIS_FILE,
421 "Too many arguments specified in cmd line/config file"));
422 fflush(stdout);
423 fclose(fhnd);
424 return -1;
425 }
426
427 fclose(fhnd);
428
429 /* Assign the new command line back to the original command line. */
430 *app_argc = argc;
431 *app_argv = argv;
432 return 0;
433
434}
435
436static int my_atoi(const char *cs)
437{
438 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000439
440 pj_cstr(&s, cs);
441 if (cs[0] == '-') {
442 s.ptr++, s.slen--;
443 return 0 - (int)pj_strtoul(&s);
444 } else if (cs[0] == '+') {
445 s.ptr++, s.slen--;
446 return pj_strtoul(&s);
447 } else {
448 return pj_strtoul(&s);
449 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000450}
451
452
453/* Parse arguments. */
454static pj_status_t parse_args(int argc, char *argv[],
455 struct app_config *cfg,
456 pj_str_t *uri_to_call)
457{
458 int c;
459 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000460 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonob7944862008-07-19 20:53:49 +0000461 OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000462 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000463 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
464 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000465 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000466 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000467 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000468 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000469 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
470 OPT_USE_ICE, OPT_USE_SRTP, OPT_SRTP_SECURE,
Benny Prijono551af422008-08-07 09:55:52 +0000471 OPT_USE_TURN, OPT_ICE_NO_HOST, OPT_ICE_NO_RTCP, OPT_TURN_SRV,
472 OPT_TURN_TCP, OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000473 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
474 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000475 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000476 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL, OPT_EC_OPT,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000477 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000478 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000479 OPT_NOREFERSUB, OPT_ACCEPT_REDIRECT,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000480 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
481 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
Benny Prijonoe10db842008-07-01 15:31:59 +0000482 OPT_TLS_NEG_TIMEOUT, OPT_TLS_SRV_NAME,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000483 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000484 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES, OPT_JB_MAX_SIZE,
Benny Prijonoebc32c32008-06-12 13:30:39 +0000485 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000486#ifdef _IONBF
487 OPT_STDOUT_NO_BUF,
488#endif
Benny Prijono91d06b62008-09-20 12:16:56 +0000489 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC,
490 OPT_NO_FORCE_LR
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000491 };
492 struct pj_getopt_option long_options[] = {
493 { "config-file",1, 0, OPT_CONFIG_FILE},
494 { "log-file", 1, 0, OPT_LOG_FILE},
495 { "log-level", 1, 0, OPT_LOG_LEVEL},
496 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
Benny Prijonod6e362a2008-07-19 17:53:47 +0000497 { "color", 0, 0, OPT_COLOR},
498 { "no-color", 0, 0, OPT_NO_COLOR},
Benny Prijonob7944862008-07-19 20:53:49 +0000499 { "light-bg", 0, 0, OPT_LIGHT_BG},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000500 { "help", 0, 0, OPT_HELP},
501 { "version", 0, 0, OPT_VERSION},
502 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000503 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000504 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505 { "null-audio", 0, 0, OPT_NULL_AUDIO},
506 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000507 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000508 { "no-tcp", 0, 0, OPT_NO_TCP},
509 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000510 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511 { "proxy", 1, 0, OPT_PROXY},
512 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
513 { "registrar", 1, 0, OPT_REGISTRAR},
514 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000515 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000516 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000517 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000518 { "id", 1, 0, OPT_ID},
519 { "contact", 1, 0, OPT_CONTACT},
Benny Prijonofce28542007-12-09 15:41:10 +0000520 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
521 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000522 { "accept-redirect", 1, 0, OPT_ACCEPT_REDIRECT},
Benny Prijono91d06b62008-09-20 12:16:56 +0000523 { "no-force-lr",0, 0, OPT_NO_FORCE_LR},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000524 { "realm", 1, 0, OPT_REALM},
525 { "username", 1, 0, OPT_USERNAME},
526 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000527 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000528 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000529 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000530 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
531 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
532 { "no-presence", 0, 0, OPT_NO_PRESENCE},
533 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000535 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000536 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
538 { "auto-conf", 0, 0, OPT_AUTO_CONF},
539 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000540 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000541 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000543
Benny Prijonoc97608e2007-03-23 16:34:20 +0000544 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000545 { "use-turn", 0, 0, OPT_USE_TURN},
546 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
Benny Prijono551af422008-08-07 09:55:52 +0000547 { "ice-no-rtcp",0, 0, OPT_ICE_NO_RTCP},
Benny Prijonof76e1392008-06-06 14:51:48 +0000548 { "turn-srv", 1, 0, OPT_TURN_SRV},
549 { "turn-tcp", 0, 0, OPT_TURN_TCP},
550 { "turn-user", 1, 0, OPT_TURN_USER},
551 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
552
Benny Prijonod8179652008-01-23 20:39:07 +0000553#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
554 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000555 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000556#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000557 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000558 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559 { "complexity", 1, 0, OPT_COMPLEXITY},
560 { "quality", 1, 0, OPT_QUALITY},
561 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000562 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000563 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000564 { "ec-opt", 1, 0, OPT_EC_OPT},
Benny Prijono00cae612006-07-31 15:19:36 +0000565 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
566 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
567 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000568 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000569 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000570 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000571 { "duration", 1, 0, OPT_DURATION},
572 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000573 { "use-tls", 0, 0, OPT_USE_TLS},
574 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000575 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
576 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000577 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000578 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
579 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
580 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijonoe10db842008-07-01 15:31:59 +0000581 { "tls-srv-name", 1, 0, OPT_TLS_SRV_NAME},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000582 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
583 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000584 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
585 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000586 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
587 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000588#ifdef _IONBF
589 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
590#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000591 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000592 { "no-tones", 0, 0, OPT_NO_TONES},
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000593 { "jb-max-size", 1, 0, OPT_JB_MAX_SIZE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000594 { NULL, 0, 0, 0}
595 };
596 pj_status_t status;
597 pjsua_acc_config *cur_acc;
598 char *config_file = NULL;
599 unsigned i;
600
601 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000602 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000603 while ((c=pj_getopt_long(argc, argv, "", long_options,
604 &option_index)) != -1)
605 {
606 switch (c) {
607 case OPT_CONFIG_FILE:
608 config_file = pj_optarg;
609 break;
610 }
611 if (config_file)
612 break;
613 }
614
615 if (config_file) {
616 status = read_config_file(app_config.pool, config_file, &argc, &argv);
617 if (status != 0)
618 return status;
619 }
620
621 cfg->acc_cnt = 0;
622 cur_acc = &cfg->acc_cfg[0];
623
624
625 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
626 * read from config file.
627 */
628 pj_optind = 0;
629 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000630 pj_str_t tmp;
631 long lval;
632
633 switch (c) {
634
Benny Prijono6f137482006-06-15 11:31:36 +0000635 case OPT_CONFIG_FILE:
636 /* Ignore as this has been processed before */
637 break;
638
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000639 case OPT_LOG_FILE:
640 cfg->log_cfg.log_filename = pj_str(pj_optarg);
641 break;
642
643 case OPT_LOG_LEVEL:
644 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
645 if (c < 0 || c > 6) {
646 PJ_LOG(1,(THIS_FILE,
647 "Error: expecting integer value 0-6 "
648 "for --log-level"));
649 return PJ_EINVAL;
650 }
651 cfg->log_cfg.level = c;
652 pj_log_set_level( c );
653 break;
654
655 case OPT_APP_LOG_LEVEL:
656 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
657 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
658 PJ_LOG(1,(THIS_FILE,
659 "Error: expecting integer value 0-6 "
660 "for --app-log-level"));
661 return PJ_EINVAL;
662 }
663 break;
664
Benny Prijonod6e362a2008-07-19 17:53:47 +0000665 case OPT_COLOR:
666 cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR;
667 break;
668
669 case OPT_NO_COLOR:
670 cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR;
671 break;
672
Benny Prijonob7944862008-07-19 20:53:49 +0000673 case OPT_LIGHT_BG:
674 pj_log_set_color(1, PJ_TERM_COLOR_R);
675 pj_log_set_color(2, PJ_TERM_COLOR_R | PJ_TERM_COLOR_G);
676 pj_log_set_color(3, PJ_TERM_COLOR_B | PJ_TERM_COLOR_G);
677 pj_log_set_color(4, 0);
678 pj_log_set_color(5, 0);
679 pj_log_set_color(77, 0);
680 break;
681
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000682 case OPT_HELP:
683 usage();
684 return PJ_EINVAL;
685
686 case OPT_VERSION: /* version */
687 pj_dump_config();
688 return PJ_EINVAL;
689
690 case OPT_NULL_AUDIO:
691 cfg->null_audio = PJ_TRUE;
692 break;
693
694 case OPT_CLOCK_RATE:
695 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000696 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000697 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000698 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000699 return PJ_EINVAL;
700 }
701 cfg->media_cfg.clock_rate = lval;
702 break;
703
Benny Prijonof3758ee2008-02-26 15:32:16 +0000704 case OPT_SND_CLOCK_RATE:
705 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000706 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000707 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000708 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000709 return PJ_EINVAL;
710 }
711 cfg->media_cfg.snd_clock_rate = lval;
712 break;
713
Benny Prijono7d60d052008-03-29 12:24:20 +0000714 case OPT_STEREO:
715 cfg->media_cfg.channel_count = 2;
716 break;
717
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000718 case OPT_LOCAL_PORT: /* local-port */
719 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000720 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000721 PJ_LOG(1,(THIS_FILE,
722 "Error: expecting integer value for "
723 "--local-port"));
724 return PJ_EINVAL;
725 }
726 cfg->udp_cfg.port = (pj_uint16_t)lval;
727 break;
728
Benny Prijono0a5cad82006-09-26 13:21:02 +0000729 case OPT_IP_ADDR: /* ip-addr */
730 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
731 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
732 break;
733
Benny Prijonoe93e2872006-06-28 16:46:49 +0000734 case OPT_NO_UDP: /* no-udp */
735 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000736 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
737 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000738 }
739
740 cfg->no_udp = PJ_TRUE;
741 break;
742
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000743 case OPT_NOREFERSUB: /* norefersub */
744 cfg->no_refersub = PJ_TRUE;
745 break;
746
Benny Prijonoe93e2872006-06-28 16:46:49 +0000747 case OPT_NO_TCP: /* no-tcp */
748 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000749 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
750 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000751 }
752
753 cfg->no_tcp = PJ_TRUE;
754 break;
755
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000756 case OPT_PROXY: /* proxy */
757 if (pjsua_verify_sip_url(pj_optarg) != 0) {
758 PJ_LOG(1,(THIS_FILE,
759 "Error: invalid SIP URL '%s' "
760 "in proxy argument", pj_optarg));
761 return PJ_EINVAL;
762 }
763 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
764 break;
765
766 case OPT_OUTBOUND_PROXY: /* outbound proxy */
767 if (pjsua_verify_sip_url(pj_optarg) != 0) {
768 PJ_LOG(1,(THIS_FILE,
769 "Error: invalid SIP URL '%s' "
770 "in outbound proxy argument", pj_optarg));
771 return PJ_EINVAL;
772 }
773 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
774 break;
775
776 case OPT_REGISTRAR: /* registrar */
777 if (pjsua_verify_sip_url(pj_optarg) != 0) {
778 PJ_LOG(1,(THIS_FILE,
779 "Error: invalid SIP URL '%s' in "
780 "registrar argument", pj_optarg));
781 return PJ_EINVAL;
782 }
783 cur_acc->reg_uri = pj_str(pj_optarg);
784 break;
785
786 case OPT_REG_TIMEOUT: /* reg-timeout */
787 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
788 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
789 PJ_LOG(1,(THIS_FILE,
790 "Error: invalid value for --reg-timeout "
791 "(expecting 1-3600)"));
792 return PJ_EINVAL;
793 }
794 break;
795
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000796 case OPT_PUBLISH: /* publish */
797 cur_acc->publish_enabled = PJ_TRUE;
798 break;
799
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000800 case OPT_100REL: /** 100rel */
801 cur_acc->require_100rel = PJ_TRUE;
802 cfg->cfg.require_100rel = PJ_TRUE;
803 break;
804
Benny Prijono48ab2b72007-11-08 09:24:30 +0000805 case OPT_USE_IMS: /* Activate IMS settings */
806 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000807 break;
808
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000809 case OPT_ID: /* id */
810 if (pjsua_verify_sip_url(pj_optarg) != 0) {
811 PJ_LOG(1,(THIS_FILE,
812 "Error: invalid SIP URL '%s' "
813 "in local id argument", pj_optarg));
814 return PJ_EINVAL;
815 }
816 cur_acc->id = pj_str(pj_optarg);
817 break;
818
819 case OPT_CONTACT: /* contact */
820 if (pjsua_verify_sip_url(pj_optarg) != 0) {
821 PJ_LOG(1,(THIS_FILE,
822 "Error: invalid SIP URL '%s' "
823 "in contact argument", pj_optarg));
824 return PJ_EINVAL;
825 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000826 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000827 break;
828
Benny Prijonofce28542007-12-09 15:41:10 +0000829 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000830 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000831 break;
832
833 case OPT_USE_COMPACT_FORM:
834 /* enable compact form - from Ticket #342 */
835 {
836 extern pj_bool_t pjsip_use_compact_form;
837 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
838 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
839
840 pjsip_use_compact_form = PJ_TRUE;
841 /* do not transmit Allow header */
842 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
843 /* Do not include rtpmap for static payload types (<96) */
844 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
845 }
846 break;
847
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000848 case OPT_ACCEPT_REDIRECT:
849 cfg->redir_op = my_atoi(pj_optarg);
850 if (cfg->redir_op<0 || cfg->redir_op>PJSIP_REDIRECT_STOP) {
851 PJ_LOG(1,(THIS_FILE,
852 "Error: accept-redirect value '%s' ", pj_optarg));
853 return PJ_EINVAL;
854 }
855 break;
856
Benny Prijono91d06b62008-09-20 12:16:56 +0000857 case OPT_NO_FORCE_LR:
858 cfg->cfg.force_lr = PJ_FALSE;
859 break;
860
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000861 case OPT_NEXT_ACCOUNT: /* Add more account. */
862 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000863 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000864 break;
865
866 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000867 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000868 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000869 break;
870
871 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000872 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000873 break;
874
875 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000876 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
877 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000878#if PJSIP_HAS_DIGEST_AKA_AUTH
879 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
880 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
881 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
882#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000883 break;
884
885 case OPT_NEXT_CRED: /* next credential */
886 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000887 break;
888
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000889 case OPT_NAMESERVER: /* nameserver */
890 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
891 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
892 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
893 return PJ_ETOOMANY;
894 }
895 break;
896
Benny Prijonoebbf6892007-03-24 17:37:25 +0000897 case OPT_STUN_DOMAIN: /* STUN domain */
898 cfg->cfg.stun_domain = pj_str(pj_optarg);
899 break;
900
Benny Prijonoc97608e2007-03-23 16:34:20 +0000901 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000902 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000903 break;
904
905 case OPT_ADD_BUDDY: /* Add to buddy list. */
906 if (pjsua_verify_sip_url(pj_optarg) != 0) {
907 PJ_LOG(1,(THIS_FILE,
908 "Error: invalid URL '%s' in "
909 "--add-buddy option", pj_optarg));
910 return -1;
911 }
912 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
913 PJ_LOG(1,(THIS_FILE,
914 "Error: too many buddies in buddy list."));
915 return -1;
916 }
917 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
918 cfg->buddy_cnt++;
919 break;
920
921 case OPT_AUTO_PLAY:
922 cfg->auto_play = 1;
923 break;
924
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000925 case OPT_AUTO_PLAY_HANGUP:
926 cfg->auto_play_hangup = 1;
927 break;
928
Benny Prijono1ebd6142006-10-19 15:48:02 +0000929 case OPT_AUTO_REC:
930 cfg->auto_rec = 1;
931 break;
932
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000933 case OPT_AUTO_LOOP:
934 cfg->auto_loop = 1;
935 break;
936
Benny Prijono7ca96da2006-08-07 12:11:40 +0000937 case OPT_AUTO_CONF:
938 cfg->auto_conf = 1;
939 break;
940
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000941 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000942 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000943 break;
944
Benny Prijono4af234b2007-01-24 02:02:09 +0000945 case OPT_PLAY_TONE:
946 {
947 int f1, f2, on, off;
948 int n;
949
950 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
951 if (n != 4) {
952 puts("Expecting f1,f2,on,off in --play-tone");
953 return -1;
954 }
955
956 cfg->tones[cfg->tone_count].freq1 = (short)f1;
957 cfg->tones[cfg->tone_count].freq2 = (short)f2;
958 cfg->tones[cfg->tone_count].on_msec = (short)on;
959 cfg->tones[cfg->tone_count].off_msec = (short)off;
960 ++cfg->tone_count;
961 }
962 break;
963
Benny Prijono1ebd6142006-10-19 15:48:02 +0000964 case OPT_REC_FILE:
965 cfg->rec_file = pj_str(pj_optarg);
966 break;
967
Benny Prijonoc97608e2007-03-23 16:34:20 +0000968 case OPT_USE_ICE:
969 cfg->media_cfg.enable_ice = PJ_TRUE;
970 break;
971
Benny Prijonof76e1392008-06-06 14:51:48 +0000972 case OPT_USE_TURN:
973 cfg->media_cfg.enable_turn = PJ_TRUE;
974 break;
975
976 case OPT_ICE_NO_HOST:
977 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
978 break;
979
Benny Prijono551af422008-08-07 09:55:52 +0000980 case OPT_ICE_NO_RTCP:
981 cfg->media_cfg.ice_no_rtcp = PJ_TRUE;
982 break;
983
Benny Prijonof76e1392008-06-06 14:51:48 +0000984 case OPT_TURN_SRV:
985 cfg->media_cfg.turn_server = pj_str(pj_optarg);
986 break;
987
988 case OPT_TURN_TCP:
989 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
990 break;
991
992 case OPT_TURN_USER:
993 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
994 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
995 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
996 break;
997
998 case OPT_TURN_PASSWD:
999 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1000 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
1001 break;
1002
Benny Prijonod8179652008-01-23 20:39:07 +00001003#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1004 case OPT_USE_SRTP:
1005 app_config.cfg.use_srtp = my_atoi(pj_optarg);
1006 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
1007 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
1008 return -1;
1009 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001010 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +00001011 break;
Benny Prijonof6508982008-01-25 09:02:33 +00001012 case OPT_SRTP_SECURE:
1013 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
1014 if (!pj_isdigit(*pj_optarg) ||
1015 app_config.cfg.srtp_secure_signaling > 2)
1016 {
1017 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
1018 return -1;
1019 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001020 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +00001021 break;
Benny Prijonod8179652008-01-23 20:39:07 +00001022#endif
1023
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001024 case OPT_RTP_PORT:
1025 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +00001026 if (cfg->rtp_cfg.port == 0) {
1027 enum { START_PORT=4000 };
1028 unsigned range;
1029
1030 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
1031 cfg->rtp_cfg.port = START_PORT +
1032 ((pj_rand() % range) & 0xFFFE);
1033 }
1034
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001035 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
1036 PJ_LOG(1,(THIS_FILE,
1037 "Error: rtp-port argument value "
1038 "(expecting 1-65535"));
1039 return -1;
1040 }
1041 break;
1042
Benny Prijonofce28542007-12-09 15:41:10 +00001043 case OPT_DIS_CODEC:
1044 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
1045 break;
1046
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001047 case OPT_ADD_CODEC:
1048 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
1049 break;
1050
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001051 /* These options were no longer valid after new pjsua */
1052 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001053 case OPT_COMPLEXITY:
1054 cfg->complexity = my_atoi(pj_optarg);
1055 if (cfg->complexity < 0 || cfg->complexity > 10) {
1056 PJ_LOG(1,(THIS_FILE,
1057 "Error: invalid --complexity (expecting 0-10"));
1058 return -1;
1059 }
1060 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +00001061 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001062
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001063 case OPT_DURATION:
1064 cfg->duration = my_atoi(pj_optarg);
1065 break;
1066
Benny Prijonof521eb02006-08-06 23:07:25 +00001067 case OPT_THREAD_CNT:
1068 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1069 if (cfg->cfg.thread_cnt > 128) {
1070 PJ_LOG(1,(THIS_FILE,
1071 "Error: invalid --thread-cnt option"));
1072 return -1;
1073 }
1074 break;
1075
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001076 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001077 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1078 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001079 PJ_LOG(1,(THIS_FILE,
1080 "Error: invalid --ptime option"));
1081 return -1;
1082 }
1083 break;
1084
Benny Prijono0a12f002006-07-26 17:05:39 +00001085 case OPT_NO_VAD:
1086 cfg->media_cfg.no_vad = PJ_TRUE;
1087 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001088
Benny Prijonod79f25c2006-08-02 19:41:37 +00001089 case OPT_EC_TAIL:
1090 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1091 if (cfg->media_cfg.ec_tail_len > 1000) {
1092 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1093 "is too big"));
1094 return -1;
1095 }
1096 break;
1097
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001098 case OPT_EC_OPT:
1099 cfg->media_cfg.ec_options = my_atoi(pj_optarg);
1100 break;
1101
Benny Prijono0498d902006-06-19 14:49:14 +00001102 case OPT_QUALITY:
1103 cfg->media_cfg.quality = my_atoi(pj_optarg);
1104 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1105 PJ_LOG(1,(THIS_FILE,
1106 "Error: invalid --quality (expecting 0-10"));
1107 return -1;
1108 }
1109 break;
1110
Benny Prijono00cae612006-07-31 15:19:36 +00001111 case OPT_ILBC_MODE:
1112 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1113 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1114 PJ_LOG(1,(THIS_FILE,
1115 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1116 return -1;
1117 }
1118 break;
1119
1120 case OPT_RX_DROP_PCT:
1121 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1122 if (cfg->media_cfg.rx_drop_pct > 100) {
1123 PJ_LOG(1,(THIS_FILE,
1124 "Error: invalid --rx-drop-pct (expecting <= 100"));
1125 return -1;
1126 }
1127 break;
1128
1129 case OPT_TX_DROP_PCT:
1130 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1131 if (cfg->media_cfg.tx_drop_pct > 100) {
1132 PJ_LOG(1,(THIS_FILE,
1133 "Error: invalid --tx-drop-pct (expecting <= 100"));
1134 return -1;
1135 }
1136 break;
1137
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001138 case OPT_AUTO_ANSWER:
1139 cfg->auto_answer = my_atoi(pj_optarg);
1140 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1141 PJ_LOG(1,(THIS_FILE,
1142 "Error: invalid code in --auto-answer "
1143 "(expecting 100-699"));
1144 return -1;
1145 }
1146 break;
1147
1148 case OPT_MAX_CALLS:
1149 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001150 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001151 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1152 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001153 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001154 return -1;
1155 }
1156 break;
1157
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001158 case OPT_USE_TLS:
1159 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001160#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1161 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1162 return -1;
1163#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001164 break;
1165
1166 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001167 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1168#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
Benny Prijonof3bbc132006-12-25 06:43:59 +00001174 case OPT_TLS_CERT_FILE:
1175 cfg->udp_cfg.tls_setting.cert_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_PRIV_FILE:
1183 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1184 break;
1185
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001186 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001187 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1188#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1189 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1190 return -1;
1191#endif
1192 break;
1193
1194 case OPT_TLS_VERIFY_SERVER:
1195 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1196 break;
1197
1198 case OPT_TLS_VERIFY_CLIENT:
1199 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1200 break;
1201
1202 case OPT_TLS_NEG_TIMEOUT:
1203 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001204 break;
1205
Benny Prijonoe10db842008-07-01 15:31:59 +00001206 case OPT_TLS_SRV_NAME:
1207 cfg->udp_cfg.tls_setting.server_name = pj_str(pj_optarg);
1208 break;
1209
Benny Prijono4e5d5512007-03-06 18:11:30 +00001210 case OPT_CAPTURE_DEV:
1211 cfg->capture_dev = atoi(pj_optarg);
1212 break;
1213
1214 case OPT_PLAYBACK_DEV:
1215 cfg->playback_dev = atoi(pj_optarg);
1216 break;
1217
Benny Prijonoebc32c32008-06-12 13:30:39 +00001218 case OPT_STDOUT_REFRESH:
1219 stdout_refresh = atoi(pj_optarg);
1220 break;
1221
1222 case OPT_STDOUT_REFRESH_TEXT:
1223 stdout_refresh_text = pj_optarg;
1224 break;
1225
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001226#ifdef _IONBF
1227 case OPT_STDOUT_NO_BUF:
1228 setvbuf(stdout, NULL, _IONBF, 0);
1229 break;
1230#endif
1231
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001232 case OPT_CAPTURE_LAT:
1233 cfg->capture_lat = atoi(pj_optarg);
1234 break;
1235
1236 case OPT_PLAYBACK_LAT:
1237 cfg->playback_lat = atoi(pj_optarg);
1238 break;
1239
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001240 case OPT_SND_AUTO_CLOSE:
1241 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1242 break;
1243
Benny Prijono91d20f42008-06-14 19:42:37 +00001244 case OPT_NO_TONES:
1245 cfg->no_tones = PJ_TRUE;
1246 break;
1247
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001248 case OPT_JB_MAX_SIZE:
1249 cfg->media_cfg.jb_max = atoi(pj_optarg);
1250 break;
1251
Benny Prijono22a300a2006-06-14 20:04:55 +00001252 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001253 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001254 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001255 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001256 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001257 }
1258 }
1259
1260 if (pj_optind != argc) {
1261 pj_str_t uri_arg;
1262
1263 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1264 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1265 return -1;
1266 }
1267 uri_arg = pj_str(argv[pj_optind]);
1268 if (uri_to_call)
1269 *uri_to_call = uri_arg;
1270 pj_optind++;
1271
1272 /* Add URI to call to buddy list if it's not already there */
1273 for (i=0; i<cfg->buddy_cnt; ++i) {
1274 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1275 break;
1276 }
1277 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1278 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1279 }
1280
1281 } else {
1282 if (uri_to_call)
1283 uri_to_call->slen = 0;
1284 }
1285
1286 if (pj_optind != argc) {
1287 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1288 return PJ_EINVAL;
1289 }
1290
Benny Prijono56315612006-07-18 14:39:40 +00001291 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1292 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001293
1294 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001295 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1296
1297 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001298 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001299 acfg->cred_count++;
1300 }
1301
1302 /* When IMS mode is enabled for the account, verify that settings
1303 * are okay.
1304 */
1305 /* For now we check if IMS mode is activated by looking if
1306 * initial_auth is set.
1307 */
1308 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1309 /* Realm must point to the real domain */
1310 if (*acfg->cred_info[0].realm.ptr=='*') {
1311 PJ_LOG(1,(THIS_FILE,
1312 "Error: cannot use '*' as realm with IMS"));
1313 return PJ_EINVAL;
1314 }
1315
1316 /* Username for authentication must be in a@b format */
1317 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1318 PJ_LOG(1,(THIS_FILE,
1319 "Error: Username for authentication must "
1320 "be in user@domain format with IMS"));
1321 return PJ_EINVAL;
1322 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001323 }
1324 }
1325
1326
1327 return PJ_SUCCESS;
1328}
1329
1330
1331/*
1332 * Save account settings
1333 */
1334static void write_account_settings(int acc_index, pj_str_t *result)
1335{
1336 unsigned i;
1337 char line[128];
1338 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1339
1340
1341 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1342 pj_strcat2(result, line);
1343
1344
1345 /* Identity */
1346 if (acc_cfg->id.slen) {
1347 pj_ansi_sprintf(line, "--id %.*s\n",
1348 (int)acc_cfg->id.slen,
1349 acc_cfg->id.ptr);
1350 pj_strcat2(result, line);
1351 }
1352
1353 /* Registrar server */
1354 if (acc_cfg->reg_uri.slen) {
1355 pj_ansi_sprintf(line, "--registrar %.*s\n",
1356 (int)acc_cfg->reg_uri.slen,
1357 acc_cfg->reg_uri.ptr);
1358 pj_strcat2(result, line);
1359
1360 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1361 acc_cfg->reg_timeout);
1362 pj_strcat2(result, line);
1363 }
1364
1365 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001366 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001367 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001368 (int)acc_cfg->force_contact.slen,
1369 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001370 pj_strcat2(result, line);
1371 }
1372
Benny Prijonofce28542007-12-09 15:41:10 +00001373 /* */
Benny Prijono7f630432008-09-24 16:52:41 +00001374 if (acc_cfg->allow_contact_rewrite!=1)
Benny Prijonofce28542007-12-09 15:41:10 +00001375 {
Benny Prijono251e02a2008-07-25 10:05:55 +00001376 pj_ansi_sprintf(line, "--auto-update-nat %i\n",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001377 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001378 pj_strcat2(result, line);
1379 }
1380
Benny Prijonod8179652008-01-23 20:39:07 +00001381#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1382 /* SRTP */
1383 if (acc_cfg->use_srtp) {
1384 pj_ansi_sprintf(line, "--use-srtp %i\n",
1385 (int)acc_cfg->use_srtp);
1386 pj_strcat2(result, line);
1387 }
1388#endif
1389
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001390 /* Proxy */
1391 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1392 pj_ansi_sprintf(line, "--proxy %.*s\n",
1393 (int)acc_cfg->proxy[i].slen,
1394 acc_cfg->proxy[i].ptr);
1395 pj_strcat2(result, line);
1396 }
1397
1398 /* Credentials */
1399 for (i=0; i<acc_cfg->cred_count; ++i) {
1400 if (acc_cfg->cred_info[i].realm.slen) {
1401 pj_ansi_sprintf(line, "--realm %.*s\n",
1402 (int)acc_cfg->cred_info[i].realm.slen,
1403 acc_cfg->cred_info[i].realm.ptr);
1404 pj_strcat2(result, line);
1405 }
1406
1407 if (acc_cfg->cred_info[i].username.slen) {
1408 pj_ansi_sprintf(line, "--username %.*s\n",
1409 (int)acc_cfg->cred_info[i].username.slen,
1410 acc_cfg->cred_info[i].username.ptr);
1411 pj_strcat2(result, line);
1412 }
1413
1414 if (acc_cfg->cred_info[i].data.slen) {
1415 pj_ansi_sprintf(line, "--password %.*s\n",
1416 (int)acc_cfg->cred_info[i].data.slen,
1417 acc_cfg->cred_info[i].data.ptr);
1418 pj_strcat2(result, line);
1419 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001420
1421 if (i != acc_cfg->cred_count - 1)
1422 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001423 }
1424
1425}
1426
1427
1428/*
1429 * Write settings.
1430 */
1431static int write_settings(const struct app_config *config,
1432 char *buf, pj_size_t max)
1433{
1434 unsigned acc_index;
1435 unsigned i;
1436 pj_str_t cfg;
1437 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001438 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001439
1440 PJ_UNUSED_ARG(max);
1441
1442 cfg.ptr = buf;
1443 cfg.slen = 0;
1444
1445 /* Logging. */
1446 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1447 pj_ansi_sprintf(line, "--log-level %d\n",
1448 config->log_cfg.level);
1449 pj_strcat2(&cfg, line);
1450
1451 pj_ansi_sprintf(line, "--app-log-level %d\n",
1452 config->log_cfg.console_level);
1453 pj_strcat2(&cfg, line);
1454
1455 if (config->log_cfg.log_filename.slen) {
1456 pj_ansi_sprintf(line, "--log-file %.*s\n",
1457 (int)config->log_cfg.log_filename.slen,
1458 config->log_cfg.log_filename.ptr);
1459 pj_strcat2(&cfg, line);
1460 }
1461
1462
1463 /* Save account settings. */
1464 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1465
1466 write_account_settings(acc_index, &cfg);
1467
1468 if (acc_index < config->acc_cnt-1)
1469 pj_strcat2(&cfg, "--next-account\n");
1470 }
1471
1472
1473 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1474
1475 /* Outbound proxy */
1476 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1477 pj_ansi_sprintf(line, "--outbound %.*s\n",
1478 (int)config->cfg.outbound_proxy[i].slen,
1479 config->cfg.outbound_proxy[i].ptr);
1480 pj_strcat2(&cfg, line);
1481 }
1482
1483
1484 /* UDP Transport. */
1485 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1486 pj_strcat2(&cfg, line);
1487
Benny Prijono0a5cad82006-09-26 13:21:02 +00001488 /* IP address, if any. */
1489 if (config->udp_cfg.public_addr.slen) {
1490 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1491 (int)config->udp_cfg.public_addr.slen,
1492 config->udp_cfg.public_addr.ptr);
1493 pj_strcat2(&cfg, line);
1494 }
1495
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001496 /* No TCP ? */
1497 if (config->no_tcp) {
1498 pj_strcat2(&cfg, "--no-tcp\n");
1499 }
1500
1501 /* No UDP ? */
1502 if (config->no_udp) {
1503 pj_strcat2(&cfg, "--no-udp\n");
1504 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001505
1506 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001507 if (config->cfg.stun_domain.slen) {
1508 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1509 (int)config->cfg.stun_domain.slen,
1510 config->cfg.stun_domain.ptr);
1511 pj_strcat2(&cfg, line);
1512 }
1513 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001514 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001515 (int)config->cfg.stun_host.slen,
1516 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001517 pj_strcat2(&cfg, line);
1518 }
1519
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001520 /* TLS */
1521 if (config->use_tls)
1522 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001523 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001524 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001525 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1526 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001527 pj_strcat2(&cfg, line);
1528 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001529 if (config->udp_cfg.tls_setting.cert_file.slen) {
1530 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1531 (int)config->udp_cfg.tls_setting.cert_file.slen,
1532 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001533 pj_strcat2(&cfg, line);
1534 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001535 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1536 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1537 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1538 config->udp_cfg.tls_setting.privkey_file.ptr);
1539 pj_strcat2(&cfg, line);
1540 }
1541
1542 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001543 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001544 (int)config->udp_cfg.tls_setting.password.slen,
1545 config->udp_cfg.tls_setting.password.ptr);
1546 pj_strcat2(&cfg, line);
1547 }
1548
Benny Prijonoe10db842008-07-01 15:31:59 +00001549 if (config->udp_cfg.tls_setting.server_name.slen) {
1550 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1551 (int)config->udp_cfg.tls_setting.server_name.slen,
1552 config->udp_cfg.tls_setting.server_name.ptr);
1553 pj_strcat2(&cfg, line);
1554 }
1555
Benny Prijonof3bbc132006-12-25 06:43:59 +00001556 if (config->udp_cfg.tls_setting.verify_server)
1557 pj_strcat2(&cfg, "--tls-verify-server\n");
1558
1559 if (config->udp_cfg.tls_setting.verify_client)
1560 pj_strcat2(&cfg, "--tls-verify-client\n");
1561
1562 if (config->udp_cfg.tls_setting.timeout.sec) {
1563 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001564 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001565 pj_strcat2(&cfg, line);
1566 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001567
1568 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1569
Benny Prijonof6508982008-01-25 09:02:33 +00001570 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001571#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001572 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1573 pj_ansi_sprintf(line, "--use-srtp %d\n",
1574 app_config.cfg.use_srtp);
1575 pj_strcat2(&cfg, line);
1576 }
1577 if (app_config.cfg.srtp_secure_signaling !=
1578 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1579 {
1580 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1581 app_config.cfg.srtp_secure_signaling);
1582 pj_strcat2(&cfg, line);
1583 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001584#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001585
Benny Prijonof76e1392008-06-06 14:51:48 +00001586 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001587 if (config->media_cfg.enable_ice)
1588 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001589
1590 if (config->media_cfg.enable_turn)
1591 pj_strcat2(&cfg, "--use-turn\n");
1592
1593 if (config->media_cfg.ice_no_host_cands)
1594 pj_strcat2(&cfg, "--ice-no-host\n");
1595
Benny Prijono551af422008-08-07 09:55:52 +00001596 if (config->media_cfg.ice_no_rtcp)
1597 pj_strcat2(&cfg, "--ice-no-rtcp\n");
1598
Benny Prijonof76e1392008-06-06 14:51:48 +00001599 if (config->media_cfg.turn_server.slen) {
1600 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1601 (int)config->media_cfg.turn_server.slen,
1602 config->media_cfg.turn_server.ptr);
1603 pj_strcat2(&cfg, line);
1604 }
1605
1606 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1607 pj_strcat2(&cfg, "--turn-tcp\n");
1608
1609 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1610 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1611 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1612 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1613 pj_strcat2(&cfg, line);
1614 }
1615
1616 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1617 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1618 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1619 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1620 pj_strcat2(&cfg, line);
1621 }
1622
1623 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001624 if (config->null_audio)
1625 pj_strcat2(&cfg, "--null-audio\n");
1626 if (config->auto_play)
1627 pj_strcat2(&cfg, "--auto-play\n");
1628 if (config->auto_loop)
1629 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001630 if (config->auto_conf)
1631 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001632 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001633 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001634 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001635 pj_strcat2(&cfg, line);
1636 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001637 for (i=0; i<config->tone_count; ++i) {
1638 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1639 config->tones[i].freq1, config->tones[i].freq2,
1640 config->tones[i].on_msec, config->tones[i].off_msec);
1641 pj_strcat2(&cfg, line);
1642 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001643 if (config->rec_file.slen) {
1644 pj_ansi_sprintf(line, "--rec-file %s\n",
1645 config->rec_file.ptr);
1646 pj_strcat2(&cfg, line);
1647 }
1648 if (config->auto_rec)
1649 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001650 if (config->capture_dev != PJSUA_INVALID_ID) {
1651 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1652 pj_strcat2(&cfg, line);
1653 }
1654 if (config->playback_dev != PJSUA_INVALID_ID) {
1655 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1656 pj_strcat2(&cfg, line);
1657 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001658 if (config->media_cfg.snd_auto_close_time != -1) {
1659 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1660 config->media_cfg.snd_auto_close_time);
1661 pj_strcat2(&cfg, line);
1662 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001663 if (config->no_tones) {
1664 pj_strcat2(&cfg, "--no-tones\n");
1665 }
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001666 if (config->media_cfg.jb_max != -1) {
1667 pj_ansi_sprintf(line, "--jb-max-size %d\n",
1668 config->media_cfg.jb_max);
1669 pj_strcat2(&cfg, line);
1670 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001671
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001672 /* Sound device latency */
1673 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1674 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1675 pj_strcat2(&cfg, line);
1676 }
1677 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1678 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1679 pj_strcat2(&cfg, line);
1680 }
1681
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001682 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001683 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001684 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001685 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001686 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001687 } else {
1688 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1689 config->media_cfg.clock_rate);
1690 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001691 }
Benny Prijono70972992006-08-05 11:13:58 +00001692
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001693 if (config->media_cfg.snd_clock_rate &&
1694 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1695 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001696 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1697 config->media_cfg.snd_clock_rate);
1698 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001699 }
1700
Benny Prijono7d60d052008-03-29 12:24:20 +00001701 /* Stereo mode. */
1702 if (config->media_cfg.channel_count == 2) {
1703 pj_ansi_sprintf(line, "--stereo\n");
1704 pj_strcat2(&cfg, line);
1705 }
1706
Benny Prijono70972992006-08-05 11:13:58 +00001707 /* quality */
1708 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001709 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001710 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001711 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001712 } else {
1713 pj_ansi_sprintf(line, "#using default --quality %d\n",
1714 config->media_cfg.quality);
1715 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001716 }
Benny Prijono0498d902006-06-19 14:49:14 +00001717
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001718
1719 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001720 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001721 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001722 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001723 pj_strcat2(&cfg, line);
1724 }
1725
Benny Prijono70972992006-08-05 11:13:58 +00001726 /* no-vad */
1727 if (config->media_cfg.no_vad) {
1728 pj_strcat2(&cfg, "--no-vad\n");
1729 }
1730
1731 /* ec-tail */
1732 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1733 pj_ansi_sprintf(line, "--ec-tail %d\n",
1734 config->media_cfg.ec_tail_len);
1735 pj_strcat2(&cfg, line);
1736 } else {
1737 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1738 config->media_cfg.ec_tail_len);
1739 pj_strcat2(&cfg, line);
1740 }
1741
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001742 /* ec-opt */
1743 if (config->media_cfg.ec_options != 0) {
1744 pj_ansi_sprintf(line, "--ec-opt %d\n",
1745 config->media_cfg.ec_options);
1746 pj_strcat2(&cfg, line);
1747 }
Benny Prijono70972992006-08-05 11:13:58 +00001748
1749 /* ilbc-mode */
1750 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1751 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1752 config->media_cfg.ilbc_mode);
1753 pj_strcat2(&cfg, line);
1754 } else {
1755 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1756 config->media_cfg.ilbc_mode);
1757 pj_strcat2(&cfg, line);
1758 }
1759
1760 /* RTP drop */
1761 if (config->media_cfg.tx_drop_pct) {
1762 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1763 config->media_cfg.tx_drop_pct);
1764 pj_strcat2(&cfg, line);
1765
1766 }
1767 if (config->media_cfg.rx_drop_pct) {
1768 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1769 config->media_cfg.rx_drop_pct);
1770 pj_strcat2(&cfg, line);
1771
1772 }
1773
1774
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001775 /* Start RTP port. */
1776 pj_ansi_sprintf(line, "--rtp-port %d\n",
1777 config->rtp_cfg.port);
1778 pj_strcat2(&cfg, line);
1779
1780 /* Add codec. */
1781 for (i=0; i<config->codec_cnt; ++i) {
1782 pj_ansi_sprintf(line, "--add-codec %s\n",
1783 config->codec_arg[i].ptr);
1784 pj_strcat2(&cfg, line);
1785 }
Benny Prijonofce28542007-12-09 15:41:10 +00001786 /* Disable codec */
1787 for (i=0; i<config->codec_dis_cnt; ++i) {
1788 pj_ansi_sprintf(line, "--dis-codec %s\n",
1789 config->codec_dis[i].ptr);
1790 pj_strcat2(&cfg, line);
1791 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001792
1793 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1794
1795 /* Auto-answer. */
1796 if (config->auto_answer != 0) {
1797 pj_ansi_sprintf(line, "--auto-answer %d\n",
1798 config->auto_answer);
1799 pj_strcat2(&cfg, line);
1800 }
1801
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001802 /* accept-redirect */
1803 if (config->redir_op != PJSIP_REDIRECT_ACCEPT) {
1804 pj_ansi_sprintf(line, "--accept-redirect %d\n",
1805 config->redir_op);
1806 pj_strcat2(&cfg, line);
1807 }
1808
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001809 /* Max calls. */
1810 pj_ansi_sprintf(line, "--max-calls %d\n",
1811 config->cfg.max_calls);
1812 pj_strcat2(&cfg, line);
1813
1814 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001815 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001816 pj_ansi_sprintf(line, "--duration %d\n",
1817 config->duration);
1818 pj_strcat2(&cfg, line);
1819 }
1820
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001821 /* norefersub ? */
1822 if (config->no_refersub) {
1823 pj_strcat2(&cfg, "--norefersub\n");
1824 }
1825
Benny Prijonofce28542007-12-09 15:41:10 +00001826 if (pjsip_use_compact_form)
1827 {
1828 pj_strcat2(&cfg, "--use-compact-form\n");
1829 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001830
Benny Prijono91d06b62008-09-20 12:16:56 +00001831 if (config->cfg.force_lr) {
1832 pj_strcat2(&cfg, "--no-force-lr\n");
1833 }
1834
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001835 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1836
1837 /* Add buddies. */
1838 for (i=0; i<config->buddy_cnt; ++i) {
1839 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1840 (int)config->buddy_cfg[i].uri.slen,
1841 config->buddy_cfg[i].uri.ptr);
1842 pj_strcat2(&cfg, line);
1843 }
1844
1845
1846 *(cfg.ptr + cfg.slen) = '\0';
1847 return cfg.slen;
1848}
1849
1850
1851/*
1852 * Dump application states.
1853 */
1854static void app_dump(pj_bool_t detail)
1855{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001856 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001857}
1858
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001859/*
1860 * Print log of call states. Since call states may be too long for logger,
1861 * printing it is a bit tricky, it should be printed part by part as long
1862 * as the logger can accept.
1863 */
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001864static void log_call_dump(int call_id)
1865{
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001866 unsigned call_dump_len;
1867 unsigned part_len;
1868 unsigned part_idx;
1869 unsigned log_decor;
1870
1871 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1872 sizeof(some_buf), " ");
1873 call_dump_len = strlen(some_buf);
1874
1875 log_decor = pj_log_get_decor();
1876 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1877 PJ_LOG(3,(THIS_FILE, "\n"));
1878 pj_log_set_decor(0);
1879
1880 part_idx = 0;
1881 part_len = PJ_LOG_MAX_SIZE-80;
1882 while (part_idx < call_dump_len) {
1883 char p_orig, *p;
1884
1885 p = &some_buf[part_idx];
1886 if (part_idx + part_len > call_dump_len)
1887 part_len = call_dump_len - part_idx;
1888 p_orig = p[part_len];
1889 p[part_len] = '\0';
1890 PJ_LOG(3,(THIS_FILE, "%s", p));
1891 p[part_len] = p_orig;
1892 part_idx += part_len;
1893 }
1894 pj_log_set_decor(log_decor);
1895}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001896
1897/*****************************************************************************
1898 * Console application
1899 */
1900
Benny Prijono91d20f42008-06-14 19:42:37 +00001901static void ringback_start(pjsua_call_id call_id)
1902{
1903 if (app_config.no_tones)
1904 return;
1905
1906 if (app_config.call_data[call_id].ringback_on)
1907 return;
1908
1909 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1910
1911 if (++app_config.ringback_cnt==1 &&
1912 app_config.ringback_slot!=PJSUA_INVALID_ID)
1913 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001914 pjsua_conf_connect(app_config.ringback_slot, 0);
1915 }
1916}
1917
1918static void ring_stop(pjsua_call_id call_id)
1919{
1920 if (app_config.no_tones)
1921 return;
1922
1923 if (app_config.call_data[call_id].ringback_on) {
1924 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1925
1926 pj_assert(app_config.ringback_cnt>0);
1927 if (--app_config.ringback_cnt == 0 &&
1928 app_config.ringback_slot!=PJSUA_INVALID_ID)
1929 {
1930 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001931 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001932 }
1933 }
1934
1935 if (app_config.call_data[call_id].ring_on) {
1936 app_config.call_data[call_id].ring_on = PJ_FALSE;
1937
1938 pj_assert(app_config.ring_cnt>0);
1939 if (--app_config.ring_cnt == 0 &&
1940 app_config.ring_slot!=PJSUA_INVALID_ID)
1941 {
1942 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001943 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001944 }
1945 }
1946}
1947
1948static void ring_start(pjsua_call_id call_id)
1949{
1950 if (app_config.no_tones)
1951 return;
1952
1953 if (app_config.call_data[call_id].ring_on)
1954 return;
1955
1956 app_config.call_data[call_id].ring_on = PJ_TRUE;
1957
1958 if (++app_config.ring_cnt==1 &&
1959 app_config.ring_slot!=PJSUA_INVALID_ID)
1960 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001961 pjsua_conf_connect(app_config.ring_slot, 0);
1962 }
1963}
1964
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001965/*
1966 * Find next call when current call is disconnected or when user
1967 * press ']'
1968 */
1969static pj_bool_t find_next_call(void)
1970{
1971 int i, max;
1972
1973 max = pjsua_call_get_max_count();
1974 for (i=current_call+1; i<max; ++i) {
1975 if (pjsua_call_is_active(i)) {
1976 current_call = i;
1977 return PJ_TRUE;
1978 }
1979 }
1980
1981 for (i=0; i<current_call; ++i) {
1982 if (pjsua_call_is_active(i)) {
1983 current_call = i;
1984 return PJ_TRUE;
1985 }
1986 }
1987
1988 current_call = PJSUA_INVALID_ID;
1989 return PJ_FALSE;
1990}
1991
1992
1993/*
1994 * Find previous call when user press '['
1995 */
1996static pj_bool_t find_prev_call(void)
1997{
1998 int i, max;
1999
2000 max = pjsua_call_get_max_count();
2001 for (i=current_call-1; i>=0; --i) {
2002 if (pjsua_call_is_active(i)) {
2003 current_call = i;
2004 return PJ_TRUE;
2005 }
2006 }
2007
2008 for (i=max-1; i>current_call; --i) {
2009 if (pjsua_call_is_active(i)) {
2010 current_call = i;
2011 return PJ_TRUE;
2012 }
2013 }
2014
2015 current_call = PJSUA_INVALID_ID;
2016 return PJ_FALSE;
2017}
2018
2019
Benny Prijono804ff0a2006-09-14 11:17:48 +00002020/* Callback from timer when the maximum call duration has been
2021 * exceeded.
2022 */
2023static void call_timeout_callback(pj_timer_heap_t *timer_heap,
2024 struct pj_timer_entry *entry)
2025{
2026 pjsua_call_id call_id = entry->id;
2027 pjsua_msg_data msg_data;
2028 pjsip_generic_string_hdr warn;
2029 pj_str_t hname = pj_str("Warning");
2030 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
2031
2032 PJ_UNUSED_ARG(timer_heap);
2033
Benny Prijono148c9dd2006-09-19 13:37:53 +00002034 if (call_id == PJSUA_INVALID_ID) {
2035 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
2036 return;
2037 }
2038
Benny Prijono804ff0a2006-09-14 11:17:48 +00002039 /* Add warning header */
2040 pjsua_msg_data_init(&msg_data);
2041 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
2042 pj_list_push_back(&msg_data.hdr_list, &warn);
2043
2044 /* Call duration has been exceeded; disconnect the call */
2045 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
2046 "for call %d, disconnecting the call",
2047 app_config.duration, call_id));
2048 entry->id = PJSUA_INVALID_ID;
2049 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
2050}
2051
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002052
2053/*
2054 * Handler when invite state has changed.
2055 */
2056static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
2057{
2058 pjsua_call_info call_info;
2059
2060 PJ_UNUSED_ARG(e);
2061
2062 pjsua_call_get_info(call_id, &call_info);
2063
2064 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
2065
Benny Prijono91d20f42008-06-14 19:42:37 +00002066 /* Stop all ringback for this call */
2067 ring_stop(call_id);
2068
Benny Prijono804ff0a2006-09-14 11:17:48 +00002069 /* Cancel duration timer, if any */
2070 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
2071 struct call_data *cd = &app_config.call_data[call_id];
2072 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2073
2074 cd->timer.id = PJSUA_INVALID_ID;
2075 pjsip_endpt_cancel_timer(endpt, &cd->timer);
2076 }
2077
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002078 /* Rewind play file when hangup automatically,
2079 * since file is not looped
2080 */
2081 if (app_config.auto_play_hangup)
2082 pjsua_player_set_pos(app_config.wav_id, 0);
2083
2084
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002085 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
2086 call_id,
2087 call_info.last_status,
2088 call_info.last_status_text.ptr));
2089
2090 if (call_id == current_call) {
2091 find_next_call();
2092 }
2093
Benny Prijono4be63b52006-11-25 14:50:25 +00002094 /* Dump media state upon disconnected */
2095 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002096 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002097 "Call %d disconnected, dumping media stats..",
2098 call_id));
2099 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002100 }
2101
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002102 } else {
2103
Benny Prijono804ff0a2006-09-14 11:17:48 +00002104 if (app_config.duration!=NO_LIMIT &&
2105 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2106 {
2107 /* Schedule timer to hangup call after the specified duration */
2108 struct call_data *cd = &app_config.call_data[call_id];
2109 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2110 pj_time_val delay;
2111
2112 cd->timer.id = call_id;
2113 delay.sec = app_config.duration;
2114 delay.msec = 0;
2115 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2116 }
2117
Benny Prijono4be63b52006-11-25 14:50:25 +00002118 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2119 int code;
2120 pj_str_t reason;
2121 pjsip_msg *msg;
2122
2123 /* This can only occur because of TX or RX message */
2124 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2125
2126 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2127 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2128 } else {
2129 msg = e->body.tsx_state.src.tdata->msg;
2130 }
2131
2132 code = msg->line.status.code;
2133 reason = msg->line.status.reason;
2134
Benny Prijono91d20f42008-06-14 19:42:37 +00002135 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2136 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2137 msg->body == NULL &&
2138 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2139 {
2140 ringback_start(call_id);
2141 }
2142
Benny Prijono4be63b52006-11-25 14:50:25 +00002143 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2144 call_id, call_info.state_text.ptr,
2145 code, (int)reason.slen, reason.ptr));
2146 } else {
2147 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2148 call_id,
2149 call_info.state_text.ptr));
2150 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002151
2152 if (current_call==PJSUA_INVALID_ID)
2153 current_call = call_id;
2154
2155 }
2156}
2157
2158
2159/**
2160 * Handler when there is incoming call.
2161 */
2162static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2163 pjsip_rx_data *rdata)
2164{
2165 pjsua_call_info call_info;
2166
2167 PJ_UNUSED_ARG(acc_id);
2168 PJ_UNUSED_ARG(rdata);
2169
2170 pjsua_call_get_info(call_id, &call_info);
2171
Benny Prijono91d20f42008-06-14 19:42:37 +00002172 /* Start ringback */
2173 ring_start(call_id);
2174
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002175 if (app_config.auto_answer > 0) {
2176 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2177 }
2178
2179 if (app_config.auto_answer < 200) {
2180 PJ_LOG(3,(THIS_FILE,
2181 "Incoming call for account %d!\n"
2182 "From: %s\n"
2183 "To: %s\n"
2184 "Press a to answer or h to reject call",
2185 acc_id,
2186 call_info.remote_info.ptr,
2187 call_info.local_info.ptr));
2188 }
2189}
2190
2191
2192/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002193 * Handler when a transaction within a call has changed state.
2194 */
2195static void on_call_tsx_state(pjsua_call_id call_id,
2196 pjsip_transaction *tsx,
2197 pjsip_event *e)
2198{
2199 const pjsip_method info_method =
2200 {
2201 PJSIP_OTHER_METHOD,
2202 { "INFO", 4 }
2203 };
2204
2205 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2206 /*
2207 * Handle INFO method.
2208 */
2209 if (tsx->role == PJSIP_ROLE_UAC &&
2210 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002211 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2212 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002213 {
2214 /* Status of outgoing INFO request */
2215 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2216 PJ_LOG(4,(THIS_FILE,
2217 "Call %d: DTMF sent successfully with INFO",
2218 call_id));
2219 } else if (tsx->status_code >= 300) {
2220 PJ_LOG(4,(THIS_FILE,
2221 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2222 call_id,
2223 tsx->status_code,
2224 (int)tsx->status_text.slen,
2225 tsx->status_text.ptr));
2226 }
2227 } else if (tsx->role == PJSIP_ROLE_UAS &&
2228 tsx->state == PJSIP_TSX_STATE_TRYING)
2229 {
2230 /* Answer incoming INFO with 200/OK */
2231 pjsip_rx_data *rdata;
2232 pjsip_tx_data *tdata;
2233 pj_status_t status;
2234
2235 rdata = e->body.tsx_state.src.rdata;
2236
2237 if (rdata->msg_info.msg->body) {
2238 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2239 200, NULL, &tdata);
2240 if (status == PJ_SUCCESS)
2241 status = pjsip_tsx_send_msg(tsx, tdata);
2242
2243 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2244 call_id,
2245 (int)rdata->msg_info.msg->body->len,
2246 rdata->msg_info.msg->body->data));
2247 } else {
2248 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2249 400, NULL, &tdata);
2250 if (status == PJ_SUCCESS)
2251 status = pjsip_tsx_send_msg(tsx, tdata);
2252 }
2253 }
2254 }
2255}
2256
2257
2258/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002259 * Callback on media state changed event.
2260 * The action may connect the call to sound device, to file, or
2261 * to loop the call.
2262 */
2263static void on_call_media_state(pjsua_call_id call_id)
2264{
2265 pjsua_call_info call_info;
2266
2267 pjsua_call_get_info(call_id, &call_info);
2268
Benny Prijono91d20f42008-06-14 19:42:37 +00002269 /* Stop ringback */
2270 ring_stop(call_id);
2271
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002272 /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
2273 * otherwise we should NOT connect the ports.
2274 */
2275 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE ||
2276 call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
2277 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002278 pj_bool_t connect_sound = PJ_TRUE;
2279
2280 /* Loopback sound, if desired */
2281 if (app_config.auto_loop) {
2282 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2283 connect_sound = PJ_FALSE;
Benny Prijonof6c77f42008-08-13 13:52:19 +00002284 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00002285
Benny Prijonof6c77f42008-08-13 13:52:19 +00002286 /* Automatically record conversation, if desired */
2287 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2288 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002289 }
2290
2291 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002292 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2293 app_config.wav_port != PJSUA_INVALID_ID)
2294 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002295 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2296 connect_sound = PJ_FALSE;
2297 }
2298
Benny Prijono7ca96da2006-08-07 12:11:40 +00002299 /* Put call in conference with other calls, if desired */
2300 if (app_config.auto_conf) {
2301 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002302 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002303 unsigned i;
2304
2305 /* Get all calls, and establish media connection between
2306 * this call and other calls.
2307 */
2308 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002309
Benny Prijono7ca96da2006-08-07 12:11:40 +00002310 for (i=0; i<call_cnt; ++i) {
2311 if (call_ids[i] == call_id)
2312 continue;
2313
2314 if (!pjsua_call_has_media(call_ids[i]))
2315 continue;
2316
2317 pjsua_conf_connect(call_info.conf_slot,
2318 pjsua_call_get_conf_port(call_ids[i]));
2319 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2320 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002321
2322 /* Automatically record conversation, if desired */
2323 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2324 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2325 app_config.rec_port);
2326 }
2327
Benny Prijono7ca96da2006-08-07 12:11:40 +00002328 }
2329
2330 /* Also connect call to local sound device */
2331 connect_sound = PJ_TRUE;
2332 }
2333
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002334 /* Otherwise connect to sound device */
2335 if (connect_sound) {
2336 pjsua_conf_connect(call_info.conf_slot, 0);
2337 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002338
2339 /* Automatically record conversation, if desired */
2340 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2341 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2342 pjsua_conf_connect(0, app_config.rec_port);
2343 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002344 }
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002345 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002346
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002347 /* Handle media status */
2348 switch (call_info.media_status) {
2349 case PJSUA_CALL_MEDIA_ACTIVE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002350 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002351 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002352
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002353 case PJSUA_CALL_MEDIA_LOCAL_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002354 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2355 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002356 break;
2357
2358 case PJSUA_CALL_MEDIA_REMOTE_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002359 PJ_LOG(3,(THIS_FILE,
2360 "Media for call %d is suspended (hold) by remote",
2361 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002362 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002363
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002364 case PJSUA_CALL_MEDIA_ERROR:
2365 PJ_LOG(3,(THIS_FILE,
Benny Prijono096c56c2007-09-15 08:30:16 +00002366 "Media has reported error, disconnecting call"));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002367 {
2368 pj_str_t reason = pj_str("ICE negotiation failed");
2369 pjsua_call_hangup(call_id, 500, &reason, NULL);
2370 }
2371 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002372
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002373 case PJSUA_CALL_MEDIA_NONE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002374 PJ_LOG(3,(THIS_FILE,
2375 "Media for call %d is inactive",
2376 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002377 break;
2378
2379 default:
2380 pj_assert(!"Unhandled media status");
2381 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002382 }
2383}
2384
Benny Prijono0875ae82006-12-26 00:11:48 +00002385/*
2386 * DTMF callback.
2387 */
2388static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2389{
2390 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2391}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002392
2393/*
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002394 * Redirection handler.
2395 */
Benny Prijono08a48b82008-11-27 12:42:07 +00002396static pjsip_redirect_op call_on_redirected(pjsua_call_id call_id,
2397 const pjsip_uri *target,
2398 const pjsip_event *e)
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002399{
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002400 PJ_UNUSED_ARG(e);
2401
Benny Prijono08a48b82008-11-27 12:42:07 +00002402 if (app_config.redir_op == PJSIP_REDIRECT_PENDING) {
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002403 char uristr[PJSIP_MAX_URL_SIZE];
2404 int len;
2405
2406 len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, target, uristr,
2407 sizeof(uristr));
2408 if (len < 1) {
2409 pj_ansi_strcpy(uristr, "--URI too long--");
2410 }
2411
2412 PJ_LOG(3,(THIS_FILE, "Call %d is being redirected to %.*s. "
2413 "Press 'Ra' to accept, 'Rr' to reject, or 'Rd' to "
2414 "disconnect.",
2415 call_id, len, uristr));
2416 }
Benny Prijono08a48b82008-11-27 12:42:07 +00002417
2418 return app_config.redir_op;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002419}
2420
2421/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002422 * Handler registration status has changed.
2423 */
2424static void on_reg_state(pjsua_acc_id acc_id)
2425{
2426 PJ_UNUSED_ARG(acc_id);
2427
2428 // Log already written.
2429}
2430
2431
2432/*
Benny Prijono563cd7a2008-08-19 20:02:15 +00002433 * Handler for incoming presence subscription request
2434 */
2435static void on_incoming_subscribe(pjsua_acc_id acc_id,
2436 pjsua_srv_pres *srv_pres,
2437 pjsua_buddy_id buddy_id,
2438 const pj_str_t *from,
2439 pjsip_rx_data *rdata,
2440 pjsip_status_code *code,
2441 pj_str_t *reason,
2442 pjsua_msg_data *msg_data)
2443{
2444 /* Just accept the request (the default behavior) */
2445 PJ_UNUSED_ARG(acc_id);
2446 PJ_UNUSED_ARG(srv_pres);
2447 PJ_UNUSED_ARG(buddy_id);
2448 PJ_UNUSED_ARG(from);
2449 PJ_UNUSED_ARG(rdata);
2450 PJ_UNUSED_ARG(code);
2451 PJ_UNUSED_ARG(reason);
2452 PJ_UNUSED_ARG(msg_data);
2453}
2454
2455
2456/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002457 * Handler on buddy state changed.
2458 */
2459static void on_buddy_state(pjsua_buddy_id buddy_id)
2460{
2461 pjsua_buddy_info info;
2462 pjsua_buddy_get_info(buddy_id, &info);
2463
2464 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2465 (int)info.uri.slen,
2466 info.uri.ptr,
2467 (int)info.status_text.slen,
2468 info.status_text.ptr));
2469}
2470
2471
2472/**
2473 * Incoming IM message (i.e. MESSAGE request)!
2474 */
2475static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2476 const pj_str_t *to, const pj_str_t *contact,
2477 const pj_str_t *mime_type, const pj_str_t *text)
2478{
2479 /* Note: call index may be -1 */
2480 PJ_UNUSED_ARG(call_id);
2481 PJ_UNUSED_ARG(to);
2482 PJ_UNUSED_ARG(contact);
2483 PJ_UNUSED_ARG(mime_type);
2484
Benny Prijonof4b538d2007-05-14 16:45:20 +00002485 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002486 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002487 (int)text->slen, text->ptr,
2488 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002489}
2490
2491
2492/**
2493 * Received typing indication
2494 */
2495static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2496 const pj_str_t *to, const pj_str_t *contact,
2497 pj_bool_t is_typing)
2498{
2499 PJ_UNUSED_ARG(call_id);
2500 PJ_UNUSED_ARG(to);
2501 PJ_UNUSED_ARG(contact);
2502
2503 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2504 (int)from->slen, from->ptr,
2505 (is_typing?"is typing..":"has stopped typing")));
2506}
2507
2508
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002509/**
2510 * Call transfer request status.
2511 */
2512static void on_call_transfer_status(pjsua_call_id call_id,
2513 int status_code,
2514 const pj_str_t *status_text,
2515 pj_bool_t final,
2516 pj_bool_t *p_cont)
2517{
2518 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2519 call_id, status_code,
2520 (int)status_text->slen, status_text->ptr,
2521 (final ? "[final]" : "")));
2522
2523 if (status_code/100 == 2) {
2524 PJ_LOG(3,(THIS_FILE,
2525 "Call %d: call transfered successfully, disconnecting call",
2526 call_id));
2527 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2528 *p_cont = PJ_FALSE;
2529 }
2530}
2531
2532
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002533/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002534 * Notification that call is being replaced.
2535 */
2536static void on_call_replaced(pjsua_call_id old_call_id,
2537 pjsua_call_id new_call_id)
2538{
2539 pjsua_call_info old_ci, new_ci;
2540
2541 pjsua_call_get_info(old_call_id, &old_ci);
2542 pjsua_call_get_info(new_call_id, &new_ci);
2543
2544 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2545 "call %d with %.*s",
2546 old_call_id,
2547 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2548 new_call_id,
2549 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2550}
2551
2552
2553/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002554 * NAT type detection callback.
2555 */
2556static void on_nat_detect(const pj_stun_nat_detect_result *res)
2557{
2558 if (res->status != PJ_SUCCESS) {
2559 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2560 } else {
2561 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2562 }
2563}
2564
2565
2566/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002567 * Print buddy list.
2568 */
2569static void print_buddy_list(void)
2570{
2571 pjsua_buddy_id ids[64];
2572 int i;
2573 unsigned count = PJ_ARRAY_SIZE(ids);
2574
2575 puts("Buddy list:");
2576
2577 pjsua_enum_buddies(ids, &count);
2578
2579 if (count == 0)
2580 puts(" -none-");
2581 else {
2582 for (i=0; i<(int)count; ++i) {
2583 pjsua_buddy_info info;
2584
2585 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2586 continue;
2587
Benny Prijono4461c7d2007-08-25 13:36:15 +00002588 printf(" [%2d] <%.*s> %.*s\n",
2589 ids[i]+1,
2590 (int)info.status_text.slen,
2591 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002592 (int)info.uri.slen,
2593 info.uri.ptr);
2594 }
2595 }
2596 puts("");
2597}
2598
2599
2600/*
2601 * Print account status.
2602 */
2603static void print_acc_status(int acc_id)
2604{
2605 char buf[80];
2606 pjsua_acc_info info;
2607
2608 pjsua_acc_get_info(acc_id, &info);
2609
2610 if (!info.has_registration) {
2611 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2612 (int)info.status_text.slen,
2613 info.status_text.ptr);
2614
2615 } else {
2616 pj_ansi_snprintf(buf, sizeof(buf),
2617 "%d/%.*s (expires=%d)",
2618 info.status,
2619 (int)info.status_text.slen,
2620 info.status_text.ptr,
2621 info.expires);
2622
2623 }
2624
2625 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2626 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002627 printf(" Online status: %.*s\n",
2628 (int)info.online_status_text.slen,
2629 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002630}
2631
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002632/* Playfile done notification, set timer to hangup calls */
2633pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2634{
2635 pj_time_val delay;
2636
2637 PJ_UNUSED_ARG(port);
2638 PJ_UNUSED_ARG(usr_data);
2639
2640 /* Just rewind WAV when it is played outside of call */
2641 if (pjsua_call_get_count() == 0) {
2642 pjsua_player_set_pos(app_config.wav_id, 0);
2643 return PJ_SUCCESS;
2644 }
2645
2646 /* Timer is already active */
2647 if (app_config.auto_hangup_timer.id == 1)
2648 return PJ_SUCCESS;
2649
2650 app_config.auto_hangup_timer.id = 1;
2651 delay.sec = 0;
2652 delay.msec = 200; /* Give 200 ms before hangup */
2653 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2654 &app_config.auto_hangup_timer,
2655 &delay);
2656
2657 return PJ_SUCCESS;
2658}
2659
2660/* Auto hangup timer callback */
2661static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2662 struct pj_timer_entry *entry)
2663{
2664 PJ_UNUSED_ARG(timer_heap);
2665 PJ_UNUSED_ARG(entry);
2666
2667 app_config.auto_hangup_timer.id = 0;
2668 pjsua_call_hangup_all();
2669}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002670
2671/*
2672 * Show a bit of help.
2673 */
2674static void keystroke_help(void)
2675{
2676 pjsua_acc_id acc_ids[16];
2677 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2678 int i;
2679
2680 printf(">>>>\n");
2681
2682 pjsua_enum_accs(acc_ids, &count);
2683
2684 printf("Account list:\n");
2685 for (i=0; i<(int)count; ++i)
2686 print_acc_status(acc_ids[i]);
2687
2688 print_buddy_list();
2689
2690 //puts("Commands:");
2691 puts("+=============================================================================+");
2692 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2693 puts("| | | |");
2694 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2695 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002696 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2697 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2698 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2699 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002700 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2701 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002702 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002703 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002704 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2705 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2706 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2707 puts("| | V Adjust audio Volume | f Save config |");
2708 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002709 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002710 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002711 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002712
2713 i = pjsua_call_get_count();
2714 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002715
2716 if (current_call != PJSUA_INVALID_ID) {
2717 pjsua_call_info ci;
2718 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2719 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2720 (int)ci.remote_info.slen, ci.remote_info.ptr,
2721 (int)ci.state_text.slen, ci.state_text.ptr);
2722 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002723}
2724
2725
2726/*
2727 * Input simple string
2728 */
2729static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2730{
2731 char *p;
2732
2733 printf("%s (empty to cancel): ", title); fflush(stdout);
2734 fgets(buf, len, stdin);
2735
2736 /* Remove trailing newlines. */
2737 for (p=buf; ; ++p) {
2738 if (*p=='\r' || *p=='\n') *p='\0';
2739 else if (!*p) break;
2740 }
2741
2742 if (!*buf)
2743 return PJ_FALSE;
2744
2745 return PJ_TRUE;
2746}
2747
2748
2749#define NO_NB -2
2750struct input_result
2751{
2752 int nb_result;
2753 char *uri_result;
2754};
2755
2756
2757/*
2758 * Input URL.
2759 */
2760static void ui_input_url(const char *title, char *buf, int len,
2761 struct input_result *result)
2762{
2763 result->nb_result = NO_NB;
2764 result->uri_result = NULL;
2765
2766 print_buddy_list();
2767
2768 printf("Choices:\n"
2769 " 0 For current dialog.\n"
2770 " -1 All %d buddies in buddy list\n"
2771 " [1 -%2d] Select from buddy list\n"
2772 " URL An URL\n"
2773 " <Enter> Empty input (or 'q') to cancel\n"
2774 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2775 printf("%s: ", title);
2776
2777 fflush(stdout);
2778 fgets(buf, len, stdin);
2779 len = strlen(buf);
2780
2781 /* Left trim */
2782 while (pj_isspace(*buf)) {
2783 ++buf;
2784 --len;
2785 }
2786
2787 /* Remove trailing newlines */
2788 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2789 buf[--len] = '\0';
2790
2791 if (len == 0 || buf[0]=='q')
2792 return;
2793
2794 if (pj_isdigit(*buf) || *buf=='-') {
2795
2796 int i;
2797
2798 if (*buf=='-')
2799 i = 1;
2800 else
2801 i = 0;
2802
2803 for (; i<len; ++i) {
2804 if (!pj_isdigit(buf[i])) {
2805 puts("Invalid input");
2806 return;
2807 }
2808 }
2809
2810 result->nb_result = my_atoi(buf);
2811
2812 if (result->nb_result >= 0 &&
2813 result->nb_result <= (int)pjsua_get_buddy_count())
2814 {
2815 return;
2816 }
2817 if (result->nb_result == -1)
2818 return;
2819
2820 puts("Invalid input");
2821 result->nb_result = NO_NB;
2822 return;
2823
2824 } else {
2825 pj_status_t status;
2826
2827 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2828 pjsua_perror(THIS_FILE, "Invalid URL", status);
2829 return;
2830 }
2831
2832 result->uri_result = buf;
2833 }
2834}
2835
2836/*
2837 * List the ports in conference bridge
2838 */
2839static void conf_list(void)
2840{
2841 unsigned i, count;
2842 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2843
2844 printf("Conference ports:\n");
2845
2846 count = PJ_ARRAY_SIZE(id);
2847 pjsua_enum_conf_ports(id, &count);
2848
2849 for (i=0; i<count; ++i) {
2850 char txlist[PJSUA_MAX_CALLS*4+10];
2851 unsigned j;
2852 pjsua_conf_port_info info;
2853
2854 pjsua_conf_get_port_info(id[i], &info);
2855
2856 txlist[0] = '\0';
2857 for (j=0; j<info.listener_cnt; ++j) {
2858 char s[10];
2859 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2860 pj_ansi_strcat(txlist, s);
2861 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002862 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002863 info.slot_id,
2864 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002865 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002866 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002867 (int)info.name.slen,
2868 info.name.ptr,
2869 txlist);
2870
2871 }
2872 puts("");
2873}
2874
2875
2876/*
Benny Prijono56315612006-07-18 14:39:40 +00002877 * Send arbitrary request to remote host
2878 */
2879static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2880{
2881 pj_str_t str_method;
2882 pjsip_method method;
2883 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002884 pjsip_endpoint *endpt;
2885 pj_status_t status;
2886
2887 endpt = pjsua_get_pjsip_endpt();
2888
2889 str_method = pj_str(cstr_method);
2890 pjsip_method_init_np(&method, &str_method);
2891
Benny Prijonofff245c2007-04-02 11:44:47 +00002892 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002893
Benny Prijonob988d762007-12-05 04:30:04 +00002894 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002895 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002896 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002897 return;
2898 }
2899}
2900
2901
2902/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002903 * Change extended online status.
2904 */
2905static void change_online_status(void)
2906{
2907 char menuin[32];
2908 pj_bool_t online_status;
2909 pjrpid_element elem;
2910 int i, choice;
2911
2912 enum {
2913 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2914 };
2915
2916 struct opt {
2917 int id;
2918 char *name;
2919 } opts[] = {
2920 { AVAILABLE, "Available" },
2921 { BUSY, "Busy"},
2922 { OTP, "On the phone"},
2923 { IDLE, "Idle"},
2924 { AWAY, "Away"},
2925 { BRB, "Be right back"},
2926 { OFFLINE, "Offline"}
2927 };
2928
2929 printf("\n"
2930 "Choices:\n");
2931 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2932 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2933 }
2934
2935 if (!simple_input("Select status", menuin, sizeof(menuin)))
2936 return;
2937
2938 choice = atoi(menuin) - 1;
2939 if (choice < 0 || choice >= OPT_MAX) {
2940 puts("Invalid selection");
2941 return;
2942 }
2943
2944 pj_bzero(&elem, sizeof(elem));
2945 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2946
2947 online_status = PJ_TRUE;
2948
2949 switch (choice) {
2950 case AVAILABLE:
2951 break;
2952 case BUSY:
2953 elem.activity = PJRPID_ACTIVITY_BUSY;
2954 elem.note = pj_str("Busy");
2955 break;
2956 case OTP:
2957 elem.activity = PJRPID_ACTIVITY_BUSY;
2958 elem.note = pj_str("On the phone");
2959 break;
2960 case IDLE:
2961 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2962 elem.note = pj_str("Idle");
2963 break;
2964 case AWAY:
2965 elem.activity = PJRPID_ACTIVITY_AWAY;
2966 elem.note = pj_str("Away");
2967 break;
2968 case BRB:
2969 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2970 elem.note = pj_str("Be right back");
2971 break;
2972 case OFFLINE:
2973 online_status = PJ_FALSE;
2974 break;
2975 }
2976
2977 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2978}
2979
2980
2981/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002982 * Change codec priorities.
2983 */
2984static void manage_codec_prio(void)
2985{
2986 pjsua_codec_info c[32];
2987 unsigned i, count = PJ_ARRAY_SIZE(c);
2988 char input[32];
2989 char *codec, *prio;
2990 pj_str_t id;
2991 int new_prio;
2992 pj_status_t status;
2993
2994 printf("List of codecs:\n");
2995
2996 pjsua_enum_codecs(c, &count);
2997 for (i=0; i<count; ++i) {
2998 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2999 c[i].codec_id.ptr);
3000 }
3001
3002 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00003003 puts("Enter codec id and its new priority "
3004 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00003005
Benny Prijono88accae2008-06-26 15:48:14 +00003006 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijonoc08682e2007-10-04 06:17:58 +00003007 fgets(input, sizeof(input), stdin);
3008 if (input[0]=='\r' || input[0]=='\n') {
3009 puts("Done");
3010 return;
3011 }
3012
3013 codec = strtok(input, " \t\r\n");
3014 prio = strtok(NULL, " \r\n");
3015
3016 if (!codec || !prio) {
3017 puts("Invalid input");
3018 return;
3019 }
3020
3021 new_prio = atoi(prio);
3022 if (new_prio < 0)
3023 new_prio = 0;
3024 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
3025 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
3026
3027 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
3028 (pj_uint8_t)new_prio);
3029 if (status != PJ_SUCCESS)
3030 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
3031}
3032
3033
3034/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003035 * Main "user interface" loop.
3036 */
3037void console_app_main(const pj_str_t *uri_to_call)
3038{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003039 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003040 char buf[128];
3041 char text[128];
3042 int i, count;
3043 char *uri;
3044 pj_str_t tmp;
3045 struct input_result result;
3046 pjsua_call_info call_info;
3047 pjsua_acc_info acc_info;
3048
3049
3050 /* If user specifies URI to call, then call the URI */
3051 if (uri_to_call->slen) {
3052 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
3053 }
3054
3055 keystroke_help();
3056
3057 for (;;) {
3058
3059 printf(">>> ");
3060 fflush(stdout);
3061
Benny Prijono990042e2007-01-21 19:36:00 +00003062 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
3063 /*
3064 * Be friendly to users who redirect commands into
3065 * program, when file ends, resume with kbd.
3066 * If exit is desired end script with q for quit
3067 */
3068 /* Reopen stdin/stdout/stderr to /dev/console */
3069#if defined(PJ_WIN32) && PJ_WIN32!=0
3070 if (freopen ("CONIN$", "r", stdin) == NULL) {
3071#else
3072 if (1) {
3073#endif
3074 puts("Cannot switch back to console from file redirection");
3075 menuin[0] = 'q';
3076 menuin[1] = '\0';
3077 } else {
3078 puts("Switched back to console from file redirection");
3079 continue;
3080 }
3081 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003082
Benny Prijonoebc32c32008-06-12 13:30:39 +00003083 if (cmd_echo) {
3084 printf("%s", menuin);
3085 }
3086
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003087 switch (menuin[0]) {
3088
3089 case 'm':
3090 /* Make call! : */
3091 printf("(You currently have %d calls)\n",
3092 pjsua_call_get_count());
3093
3094 uri = NULL;
3095 ui_input_url("Make call", buf, sizeof(buf), &result);
3096 if (result.nb_result != NO_NB) {
3097
3098 if (result.nb_result == -1 || result.nb_result == 0) {
3099 puts("You can't do that with make call!");
3100 continue;
3101 } else {
3102 pjsua_buddy_info binfo;
3103 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3104 uri = binfo.uri.ptr;
3105 }
3106
3107 } else if (result.uri_result) {
3108 uri = result.uri_result;
3109 }
3110
3111 tmp = pj_str(uri);
3112 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
3113 break;
3114
3115 case 'M':
3116 /* Make multiple calls! : */
3117 printf("(You currently have %d calls)\n",
3118 pjsua_call_get_count());
3119
3120 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
3121 continue;
3122
3123 count = my_atoi(menuin);
3124 if (count < 1)
3125 continue;
3126
3127 ui_input_url("Make call", buf, sizeof(buf), &result);
3128 if (result.nb_result != NO_NB) {
3129 pjsua_buddy_info binfo;
3130 if (result.nb_result == -1 || result.nb_result == 0) {
3131 puts("You can't do that with make call!");
3132 continue;
3133 }
3134 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3135 uri = binfo.uri.ptr;
3136 } else {
3137 uri = result.uri_result;
3138 }
3139
3140 for (i=0; i<my_atoi(menuin); ++i) {
3141 pj_status_t status;
3142
3143 tmp = pj_str(uri);
3144 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
3145 NULL, NULL);
3146 if (status != PJ_SUCCESS)
3147 break;
3148 }
3149 break;
3150
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003151 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00003152 i = pjsua_detect_nat_type();
3153 if (i != PJ_SUCCESS)
3154 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003155 break;
3156
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003157 case 'i':
3158 /* Send instant messaeg */
3159
3160 /* i is for call index to send message, if any */
3161 i = -1;
3162
3163 /* Make compiler happy. */
3164 uri = NULL;
3165
3166 /* Input destination. */
3167 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3168 if (result.nb_result != NO_NB) {
3169
3170 if (result.nb_result == -1) {
3171 puts("You can't send broadcast IM like that!");
3172 continue;
3173
3174 } else if (result.nb_result == 0) {
3175
3176 i = current_call;
3177
3178 } else {
3179 pjsua_buddy_info binfo;
3180 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3181 uri = binfo.uri.ptr;
3182 }
3183
3184 } else if (result.uri_result) {
3185 uri = result.uri_result;
3186 }
3187
3188
3189 /* Send typing indication. */
3190 if (i != -1)
3191 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3192 else {
3193 pj_str_t tmp_uri = pj_str(uri);
3194 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3195 }
3196
3197 /* Input the IM . */
3198 if (!simple_input("Message", text, sizeof(text))) {
3199 /*
3200 * Cancelled.
3201 * Send typing notification too, saying we're not typing.
3202 */
3203 if (i != -1)
3204 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3205 else {
3206 pj_str_t tmp_uri = pj_str(uri);
3207 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3208 }
3209 continue;
3210 }
3211
3212 tmp = pj_str(text);
3213
3214 /* Send the IM */
3215 if (i != -1)
3216 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3217 else {
3218 pj_str_t tmp_uri = pj_str(uri);
3219 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3220 }
3221
3222 break;
3223
3224 case 'a':
3225
3226 if (current_call != -1) {
3227 pjsua_call_get_info(current_call, &call_info);
3228 } else {
3229 /* Make compiler happy */
3230 call_info.role = PJSIP_ROLE_UAC;
3231 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3232 }
3233
3234 if (current_call == -1 ||
3235 call_info.role != PJSIP_ROLE_UAS ||
3236 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3237 {
3238 puts("No pending incoming call");
3239 fflush(stdout);
3240 continue;
3241
3242 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003243 int st_code;
3244 char contact[120];
3245 pj_str_t hname = { "Contact", 7 };
3246 pj_str_t hvalue;
3247 pjsip_generic_string_hdr hcontact;
3248 pjsua_msg_data msg_data;
3249
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003250 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3251 continue;
3252
Benny Prijono20d36722007-02-22 14:52:24 +00003253 st_code = my_atoi(buf);
3254 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003255 continue;
3256
Benny Prijono20d36722007-02-22 14:52:24 +00003257 pjsua_msg_data_init(&msg_data);
3258
3259 if (st_code/100 == 3) {
3260 if (!simple_input("Enter URL to be put in Contact",
3261 contact, sizeof(contact)))
3262 continue;
3263 hvalue = pj_str(contact);
3264 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3265
3266 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3267 }
3268
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003269 /*
3270 * Must check again!
3271 * Call may have been disconnected while we're waiting for
3272 * keyboard input.
3273 */
3274 if (current_call == -1) {
3275 puts("Call has been disconnected");
3276 fflush(stdout);
3277 continue;
3278 }
3279
Benny Prijono20d36722007-02-22 14:52:24 +00003280 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003281 }
3282
3283 break;
3284
3285
3286 case 'h':
3287
3288 if (current_call == -1) {
3289 puts("No current call");
3290 fflush(stdout);
3291 continue;
3292
3293 } else if (menuin[1] == 'a') {
3294
3295 /* Hangup all calls */
3296 pjsua_call_hangup_all();
3297
3298 } else {
3299
3300 /* Hangup current calls */
3301 pjsua_call_hangup(current_call, 0, NULL, NULL);
3302 }
3303 break;
3304
3305 case ']':
3306 case '[':
3307 /*
3308 * Cycle next/prev dialog.
3309 */
3310 if (menuin[0] == ']') {
3311 find_next_call();
3312
3313 } else {
3314 find_prev_call();
3315 }
3316
3317 if (current_call != -1) {
3318
3319 pjsua_call_get_info(current_call, &call_info);
3320 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3321 (int)call_info.remote_info.slen,
3322 call_info.remote_info.ptr));
3323
3324 } else {
3325 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3326 }
3327 break;
3328
3329
3330 case '>':
3331 case '<':
3332 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3333 break;
3334
3335 i = my_atoi(buf);
3336 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003337 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003338 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3339 } else {
3340 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3341 }
3342 break;
3343
3344
3345 case '+':
3346 if (menuin[1] == 'b') {
3347
3348 pjsua_buddy_config buddy_cfg;
3349 pjsua_buddy_id buddy_id;
3350 pj_status_t status;
3351
3352 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3353 break;
3354
3355 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3356 printf("Invalid SIP URI '%s'\n", buf);
3357 break;
3358 }
3359
Benny Prijonoac623b32006-07-03 15:19:31 +00003360 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003361
3362 buddy_cfg.uri = pj_str(buf);
3363 buddy_cfg.subscribe = PJ_TRUE;
3364
3365 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3366 if (status == PJ_SUCCESS) {
3367 printf("New buddy '%s' added at index %d\n",
3368 buf, buddy_id+1);
3369 }
3370
3371 } else if (menuin[1] == 'a') {
3372
Benny Prijonofb2b3652007-06-28 07:15:03 +00003373 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3374 pjsua_acc_config acc_cfg;
3375 pj_status_t status;
3376
3377 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3378 break;
3379 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3380 break;
3381 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3382 break;
3383 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3384 break;
3385 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3386 break;
3387
3388 pjsua_acc_config_default(&acc_cfg);
3389 acc_cfg.id = pj_str(id);
3390 acc_cfg.reg_uri = pj_str(registrar);
3391 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003392 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003393 acc_cfg.cred_info[0].realm = pj_str(realm);
3394 acc_cfg.cred_info[0].username = pj_str(uname);
3395 acc_cfg.cred_info[0].data_type = 0;
3396 acc_cfg.cred_info[0].data = pj_str(passwd);
3397
3398 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3399 if (status != PJ_SUCCESS) {
3400 pjsua_perror(THIS_FILE, "Error adding new account", status);
3401 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003402
3403 } else {
3404 printf("Invalid input %s\n", menuin);
3405 }
3406 break;
3407
3408 case '-':
3409 if (menuin[1] == 'b') {
3410 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3411 break;
3412
3413 i = my_atoi(buf) - 1;
3414
3415 if (!pjsua_buddy_is_valid(i)) {
3416 printf("Invalid buddy id %d\n", i);
3417 } else {
3418 pjsua_buddy_del(i);
3419 printf("Buddy %d deleted\n", i);
3420 }
3421
3422 } else if (menuin[1] == 'a') {
3423
3424 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3425 break;
3426
3427 i = my_atoi(buf);
3428
3429 if (!pjsua_acc_is_valid(i)) {
3430 printf("Invalid account id %d\n", i);
3431 } else {
3432 pjsua_acc_del(i);
3433 printf("Account %d deleted\n", i);
3434 }
3435
3436 } else {
3437 printf("Invalid input %s\n", menuin);
3438 }
3439 break;
3440
3441 case 'H':
3442 /*
3443 * Hold call.
3444 */
3445 if (current_call != -1) {
3446
3447 pjsua_call_set_hold(current_call, NULL);
3448
3449 } else {
3450 PJ_LOG(3,(THIS_FILE, "No current call"));
3451 }
3452 break;
3453
3454 case 'v':
3455 /*
3456 * Send re-INVITE (to release hold, etc).
3457 */
3458 if (current_call != -1) {
3459
3460 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3461
3462 } else {
3463 PJ_LOG(3,(THIS_FILE, "No current call"));
3464 }
3465 break;
3466
Benny Prijonoc08682e2007-10-04 06:17:58 +00003467 case 'U':
3468 /*
3469 * Send UPDATE
3470 */
3471 if (current_call != -1) {
3472
3473 pjsua_call_update(current_call, 0, NULL);
3474
3475 } else {
3476 PJ_LOG(3,(THIS_FILE, "No current call"));
3477 }
3478 break;
3479
3480 case 'C':
3481 if (menuin[1] == 'p') {
3482 manage_codec_prio();
3483 }
3484 break;
3485
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003486 case 'x':
3487 /*
3488 * Transfer call.
3489 */
3490 if (current_call == -1) {
3491
3492 PJ_LOG(3,(THIS_FILE, "No current call"));
3493
3494 } else {
3495 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003496 pjsua_msg_data msg_data;
3497 pjsip_generic_string_hdr refer_sub;
3498 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3499 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003500 pjsua_call_info ci;
3501
3502 pjsua_call_get_info(current_call, &ci);
3503 printf("Transfering current call [%d] %.*s\n",
3504 current_call,
3505 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003506
3507 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3508
3509 /* Check if call is still there. */
3510
3511 if (call != current_call) {
3512 puts("Call has been disconnected");
3513 continue;
3514 }
3515
Benny Prijonod524e822006-09-22 12:48:18 +00003516 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003517 if (app_config.no_refersub) {
3518 /* Add Refer-Sub: false in outgoing REFER request */
3519 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3520 &STR_FALSE);
3521 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3522 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003523 if (result.nb_result != NO_NB) {
3524 if (result.nb_result == -1 || result.nb_result == 0)
3525 puts("You can't do that with transfer call!");
3526 else {
3527 pjsua_buddy_info binfo;
3528 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003529 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003530 }
3531
3532 } else if (result.uri_result) {
3533 pj_str_t tmp;
3534 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003535 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003536 }
3537 }
3538 break;
3539
Benny Prijonof7b1c392006-11-11 16:46:34 +00003540 case 'X':
3541 /*
3542 * Transfer call with replaces.
3543 */
3544 if (current_call == -1) {
3545
3546 PJ_LOG(3,(THIS_FILE, "No current call"));
3547
3548 } else {
3549 int call = current_call;
3550 int dst_call;
3551 pjsua_msg_data msg_data;
3552 pjsip_generic_string_hdr refer_sub;
3553 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3554 pj_str_t STR_FALSE = { "false", 5 };
3555 pjsua_call_id ids[PJSUA_MAX_CALLS];
3556 pjsua_call_info ci;
3557 unsigned i, count;
3558
3559 count = PJ_ARRAY_SIZE(ids);
3560 pjsua_enum_calls(ids, &count);
3561
3562 if (count <= 1) {
3563 puts("There are no other calls");
3564 continue;
3565 }
3566
3567 pjsua_call_get_info(current_call, &ci);
3568 printf("Transfer call [%d] %.*s to one of the following:\n",
3569 current_call,
3570 (int)ci.remote_info.slen, ci.remote_info.ptr);
3571
3572 for (i=0; i<count; ++i) {
3573 pjsua_call_info call_info;
3574
3575 if (ids[i] == call)
3576 continue;
3577
3578 pjsua_call_get_info(ids[i], &call_info);
3579 printf("%d %.*s [%.*s]\n",
3580 ids[i],
3581 (int)call_info.remote_info.slen,
3582 call_info.remote_info.ptr,
3583 (int)call_info.state_text.slen,
3584 call_info.state_text.ptr);
3585 }
3586
3587 if (!simple_input("Enter call number to be replaced",
3588 buf, sizeof(buf)))
3589 continue;
3590
3591 dst_call = my_atoi(buf);
3592
3593 /* Check if call is still there. */
3594
3595 if (call != current_call) {
3596 puts("Call has been disconnected");
3597 continue;
3598 }
3599
3600 /* Check that destination call is valid. */
3601 if (dst_call == call) {
3602 puts("Destination call number must not be the same "
3603 "as the call being transfered");
3604 continue;
3605 }
3606 if (dst_call >= PJSUA_MAX_CALLS) {
3607 puts("Invalid destination call number");
3608 continue;
3609 }
3610 if (!pjsua_call_is_active(dst_call)) {
3611 puts("Invalid destination call number");
3612 continue;
3613 }
3614
3615 pjsua_msg_data_init(&msg_data);
3616 if (app_config.no_refersub) {
3617 /* Add Refer-Sub: false in outgoing REFER request */
3618 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3619 &STR_FALSE);
3620 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3621 }
3622
3623 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3624 }
3625 break;
3626
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003627 case '#':
3628 /*
3629 * Send DTMF strings.
3630 */
3631 if (current_call == -1) {
3632
3633 PJ_LOG(3,(THIS_FILE, "No current call"));
3634
3635 } else if (!pjsua_call_has_media(current_call)) {
3636
3637 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3638
3639 } else {
3640 pj_str_t digits;
3641 int call = current_call;
3642 pj_status_t status;
3643
3644 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3645 sizeof(buf)))
3646 {
3647 break;
3648 }
3649
3650 if (call != current_call) {
3651 puts("Call has been disconnected");
3652 continue;
3653 }
3654
3655 digits = pj_str(buf);
3656 status = pjsua_call_dial_dtmf(current_call, &digits);
3657 if (status != PJ_SUCCESS) {
3658 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3659 } else {
3660 puts("DTMF digits enqueued for transmission");
3661 }
3662 }
3663 break;
3664
Benny Prijonofeb69f42007-10-05 09:12:26 +00003665 case '*':
3666 /* Send DTMF with INFO */
3667 if (current_call == -1) {
3668
3669 PJ_LOG(3,(THIS_FILE, "No current call"));
3670
3671 } else {
3672 const pj_str_t SIP_INFO = pj_str("INFO");
3673 pj_str_t digits;
3674 int call = current_call;
3675 int i;
3676 pj_status_t status;
3677
3678 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3679 sizeof(buf)))
3680 {
3681 break;
3682 }
3683
3684 if (call != current_call) {
3685 puts("Call has been disconnected");
3686 continue;
3687 }
3688
3689 digits = pj_str(buf);
3690 for (i=0; i<digits.slen; ++i) {
3691 pjsua_msg_data msg_data;
3692 char body[80];
3693
3694 pjsua_msg_data_init(&msg_data);
3695 msg_data.content_type = pj_str("application/dtmf-relay");
3696
3697 pj_ansi_snprintf(body, sizeof(body),
3698 "Signal=%c\r\n"
3699 "Duration=160",
3700 buf[i]);
3701 msg_data.msg_body = pj_str(body);
3702
3703 status = pjsua_call_send_request(current_call, &SIP_INFO,
3704 &msg_data);
3705 if (status != PJ_SUCCESS) {
3706 break;
3707 }
3708 }
3709 }
3710 break;
3711
Benny Prijono56315612006-07-18 14:39:40 +00003712 case 'S':
3713 /*
3714 * Send arbitrary request
3715 */
3716 if (pjsua_acc_get_count() == 0) {
3717 puts("Sorry, need at least one account configured");
3718 break;
3719 }
3720
3721 puts("Send arbitrary request to remote host");
3722
3723 /* Input METHOD */
3724 if (!simple_input("Request method:",text,sizeof(text)))
3725 break;
3726
3727 /* Input destination URI */
3728 uri = NULL;
3729 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3730 if (result.nb_result != NO_NB) {
3731
3732 if (result.nb_result == -1 || result.nb_result == 0) {
3733 puts("Sorry you can't do that!");
3734 continue;
3735 } else {
3736 pjsua_buddy_info binfo;
3737 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3738 uri = binfo.uri.ptr;
3739 }
3740
3741 } else if (result.uri_result) {
3742 uri = result.uri_result;
3743 }
3744
3745 tmp = pj_str(uri);
3746
3747 send_request(text, &tmp);
3748 break;
3749
Benny Prijonoebc32c32008-06-12 13:30:39 +00003750 case 'e':
3751 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3752 pj_str_t tmp;
3753
3754 tmp.ptr = menuin+5;
3755 tmp.slen = pj_ansi_strlen(menuin)-6;
3756
3757 if (tmp.slen < 1) {
3758 puts("Usage: echo [0|1]");
3759 break;
3760 }
3761
Benny Prijonoddd02de2008-06-26 22:20:11 +00003762 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003763 }
3764 break;
3765
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003766 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003767 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3768 pj_str_t tmp;
3769 int delay;
3770
3771 tmp.ptr = menuin+6;
3772 tmp.slen = pj_ansi_strlen(menuin)-7;
3773
3774 if (tmp.slen < 1) {
3775 puts("Usage: sleep MSEC");
3776 break;
3777 }
3778
3779 delay = pj_strtoul(&tmp);
3780 if (delay < 0) delay = 0;
3781 pj_thread_sleep(delay);
3782 break;
3783 }
3784 /* Continue below */
3785
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003786 case 'u':
3787 /*
3788 * Subscribe/unsubscribe presence.
3789 */
3790 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3791 if (result.nb_result != NO_NB) {
3792 if (result.nb_result == -1) {
3793 int i, count;
3794 count = pjsua_get_buddy_count();
3795 for (i=0; i<count; ++i)
3796 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3797 } else if (result.nb_result == 0) {
3798 puts("Sorry, can only subscribe to buddy's presence, "
3799 "not from existing call");
3800 } else {
3801 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3802 }
3803
3804 } else if (result.uri_result) {
3805 puts("Sorry, can only subscribe to buddy's presence, "
3806 "not arbitrary URL (for now)");
3807 }
3808
3809 break;
3810
3811 case 'r':
3812 switch (menuin[1]) {
3813 case 'r':
3814 /*
3815 * Re-Register.
3816 */
3817 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3818 break;
3819 case 'u':
3820 /*
3821 * Unregister
3822 */
3823 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3824 break;
3825 }
3826 break;
3827
3828 case 't':
3829 pjsua_acc_get_info(current_acc, &acc_info);
3830 acc_info.online_status = !acc_info.online_status;
3831 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3832 printf("Setting %s online status to %s\n",
3833 acc_info.acc_uri.ptr,
3834 (acc_info.online_status?"online":"offline"));
3835 break;
3836
Benny Prijono4461c7d2007-08-25 13:36:15 +00003837 case 'T':
3838 change_online_status();
3839 break;
3840
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003841 case 'c':
3842 switch (menuin[1]) {
3843 case 'l':
3844 conf_list();
3845 break;
3846 case 'c':
3847 case 'd':
3848 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003849 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003850 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003851 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003852 const char *src_title, *dst_title;
3853
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003854 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003855
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003856 if (cnt != 3) {
3857 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003858
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003859 src_title = (menuin[1]=='c'?
3860 "Connect src port #":
3861 "Disconnect src port #");
3862 dst_title = (menuin[1]=='c'?
3863 "To dst port #":
3864 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003865
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003866 if (!simple_input(src_title, src_port, sizeof(src_port)))
3867 break;
3868
3869 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3870 break;
3871 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003872
3873 if (menuin[1]=='c') {
3874 status = pjsua_conf_connect(my_atoi(src_port),
3875 my_atoi(dst_port));
3876 } else {
3877 status = pjsua_conf_disconnect(my_atoi(src_port),
3878 my_atoi(dst_port));
3879 }
3880 if (status == PJ_SUCCESS) {
3881 puts("Success");
3882 } else {
3883 puts("ERROR!!");
3884 }
3885 }
3886 break;
3887 }
3888 break;
3889
Benny Prijono6dd967c2006-12-26 02:27:14 +00003890 case 'V':
3891 /* Adjust audio volume */
3892 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3893 if (simple_input(buf,text,sizeof(text))) {
3894 char *err;
3895 app_config.mic_level = (float)strtod(text, &err);
3896 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3897 }
3898 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3899 app_config.speaker_level);
3900 if (simple_input(buf,text,sizeof(text))) {
3901 char *err;
3902 app_config.speaker_level = (float)strtod(text, &err);
3903 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3904 }
3905
3906 break;
3907
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003908 case 'd':
3909 if (menuin[1] == 'c') {
3910 char settings[2000];
3911 int len;
3912
3913 len = write_settings(&app_config, settings, sizeof(settings));
3914 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003915 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003916 else
3917 PJ_LOG(3,(THIS_FILE,
3918 "Dumping configuration (%d bytes):\n%s\n",
3919 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003920
3921 } else if (menuin[1] == 'q') {
3922
3923 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00003924 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00003925 } else {
3926 PJ_LOG(3,(THIS_FILE, "No current call"));
3927 }
3928
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003929 } else {
3930 app_dump(menuin[1]=='d');
3931 }
3932 break;
3933
3934
3935 case 'f':
3936 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3937 char settings[2000];
3938 int len;
3939
3940 len = write_settings(&app_config, settings, sizeof(settings));
3941 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003942 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003943 else {
3944 pj_oshandle_t fd;
3945 pj_status_t status;
3946
3947 status = pj_file_open(app_config.pool, buf,
3948 PJ_O_WRONLY, &fd);
3949 if (status != PJ_SUCCESS) {
3950 pjsua_perror(THIS_FILE, "Unable to open file", status);
3951 } else {
3952 pj_ssize_t size = len;
3953 pj_file_write(fd, settings, &size);
3954 pj_file_close(fd);
3955
3956 printf("Settings successfully written to '%s'\n", buf);
3957 }
3958 }
3959
3960 }
3961 break;
3962
3963
3964 case 'q':
3965 goto on_exit;
3966
3967
Benny Prijono5e51a4e2008-11-27 00:06:46 +00003968 case 'R':
3969 if (!pjsua_call_is_active(current_call)) {
3970 PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
3971 } else if (menuin[1] == 'a') {
3972 pjsua_call_process_redirect(current_call,
3973 PJSIP_REDIRECT_ACCEPT);
3974 } else if (menuin[1] == 'r') {
3975 pjsua_call_process_redirect(current_call,
3976 PJSIP_REDIRECT_REJECT);
3977 } else {
3978 pjsua_call_process_redirect(current_call,
3979 PJSIP_REDIRECT_STOP);
3980 }
3981 break;
3982
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003983 default:
3984 if (menuin[0] != '\n' && menuin[0] != '\r') {
3985 printf("Invalid input %s", menuin);
3986 }
3987 keystroke_help();
3988 break;
3989 }
3990 }
3991
3992on_exit:
3993 ;
3994}
3995
3996
3997/*****************************************************************************
3998 * Public API
3999 */
4000
4001pj_status_t app_init(int argc, char *argv[])
4002{
Benny Prijonoe93e2872006-06-28 16:46:49 +00004003 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00004004 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004005 unsigned i;
4006 pj_status_t status;
4007
4008 /* Create pjsua */
4009 status = pjsua_create();
4010 if (status != PJ_SUCCESS)
4011 return status;
4012
4013 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00004014 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004015
4016 /* Initialize default config */
4017 default_config(&app_config);
4018
4019 /* Parse the arguments */
4020 status = parse_args(argc, argv, &app_config, &uri_arg);
4021 if (status != PJ_SUCCESS)
4022 return status;
4023
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004024 /* Initialize application callbacks */
4025 app_config.cfg.cb.on_call_state = &on_call_state;
4026 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
4027 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00004028 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00004029 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00004030 app_config.cfg.cb.on_call_redirected = &call_on_redirected;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004031 app_config.cfg.cb.on_reg_state = &on_reg_state;
Benny Prijono563cd7a2008-08-19 20:02:15 +00004032 app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004033 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
4034 app_config.cfg.cb.on_pager = &on_pager;
4035 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00004036 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00004037 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00004038 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004039
4040 /* Initialize pjsua */
4041 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
4042 &app_config.media_cfg);
4043 if (status != PJ_SUCCESS)
4044 return status;
4045
Benny Prijonoe909eac2006-07-27 22:04:56 +00004046#ifdef STEREO_DEMO
4047 stereo_demo();
4048#endif
4049
Benny Prijono804ff0a2006-09-14 11:17:48 +00004050 /* Initialize calls data */
4051 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
4052 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
4053 app_config.call_data[i].timer.cb = &call_timeout_callback;
4054 }
4055
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004056 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00004057 for (i=0; i<app_config.wav_count; ++i) {
4058 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004059 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00004060
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004061 if (app_config.auto_play_hangup)
4062 play_options |= PJMEDIA_FILE_NO_LOOP;
4063
4064 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00004065 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004066 if (status != PJ_SUCCESS)
4067 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00004068
Benny Prijono72c04d22007-02-17 22:20:58 +00004069 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00004070 app_config.wav_id = wav_id;
4071 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004072 if (app_config.auto_play_hangup) {
4073 pjmedia_port *port;
4074
4075 pjsua_player_get_port(app_config.wav_id, &port);
4076 status = pjmedia_wav_player_set_eof_cb(port, NULL,
4077 &on_playfile_done);
4078 if (status != PJ_SUCCESS)
4079 goto on_error;
4080
4081 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
4082 &hangup_timeout_callback);
4083 }
Benny Prijono32e4f492007-01-24 00:44:26 +00004084 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004085 }
4086
Benny Prijono4af234b2007-01-24 02:02:09 +00004087 /* Optionally registers tone players */
4088 for (i=0; i<app_config.tone_count; ++i) {
4089 pjmedia_port *tport;
4090 char name[80];
4091 pj_str_t label;
4092 pj_status_t status;
4093
4094 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
4095 app_config.tones[i].freq1,
4096 app_config.tones[i].freq2);
4097 label = pj_str(name);
4098 status = pjmedia_tonegen_create2(app_config.pool, &label,
4099 8000, 1, 160, 16,
4100 PJMEDIA_TONEGEN_LOOP, &tport);
4101 if (status != PJ_SUCCESS) {
4102 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
4103 goto on_error;
4104 }
4105
4106 status = pjsua_conf_add_port(app_config.pool, tport,
4107 &app_config.tone_slots[i]);
4108 pj_assert(status == PJ_SUCCESS);
4109
4110 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
4111 pj_assert(status == PJ_SUCCESS);
4112 }
4113
Benny Prijono1ebd6142006-10-19 15:48:02 +00004114 /* Optionally create recorder file, if any. */
4115 if (app_config.rec_file.slen) {
4116 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
4117 &app_config.rec_id);
4118 if (status != PJ_SUCCESS)
4119 goto on_error;
4120
4121 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
4122 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004123
Benny Prijonoe347cb02007-02-14 14:36:13 +00004124 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
4125
Benny Prijono91d20f42008-06-14 19:42:37 +00004126 /* Create ringback tones */
4127 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00004128 unsigned i, samples_per_frame;
4129 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00004130 pj_str_t name;
4131
Benny Prijono4f966892008-06-14 22:43:56 +00004132 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
4133 app_config.media_cfg.clock_rate *
4134 app_config.media_cfg.channel_count / 1000;
4135
Benny Prijono91d20f42008-06-14 19:42:37 +00004136 /* Ringback tone (call is ringing) */
4137 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00004138 status = pjmedia_tonegen_create2(app_config.pool, &name,
4139 app_config.media_cfg.clock_rate,
4140 app_config.media_cfg.channel_count,
4141 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004142 16, PJMEDIA_TONEGEN_LOOP,
4143 &app_config.ringback_port);
4144 if (status != PJ_SUCCESS)
4145 goto on_error;
4146
Benny Prijonoe28cec82008-06-14 20:40:53 +00004147 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00004148 for (i=0; i<RINGBACK_CNT; ++i) {
4149 tone[i].freq1 = RINGBACK_FREQ1;
4150 tone[i].freq2 = RINGBACK_FREQ2;
4151 tone[i].on_msec = RINGBACK_ON;
4152 tone[i].off_msec = RINGBACK_OFF;
4153 }
4154 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004155
Benny Prijono4f966892008-06-14 22:43:56 +00004156 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004157 PJMEDIA_TONEGEN_LOOP);
4158
4159
Benny Prijono91d20f42008-06-14 19:42:37 +00004160 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
4161 &app_config.ringback_slot);
4162 if (status != PJ_SUCCESS)
4163 goto on_error;
4164
4165 /* Ring (to alert incoming call) */
4166 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00004167 status = pjmedia_tonegen_create2(app_config.pool, &name,
4168 app_config.media_cfg.clock_rate,
4169 app_config.media_cfg.channel_count,
4170 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004171 16, PJMEDIA_TONEGEN_LOOP,
4172 &app_config.ring_port);
4173 if (status != PJ_SUCCESS)
4174 goto on_error;
4175
Benny Prijono4f966892008-06-14 22:43:56 +00004176 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00004177 tone[i].freq1 = RING_FREQ1;
4178 tone[i].freq2 = RING_FREQ2;
4179 tone[i].on_msec = RING_ON;
4180 tone[i].off_msec = RING_OFF;
4181 }
Benny Prijono4f966892008-06-14 22:43:56 +00004182 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004183
Benny Prijono4f966892008-06-14 22:43:56 +00004184 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004185 tone, PJMEDIA_TONEGEN_LOOP);
4186
Benny Prijono91d20f42008-06-14 19:42:37 +00004187 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4188 &app_config.ring_slot);
4189 if (status != PJ_SUCCESS)
4190 goto on_error;
4191
4192 }
4193
Benny Prijono87ef89a2007-01-14 00:39:45 +00004194 /* Add UDP transport unless it's disabled. */
4195 if (!app_config.no_udp) {
4196 pjsua_acc_id aid;
4197
4198 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
4199 &app_config.udp_cfg,
4200 &transport_id);
4201 if (status != PJ_SUCCESS)
4202 goto on_error;
4203
4204 /* Add local account */
4205 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4206 //pjsua_acc_set_transport(aid, transport_id);
4207 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004208
4209 if (app_config.udp_cfg.port == 0) {
4210 pjsua_transport_info ti;
4211 pj_sockaddr_in *a;
4212
4213 pjsua_transport_get_info(transport_id, &ti);
4214 a = (pj_sockaddr_in*)&ti.local_addr;
4215
4216 tcp_cfg.port = pj_ntohs(a->sin_port);
4217 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004218 }
4219
Benny Prijonoe93e2872006-06-28 16:46:49 +00004220 /* Add TCP transport unless it's disabled */
4221 if (!app_config.no_tcp) {
4222 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004223 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004224 &transport_id);
4225 if (status != PJ_SUCCESS)
4226 goto on_error;
4227
4228 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004229 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004230 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4231
4232 }
4233
Benny Prijonoe93e2872006-06-28 16:46:49 +00004234
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004235#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4236 /* Add TLS transport when application wants one */
4237 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004238
4239 pjsua_acc_id acc_id;
4240
4241 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004242 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004243 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004244 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004245 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004246 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004247 if (status != PJ_SUCCESS)
4248 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004249
4250 /* Add local account */
4251 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4252 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004253 }
4254#endif
4255
Benny Prijonoe93e2872006-06-28 16:46:49 +00004256 if (transport_id == -1) {
Benny Prijonod6e362a2008-07-19 17:53:47 +00004257 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
Benny Prijonoe93e2872006-06-28 16:46:49 +00004258 status = -1;
4259 goto on_error;
4260 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004261
4262
4263 /* Add accounts */
4264 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004265 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004266 if (status != PJ_SUCCESS)
4267 goto on_error;
4268 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4269 }
4270
4271 /* Add buddies */
4272 for (i=0; i<app_config.buddy_cnt; ++i) {
4273 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4274 if (status != PJ_SUCCESS)
4275 goto on_error;
4276 }
4277
4278 /* Optionally set codec orders */
4279 for (i=0; i<app_config.codec_cnt; ++i) {
4280 pjsua_codec_set_priority(&app_config.codec_arg[i],
4281 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4282 }
4283
Benny Prijonofce28542007-12-09 15:41:10 +00004284 /* Optionally disable some codec */
4285 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4286 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4287 }
4288
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004289 /* Add RTP transports */
Benny Prijono40860c32008-09-04 13:55:33 +00004290#ifdef TRANSPORT_ADAPTER_SAMPLE
4291 status = transport_adapter_sample();
4292
4293#else
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004294 status = pjsua_media_transports_create(&app_config.rtp_cfg);
Benny Prijono40860c32008-09-04 13:55:33 +00004295#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004296 if (status != PJ_SUCCESS)
4297 goto on_error;
4298
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004299 /* Set sound device latency */
4300 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4301
Benny Prijono22a300a2006-06-14 20:04:55 +00004302 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004303#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004304 if (app_config.null_audio) {
4305 status = pjsua_set_null_snd_dev();
4306 if (status != PJ_SUCCESS)
4307 return status;
4308 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004309#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004310
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004311 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4312 app_config.playback_dev != PJSUA_INVALID_ID)
4313 {
4314 status = pjsua_set_snd_dev(app_config.capture_dev,
4315 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004316 if (status != PJ_SUCCESS)
4317 goto on_error;
4318 }
4319
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004320 return PJ_SUCCESS;
4321
4322on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004323 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004324 return status;
4325}
4326
4327
Benny Prijonoebc32c32008-06-12 13:30:39 +00004328static int stdout_refresh_proc(void *arg)
4329{
4330 PJ_UNUSED_ARG(arg);
4331
4332 /* Set thread to lowest priority so that it doesn't clobber
4333 * stdout output
4334 */
4335 pj_thread_set_prio(pj_thread_this(),
4336 pj_thread_get_prio_min(pj_thread_this()));
4337
4338 while (!stdout_refresh_quit) {
4339 pj_thread_sleep(stdout_refresh * 1000);
4340 puts(stdout_refresh_text);
4341 fflush(stdout);
4342 }
4343
4344 return 0;
4345}
4346
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004347pj_status_t app_main(void)
4348{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004349 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004350 pj_status_t status;
4351
4352 /* Start pjsua */
4353 status = pjsua_start();
4354 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004355 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004356 return status;
4357 }
4358
Benny Prijonoebc32c32008-06-12 13:30:39 +00004359 /* Start console refresh thread */
4360 if (stdout_refresh > 0) {
4361 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4362 NULL, 0, 0, &stdout_refresh_thread);
4363 }
4364
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004365 console_app_main(&uri_arg);
4366
Benny Prijonoebc32c32008-06-12 13:30:39 +00004367 if (stdout_refresh_thread) {
4368 stdout_refresh_quit = PJ_TRUE;
4369 pj_thread_join(stdout_refresh_thread);
4370 pj_thread_destroy(stdout_refresh_thread);
4371 }
4372
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004373 return PJ_SUCCESS;
4374}
4375
4376pj_status_t app_destroy(void)
4377{
Benny Prijonof762ee72006-12-01 11:14:37 +00004378 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004379 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004380
Benny Prijonoe909eac2006-07-27 22:04:56 +00004381#ifdef STEREO_DEMO
4382 if (app_config.snd) {
4383 pjmedia_snd_port_destroy(app_config.snd);
4384 app_config.snd = NULL;
4385 }
4386#endif
4387
Benny Prijono91d20f42008-06-14 19:42:37 +00004388 /* Close ringback port */
4389 if (app_config.ringback_port &&
4390 app_config.ringback_slot != PJSUA_INVALID_ID)
4391 {
4392 pjsua_conf_remove_port(app_config.ringback_slot);
4393 app_config.ringback_slot = PJSUA_INVALID_ID;
4394 pjmedia_port_destroy(app_config.ringback_port);
4395 app_config.ringback_port = NULL;
4396 }
4397
4398 /* Close ring port */
4399 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4400 pjsua_conf_remove_port(app_config.ring_slot);
4401 app_config.ring_slot = PJSUA_INVALID_ID;
4402 pjmedia_port_destroy(app_config.ring_port);
4403 app_config.ring_port = NULL;
4404 }
4405
Benny Prijono4af234b2007-01-24 02:02:09 +00004406 /* Close tone generators */
4407 for (i=0; i<app_config.tone_count; ++i) {
4408 pjsua_conf_remove_port(app_config.tone_slots[i]);
4409 }
4410
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004411 if (app_config.pool) {
4412 pj_pool_release(app_config.pool);
4413 app_config.pool = NULL;
4414 }
4415
Benny Prijonof762ee72006-12-01 11:14:37 +00004416 status = pjsua_destroy();
4417
4418 pj_bzero(&app_config, sizeof(app_config));
4419
4420 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004421}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004422
4423
4424#ifdef STEREO_DEMO
4425static void stereo_demo()
4426{
4427 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004428 pj_status_t status;
4429
4430 /* Disable existing sound device */
4431 conf = pjsua_set_no_snd_dev();
4432
Benny Prijonoe909eac2006-07-27 22:04:56 +00004433 /* Create stereo-mono splitter/combiner */
4434 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004435 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004436 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004437 2 * conf->info.samples_per_frame,
4438 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004439 0 /* options */,
4440 &splitter);
4441 pj_assert(status == PJ_SUCCESS);
4442
4443 /* Connect channel0 (left channel?) to conference port slot0 */
4444 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4445 0 /*options*/,
4446 conf);
4447 pj_assert(status == PJ_SUCCESS);
4448
4449 /* Create reverse channel for channel1 (right channel?)... */
4450 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4451 splitter,
4452 1 /* ch1 */,
4453 0 /* options */,
4454 &ch1);
4455 pj_assert(status == PJ_SUCCESS);
4456
4457 /* .. and register it to conference bridge (it would be slot1
4458 * if there's no other devices connected to the bridge)
4459 */
4460 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4461 pj_assert(status == PJ_SUCCESS);
4462
4463 /* Create sound device */
4464 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004465 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004466 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004467 2 * conf->info.samples_per_frame,
4468 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004469 0, &app_config.snd);
4470 pj_assert(status == PJ_SUCCESS);
4471
4472
4473 /* Connect the splitter to the sound device */
4474 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4475 pj_assert(status == PJ_SUCCESS);
4476
4477}
4478#endif
4479
Benny Prijono40860c32008-09-04 13:55:33 +00004480#ifdef TRANSPORT_ADAPTER_SAMPLE
4481static pj_status_t create_transport_adapter(pjmedia_endpt *med_endpt, int port,
4482 pjmedia_transport **p_tp)
4483{
4484 pjmedia_transport *udp;
4485 pj_status_t status;
4486
4487 /* Create the UDP media transport */
4488 status = pjmedia_transport_udp_create(med_endpt, NULL, port, 0, &udp);
4489 if (status != PJ_SUCCESS)
4490 return status;
4491
4492 /* Create the adapter */
4493 status = pjmedia_tp_adapter_create(med_endpt, NULL, udp, p_tp);
4494 if (status != PJ_SUCCESS) {
4495 pjmedia_transport_close(udp);
4496 return status;
4497 }
4498
4499 return PJ_SUCCESS;
4500}
4501
4502static pj_status_t transport_adapter_sample(void)
4503{
4504 pjsua_media_transport tp[PJSUA_MAX_CALLS];
4505 pj_status_t status;
4506 int port = 7000;
4507 unsigned i;
4508
4509 for (i=0; i<app_config.cfg.max_calls; ++i) {
4510 status = create_transport_adapter(pjsua_get_pjmedia_endpt(),
4511 port + i*10,
4512 &tp[i].transport);
4513 if (status != PJ_SUCCESS)
4514 return status;
4515 }
4516
4517 return pjsua_media_transports_attach(tp, i, PJ_TRUE);
4518}
4519#endif
4520