blob: e8e88750564869c38d19b1917d022c9e314f0fb5 [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");
Benny Prijono30fe4852008-12-10 16:54:16 +0000180 puts (" --contact-params=S Append the specified parameters S in Contact URI");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181 puts (" --proxy=url Optional URL of proxy server to visit");
182 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000183 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000184 puts (" --realm=string Set realm");
185 puts (" --username=string Set authentication username");
186 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000187 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000188 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000189 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
190 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000191 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000192 puts ("");
193 puts ("SIP Account Control:");
194 puts (" --next-account Add more account");
195 puts ("");
196 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000197 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
198 puts (" TCP and UDP transports on the specified port, unless");
199 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000200 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
201 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000202 puts (" --no-tcp Disable TCP transport.");
203 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000204 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
205 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000206 puts (" --outbound=url Set the URL of global outbound proxy server");
207 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000208 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000209 puts ("");
210 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000211 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000212 puts (" --tls-ca-file Specify TLS CA file (default=none)");
213 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
214 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
215 puts (" --tls-password Specify TLS password to private key file (default=none)");
216 puts (" --tls-verify-server Verify server's certificate (default=no)");
217 puts (" --tls-verify-client Verify client's certificate (default=no)");
218 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
Benny Prijonoe10db842008-07-01 15:31:59 +0000219 puts (" --tls-srv-name Specify TLS server name for multi-hosting server (optional)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000220
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000221 puts ("");
222 puts ("Media Options:");
223 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000224 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonof3758ee2008-02-26 15:32:16 +0000225 puts (" --clock-rate=N Override conference bridge clock rate");
226 puts (" --snd-clock-rate=N Override sound device clock rate");
Benny Prijono7d60d052008-03-29 12:24:20 +0000227 puts (" --stereo Audio device and conference bridge opened in stereo mode");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000228 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000229 puts (" --play-file=file Register WAV file in conference bridge.");
230 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000231 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
232 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
233 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000234 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000235 puts (" --auto-play Automatically play the file (to incoming calls only)");
236 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000237 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000238 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
239 puts (" --auto-rec Automatically record conversation");
Benny Prijono00cae612006-07-31 15:19:36 +0000240 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000241 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000242 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000243 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000244 puts (" --ec-opt=OPT Select echo canceller algorithm (0=default, ");
245 puts (" 1=speex, 2=suppressor)");
Benny Prijono00cae612006-07-31 15:19:36 +0000246 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000247 puts (" --capture-dev=id Audio capture device ID (default=-1)");
248 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +0000249 puts (" --capture-lat=N Audio capture latency, in ms (default=100)");
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000250 puts (" --playback-lat=N Audio playback latency, in ms (default=100)");
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000251 puts (" --snd-auto-close=N Auto close audio device when it is idle for N seconds.");
252 puts (" Specify N=-1 (default) to disable this feature.");
253 puts (" Specify N=0 for instant close when unused.");
Benny Prijono91d20f42008-06-14 19:42:37 +0000254 puts (" --no-tones Disable audible tones");
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000255 puts (" --jb-max-size Specify jitter buffer maximum size, in frames (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000256
Benny Prijonof76e1392008-06-06 14:51:48 +0000257 puts ("");
258 puts ("Media Transport Options:");
259 puts (" --use-ice Enable ICE (default:no)");
Benny Prijono551af422008-08-07 09:55:52 +0000260 puts (" --ice-no-host Disable ICE host candidates (default: no)");
261 puts (" --ice-no-rtcp Disable RTCP component in ICE (default: no)");
Benny Prijonof76e1392008-06-06 14:51:48 +0000262 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
263 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
264 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
265 puts (" --use-turn Enable TURN relay with ICE (default:no)");
266 puts (" --turn-srv Domain or host name of TURN server (\"NAME:PORT\" format)");
267 puts (" --turn-tcp Use TCP connection to TURN server (default no)");
268 puts (" --turn-user TURN username");
269 puts (" --turn-passwd TURN password");
Benny Prijono0a12f002006-07-26 17:05:39 +0000270
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000271 puts ("");
272 puts ("Buddy List (can be more than one):");
273 puts (" --add-buddy url Add the specified URL to the buddy list.");
274 puts ("");
275 puts ("User Agent options:");
276 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
277 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000278 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000279 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000280 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000281 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono91d06b62008-09-20 12:16:56 +0000282 puts (" --no-force-lr Allow strict-route to be used (i.e. do not force lr)");
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000283 puts (" --accept-redirect=N Specify how to handle call redirect (3xx) response.");
284 puts (" 0: reject, 1: follow automatically (default), 2: ask");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000285
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000286 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000287 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
288 puts ("");
289
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000290 fflush(stdout);
291}
292
293
294/* Set default config. */
295static void default_config(struct app_config *cfg)
296{
Benny Prijono56315612006-07-18 14:39:40 +0000297 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000298 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000299
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000300 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000301 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000302 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
303
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000304 pjsua_logging_config_default(&cfg->log_cfg);
305 pjsua_media_config_default(&cfg->media_cfg);
306 pjsua_transport_config_default(&cfg->udp_cfg);
307 cfg->udp_cfg.port = 5060;
308 pjsua_transport_config_default(&cfg->rtp_cfg);
309 cfg->rtp_cfg.port = 4000;
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000310 cfg->redir_op = PJSIP_REDIRECT_ACCEPT;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000311 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000312 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000313 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000314 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000315 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000316 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000317 cfg->capture_dev = PJSUA_INVALID_ID;
318 cfg->playback_dev = PJSUA_INVALID_ID;
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000319 cfg->capture_lat = PJMEDIA_SND_DEFAULT_REC_LATENCY;
320 cfg->playback_lat = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono91d20f42008-06-14 19:42:37 +0000321 cfg->ringback_slot = PJSUA_INVALID_ID;
322 cfg->ring_slot = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000323
324 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
325 pjsua_acc_config_default(&cfg->acc_cfg[i]);
326
327 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
328 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329}
330
331
332/*
333 * Read command arguments from config file.
334 */
335static int read_config_file(pj_pool_t *pool, const char *filename,
336 int *app_argc, char ***app_argv)
337{
338 int i;
339 FILE *fhnd;
340 char line[200];
341 int argc = 0;
342 char **argv;
343 enum { MAX_ARGS = 64 };
344
345 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
346 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
347 argv[argc++] = *app_argv[0];
348
349 /* Open config file. */
350 fhnd = fopen(filename, "rt");
351 if (!fhnd) {
352 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
353 fflush(stdout);
354 return -1;
355 }
356
357 /* Scan tokens in the file. */
358 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000359 char *token;
360 char *p;
361 const char *whitespace = " \t\r\n";
362 char cDelimiter;
363 int len, token_len;
364
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000365 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000366
367 // Trim ending newlines
368 len = strlen(line);
369 if (line[len-1]=='\n')
370 line[--len] = '\0';
371 if (line[len-1]=='\r')
372 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000373
Benny Prijonobf5b4692007-06-28 03:20:17 +0000374 if (len==0) continue;
375
376 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
377 // first, scan whitespaces
378 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
379
380 if (*p == '\0') // are we done yet?
381 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000382
Benny Prijonobf5b4692007-06-28 03:20:17 +0000383 if (*p == '"' || *p == '\'') { // is token a quoted string
384 cDelimiter = *p++; // save quote delimiter
385 token = p;
386
387 while (*p != '\0' && *p != cDelimiter) p++;
388
389 if (*p == '\0') // found end of the line, but,
390 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000391
Benny Prijonobf5b4692007-06-28 03:20:17 +0000392 } else { // token's not a quoted string
393 token = p;
394
395 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
396
397 cDelimiter = *p;
398 }
399
400 *p = '\0';
401 token_len = p-token;
402
403 if (token_len > 0) {
404 if (*token == '#')
405 break; // ignore remainder of line
406
407 argv[argc] = pj_pool_alloc(pool, token_len + 1);
408 pj_memcpy(argv[argc], token, token_len + 1);
409 ++argc;
410 }
411
412 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000413 }
414 }
415
416 /* Copy arguments from command line */
417 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
418 argv[argc++] = (*app_argv)[i];
419
420 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
421 PJ_LOG(1,(THIS_FILE,
422 "Too many arguments specified in cmd line/config file"));
423 fflush(stdout);
424 fclose(fhnd);
425 return -1;
426 }
427
428 fclose(fhnd);
429
430 /* Assign the new command line back to the original command line. */
431 *app_argc = argc;
432 *app_argv = argv;
433 return 0;
434
435}
436
437static int my_atoi(const char *cs)
438{
439 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000440
441 pj_cstr(&s, cs);
442 if (cs[0] == '-') {
443 s.ptr++, s.slen--;
444 return 0 - (int)pj_strtoul(&s);
445 } else if (cs[0] == '+') {
446 s.ptr++, s.slen--;
447 return pj_strtoul(&s);
448 } else {
449 return pj_strtoul(&s);
450 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000451}
452
453
454/* Parse arguments. */
455static pj_status_t parse_args(int argc, char *argv[],
456 struct app_config *cfg,
457 pj_str_t *uri_to_call)
458{
459 int c;
460 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000461 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonob7944862008-07-19 20:53:49 +0000462 OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000463 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000464 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
465 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono30fe4852008-12-10 16:54:16 +0000466 OPT_CONTACT_PARAMS,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000467 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000468 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000469 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000470 OPT_AUTO_ANSWER, OPT_AUTO_PLAY, OPT_AUTO_PLAY_HANGUP, OPT_AUTO_LOOP,
Benny Prijono7d60d052008-03-29 12:24:20 +0000471 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_SND_CLOCK_RATE, OPT_STEREO,
472 OPT_USE_ICE, OPT_USE_SRTP, OPT_SRTP_SECURE,
Benny Prijono551af422008-08-07 09:55:52 +0000473 OPT_USE_TURN, OPT_ICE_NO_HOST, OPT_ICE_NO_RTCP, OPT_TURN_SRV,
474 OPT_TURN_TCP, OPT_TURN_USER, OPT_TURN_PASSWD,
Benny Prijono4af234b2007-01-24 02:02:09 +0000475 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
476 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000477 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000478 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL, OPT_EC_OPT,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000479 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000480 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000481 OPT_NOREFERSUB, OPT_ACCEPT_REDIRECT,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000482 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
483 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
Benny Prijonoe10db842008-07-01 15:31:59 +0000484 OPT_TLS_NEG_TIMEOUT, OPT_TLS_SRV_NAME,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000485 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000486 OPT_CAPTURE_LAT, OPT_PLAYBACK_LAT, OPT_NO_TONES, OPT_JB_MAX_SIZE,
Benny Prijonoebc32c32008-06-12 13:30:39 +0000487 OPT_STDOUT_REFRESH, OPT_STDOUT_REFRESH_TEXT,
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000488#ifdef _IONBF
489 OPT_STDOUT_NO_BUF,
490#endif
Benny Prijono91d06b62008-09-20 12:16:56 +0000491 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC,
492 OPT_NO_FORCE_LR
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000493 };
494 struct pj_getopt_option long_options[] = {
495 { "config-file",1, 0, OPT_CONFIG_FILE},
496 { "log-file", 1, 0, OPT_LOG_FILE},
497 { "log-level", 1, 0, OPT_LOG_LEVEL},
498 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
Benny Prijonod6e362a2008-07-19 17:53:47 +0000499 { "color", 0, 0, OPT_COLOR},
500 { "no-color", 0, 0, OPT_NO_COLOR},
Benny Prijonob7944862008-07-19 20:53:49 +0000501 { "light-bg", 0, 0, OPT_LIGHT_BG},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 { "help", 0, 0, OPT_HELP},
503 { "version", 0, 0, OPT_VERSION},
504 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
Benny Prijonof3758ee2008-02-26 15:32:16 +0000505 { "snd-clock-rate", 1, 0, OPT_SND_CLOCK_RATE},
Benny Prijono7d60d052008-03-29 12:24:20 +0000506 { "stereo", 0, 0, OPT_STEREO},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000507 { "null-audio", 0, 0, OPT_NULL_AUDIO},
508 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000509 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000510 { "no-tcp", 0, 0, OPT_NO_TCP},
511 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000512 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000513 { "proxy", 1, 0, OPT_PROXY},
514 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
515 { "registrar", 1, 0, OPT_REGISTRAR},
516 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000517 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000518 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000519 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000520 { "id", 1, 0, OPT_ID},
521 { "contact", 1, 0, OPT_CONTACT},
Benny Prijono30fe4852008-12-10 16:54:16 +0000522 { "contact-params",1,0, OPT_CONTACT_PARAMS},
Benny Prijonofce28542007-12-09 15:41:10 +0000523 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
524 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000525 { "accept-redirect", 1, 0, OPT_ACCEPT_REDIRECT},
Benny Prijono91d06b62008-09-20 12:16:56 +0000526 { "no-force-lr",0, 0, OPT_NO_FORCE_LR},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000527 { "realm", 1, 0, OPT_REALM},
528 { "username", 1, 0, OPT_USERNAME},
529 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000530 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000531 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000532 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000533 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
534 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
535 { "no-presence", 0, 0, OPT_NO_PRESENCE},
536 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000538 { "auto-play-hangup",0, 0, OPT_AUTO_PLAY_HANGUP},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000539 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000540 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
541 { "auto-conf", 0, 0, OPT_AUTO_CONF},
542 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000543 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000544 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000545 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonof76e1392008-06-06 14:51:48 +0000546
Benny Prijonoc97608e2007-03-23 16:34:20 +0000547 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonof76e1392008-06-06 14:51:48 +0000548 { "use-turn", 0, 0, OPT_USE_TURN},
549 { "ice-no-host",0, 0, OPT_ICE_NO_HOST},
Benny Prijono551af422008-08-07 09:55:52 +0000550 { "ice-no-rtcp",0, 0, OPT_ICE_NO_RTCP},
Benny Prijonof76e1392008-06-06 14:51:48 +0000551 { "turn-srv", 1, 0, OPT_TURN_SRV},
552 { "turn-tcp", 0, 0, OPT_TURN_TCP},
553 { "turn-user", 1, 0, OPT_TURN_USER},
554 { "turn-passwd",1, 0, OPT_TURN_PASSWD},
555
Benny Prijonod8179652008-01-23 20:39:07 +0000556#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
557 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000558 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000559#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000560 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000561 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000562 { "complexity", 1, 0, OPT_COMPLEXITY},
563 { "quality", 1, 0, OPT_QUALITY},
564 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000565 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000566 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono59b3ffe2008-08-11 18:10:42 +0000567 { "ec-opt", 1, 0, OPT_EC_OPT},
Benny Prijono00cae612006-07-31 15:19:36 +0000568 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
569 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
570 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000571 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000572 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000573 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000574 { "duration", 1, 0, OPT_DURATION},
575 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000576 { "use-tls", 0, 0, OPT_USE_TLS},
577 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000578 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
579 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000580 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000581 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
582 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
583 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijonoe10db842008-07-01 15:31:59 +0000584 { "tls-srv-name", 1, 0, OPT_TLS_SRV_NAME},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000585 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
586 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Nanang Izzuddind7fefd72008-06-12 12:48:59 +0000587 { "capture-lat", 1, 0, OPT_CAPTURE_LAT},
588 { "playback-lat", 1, 0, OPT_PLAYBACK_LAT},
Benny Prijonoebc32c32008-06-12 13:30:39 +0000589 { "stdout-refresh", 1, 0, OPT_STDOUT_REFRESH},
590 { "stdout-refresh-text", 1, 0, OPT_STDOUT_REFRESH_TEXT},
Benny Prijono4d0da3a2008-06-26 20:23:47 +0000591#ifdef _IONBF
592 { "stdout-no-buf", 0, 0, OPT_STDOUT_NO_BUF },
593#endif
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000594 { "snd-auto-close", 1, 0, OPT_SND_AUTO_CLOSE},
Benny Prijono91d20f42008-06-14 19:42:37 +0000595 { "no-tones", 0, 0, OPT_NO_TONES},
Nanang Izzuddinc9853542008-07-17 16:59:07 +0000596 { "jb-max-size", 1, 0, OPT_JB_MAX_SIZE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000597 { NULL, 0, 0, 0}
598 };
599 pj_status_t status;
600 pjsua_acc_config *cur_acc;
601 char *config_file = NULL;
602 unsigned i;
603
604 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000605 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000606 while ((c=pj_getopt_long(argc, argv, "", long_options,
607 &option_index)) != -1)
608 {
609 switch (c) {
610 case OPT_CONFIG_FILE:
611 config_file = pj_optarg;
612 break;
613 }
614 if (config_file)
615 break;
616 }
617
618 if (config_file) {
619 status = read_config_file(app_config.pool, config_file, &argc, &argv);
620 if (status != 0)
621 return status;
622 }
623
624 cfg->acc_cnt = 0;
625 cur_acc = &cfg->acc_cfg[0];
626
627
628 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
629 * read from config file.
630 */
631 pj_optind = 0;
632 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000633 pj_str_t tmp;
634 long lval;
635
636 switch (c) {
637
Benny Prijono6f137482006-06-15 11:31:36 +0000638 case OPT_CONFIG_FILE:
639 /* Ignore as this has been processed before */
640 break;
641
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000642 case OPT_LOG_FILE:
643 cfg->log_cfg.log_filename = pj_str(pj_optarg);
644 break;
645
646 case OPT_LOG_LEVEL:
647 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
648 if (c < 0 || c > 6) {
649 PJ_LOG(1,(THIS_FILE,
650 "Error: expecting integer value 0-6 "
651 "for --log-level"));
652 return PJ_EINVAL;
653 }
654 cfg->log_cfg.level = c;
655 pj_log_set_level( c );
656 break;
657
658 case OPT_APP_LOG_LEVEL:
659 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
660 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
661 PJ_LOG(1,(THIS_FILE,
662 "Error: expecting integer value 0-6 "
663 "for --app-log-level"));
664 return PJ_EINVAL;
665 }
666 break;
667
Benny Prijonod6e362a2008-07-19 17:53:47 +0000668 case OPT_COLOR:
669 cfg->log_cfg.decor |= PJ_LOG_HAS_COLOR;
670 break;
671
672 case OPT_NO_COLOR:
673 cfg->log_cfg.decor &= ~PJ_LOG_HAS_COLOR;
674 break;
675
Benny Prijonob7944862008-07-19 20:53:49 +0000676 case OPT_LIGHT_BG:
677 pj_log_set_color(1, PJ_TERM_COLOR_R);
678 pj_log_set_color(2, PJ_TERM_COLOR_R | PJ_TERM_COLOR_G);
679 pj_log_set_color(3, PJ_TERM_COLOR_B | PJ_TERM_COLOR_G);
680 pj_log_set_color(4, 0);
681 pj_log_set_color(5, 0);
682 pj_log_set_color(77, 0);
683 break;
684
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000685 case OPT_HELP:
686 usage();
687 return PJ_EINVAL;
688
689 case OPT_VERSION: /* version */
690 pj_dump_config();
691 return PJ_EINVAL;
692
693 case OPT_NULL_AUDIO:
694 cfg->null_audio = PJ_TRUE;
695 break;
696
697 case OPT_CLOCK_RATE:
698 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000699 if (lval < 8000 || lval > 192000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000700 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000701 "8000-192000 for conference clock rate"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000702 return PJ_EINVAL;
703 }
704 cfg->media_cfg.clock_rate = lval;
705 break;
706
Benny Prijonof3758ee2008-02-26 15:32:16 +0000707 case OPT_SND_CLOCK_RATE:
708 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000709 if (lval < 8000 || lval > 192000) {
Benny Prijonof3758ee2008-02-26 15:32:16 +0000710 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
Benny Prijono5dfac8f2008-06-02 14:06:19 +0000711 "8000-192000 for sound device clock rate"));
Benny Prijonof3758ee2008-02-26 15:32:16 +0000712 return PJ_EINVAL;
713 }
714 cfg->media_cfg.snd_clock_rate = lval;
715 break;
716
Benny Prijono7d60d052008-03-29 12:24:20 +0000717 case OPT_STEREO:
718 cfg->media_cfg.channel_count = 2;
719 break;
720
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000721 case OPT_LOCAL_PORT: /* local-port */
722 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000723 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000724 PJ_LOG(1,(THIS_FILE,
725 "Error: expecting integer value for "
726 "--local-port"));
727 return PJ_EINVAL;
728 }
729 cfg->udp_cfg.port = (pj_uint16_t)lval;
730 break;
731
Benny Prijono0a5cad82006-09-26 13:21:02 +0000732 case OPT_IP_ADDR: /* ip-addr */
733 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
734 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
735 break;
736
Benny Prijonoe93e2872006-06-28 16:46:49 +0000737 case OPT_NO_UDP: /* no-udp */
738 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000739 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
740 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000741 }
742
743 cfg->no_udp = PJ_TRUE;
744 break;
745
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000746 case OPT_NOREFERSUB: /* norefersub */
747 cfg->no_refersub = PJ_TRUE;
748 break;
749
Benny Prijonoe93e2872006-06-28 16:46:49 +0000750 case OPT_NO_TCP: /* no-tcp */
751 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000752 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
753 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000754 }
755
756 cfg->no_tcp = PJ_TRUE;
757 break;
758
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000759 case OPT_PROXY: /* proxy */
760 if (pjsua_verify_sip_url(pj_optarg) != 0) {
761 PJ_LOG(1,(THIS_FILE,
762 "Error: invalid SIP URL '%s' "
763 "in proxy argument", pj_optarg));
764 return PJ_EINVAL;
765 }
766 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
767 break;
768
769 case OPT_OUTBOUND_PROXY: /* outbound proxy */
770 if (pjsua_verify_sip_url(pj_optarg) != 0) {
771 PJ_LOG(1,(THIS_FILE,
772 "Error: invalid SIP URL '%s' "
773 "in outbound proxy argument", pj_optarg));
774 return PJ_EINVAL;
775 }
776 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
777 break;
778
779 case OPT_REGISTRAR: /* registrar */
780 if (pjsua_verify_sip_url(pj_optarg) != 0) {
781 PJ_LOG(1,(THIS_FILE,
782 "Error: invalid SIP URL '%s' in "
783 "registrar argument", pj_optarg));
784 return PJ_EINVAL;
785 }
786 cur_acc->reg_uri = pj_str(pj_optarg);
787 break;
788
789 case OPT_REG_TIMEOUT: /* reg-timeout */
790 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
791 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
792 PJ_LOG(1,(THIS_FILE,
793 "Error: invalid value for --reg-timeout "
794 "(expecting 1-3600)"));
795 return PJ_EINVAL;
796 }
797 break;
798
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000799 case OPT_PUBLISH: /* publish */
800 cur_acc->publish_enabled = PJ_TRUE;
801 break;
802
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000803 case OPT_100REL: /** 100rel */
804 cur_acc->require_100rel = PJ_TRUE;
805 cfg->cfg.require_100rel = PJ_TRUE;
806 break;
807
Benny Prijono48ab2b72007-11-08 09:24:30 +0000808 case OPT_USE_IMS: /* Activate IMS settings */
809 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000810 break;
811
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000812 case OPT_ID: /* id */
813 if (pjsua_verify_sip_url(pj_optarg) != 0) {
814 PJ_LOG(1,(THIS_FILE,
815 "Error: invalid SIP URL '%s' "
816 "in local id argument", pj_optarg));
817 return PJ_EINVAL;
818 }
819 cur_acc->id = pj_str(pj_optarg);
820 break;
821
822 case OPT_CONTACT: /* contact */
823 if (pjsua_verify_sip_url(pj_optarg) != 0) {
824 PJ_LOG(1,(THIS_FILE,
825 "Error: invalid SIP URL '%s' "
826 "in contact argument", pj_optarg));
827 return PJ_EINVAL;
828 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000829 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000830 break;
831
Benny Prijono30fe4852008-12-10 16:54:16 +0000832 case OPT_CONTACT_PARAMS:
833 cur_acc->contact_params = pj_str(pj_optarg);
834 break;
835
Benny Prijonofce28542007-12-09 15:41:10 +0000836 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000837 cur_acc->allow_contact_rewrite = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonofce28542007-12-09 15:41:10 +0000838 break;
839
840 case OPT_USE_COMPACT_FORM:
841 /* enable compact form - from Ticket #342 */
842 {
843 extern pj_bool_t pjsip_use_compact_form;
844 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
845 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
846
847 pjsip_use_compact_form = PJ_TRUE;
848 /* do not transmit Allow header */
849 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
850 /* Do not include rtpmap for static payload types (<96) */
851 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
852 }
853 break;
854
Benny Prijono5e51a4e2008-11-27 00:06:46 +0000855 case OPT_ACCEPT_REDIRECT:
856 cfg->redir_op = my_atoi(pj_optarg);
857 if (cfg->redir_op<0 || cfg->redir_op>PJSIP_REDIRECT_STOP) {
858 PJ_LOG(1,(THIS_FILE,
859 "Error: accept-redirect value '%s' ", pj_optarg));
860 return PJ_EINVAL;
861 }
862 break;
863
Benny Prijono91d06b62008-09-20 12:16:56 +0000864 case OPT_NO_FORCE_LR:
865 cfg->cfg.force_lr = PJ_FALSE;
866 break;
867
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000868 case OPT_NEXT_ACCOUNT: /* Add more account. */
869 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000870 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000871 break;
872
873 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000874 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000875 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000876 break;
877
878 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000879 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000880 break;
881
882 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000883 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
884 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000885#if PJSIP_HAS_DIGEST_AKA_AUTH
886 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
887 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
888 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
889#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000890 break;
891
892 case OPT_NEXT_CRED: /* next credential */
893 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000894 break;
895
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000896 case OPT_NAMESERVER: /* nameserver */
897 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
898 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
899 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
900 return PJ_ETOOMANY;
901 }
902 break;
903
Benny Prijonoebbf6892007-03-24 17:37:25 +0000904 case OPT_STUN_DOMAIN: /* STUN domain */
905 cfg->cfg.stun_domain = pj_str(pj_optarg);
906 break;
907
Benny Prijonoc97608e2007-03-23 16:34:20 +0000908 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000909 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000910 break;
911
912 case OPT_ADD_BUDDY: /* Add to buddy list. */
913 if (pjsua_verify_sip_url(pj_optarg) != 0) {
914 PJ_LOG(1,(THIS_FILE,
915 "Error: invalid URL '%s' in "
916 "--add-buddy option", pj_optarg));
917 return -1;
918 }
919 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
920 PJ_LOG(1,(THIS_FILE,
921 "Error: too many buddies in buddy list."));
922 return -1;
923 }
924 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
925 cfg->buddy_cnt++;
926 break;
927
928 case OPT_AUTO_PLAY:
929 cfg->auto_play = 1;
930 break;
931
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +0000932 case OPT_AUTO_PLAY_HANGUP:
933 cfg->auto_play_hangup = 1;
934 break;
935
Benny Prijono1ebd6142006-10-19 15:48:02 +0000936 case OPT_AUTO_REC:
937 cfg->auto_rec = 1;
938 break;
939
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000940 case OPT_AUTO_LOOP:
941 cfg->auto_loop = 1;
942 break;
943
Benny Prijono7ca96da2006-08-07 12:11:40 +0000944 case OPT_AUTO_CONF:
945 cfg->auto_conf = 1;
946 break;
947
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000948 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000949 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000950 break;
951
Benny Prijono4af234b2007-01-24 02:02:09 +0000952 case OPT_PLAY_TONE:
953 {
954 int f1, f2, on, off;
955 int n;
956
957 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
958 if (n != 4) {
959 puts("Expecting f1,f2,on,off in --play-tone");
960 return -1;
961 }
962
963 cfg->tones[cfg->tone_count].freq1 = (short)f1;
964 cfg->tones[cfg->tone_count].freq2 = (short)f2;
965 cfg->tones[cfg->tone_count].on_msec = (short)on;
966 cfg->tones[cfg->tone_count].off_msec = (short)off;
967 ++cfg->tone_count;
968 }
969 break;
970
Benny Prijono1ebd6142006-10-19 15:48:02 +0000971 case OPT_REC_FILE:
972 cfg->rec_file = pj_str(pj_optarg);
973 break;
974
Benny Prijonoc97608e2007-03-23 16:34:20 +0000975 case OPT_USE_ICE:
976 cfg->media_cfg.enable_ice = PJ_TRUE;
977 break;
978
Benny Prijonof76e1392008-06-06 14:51:48 +0000979 case OPT_USE_TURN:
980 cfg->media_cfg.enable_turn = PJ_TRUE;
981 break;
982
983 case OPT_ICE_NO_HOST:
984 cfg->media_cfg.ice_no_host_cands = PJ_TRUE;
985 break;
986
Benny Prijono551af422008-08-07 09:55:52 +0000987 case OPT_ICE_NO_RTCP:
988 cfg->media_cfg.ice_no_rtcp = PJ_TRUE;
989 break;
990
Benny Prijonof76e1392008-06-06 14:51:48 +0000991 case OPT_TURN_SRV:
992 cfg->media_cfg.turn_server = pj_str(pj_optarg);
993 break;
994
995 case OPT_TURN_TCP:
996 cfg->media_cfg.turn_conn_type = PJ_TURN_TP_TCP;
997 break;
998
999 case OPT_TURN_USER:
1000 cfg->media_cfg.turn_auth_cred.type = PJ_STUN_AUTH_CRED_STATIC;
1001 cfg->media_cfg.turn_auth_cred.data.static_cred.realm = pj_str("*");
1002 cfg->media_cfg.turn_auth_cred.data.static_cred.username = pj_str(pj_optarg);
1003 break;
1004
1005 case OPT_TURN_PASSWD:
1006 cfg->media_cfg.turn_auth_cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1007 cfg->media_cfg.turn_auth_cred.data.static_cred.data = pj_str(pj_optarg);
1008 break;
1009
Benny Prijonod8179652008-01-23 20:39:07 +00001010#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1011 case OPT_USE_SRTP:
1012 app_config.cfg.use_srtp = my_atoi(pj_optarg);
1013 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
1014 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
1015 return -1;
1016 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001017 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +00001018 break;
Benny Prijonof6508982008-01-25 09:02:33 +00001019 case OPT_SRTP_SECURE:
1020 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
1021 if (!pj_isdigit(*pj_optarg) ||
1022 app_config.cfg.srtp_secure_signaling > 2)
1023 {
1024 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
1025 return -1;
1026 }
Benny Prijono3ec13c72008-01-29 11:52:58 +00001027 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +00001028 break;
Benny Prijonod8179652008-01-23 20:39:07 +00001029#endif
1030
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001031 case OPT_RTP_PORT:
1032 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +00001033 if (cfg->rtp_cfg.port == 0) {
1034 enum { START_PORT=4000 };
1035 unsigned range;
1036
1037 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
1038 cfg->rtp_cfg.port = START_PORT +
1039 ((pj_rand() % range) & 0xFFFE);
1040 }
1041
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001042 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
1043 PJ_LOG(1,(THIS_FILE,
1044 "Error: rtp-port argument value "
1045 "(expecting 1-65535"));
1046 return -1;
1047 }
1048 break;
1049
Benny Prijonofce28542007-12-09 15:41:10 +00001050 case OPT_DIS_CODEC:
1051 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
1052 break;
1053
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001054 case OPT_ADD_CODEC:
1055 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
1056 break;
1057
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001058 /* These options were no longer valid after new pjsua */
1059 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001060 case OPT_COMPLEXITY:
1061 cfg->complexity = my_atoi(pj_optarg);
1062 if (cfg->complexity < 0 || cfg->complexity > 10) {
1063 PJ_LOG(1,(THIS_FILE,
1064 "Error: invalid --complexity (expecting 0-10"));
1065 return -1;
1066 }
1067 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +00001068 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001069
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001070 case OPT_DURATION:
1071 cfg->duration = my_atoi(pj_optarg);
1072 break;
1073
Benny Prijonof521eb02006-08-06 23:07:25 +00001074 case OPT_THREAD_CNT:
1075 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
1076 if (cfg->cfg.thread_cnt > 128) {
1077 PJ_LOG(1,(THIS_FILE,
1078 "Error: invalid --thread-cnt option"));
1079 return -1;
1080 }
1081 break;
1082
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001083 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +00001084 cfg->media_cfg.ptime = my_atoi(pj_optarg);
1085 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001086 PJ_LOG(1,(THIS_FILE,
1087 "Error: invalid --ptime option"));
1088 return -1;
1089 }
1090 break;
1091
Benny Prijono0a12f002006-07-26 17:05:39 +00001092 case OPT_NO_VAD:
1093 cfg->media_cfg.no_vad = PJ_TRUE;
1094 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001095
Benny Prijonod79f25c2006-08-02 19:41:37 +00001096 case OPT_EC_TAIL:
1097 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
1098 if (cfg->media_cfg.ec_tail_len > 1000) {
1099 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
1100 "is too big"));
1101 return -1;
1102 }
1103 break;
1104
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001105 case OPT_EC_OPT:
1106 cfg->media_cfg.ec_options = my_atoi(pj_optarg);
1107 break;
1108
Benny Prijono0498d902006-06-19 14:49:14 +00001109 case OPT_QUALITY:
1110 cfg->media_cfg.quality = my_atoi(pj_optarg);
1111 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
1112 PJ_LOG(1,(THIS_FILE,
1113 "Error: invalid --quality (expecting 0-10"));
1114 return -1;
1115 }
1116 break;
1117
Benny Prijono00cae612006-07-31 15:19:36 +00001118 case OPT_ILBC_MODE:
1119 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
1120 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
1121 PJ_LOG(1,(THIS_FILE,
1122 "Error: invalid --ilbc-mode (expecting 20 or 30"));
1123 return -1;
1124 }
1125 break;
1126
1127 case OPT_RX_DROP_PCT:
1128 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
1129 if (cfg->media_cfg.rx_drop_pct > 100) {
1130 PJ_LOG(1,(THIS_FILE,
1131 "Error: invalid --rx-drop-pct (expecting <= 100"));
1132 return -1;
1133 }
1134 break;
1135
1136 case OPT_TX_DROP_PCT:
1137 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
1138 if (cfg->media_cfg.tx_drop_pct > 100) {
1139 PJ_LOG(1,(THIS_FILE,
1140 "Error: invalid --tx-drop-pct (expecting <= 100"));
1141 return -1;
1142 }
1143 break;
1144
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001145 case OPT_AUTO_ANSWER:
1146 cfg->auto_answer = my_atoi(pj_optarg);
1147 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
1148 PJ_LOG(1,(THIS_FILE,
1149 "Error: invalid code in --auto-answer "
1150 "(expecting 100-699"));
1151 return -1;
1152 }
1153 break;
1154
1155 case OPT_MAX_CALLS:
1156 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +00001157 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +00001158 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
1159 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +00001160 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001161 return -1;
1162 }
1163 break;
1164
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001165 case OPT_USE_TLS:
1166 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001167#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1168 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1169 return -1;
1170#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001171 break;
1172
1173 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001174 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
1175#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1176 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1177 return -1;
1178#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001179 break;
1180
Benny Prijonof3bbc132006-12-25 06:43:59 +00001181 case OPT_TLS_CERT_FILE:
1182 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
1183#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1184 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1185 return -1;
1186#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001187 break;
1188
Benny Prijonof3bbc132006-12-25 06:43:59 +00001189 case OPT_TLS_PRIV_FILE:
1190 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
1191 break;
1192
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001193 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +00001194 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
1195#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
1196 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
1197 return -1;
1198#endif
1199 break;
1200
1201 case OPT_TLS_VERIFY_SERVER:
1202 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
1203 break;
1204
1205 case OPT_TLS_VERIFY_CLIENT:
1206 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1207 break;
1208
1209 case OPT_TLS_NEG_TIMEOUT:
1210 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001211 break;
1212
Benny Prijonoe10db842008-07-01 15:31:59 +00001213 case OPT_TLS_SRV_NAME:
1214 cfg->udp_cfg.tls_setting.server_name = pj_str(pj_optarg);
1215 break;
1216
Benny Prijono4e5d5512007-03-06 18:11:30 +00001217 case OPT_CAPTURE_DEV:
1218 cfg->capture_dev = atoi(pj_optarg);
1219 break;
1220
1221 case OPT_PLAYBACK_DEV:
1222 cfg->playback_dev = atoi(pj_optarg);
1223 break;
1224
Benny Prijonoebc32c32008-06-12 13:30:39 +00001225 case OPT_STDOUT_REFRESH:
1226 stdout_refresh = atoi(pj_optarg);
1227 break;
1228
1229 case OPT_STDOUT_REFRESH_TEXT:
1230 stdout_refresh_text = pj_optarg;
1231 break;
1232
Benny Prijono4d0da3a2008-06-26 20:23:47 +00001233#ifdef _IONBF
1234 case OPT_STDOUT_NO_BUF:
1235 setvbuf(stdout, NULL, _IONBF, 0);
1236 break;
1237#endif
1238
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001239 case OPT_CAPTURE_LAT:
1240 cfg->capture_lat = atoi(pj_optarg);
1241 break;
1242
1243 case OPT_PLAYBACK_LAT:
1244 cfg->playback_lat = atoi(pj_optarg);
1245 break;
1246
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001247 case OPT_SND_AUTO_CLOSE:
1248 cfg->media_cfg.snd_auto_close_time = atoi(pj_optarg);
1249 break;
1250
Benny Prijono91d20f42008-06-14 19:42:37 +00001251 case OPT_NO_TONES:
1252 cfg->no_tones = PJ_TRUE;
1253 break;
1254
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001255 case OPT_JB_MAX_SIZE:
1256 cfg->media_cfg.jb_max = atoi(pj_optarg);
1257 break;
1258
Benny Prijono22a300a2006-06-14 20:04:55 +00001259 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001260 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001261 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001262 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001263 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001264 }
1265 }
1266
1267 if (pj_optind != argc) {
1268 pj_str_t uri_arg;
1269
1270 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1271 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1272 return -1;
1273 }
1274 uri_arg = pj_str(argv[pj_optind]);
1275 if (uri_to_call)
1276 *uri_to_call = uri_arg;
1277 pj_optind++;
1278
1279 /* Add URI to call to buddy list if it's not already there */
1280 for (i=0; i<cfg->buddy_cnt; ++i) {
1281 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1282 break;
1283 }
1284 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1285 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1286 }
1287
1288 } else {
1289 if (uri_to_call)
1290 uri_to_call->slen = 0;
1291 }
1292
1293 if (pj_optind != argc) {
1294 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1295 return PJ_EINVAL;
1296 }
1297
Benny Prijono56315612006-07-18 14:39:40 +00001298 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1299 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001300
1301 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001302 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1303
1304 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001305 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001306 acfg->cred_count++;
1307 }
1308
1309 /* When IMS mode is enabled for the account, verify that settings
1310 * are okay.
1311 */
1312 /* For now we check if IMS mode is activated by looking if
1313 * initial_auth is set.
1314 */
1315 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1316 /* Realm must point to the real domain */
1317 if (*acfg->cred_info[0].realm.ptr=='*') {
1318 PJ_LOG(1,(THIS_FILE,
1319 "Error: cannot use '*' as realm with IMS"));
1320 return PJ_EINVAL;
1321 }
1322
1323 /* Username for authentication must be in a@b format */
1324 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1325 PJ_LOG(1,(THIS_FILE,
1326 "Error: Username for authentication must "
1327 "be in user@domain format with IMS"));
1328 return PJ_EINVAL;
1329 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001330 }
1331 }
1332
1333
1334 return PJ_SUCCESS;
1335}
1336
1337
1338/*
1339 * Save account settings
1340 */
1341static void write_account_settings(int acc_index, pj_str_t *result)
1342{
1343 unsigned i;
1344 char line[128];
1345 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1346
1347
1348 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1349 pj_strcat2(result, line);
1350
1351
1352 /* Identity */
1353 if (acc_cfg->id.slen) {
1354 pj_ansi_sprintf(line, "--id %.*s\n",
1355 (int)acc_cfg->id.slen,
1356 acc_cfg->id.ptr);
1357 pj_strcat2(result, line);
1358 }
1359
1360 /* Registrar server */
1361 if (acc_cfg->reg_uri.slen) {
1362 pj_ansi_sprintf(line, "--registrar %.*s\n",
1363 (int)acc_cfg->reg_uri.slen,
1364 acc_cfg->reg_uri.ptr);
1365 pj_strcat2(result, line);
1366
1367 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1368 acc_cfg->reg_timeout);
1369 pj_strcat2(result, line);
1370 }
1371
1372 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001373 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001374 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001375 (int)acc_cfg->force_contact.slen,
1376 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001377 pj_strcat2(result, line);
1378 }
1379
Benny Prijono30fe4852008-12-10 16:54:16 +00001380 /* Contact parameters */
1381 if (acc_cfg->contact_params.slen) {
1382 pj_ansi_sprintf(line, "--contact-params %.*s\n",
1383 (int)acc_cfg->contact_params.slen,
1384 acc_cfg->contact_params.ptr);
1385 pj_strcat2(result, line);
1386 }
1387
Benny Prijonofce28542007-12-09 15:41:10 +00001388 /* */
Benny Prijono7f630432008-09-24 16:52:41 +00001389 if (acc_cfg->allow_contact_rewrite!=1)
Benny Prijonofce28542007-12-09 15:41:10 +00001390 {
Benny Prijono251e02a2008-07-25 10:05:55 +00001391 pj_ansi_sprintf(line, "--auto-update-nat %i\n",
Benny Prijonoe8554ef2008-03-22 09:33:52 +00001392 (int)acc_cfg->allow_contact_rewrite);
Benny Prijonofce28542007-12-09 15:41:10 +00001393 pj_strcat2(result, line);
1394 }
1395
Benny Prijonod8179652008-01-23 20:39:07 +00001396#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1397 /* SRTP */
1398 if (acc_cfg->use_srtp) {
1399 pj_ansi_sprintf(line, "--use-srtp %i\n",
1400 (int)acc_cfg->use_srtp);
1401 pj_strcat2(result, line);
1402 }
1403#endif
1404
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001405 /* Proxy */
1406 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1407 pj_ansi_sprintf(line, "--proxy %.*s\n",
1408 (int)acc_cfg->proxy[i].slen,
1409 acc_cfg->proxy[i].ptr);
1410 pj_strcat2(result, line);
1411 }
1412
1413 /* Credentials */
1414 for (i=0; i<acc_cfg->cred_count; ++i) {
1415 if (acc_cfg->cred_info[i].realm.slen) {
1416 pj_ansi_sprintf(line, "--realm %.*s\n",
1417 (int)acc_cfg->cred_info[i].realm.slen,
1418 acc_cfg->cred_info[i].realm.ptr);
1419 pj_strcat2(result, line);
1420 }
1421
1422 if (acc_cfg->cred_info[i].username.slen) {
1423 pj_ansi_sprintf(line, "--username %.*s\n",
1424 (int)acc_cfg->cred_info[i].username.slen,
1425 acc_cfg->cred_info[i].username.ptr);
1426 pj_strcat2(result, line);
1427 }
1428
1429 if (acc_cfg->cred_info[i].data.slen) {
1430 pj_ansi_sprintf(line, "--password %.*s\n",
1431 (int)acc_cfg->cred_info[i].data.slen,
1432 acc_cfg->cred_info[i].data.ptr);
1433 pj_strcat2(result, line);
1434 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001435
1436 if (i != acc_cfg->cred_count - 1)
1437 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001438 }
1439
1440}
1441
1442
1443/*
1444 * Write settings.
1445 */
1446static int write_settings(const struct app_config *config,
1447 char *buf, pj_size_t max)
1448{
1449 unsigned acc_index;
1450 unsigned i;
1451 pj_str_t cfg;
1452 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001453 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001454
1455 PJ_UNUSED_ARG(max);
1456
1457 cfg.ptr = buf;
1458 cfg.slen = 0;
1459
1460 /* Logging. */
1461 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1462 pj_ansi_sprintf(line, "--log-level %d\n",
1463 config->log_cfg.level);
1464 pj_strcat2(&cfg, line);
1465
1466 pj_ansi_sprintf(line, "--app-log-level %d\n",
1467 config->log_cfg.console_level);
1468 pj_strcat2(&cfg, line);
1469
1470 if (config->log_cfg.log_filename.slen) {
1471 pj_ansi_sprintf(line, "--log-file %.*s\n",
1472 (int)config->log_cfg.log_filename.slen,
1473 config->log_cfg.log_filename.ptr);
1474 pj_strcat2(&cfg, line);
1475 }
1476
1477
1478 /* Save account settings. */
1479 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1480
1481 write_account_settings(acc_index, &cfg);
1482
1483 if (acc_index < config->acc_cnt-1)
1484 pj_strcat2(&cfg, "--next-account\n");
1485 }
1486
1487
1488 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1489
1490 /* Outbound proxy */
1491 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1492 pj_ansi_sprintf(line, "--outbound %.*s\n",
1493 (int)config->cfg.outbound_proxy[i].slen,
1494 config->cfg.outbound_proxy[i].ptr);
1495 pj_strcat2(&cfg, line);
1496 }
1497
1498
1499 /* UDP Transport. */
1500 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1501 pj_strcat2(&cfg, line);
1502
Benny Prijono0a5cad82006-09-26 13:21:02 +00001503 /* IP address, if any. */
1504 if (config->udp_cfg.public_addr.slen) {
1505 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1506 (int)config->udp_cfg.public_addr.slen,
1507 config->udp_cfg.public_addr.ptr);
1508 pj_strcat2(&cfg, line);
1509 }
1510
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001511 /* No TCP ? */
1512 if (config->no_tcp) {
1513 pj_strcat2(&cfg, "--no-tcp\n");
1514 }
1515
1516 /* No UDP ? */
1517 if (config->no_udp) {
1518 pj_strcat2(&cfg, "--no-udp\n");
1519 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001520
1521 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001522 if (config->cfg.stun_domain.slen) {
1523 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1524 (int)config->cfg.stun_domain.slen,
1525 config->cfg.stun_domain.ptr);
1526 pj_strcat2(&cfg, line);
1527 }
1528 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001529 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001530 (int)config->cfg.stun_host.slen,
1531 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001532 pj_strcat2(&cfg, line);
1533 }
1534
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001535 /* TLS */
1536 if (config->use_tls)
1537 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001538 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001539 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001540 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1541 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001542 pj_strcat2(&cfg, line);
1543 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001544 if (config->udp_cfg.tls_setting.cert_file.slen) {
1545 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1546 (int)config->udp_cfg.tls_setting.cert_file.slen,
1547 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001548 pj_strcat2(&cfg, line);
1549 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001550 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1551 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1552 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1553 config->udp_cfg.tls_setting.privkey_file.ptr);
1554 pj_strcat2(&cfg, line);
1555 }
1556
1557 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001558 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001559 (int)config->udp_cfg.tls_setting.password.slen,
1560 config->udp_cfg.tls_setting.password.ptr);
1561 pj_strcat2(&cfg, line);
1562 }
1563
Benny Prijonoe10db842008-07-01 15:31:59 +00001564 if (config->udp_cfg.tls_setting.server_name.slen) {
1565 pj_ansi_sprintf(line, "--tls-srv-name %.*s\n",
1566 (int)config->udp_cfg.tls_setting.server_name.slen,
1567 config->udp_cfg.tls_setting.server_name.ptr);
1568 pj_strcat2(&cfg, line);
1569 }
1570
Benny Prijonof3bbc132006-12-25 06:43:59 +00001571 if (config->udp_cfg.tls_setting.verify_server)
1572 pj_strcat2(&cfg, "--tls-verify-server\n");
1573
1574 if (config->udp_cfg.tls_setting.verify_client)
1575 pj_strcat2(&cfg, "--tls-verify-client\n");
1576
1577 if (config->udp_cfg.tls_setting.timeout.sec) {
1578 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001579 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001580 pj_strcat2(&cfg, line);
1581 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001582
1583 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1584
Benny Prijonof6508982008-01-25 09:02:33 +00001585 /* SRTP */
Benny Prijonofe5a6942008-02-18 12:16:23 +00001586#if PJMEDIA_HAS_SRTP
Benny Prijonof6508982008-01-25 09:02:33 +00001587 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1588 pj_ansi_sprintf(line, "--use-srtp %d\n",
1589 app_config.cfg.use_srtp);
1590 pj_strcat2(&cfg, line);
1591 }
1592 if (app_config.cfg.srtp_secure_signaling !=
1593 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1594 {
1595 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1596 app_config.cfg.srtp_secure_signaling);
1597 pj_strcat2(&cfg, line);
1598 }
Benny Prijonofe5a6942008-02-18 12:16:23 +00001599#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001600
Benny Prijonof76e1392008-06-06 14:51:48 +00001601 /* Media Transport*/
Benny Prijonoc97608e2007-03-23 16:34:20 +00001602 if (config->media_cfg.enable_ice)
1603 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonof76e1392008-06-06 14:51:48 +00001604
1605 if (config->media_cfg.enable_turn)
1606 pj_strcat2(&cfg, "--use-turn\n");
1607
1608 if (config->media_cfg.ice_no_host_cands)
1609 pj_strcat2(&cfg, "--ice-no-host\n");
1610
Benny Prijono551af422008-08-07 09:55:52 +00001611 if (config->media_cfg.ice_no_rtcp)
1612 pj_strcat2(&cfg, "--ice-no-rtcp\n");
1613
Benny Prijonof76e1392008-06-06 14:51:48 +00001614 if (config->media_cfg.turn_server.slen) {
1615 pj_ansi_sprintf(line, "--turn-srv %.*s\n",
1616 (int)config->media_cfg.turn_server.slen,
1617 config->media_cfg.turn_server.ptr);
1618 pj_strcat2(&cfg, line);
1619 }
1620
1621 if (config->media_cfg.turn_conn_type == PJ_TURN_TP_TCP)
1622 pj_strcat2(&cfg, "--turn-tcp\n");
1623
1624 if (config->media_cfg.turn_auth_cred.data.static_cred.username.slen) {
1625 pj_ansi_sprintf(line, "--turn-user %.*s\n",
1626 (int)config->media_cfg.turn_auth_cred.data.static_cred.username.slen,
1627 config->media_cfg.turn_auth_cred.data.static_cred.username.ptr);
1628 pj_strcat2(&cfg, line);
1629 }
1630
1631 if (config->media_cfg.turn_auth_cred.data.static_cred.data.slen) {
1632 pj_ansi_sprintf(line, "--turn-passwd %.*s\n",
1633 (int)config->media_cfg.turn_auth_cred.data.static_cred.data.slen,
1634 config->media_cfg.turn_auth_cred.data.static_cred.data.ptr);
1635 pj_strcat2(&cfg, line);
1636 }
1637
1638 /* Media */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001639 if (config->null_audio)
1640 pj_strcat2(&cfg, "--null-audio\n");
1641 if (config->auto_play)
1642 pj_strcat2(&cfg, "--auto-play\n");
1643 if (config->auto_loop)
1644 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001645 if (config->auto_conf)
1646 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001647 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001648 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001649 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001650 pj_strcat2(&cfg, line);
1651 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001652 for (i=0; i<config->tone_count; ++i) {
1653 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1654 config->tones[i].freq1, config->tones[i].freq2,
1655 config->tones[i].on_msec, config->tones[i].off_msec);
1656 pj_strcat2(&cfg, line);
1657 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001658 if (config->rec_file.slen) {
1659 pj_ansi_sprintf(line, "--rec-file %s\n",
1660 config->rec_file.ptr);
1661 pj_strcat2(&cfg, line);
1662 }
1663 if (config->auto_rec)
1664 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001665 if (config->capture_dev != PJSUA_INVALID_ID) {
1666 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1667 pj_strcat2(&cfg, line);
1668 }
1669 if (config->playback_dev != PJSUA_INVALID_ID) {
1670 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1671 pj_strcat2(&cfg, line);
1672 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001673 if (config->media_cfg.snd_auto_close_time != -1) {
1674 pj_ansi_sprintf(line, "--snd-auto-close %d\n",
1675 config->media_cfg.snd_auto_close_time);
1676 pj_strcat2(&cfg, line);
1677 }
Benny Prijono91d20f42008-06-14 19:42:37 +00001678 if (config->no_tones) {
1679 pj_strcat2(&cfg, "--no-tones\n");
1680 }
Nanang Izzuddinc9853542008-07-17 16:59:07 +00001681 if (config->media_cfg.jb_max != -1) {
1682 pj_ansi_sprintf(line, "--jb-max-size %d\n",
1683 config->media_cfg.jb_max);
1684 pj_strcat2(&cfg, line);
1685 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001686
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00001687 /* Sound device latency */
1688 if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) {
1689 pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat);
1690 pj_strcat2(&cfg, line);
1691 }
1692 if (config->playback_dev != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) {
1693 pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat);
1694 pj_strcat2(&cfg, line);
1695 }
1696
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001697 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001698 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001699 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001700 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001701 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001702 } else {
1703 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1704 config->media_cfg.clock_rate);
1705 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001706 }
Benny Prijono70972992006-08-05 11:13:58 +00001707
Benny Prijonoc59ca6e2008-04-10 11:04:49 +00001708 if (config->media_cfg.snd_clock_rate &&
1709 config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate)
1710 {
Benny Prijonof3758ee2008-02-26 15:32:16 +00001711 pj_ansi_sprintf(line, "--snd-clock-rate %d\n",
1712 config->media_cfg.snd_clock_rate);
1713 pj_strcat2(&cfg, line);
Benny Prijonof3758ee2008-02-26 15:32:16 +00001714 }
1715
Benny Prijono7d60d052008-03-29 12:24:20 +00001716 /* Stereo mode. */
1717 if (config->media_cfg.channel_count == 2) {
1718 pj_ansi_sprintf(line, "--stereo\n");
1719 pj_strcat2(&cfg, line);
1720 }
1721
Benny Prijono70972992006-08-05 11:13:58 +00001722 /* quality */
1723 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001724 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001725 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001726 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001727 } else {
1728 pj_ansi_sprintf(line, "#using default --quality %d\n",
1729 config->media_cfg.quality);
1730 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001731 }
Benny Prijono0498d902006-06-19 14:49:14 +00001732
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001733
1734 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001735 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001736 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001737 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001738 pj_strcat2(&cfg, line);
1739 }
1740
Benny Prijono70972992006-08-05 11:13:58 +00001741 /* no-vad */
1742 if (config->media_cfg.no_vad) {
1743 pj_strcat2(&cfg, "--no-vad\n");
1744 }
1745
1746 /* ec-tail */
1747 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1748 pj_ansi_sprintf(line, "--ec-tail %d\n",
1749 config->media_cfg.ec_tail_len);
1750 pj_strcat2(&cfg, line);
1751 } else {
1752 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1753 config->media_cfg.ec_tail_len);
1754 pj_strcat2(&cfg, line);
1755 }
1756
Benny Prijono59b3ffe2008-08-11 18:10:42 +00001757 /* ec-opt */
1758 if (config->media_cfg.ec_options != 0) {
1759 pj_ansi_sprintf(line, "--ec-opt %d\n",
1760 config->media_cfg.ec_options);
1761 pj_strcat2(&cfg, line);
1762 }
Benny Prijono70972992006-08-05 11:13:58 +00001763
1764 /* ilbc-mode */
1765 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1766 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1767 config->media_cfg.ilbc_mode);
1768 pj_strcat2(&cfg, line);
1769 } else {
1770 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1771 config->media_cfg.ilbc_mode);
1772 pj_strcat2(&cfg, line);
1773 }
1774
1775 /* RTP drop */
1776 if (config->media_cfg.tx_drop_pct) {
1777 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1778 config->media_cfg.tx_drop_pct);
1779 pj_strcat2(&cfg, line);
1780
1781 }
1782 if (config->media_cfg.rx_drop_pct) {
1783 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1784 config->media_cfg.rx_drop_pct);
1785 pj_strcat2(&cfg, line);
1786
1787 }
1788
1789
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001790 /* Start RTP port. */
1791 pj_ansi_sprintf(line, "--rtp-port %d\n",
1792 config->rtp_cfg.port);
1793 pj_strcat2(&cfg, line);
1794
1795 /* Add codec. */
1796 for (i=0; i<config->codec_cnt; ++i) {
1797 pj_ansi_sprintf(line, "--add-codec %s\n",
1798 config->codec_arg[i].ptr);
1799 pj_strcat2(&cfg, line);
1800 }
Benny Prijonofce28542007-12-09 15:41:10 +00001801 /* Disable codec */
1802 for (i=0; i<config->codec_dis_cnt; ++i) {
1803 pj_ansi_sprintf(line, "--dis-codec %s\n",
1804 config->codec_dis[i].ptr);
1805 pj_strcat2(&cfg, line);
1806 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001807
1808 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1809
1810 /* Auto-answer. */
1811 if (config->auto_answer != 0) {
1812 pj_ansi_sprintf(line, "--auto-answer %d\n",
1813 config->auto_answer);
1814 pj_strcat2(&cfg, line);
1815 }
1816
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001817 /* accept-redirect */
1818 if (config->redir_op != PJSIP_REDIRECT_ACCEPT) {
1819 pj_ansi_sprintf(line, "--accept-redirect %d\n",
1820 config->redir_op);
1821 pj_strcat2(&cfg, line);
1822 }
1823
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001824 /* Max calls. */
1825 pj_ansi_sprintf(line, "--max-calls %d\n",
1826 config->cfg.max_calls);
1827 pj_strcat2(&cfg, line);
1828
1829 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001830 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001831 pj_ansi_sprintf(line, "--duration %d\n",
1832 config->duration);
1833 pj_strcat2(&cfg, line);
1834 }
1835
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001836 /* norefersub ? */
1837 if (config->no_refersub) {
1838 pj_strcat2(&cfg, "--norefersub\n");
1839 }
1840
Benny Prijonofce28542007-12-09 15:41:10 +00001841 if (pjsip_use_compact_form)
1842 {
1843 pj_strcat2(&cfg, "--use-compact-form\n");
1844 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001845
Benny Prijono91d06b62008-09-20 12:16:56 +00001846 if (config->cfg.force_lr) {
1847 pj_strcat2(&cfg, "--no-force-lr\n");
1848 }
1849
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001850 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1851
1852 /* Add buddies. */
1853 for (i=0; i<config->buddy_cnt; ++i) {
1854 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1855 (int)config->buddy_cfg[i].uri.slen,
1856 config->buddy_cfg[i].uri.ptr);
1857 pj_strcat2(&cfg, line);
1858 }
1859
1860
1861 *(cfg.ptr + cfg.slen) = '\0';
1862 return cfg.slen;
1863}
1864
1865
1866/*
1867 * Dump application states.
1868 */
1869static void app_dump(pj_bool_t detail)
1870{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001871 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001872}
1873
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001874/*
1875 * Print log of call states. Since call states may be too long for logger,
1876 * printing it is a bit tricky, it should be printed part by part as long
1877 * as the logger can accept.
1878 */
Benny Prijono5e51a4e2008-11-27 00:06:46 +00001879static void log_call_dump(int call_id)
1880{
Nanang Izzuddin660eee82008-07-17 14:54:03 +00001881 unsigned call_dump_len;
1882 unsigned part_len;
1883 unsigned part_idx;
1884 unsigned log_decor;
1885
1886 pjsua_call_dump(call_id, PJ_TRUE, some_buf,
1887 sizeof(some_buf), " ");
1888 call_dump_len = strlen(some_buf);
1889
1890 log_decor = pj_log_get_decor();
1891 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1892 PJ_LOG(3,(THIS_FILE, "\n"));
1893 pj_log_set_decor(0);
1894
1895 part_idx = 0;
1896 part_len = PJ_LOG_MAX_SIZE-80;
1897 while (part_idx < call_dump_len) {
1898 char p_orig, *p;
1899
1900 p = &some_buf[part_idx];
1901 if (part_idx + part_len > call_dump_len)
1902 part_len = call_dump_len - part_idx;
1903 p_orig = p[part_len];
1904 p[part_len] = '\0';
1905 PJ_LOG(3,(THIS_FILE, "%s", p));
1906 p[part_len] = p_orig;
1907 part_idx += part_len;
1908 }
1909 pj_log_set_decor(log_decor);
1910}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001911
1912/*****************************************************************************
1913 * Console application
1914 */
1915
Benny Prijono91d20f42008-06-14 19:42:37 +00001916static void ringback_start(pjsua_call_id call_id)
1917{
1918 if (app_config.no_tones)
1919 return;
1920
1921 if (app_config.call_data[call_id].ringback_on)
1922 return;
1923
1924 app_config.call_data[call_id].ringback_on = PJ_TRUE;
1925
1926 if (++app_config.ringback_cnt==1 &&
1927 app_config.ringback_slot!=PJSUA_INVALID_ID)
1928 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001929 pjsua_conf_connect(app_config.ringback_slot, 0);
1930 }
1931}
1932
1933static void ring_stop(pjsua_call_id call_id)
1934{
1935 if (app_config.no_tones)
1936 return;
1937
1938 if (app_config.call_data[call_id].ringback_on) {
1939 app_config.call_data[call_id].ringback_on = PJ_FALSE;
1940
1941 pj_assert(app_config.ringback_cnt>0);
1942 if (--app_config.ringback_cnt == 0 &&
1943 app_config.ringback_slot!=PJSUA_INVALID_ID)
1944 {
1945 pjsua_conf_disconnect(app_config.ringback_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001946 pjmedia_tonegen_rewind(app_config.ringback_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001947 }
1948 }
1949
1950 if (app_config.call_data[call_id].ring_on) {
1951 app_config.call_data[call_id].ring_on = PJ_FALSE;
1952
1953 pj_assert(app_config.ring_cnt>0);
1954 if (--app_config.ring_cnt == 0 &&
1955 app_config.ring_slot!=PJSUA_INVALID_ID)
1956 {
1957 pjsua_conf_disconnect(app_config.ring_slot, 0);
Benny Prijonoe28cec82008-06-14 20:40:53 +00001958 pjmedia_tonegen_rewind(app_config.ring_port);
Benny Prijono91d20f42008-06-14 19:42:37 +00001959 }
1960 }
1961}
1962
1963static void ring_start(pjsua_call_id call_id)
1964{
1965 if (app_config.no_tones)
1966 return;
1967
1968 if (app_config.call_data[call_id].ring_on)
1969 return;
1970
1971 app_config.call_data[call_id].ring_on = PJ_TRUE;
1972
1973 if (++app_config.ring_cnt==1 &&
1974 app_config.ring_slot!=PJSUA_INVALID_ID)
1975 {
Benny Prijono91d20f42008-06-14 19:42:37 +00001976 pjsua_conf_connect(app_config.ring_slot, 0);
1977 }
1978}
1979
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001980/*
1981 * Find next call when current call is disconnected or when user
1982 * press ']'
1983 */
1984static pj_bool_t find_next_call(void)
1985{
1986 int i, max;
1987
1988 max = pjsua_call_get_max_count();
1989 for (i=current_call+1; i<max; ++i) {
1990 if (pjsua_call_is_active(i)) {
1991 current_call = i;
1992 return PJ_TRUE;
1993 }
1994 }
1995
1996 for (i=0; i<current_call; ++i) {
1997 if (pjsua_call_is_active(i)) {
1998 current_call = i;
1999 return PJ_TRUE;
2000 }
2001 }
2002
2003 current_call = PJSUA_INVALID_ID;
2004 return PJ_FALSE;
2005}
2006
2007
2008/*
2009 * Find previous call when user press '['
2010 */
2011static pj_bool_t find_prev_call(void)
2012{
2013 int i, max;
2014
2015 max = pjsua_call_get_max_count();
2016 for (i=current_call-1; i>=0; --i) {
2017 if (pjsua_call_is_active(i)) {
2018 current_call = i;
2019 return PJ_TRUE;
2020 }
2021 }
2022
2023 for (i=max-1; i>current_call; --i) {
2024 if (pjsua_call_is_active(i)) {
2025 current_call = i;
2026 return PJ_TRUE;
2027 }
2028 }
2029
2030 current_call = PJSUA_INVALID_ID;
2031 return PJ_FALSE;
2032}
2033
2034
Benny Prijono804ff0a2006-09-14 11:17:48 +00002035/* Callback from timer when the maximum call duration has been
2036 * exceeded.
2037 */
2038static void call_timeout_callback(pj_timer_heap_t *timer_heap,
2039 struct pj_timer_entry *entry)
2040{
2041 pjsua_call_id call_id = entry->id;
2042 pjsua_msg_data msg_data;
2043 pjsip_generic_string_hdr warn;
2044 pj_str_t hname = pj_str("Warning");
2045 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
2046
2047 PJ_UNUSED_ARG(timer_heap);
2048
Benny Prijono148c9dd2006-09-19 13:37:53 +00002049 if (call_id == PJSUA_INVALID_ID) {
2050 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
2051 return;
2052 }
2053
Benny Prijono804ff0a2006-09-14 11:17:48 +00002054 /* Add warning header */
2055 pjsua_msg_data_init(&msg_data);
2056 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
2057 pj_list_push_back(&msg_data.hdr_list, &warn);
2058
2059 /* Call duration has been exceeded; disconnect the call */
2060 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
2061 "for call %d, disconnecting the call",
2062 app_config.duration, call_id));
2063 entry->id = PJSUA_INVALID_ID;
2064 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
2065}
2066
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002067
2068/*
2069 * Handler when invite state has changed.
2070 */
2071static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
2072{
2073 pjsua_call_info call_info;
2074
2075 PJ_UNUSED_ARG(e);
2076
2077 pjsua_call_get_info(call_id, &call_info);
2078
2079 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
2080
Benny Prijono91d20f42008-06-14 19:42:37 +00002081 /* Stop all ringback for this call */
2082 ring_stop(call_id);
2083
Benny Prijono804ff0a2006-09-14 11:17:48 +00002084 /* Cancel duration timer, if any */
2085 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
2086 struct call_data *cd = &app_config.call_data[call_id];
2087 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2088
2089 cd->timer.id = PJSUA_INVALID_ID;
2090 pjsip_endpt_cancel_timer(endpt, &cd->timer);
2091 }
2092
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002093 /* Rewind play file when hangup automatically,
2094 * since file is not looped
2095 */
2096 if (app_config.auto_play_hangup)
2097 pjsua_player_set_pos(app_config.wav_id, 0);
2098
2099
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002100 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
2101 call_id,
2102 call_info.last_status,
2103 call_info.last_status_text.ptr));
2104
2105 if (call_id == current_call) {
2106 find_next_call();
2107 }
2108
Benny Prijono4be63b52006-11-25 14:50:25 +00002109 /* Dump media state upon disconnected */
2110 if (1) {
Benny Prijono4be63b52006-11-25 14:50:25 +00002111 PJ_LOG(5,(THIS_FILE,
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002112 "Call %d disconnected, dumping media stats..",
2113 call_id));
2114 log_call_dump(call_id);
Benny Prijono4be63b52006-11-25 14:50:25 +00002115 }
2116
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002117 } else {
2118
Benny Prijono804ff0a2006-09-14 11:17:48 +00002119 if (app_config.duration!=NO_LIMIT &&
2120 call_info.state == PJSIP_INV_STATE_CONFIRMED)
2121 {
2122 /* Schedule timer to hangup call after the specified duration */
2123 struct call_data *cd = &app_config.call_data[call_id];
2124 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
2125 pj_time_val delay;
2126
2127 cd->timer.id = call_id;
2128 delay.sec = app_config.duration;
2129 delay.msec = 0;
2130 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
2131 }
2132
Benny Prijono4be63b52006-11-25 14:50:25 +00002133 if (call_info.state == PJSIP_INV_STATE_EARLY) {
2134 int code;
2135 pj_str_t reason;
2136 pjsip_msg *msg;
2137
2138 /* This can only occur because of TX or RX message */
2139 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
2140
2141 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2142 msg = e->body.tsx_state.src.rdata->msg_info.msg;
2143 } else {
2144 msg = e->body.tsx_state.src.tdata->msg;
2145 }
2146
2147 code = msg->line.status.code;
2148 reason = msg->line.status.reason;
2149
Benny Prijono91d20f42008-06-14 19:42:37 +00002150 /* Start ringback for 180 for UAC unless there's SDP in 180 */
2151 if (call_info.role==PJSIP_ROLE_UAC && code==180 &&
2152 msg->body == NULL &&
2153 call_info.media_status==PJSUA_CALL_MEDIA_NONE)
2154 {
2155 ringback_start(call_id);
2156 }
2157
Benny Prijono4be63b52006-11-25 14:50:25 +00002158 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
2159 call_id, call_info.state_text.ptr,
2160 code, (int)reason.slen, reason.ptr));
2161 } else {
2162 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
2163 call_id,
2164 call_info.state_text.ptr));
2165 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002166
2167 if (current_call==PJSUA_INVALID_ID)
2168 current_call = call_id;
2169
2170 }
2171}
2172
2173
2174/**
2175 * Handler when there is incoming call.
2176 */
2177static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
2178 pjsip_rx_data *rdata)
2179{
2180 pjsua_call_info call_info;
2181
2182 PJ_UNUSED_ARG(acc_id);
2183 PJ_UNUSED_ARG(rdata);
2184
2185 pjsua_call_get_info(call_id, &call_info);
2186
Benny Prijono91d20f42008-06-14 19:42:37 +00002187 /* Start ringback */
2188 ring_start(call_id);
2189
Benny Prijono2285e7e2008-12-17 14:28:18 +00002190 if (current_call==PJSUA_INVALID_ID)
2191 current_call = call_id;
2192
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002193 if (app_config.auto_answer > 0) {
2194 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
2195 }
2196
2197 if (app_config.auto_answer < 200) {
2198 PJ_LOG(3,(THIS_FILE,
2199 "Incoming call for account %d!\n"
2200 "From: %s\n"
2201 "To: %s\n"
2202 "Press a to answer or h to reject call",
2203 acc_id,
2204 call_info.remote_info.ptr,
2205 call_info.local_info.ptr));
2206 }
2207}
2208
2209
2210/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00002211 * Handler when a transaction within a call has changed state.
2212 */
2213static void on_call_tsx_state(pjsua_call_id call_id,
2214 pjsip_transaction *tsx,
2215 pjsip_event *e)
2216{
2217 const pjsip_method info_method =
2218 {
2219 PJSIP_OTHER_METHOD,
2220 { "INFO", 4 }
2221 };
2222
2223 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
2224 /*
2225 * Handle INFO method.
2226 */
2227 if (tsx->role == PJSIP_ROLE_UAC &&
2228 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00002229 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
2230 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00002231 {
2232 /* Status of outgoing INFO request */
2233 if (tsx->status_code >= 200 && tsx->status_code < 300) {
2234 PJ_LOG(4,(THIS_FILE,
2235 "Call %d: DTMF sent successfully with INFO",
2236 call_id));
2237 } else if (tsx->status_code >= 300) {
2238 PJ_LOG(4,(THIS_FILE,
2239 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
2240 call_id,
2241 tsx->status_code,
2242 (int)tsx->status_text.slen,
2243 tsx->status_text.ptr));
2244 }
2245 } else if (tsx->role == PJSIP_ROLE_UAS &&
2246 tsx->state == PJSIP_TSX_STATE_TRYING)
2247 {
2248 /* Answer incoming INFO with 200/OK */
2249 pjsip_rx_data *rdata;
2250 pjsip_tx_data *tdata;
2251 pj_status_t status;
2252
2253 rdata = e->body.tsx_state.src.rdata;
2254
2255 if (rdata->msg_info.msg->body) {
2256 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2257 200, NULL, &tdata);
2258 if (status == PJ_SUCCESS)
2259 status = pjsip_tsx_send_msg(tsx, tdata);
2260
2261 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
2262 call_id,
2263 (int)rdata->msg_info.msg->body->len,
2264 rdata->msg_info.msg->body->data));
2265 } else {
2266 status = pjsip_endpt_create_response(tsx->endpt, rdata,
2267 400, NULL, &tdata);
2268 if (status == PJ_SUCCESS)
2269 status = pjsip_tsx_send_msg(tsx, tdata);
2270 }
2271 }
2272 }
2273}
2274
2275
2276/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002277 * Callback on media state changed event.
2278 * The action may connect the call to sound device, to file, or
2279 * to loop the call.
2280 */
2281static void on_call_media_state(pjsua_call_id call_id)
2282{
2283 pjsua_call_info call_info;
2284
2285 pjsua_call_get_info(call_id, &call_info);
2286
Benny Prijono91d20f42008-06-14 19:42:37 +00002287 /* Stop ringback */
2288 ring_stop(call_id);
2289
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002290 /* Connect ports appropriately when media status is ACTIVE or REMOTE HOLD,
2291 * otherwise we should NOT connect the ports.
2292 */
2293 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE ||
2294 call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD)
2295 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002296 pj_bool_t connect_sound = PJ_TRUE;
2297
2298 /* Loopback sound, if desired */
2299 if (app_config.auto_loop) {
2300 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
2301 connect_sound = PJ_FALSE;
Benny Prijonof6c77f42008-08-13 13:52:19 +00002302 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00002303
Benny Prijonof6c77f42008-08-13 13:52:19 +00002304 /* Automatically record conversation, if desired */
2305 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2306 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002307 }
2308
2309 /* Stream a file, if desired */
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002310 if ((app_config.auto_play || app_config.auto_play_hangup) &&
2311 app_config.wav_port != PJSUA_INVALID_ID)
2312 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002313 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
2314 connect_sound = PJ_FALSE;
2315 }
2316
Benny Prijono7ca96da2006-08-07 12:11:40 +00002317 /* Put call in conference with other calls, if desired */
2318 if (app_config.auto_conf) {
2319 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00002320 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00002321 unsigned i;
2322
2323 /* Get all calls, and establish media connection between
2324 * this call and other calls.
2325 */
2326 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00002327
Benny Prijono7ca96da2006-08-07 12:11:40 +00002328 for (i=0; i<call_cnt; ++i) {
2329 if (call_ids[i] == call_id)
2330 continue;
2331
2332 if (!pjsua_call_has_media(call_ids[i]))
2333 continue;
2334
2335 pjsua_conf_connect(call_info.conf_slot,
2336 pjsua_call_get_conf_port(call_ids[i]));
2337 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2338 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002339
2340 /* Automatically record conversation, if desired */
2341 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2342 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
2343 app_config.rec_port);
2344 }
2345
Benny Prijono7ca96da2006-08-07 12:11:40 +00002346 }
2347
2348 /* Also connect call to local sound device */
2349 connect_sound = PJ_TRUE;
2350 }
2351
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002352 /* Otherwise connect to sound device */
2353 if (connect_sound) {
2354 pjsua_conf_connect(call_info.conf_slot, 0);
2355 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002356
2357 /* Automatically record conversation, if desired */
2358 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
2359 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
2360 pjsua_conf_connect(0, app_config.rec_port);
2361 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002362 }
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002363 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002364
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002365 /* Handle media status */
2366 switch (call_info.media_status) {
2367 case PJSUA_CALL_MEDIA_ACTIVE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002368 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002369 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002370
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002371 case PJSUA_CALL_MEDIA_LOCAL_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002372 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
2373 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002374 break;
2375
2376 case PJSUA_CALL_MEDIA_REMOTE_HOLD:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002377 PJ_LOG(3,(THIS_FILE,
2378 "Media for call %d is suspended (hold) by remote",
2379 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002380 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002381
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002382 case PJSUA_CALL_MEDIA_ERROR:
2383 PJ_LOG(3,(THIS_FILE,
Benny Prijono096c56c2007-09-15 08:30:16 +00002384 "Media has reported error, disconnecting call"));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002385 {
2386 pj_str_t reason = pj_str("ICE negotiation failed");
2387 pjsua_call_hangup(call_id, 500, &reason, NULL);
2388 }
2389 break;
Benny Prijono096c56c2007-09-15 08:30:16 +00002390
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002391 case PJSUA_CALL_MEDIA_NONE:
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002392 PJ_LOG(3,(THIS_FILE,
2393 "Media for call %d is inactive",
2394 call_id));
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002395 break;
2396
2397 default:
2398 pj_assert(!"Unhandled media status");
2399 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002400 }
2401}
2402
Benny Prijono0875ae82006-12-26 00:11:48 +00002403/*
2404 * DTMF callback.
2405 */
2406static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
2407{
2408 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
2409}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002410
2411/*
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002412 * Redirection handler.
2413 */
Benny Prijono08a48b82008-11-27 12:42:07 +00002414static pjsip_redirect_op call_on_redirected(pjsua_call_id call_id,
2415 const pjsip_uri *target,
2416 const pjsip_event *e)
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002417{
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002418 PJ_UNUSED_ARG(e);
2419
Benny Prijono08a48b82008-11-27 12:42:07 +00002420 if (app_config.redir_op == PJSIP_REDIRECT_PENDING) {
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002421 char uristr[PJSIP_MAX_URL_SIZE];
2422 int len;
2423
2424 len = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, target, uristr,
2425 sizeof(uristr));
2426 if (len < 1) {
2427 pj_ansi_strcpy(uristr, "--URI too long--");
2428 }
2429
2430 PJ_LOG(3,(THIS_FILE, "Call %d is being redirected to %.*s. "
2431 "Press 'Ra' to accept, 'Rr' to reject, or 'Rd' to "
2432 "disconnect.",
2433 call_id, len, uristr));
2434 }
Benny Prijono08a48b82008-11-27 12:42:07 +00002435
2436 return app_config.redir_op;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00002437}
2438
2439/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002440 * Handler registration status has changed.
2441 */
2442static void on_reg_state(pjsua_acc_id acc_id)
2443{
2444 PJ_UNUSED_ARG(acc_id);
2445
2446 // Log already written.
2447}
2448
2449
2450/*
Benny Prijono563cd7a2008-08-19 20:02:15 +00002451 * Handler for incoming presence subscription request
2452 */
2453static void on_incoming_subscribe(pjsua_acc_id acc_id,
2454 pjsua_srv_pres *srv_pres,
2455 pjsua_buddy_id buddy_id,
2456 const pj_str_t *from,
2457 pjsip_rx_data *rdata,
2458 pjsip_status_code *code,
2459 pj_str_t *reason,
2460 pjsua_msg_data *msg_data)
2461{
2462 /* Just accept the request (the default behavior) */
2463 PJ_UNUSED_ARG(acc_id);
2464 PJ_UNUSED_ARG(srv_pres);
2465 PJ_UNUSED_ARG(buddy_id);
2466 PJ_UNUSED_ARG(from);
2467 PJ_UNUSED_ARG(rdata);
2468 PJ_UNUSED_ARG(code);
2469 PJ_UNUSED_ARG(reason);
2470 PJ_UNUSED_ARG(msg_data);
2471}
2472
2473
2474/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002475 * Handler on buddy state changed.
2476 */
2477static void on_buddy_state(pjsua_buddy_id buddy_id)
2478{
2479 pjsua_buddy_info info;
2480 pjsua_buddy_get_info(buddy_id, &info);
2481
2482 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
2483 (int)info.uri.slen,
2484 info.uri.ptr,
2485 (int)info.status_text.slen,
2486 info.status_text.ptr));
2487}
2488
2489
2490/**
2491 * Incoming IM message (i.e. MESSAGE request)!
2492 */
2493static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
2494 const pj_str_t *to, const pj_str_t *contact,
2495 const pj_str_t *mime_type, const pj_str_t *text)
2496{
2497 /* Note: call index may be -1 */
2498 PJ_UNUSED_ARG(call_id);
2499 PJ_UNUSED_ARG(to);
2500 PJ_UNUSED_ARG(contact);
2501 PJ_UNUSED_ARG(mime_type);
2502
Benny Prijonof4b538d2007-05-14 16:45:20 +00002503 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002504 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00002505 (int)text->slen, text->ptr,
2506 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002507}
2508
2509
2510/**
2511 * Received typing indication
2512 */
2513static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
2514 const pj_str_t *to, const pj_str_t *contact,
2515 pj_bool_t is_typing)
2516{
2517 PJ_UNUSED_ARG(call_id);
2518 PJ_UNUSED_ARG(to);
2519 PJ_UNUSED_ARG(contact);
2520
2521 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
2522 (int)from->slen, from->ptr,
2523 (is_typing?"is typing..":"has stopped typing")));
2524}
2525
2526
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002527/**
2528 * Call transfer request status.
2529 */
2530static void on_call_transfer_status(pjsua_call_id call_id,
2531 int status_code,
2532 const pj_str_t *status_text,
2533 pj_bool_t final,
2534 pj_bool_t *p_cont)
2535{
2536 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
2537 call_id, status_code,
2538 (int)status_text->slen, status_text->ptr,
2539 (final ? "[final]" : "")));
2540
2541 if (status_code/100 == 2) {
2542 PJ_LOG(3,(THIS_FILE,
2543 "Call %d: call transfered successfully, disconnecting call",
2544 call_id));
2545 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
2546 *p_cont = PJ_FALSE;
2547 }
2548}
2549
2550
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002551/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002552 * Notification that call is being replaced.
2553 */
2554static void on_call_replaced(pjsua_call_id old_call_id,
2555 pjsua_call_id new_call_id)
2556{
2557 pjsua_call_info old_ci, new_ci;
2558
2559 pjsua_call_get_info(old_call_id, &old_ci);
2560 pjsua_call_get_info(new_call_id, &new_ci);
2561
2562 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2563 "call %d with %.*s",
2564 old_call_id,
2565 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2566 new_call_id,
2567 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2568}
2569
2570
2571/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002572 * NAT type detection callback.
2573 */
2574static void on_nat_detect(const pj_stun_nat_detect_result *res)
2575{
2576 if (res->status != PJ_SUCCESS) {
2577 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2578 } else {
2579 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2580 }
2581}
2582
2583
2584/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002585 * Print buddy list.
2586 */
2587static void print_buddy_list(void)
2588{
2589 pjsua_buddy_id ids[64];
2590 int i;
2591 unsigned count = PJ_ARRAY_SIZE(ids);
2592
2593 puts("Buddy list:");
2594
2595 pjsua_enum_buddies(ids, &count);
2596
2597 if (count == 0)
2598 puts(" -none-");
2599 else {
2600 for (i=0; i<(int)count; ++i) {
2601 pjsua_buddy_info info;
2602
2603 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2604 continue;
2605
Benny Prijono4461c7d2007-08-25 13:36:15 +00002606 printf(" [%2d] <%.*s> %.*s\n",
2607 ids[i]+1,
2608 (int)info.status_text.slen,
2609 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002610 (int)info.uri.slen,
2611 info.uri.ptr);
2612 }
2613 }
2614 puts("");
2615}
2616
2617
2618/*
2619 * Print account status.
2620 */
2621static void print_acc_status(int acc_id)
2622{
2623 char buf[80];
2624 pjsua_acc_info info;
2625
2626 pjsua_acc_get_info(acc_id, &info);
2627
2628 if (!info.has_registration) {
2629 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2630 (int)info.status_text.slen,
2631 info.status_text.ptr);
2632
2633 } else {
2634 pj_ansi_snprintf(buf, sizeof(buf),
2635 "%d/%.*s (expires=%d)",
2636 info.status,
2637 (int)info.status_text.slen,
2638 info.status_text.ptr,
2639 info.expires);
2640
2641 }
2642
2643 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2644 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002645 printf(" Online status: %.*s\n",
2646 (int)info.online_status_text.slen,
2647 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002648}
2649
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00002650/* Playfile done notification, set timer to hangup calls */
2651pj_status_t on_playfile_done(pjmedia_port *port, void *usr_data)
2652{
2653 pj_time_val delay;
2654
2655 PJ_UNUSED_ARG(port);
2656 PJ_UNUSED_ARG(usr_data);
2657
2658 /* Just rewind WAV when it is played outside of call */
2659 if (pjsua_call_get_count() == 0) {
2660 pjsua_player_set_pos(app_config.wav_id, 0);
2661 return PJ_SUCCESS;
2662 }
2663
2664 /* Timer is already active */
2665 if (app_config.auto_hangup_timer.id == 1)
2666 return PJ_SUCCESS;
2667
2668 app_config.auto_hangup_timer.id = 1;
2669 delay.sec = 0;
2670 delay.msec = 200; /* Give 200 ms before hangup */
2671 pjsip_endpt_schedule_timer(pjsua_get_pjsip_endpt(),
2672 &app_config.auto_hangup_timer,
2673 &delay);
2674
2675 return PJ_SUCCESS;
2676}
2677
2678/* Auto hangup timer callback */
2679static void hangup_timeout_callback(pj_timer_heap_t *timer_heap,
2680 struct pj_timer_entry *entry)
2681{
2682 PJ_UNUSED_ARG(timer_heap);
2683 PJ_UNUSED_ARG(entry);
2684
2685 app_config.auto_hangup_timer.id = 0;
2686 pjsua_call_hangup_all();
2687}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002688
2689/*
2690 * Show a bit of help.
2691 */
2692static void keystroke_help(void)
2693{
2694 pjsua_acc_id acc_ids[16];
2695 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2696 int i;
2697
2698 printf(">>>>\n");
2699
2700 pjsua_enum_accs(acc_ids, &count);
2701
2702 printf("Account list:\n");
2703 for (i=0; i<(int)count; ++i)
2704 print_acc_status(acc_ids[i]);
2705
2706 print_buddy_list();
2707
2708 //puts("Commands:");
2709 puts("+=============================================================================+");
2710 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2711 puts("| | | |");
2712 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2713 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002714 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2715 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2716 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2717 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002718 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2719 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002720 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002721 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002722 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2723 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2724 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2725 puts("| | V Adjust audio Volume | f Save config |");
2726 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002727 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijonoddd02de2008-06-26 22:20:11 +00002728 puts("| q QUIT sleep MS echo [0|1|txt] n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002729 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002730
2731 i = pjsua_call_get_count();
2732 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002733
2734 if (current_call != PJSUA_INVALID_ID) {
2735 pjsua_call_info ci;
2736 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2737 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2738 (int)ci.remote_info.slen, ci.remote_info.ptr,
2739 (int)ci.state_text.slen, ci.state_text.ptr);
2740 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002741}
2742
2743
2744/*
2745 * Input simple string
2746 */
2747static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2748{
2749 char *p;
2750
2751 printf("%s (empty to cancel): ", title); fflush(stdout);
2752 fgets(buf, len, stdin);
2753
2754 /* Remove trailing newlines. */
2755 for (p=buf; ; ++p) {
2756 if (*p=='\r' || *p=='\n') *p='\0';
2757 else if (!*p) break;
2758 }
2759
2760 if (!*buf)
2761 return PJ_FALSE;
2762
2763 return PJ_TRUE;
2764}
2765
2766
2767#define NO_NB -2
2768struct input_result
2769{
2770 int nb_result;
2771 char *uri_result;
2772};
2773
2774
2775/*
2776 * Input URL.
2777 */
2778static void ui_input_url(const char *title, char *buf, int len,
2779 struct input_result *result)
2780{
2781 result->nb_result = NO_NB;
2782 result->uri_result = NULL;
2783
2784 print_buddy_list();
2785
2786 printf("Choices:\n"
2787 " 0 For current dialog.\n"
2788 " -1 All %d buddies in buddy list\n"
2789 " [1 -%2d] Select from buddy list\n"
2790 " URL An URL\n"
2791 " <Enter> Empty input (or 'q') to cancel\n"
2792 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2793 printf("%s: ", title);
2794
2795 fflush(stdout);
2796 fgets(buf, len, stdin);
2797 len = strlen(buf);
2798
2799 /* Left trim */
2800 while (pj_isspace(*buf)) {
2801 ++buf;
2802 --len;
2803 }
2804
2805 /* Remove trailing newlines */
2806 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2807 buf[--len] = '\0';
2808
2809 if (len == 0 || buf[0]=='q')
2810 return;
2811
2812 if (pj_isdigit(*buf) || *buf=='-') {
2813
2814 int i;
2815
2816 if (*buf=='-')
2817 i = 1;
2818 else
2819 i = 0;
2820
2821 for (; i<len; ++i) {
2822 if (!pj_isdigit(buf[i])) {
2823 puts("Invalid input");
2824 return;
2825 }
2826 }
2827
2828 result->nb_result = my_atoi(buf);
2829
2830 if (result->nb_result >= 0 &&
2831 result->nb_result <= (int)pjsua_get_buddy_count())
2832 {
2833 return;
2834 }
2835 if (result->nb_result == -1)
2836 return;
2837
2838 puts("Invalid input");
2839 result->nb_result = NO_NB;
2840 return;
2841
2842 } else {
2843 pj_status_t status;
2844
2845 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2846 pjsua_perror(THIS_FILE, "Invalid URL", status);
2847 return;
2848 }
2849
2850 result->uri_result = buf;
2851 }
2852}
2853
2854/*
2855 * List the ports in conference bridge
2856 */
2857static void conf_list(void)
2858{
2859 unsigned i, count;
2860 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2861
2862 printf("Conference ports:\n");
2863
2864 count = PJ_ARRAY_SIZE(id);
2865 pjsua_enum_conf_ports(id, &count);
2866
2867 for (i=0; i<count; ++i) {
2868 char txlist[PJSUA_MAX_CALLS*4+10];
2869 unsigned j;
2870 pjsua_conf_port_info info;
2871
2872 pjsua_conf_get_port_info(id[i], &info);
2873
2874 txlist[0] = '\0';
2875 for (j=0; j<info.listener_cnt; ++j) {
2876 char s[10];
2877 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2878 pj_ansi_strcat(txlist, s);
2879 }
Benny Prijono7d60d052008-03-29 12:24:20 +00002880 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002881 info.slot_id,
2882 info.clock_rate/1000,
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002883 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
Benny Prijono7d60d052008-03-29 12:24:20 +00002884 info.channel_count,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002885 (int)info.name.slen,
2886 info.name.ptr,
2887 txlist);
2888
2889 }
2890 puts("");
2891}
2892
2893
2894/*
Benny Prijono56315612006-07-18 14:39:40 +00002895 * Send arbitrary request to remote host
2896 */
2897static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2898{
2899 pj_str_t str_method;
2900 pjsip_method method;
2901 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002902 pjsip_endpoint *endpt;
2903 pj_status_t status;
2904
2905 endpt = pjsua_get_pjsip_endpt();
2906
2907 str_method = pj_str(cstr_method);
2908 pjsip_method_init_np(&method, &str_method);
2909
Benny Prijonofff245c2007-04-02 11:44:47 +00002910 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002911
Benny Prijonob988d762007-12-05 04:30:04 +00002912 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002913 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002914 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002915 return;
2916 }
2917}
2918
2919
2920/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002921 * Change extended online status.
2922 */
2923static void change_online_status(void)
2924{
2925 char menuin[32];
2926 pj_bool_t online_status;
2927 pjrpid_element elem;
2928 int i, choice;
2929
2930 enum {
2931 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2932 };
2933
2934 struct opt {
2935 int id;
2936 char *name;
2937 } opts[] = {
2938 { AVAILABLE, "Available" },
2939 { BUSY, "Busy"},
2940 { OTP, "On the phone"},
2941 { IDLE, "Idle"},
2942 { AWAY, "Away"},
2943 { BRB, "Be right back"},
2944 { OFFLINE, "Offline"}
2945 };
2946
2947 printf("\n"
2948 "Choices:\n");
2949 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2950 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2951 }
2952
2953 if (!simple_input("Select status", menuin, sizeof(menuin)))
2954 return;
2955
2956 choice = atoi(menuin) - 1;
2957 if (choice < 0 || choice >= OPT_MAX) {
2958 puts("Invalid selection");
2959 return;
2960 }
2961
2962 pj_bzero(&elem, sizeof(elem));
2963 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2964
2965 online_status = PJ_TRUE;
2966
2967 switch (choice) {
2968 case AVAILABLE:
2969 break;
2970 case BUSY:
2971 elem.activity = PJRPID_ACTIVITY_BUSY;
2972 elem.note = pj_str("Busy");
2973 break;
2974 case OTP:
2975 elem.activity = PJRPID_ACTIVITY_BUSY;
2976 elem.note = pj_str("On the phone");
2977 break;
2978 case IDLE:
2979 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2980 elem.note = pj_str("Idle");
2981 break;
2982 case AWAY:
2983 elem.activity = PJRPID_ACTIVITY_AWAY;
2984 elem.note = pj_str("Away");
2985 break;
2986 case BRB:
2987 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2988 elem.note = pj_str("Be right back");
2989 break;
2990 case OFFLINE:
2991 online_status = PJ_FALSE;
2992 break;
2993 }
2994
2995 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2996}
2997
2998
2999/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00003000 * Change codec priorities.
3001 */
3002static void manage_codec_prio(void)
3003{
3004 pjsua_codec_info c[32];
3005 unsigned i, count = PJ_ARRAY_SIZE(c);
3006 char input[32];
3007 char *codec, *prio;
3008 pj_str_t id;
3009 int new_prio;
3010 pj_status_t status;
3011
3012 printf("List of codecs:\n");
3013
3014 pjsua_enum_codecs(c, &count);
3015 for (i=0; i<count; ++i) {
3016 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
3017 c[i].codec_id.ptr);
3018 }
3019
3020 puts("");
Benny Prijono88accae2008-06-26 15:48:14 +00003021 puts("Enter codec id and its new priority "
3022 "(e.g. \"speex/16000 200\"), empty to cancel:");
Benny Prijonoc08682e2007-10-04 06:17:58 +00003023
Benny Prijono88accae2008-06-26 15:48:14 +00003024 printf("Codec name (\"*\" for all) and priority: ");
Benny Prijonoc08682e2007-10-04 06:17:58 +00003025 fgets(input, sizeof(input), stdin);
3026 if (input[0]=='\r' || input[0]=='\n') {
3027 puts("Done");
3028 return;
3029 }
3030
3031 codec = strtok(input, " \t\r\n");
3032 prio = strtok(NULL, " \r\n");
3033
3034 if (!codec || !prio) {
3035 puts("Invalid input");
3036 return;
3037 }
3038
3039 new_prio = atoi(prio);
3040 if (new_prio < 0)
3041 new_prio = 0;
3042 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
3043 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
3044
3045 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
3046 (pj_uint8_t)new_prio);
3047 if (status != PJ_SUCCESS)
3048 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
3049}
3050
3051
3052/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003053 * Main "user interface" loop.
3054 */
3055void console_app_main(const pj_str_t *uri_to_call)
3056{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003057 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003058 char buf[128];
3059 char text[128];
3060 int i, count;
3061 char *uri;
3062 pj_str_t tmp;
3063 struct input_result result;
3064 pjsua_call_info call_info;
3065 pjsua_acc_info acc_info;
3066
3067
3068 /* If user specifies URI to call, then call the URI */
3069 if (uri_to_call->slen) {
3070 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
3071 }
3072
3073 keystroke_help();
3074
3075 for (;;) {
3076
3077 printf(">>> ");
3078 fflush(stdout);
3079
Benny Prijono990042e2007-01-21 19:36:00 +00003080 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
3081 /*
3082 * Be friendly to users who redirect commands into
3083 * program, when file ends, resume with kbd.
3084 * If exit is desired end script with q for quit
3085 */
3086 /* Reopen stdin/stdout/stderr to /dev/console */
3087#if defined(PJ_WIN32) && PJ_WIN32!=0
3088 if (freopen ("CONIN$", "r", stdin) == NULL) {
3089#else
3090 if (1) {
3091#endif
3092 puts("Cannot switch back to console from file redirection");
3093 menuin[0] = 'q';
3094 menuin[1] = '\0';
3095 } else {
3096 puts("Switched back to console from file redirection");
3097 continue;
3098 }
3099 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003100
Benny Prijonoebc32c32008-06-12 13:30:39 +00003101 if (cmd_echo) {
3102 printf("%s", menuin);
3103 }
3104
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003105 switch (menuin[0]) {
3106
3107 case 'm':
3108 /* Make call! : */
3109 printf("(You currently have %d calls)\n",
3110 pjsua_call_get_count());
3111
3112 uri = NULL;
3113 ui_input_url("Make call", buf, sizeof(buf), &result);
3114 if (result.nb_result != NO_NB) {
3115
3116 if (result.nb_result == -1 || result.nb_result == 0) {
3117 puts("You can't do that with make call!");
3118 continue;
3119 } else {
3120 pjsua_buddy_info binfo;
3121 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3122 uri = binfo.uri.ptr;
3123 }
3124
3125 } else if (result.uri_result) {
3126 uri = result.uri_result;
3127 }
3128
3129 tmp = pj_str(uri);
3130 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
3131 break;
3132
3133 case 'M':
3134 /* Make multiple calls! : */
3135 printf("(You currently have %d calls)\n",
3136 pjsua_call_get_count());
3137
3138 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
3139 continue;
3140
3141 count = my_atoi(menuin);
3142 if (count < 1)
3143 continue;
3144
3145 ui_input_url("Make call", buf, sizeof(buf), &result);
3146 if (result.nb_result != NO_NB) {
3147 pjsua_buddy_info binfo;
3148 if (result.nb_result == -1 || result.nb_result == 0) {
3149 puts("You can't do that with make call!");
3150 continue;
3151 }
3152 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3153 uri = binfo.uri.ptr;
3154 } else {
3155 uri = result.uri_result;
3156 }
3157
3158 for (i=0; i<my_atoi(menuin); ++i) {
3159 pj_status_t status;
3160
3161 tmp = pj_str(uri);
3162 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
3163 NULL, NULL);
3164 if (status != PJ_SUCCESS)
3165 break;
3166 }
3167 break;
3168
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003169 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00003170 i = pjsua_detect_nat_type();
3171 if (i != PJ_SUCCESS)
3172 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00003173 break;
3174
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003175 case 'i':
3176 /* Send instant messaeg */
3177
3178 /* i is for call index to send message, if any */
3179 i = -1;
3180
3181 /* Make compiler happy. */
3182 uri = NULL;
3183
3184 /* Input destination. */
3185 ui_input_url("Send IM to", buf, sizeof(buf), &result);
3186 if (result.nb_result != NO_NB) {
3187
3188 if (result.nb_result == -1) {
3189 puts("You can't send broadcast IM like that!");
3190 continue;
3191
3192 } else if (result.nb_result == 0) {
3193
3194 i = current_call;
3195
3196 } else {
3197 pjsua_buddy_info binfo;
3198 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3199 uri = binfo.uri.ptr;
3200 }
3201
3202 } else if (result.uri_result) {
3203 uri = result.uri_result;
3204 }
3205
3206
3207 /* Send typing indication. */
3208 if (i != -1)
3209 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
3210 else {
3211 pj_str_t tmp_uri = pj_str(uri);
3212 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
3213 }
3214
3215 /* Input the IM . */
3216 if (!simple_input("Message", text, sizeof(text))) {
3217 /*
3218 * Cancelled.
3219 * Send typing notification too, saying we're not typing.
3220 */
3221 if (i != -1)
3222 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
3223 else {
3224 pj_str_t tmp_uri = pj_str(uri);
3225 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
3226 }
3227 continue;
3228 }
3229
3230 tmp = pj_str(text);
3231
3232 /* Send the IM */
3233 if (i != -1)
3234 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
3235 else {
3236 pj_str_t tmp_uri = pj_str(uri);
3237 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
3238 }
3239
3240 break;
3241
3242 case 'a':
3243
3244 if (current_call != -1) {
3245 pjsua_call_get_info(current_call, &call_info);
3246 } else {
3247 /* Make compiler happy */
3248 call_info.role = PJSIP_ROLE_UAC;
3249 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
3250 }
3251
3252 if (current_call == -1 ||
3253 call_info.role != PJSIP_ROLE_UAS ||
3254 call_info.state >= PJSIP_INV_STATE_CONNECTING)
3255 {
3256 puts("No pending incoming call");
3257 fflush(stdout);
3258 continue;
3259
3260 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00003261 int st_code;
3262 char contact[120];
3263 pj_str_t hname = { "Contact", 7 };
3264 pj_str_t hvalue;
3265 pjsip_generic_string_hdr hcontact;
3266 pjsua_msg_data msg_data;
3267
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003268 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
3269 continue;
3270
Benny Prijono20d36722007-02-22 14:52:24 +00003271 st_code = my_atoi(buf);
3272 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003273 continue;
3274
Benny Prijono20d36722007-02-22 14:52:24 +00003275 pjsua_msg_data_init(&msg_data);
3276
3277 if (st_code/100 == 3) {
3278 if (!simple_input("Enter URL to be put in Contact",
3279 contact, sizeof(contact)))
3280 continue;
3281 hvalue = pj_str(contact);
3282 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
3283
3284 pj_list_push_back(&msg_data.hdr_list, &hcontact);
3285 }
3286
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003287 /*
3288 * Must check again!
3289 * Call may have been disconnected while we're waiting for
3290 * keyboard input.
3291 */
3292 if (current_call == -1) {
3293 puts("Call has been disconnected");
3294 fflush(stdout);
3295 continue;
3296 }
3297
Benny Prijono20d36722007-02-22 14:52:24 +00003298 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003299 }
3300
3301 break;
3302
3303
3304 case 'h':
3305
3306 if (current_call == -1) {
3307 puts("No current call");
3308 fflush(stdout);
3309 continue;
3310
3311 } else if (menuin[1] == 'a') {
3312
3313 /* Hangup all calls */
3314 pjsua_call_hangup_all();
3315
3316 } else {
3317
3318 /* Hangup current calls */
3319 pjsua_call_hangup(current_call, 0, NULL, NULL);
3320 }
3321 break;
3322
3323 case ']':
3324 case '[':
3325 /*
3326 * Cycle next/prev dialog.
3327 */
3328 if (menuin[0] == ']') {
3329 find_next_call();
3330
3331 } else {
3332 find_prev_call();
3333 }
3334
3335 if (current_call != -1) {
3336
3337 pjsua_call_get_info(current_call, &call_info);
3338 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
3339 (int)call_info.remote_info.slen,
3340 call_info.remote_info.ptr));
3341
3342 } else {
3343 PJ_LOG(3,(THIS_FILE,"No current dialog"));
3344 }
3345 break;
3346
3347
3348 case '>':
3349 case '<':
3350 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
3351 break;
3352
3353 i = my_atoi(buf);
3354 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003355 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003356 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
3357 } else {
3358 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
3359 }
3360 break;
3361
3362
3363 case '+':
3364 if (menuin[1] == 'b') {
3365
3366 pjsua_buddy_config buddy_cfg;
3367 pjsua_buddy_id buddy_id;
3368 pj_status_t status;
3369
3370 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
3371 break;
3372
3373 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
3374 printf("Invalid SIP URI '%s'\n", buf);
3375 break;
3376 }
3377
Benny Prijonoac623b32006-07-03 15:19:31 +00003378 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003379
3380 buddy_cfg.uri = pj_str(buf);
3381 buddy_cfg.subscribe = PJ_TRUE;
3382
3383 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
3384 if (status == PJ_SUCCESS) {
3385 printf("New buddy '%s' added at index %d\n",
3386 buf, buddy_id+1);
3387 }
3388
3389 } else if (menuin[1] == 'a') {
3390
Benny Prijonofb2b3652007-06-28 07:15:03 +00003391 char id[80], registrar[80], realm[80], uname[80], passwd[30];
3392 pjsua_acc_config acc_cfg;
3393 pj_status_t status;
3394
3395 if (!simple_input("Your SIP URL:", id, sizeof(id)))
3396 break;
3397 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
3398 break;
3399 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
3400 break;
3401 if (!simple_input("Auth Username:", uname, sizeof(uname)))
3402 break;
3403 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
3404 break;
3405
3406 pjsua_acc_config_default(&acc_cfg);
3407 acc_cfg.id = pj_str(id);
3408 acc_cfg.reg_uri = pj_str(registrar);
3409 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00003410 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00003411 acc_cfg.cred_info[0].realm = pj_str(realm);
3412 acc_cfg.cred_info[0].username = pj_str(uname);
3413 acc_cfg.cred_info[0].data_type = 0;
3414 acc_cfg.cred_info[0].data = pj_str(passwd);
3415
3416 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
3417 if (status != PJ_SUCCESS) {
3418 pjsua_perror(THIS_FILE, "Error adding new account", status);
3419 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003420
3421 } else {
3422 printf("Invalid input %s\n", menuin);
3423 }
3424 break;
3425
3426 case '-':
3427 if (menuin[1] == 'b') {
3428 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
3429 break;
3430
3431 i = my_atoi(buf) - 1;
3432
3433 if (!pjsua_buddy_is_valid(i)) {
3434 printf("Invalid buddy id %d\n", i);
3435 } else {
3436 pjsua_buddy_del(i);
3437 printf("Buddy %d deleted\n", i);
3438 }
3439
3440 } else if (menuin[1] == 'a') {
3441
3442 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
3443 break;
3444
3445 i = my_atoi(buf);
3446
3447 if (!pjsua_acc_is_valid(i)) {
3448 printf("Invalid account id %d\n", i);
3449 } else {
3450 pjsua_acc_del(i);
3451 printf("Account %d deleted\n", i);
3452 }
3453
3454 } else {
3455 printf("Invalid input %s\n", menuin);
3456 }
3457 break;
3458
3459 case 'H':
3460 /*
3461 * Hold call.
3462 */
3463 if (current_call != -1) {
3464
3465 pjsua_call_set_hold(current_call, NULL);
3466
3467 } else {
3468 PJ_LOG(3,(THIS_FILE, "No current call"));
3469 }
3470 break;
3471
3472 case 'v':
3473 /*
3474 * Send re-INVITE (to release hold, etc).
3475 */
3476 if (current_call != -1) {
3477
3478 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
3479
3480 } else {
3481 PJ_LOG(3,(THIS_FILE, "No current call"));
3482 }
3483 break;
3484
Benny Prijonoc08682e2007-10-04 06:17:58 +00003485 case 'U':
3486 /*
3487 * Send UPDATE
3488 */
3489 if (current_call != -1) {
3490
3491 pjsua_call_update(current_call, 0, NULL);
3492
3493 } else {
3494 PJ_LOG(3,(THIS_FILE, "No current call"));
3495 }
3496 break;
3497
3498 case 'C':
3499 if (menuin[1] == 'p') {
3500 manage_codec_prio();
3501 }
3502 break;
3503
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003504 case 'x':
3505 /*
3506 * Transfer call.
3507 */
3508 if (current_call == -1) {
3509
3510 PJ_LOG(3,(THIS_FILE, "No current call"));
3511
3512 } else {
3513 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00003514 pjsua_msg_data msg_data;
3515 pjsip_generic_string_hdr refer_sub;
3516 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3517 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00003518 pjsua_call_info ci;
3519
3520 pjsua_call_get_info(current_call, &ci);
3521 printf("Transfering current call [%d] %.*s\n",
3522 current_call,
3523 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003524
3525 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
3526
3527 /* Check if call is still there. */
3528
3529 if (call != current_call) {
3530 puts("Call has been disconnected");
3531 continue;
3532 }
3533
Benny Prijonod524e822006-09-22 12:48:18 +00003534 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003535 if (app_config.no_refersub) {
3536 /* Add Refer-Sub: false in outgoing REFER request */
3537 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3538 &STR_FALSE);
3539 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3540 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003541 if (result.nb_result != NO_NB) {
3542 if (result.nb_result == -1 || result.nb_result == 0)
3543 puts("You can't do that with transfer call!");
3544 else {
3545 pjsua_buddy_info binfo;
3546 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00003547 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003548 }
3549
3550 } else if (result.uri_result) {
3551 pj_str_t tmp;
3552 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00003553 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003554 }
3555 }
3556 break;
3557
Benny Prijonof7b1c392006-11-11 16:46:34 +00003558 case 'X':
3559 /*
3560 * Transfer call with replaces.
3561 */
3562 if (current_call == -1) {
3563
3564 PJ_LOG(3,(THIS_FILE, "No current call"));
3565
3566 } else {
3567 int call = current_call;
3568 int dst_call;
3569 pjsua_msg_data msg_data;
3570 pjsip_generic_string_hdr refer_sub;
3571 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
3572 pj_str_t STR_FALSE = { "false", 5 };
3573 pjsua_call_id ids[PJSUA_MAX_CALLS];
3574 pjsua_call_info ci;
3575 unsigned i, count;
3576
3577 count = PJ_ARRAY_SIZE(ids);
3578 pjsua_enum_calls(ids, &count);
3579
3580 if (count <= 1) {
3581 puts("There are no other calls");
3582 continue;
3583 }
3584
3585 pjsua_call_get_info(current_call, &ci);
3586 printf("Transfer call [%d] %.*s to one of the following:\n",
3587 current_call,
3588 (int)ci.remote_info.slen, ci.remote_info.ptr);
3589
3590 for (i=0; i<count; ++i) {
3591 pjsua_call_info call_info;
3592
3593 if (ids[i] == call)
3594 continue;
3595
3596 pjsua_call_get_info(ids[i], &call_info);
3597 printf("%d %.*s [%.*s]\n",
3598 ids[i],
3599 (int)call_info.remote_info.slen,
3600 call_info.remote_info.ptr,
3601 (int)call_info.state_text.slen,
3602 call_info.state_text.ptr);
3603 }
3604
3605 if (!simple_input("Enter call number to be replaced",
3606 buf, sizeof(buf)))
3607 continue;
3608
3609 dst_call = my_atoi(buf);
3610
3611 /* Check if call is still there. */
3612
3613 if (call != current_call) {
3614 puts("Call has been disconnected");
3615 continue;
3616 }
3617
3618 /* Check that destination call is valid. */
3619 if (dst_call == call) {
3620 puts("Destination call number must not be the same "
3621 "as the call being transfered");
3622 continue;
3623 }
3624 if (dst_call >= PJSUA_MAX_CALLS) {
3625 puts("Invalid destination call number");
3626 continue;
3627 }
3628 if (!pjsua_call_is_active(dst_call)) {
3629 puts("Invalid destination call number");
3630 continue;
3631 }
3632
3633 pjsua_msg_data_init(&msg_data);
3634 if (app_config.no_refersub) {
3635 /* Add Refer-Sub: false in outgoing REFER request */
3636 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3637 &STR_FALSE);
3638 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3639 }
3640
3641 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3642 }
3643 break;
3644
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003645 case '#':
3646 /*
3647 * Send DTMF strings.
3648 */
3649 if (current_call == -1) {
3650
3651 PJ_LOG(3,(THIS_FILE, "No current call"));
3652
3653 } else if (!pjsua_call_has_media(current_call)) {
3654
3655 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3656
3657 } else {
3658 pj_str_t digits;
3659 int call = current_call;
3660 pj_status_t status;
3661
3662 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3663 sizeof(buf)))
3664 {
3665 break;
3666 }
3667
3668 if (call != current_call) {
3669 puts("Call has been disconnected");
3670 continue;
3671 }
3672
3673 digits = pj_str(buf);
3674 status = pjsua_call_dial_dtmf(current_call, &digits);
3675 if (status != PJ_SUCCESS) {
3676 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3677 } else {
3678 puts("DTMF digits enqueued for transmission");
3679 }
3680 }
3681 break;
3682
Benny Prijonofeb69f42007-10-05 09:12:26 +00003683 case '*':
3684 /* Send DTMF with INFO */
3685 if (current_call == -1) {
3686
3687 PJ_LOG(3,(THIS_FILE, "No current call"));
3688
3689 } else {
3690 const pj_str_t SIP_INFO = pj_str("INFO");
3691 pj_str_t digits;
3692 int call = current_call;
3693 int i;
3694 pj_status_t status;
3695
3696 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3697 sizeof(buf)))
3698 {
3699 break;
3700 }
3701
3702 if (call != current_call) {
3703 puts("Call has been disconnected");
3704 continue;
3705 }
3706
3707 digits = pj_str(buf);
3708 for (i=0; i<digits.slen; ++i) {
3709 pjsua_msg_data msg_data;
3710 char body[80];
3711
3712 pjsua_msg_data_init(&msg_data);
3713 msg_data.content_type = pj_str("application/dtmf-relay");
3714
3715 pj_ansi_snprintf(body, sizeof(body),
3716 "Signal=%c\r\n"
3717 "Duration=160",
3718 buf[i]);
3719 msg_data.msg_body = pj_str(body);
3720
3721 status = pjsua_call_send_request(current_call, &SIP_INFO,
3722 &msg_data);
3723 if (status != PJ_SUCCESS) {
3724 break;
3725 }
3726 }
3727 }
3728 break;
3729
Benny Prijono56315612006-07-18 14:39:40 +00003730 case 'S':
3731 /*
3732 * Send arbitrary request
3733 */
3734 if (pjsua_acc_get_count() == 0) {
3735 puts("Sorry, need at least one account configured");
3736 break;
3737 }
3738
3739 puts("Send arbitrary request to remote host");
3740
3741 /* Input METHOD */
3742 if (!simple_input("Request method:",text,sizeof(text)))
3743 break;
3744
3745 /* Input destination URI */
3746 uri = NULL;
3747 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3748 if (result.nb_result != NO_NB) {
3749
3750 if (result.nb_result == -1 || result.nb_result == 0) {
3751 puts("Sorry you can't do that!");
3752 continue;
3753 } else {
3754 pjsua_buddy_info binfo;
3755 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3756 uri = binfo.uri.ptr;
3757 }
3758
3759 } else if (result.uri_result) {
3760 uri = result.uri_result;
3761 }
3762
3763 tmp = pj_str(uri);
3764
3765 send_request(text, &tmp);
3766 break;
3767
Benny Prijonoebc32c32008-06-12 13:30:39 +00003768 case 'e':
3769 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
3770 pj_str_t tmp;
3771
3772 tmp.ptr = menuin+5;
3773 tmp.slen = pj_ansi_strlen(menuin)-6;
3774
3775 if (tmp.slen < 1) {
3776 puts("Usage: echo [0|1]");
3777 break;
3778 }
3779
Benny Prijonoddd02de2008-06-26 22:20:11 +00003780 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
Benny Prijonoebc32c32008-06-12 13:30:39 +00003781 }
3782 break;
3783
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003784 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003785 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3786 pj_str_t tmp;
3787 int delay;
3788
3789 tmp.ptr = menuin+6;
3790 tmp.slen = pj_ansi_strlen(menuin)-7;
3791
3792 if (tmp.slen < 1) {
3793 puts("Usage: sleep MSEC");
3794 break;
3795 }
3796
3797 delay = pj_strtoul(&tmp);
3798 if (delay < 0) delay = 0;
3799 pj_thread_sleep(delay);
3800 break;
3801 }
3802 /* Continue below */
3803
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003804 case 'u':
3805 /*
3806 * Subscribe/unsubscribe presence.
3807 */
3808 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3809 if (result.nb_result != NO_NB) {
3810 if (result.nb_result == -1) {
3811 int i, count;
3812 count = pjsua_get_buddy_count();
3813 for (i=0; i<count; ++i)
3814 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3815 } else if (result.nb_result == 0) {
3816 puts("Sorry, can only subscribe to buddy's presence, "
3817 "not from existing call");
3818 } else {
3819 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3820 }
3821
3822 } else if (result.uri_result) {
3823 puts("Sorry, can only subscribe to buddy's presence, "
3824 "not arbitrary URL (for now)");
3825 }
3826
3827 break;
3828
3829 case 'r':
3830 switch (menuin[1]) {
3831 case 'r':
3832 /*
3833 * Re-Register.
3834 */
3835 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3836 break;
3837 case 'u':
3838 /*
3839 * Unregister
3840 */
3841 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3842 break;
3843 }
3844 break;
3845
3846 case 't':
3847 pjsua_acc_get_info(current_acc, &acc_info);
3848 acc_info.online_status = !acc_info.online_status;
3849 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3850 printf("Setting %s online status to %s\n",
3851 acc_info.acc_uri.ptr,
3852 (acc_info.online_status?"online":"offline"));
3853 break;
3854
Benny Prijono4461c7d2007-08-25 13:36:15 +00003855 case 'T':
3856 change_online_status();
3857 break;
3858
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003859 case 'c':
3860 switch (menuin[1]) {
3861 case 'l':
3862 conf_list();
3863 break;
3864 case 'c':
3865 case 'd':
3866 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003867 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003868 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003869 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003870 const char *src_title, *dst_title;
3871
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003872 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003873
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003874 if (cnt != 3) {
3875 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003876
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003877 src_title = (menuin[1]=='c'?
3878 "Connect src port #":
3879 "Disconnect src port #");
3880 dst_title = (menuin[1]=='c'?
3881 "To dst port #":
3882 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003883
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003884 if (!simple_input(src_title, src_port, sizeof(src_port)))
3885 break;
3886
3887 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3888 break;
3889 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003890
3891 if (menuin[1]=='c') {
3892 status = pjsua_conf_connect(my_atoi(src_port),
3893 my_atoi(dst_port));
3894 } else {
3895 status = pjsua_conf_disconnect(my_atoi(src_port),
3896 my_atoi(dst_port));
3897 }
3898 if (status == PJ_SUCCESS) {
3899 puts("Success");
3900 } else {
3901 puts("ERROR!!");
3902 }
3903 }
3904 break;
3905 }
3906 break;
3907
Benny Prijono6dd967c2006-12-26 02:27:14 +00003908 case 'V':
3909 /* Adjust audio volume */
3910 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3911 if (simple_input(buf,text,sizeof(text))) {
3912 char *err;
3913 app_config.mic_level = (float)strtod(text, &err);
3914 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3915 }
3916 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3917 app_config.speaker_level);
3918 if (simple_input(buf,text,sizeof(text))) {
3919 char *err;
3920 app_config.speaker_level = (float)strtod(text, &err);
3921 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3922 }
3923
3924 break;
3925
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003926 case 'd':
3927 if (menuin[1] == 'c') {
3928 char settings[2000];
3929 int len;
3930
3931 len = write_settings(&app_config, settings, sizeof(settings));
3932 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003933 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003934 else
3935 PJ_LOG(3,(THIS_FILE,
3936 "Dumping configuration (%d bytes):\n%s\n",
3937 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003938
3939 } else if (menuin[1] == 'q') {
3940
3941 if (current_call != PJSUA_INVALID_ID) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00003942 log_call_dump(current_call);
Benny Prijono819506c2006-06-22 22:29:51 +00003943 } else {
3944 PJ_LOG(3,(THIS_FILE, "No current call"));
3945 }
3946
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003947 } else {
3948 app_dump(menuin[1]=='d');
3949 }
3950 break;
3951
3952
3953 case 'f':
3954 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3955 char settings[2000];
3956 int len;
3957
3958 len = write_settings(&app_config, settings, sizeof(settings));
3959 if (len < 1)
Benny Prijonod6e362a2008-07-19 17:53:47 +00003960 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003961 else {
3962 pj_oshandle_t fd;
3963 pj_status_t status;
3964
3965 status = pj_file_open(app_config.pool, buf,
3966 PJ_O_WRONLY, &fd);
3967 if (status != PJ_SUCCESS) {
3968 pjsua_perror(THIS_FILE, "Unable to open file", status);
3969 } else {
3970 pj_ssize_t size = len;
3971 pj_file_write(fd, settings, &size);
3972 pj_file_close(fd);
3973
3974 printf("Settings successfully written to '%s'\n", buf);
3975 }
3976 }
3977
3978 }
3979 break;
3980
3981
3982 case 'q':
3983 goto on_exit;
3984
3985
Benny Prijono5e51a4e2008-11-27 00:06:46 +00003986 case 'R':
3987 if (!pjsua_call_is_active(current_call)) {
3988 PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
3989 } else if (menuin[1] == 'a') {
3990 pjsua_call_process_redirect(current_call,
3991 PJSIP_REDIRECT_ACCEPT);
3992 } else if (menuin[1] == 'r') {
3993 pjsua_call_process_redirect(current_call,
3994 PJSIP_REDIRECT_REJECT);
3995 } else {
3996 pjsua_call_process_redirect(current_call,
3997 PJSIP_REDIRECT_STOP);
3998 }
3999 break;
4000
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004001 default:
4002 if (menuin[0] != '\n' && menuin[0] != '\r') {
4003 printf("Invalid input %s", menuin);
4004 }
4005 keystroke_help();
4006 break;
4007 }
4008 }
4009
4010on_exit:
4011 ;
4012}
4013
4014
4015/*****************************************************************************
4016 * Public API
4017 */
4018
4019pj_status_t app_init(int argc, char *argv[])
4020{
Benny Prijonoe93e2872006-06-28 16:46:49 +00004021 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00004022 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004023 unsigned i;
4024 pj_status_t status;
4025
4026 /* Create pjsua */
4027 status = pjsua_create();
4028 if (status != PJ_SUCCESS)
4029 return status;
4030
4031 /* Create pool for application */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00004032 app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004033
4034 /* Initialize default config */
4035 default_config(&app_config);
4036
4037 /* Parse the arguments */
4038 status = parse_args(argc, argv, &app_config, &uri_arg);
4039 if (status != PJ_SUCCESS)
4040 return status;
4041
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004042 /* Initialize application callbacks */
4043 app_config.cfg.cb.on_call_state = &on_call_state;
4044 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
4045 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00004046 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00004047 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijono5e51a4e2008-11-27 00:06:46 +00004048 app_config.cfg.cb.on_call_redirected = &call_on_redirected;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004049 app_config.cfg.cb.on_reg_state = &on_reg_state;
Benny Prijono563cd7a2008-08-19 20:02:15 +00004050 app_config.cfg.cb.on_incoming_subscribe = &on_incoming_subscribe;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004051 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
4052 app_config.cfg.cb.on_pager = &on_pager;
4053 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00004054 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00004055 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00004056 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004057
4058 /* Initialize pjsua */
4059 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
4060 &app_config.media_cfg);
4061 if (status != PJ_SUCCESS)
4062 return status;
4063
Benny Prijonoe909eac2006-07-27 22:04:56 +00004064#ifdef STEREO_DEMO
4065 stereo_demo();
4066#endif
4067
Benny Prijono804ff0a2006-09-14 11:17:48 +00004068 /* Initialize calls data */
4069 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
4070 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
4071 app_config.call_data[i].timer.cb = &call_timeout_callback;
4072 }
4073
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004074 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00004075 for (i=0; i<app_config.wav_count; ++i) {
4076 pjsua_player_id wav_id;
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004077 unsigned play_options = 0;
Benny Prijono32e4f492007-01-24 00:44:26 +00004078
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004079 if (app_config.auto_play_hangup)
4080 play_options |= PJMEDIA_FILE_NO_LOOP;
4081
4082 status = pjsua_player_create(&app_config.wav_files[i], play_options,
Benny Prijono32e4f492007-01-24 00:44:26 +00004083 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004084 if (status != PJ_SUCCESS)
4085 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00004086
Benny Prijono72c04d22007-02-17 22:20:58 +00004087 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00004088 app_config.wav_id = wav_id;
4089 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
Nanang Izzuddinb6133fb2008-06-20 21:45:50 +00004090 if (app_config.auto_play_hangup) {
4091 pjmedia_port *port;
4092
4093 pjsua_player_get_port(app_config.wav_id, &port);
4094 status = pjmedia_wav_player_set_eof_cb(port, NULL,
4095 &on_playfile_done);
4096 if (status != PJ_SUCCESS)
4097 goto on_error;
4098
4099 pj_timer_entry_init(&app_config.auto_hangup_timer, 0, NULL,
4100 &hangup_timeout_callback);
4101 }
Benny Prijono32e4f492007-01-24 00:44:26 +00004102 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004103 }
4104
Benny Prijono4af234b2007-01-24 02:02:09 +00004105 /* Optionally registers tone players */
4106 for (i=0; i<app_config.tone_count; ++i) {
4107 pjmedia_port *tport;
4108 char name[80];
4109 pj_str_t label;
4110 pj_status_t status;
4111
4112 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
4113 app_config.tones[i].freq1,
4114 app_config.tones[i].freq2);
4115 label = pj_str(name);
4116 status = pjmedia_tonegen_create2(app_config.pool, &label,
4117 8000, 1, 160, 16,
4118 PJMEDIA_TONEGEN_LOOP, &tport);
4119 if (status != PJ_SUCCESS) {
4120 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
4121 goto on_error;
4122 }
4123
4124 status = pjsua_conf_add_port(app_config.pool, tport,
4125 &app_config.tone_slots[i]);
4126 pj_assert(status == PJ_SUCCESS);
4127
4128 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
4129 pj_assert(status == PJ_SUCCESS);
4130 }
4131
Benny Prijono1ebd6142006-10-19 15:48:02 +00004132 /* Optionally create recorder file, if any. */
4133 if (app_config.rec_file.slen) {
4134 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
4135 &app_config.rec_id);
4136 if (status != PJ_SUCCESS)
4137 goto on_error;
4138
4139 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
4140 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004141
Benny Prijonoe347cb02007-02-14 14:36:13 +00004142 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
4143
Benny Prijono91d20f42008-06-14 19:42:37 +00004144 /* Create ringback tones */
4145 if (app_config.no_tones == PJ_FALSE) {
Benny Prijono4f966892008-06-14 22:43:56 +00004146 unsigned i, samples_per_frame;
4147 pjmedia_tone_desc tone[RING_CNT+RINGBACK_CNT];
Benny Prijono91d20f42008-06-14 19:42:37 +00004148 pj_str_t name;
4149
Benny Prijono4f966892008-06-14 22:43:56 +00004150 samples_per_frame = app_config.media_cfg.audio_frame_ptime *
4151 app_config.media_cfg.clock_rate *
4152 app_config.media_cfg.channel_count / 1000;
4153
Benny Prijono91d20f42008-06-14 19:42:37 +00004154 /* Ringback tone (call is ringing) */
4155 name = pj_str("ringback");
Benny Prijono4f966892008-06-14 22:43:56 +00004156 status = pjmedia_tonegen_create2(app_config.pool, &name,
4157 app_config.media_cfg.clock_rate,
4158 app_config.media_cfg.channel_count,
4159 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004160 16, PJMEDIA_TONEGEN_LOOP,
4161 &app_config.ringback_port);
4162 if (status != PJ_SUCCESS)
4163 goto on_error;
4164
Benny Prijonoe28cec82008-06-14 20:40:53 +00004165 pj_bzero(&tone, sizeof(tone));
Benny Prijono4f966892008-06-14 22:43:56 +00004166 for (i=0; i<RINGBACK_CNT; ++i) {
4167 tone[i].freq1 = RINGBACK_FREQ1;
4168 tone[i].freq2 = RINGBACK_FREQ2;
4169 tone[i].on_msec = RINGBACK_ON;
4170 tone[i].off_msec = RINGBACK_OFF;
4171 }
4172 tone[RINGBACK_CNT-1].off_msec = RINGBACK_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004173
Benny Prijono4f966892008-06-14 22:43:56 +00004174 pjmedia_tonegen_play(app_config.ringback_port, RINGBACK_CNT, tone,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004175 PJMEDIA_TONEGEN_LOOP);
4176
4177
Benny Prijono91d20f42008-06-14 19:42:37 +00004178 status = pjsua_conf_add_port(app_config.pool, app_config.ringback_port,
4179 &app_config.ringback_slot);
4180 if (status != PJ_SUCCESS)
4181 goto on_error;
4182
4183 /* Ring (to alert incoming call) */
4184 name = pj_str("ring");
Benny Prijono4f966892008-06-14 22:43:56 +00004185 status = pjmedia_tonegen_create2(app_config.pool, &name,
4186 app_config.media_cfg.clock_rate,
4187 app_config.media_cfg.channel_count,
4188 samples_per_frame,
Benny Prijono91d20f42008-06-14 19:42:37 +00004189 16, PJMEDIA_TONEGEN_LOOP,
4190 &app_config.ring_port);
4191 if (status != PJ_SUCCESS)
4192 goto on_error;
4193
Benny Prijono4f966892008-06-14 22:43:56 +00004194 for (i=0; i<RING_CNT; ++i) {
Benny Prijonoe28cec82008-06-14 20:40:53 +00004195 tone[i].freq1 = RING_FREQ1;
4196 tone[i].freq2 = RING_FREQ2;
4197 tone[i].on_msec = RING_ON;
4198 tone[i].off_msec = RING_OFF;
4199 }
Benny Prijono4f966892008-06-14 22:43:56 +00004200 tone[RING_CNT-1].off_msec = RING_INTERVAL;
Benny Prijonoe28cec82008-06-14 20:40:53 +00004201
Benny Prijono4f966892008-06-14 22:43:56 +00004202 pjmedia_tonegen_play(app_config.ring_port, RING_CNT,
Benny Prijonoe28cec82008-06-14 20:40:53 +00004203 tone, PJMEDIA_TONEGEN_LOOP);
4204
Benny Prijono91d20f42008-06-14 19:42:37 +00004205 status = pjsua_conf_add_port(app_config.pool, app_config.ring_port,
4206 &app_config.ring_slot);
4207 if (status != PJ_SUCCESS)
4208 goto on_error;
4209
4210 }
4211
Benny Prijono87ef89a2007-01-14 00:39:45 +00004212 /* Add UDP transport unless it's disabled. */
4213 if (!app_config.no_udp) {
4214 pjsua_acc_id aid;
4215
4216 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
4217 &app_config.udp_cfg,
4218 &transport_id);
4219 if (status != PJ_SUCCESS)
4220 goto on_error;
4221
4222 /* Add local account */
4223 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
4224 //pjsua_acc_set_transport(aid, transport_id);
4225 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00004226
4227 if (app_config.udp_cfg.port == 0) {
4228 pjsua_transport_info ti;
4229 pj_sockaddr_in *a;
4230
4231 pjsua_transport_get_info(transport_id, &ti);
4232 a = (pj_sockaddr_in*)&ti.local_addr;
4233
4234 tcp_cfg.port = pj_ntohs(a->sin_port);
4235 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00004236 }
4237
Benny Prijonoe93e2872006-06-28 16:46:49 +00004238 /* Add TCP transport unless it's disabled */
4239 if (!app_config.no_tcp) {
4240 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00004241 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00004242 &transport_id);
4243 if (status != PJ_SUCCESS)
4244 goto on_error;
4245
4246 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00004247 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00004248 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4249
4250 }
4251
Benny Prijonoe93e2872006-06-28 16:46:49 +00004252
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004253#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
4254 /* Add TLS transport when application wants one */
4255 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00004256
4257 pjsua_acc_id acc_id;
4258
4259 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00004260 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004261 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00004262 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004263 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00004264 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004265 if (status != PJ_SUCCESS)
4266 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00004267
4268 /* Add local account */
4269 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
4270 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00004271 }
4272#endif
4273
Benny Prijonoe93e2872006-06-28 16:46:49 +00004274 if (transport_id == -1) {
Benny Prijonod6e362a2008-07-19 17:53:47 +00004275 PJ_LOG(1,(THIS_FILE, "Error: no transport is configured"));
Benny Prijonoe93e2872006-06-28 16:46:49 +00004276 status = -1;
4277 goto on_error;
4278 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004279
4280
4281 /* Add accounts */
4282 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00004283 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004284 if (status != PJ_SUCCESS)
4285 goto on_error;
4286 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
4287 }
4288
4289 /* Add buddies */
4290 for (i=0; i<app_config.buddy_cnt; ++i) {
4291 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
4292 if (status != PJ_SUCCESS)
4293 goto on_error;
4294 }
4295
4296 /* Optionally set codec orders */
4297 for (i=0; i<app_config.codec_cnt; ++i) {
4298 pjsua_codec_set_priority(&app_config.codec_arg[i],
4299 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
4300 }
4301
Benny Prijonofce28542007-12-09 15:41:10 +00004302 /* Optionally disable some codec */
4303 for (i=0; i<app_config.codec_dis_cnt; ++i) {
4304 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
4305 }
4306
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004307 /* Add RTP transports */
Benny Prijono40860c32008-09-04 13:55:33 +00004308#ifdef TRANSPORT_ADAPTER_SAMPLE
4309 status = transport_adapter_sample();
4310
4311#else
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004312 status = pjsua_media_transports_create(&app_config.rtp_cfg);
Benny Prijono40860c32008-09-04 13:55:33 +00004313#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004314 if (status != PJ_SUCCESS)
4315 goto on_error;
4316
Nanang Izzuddind7fefd72008-06-12 12:48:59 +00004317 /* Set sound device latency */
4318 pjmedia_snd_set_latency(app_config.capture_lat, app_config.playback_lat);
4319
Benny Prijono22a300a2006-06-14 20:04:55 +00004320 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00004321#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00004322 if (app_config.null_audio) {
4323 status = pjsua_set_null_snd_dev();
4324 if (status != PJ_SUCCESS)
4325 return status;
4326 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00004327#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00004328
Nanang Izzuddin68559c32008-06-13 17:01:46 +00004329 if (app_config.capture_dev != PJSUA_INVALID_ID ||
4330 app_config.playback_dev != PJSUA_INVALID_ID)
4331 {
4332 status = pjsua_set_snd_dev(app_config.capture_dev,
4333 app_config.playback_dev);
Benny Prijono4e5d5512007-03-06 18:11:30 +00004334 if (status != PJ_SUCCESS)
4335 goto on_error;
4336 }
4337
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004338 return PJ_SUCCESS;
4339
4340on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004341 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004342 return status;
4343}
4344
4345
Benny Prijonoebc32c32008-06-12 13:30:39 +00004346static int stdout_refresh_proc(void *arg)
4347{
4348 PJ_UNUSED_ARG(arg);
4349
4350 /* Set thread to lowest priority so that it doesn't clobber
4351 * stdout output
4352 */
4353 pj_thread_set_prio(pj_thread_this(),
4354 pj_thread_get_prio_min(pj_thread_this()));
4355
4356 while (!stdout_refresh_quit) {
4357 pj_thread_sleep(stdout_refresh * 1000);
4358 puts(stdout_refresh_text);
4359 fflush(stdout);
4360 }
4361
4362 return 0;
4363}
4364
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004365pj_status_t app_main(void)
4366{
Benny Prijonoebc32c32008-06-12 13:30:39 +00004367 pj_thread_t *stdout_refresh_thread = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004368 pj_status_t status;
4369
4370 /* Start pjsua */
4371 status = pjsua_start();
4372 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00004373 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004374 return status;
4375 }
4376
Benny Prijonoebc32c32008-06-12 13:30:39 +00004377 /* Start console refresh thread */
4378 if (stdout_refresh > 0) {
4379 pj_thread_create(app_config.pool, "stdout", &stdout_refresh_proc,
4380 NULL, 0, 0, &stdout_refresh_thread);
4381 }
4382
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004383 console_app_main(&uri_arg);
4384
Benny Prijonoebc32c32008-06-12 13:30:39 +00004385 if (stdout_refresh_thread) {
4386 stdout_refresh_quit = PJ_TRUE;
4387 pj_thread_join(stdout_refresh_thread);
4388 pj_thread_destroy(stdout_refresh_thread);
4389 }
4390
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004391 return PJ_SUCCESS;
4392}
4393
4394pj_status_t app_destroy(void)
4395{
Benny Prijonof762ee72006-12-01 11:14:37 +00004396 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00004397 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00004398
Benny Prijonoe909eac2006-07-27 22:04:56 +00004399#ifdef STEREO_DEMO
4400 if (app_config.snd) {
4401 pjmedia_snd_port_destroy(app_config.snd);
4402 app_config.snd = NULL;
4403 }
4404#endif
4405
Benny Prijono91d20f42008-06-14 19:42:37 +00004406 /* Close ringback port */
4407 if (app_config.ringback_port &&
4408 app_config.ringback_slot != PJSUA_INVALID_ID)
4409 {
4410 pjsua_conf_remove_port(app_config.ringback_slot);
4411 app_config.ringback_slot = PJSUA_INVALID_ID;
4412 pjmedia_port_destroy(app_config.ringback_port);
4413 app_config.ringback_port = NULL;
4414 }
4415
4416 /* Close ring port */
4417 if (app_config.ring_port && app_config.ring_slot != PJSUA_INVALID_ID) {
4418 pjsua_conf_remove_port(app_config.ring_slot);
4419 app_config.ring_slot = PJSUA_INVALID_ID;
4420 pjmedia_port_destroy(app_config.ring_port);
4421 app_config.ring_port = NULL;
4422 }
4423
Benny Prijono4af234b2007-01-24 02:02:09 +00004424 /* Close tone generators */
4425 for (i=0; i<app_config.tone_count; ++i) {
4426 pjsua_conf_remove_port(app_config.tone_slots[i]);
4427 }
4428
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004429 if (app_config.pool) {
4430 pj_pool_release(app_config.pool);
4431 app_config.pool = NULL;
4432 }
4433
Benny Prijonof762ee72006-12-01 11:14:37 +00004434 status = pjsua_destroy();
4435
4436 pj_bzero(&app_config, sizeof(app_config));
4437
4438 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004439}
Benny Prijonoe909eac2006-07-27 22:04:56 +00004440
4441
4442#ifdef STEREO_DEMO
4443static void stereo_demo()
4444{
4445 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00004446 pj_status_t status;
4447
4448 /* Disable existing sound device */
4449 conf = pjsua_set_no_snd_dev();
4450
Benny Prijonoe909eac2006-07-27 22:04:56 +00004451 /* Create stereo-mono splitter/combiner */
4452 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004453 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004454 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004455 2 * conf->info.samples_per_frame,
4456 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004457 0 /* options */,
4458 &splitter);
4459 pj_assert(status == PJ_SUCCESS);
4460
4461 /* Connect channel0 (left channel?) to conference port slot0 */
4462 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
4463 0 /*options*/,
4464 conf);
4465 pj_assert(status == PJ_SUCCESS);
4466
4467 /* Create reverse channel for channel1 (right channel?)... */
4468 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
4469 splitter,
4470 1 /* ch1 */,
4471 0 /* options */,
4472 &ch1);
4473 pj_assert(status == PJ_SUCCESS);
4474
4475 /* .. and register it to conference bridge (it would be slot1
4476 * if there's no other devices connected to the bridge)
4477 */
4478 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
4479 pj_assert(status == PJ_SUCCESS);
4480
4481 /* Create sound device */
4482 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004483 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004484 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00004485 2 * conf->info.samples_per_frame,
4486 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00004487 0, &app_config.snd);
4488 pj_assert(status == PJ_SUCCESS);
4489
4490
4491 /* Connect the splitter to the sound device */
4492 status = pjmedia_snd_port_connect(app_config.snd, splitter);
4493 pj_assert(status == PJ_SUCCESS);
4494
4495}
4496#endif
4497
Benny Prijono40860c32008-09-04 13:55:33 +00004498#ifdef TRANSPORT_ADAPTER_SAMPLE
4499static pj_status_t create_transport_adapter(pjmedia_endpt *med_endpt, int port,
4500 pjmedia_transport **p_tp)
4501{
4502 pjmedia_transport *udp;
4503 pj_status_t status;
4504
4505 /* Create the UDP media transport */
4506 status = pjmedia_transport_udp_create(med_endpt, NULL, port, 0, &udp);
4507 if (status != PJ_SUCCESS)
4508 return status;
4509
4510 /* Create the adapter */
4511 status = pjmedia_tp_adapter_create(med_endpt, NULL, udp, p_tp);
4512 if (status != PJ_SUCCESS) {
4513 pjmedia_transport_close(udp);
4514 return status;
4515 }
4516
4517 return PJ_SUCCESS;
4518}
4519
4520static pj_status_t transport_adapter_sample(void)
4521{
4522 pjsua_media_transport tp[PJSUA_MAX_CALLS];
4523 pj_status_t status;
4524 int port = 7000;
4525 unsigned i;
4526
4527 for (i=0; i<app_config.cfg.max_calls; ++i) {
4528 status = create_transport_adapter(pjsua_get_pjmedia_endpt(),
4529 port + i*10,
4530 &tp[i].transport);
4531 if (status != PJ_SUCCESS)
4532 return status;
4533 }
4534
4535 return pjsua_media_transports_attach(tp, i, PJ_TRUE);
4536}
4537#endif
4538