blob: f9dd1a3102149811c264dbc3b97e59a806e2d6cd [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 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 Prijonoeebe9af2006-06-13 22:57:13 +000026
Benny Prijono804ff0a2006-09-14 11:17:48 +000027/* Call specific data */
28struct call_data
29{
30 pj_timer_entry timer;
31};
32
Benny Prijonoeebe9af2006-06-13 22:57:13 +000033
34/* Pjsua application data */
35static struct app_config
36{
37 pjsua_config cfg;
38 pjsua_logging_config log_cfg;
39 pjsua_media_config media_cfg;
Benny Prijono4ddad2c2006-10-18 17:16:34 +000040 pj_bool_t no_refersub;
Benny Prijonoe93e2872006-06-28 16:46:49 +000041 pj_bool_t no_tcp;
42 pj_bool_t no_udp;
Benny Prijono6e0e54b2006-12-08 21:58:31 +000043 pj_bool_t use_tls;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000044 pjsua_transport_config udp_cfg;
45 pjsua_transport_config rtp_cfg;
46
47 unsigned acc_cnt;
48 pjsua_acc_config acc_cfg[PJSUA_MAX_ACC];
49
50 unsigned buddy_cnt;
51 pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES];
52
Benny Prijono804ff0a2006-09-14 11:17:48 +000053 struct call_data call_data[PJSUA_MAX_CALLS];
54
Benny Prijonoeebe9af2006-06-13 22:57:13 +000055 pj_pool_t *pool;
56 /* Compatibility with older pjsua */
57
58 unsigned codec_cnt;
59 pj_str_t codec_arg[32];
Benny Prijonofce28542007-12-09 15:41:10 +000060 unsigned codec_dis_cnt;
61 pj_str_t codec_dis[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000062 pj_bool_t null_audio;
Benny Prijono32e4f492007-01-24 00:44:26 +000063 unsigned wav_count;
64 pj_str_t wav_files[32];
Benny Prijono4af234b2007-01-24 02:02:09 +000065 unsigned tone_count;
66 pjmedia_tone_desc tones[32];
67 pjsua_conf_port_id tone_slots[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +000068 pjsua_player_id wav_id;
69 pjsua_conf_port_id wav_port;
70 pj_bool_t auto_play;
71 pj_bool_t auto_loop;
Benny Prijono7ca96da2006-08-07 12:11:40 +000072 pj_bool_t auto_conf;
Benny Prijono1ebd6142006-10-19 15:48:02 +000073 pj_str_t rec_file;
74 pj_bool_t auto_rec;
75 pjsua_recorder_id rec_id;
76 pjsua_conf_port_id rec_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000077 unsigned auto_answer;
78 unsigned duration;
Benny Prijonoe909eac2006-07-27 22:04:56 +000079
80#ifdef STEREO_DEMO
81 pjmedia_snd_port *snd;
82#endif
83
Benny Prijono6dd967c2006-12-26 02:27:14 +000084 float mic_level,
85 speaker_level;
86
Benny Prijono4e5d5512007-03-06 18:11:30 +000087 int capture_dev, playback_dev;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000088} app_config;
89
90
Benny Prijono21b9ad92006-08-15 13:11:22 +000091//static pjsua_acc_id current_acc;
92#define current_acc pjsua_acc_get_default()
Benny Prijonof7b1c392006-11-11 16:46:34 +000093static pjsua_call_id current_call = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000094static pj_str_t uri_arg;
95
Benny Prijono594e4c52006-09-14 18:51:01 +000096#ifdef STEREO_DEMO
Benny Prijonoe909eac2006-07-27 22:04:56 +000097static void stereo_demo();
Benny Prijono594e4c52006-09-14 18:51:01 +000098#endif
Benny Prijonoad2e0ca2007-04-29 12:31:51 +000099pj_status_t app_destroy(void);
Benny Prijonoe909eac2006-07-27 22:04:56 +0000100
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000101/*****************************************************************************
102 * Configuration manipulation
103 */
104
105/* Show usage */
106static void usage(void)
107{
108 puts ("Usage:");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000109 puts (" pjsua [options] [SIP URL to call]");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000110 puts ("");
111 puts ("General options:");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000112 puts (" --config-file=file Read the config/arguments from file.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000113 puts (" --help Display this help screen");
114 puts (" --version Display version info");
115 puts ("");
116 puts ("Logging options:");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000117 puts (" --log-file=fname Log to filename (default stderr)");
118 puts (" --log-level=N Set log max level to N (0(none) to 6(trace)) (default=5)");
119 puts (" --app-log-level=N Set log max level for stdout display (default=4)");
120 puts ("");
121 puts ("SIP Account options:");
Benny Prijono48ab2b72007-11-08 09:24:30 +0000122 puts (" --use-ims Enable 3GPP/IMS related settings on this account");
Benny Prijonod8179652008-01-23 20:39:07 +0000123#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijonof6508982008-01-25 09:02:33 +0000124 puts (" --use-srtp=N Use SRTP? 0:disabled, 1:optional, 2:mandatory (def:0)");
125 puts (" --srtp-secure=N SRTP require secure SIP? 0:no, 1:tls, 1:sips (def:1)");
Benny Prijonod8179652008-01-23 20:39:07 +0000126#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000127 puts (" --registrar=url Set the URL of registrar server");
128 puts (" --id=url Set the URL of local ID (used in From header)");
129 puts (" --contact=url Optionally override the Contact information");
130 puts (" --proxy=url Optional URL of proxy server to visit");
131 puts (" May be specified multiple times");
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000132 puts (" --reg-timeout=SEC Optional registration interval (default 55)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000133 puts (" --realm=string Set realm");
134 puts (" --username=string Set authentication username");
135 puts (" --password=string Set authentication password");
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000136 puts (" --publish Send presence PUBLISH for this account");
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000137 puts (" --use-100rel Require reliable provisional response (100rel)");
Benny Prijonofce28542007-12-09 15:41:10 +0000138 puts (" --auto-update-nat=N Where N is 0 or 1 to enable/disable SIP traversal behind");
139 puts (" symmetric NAT (default 1)");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000140 puts (" --next-cred Add another credentials");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000141 puts ("");
142 puts ("SIP Account Control:");
143 puts (" --next-account Add more account");
144 puts ("");
145 puts ("Transport Options:");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000146 puts (" --local-port=port Set TCP/UDP port. This implicitly enables both ");
147 puts (" TCP and UDP transports on the specified port, unless");
148 puts (" if TCP or UDP is disabled.");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000149 puts (" --ip-addr=IP Use the specifed address as SIP and RTP addresses.");
150 puts (" (Hint: the IP may be the public IP of the NAT/router)");
Benny Prijonoe93e2872006-06-28 16:46:49 +0000151 puts (" --no-tcp Disable TCP transport.");
152 puts (" --no-udp Disable UDP transport.");
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000153 puts (" --nameserver=NS Add the specified nameserver to enable SRV resolution");
154 puts (" This option can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000155 puts (" --outbound=url Set the URL of global outbound proxy server");
156 puts (" May be specified multiple times");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000157 puts (" --stun-srv=name Set STUN server host or domain");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000158 puts ("");
159 puts ("TLS Options:");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000160 puts (" --use-tls Enable TLS transport (default=no)");
Benny Prijonof3bbc132006-12-25 06:43:59 +0000161 puts (" --tls-ca-file Specify TLS CA file (default=none)");
162 puts (" --tls-cert-file Specify TLS certificate file (default=none)");
163 puts (" --tls-privkey-file Specify TLS private key file (default=none)");
164 puts (" --tls-password Specify TLS password to private key file (default=none)");
165 puts (" --tls-verify-server Verify server's certificate (default=no)");
166 puts (" --tls-verify-client Verify client's certificate (default=no)");
167 puts (" --tls-neg-timeout Specify TLS negotiation timeout (default=no)");
168
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000169 puts ("");
170 puts ("Media Options:");
Benny Prijonoc97608e2007-03-23 16:34:20 +0000171 puts (" --use-ice Enable ICE (default:no)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000172 puts (" --add-codec=name Manually add codec (default is to enable all)");
Benny Prijonofce28542007-12-09 15:41:10 +0000173 puts (" --dis-codec=name Disable codec (can be specified multiple times)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000174 puts (" --clock-rate=N Override sound device clock rate");
175 puts (" --null-audio Use NULL audio device");
Benny Prijono4af234b2007-01-24 02:02:09 +0000176 puts (" --play-file=file Register WAV file in conference bridge.");
177 puts (" This can be specified multiple times.");
Benny Prijonob67eaac2007-02-18 20:56:00 +0000178 puts (" --play-tone=FORMAT Register tone to the conference bridge.");
179 puts (" FORMAT is 'F1,F2,ON,OFF', where F1,F2 are");
180 puts (" frequencies, and ON,OFF=on/off duration in msec.");
Benny Prijono4af234b2007-01-24 02:02:09 +0000181 puts (" This can be specified multiple times.");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182 puts (" --auto-play Automatically play the file (to incoming calls only)");
183 puts (" --auto-loop Automatically loop incoming RTP to outgoing RTP");
Benny Prijono7ca96da2006-08-07 12:11:40 +0000184 puts (" --auto-conf Automatically put calls in conference with others");
Benny Prijono1ebd6142006-10-19 15:48:02 +0000185 puts (" --rec-file=file Open file recorder (extension can be .wav or .mp3");
186 puts (" --auto-rec Automatically record conversation");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000187 puts (" --rtp-port=N Base port to try for RTP (default=4000)");
Benny Prijono00cae612006-07-31 15:19:36 +0000188 puts (" --quality=N Specify media quality (0-10, default=6)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000189 puts (" --ptime=MSEC Override codec ptime to MSEC (default=specific)");
Benny Prijono0a12f002006-07-26 17:05:39 +0000190 puts (" --no-vad Disable VAD/silence detector (default=vad enabled)");
Benny Prijonod79f25c2006-08-02 19:41:37 +0000191 puts (" --ec-tail=MSEC Set echo canceller tail length (default=256)");
Benny Prijono00cae612006-07-31 15:19:36 +0000192 puts (" --ilbc-mode=MODE Set iLBC codec mode (20 or 30, default is 20)");
193 puts (" --rx-drop-pct=PCT Drop PCT percent of RX RTP (for pkt lost sim, default: 0)");
194 puts (" --tx-drop-pct=PCT Drop PCT percent of TX RTP (for pkt lost sim, default: 0)");
Benny Prijono4e5d5512007-03-06 18:11:30 +0000195 puts (" --capture-dev=id Audio capture device ID (default=-1)");
196 puts (" --playback-dev=id Audio playback device ID (default=-1)");
Benny Prijono00cae612006-07-31 15:19:36 +0000197
Benny Prijono0a12f002006-07-26 17:05:39 +0000198
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199 puts ("");
200 puts ("Buddy List (can be more than one):");
201 puts (" --add-buddy url Add the specified URL to the buddy list.");
202 puts ("");
203 puts ("User Agent options:");
204 puts (" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
205 puts (" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof521eb02006-08-06 23:07:25 +0000206 puts (" --thread-cnt=N Number of worker threads (default:1)");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000207 puts (" --duration=SEC Set maximum call duration (default:no limit)");
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000208 puts (" --norefersub Suppress event subscription when transfering calls");
Benny Prijonofce28542007-12-09 15:41:10 +0000209 puts (" --use-compact-form Minimize SIP message size");
Benny Prijono804ff0a2006-09-14 11:17:48 +0000210
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000211 puts ("");
Benny Prijono0a5cad82006-09-26 13:21:02 +0000212 puts ("When URL is specified, pjsua will immediately initiate call to that URL");
213 puts ("");
214
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000215 fflush(stdout);
216}
217
218
219/* Set default config. */
220static void default_config(struct app_config *cfg)
221{
Benny Prijono56315612006-07-18 14:39:40 +0000222 char tmp[80];
Benny Prijono1febfdf2007-02-18 01:35:04 +0000223 unsigned i;
Benny Prijono56315612006-07-18 14:39:40 +0000224
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000225 pjsua_config_default(&cfg->cfg);
Benny Prijono106f5b72007-08-30 13:49:33 +0000226 pj_ansi_sprintf(tmp, "PJSUA v%s/%s", pj_get_version(), PJ_OS_NAME);
Benny Prijono56315612006-07-18 14:39:40 +0000227 pj_strdup2_with_null(app_config.pool, &cfg->cfg.user_agent, tmp);
228
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000229 pjsua_logging_config_default(&cfg->log_cfg);
230 pjsua_media_config_default(&cfg->media_cfg);
231 pjsua_transport_config_default(&cfg->udp_cfg);
232 cfg->udp_cfg.port = 5060;
233 pjsua_transport_config_default(&cfg->rtp_cfg);
234 cfg->rtp_cfg.port = 4000;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000235 cfg->duration = NO_LIMIT;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000236 cfg->wav_id = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000237 cfg->rec_id = PJSUA_INVALID_ID;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000238 cfg->wav_port = PJSUA_INVALID_ID;
Benny Prijono1ebd6142006-10-19 15:48:02 +0000239 cfg->rec_port = PJSUA_INVALID_ID;
Benny Prijono6dd967c2006-12-26 02:27:14 +0000240 cfg->mic_level = cfg->speaker_level = 1.0;
Benny Prijono4e5d5512007-03-06 18:11:30 +0000241 cfg->capture_dev = PJSUA_INVALID_ID;
242 cfg->playback_dev = PJSUA_INVALID_ID;
Benny Prijono1febfdf2007-02-18 01:35:04 +0000243
244 for (i=0; i<PJ_ARRAY_SIZE(cfg->acc_cfg); ++i)
245 pjsua_acc_config_default(&cfg->acc_cfg[i]);
246
247 for (i=0; i<PJ_ARRAY_SIZE(cfg->buddy_cfg); ++i)
248 pjsua_buddy_config_default(&cfg->buddy_cfg[i]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000249}
250
251
252/*
253 * Read command arguments from config file.
254 */
255static int read_config_file(pj_pool_t *pool, const char *filename,
256 int *app_argc, char ***app_argv)
257{
258 int i;
259 FILE *fhnd;
260 char line[200];
261 int argc = 0;
262 char **argv;
263 enum { MAX_ARGS = 64 };
264
265 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
266 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
267 argv[argc++] = *app_argv[0];
268
269 /* Open config file. */
270 fhnd = fopen(filename, "rt");
271 if (!fhnd) {
272 PJ_LOG(1,(THIS_FILE, "Unable to open config file %s", filename));
273 fflush(stdout);
274 return -1;
275 }
276
277 /* Scan tokens in the file. */
278 while (argc < MAX_ARGS && !feof(fhnd)) {
Benny Prijonobf5b4692007-06-28 03:20:17 +0000279 char *token;
280 char *p;
281 const char *whitespace = " \t\r\n";
282 char cDelimiter;
283 int len, token_len;
284
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000285 if (fgets(line, sizeof(line), fhnd) == NULL) break;
Benny Prijonobf5b4692007-06-28 03:20:17 +0000286
287 // Trim ending newlines
288 len = strlen(line);
289 if (line[len-1]=='\n')
290 line[--len] = '\0';
291 if (line[len-1]=='\r')
292 line[--len] = '\0';
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000293
Benny Prijonobf5b4692007-06-28 03:20:17 +0000294 if (len==0) continue;
295
296 for (p = line; *p != '\0' && argc < MAX_ARGS; p++) {
297 // first, scan whitespaces
298 while (*p != '\0' && strchr(whitespace, *p) != NULL) p++;
299
300 if (*p == '\0') // are we done yet?
301 break;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000302
Benny Prijonobf5b4692007-06-28 03:20:17 +0000303 if (*p == '"' || *p == '\'') { // is token a quoted string
304 cDelimiter = *p++; // save quote delimiter
305 token = p;
306
307 while (*p != '\0' && *p != cDelimiter) p++;
308
309 if (*p == '\0') // found end of the line, but,
310 cDelimiter = '\0'; // didn't find a matching quote
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000311
Benny Prijonobf5b4692007-06-28 03:20:17 +0000312 } else { // token's not a quoted string
313 token = p;
314
315 while (*p != '\0' && strchr(whitespace, *p) == NULL) p++;
316
317 cDelimiter = *p;
318 }
319
320 *p = '\0';
321 token_len = p-token;
322
323 if (token_len > 0) {
324 if (*token == '#')
325 break; // ignore remainder of line
326
327 argv[argc] = pj_pool_alloc(pool, token_len + 1);
328 pj_memcpy(argv[argc], token, token_len + 1);
329 ++argc;
330 }
331
332 *p = cDelimiter;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000333 }
334 }
335
336 /* Copy arguments from command line */
337 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
338 argv[argc++] = (*app_argv)[i];
339
340 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
341 PJ_LOG(1,(THIS_FILE,
342 "Too many arguments specified in cmd line/config file"));
343 fflush(stdout);
344 fclose(fhnd);
345 return -1;
346 }
347
348 fclose(fhnd);
349
350 /* Assign the new command line back to the original command line. */
351 *app_argc = argc;
352 *app_argv = argv;
353 return 0;
354
355}
356
357static int my_atoi(const char *cs)
358{
359 pj_str_t s;
Benny Prijono1e2dbe62007-06-15 04:15:16 +0000360
361 pj_cstr(&s, cs);
362 if (cs[0] == '-') {
363 s.ptr++, s.slen--;
364 return 0 - (int)pj_strtoul(&s);
365 } else if (cs[0] == '+') {
366 s.ptr++, s.slen--;
367 return pj_strtoul(&s);
368 } else {
369 return pj_strtoul(&s);
370 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000371}
372
373
374/* Parse arguments. */
375static pj_status_t parse_args(int argc, char *argv[],
376 struct app_config *cfg,
377 pj_str_t *uri_to_call)
378{
379 int c;
380 int option_index;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000381 enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000382 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO,
Benny Prijono0a5cad82006-09-26 13:21:02 +0000383 OPT_LOCAL_PORT, OPT_IP_ADDR, OPT_PROXY, OPT_OUTBOUND_PROXY,
384 OPT_REGISTRAR, OPT_REG_TIMEOUT, OPT_PUBLISH, OPT_ID, OPT_CONTACT,
Benny Prijono48ab2b72007-11-08 09:24:30 +0000385 OPT_100REL, OPT_USE_IMS, OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
Benny Prijonoebbf6892007-03-24 17:37:25 +0000386 OPT_NAMESERVER, OPT_STUN_DOMAIN, OPT_STUN_SRV,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
388 OPT_AUTO_ANSWER, OPT_AUTO_HANGUP, OPT_AUTO_PLAY, OPT_AUTO_LOOP,
Benny Prijonof6508982008-01-25 09:02:33 +0000389 OPT_AUTO_CONF, OPT_CLOCK_RATE, OPT_USE_ICE, OPT_USE_SRTP,
390 OPT_SRTP_SECURE,
Benny Prijono4af234b2007-01-24 02:02:09 +0000391 OPT_PLAY_FILE, OPT_PLAY_TONE, OPT_RTP_PORT, OPT_ADD_CODEC,
392 OPT_ILBC_MODE, OPT_REC_FILE, OPT_AUTO_REC,
Benny Prijono0a12f002006-07-26 17:05:39 +0000393 OPT_COMPLEXITY, OPT_QUALITY, OPT_PTIME, OPT_NO_VAD,
Benny Prijonod79f25c2006-08-02 19:41:37 +0000394 OPT_RX_DROP_PCT, OPT_TX_DROP_PCT, OPT_EC_TAIL,
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000395 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
Benny Prijonof521eb02006-08-06 23:07:25 +0000396 OPT_DURATION, OPT_NO_TCP, OPT_NO_UDP, OPT_THREAD_CNT,
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000397 OPT_NOREFERSUB,
Benny Prijonof3bbc132006-12-25 06:43:59 +0000398 OPT_USE_TLS, OPT_TLS_CA_FILE, OPT_TLS_CERT_FILE, OPT_TLS_PRIV_FILE,
399 OPT_TLS_PASSWORD, OPT_TLS_VERIFY_SERVER, OPT_TLS_VERIFY_CLIENT,
400 OPT_TLS_NEG_TIMEOUT,
Benny Prijono4e5d5512007-03-06 18:11:30 +0000401 OPT_CAPTURE_DEV, OPT_PLAYBACK_DEV,
Benny Prijonofce28542007-12-09 15:41:10 +0000402 OPT_AUTO_UPDATE_NAT,OPT_USE_COMPACT_FORM,OPT_DIS_CODEC
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000403 };
404 struct pj_getopt_option long_options[] = {
405 { "config-file",1, 0, OPT_CONFIG_FILE},
406 { "log-file", 1, 0, OPT_LOG_FILE},
407 { "log-level", 1, 0, OPT_LOG_LEVEL},
408 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
409 { "help", 0, 0, OPT_HELP},
410 { "version", 0, 0, OPT_VERSION},
411 { "clock-rate", 1, 0, OPT_CLOCK_RATE},
412 { "null-audio", 0, 0, OPT_NULL_AUDIO},
413 { "local-port", 1, 0, OPT_LOCAL_PORT},
Benny Prijono0a5cad82006-09-26 13:21:02 +0000414 { "ip-addr", 1, 0, OPT_IP_ADDR},
Benny Prijonoe93e2872006-06-28 16:46:49 +0000415 { "no-tcp", 0, 0, OPT_NO_TCP},
416 { "no-udp", 0, 0, OPT_NO_UDP},
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000417 { "norefersub", 0, 0, OPT_NOREFERSUB},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000418 { "proxy", 1, 0, OPT_PROXY},
419 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
420 { "registrar", 1, 0, OPT_REGISTRAR},
421 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000422 { "publish", 0, 0, OPT_PUBLISH},
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000423 { "use-100rel", 0, 0, OPT_100REL},
Benny Prijono48ab2b72007-11-08 09:24:30 +0000424 { "use-ims", 0, 0, OPT_USE_IMS},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000425 { "id", 1, 0, OPT_ID},
426 { "contact", 1, 0, OPT_CONTACT},
Benny Prijonofce28542007-12-09 15:41:10 +0000427 { "auto-update-nat", 1, 0, OPT_AUTO_UPDATE_NAT},
428 { "use-compact-form", 0, 0, OPT_USE_COMPACT_FORM},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000429 { "realm", 1, 0, OPT_REALM},
430 { "username", 1, 0, OPT_USERNAME},
431 { "password", 1, 0, OPT_PASSWORD},
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000432 { "nameserver", 1, 0, OPT_NAMESERVER},
Benny Prijonoebbf6892007-03-24 17:37:25 +0000433 { "stun-domain",1, 0, OPT_STUN_DOMAIN},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000434 { "stun-srv", 1, 0, OPT_STUN_SRV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000435 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
436 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
437 { "no-presence", 0, 0, OPT_NO_PRESENCE},
438 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
439 { "auto-hangup",1, 0, OPT_AUTO_HANGUP},
440 { "auto-play", 0, 0, OPT_AUTO_PLAY},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000441 { "auto-rec", 0, 0, OPT_AUTO_REC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000442 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
443 { "auto-conf", 0, 0, OPT_AUTO_CONF},
444 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijono4af234b2007-01-24 02:02:09 +0000445 { "play-tone", 1, 0, OPT_PLAY_TONE},
Benny Prijono1ebd6142006-10-19 15:48:02 +0000446 { "rec-file", 1, 0, OPT_REC_FILE},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000447 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonoc97608e2007-03-23 16:34:20 +0000448 { "use-ice", 0, 0, OPT_USE_ICE},
Benny Prijonod8179652008-01-23 20:39:07 +0000449#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
450 { "use-srtp", 1, 0, OPT_USE_SRTP},
Benny Prijonof6508982008-01-25 09:02:33 +0000451 { "srtp-secure",1, 0, OPT_SRTP_SECURE},
Benny Prijonod8179652008-01-23 20:39:07 +0000452#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000453 { "add-codec", 1, 0, OPT_ADD_CODEC},
Benny Prijonofce28542007-12-09 15:41:10 +0000454 { "dis-codec", 1, 0, OPT_DIS_CODEC},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000455 { "complexity", 1, 0, OPT_COMPLEXITY},
456 { "quality", 1, 0, OPT_QUALITY},
457 { "ptime", 1, 0, OPT_PTIME},
Benny Prijono0a12f002006-07-26 17:05:39 +0000458 { "no-vad", 0, 0, OPT_NO_VAD},
Benny Prijonod79f25c2006-08-02 19:41:37 +0000459 { "ec-tail", 1, 0, OPT_EC_TAIL},
Benny Prijono00cae612006-07-31 15:19:36 +0000460 { "ilbc-mode", 1, 0, OPT_ILBC_MODE},
461 { "rx-drop-pct",1, 0, OPT_RX_DROP_PCT},
462 { "tx-drop-pct",1, 0, OPT_TX_DROP_PCT},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000463 { "next-account",0,0, OPT_NEXT_ACCOUNT},
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000464 { "next-cred", 0, 0, OPT_NEXT_CRED},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000465 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof521eb02006-08-06 23:07:25 +0000466 { "duration", 1, 0, OPT_DURATION},
467 { "thread-cnt", 1, 0, OPT_THREAD_CNT},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000468 { "use-tls", 0, 0, OPT_USE_TLS},
469 { "tls-ca-file",1, 0, OPT_TLS_CA_FILE},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000470 { "tls-cert-file",1,0, OPT_TLS_CERT_FILE},
471 { "tls-privkey-file",1,0, OPT_TLS_PRIV_FILE},
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000472 { "tls-password",1,0, OPT_TLS_PASSWORD},
Benny Prijonof3bbc132006-12-25 06:43:59 +0000473 { "tls-verify-server", 0, 0, OPT_TLS_VERIFY_SERVER},
474 { "tls-verify-client", 0, 0, OPT_TLS_VERIFY_CLIENT},
475 { "tls-neg-timeout", 1, 0, OPT_TLS_NEG_TIMEOUT},
Benny Prijono4e5d5512007-03-06 18:11:30 +0000476 { "capture-dev", 1, 0, OPT_CAPTURE_DEV},
477 { "playback-dev", 1, 0, OPT_PLAYBACK_DEV},
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000478 { NULL, 0, 0, 0}
479 };
480 pj_status_t status;
481 pjsua_acc_config *cur_acc;
482 char *config_file = NULL;
483 unsigned i;
484
485 /* Run pj_getopt once to see if user specifies config file to read. */
Benny Prijonof762ee72006-12-01 11:14:37 +0000486 pj_optind = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487 while ((c=pj_getopt_long(argc, argv, "", long_options,
488 &option_index)) != -1)
489 {
490 switch (c) {
491 case OPT_CONFIG_FILE:
492 config_file = pj_optarg;
493 break;
494 }
495 if (config_file)
496 break;
497 }
498
499 if (config_file) {
500 status = read_config_file(app_config.pool, config_file, &argc, &argv);
501 if (status != 0)
502 return status;
503 }
504
505 cfg->acc_cnt = 0;
506 cur_acc = &cfg->acc_cfg[0];
507
508
509 /* Reinitialize and re-run pj_getopt again, possibly with new arguments
510 * read from config file.
511 */
512 pj_optind = 0;
513 while((c=pj_getopt_long(argc,argv, "", long_options,&option_index))!=-1) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514 pj_str_t tmp;
515 long lval;
516
517 switch (c) {
518
Benny Prijono6f137482006-06-15 11:31:36 +0000519 case OPT_CONFIG_FILE:
520 /* Ignore as this has been processed before */
521 break;
522
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000523 case OPT_LOG_FILE:
524 cfg->log_cfg.log_filename = pj_str(pj_optarg);
525 break;
526
527 case OPT_LOG_LEVEL:
528 c = pj_strtoul(pj_cstr(&tmp, pj_optarg));
529 if (c < 0 || c > 6) {
530 PJ_LOG(1,(THIS_FILE,
531 "Error: expecting integer value 0-6 "
532 "for --log-level"));
533 return PJ_EINVAL;
534 }
535 cfg->log_cfg.level = c;
536 pj_log_set_level( c );
537 break;
538
539 case OPT_APP_LOG_LEVEL:
540 cfg->log_cfg.console_level = pj_strtoul(pj_cstr(&tmp, pj_optarg));
541 if (cfg->log_cfg.console_level < 0 || cfg->log_cfg.console_level > 6) {
542 PJ_LOG(1,(THIS_FILE,
543 "Error: expecting integer value 0-6 "
544 "for --app-log-level"));
545 return PJ_EINVAL;
546 }
547 break;
548
549 case OPT_HELP:
550 usage();
551 return PJ_EINVAL;
552
553 case OPT_VERSION: /* version */
554 pj_dump_config();
555 return PJ_EINVAL;
556
557 case OPT_NULL_AUDIO:
558 cfg->null_audio = PJ_TRUE;
559 break;
560
561 case OPT_CLOCK_RATE:
562 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
563 if (lval < 8000 || lval > 48000) {
564 PJ_LOG(1,(THIS_FILE, "Error: expecting value between "
565 "8000-48000 for clock rate"));
566 return PJ_EINVAL;
567 }
568 cfg->media_cfg.clock_rate = lval;
569 break;
570
571 case OPT_LOCAL_PORT: /* local-port */
572 lval = pj_strtoul(pj_cstr(&tmp, pj_optarg));
Benny Prijonoe347cb02007-02-14 14:36:13 +0000573 if (lval < 0 || lval > 65535) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000574 PJ_LOG(1,(THIS_FILE,
575 "Error: expecting integer value for "
576 "--local-port"));
577 return PJ_EINVAL;
578 }
579 cfg->udp_cfg.port = (pj_uint16_t)lval;
580 break;
581
Benny Prijono0a5cad82006-09-26 13:21:02 +0000582 case OPT_IP_ADDR: /* ip-addr */
583 cfg->udp_cfg.public_addr = pj_str(pj_optarg);
584 cfg->rtp_cfg.public_addr = pj_str(pj_optarg);
585 break;
586
Benny Prijonoe93e2872006-06-28 16:46:49 +0000587 case OPT_NO_UDP: /* no-udp */
588 if (cfg->no_tcp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000589 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
590 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000591 }
592
593 cfg->no_udp = PJ_TRUE;
594 break;
595
Benny Prijono4ddad2c2006-10-18 17:16:34 +0000596 case OPT_NOREFERSUB: /* norefersub */
597 cfg->no_refersub = PJ_TRUE;
598 break;
599
Benny Prijonoe93e2872006-06-28 16:46:49 +0000600 case OPT_NO_TCP: /* no-tcp */
601 if (cfg->no_udp) {
Benny Prijonob988d762007-12-05 04:30:04 +0000602 PJ_LOG(1,(THIS_FILE,"Error: can not disable both TCP and UDP"));
603 return PJ_EINVAL;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000604 }
605
606 cfg->no_tcp = PJ_TRUE;
607 break;
608
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000609 case OPT_PROXY: /* proxy */
610 if (pjsua_verify_sip_url(pj_optarg) != 0) {
611 PJ_LOG(1,(THIS_FILE,
612 "Error: invalid SIP URL '%s' "
613 "in proxy argument", pj_optarg));
614 return PJ_EINVAL;
615 }
616 cur_acc->proxy[cur_acc->proxy_cnt++] = pj_str(pj_optarg);
617 break;
618
619 case OPT_OUTBOUND_PROXY: /* outbound proxy */
620 if (pjsua_verify_sip_url(pj_optarg) != 0) {
621 PJ_LOG(1,(THIS_FILE,
622 "Error: invalid SIP URL '%s' "
623 "in outbound proxy argument", pj_optarg));
624 return PJ_EINVAL;
625 }
626 cfg->cfg.outbound_proxy[cfg->cfg.outbound_proxy_cnt++] = pj_str(pj_optarg);
627 break;
628
629 case OPT_REGISTRAR: /* registrar */
630 if (pjsua_verify_sip_url(pj_optarg) != 0) {
631 PJ_LOG(1,(THIS_FILE,
632 "Error: invalid SIP URL '%s' in "
633 "registrar argument", pj_optarg));
634 return PJ_EINVAL;
635 }
636 cur_acc->reg_uri = pj_str(pj_optarg);
637 break;
638
639 case OPT_REG_TIMEOUT: /* reg-timeout */
640 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,pj_optarg));
641 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
642 PJ_LOG(1,(THIS_FILE,
643 "Error: invalid value for --reg-timeout "
644 "(expecting 1-3600)"));
645 return PJ_EINVAL;
646 }
647 break;
648
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000649 case OPT_PUBLISH: /* publish */
650 cur_acc->publish_enabled = PJ_TRUE;
651 break;
652
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000653 case OPT_100REL: /** 100rel */
654 cur_acc->require_100rel = PJ_TRUE;
655 cfg->cfg.require_100rel = PJ_TRUE;
656 break;
657
Benny Prijono48ab2b72007-11-08 09:24:30 +0000658 case OPT_USE_IMS: /* Activate IMS settings */
659 cur_acc->auth_pref.initial_auth = PJ_TRUE;
Benny Prijono2a67ea42007-10-25 02:51:33 +0000660 break;
661
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000662 case OPT_ID: /* id */
663 if (pjsua_verify_sip_url(pj_optarg) != 0) {
664 PJ_LOG(1,(THIS_FILE,
665 "Error: invalid SIP URL '%s' "
666 "in local id argument", pj_optarg));
667 return PJ_EINVAL;
668 }
669 cur_acc->id = pj_str(pj_optarg);
670 break;
671
672 case OPT_CONTACT: /* contact */
673 if (pjsua_verify_sip_url(pj_optarg) != 0) {
674 PJ_LOG(1,(THIS_FILE,
675 "Error: invalid SIP URL '%s' "
676 "in contact argument", pj_optarg));
677 return PJ_EINVAL;
678 }
Benny Prijonob4a17c92006-07-10 14:40:21 +0000679 cur_acc->force_contact = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000680 break;
681
Benny Prijonofce28542007-12-09 15:41:10 +0000682 case OPT_AUTO_UPDATE_NAT: /* OPT_AUTO_UPDATE_NAT */
683 cur_acc->auto_update_nat = pj_strtoul(pj_cstr(&tmp, pj_optarg));
684 break;
685
686 case OPT_USE_COMPACT_FORM:
687 /* enable compact form - from Ticket #342 */
688 {
689 extern pj_bool_t pjsip_use_compact_form;
690 extern pj_bool_t pjsip_include_allow_hdr_in_dlg;
691 extern pj_bool_t pjmedia_add_rtpmap_for_static_pt;
692
693 pjsip_use_compact_form = PJ_TRUE;
694 /* do not transmit Allow header */
695 pjsip_include_allow_hdr_in_dlg = PJ_FALSE;
696 /* Do not include rtpmap for static payload types (<96) */
697 pjmedia_add_rtpmap_for_static_pt = PJ_FALSE;
698 }
699 break;
700
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000701 case OPT_NEXT_ACCOUNT: /* Add more account. */
702 cfg->acc_cnt++;
Benny Prijono56315612006-07-18 14:39:40 +0000703 cur_acc = &cfg->acc_cfg[cfg->acc_cnt];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000704 break;
705
706 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000707 cur_acc->cred_info[cur_acc->cred_count].username = pj_str(pj_optarg);
Benny Prijono48ab2b72007-11-08 09:24:30 +0000708 cur_acc->cred_info[cur_acc->cred_count].scheme = pj_str("Digest");
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000709 break;
710
711 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000712 cur_acc->cred_info[cur_acc->cred_count].realm = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000713 break;
714
715 case OPT_PASSWORD: /* authentication password */
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000716 cur_acc->cred_info[cur_acc->cred_count].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
717 cur_acc->cred_info[cur_acc->cred_count].data = pj_str(pj_optarg);
Benny Prijono28f673a2007-10-15 07:04:59 +0000718#if PJSIP_HAS_DIGEST_AKA_AUTH
719 cur_acc->cred_info[cur_acc->cred_count].data_type |= PJSIP_CRED_DATA_EXT_AKA;
720 cur_acc->cred_info[cur_acc->cred_count].ext.aka.k = pj_str(pj_optarg);
721 cur_acc->cred_info[cur_acc->cred_count].ext.aka.cb = &pjsip_auth_create_aka_response;
722#endif
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000723 break;
724
725 case OPT_NEXT_CRED: /* next credential */
726 cur_acc->cred_count++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000727 break;
728
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000729 case OPT_NAMESERVER: /* nameserver */
730 cfg->cfg.nameserver[cfg->cfg.nameserver_count++] = pj_str(pj_optarg);
731 if (cfg->cfg.nameserver_count > PJ_ARRAY_SIZE(cfg->cfg.nameserver)) {
732 PJ_LOG(1,(THIS_FILE, "Error: too many nameservers"));
733 return PJ_ETOOMANY;
734 }
735 break;
736
Benny Prijonoebbf6892007-03-24 17:37:25 +0000737 case OPT_STUN_DOMAIN: /* STUN domain */
738 cfg->cfg.stun_domain = pj_str(pj_optarg);
739 break;
740
Benny Prijonoc97608e2007-03-23 16:34:20 +0000741 case OPT_STUN_SRV: /* STUN server */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000742 cfg->cfg.stun_host = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000743 break;
744
745 case OPT_ADD_BUDDY: /* Add to buddy list. */
746 if (pjsua_verify_sip_url(pj_optarg) != 0) {
747 PJ_LOG(1,(THIS_FILE,
748 "Error: invalid URL '%s' in "
749 "--add-buddy option", pj_optarg));
750 return -1;
751 }
752 if (cfg->buddy_cnt == PJ_ARRAY_SIZE(cfg->buddy_cfg)) {
753 PJ_LOG(1,(THIS_FILE,
754 "Error: too many buddies in buddy list."));
755 return -1;
756 }
757 cfg->buddy_cfg[cfg->buddy_cnt].uri = pj_str(pj_optarg);
758 cfg->buddy_cnt++;
759 break;
760
761 case OPT_AUTO_PLAY:
762 cfg->auto_play = 1;
763 break;
764
Benny Prijono1ebd6142006-10-19 15:48:02 +0000765 case OPT_AUTO_REC:
766 cfg->auto_rec = 1;
767 break;
768
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000769 case OPT_AUTO_LOOP:
770 cfg->auto_loop = 1;
771 break;
772
Benny Prijono7ca96da2006-08-07 12:11:40 +0000773 case OPT_AUTO_CONF:
774 cfg->auto_conf = 1;
775 break;
776
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000777 case OPT_PLAY_FILE:
Benny Prijono32e4f492007-01-24 00:44:26 +0000778 cfg->wav_files[cfg->wav_count++] = pj_str(pj_optarg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000779 break;
780
Benny Prijono4af234b2007-01-24 02:02:09 +0000781 case OPT_PLAY_TONE:
782 {
783 int f1, f2, on, off;
784 int n;
785
786 n = sscanf(pj_optarg, "%d,%d,%d,%d", &f1, &f2, &on, &off);
787 if (n != 4) {
788 puts("Expecting f1,f2,on,off in --play-tone");
789 return -1;
790 }
791
792 cfg->tones[cfg->tone_count].freq1 = (short)f1;
793 cfg->tones[cfg->tone_count].freq2 = (short)f2;
794 cfg->tones[cfg->tone_count].on_msec = (short)on;
795 cfg->tones[cfg->tone_count].off_msec = (short)off;
796 ++cfg->tone_count;
797 }
798 break;
799
Benny Prijono1ebd6142006-10-19 15:48:02 +0000800 case OPT_REC_FILE:
801 cfg->rec_file = pj_str(pj_optarg);
802 break;
803
Benny Prijonoc97608e2007-03-23 16:34:20 +0000804 case OPT_USE_ICE:
805 cfg->media_cfg.enable_ice = PJ_TRUE;
806 break;
807
Benny Prijonod8179652008-01-23 20:39:07 +0000808#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
809 case OPT_USE_SRTP:
810 app_config.cfg.use_srtp = my_atoi(pj_optarg);
811 if (!pj_isdigit(*pj_optarg) || app_config.cfg.use_srtp > 2) {
812 PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option"));
813 return -1;
814 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000815 cur_acc->use_srtp = app_config.cfg.use_srtp;
Benny Prijonod8179652008-01-23 20:39:07 +0000816 break;
Benny Prijonof6508982008-01-25 09:02:33 +0000817 case OPT_SRTP_SECURE:
818 app_config.cfg.srtp_secure_signaling = my_atoi(pj_optarg);
819 if (!pj_isdigit(*pj_optarg) ||
820 app_config.cfg.srtp_secure_signaling > 2)
821 {
822 PJ_LOG(1,(THIS_FILE, "Invalid value for --srtp-secure option"));
823 return -1;
824 }
Benny Prijono3ec13c72008-01-29 11:52:58 +0000825 cur_acc->srtp_secure_signaling = app_config.cfg.srtp_secure_signaling;
Benny Prijonof6508982008-01-25 09:02:33 +0000826 break;
Benny Prijonod8179652008-01-23 20:39:07 +0000827#endif
828
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000829 case OPT_RTP_PORT:
830 cfg->rtp_cfg.port = my_atoi(pj_optarg);
Benny Prijono5583a802007-06-26 12:20:37 +0000831 if (cfg->rtp_cfg.port == 0) {
832 enum { START_PORT=4000 };
833 unsigned range;
834
835 range = (65535-START_PORT-PJSUA_MAX_CALLS*2);
836 cfg->rtp_cfg.port = START_PORT +
837 ((pj_rand() % range) & 0xFFFE);
838 }
839
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000840 if (cfg->rtp_cfg.port < 1 || cfg->rtp_cfg.port > 65535) {
841 PJ_LOG(1,(THIS_FILE,
842 "Error: rtp-port argument value "
843 "(expecting 1-65535"));
844 return -1;
845 }
846 break;
847
Benny Prijonofce28542007-12-09 15:41:10 +0000848 case OPT_DIS_CODEC:
849 cfg->codec_dis[cfg->codec_dis_cnt++] = pj_str(pj_optarg);
850 break;
851
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000852 case OPT_ADD_CODEC:
853 cfg->codec_arg[cfg->codec_cnt++] = pj_str(pj_optarg);
854 break;
855
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000856 /* These options were no longer valid after new pjsua */
857 /*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000858 case OPT_COMPLEXITY:
859 cfg->complexity = my_atoi(pj_optarg);
860 if (cfg->complexity < 0 || cfg->complexity > 10) {
861 PJ_LOG(1,(THIS_FILE,
862 "Error: invalid --complexity (expecting 0-10"));
863 return -1;
864 }
865 break;
Benny Prijono804ff0a2006-09-14 11:17:48 +0000866 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000867
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000868 case OPT_DURATION:
869 cfg->duration = my_atoi(pj_optarg);
870 break;
871
Benny Prijonof521eb02006-08-06 23:07:25 +0000872 case OPT_THREAD_CNT:
873 cfg->cfg.thread_cnt = my_atoi(pj_optarg);
874 if (cfg->cfg.thread_cnt > 128) {
875 PJ_LOG(1,(THIS_FILE,
876 "Error: invalid --thread-cnt option"));
877 return -1;
878 }
879 break;
880
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000881 case OPT_PTIME:
Benny Prijono0a12f002006-07-26 17:05:39 +0000882 cfg->media_cfg.ptime = my_atoi(pj_optarg);
883 if (cfg->media_cfg.ptime < 10 || cfg->media_cfg.ptime > 1000) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000884 PJ_LOG(1,(THIS_FILE,
885 "Error: invalid --ptime option"));
886 return -1;
887 }
888 break;
889
Benny Prijono0a12f002006-07-26 17:05:39 +0000890 case OPT_NO_VAD:
891 cfg->media_cfg.no_vad = PJ_TRUE;
892 break;
Benny Prijonoeef4f8c2006-06-19 12:07:44 +0000893
Benny Prijonod79f25c2006-08-02 19:41:37 +0000894 case OPT_EC_TAIL:
895 cfg->media_cfg.ec_tail_len = my_atoi(pj_optarg);
896 if (cfg->media_cfg.ec_tail_len > 1000) {
897 PJ_LOG(1,(THIS_FILE, "I think the ec-tail length setting "
898 "is too big"));
899 return -1;
900 }
901 break;
902
Benny Prijono0498d902006-06-19 14:49:14 +0000903 case OPT_QUALITY:
904 cfg->media_cfg.quality = my_atoi(pj_optarg);
905 if (cfg->media_cfg.quality < 0 || cfg->media_cfg.quality > 10) {
906 PJ_LOG(1,(THIS_FILE,
907 "Error: invalid --quality (expecting 0-10"));
908 return -1;
909 }
910 break;
911
Benny Prijono00cae612006-07-31 15:19:36 +0000912 case OPT_ILBC_MODE:
913 cfg->media_cfg.ilbc_mode = my_atoi(pj_optarg);
914 if (cfg->media_cfg.ilbc_mode!=20 && cfg->media_cfg.ilbc_mode!=30) {
915 PJ_LOG(1,(THIS_FILE,
916 "Error: invalid --ilbc-mode (expecting 20 or 30"));
917 return -1;
918 }
919 break;
920
921 case OPT_RX_DROP_PCT:
922 cfg->media_cfg.rx_drop_pct = my_atoi(pj_optarg);
923 if (cfg->media_cfg.rx_drop_pct > 100) {
924 PJ_LOG(1,(THIS_FILE,
925 "Error: invalid --rx-drop-pct (expecting <= 100"));
926 return -1;
927 }
928 break;
929
930 case OPT_TX_DROP_PCT:
931 cfg->media_cfg.tx_drop_pct = my_atoi(pj_optarg);
932 if (cfg->media_cfg.tx_drop_pct > 100) {
933 PJ_LOG(1,(THIS_FILE,
934 "Error: invalid --tx-drop-pct (expecting <= 100"));
935 return -1;
936 }
937 break;
938
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000939 case OPT_AUTO_ANSWER:
940 cfg->auto_answer = my_atoi(pj_optarg);
941 if (cfg->auto_answer < 100 || cfg->auto_answer > 699) {
942 PJ_LOG(1,(THIS_FILE,
943 "Error: invalid code in --auto-answer "
944 "(expecting 100-699"));
945 return -1;
946 }
947 break;
948
949 case OPT_MAX_CALLS:
950 cfg->cfg.max_calls = my_atoi(pj_optarg);
Benny Prijono48af79c2006-07-22 12:49:17 +0000951 if (cfg->cfg.max_calls < 1 || cfg->cfg.max_calls > PJSUA_MAX_CALLS) {
Benny Prijono804ff0a2006-09-14 11:17:48 +0000952 PJ_LOG(1,(THIS_FILE,"Error: maximum call setting exceeds "
953 "compile time limit (PJSUA_MAX_CALLS=%d)",
Benny Prijono48af79c2006-07-22 12:49:17 +0000954 PJSUA_MAX_CALLS));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000955 return -1;
956 }
957 break;
958
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000959 case OPT_USE_TLS:
960 cfg->use_tls = PJ_TRUE;
Benny Prijonof3bbc132006-12-25 06:43:59 +0000961#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
962 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
963 return -1;
964#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000965 break;
966
967 case OPT_TLS_CA_FILE:
Benny Prijonof3bbc132006-12-25 06:43:59 +0000968 cfg->udp_cfg.tls_setting.ca_list_file = pj_str(pj_optarg);
969#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
970 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
971 return -1;
972#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000973 break;
974
Benny Prijonof3bbc132006-12-25 06:43:59 +0000975 case OPT_TLS_CERT_FILE:
976 cfg->udp_cfg.tls_setting.cert_file = pj_str(pj_optarg);
977#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
978 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
979 return -1;
980#endif
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000981 break;
982
Benny Prijonof3bbc132006-12-25 06:43:59 +0000983 case OPT_TLS_PRIV_FILE:
984 cfg->udp_cfg.tls_setting.privkey_file = pj_str(pj_optarg);
985 break;
986
Benny Prijono6e0e54b2006-12-08 21:58:31 +0000987 case OPT_TLS_PASSWORD:
Benny Prijonof3bbc132006-12-25 06:43:59 +0000988 cfg->udp_cfg.tls_setting.password = pj_str(pj_optarg);
989#if !defined(PJSIP_HAS_TLS_TRANSPORT) || PJSIP_HAS_TLS_TRANSPORT==0
990 PJ_LOG(1,(THIS_FILE, "Error: TLS support is not configured"));
991 return -1;
992#endif
993 break;
994
995 case OPT_TLS_VERIFY_SERVER:
996 cfg->udp_cfg.tls_setting.verify_server = PJ_TRUE;
997 break;
998
999 case OPT_TLS_VERIFY_CLIENT:
1000 cfg->udp_cfg.tls_setting.verify_client = PJ_TRUE;
1001 break;
1002
1003 case OPT_TLS_NEG_TIMEOUT:
1004 cfg->udp_cfg.tls_setting.timeout.sec = atoi(pj_optarg);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001005 break;
1006
Benny Prijono4e5d5512007-03-06 18:11:30 +00001007 case OPT_CAPTURE_DEV:
1008 cfg->capture_dev = atoi(pj_optarg);
1009 break;
1010
1011 case OPT_PLAYBACK_DEV:
1012 cfg->playback_dev = atoi(pj_optarg);
1013 break;
1014
Benny Prijono22a300a2006-06-14 20:04:55 +00001015 default:
Benny Prijono787b8692006-06-19 12:40:03 +00001016 PJ_LOG(1,(THIS_FILE,
Benny Prijonod6388ac2006-09-09 13:23:09 +00001017 "Argument \"%s\" is not valid. Use --help to see help",
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001018 argv[pj_optind-1]));
Benny Prijono22a300a2006-06-14 20:04:55 +00001019 return -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001020 }
1021 }
1022
1023 if (pj_optind != argc) {
1024 pj_str_t uri_arg;
1025
1026 if (pjsua_verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1027 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1028 return -1;
1029 }
1030 uri_arg = pj_str(argv[pj_optind]);
1031 if (uri_to_call)
1032 *uri_to_call = uri_arg;
1033 pj_optind++;
1034
1035 /* Add URI to call to buddy list if it's not already there */
1036 for (i=0; i<cfg->buddy_cnt; ++i) {
1037 if (pj_stricmp(&cfg->buddy_cfg[i].uri, &uri_arg)==0)
1038 break;
1039 }
1040 if (i == cfg->buddy_cnt && cfg->buddy_cnt < PJSUA_MAX_BUDDIES) {
1041 cfg->buddy_cfg[cfg->buddy_cnt++].uri = uri_arg;
1042 }
1043
1044 } else {
1045 if (uri_to_call)
1046 uri_to_call->slen = 0;
1047 }
1048
1049 if (pj_optind != argc) {
1050 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1051 return PJ_EINVAL;
1052 }
1053
Benny Prijono56315612006-07-18 14:39:40 +00001054 if (cfg->acc_cfg[cfg->acc_cnt].id.slen)
1055 cfg->acc_cnt++;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001056
1057 for (i=0; i<cfg->acc_cnt; ++i) {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001058 pjsua_acc_config *acfg = &cfg->acc_cfg[i];
1059
1060 if (acfg->cred_info[acfg->cred_count].username.slen)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001061 {
Benny Prijono48ab2b72007-11-08 09:24:30 +00001062 acfg->cred_count++;
1063 }
1064
1065 /* When IMS mode is enabled for the account, verify that settings
1066 * are okay.
1067 */
1068 /* For now we check if IMS mode is activated by looking if
1069 * initial_auth is set.
1070 */
1071 if (acfg->auth_pref.initial_auth && acfg->cred_count) {
1072 /* Realm must point to the real domain */
1073 if (*acfg->cred_info[0].realm.ptr=='*') {
1074 PJ_LOG(1,(THIS_FILE,
1075 "Error: cannot use '*' as realm with IMS"));
1076 return PJ_EINVAL;
1077 }
1078
1079 /* Username for authentication must be in a@b format */
1080 if (strchr(acfg->cred_info[0].username.ptr, '@')==0) {
1081 PJ_LOG(1,(THIS_FILE,
1082 "Error: Username for authentication must "
1083 "be in user@domain format with IMS"));
1084 return PJ_EINVAL;
1085 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001086 }
1087 }
1088
1089
1090 return PJ_SUCCESS;
1091}
1092
1093
1094/*
1095 * Save account settings
1096 */
1097static void write_account_settings(int acc_index, pj_str_t *result)
1098{
1099 unsigned i;
1100 char line[128];
1101 pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index];
1102
1103
1104 pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index);
1105 pj_strcat2(result, line);
1106
1107
1108 /* Identity */
1109 if (acc_cfg->id.slen) {
1110 pj_ansi_sprintf(line, "--id %.*s\n",
1111 (int)acc_cfg->id.slen,
1112 acc_cfg->id.ptr);
1113 pj_strcat2(result, line);
1114 }
1115
1116 /* Registrar server */
1117 if (acc_cfg->reg_uri.slen) {
1118 pj_ansi_sprintf(line, "--registrar %.*s\n",
1119 (int)acc_cfg->reg_uri.slen,
1120 acc_cfg->reg_uri.ptr);
1121 pj_strcat2(result, line);
1122
1123 pj_ansi_sprintf(line, "--reg-timeout %u\n",
1124 acc_cfg->reg_timeout);
1125 pj_strcat2(result, line);
1126 }
1127
1128 /* Contact */
Benny Prijonob4a17c92006-07-10 14:40:21 +00001129 if (acc_cfg->force_contact.slen) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001130 pj_ansi_sprintf(line, "--contact %.*s\n",
Benny Prijonob4a17c92006-07-10 14:40:21 +00001131 (int)acc_cfg->force_contact.slen,
1132 acc_cfg->force_contact.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001133 pj_strcat2(result, line);
1134 }
1135
Benny Prijonofce28542007-12-09 15:41:10 +00001136 /* */
1137 //if (acc_cfg->auto_update_nat)
1138 {
1139 pj_ansi_sprintf(line, "--auto-update-nat %i\n",
1140 (int)acc_cfg->auto_update_nat);
1141 pj_strcat2(result, line);
1142 }
1143
Benny Prijonod8179652008-01-23 20:39:07 +00001144#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1145 /* SRTP */
1146 if (acc_cfg->use_srtp) {
1147 pj_ansi_sprintf(line, "--use-srtp %i\n",
1148 (int)acc_cfg->use_srtp);
1149 pj_strcat2(result, line);
1150 }
1151#endif
1152
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001153 /* Proxy */
1154 for (i=0; i<acc_cfg->proxy_cnt; ++i) {
1155 pj_ansi_sprintf(line, "--proxy %.*s\n",
1156 (int)acc_cfg->proxy[i].slen,
1157 acc_cfg->proxy[i].ptr);
1158 pj_strcat2(result, line);
1159 }
1160
1161 /* Credentials */
1162 for (i=0; i<acc_cfg->cred_count; ++i) {
1163 if (acc_cfg->cred_info[i].realm.slen) {
1164 pj_ansi_sprintf(line, "--realm %.*s\n",
1165 (int)acc_cfg->cred_info[i].realm.slen,
1166 acc_cfg->cred_info[i].realm.ptr);
1167 pj_strcat2(result, line);
1168 }
1169
1170 if (acc_cfg->cred_info[i].username.slen) {
1171 pj_ansi_sprintf(line, "--username %.*s\n",
1172 (int)acc_cfg->cred_info[i].username.slen,
1173 acc_cfg->cred_info[i].username.ptr);
1174 pj_strcat2(result, line);
1175 }
1176
1177 if (acc_cfg->cred_info[i].data.slen) {
1178 pj_ansi_sprintf(line, "--password %.*s\n",
1179 (int)acc_cfg->cred_info[i].data.slen,
1180 acc_cfg->cred_info[i].data.ptr);
1181 pj_strcat2(result, line);
1182 }
Benny Prijonoeef4f8c2006-06-19 12:07:44 +00001183
1184 if (i != acc_cfg->cred_count - 1)
1185 pj_strcat2(result, "--next-cred\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001186 }
1187
1188}
1189
1190
1191/*
1192 * Write settings.
1193 */
1194static int write_settings(const struct app_config *config,
1195 char *buf, pj_size_t max)
1196{
1197 unsigned acc_index;
1198 unsigned i;
1199 pj_str_t cfg;
1200 char line[128];
Benny Prijonofce28542007-12-09 15:41:10 +00001201 extern pj_bool_t pjsip_use_compact_form;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001202
1203 PJ_UNUSED_ARG(max);
1204
1205 cfg.ptr = buf;
1206 cfg.slen = 0;
1207
1208 /* Logging. */
1209 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
1210 pj_ansi_sprintf(line, "--log-level %d\n",
1211 config->log_cfg.level);
1212 pj_strcat2(&cfg, line);
1213
1214 pj_ansi_sprintf(line, "--app-log-level %d\n",
1215 config->log_cfg.console_level);
1216 pj_strcat2(&cfg, line);
1217
1218 if (config->log_cfg.log_filename.slen) {
1219 pj_ansi_sprintf(line, "--log-file %.*s\n",
1220 (int)config->log_cfg.log_filename.slen,
1221 config->log_cfg.log_filename.ptr);
1222 pj_strcat2(&cfg, line);
1223 }
1224
1225
1226 /* Save account settings. */
1227 for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) {
1228
1229 write_account_settings(acc_index, &cfg);
1230
1231 if (acc_index < config->acc_cnt-1)
1232 pj_strcat2(&cfg, "--next-account\n");
1233 }
1234
1235
1236 pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n");
1237
1238 /* Outbound proxy */
1239 for (i=0; i<config->cfg.outbound_proxy_cnt; ++i) {
1240 pj_ansi_sprintf(line, "--outbound %.*s\n",
1241 (int)config->cfg.outbound_proxy[i].slen,
1242 config->cfg.outbound_proxy[i].ptr);
1243 pj_strcat2(&cfg, line);
1244 }
1245
1246
1247 /* UDP Transport. */
1248 pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port);
1249 pj_strcat2(&cfg, line);
1250
Benny Prijono0a5cad82006-09-26 13:21:02 +00001251 /* IP address, if any. */
1252 if (config->udp_cfg.public_addr.slen) {
1253 pj_ansi_sprintf(line, "--ip-addr %.*s\n",
1254 (int)config->udp_cfg.public_addr.slen,
1255 config->udp_cfg.public_addr.ptr);
1256 pj_strcat2(&cfg, line);
1257 }
1258
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001259 /* No TCP ? */
1260 if (config->no_tcp) {
1261 pj_strcat2(&cfg, "--no-tcp\n");
1262 }
1263
1264 /* No UDP ? */
1265 if (config->no_udp) {
1266 pj_strcat2(&cfg, "--no-udp\n");
1267 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001268
1269 /* STUN */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001270 if (config->cfg.stun_domain.slen) {
1271 pj_ansi_sprintf(line, "--stun-domain %.*s\n",
1272 (int)config->cfg.stun_domain.slen,
1273 config->cfg.stun_domain.ptr);
1274 pj_strcat2(&cfg, line);
1275 }
1276 if (config->cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001277 pj_ansi_sprintf(line, "--stun-srv %.*s\n",
Benny Prijonoebbf6892007-03-24 17:37:25 +00001278 (int)config->cfg.stun_host.slen,
1279 config->cfg.stun_host.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001280 pj_strcat2(&cfg, line);
1281 }
1282
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001283 /* TLS */
1284 if (config->use_tls)
1285 pj_strcat2(&cfg, "--use-tls\n");
Benny Prijonof3bbc132006-12-25 06:43:59 +00001286 if (config->udp_cfg.tls_setting.ca_list_file.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001287 pj_ansi_sprintf(line, "--tls-ca-file %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001288 (int)config->udp_cfg.tls_setting.ca_list_file.slen,
1289 config->udp_cfg.tls_setting.ca_list_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001290 pj_strcat2(&cfg, line);
1291 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001292 if (config->udp_cfg.tls_setting.cert_file.slen) {
1293 pj_ansi_sprintf(line, "--tls-cert-file %.*s\n",
1294 (int)config->udp_cfg.tls_setting.cert_file.slen,
1295 config->udp_cfg.tls_setting.cert_file.ptr);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001296 pj_strcat2(&cfg, line);
1297 }
Benny Prijonof3bbc132006-12-25 06:43:59 +00001298 if (config->udp_cfg.tls_setting.privkey_file.slen) {
1299 pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n",
1300 (int)config->udp_cfg.tls_setting.privkey_file.slen,
1301 config->udp_cfg.tls_setting.privkey_file.ptr);
1302 pj_strcat2(&cfg, line);
1303 }
1304
1305 if (config->udp_cfg.tls_setting.password.slen) {
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001306 pj_ansi_sprintf(line, "--tls-password %.*s\n",
Benny Prijonof3bbc132006-12-25 06:43:59 +00001307 (int)config->udp_cfg.tls_setting.password.slen,
1308 config->udp_cfg.tls_setting.password.ptr);
1309 pj_strcat2(&cfg, line);
1310 }
1311
1312 if (config->udp_cfg.tls_setting.verify_server)
1313 pj_strcat2(&cfg, "--tls-verify-server\n");
1314
1315 if (config->udp_cfg.tls_setting.verify_client)
1316 pj_strcat2(&cfg, "--tls-verify-client\n");
1317
1318 if (config->udp_cfg.tls_setting.timeout.sec) {
1319 pj_ansi_sprintf(line, "--tls-neg-timeout %d\n",
Benny Prijonoe960bb52007-01-21 17:53:39 +00001320 (int)config->udp_cfg.tls_setting.timeout.sec);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001321 pj_strcat2(&cfg, line);
1322 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001323
1324 pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n");
1325
Benny Prijonof6508982008-01-25 09:02:33 +00001326 /* SRTP */
1327 if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) {
1328 pj_ansi_sprintf(line, "--use-srtp %d\n",
1329 app_config.cfg.use_srtp);
1330 pj_strcat2(&cfg, line);
1331 }
1332 if (app_config.cfg.srtp_secure_signaling !=
1333 PJSUA_DEFAULT_SRTP_SECURE_SIGNALING)
1334 {
1335 pj_ansi_sprintf(line, "--srtp-secure %d\n",
1336 app_config.cfg.srtp_secure_signaling);
1337 pj_strcat2(&cfg, line);
1338 }
1339
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001340
1341 /* Media */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001342 if (config->media_cfg.enable_ice)
1343 pj_strcat2(&cfg, "--use-ice\n");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001344 if (config->null_audio)
1345 pj_strcat2(&cfg, "--null-audio\n");
1346 if (config->auto_play)
1347 pj_strcat2(&cfg, "--auto-play\n");
1348 if (config->auto_loop)
1349 pj_strcat2(&cfg, "--auto-loop\n");
Benny Prijono7ca96da2006-08-07 12:11:40 +00001350 if (config->auto_conf)
1351 pj_strcat2(&cfg, "--auto-conf\n");
Benny Prijono32e4f492007-01-24 00:44:26 +00001352 for (i=0; i<config->wav_count; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001353 pj_ansi_sprintf(line, "--play-file %s\n",
Benny Prijono32e4f492007-01-24 00:44:26 +00001354 config->wav_files[i].ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001355 pj_strcat2(&cfg, line);
1356 }
Benny Prijono4af234b2007-01-24 02:02:09 +00001357 for (i=0; i<config->tone_count; ++i) {
1358 pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n",
1359 config->tones[i].freq1, config->tones[i].freq2,
1360 config->tones[i].on_msec, config->tones[i].off_msec);
1361 pj_strcat2(&cfg, line);
1362 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001363 if (config->rec_file.slen) {
1364 pj_ansi_sprintf(line, "--rec-file %s\n",
1365 config->rec_file.ptr);
1366 pj_strcat2(&cfg, line);
1367 }
1368 if (config->auto_rec)
1369 pj_strcat2(&cfg, "--auto-rec\n");
Benny Prijono4e5d5512007-03-06 18:11:30 +00001370 if (config->capture_dev != PJSUA_INVALID_ID) {
1371 pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev);
1372 pj_strcat2(&cfg, line);
1373 }
1374 if (config->playback_dev != PJSUA_INVALID_ID) {
1375 pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev);
1376 pj_strcat2(&cfg, line);
1377 }
Benny Prijono1ebd6142006-10-19 15:48:02 +00001378
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001379 /* Media clock rate. */
Benny Prijono70972992006-08-05 11:13:58 +00001380 if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001381 pj_ansi_sprintf(line, "--clock-rate %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001382 config->media_cfg.clock_rate);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001383 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001384 } else {
1385 pj_ansi_sprintf(line, "#using default --clock-rate %d\n",
1386 config->media_cfg.clock_rate);
1387 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001388 }
Benny Prijono70972992006-08-05 11:13:58 +00001389
1390 /* quality */
1391 if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001392 pj_ansi_sprintf(line, "--quality %d\n",
Benny Prijono0498d902006-06-19 14:49:14 +00001393 config->media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001394 pj_strcat2(&cfg, line);
Benny Prijono70972992006-08-05 11:13:58 +00001395 } else {
1396 pj_ansi_sprintf(line, "#using default --quality %d\n",
1397 config->media_cfg.quality);
1398 pj_strcat2(&cfg, line);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001399 }
Benny Prijono0498d902006-06-19 14:49:14 +00001400
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001401
1402 /* ptime */
Benny Prijono2adfe292007-05-11 10:36:08 +00001403 if (config->media_cfg.ptime) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001404 pj_ansi_sprintf(line, "--ptime %d\n",
Benny Prijono2adfe292007-05-11 10:36:08 +00001405 config->media_cfg.ptime);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001406 pj_strcat2(&cfg, line);
1407 }
1408
Benny Prijono70972992006-08-05 11:13:58 +00001409 /* no-vad */
1410 if (config->media_cfg.no_vad) {
1411 pj_strcat2(&cfg, "--no-vad\n");
1412 }
1413
1414 /* ec-tail */
1415 if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) {
1416 pj_ansi_sprintf(line, "--ec-tail %d\n",
1417 config->media_cfg.ec_tail_len);
1418 pj_strcat2(&cfg, line);
1419 } else {
1420 pj_ansi_sprintf(line, "#using default --ec-tail %d\n",
1421 config->media_cfg.ec_tail_len);
1422 pj_strcat2(&cfg, line);
1423 }
1424
1425
1426 /* ilbc-mode */
1427 if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) {
1428 pj_ansi_sprintf(line, "--ilbc-mode %d\n",
1429 config->media_cfg.ilbc_mode);
1430 pj_strcat2(&cfg, line);
1431 } else {
1432 pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n",
1433 config->media_cfg.ilbc_mode);
1434 pj_strcat2(&cfg, line);
1435 }
1436
1437 /* RTP drop */
1438 if (config->media_cfg.tx_drop_pct) {
1439 pj_ansi_sprintf(line, "--tx-drop-pct %d\n",
1440 config->media_cfg.tx_drop_pct);
1441 pj_strcat2(&cfg, line);
1442
1443 }
1444 if (config->media_cfg.rx_drop_pct) {
1445 pj_ansi_sprintf(line, "--rx-drop-pct %d\n",
1446 config->media_cfg.rx_drop_pct);
1447 pj_strcat2(&cfg, line);
1448
1449 }
1450
1451
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001452 /* Start RTP port. */
1453 pj_ansi_sprintf(line, "--rtp-port %d\n",
1454 config->rtp_cfg.port);
1455 pj_strcat2(&cfg, line);
1456
1457 /* Add codec. */
1458 for (i=0; i<config->codec_cnt; ++i) {
1459 pj_ansi_sprintf(line, "--add-codec %s\n",
1460 config->codec_arg[i].ptr);
1461 pj_strcat2(&cfg, line);
1462 }
Benny Prijonofce28542007-12-09 15:41:10 +00001463 /* Disable codec */
1464 for (i=0; i<config->codec_dis_cnt; ++i) {
1465 pj_ansi_sprintf(line, "--dis-codec %s\n",
1466 config->codec_dis[i].ptr);
1467 pj_strcat2(&cfg, line);
1468 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001469
1470 pj_strcat2(&cfg, "\n#\n# User agent:\n#\n");
1471
1472 /* Auto-answer. */
1473 if (config->auto_answer != 0) {
1474 pj_ansi_sprintf(line, "--auto-answer %d\n",
1475 config->auto_answer);
1476 pj_strcat2(&cfg, line);
1477 }
1478
1479 /* Max calls. */
1480 pj_ansi_sprintf(line, "--max-calls %d\n",
1481 config->cfg.max_calls);
1482 pj_strcat2(&cfg, line);
1483
1484 /* Uas-duration. */
Benny Prijono804ff0a2006-09-14 11:17:48 +00001485 if (config->duration != NO_LIMIT) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001486 pj_ansi_sprintf(line, "--duration %d\n",
1487 config->duration);
1488 pj_strcat2(&cfg, line);
1489 }
1490
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001491 /* norefersub ? */
1492 if (config->no_refersub) {
1493 pj_strcat2(&cfg, "--norefersub\n");
1494 }
1495
Benny Prijonofce28542007-12-09 15:41:10 +00001496 if (pjsip_use_compact_form)
1497 {
1498 pj_strcat2(&cfg, "--use-compact-form\n");
1499 }
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001500
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001501 pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n");
1502
1503 /* Add buddies. */
1504 for (i=0; i<config->buddy_cnt; ++i) {
1505 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
1506 (int)config->buddy_cfg[i].uri.slen,
1507 config->buddy_cfg[i].uri.ptr);
1508 pj_strcat2(&cfg, line);
1509 }
1510
1511
1512 *(cfg.ptr + cfg.slen) = '\0';
1513 return cfg.slen;
1514}
1515
1516
1517/*
1518 * Dump application states.
1519 */
1520static void app_dump(pj_bool_t detail)
1521{
Benny Prijonoda9785b2007-04-02 20:43:06 +00001522 pjsua_dump(detail);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001523}
1524
1525
1526/*****************************************************************************
1527 * Console application
1528 */
1529
1530/*
1531 * Find next call when current call is disconnected or when user
1532 * press ']'
1533 */
1534static pj_bool_t find_next_call(void)
1535{
1536 int i, max;
1537
1538 max = pjsua_call_get_max_count();
1539 for (i=current_call+1; i<max; ++i) {
1540 if (pjsua_call_is_active(i)) {
1541 current_call = i;
1542 return PJ_TRUE;
1543 }
1544 }
1545
1546 for (i=0; i<current_call; ++i) {
1547 if (pjsua_call_is_active(i)) {
1548 current_call = i;
1549 return PJ_TRUE;
1550 }
1551 }
1552
1553 current_call = PJSUA_INVALID_ID;
1554 return PJ_FALSE;
1555}
1556
1557
1558/*
1559 * Find previous call when user press '['
1560 */
1561static pj_bool_t find_prev_call(void)
1562{
1563 int i, max;
1564
1565 max = pjsua_call_get_max_count();
1566 for (i=current_call-1; i>=0; --i) {
1567 if (pjsua_call_is_active(i)) {
1568 current_call = i;
1569 return PJ_TRUE;
1570 }
1571 }
1572
1573 for (i=max-1; i>current_call; --i) {
1574 if (pjsua_call_is_active(i)) {
1575 current_call = i;
1576 return PJ_TRUE;
1577 }
1578 }
1579
1580 current_call = PJSUA_INVALID_ID;
1581 return PJ_FALSE;
1582}
1583
1584
Benny Prijono804ff0a2006-09-14 11:17:48 +00001585/* Callback from timer when the maximum call duration has been
1586 * exceeded.
1587 */
1588static void call_timeout_callback(pj_timer_heap_t *timer_heap,
1589 struct pj_timer_entry *entry)
1590{
1591 pjsua_call_id call_id = entry->id;
1592 pjsua_msg_data msg_data;
1593 pjsip_generic_string_hdr warn;
1594 pj_str_t hname = pj_str("Warning");
1595 pj_str_t hvalue = pj_str("399 pjsua \"Call duration exceeded\"");
1596
1597 PJ_UNUSED_ARG(timer_heap);
1598
Benny Prijono148c9dd2006-09-19 13:37:53 +00001599 if (call_id == PJSUA_INVALID_ID) {
1600 PJ_LOG(1,(THIS_FILE, "Invalid call ID in timer callback"));
1601 return;
1602 }
1603
Benny Prijono804ff0a2006-09-14 11:17:48 +00001604 /* Add warning header */
1605 pjsua_msg_data_init(&msg_data);
1606 pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
1607 pj_list_push_back(&msg_data.hdr_list, &warn);
1608
1609 /* Call duration has been exceeded; disconnect the call */
1610 PJ_LOG(3,(THIS_FILE, "Duration (%d seconds) has been exceeded "
1611 "for call %d, disconnecting the call",
1612 app_config.duration, call_id));
1613 entry->id = PJSUA_INVALID_ID;
1614 pjsua_call_hangup(call_id, 200, NULL, &msg_data);
1615}
1616
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001617
1618/*
1619 * Handler when invite state has changed.
1620 */
1621static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
1622{
1623 pjsua_call_info call_info;
1624
1625 PJ_UNUSED_ARG(e);
1626
1627 pjsua_call_get_info(call_id, &call_info);
1628
1629 if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) {
1630
Benny Prijono804ff0a2006-09-14 11:17:48 +00001631 /* Cancel duration timer, if any */
1632 if (app_config.call_data[call_id].timer.id != PJSUA_INVALID_ID) {
1633 struct call_data *cd = &app_config.call_data[call_id];
1634 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1635
1636 cd->timer.id = PJSUA_INVALID_ID;
1637 pjsip_endpt_cancel_timer(endpt, &cd->timer);
1638 }
1639
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001640 PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]",
1641 call_id,
1642 call_info.last_status,
1643 call_info.last_status_text.ptr));
1644
1645 if (call_id == current_call) {
1646 find_next_call();
1647 }
1648
Benny Prijono4be63b52006-11-25 14:50:25 +00001649 /* Dump media state upon disconnected */
1650 if (1) {
1651 char buf[1024];
1652 pjsua_call_dump(call_id, PJ_TRUE, buf,
1653 sizeof(buf), " ");
1654 PJ_LOG(5,(THIS_FILE,
1655 "Call %d disconnected, dumping media stats\n%s",
1656 call_id, buf));
1657 }
1658
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001659 } else {
1660
Benny Prijono804ff0a2006-09-14 11:17:48 +00001661 if (app_config.duration!=NO_LIMIT &&
1662 call_info.state == PJSIP_INV_STATE_CONFIRMED)
1663 {
1664 /* Schedule timer to hangup call after the specified duration */
1665 struct call_data *cd = &app_config.call_data[call_id];
1666 pjsip_endpoint *endpt = pjsua_get_pjsip_endpt();
1667 pj_time_val delay;
1668
1669 cd->timer.id = call_id;
1670 delay.sec = app_config.duration;
1671 delay.msec = 0;
1672 pjsip_endpt_schedule_timer(endpt, &cd->timer, &delay);
1673 }
1674
Benny Prijono4be63b52006-11-25 14:50:25 +00001675 if (call_info.state == PJSIP_INV_STATE_EARLY) {
1676 int code;
1677 pj_str_t reason;
1678 pjsip_msg *msg;
1679
1680 /* This can only occur because of TX or RX message */
1681 pj_assert(e->type == PJSIP_EVENT_TSX_STATE);
1682
1683 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1684 msg = e->body.tsx_state.src.rdata->msg_info.msg;
1685 } else {
1686 msg = e->body.tsx_state.src.tdata->msg;
1687 }
1688
1689 code = msg->line.status.code;
1690 reason = msg->line.status.reason;
1691
1692 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s (%d %.*s)",
1693 call_id, call_info.state_text.ptr,
1694 code, (int)reason.slen, reason.ptr));
1695 } else {
1696 PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s",
1697 call_id,
1698 call_info.state_text.ptr));
1699 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001700
1701 if (current_call==PJSUA_INVALID_ID)
1702 current_call = call_id;
1703
1704 }
1705}
1706
1707
1708/**
1709 * Handler when there is incoming call.
1710 */
1711static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
1712 pjsip_rx_data *rdata)
1713{
1714 pjsua_call_info call_info;
1715
1716 PJ_UNUSED_ARG(acc_id);
1717 PJ_UNUSED_ARG(rdata);
1718
1719 pjsua_call_get_info(call_id, &call_info);
1720
1721 if (app_config.auto_answer > 0) {
1722 pjsua_call_answer(call_id, app_config.auto_answer, NULL, NULL);
1723 }
1724
1725 if (app_config.auto_answer < 200) {
1726 PJ_LOG(3,(THIS_FILE,
1727 "Incoming call for account %d!\n"
1728 "From: %s\n"
1729 "To: %s\n"
1730 "Press a to answer or h to reject call",
1731 acc_id,
1732 call_info.remote_info.ptr,
1733 call_info.local_info.ptr));
1734 }
1735}
1736
1737
1738/*
Benny Prijonofeb69f42007-10-05 09:12:26 +00001739 * Handler when a transaction within a call has changed state.
1740 */
1741static void on_call_tsx_state(pjsua_call_id call_id,
1742 pjsip_transaction *tsx,
1743 pjsip_event *e)
1744{
1745 const pjsip_method info_method =
1746 {
1747 PJSIP_OTHER_METHOD,
1748 { "INFO", 4 }
1749 };
1750
1751 if (pjsip_method_cmp(&tsx->method, &info_method)==0) {
1752 /*
1753 * Handle INFO method.
1754 */
1755 if (tsx->role == PJSIP_ROLE_UAC &&
1756 (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
Benny Prijonob071a782007-10-10 13:12:37 +00001757 (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
1758 e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED)))
Benny Prijonofeb69f42007-10-05 09:12:26 +00001759 {
1760 /* Status of outgoing INFO request */
1761 if (tsx->status_code >= 200 && tsx->status_code < 300) {
1762 PJ_LOG(4,(THIS_FILE,
1763 "Call %d: DTMF sent successfully with INFO",
1764 call_id));
1765 } else if (tsx->status_code >= 300) {
1766 PJ_LOG(4,(THIS_FILE,
1767 "Call %d: Failed to send DTMF with INFO: %d/%.*s",
1768 call_id,
1769 tsx->status_code,
1770 (int)tsx->status_text.slen,
1771 tsx->status_text.ptr));
1772 }
1773 } else if (tsx->role == PJSIP_ROLE_UAS &&
1774 tsx->state == PJSIP_TSX_STATE_TRYING)
1775 {
1776 /* Answer incoming INFO with 200/OK */
1777 pjsip_rx_data *rdata;
1778 pjsip_tx_data *tdata;
1779 pj_status_t status;
1780
1781 rdata = e->body.tsx_state.src.rdata;
1782
1783 if (rdata->msg_info.msg->body) {
1784 status = pjsip_endpt_create_response(tsx->endpt, rdata,
1785 200, NULL, &tdata);
1786 if (status == PJ_SUCCESS)
1787 status = pjsip_tsx_send_msg(tsx, tdata);
1788
1789 PJ_LOG(3,(THIS_FILE, "Call %d: incoming INFO:\n%.*s",
1790 call_id,
1791 (int)rdata->msg_info.msg->body->len,
1792 rdata->msg_info.msg->body->data));
1793 } else {
1794 status = pjsip_endpt_create_response(tsx->endpt, rdata,
1795 400, NULL, &tdata);
1796 if (status == PJ_SUCCESS)
1797 status = pjsip_tsx_send_msg(tsx, tdata);
1798 }
1799 }
1800 }
1801}
1802
1803
1804/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001805 * Callback on media state changed event.
1806 * The action may connect the call to sound device, to file, or
1807 * to loop the call.
1808 */
1809static void on_call_media_state(pjsua_call_id call_id)
1810{
1811 pjsua_call_info call_info;
1812
1813 pjsua_call_get_info(call_id, &call_info);
1814
1815 if (call_info.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
1816 pj_bool_t connect_sound = PJ_TRUE;
1817
1818 /* Loopback sound, if desired */
1819 if (app_config.auto_loop) {
1820 pjsua_conf_connect(call_info.conf_slot, call_info.conf_slot);
1821 connect_sound = PJ_FALSE;
Benny Prijono1ebd6142006-10-19 15:48:02 +00001822
1823 /* Automatically record conversation, if desired */
1824 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1825 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
1826 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001827 }
1828
1829 /* Stream a file, if desired */
1830 if (app_config.auto_play && app_config.wav_port != PJSUA_INVALID_ID) {
1831 pjsua_conf_connect(app_config.wav_port, call_info.conf_slot);
1832 connect_sound = PJ_FALSE;
1833 }
1834
Benny Prijono7ca96da2006-08-07 12:11:40 +00001835 /* Put call in conference with other calls, if desired */
1836 if (app_config.auto_conf) {
1837 pjsua_call_id call_ids[PJSUA_MAX_CALLS];
Benny Prijono10861bc2006-08-07 13:22:43 +00001838 unsigned call_cnt=PJ_ARRAY_SIZE(call_ids);
Benny Prijono7ca96da2006-08-07 12:11:40 +00001839 unsigned i;
1840
1841 /* Get all calls, and establish media connection between
1842 * this call and other calls.
1843 */
1844 pjsua_enum_calls(call_ids, &call_cnt);
Benny Prijono10861bc2006-08-07 13:22:43 +00001845
Benny Prijono7ca96da2006-08-07 12:11:40 +00001846 for (i=0; i<call_cnt; ++i) {
1847 if (call_ids[i] == call_id)
1848 continue;
1849
1850 if (!pjsua_call_has_media(call_ids[i]))
1851 continue;
1852
1853 pjsua_conf_connect(call_info.conf_slot,
1854 pjsua_call_get_conf_port(call_ids[i]));
1855 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
1856 call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001857
1858 /* Automatically record conversation, if desired */
1859 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1860 pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]),
1861 app_config.rec_port);
1862 }
1863
Benny Prijono7ca96da2006-08-07 12:11:40 +00001864 }
1865
1866 /* Also connect call to local sound device */
1867 connect_sound = PJ_TRUE;
1868 }
1869
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001870 /* Otherwise connect to sound device */
1871 if (connect_sound) {
1872 pjsua_conf_connect(call_info.conf_slot, 0);
1873 pjsua_conf_connect(0, call_info.conf_slot);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001874
1875 /* Automatically record conversation, if desired */
1876 if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) {
1877 pjsua_conf_connect(call_info.conf_slot, app_config.rec_port);
1878 pjsua_conf_connect(0, app_config.rec_port);
1879 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001880 }
1881
1882 PJ_LOG(3,(THIS_FILE, "Media for call %d is active", call_id));
1883
1884 } else if (call_info.media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
1885 PJ_LOG(3,(THIS_FILE, "Media for call %d is suspended (hold) by local",
1886 call_id));
1887 } else if (call_info.media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
1888 PJ_LOG(3,(THIS_FILE,
1889 "Media for call %d is suspended (hold) by remote",
1890 call_id));
Benny Prijono096c56c2007-09-15 08:30:16 +00001891 } else if (call_info.media_status == PJSUA_CALL_MEDIA_ERROR) {
1892 pj_str_t reason = pj_str("ICE negotiation failed");
1893
1894 PJ_LOG(1,(THIS_FILE,
1895 "Media has reported error, disconnecting call"));
1896
1897 pjsua_call_hangup(call_id, 500, &reason, NULL);
1898
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001899 } else {
1900 PJ_LOG(3,(THIS_FILE,
1901 "Media for call %d is inactive",
1902 call_id));
1903 }
1904}
1905
Benny Prijono0875ae82006-12-26 00:11:48 +00001906/*
1907 * DTMF callback.
1908 */
1909static void call_on_dtmf_callback(pjsua_call_id call_id, int dtmf)
1910{
1911 PJ_LOG(3,(THIS_FILE, "Incoming DTMF on call %d: %c", call_id, dtmf));
1912}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001913
1914/*
1915 * Handler registration status has changed.
1916 */
1917static void on_reg_state(pjsua_acc_id acc_id)
1918{
1919 PJ_UNUSED_ARG(acc_id);
1920
1921 // Log already written.
1922}
1923
1924
1925/*
1926 * Handler on buddy state changed.
1927 */
1928static void on_buddy_state(pjsua_buddy_id buddy_id)
1929{
1930 pjsua_buddy_info info;
1931 pjsua_buddy_get_info(buddy_id, &info);
1932
1933 PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s",
1934 (int)info.uri.slen,
1935 info.uri.ptr,
1936 (int)info.status_text.slen,
1937 info.status_text.ptr));
1938}
1939
1940
1941/**
1942 * Incoming IM message (i.e. MESSAGE request)!
1943 */
1944static void on_pager(pjsua_call_id call_id, const pj_str_t *from,
1945 const pj_str_t *to, const pj_str_t *contact,
1946 const pj_str_t *mime_type, const pj_str_t *text)
1947{
1948 /* Note: call index may be -1 */
1949 PJ_UNUSED_ARG(call_id);
1950 PJ_UNUSED_ARG(to);
1951 PJ_UNUSED_ARG(contact);
1952 PJ_UNUSED_ARG(mime_type);
1953
Benny Prijonof4b538d2007-05-14 16:45:20 +00001954 PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s (%.*s)",
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001955 (int)from->slen, from->ptr,
Benny Prijonof4b538d2007-05-14 16:45:20 +00001956 (int)text->slen, text->ptr,
1957 (int)mime_type->slen, mime_type->ptr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001958}
1959
1960
1961/**
1962 * Received typing indication
1963 */
1964static void on_typing(pjsua_call_id call_id, const pj_str_t *from,
1965 const pj_str_t *to, const pj_str_t *contact,
1966 pj_bool_t is_typing)
1967{
1968 PJ_UNUSED_ARG(call_id);
1969 PJ_UNUSED_ARG(to);
1970 PJ_UNUSED_ARG(contact);
1971
1972 PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s",
1973 (int)from->slen, from->ptr,
1974 (is_typing?"is typing..":"has stopped typing")));
1975}
1976
1977
Benny Prijono4ddad2c2006-10-18 17:16:34 +00001978/**
1979 * Call transfer request status.
1980 */
1981static void on_call_transfer_status(pjsua_call_id call_id,
1982 int status_code,
1983 const pj_str_t *status_text,
1984 pj_bool_t final,
1985 pj_bool_t *p_cont)
1986{
1987 PJ_LOG(3,(THIS_FILE, "Call %d: transfer status=%d (%.*s) %s",
1988 call_id, status_code,
1989 (int)status_text->slen, status_text->ptr,
1990 (final ? "[final]" : "")));
1991
1992 if (status_code/100 == 2) {
1993 PJ_LOG(3,(THIS_FILE,
1994 "Call %d: call transfered successfully, disconnecting call",
1995 call_id));
1996 pjsua_call_hangup(call_id, PJSIP_SC_GONE, NULL, NULL);
1997 *p_cont = PJ_FALSE;
1998 }
1999}
2000
2001
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002002/*
Benny Prijonof7b1c392006-11-11 16:46:34 +00002003 * Notification that call is being replaced.
2004 */
2005static void on_call_replaced(pjsua_call_id old_call_id,
2006 pjsua_call_id new_call_id)
2007{
2008 pjsua_call_info old_ci, new_ci;
2009
2010 pjsua_call_get_info(old_call_id, &old_ci);
2011 pjsua_call_get_info(new_call_id, &new_ci);
2012
2013 PJ_LOG(3,(THIS_FILE, "Call %d with %.*s is being replaced by "
2014 "call %d with %.*s",
2015 old_call_id,
2016 (int)old_ci.remote_info.slen, old_ci.remote_info.ptr,
2017 new_call_id,
2018 (int)new_ci.remote_info.slen, new_ci.remote_info.ptr));
2019}
2020
2021
2022/*
Benny Prijono6ba8c542007-10-16 01:34:14 +00002023 * NAT type detection callback.
2024 */
2025static void on_nat_detect(const pj_stun_nat_detect_result *res)
2026{
2027 if (res->status != PJ_SUCCESS) {
2028 pjsua_perror(THIS_FILE, "NAT detection failed", res->status);
2029 } else {
2030 PJ_LOG(3, (THIS_FILE, "NAT detected as %s", res->nat_type_name));
2031 }
2032}
2033
2034
2035/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002036 * Print buddy list.
2037 */
2038static void print_buddy_list(void)
2039{
2040 pjsua_buddy_id ids[64];
2041 int i;
2042 unsigned count = PJ_ARRAY_SIZE(ids);
2043
2044 puts("Buddy list:");
2045
2046 pjsua_enum_buddies(ids, &count);
2047
2048 if (count == 0)
2049 puts(" -none-");
2050 else {
2051 for (i=0; i<(int)count; ++i) {
2052 pjsua_buddy_info info;
2053
2054 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
2055 continue;
2056
Benny Prijono4461c7d2007-08-25 13:36:15 +00002057 printf(" [%2d] <%.*s> %.*s\n",
2058 ids[i]+1,
2059 (int)info.status_text.slen,
2060 info.status_text.ptr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002061 (int)info.uri.slen,
2062 info.uri.ptr);
2063 }
2064 }
2065 puts("");
2066}
2067
2068
2069/*
2070 * Print account status.
2071 */
2072static void print_acc_status(int acc_id)
2073{
2074 char buf[80];
2075 pjsua_acc_info info;
2076
2077 pjsua_acc_get_info(acc_id, &info);
2078
2079 if (!info.has_registration) {
2080 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
2081 (int)info.status_text.slen,
2082 info.status_text.ptr);
2083
2084 } else {
2085 pj_ansi_snprintf(buf, sizeof(buf),
2086 "%d/%.*s (expires=%d)",
2087 info.status,
2088 (int)info.status_text.slen,
2089 info.status_text.ptr,
2090 info.expires);
2091
2092 }
2093
2094 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
2095 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
Benny Prijono4461c7d2007-08-25 13:36:15 +00002096 printf(" Online status: %.*s\n",
2097 (int)info.online_status_text.slen,
2098 info.online_status_text.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002099}
2100
2101
2102/*
2103 * Show a bit of help.
2104 */
2105static void keystroke_help(void)
2106{
2107 pjsua_acc_id acc_ids[16];
2108 unsigned count = PJ_ARRAY_SIZE(acc_ids);
2109 int i;
2110
2111 printf(">>>>\n");
2112
2113 pjsua_enum_accs(acc_ids, &count);
2114
2115 printf("Account list:\n");
2116 for (i=0; i<(int)count; ++i)
2117 print_acc_status(acc_ids[i]);
2118
2119 print_buddy_list();
2120
2121 //puts("Commands:");
2122 puts("+=============================================================================+");
2123 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
2124 puts("| | | |");
2125 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
2126 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
Benny Prijono4461c7d2007-08-25 13:36:15 +00002127 puts("| a Answer call | i Send IM | !a Modify accnt. |");
2128 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
2129 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
2130 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002131 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
2132 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002133 puts("| x Xfer call | Media Commands: | Status & Config: |");
Benny Prijonof7b1c392006-11-11 16:46:34 +00002134 puts("| X Xfer with Replaces | | |");
Benny Prijonoc08682e2007-10-04 06:17:58 +00002135 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
2136 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
2137 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
2138 puts("| | V Adjust audio Volume | f Save config |");
2139 puts("| S Send arbitrary REQUEST | Cp Codec priorities | f Save config |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002140 puts("+------------------------------+--------------------------+-------------------+");
Benny Prijono438e65b2007-11-03 21:42:10 +00002141 puts("| q QUIT sleep N: console sleep for N ms n: detect NAT type |");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002142 puts("+=============================================================================+");
Benny Prijono48af79c2006-07-22 12:49:17 +00002143
2144 i = pjsua_call_get_count();
2145 printf("You have %d active call%s\n", i, (i>1?"s":""));
Benny Prijonof7b1c392006-11-11 16:46:34 +00002146
2147 if (current_call != PJSUA_INVALID_ID) {
2148 pjsua_call_info ci;
2149 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
2150 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
2151 (int)ci.remote_info.slen, ci.remote_info.ptr,
2152 (int)ci.state_text.slen, ci.state_text.ptr);
2153 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002154}
2155
2156
2157/*
2158 * Input simple string
2159 */
2160static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
2161{
2162 char *p;
2163
2164 printf("%s (empty to cancel): ", title); fflush(stdout);
2165 fgets(buf, len, stdin);
2166
2167 /* Remove trailing newlines. */
2168 for (p=buf; ; ++p) {
2169 if (*p=='\r' || *p=='\n') *p='\0';
2170 else if (!*p) break;
2171 }
2172
2173 if (!*buf)
2174 return PJ_FALSE;
2175
2176 return PJ_TRUE;
2177}
2178
2179
2180#define NO_NB -2
2181struct input_result
2182{
2183 int nb_result;
2184 char *uri_result;
2185};
2186
2187
2188/*
2189 * Input URL.
2190 */
2191static void ui_input_url(const char *title, char *buf, int len,
2192 struct input_result *result)
2193{
2194 result->nb_result = NO_NB;
2195 result->uri_result = NULL;
2196
2197 print_buddy_list();
2198
2199 printf("Choices:\n"
2200 " 0 For current dialog.\n"
2201 " -1 All %d buddies in buddy list\n"
2202 " [1 -%2d] Select from buddy list\n"
2203 " URL An URL\n"
2204 " <Enter> Empty input (or 'q') to cancel\n"
2205 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
2206 printf("%s: ", title);
2207
2208 fflush(stdout);
2209 fgets(buf, len, stdin);
2210 len = strlen(buf);
2211
2212 /* Left trim */
2213 while (pj_isspace(*buf)) {
2214 ++buf;
2215 --len;
2216 }
2217
2218 /* Remove trailing newlines */
2219 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
2220 buf[--len] = '\0';
2221
2222 if (len == 0 || buf[0]=='q')
2223 return;
2224
2225 if (pj_isdigit(*buf) || *buf=='-') {
2226
2227 int i;
2228
2229 if (*buf=='-')
2230 i = 1;
2231 else
2232 i = 0;
2233
2234 for (; i<len; ++i) {
2235 if (!pj_isdigit(buf[i])) {
2236 puts("Invalid input");
2237 return;
2238 }
2239 }
2240
2241 result->nb_result = my_atoi(buf);
2242
2243 if (result->nb_result >= 0 &&
2244 result->nb_result <= (int)pjsua_get_buddy_count())
2245 {
2246 return;
2247 }
2248 if (result->nb_result == -1)
2249 return;
2250
2251 puts("Invalid input");
2252 result->nb_result = NO_NB;
2253 return;
2254
2255 } else {
2256 pj_status_t status;
2257
2258 if ((status=pjsua_verify_sip_url(buf)) != PJ_SUCCESS) {
2259 pjsua_perror(THIS_FILE, "Invalid URL", status);
2260 return;
2261 }
2262
2263 result->uri_result = buf;
2264 }
2265}
2266
2267/*
2268 * List the ports in conference bridge
2269 */
2270static void conf_list(void)
2271{
2272 unsigned i, count;
2273 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
2274
2275 printf("Conference ports:\n");
2276
2277 count = PJ_ARRAY_SIZE(id);
2278 pjsua_enum_conf_ports(id, &count);
2279
2280 for (i=0; i<count; ++i) {
2281 char txlist[PJSUA_MAX_CALLS*4+10];
2282 unsigned j;
2283 pjsua_conf_port_info info;
2284
2285 pjsua_conf_get_port_info(id[i], &info);
2286
2287 txlist[0] = '\0';
2288 for (j=0; j<info.listener_cnt; ++j) {
2289 char s[10];
2290 pj_ansi_sprintf(s, "#%d ", info.listeners[j]);
2291 pj_ansi_strcat(txlist, s);
2292 }
2293 printf("Port #%02d[%2dKHz/%dms] %20.*s transmitting to: %s\n",
2294 info.slot_id,
2295 info.clock_rate/1000,
2296 info.samples_per_frame * 1000 / info.clock_rate,
2297 (int)info.name.slen,
2298 info.name.ptr,
2299 txlist);
2300
2301 }
2302 puts("");
2303}
2304
2305
2306/*
Benny Prijono56315612006-07-18 14:39:40 +00002307 * Send arbitrary request to remote host
2308 */
2309static void send_request(char *cstr_method, const pj_str_t *dst_uri)
2310{
2311 pj_str_t str_method;
2312 pjsip_method method;
2313 pjsip_tx_data *tdata;
Benny Prijono56315612006-07-18 14:39:40 +00002314 pjsip_endpoint *endpt;
2315 pj_status_t status;
2316
2317 endpt = pjsua_get_pjsip_endpt();
2318
2319 str_method = pj_str(cstr_method);
2320 pjsip_method_init_np(&method, &str_method);
2321
Benny Prijonofff245c2007-04-02 11:44:47 +00002322 status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata);
Benny Prijono56315612006-07-18 14:39:40 +00002323
Benny Prijonob988d762007-12-05 04:30:04 +00002324 status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL);
Benny Prijono56315612006-07-18 14:39:40 +00002325 if (status != PJ_SUCCESS) {
Benny Prijonob988d762007-12-05 04:30:04 +00002326 pjsua_perror(THIS_FILE, "Unable to send request", status);
Benny Prijono56315612006-07-18 14:39:40 +00002327 return;
2328 }
2329}
2330
2331
2332/*
Benny Prijono4461c7d2007-08-25 13:36:15 +00002333 * Change extended online status.
2334 */
2335static void change_online_status(void)
2336{
2337 char menuin[32];
2338 pj_bool_t online_status;
2339 pjrpid_element elem;
2340 int i, choice;
2341
2342 enum {
2343 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
2344 };
2345
2346 struct opt {
2347 int id;
2348 char *name;
2349 } opts[] = {
2350 { AVAILABLE, "Available" },
2351 { BUSY, "Busy"},
2352 { OTP, "On the phone"},
2353 { IDLE, "Idle"},
2354 { AWAY, "Away"},
2355 { BRB, "Be right back"},
2356 { OFFLINE, "Offline"}
2357 };
2358
2359 printf("\n"
2360 "Choices:\n");
2361 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
2362 printf(" %d %s\n", opts[i].id+1, opts[i].name);
2363 }
2364
2365 if (!simple_input("Select status", menuin, sizeof(menuin)))
2366 return;
2367
2368 choice = atoi(menuin) - 1;
2369 if (choice < 0 || choice >= OPT_MAX) {
2370 puts("Invalid selection");
2371 return;
2372 }
2373
2374 pj_bzero(&elem, sizeof(elem));
2375 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
2376
2377 online_status = PJ_TRUE;
2378
2379 switch (choice) {
2380 case AVAILABLE:
2381 break;
2382 case BUSY:
2383 elem.activity = PJRPID_ACTIVITY_BUSY;
2384 elem.note = pj_str("Busy");
2385 break;
2386 case OTP:
2387 elem.activity = PJRPID_ACTIVITY_BUSY;
2388 elem.note = pj_str("On the phone");
2389 break;
2390 case IDLE:
2391 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2392 elem.note = pj_str("Idle");
2393 break;
2394 case AWAY:
2395 elem.activity = PJRPID_ACTIVITY_AWAY;
2396 elem.note = pj_str("Away");
2397 break;
2398 case BRB:
2399 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
2400 elem.note = pj_str("Be right back");
2401 break;
2402 case OFFLINE:
2403 online_status = PJ_FALSE;
2404 break;
2405 }
2406
2407 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
2408}
2409
2410
2411/*
Benny Prijonoc08682e2007-10-04 06:17:58 +00002412 * Change codec priorities.
2413 */
2414static void manage_codec_prio(void)
2415{
2416 pjsua_codec_info c[32];
2417 unsigned i, count = PJ_ARRAY_SIZE(c);
2418 char input[32];
2419 char *codec, *prio;
2420 pj_str_t id;
2421 int new_prio;
2422 pj_status_t status;
2423
2424 printf("List of codecs:\n");
2425
2426 pjsua_enum_codecs(c, &count);
2427 for (i=0; i<count; ++i) {
2428 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
2429 c[i].codec_id.ptr);
2430 }
2431
2432 puts("");
2433 puts("Enter codec name and its new priority (e.g. \"speex/16000 200\"), empty to cancel:");
2434
2435 printf("Codec name and priority: ");
2436 fgets(input, sizeof(input), stdin);
2437 if (input[0]=='\r' || input[0]=='\n') {
2438 puts("Done");
2439 return;
2440 }
2441
2442 codec = strtok(input, " \t\r\n");
2443 prio = strtok(NULL, " \r\n");
2444
2445 if (!codec || !prio) {
2446 puts("Invalid input");
2447 return;
2448 }
2449
2450 new_prio = atoi(prio);
2451 if (new_prio < 0)
2452 new_prio = 0;
2453 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
2454 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
2455
2456 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
2457 (pj_uint8_t)new_prio);
2458 if (status != PJ_SUCCESS)
2459 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
2460}
2461
2462
2463/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002464 * Main "user interface" loop.
2465 */
2466void console_app_main(const pj_str_t *uri_to_call)
2467{
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00002468 char menuin[32];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002469 char buf[128];
2470 char text[128];
2471 int i, count;
2472 char *uri;
2473 pj_str_t tmp;
2474 struct input_result result;
2475 pjsua_call_info call_info;
2476 pjsua_acc_info acc_info;
2477
2478
2479 /* If user specifies URI to call, then call the URI */
2480 if (uri_to_call->slen) {
2481 pjsua_call_make_call( current_acc, uri_to_call, 0, NULL, NULL, NULL);
2482 }
2483
2484 keystroke_help();
2485
2486 for (;;) {
2487
2488 printf(">>> ");
2489 fflush(stdout);
2490
Benny Prijono990042e2007-01-21 19:36:00 +00002491 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
2492 /*
2493 * Be friendly to users who redirect commands into
2494 * program, when file ends, resume with kbd.
2495 * If exit is desired end script with q for quit
2496 */
2497 /* Reopen stdin/stdout/stderr to /dev/console */
2498#if defined(PJ_WIN32) && PJ_WIN32!=0
2499 if (freopen ("CONIN$", "r", stdin) == NULL) {
2500#else
2501 if (1) {
2502#endif
2503 puts("Cannot switch back to console from file redirection");
2504 menuin[0] = 'q';
2505 menuin[1] = '\0';
2506 } else {
2507 puts("Switched back to console from file redirection");
2508 continue;
2509 }
2510 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002511
2512 switch (menuin[0]) {
2513
2514 case 'm':
2515 /* Make call! : */
2516 printf("(You currently have %d calls)\n",
2517 pjsua_call_get_count());
2518
2519 uri = NULL;
2520 ui_input_url("Make call", buf, sizeof(buf), &result);
2521 if (result.nb_result != NO_NB) {
2522
2523 if (result.nb_result == -1 || result.nb_result == 0) {
2524 puts("You can't do that with make call!");
2525 continue;
2526 } else {
2527 pjsua_buddy_info binfo;
2528 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2529 uri = binfo.uri.ptr;
2530 }
2531
2532 } else if (result.uri_result) {
2533 uri = result.uri_result;
2534 }
2535
2536 tmp = pj_str(uri);
2537 pjsua_call_make_call( current_acc, &tmp, 0, NULL, NULL, NULL);
2538 break;
2539
2540 case 'M':
2541 /* Make multiple calls! : */
2542 printf("(You currently have %d calls)\n",
2543 pjsua_call_get_count());
2544
2545 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
2546 continue;
2547
2548 count = my_atoi(menuin);
2549 if (count < 1)
2550 continue;
2551
2552 ui_input_url("Make call", buf, sizeof(buf), &result);
2553 if (result.nb_result != NO_NB) {
2554 pjsua_buddy_info binfo;
2555 if (result.nb_result == -1 || result.nb_result == 0) {
2556 puts("You can't do that with make call!");
2557 continue;
2558 }
2559 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2560 uri = binfo.uri.ptr;
2561 } else {
2562 uri = result.uri_result;
2563 }
2564
2565 for (i=0; i<my_atoi(menuin); ++i) {
2566 pj_status_t status;
2567
2568 tmp = pj_str(uri);
2569 status = pjsua_call_make_call(current_acc, &tmp, 0, NULL,
2570 NULL, NULL);
2571 if (status != PJ_SUCCESS)
2572 break;
2573 }
2574 break;
2575
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002576 case 'n':
Benny Prijono438e65b2007-11-03 21:42:10 +00002577 i = pjsua_detect_nat_type();
2578 if (i != PJ_SUCCESS)
2579 pjsua_perror(THIS_FILE, "Error", i);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002580 break;
2581
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002582 case 'i':
2583 /* Send instant messaeg */
2584
2585 /* i is for call index to send message, if any */
2586 i = -1;
2587
2588 /* Make compiler happy. */
2589 uri = NULL;
2590
2591 /* Input destination. */
2592 ui_input_url("Send IM to", buf, sizeof(buf), &result);
2593 if (result.nb_result != NO_NB) {
2594
2595 if (result.nb_result == -1) {
2596 puts("You can't send broadcast IM like that!");
2597 continue;
2598
2599 } else if (result.nb_result == 0) {
2600
2601 i = current_call;
2602
2603 } else {
2604 pjsua_buddy_info binfo;
2605 pjsua_buddy_get_info(result.nb_result-1, &binfo);
2606 uri = binfo.uri.ptr;
2607 }
2608
2609 } else if (result.uri_result) {
2610 uri = result.uri_result;
2611 }
2612
2613
2614 /* Send typing indication. */
2615 if (i != -1)
2616 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
2617 else {
2618 pj_str_t tmp_uri = pj_str(uri);
2619 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
2620 }
2621
2622 /* Input the IM . */
2623 if (!simple_input("Message", text, sizeof(text))) {
2624 /*
2625 * Cancelled.
2626 * Send typing notification too, saying we're not typing.
2627 */
2628 if (i != -1)
2629 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
2630 else {
2631 pj_str_t tmp_uri = pj_str(uri);
2632 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
2633 }
2634 continue;
2635 }
2636
2637 tmp = pj_str(text);
2638
2639 /* Send the IM */
2640 if (i != -1)
2641 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
2642 else {
2643 pj_str_t tmp_uri = pj_str(uri);
2644 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
2645 }
2646
2647 break;
2648
2649 case 'a':
2650
2651 if (current_call != -1) {
2652 pjsua_call_get_info(current_call, &call_info);
2653 } else {
2654 /* Make compiler happy */
2655 call_info.role = PJSIP_ROLE_UAC;
2656 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
2657 }
2658
2659 if (current_call == -1 ||
2660 call_info.role != PJSIP_ROLE_UAS ||
2661 call_info.state >= PJSIP_INV_STATE_CONNECTING)
2662 {
2663 puts("No pending incoming call");
2664 fflush(stdout);
2665 continue;
2666
2667 } else {
Benny Prijono20d36722007-02-22 14:52:24 +00002668 int st_code;
2669 char contact[120];
2670 pj_str_t hname = { "Contact", 7 };
2671 pj_str_t hvalue;
2672 pjsip_generic_string_hdr hcontact;
2673 pjsua_msg_data msg_data;
2674
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002675 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
2676 continue;
2677
Benny Prijono20d36722007-02-22 14:52:24 +00002678 st_code = my_atoi(buf);
2679 if (st_code < 100)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002680 continue;
2681
Benny Prijono20d36722007-02-22 14:52:24 +00002682 pjsua_msg_data_init(&msg_data);
2683
2684 if (st_code/100 == 3) {
2685 if (!simple_input("Enter URL to be put in Contact",
2686 contact, sizeof(contact)))
2687 continue;
2688 hvalue = pj_str(contact);
2689 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
2690
2691 pj_list_push_back(&msg_data.hdr_list, &hcontact);
2692 }
2693
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002694 /*
2695 * Must check again!
2696 * Call may have been disconnected while we're waiting for
2697 * keyboard input.
2698 */
2699 if (current_call == -1) {
2700 puts("Call has been disconnected");
2701 fflush(stdout);
2702 continue;
2703 }
2704
Benny Prijono20d36722007-02-22 14:52:24 +00002705 pjsua_call_answer(current_call, st_code, NULL, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002706 }
2707
2708 break;
2709
2710
2711 case 'h':
2712
2713 if (current_call == -1) {
2714 puts("No current call");
2715 fflush(stdout);
2716 continue;
2717
2718 } else if (menuin[1] == 'a') {
2719
2720 /* Hangup all calls */
2721 pjsua_call_hangup_all();
2722
2723 } else {
2724
2725 /* Hangup current calls */
2726 pjsua_call_hangup(current_call, 0, NULL, NULL);
2727 }
2728 break;
2729
2730 case ']':
2731 case '[':
2732 /*
2733 * Cycle next/prev dialog.
2734 */
2735 if (menuin[0] == ']') {
2736 find_next_call();
2737
2738 } else {
2739 find_prev_call();
2740 }
2741
2742 if (current_call != -1) {
2743
2744 pjsua_call_get_info(current_call, &call_info);
2745 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
2746 (int)call_info.remote_info.slen,
2747 call_info.remote_info.ptr));
2748
2749 } else {
2750 PJ_LOG(3,(THIS_FILE,"No current dialog"));
2751 }
2752 break;
2753
2754
2755 case '>':
2756 case '<':
2757 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
2758 break;
2759
2760 i = my_atoi(buf);
2761 if (pjsua_acc_is_valid(i)) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00002762 pjsua_acc_set_default(i);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002763 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
2764 } else {
2765 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
2766 }
2767 break;
2768
2769
2770 case '+':
2771 if (menuin[1] == 'b') {
2772
2773 pjsua_buddy_config buddy_cfg;
2774 pjsua_buddy_id buddy_id;
2775 pj_status_t status;
2776
2777 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
2778 break;
2779
2780 if (pjsua_verify_sip_url(buf) != PJ_SUCCESS) {
2781 printf("Invalid SIP URI '%s'\n", buf);
2782 break;
2783 }
2784
Benny Prijonoac623b32006-07-03 15:19:31 +00002785 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002786
2787 buddy_cfg.uri = pj_str(buf);
2788 buddy_cfg.subscribe = PJ_TRUE;
2789
2790 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
2791 if (status == PJ_SUCCESS) {
2792 printf("New buddy '%s' added at index %d\n",
2793 buf, buddy_id+1);
2794 }
2795
2796 } else if (menuin[1] == 'a') {
2797
Benny Prijonofb2b3652007-06-28 07:15:03 +00002798 char id[80], registrar[80], realm[80], uname[80], passwd[30];
2799 pjsua_acc_config acc_cfg;
2800 pj_status_t status;
2801
2802 if (!simple_input("Your SIP URL:", id, sizeof(id)))
2803 break;
2804 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
2805 break;
2806 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
2807 break;
2808 if (!simple_input("Auth Username:", uname, sizeof(uname)))
2809 break;
2810 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
2811 break;
2812
2813 pjsua_acc_config_default(&acc_cfg);
2814 acc_cfg.id = pj_str(id);
2815 acc_cfg.reg_uri = pj_str(registrar);
2816 acc_cfg.cred_count = 1;
Benny Prijono48ab2b72007-11-08 09:24:30 +00002817 acc_cfg.cred_info[0].scheme = pj_str("Digest");
Benny Prijonofb2b3652007-06-28 07:15:03 +00002818 acc_cfg.cred_info[0].realm = pj_str(realm);
2819 acc_cfg.cred_info[0].username = pj_str(uname);
2820 acc_cfg.cred_info[0].data_type = 0;
2821 acc_cfg.cred_info[0].data = pj_str(passwd);
2822
2823 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
2824 if (status != PJ_SUCCESS) {
2825 pjsua_perror(THIS_FILE, "Error adding new account", status);
2826 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002827
2828 } else {
2829 printf("Invalid input %s\n", menuin);
2830 }
2831 break;
2832
2833 case '-':
2834 if (menuin[1] == 'b') {
2835 if (!simple_input("Enter buddy ID to delete",buf,sizeof(buf)))
2836 break;
2837
2838 i = my_atoi(buf) - 1;
2839
2840 if (!pjsua_buddy_is_valid(i)) {
2841 printf("Invalid buddy id %d\n", i);
2842 } else {
2843 pjsua_buddy_del(i);
2844 printf("Buddy %d deleted\n", i);
2845 }
2846
2847 } else if (menuin[1] == 'a') {
2848
2849 if (!simple_input("Enter account ID to delete",buf,sizeof(buf)))
2850 break;
2851
2852 i = my_atoi(buf);
2853
2854 if (!pjsua_acc_is_valid(i)) {
2855 printf("Invalid account id %d\n", i);
2856 } else {
2857 pjsua_acc_del(i);
2858 printf("Account %d deleted\n", i);
2859 }
2860
2861 } else {
2862 printf("Invalid input %s\n", menuin);
2863 }
2864 break;
2865
2866 case 'H':
2867 /*
2868 * Hold call.
2869 */
2870 if (current_call != -1) {
2871
2872 pjsua_call_set_hold(current_call, NULL);
2873
2874 } else {
2875 PJ_LOG(3,(THIS_FILE, "No current call"));
2876 }
2877 break;
2878
2879 case 'v':
2880 /*
2881 * Send re-INVITE (to release hold, etc).
2882 */
2883 if (current_call != -1) {
2884
2885 pjsua_call_reinvite(current_call, PJ_TRUE, NULL);
2886
2887 } else {
2888 PJ_LOG(3,(THIS_FILE, "No current call"));
2889 }
2890 break;
2891
Benny Prijonoc08682e2007-10-04 06:17:58 +00002892 case 'U':
2893 /*
2894 * Send UPDATE
2895 */
2896 if (current_call != -1) {
2897
2898 pjsua_call_update(current_call, 0, NULL);
2899
2900 } else {
2901 PJ_LOG(3,(THIS_FILE, "No current call"));
2902 }
2903 break;
2904
2905 case 'C':
2906 if (menuin[1] == 'p') {
2907 manage_codec_prio();
2908 }
2909 break;
2910
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002911 case 'x':
2912 /*
2913 * Transfer call.
2914 */
2915 if (current_call == -1) {
2916
2917 PJ_LOG(3,(THIS_FILE, "No current call"));
2918
2919 } else {
2920 int call = current_call;
Benny Prijonod524e822006-09-22 12:48:18 +00002921 pjsua_msg_data msg_data;
2922 pjsip_generic_string_hdr refer_sub;
2923 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
2924 pj_str_t STR_FALSE = { "false", 5 };
Benny Prijonof7b1c392006-11-11 16:46:34 +00002925 pjsua_call_info ci;
2926
2927 pjsua_call_get_info(current_call, &ci);
2928 printf("Transfering current call [%d] %.*s\n",
2929 current_call,
2930 (int)ci.remote_info.slen, ci.remote_info.ptr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002931
2932 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
2933
2934 /* Check if call is still there. */
2935
2936 if (call != current_call) {
2937 puts("Call has been disconnected");
2938 continue;
2939 }
2940
Benny Prijonod524e822006-09-22 12:48:18 +00002941 pjsua_msg_data_init(&msg_data);
Benny Prijono4ddad2c2006-10-18 17:16:34 +00002942 if (app_config.no_refersub) {
2943 /* Add Refer-Sub: false in outgoing REFER request */
2944 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
2945 &STR_FALSE);
2946 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
2947 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002948 if (result.nb_result != NO_NB) {
2949 if (result.nb_result == -1 || result.nb_result == 0)
2950 puts("You can't do that with transfer call!");
2951 else {
2952 pjsua_buddy_info binfo;
2953 pjsua_buddy_get_info(result.nb_result-1, &binfo);
Benny Prijonod524e822006-09-22 12:48:18 +00002954 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002955 }
2956
2957 } else if (result.uri_result) {
2958 pj_str_t tmp;
2959 tmp = pj_str(result.uri_result);
Benny Prijonod524e822006-09-22 12:48:18 +00002960 pjsua_call_xfer( current_call, &tmp, &msg_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002961 }
2962 }
2963 break;
2964
Benny Prijonof7b1c392006-11-11 16:46:34 +00002965 case 'X':
2966 /*
2967 * Transfer call with replaces.
2968 */
2969 if (current_call == -1) {
2970
2971 PJ_LOG(3,(THIS_FILE, "No current call"));
2972
2973 } else {
2974 int call = current_call;
2975 int dst_call;
2976 pjsua_msg_data msg_data;
2977 pjsip_generic_string_hdr refer_sub;
2978 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
2979 pj_str_t STR_FALSE = { "false", 5 };
2980 pjsua_call_id ids[PJSUA_MAX_CALLS];
2981 pjsua_call_info ci;
2982 unsigned i, count;
2983
2984 count = PJ_ARRAY_SIZE(ids);
2985 pjsua_enum_calls(ids, &count);
2986
2987 if (count <= 1) {
2988 puts("There are no other calls");
2989 continue;
2990 }
2991
2992 pjsua_call_get_info(current_call, &ci);
2993 printf("Transfer call [%d] %.*s to one of the following:\n",
2994 current_call,
2995 (int)ci.remote_info.slen, ci.remote_info.ptr);
2996
2997 for (i=0; i<count; ++i) {
2998 pjsua_call_info call_info;
2999
3000 if (ids[i] == call)
3001 continue;
3002
3003 pjsua_call_get_info(ids[i], &call_info);
3004 printf("%d %.*s [%.*s]\n",
3005 ids[i],
3006 (int)call_info.remote_info.slen,
3007 call_info.remote_info.ptr,
3008 (int)call_info.state_text.slen,
3009 call_info.state_text.ptr);
3010 }
3011
3012 if (!simple_input("Enter call number to be replaced",
3013 buf, sizeof(buf)))
3014 continue;
3015
3016 dst_call = my_atoi(buf);
3017
3018 /* Check if call is still there. */
3019
3020 if (call != current_call) {
3021 puts("Call has been disconnected");
3022 continue;
3023 }
3024
3025 /* Check that destination call is valid. */
3026 if (dst_call == call) {
3027 puts("Destination call number must not be the same "
3028 "as the call being transfered");
3029 continue;
3030 }
3031 if (dst_call >= PJSUA_MAX_CALLS) {
3032 puts("Invalid destination call number");
3033 continue;
3034 }
3035 if (!pjsua_call_is_active(dst_call)) {
3036 puts("Invalid destination call number");
3037 continue;
3038 }
3039
3040 pjsua_msg_data_init(&msg_data);
3041 if (app_config.no_refersub) {
3042 /* Add Refer-Sub: false in outgoing REFER request */
3043 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
3044 &STR_FALSE);
3045 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
3046 }
3047
3048 pjsua_call_xfer_replaces(call, dst_call, 0, &msg_data);
3049 }
3050 break;
3051
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003052 case '#':
3053 /*
3054 * Send DTMF strings.
3055 */
3056 if (current_call == -1) {
3057
3058 PJ_LOG(3,(THIS_FILE, "No current call"));
3059
3060 } else if (!pjsua_call_has_media(current_call)) {
3061
3062 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
3063
3064 } else {
3065 pj_str_t digits;
3066 int call = current_call;
3067 pj_status_t status;
3068
3069 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3070 sizeof(buf)))
3071 {
3072 break;
3073 }
3074
3075 if (call != current_call) {
3076 puts("Call has been disconnected");
3077 continue;
3078 }
3079
3080 digits = pj_str(buf);
3081 status = pjsua_call_dial_dtmf(current_call, &digits);
3082 if (status != PJ_SUCCESS) {
3083 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
3084 } else {
3085 puts("DTMF digits enqueued for transmission");
3086 }
3087 }
3088 break;
3089
Benny Prijonofeb69f42007-10-05 09:12:26 +00003090 case '*':
3091 /* Send DTMF with INFO */
3092 if (current_call == -1) {
3093
3094 PJ_LOG(3,(THIS_FILE, "No current call"));
3095
3096 } else {
3097 const pj_str_t SIP_INFO = pj_str("INFO");
3098 pj_str_t digits;
3099 int call = current_call;
3100 int i;
3101 pj_status_t status;
3102
3103 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
3104 sizeof(buf)))
3105 {
3106 break;
3107 }
3108
3109 if (call != current_call) {
3110 puts("Call has been disconnected");
3111 continue;
3112 }
3113
3114 digits = pj_str(buf);
3115 for (i=0; i<digits.slen; ++i) {
3116 pjsua_msg_data msg_data;
3117 char body[80];
3118
3119 pjsua_msg_data_init(&msg_data);
3120 msg_data.content_type = pj_str("application/dtmf-relay");
3121
3122 pj_ansi_snprintf(body, sizeof(body),
3123 "Signal=%c\r\n"
3124 "Duration=160",
3125 buf[i]);
3126 msg_data.msg_body = pj_str(body);
3127
3128 status = pjsua_call_send_request(current_call, &SIP_INFO,
3129 &msg_data);
3130 if (status != PJ_SUCCESS) {
3131 break;
3132 }
3133 }
3134 }
3135 break;
3136
Benny Prijono56315612006-07-18 14:39:40 +00003137 case 'S':
3138 /*
3139 * Send arbitrary request
3140 */
3141 if (pjsua_acc_get_count() == 0) {
3142 puts("Sorry, need at least one account configured");
3143 break;
3144 }
3145
3146 puts("Send arbitrary request to remote host");
3147
3148 /* Input METHOD */
3149 if (!simple_input("Request method:",text,sizeof(text)))
3150 break;
3151
3152 /* Input destination URI */
3153 uri = NULL;
3154 ui_input_url("Destination URI", buf, sizeof(buf), &result);
3155 if (result.nb_result != NO_NB) {
3156
3157 if (result.nb_result == -1 || result.nb_result == 0) {
3158 puts("Sorry you can't do that!");
3159 continue;
3160 } else {
3161 pjsua_buddy_info binfo;
3162 pjsua_buddy_get_info(result.nb_result-1, &binfo);
3163 uri = binfo.uri.ptr;
3164 }
3165
3166 } else if (result.uri_result) {
3167 uri = result.uri_result;
3168 }
3169
3170 tmp = pj_str(uri);
3171
3172 send_request(text, &tmp);
3173 break;
3174
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003175 case 's':
Benny Prijono990042e2007-01-21 19:36:00 +00003176 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
3177 pj_str_t tmp;
3178 int delay;
3179
3180 tmp.ptr = menuin+6;
3181 tmp.slen = pj_ansi_strlen(menuin)-7;
3182
3183 if (tmp.slen < 1) {
3184 puts("Usage: sleep MSEC");
3185 break;
3186 }
3187
3188 delay = pj_strtoul(&tmp);
3189 if (delay < 0) delay = 0;
3190 pj_thread_sleep(delay);
3191 break;
3192 }
3193 /* Continue below */
3194
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003195 case 'u':
3196 /*
3197 * Subscribe/unsubscribe presence.
3198 */
3199 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
3200 if (result.nb_result != NO_NB) {
3201 if (result.nb_result == -1) {
3202 int i, count;
3203 count = pjsua_get_buddy_count();
3204 for (i=0; i<count; ++i)
3205 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
3206 } else if (result.nb_result == 0) {
3207 puts("Sorry, can only subscribe to buddy's presence, "
3208 "not from existing call");
3209 } else {
3210 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
3211 }
3212
3213 } else if (result.uri_result) {
3214 puts("Sorry, can only subscribe to buddy's presence, "
3215 "not arbitrary URL (for now)");
3216 }
3217
3218 break;
3219
3220 case 'r':
3221 switch (menuin[1]) {
3222 case 'r':
3223 /*
3224 * Re-Register.
3225 */
3226 pjsua_acc_set_registration(current_acc, PJ_TRUE);
3227 break;
3228 case 'u':
3229 /*
3230 * Unregister
3231 */
3232 pjsua_acc_set_registration(current_acc, PJ_FALSE);
3233 break;
3234 }
3235 break;
3236
3237 case 't':
3238 pjsua_acc_get_info(current_acc, &acc_info);
3239 acc_info.online_status = !acc_info.online_status;
3240 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
3241 printf("Setting %s online status to %s\n",
3242 acc_info.acc_uri.ptr,
3243 (acc_info.online_status?"online":"offline"));
3244 break;
3245
Benny Prijono4461c7d2007-08-25 13:36:15 +00003246 case 'T':
3247 change_online_status();
3248 break;
3249
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003250 case 'c':
3251 switch (menuin[1]) {
3252 case 'l':
3253 conf_list();
3254 break;
3255 case 'c':
3256 case 'd':
3257 {
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003258 char tmp[10], src_port[10], dst_port[10];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003259 pj_status_t status;
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003260 int cnt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003261 const char *src_title, *dst_title;
3262
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003263 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003264
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003265 if (cnt != 3) {
3266 conf_list();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003267
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003268 src_title = (menuin[1]=='c'?
3269 "Connect src port #":
3270 "Disconnect src port #");
3271 dst_title = (menuin[1]=='c'?
3272 "To dst port #":
3273 "From dst port #");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003274
Benny Prijono2b6ec1a2006-11-26 10:49:58 +00003275 if (!simple_input(src_title, src_port, sizeof(src_port)))
3276 break;
3277
3278 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
3279 break;
3280 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003281
3282 if (menuin[1]=='c') {
3283 status = pjsua_conf_connect(my_atoi(src_port),
3284 my_atoi(dst_port));
3285 } else {
3286 status = pjsua_conf_disconnect(my_atoi(src_port),
3287 my_atoi(dst_port));
3288 }
3289 if (status == PJ_SUCCESS) {
3290 puts("Success");
3291 } else {
3292 puts("ERROR!!");
3293 }
3294 }
3295 break;
3296 }
3297 break;
3298
Benny Prijono6dd967c2006-12-26 02:27:14 +00003299 case 'V':
3300 /* Adjust audio volume */
3301 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
3302 if (simple_input(buf,text,sizeof(text))) {
3303 char *err;
3304 app_config.mic_level = (float)strtod(text, &err);
3305 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
3306 }
3307 sprintf(buf, "Adjust speaker level: [%4.1fx] ",
3308 app_config.speaker_level);
3309 if (simple_input(buf,text,sizeof(text))) {
3310 char *err;
3311 app_config.speaker_level = (float)strtod(text, &err);
3312 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
3313 }
3314
3315 break;
3316
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003317 case 'd':
3318 if (menuin[1] == 'c') {
3319 char settings[2000];
3320 int len;
3321
3322 len = write_settings(&app_config, settings, sizeof(settings));
3323 if (len < 1)
3324 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3325 else
3326 PJ_LOG(3,(THIS_FILE,
3327 "Dumping configuration (%d bytes):\n%s\n",
3328 len, settings));
Benny Prijono819506c2006-06-22 22:29:51 +00003329
3330 } else if (menuin[1] == 'q') {
3331
3332 if (current_call != PJSUA_INVALID_ID) {
3333 char buf[1024];
3334 pjsua_call_dump(current_call, PJ_TRUE, buf,
3335 sizeof(buf), " ");
3336 PJ_LOG(3,(THIS_FILE, "\n%s", buf));
3337 } else {
3338 PJ_LOG(3,(THIS_FILE, "No current call"));
3339 }
3340
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003341 } else {
3342 app_dump(menuin[1]=='d');
3343 }
3344 break;
3345
3346
3347 case 'f':
3348 if (simple_input("Enter output filename", buf, sizeof(buf))) {
3349 char settings[2000];
3350 int len;
3351
3352 len = write_settings(&app_config, settings, sizeof(settings));
3353 if (len < 1)
3354 PJ_LOG(3,(THIS_FILE, "Error: not enough buffer"));
3355 else {
3356 pj_oshandle_t fd;
3357 pj_status_t status;
3358
3359 status = pj_file_open(app_config.pool, buf,
3360 PJ_O_WRONLY, &fd);
3361 if (status != PJ_SUCCESS) {
3362 pjsua_perror(THIS_FILE, "Unable to open file", status);
3363 } else {
3364 pj_ssize_t size = len;
3365 pj_file_write(fd, settings, &size);
3366 pj_file_close(fd);
3367
3368 printf("Settings successfully written to '%s'\n", buf);
3369 }
3370 }
3371
3372 }
3373 break;
3374
3375
3376 case 'q':
3377 goto on_exit;
3378
3379
3380 default:
3381 if (menuin[0] != '\n' && menuin[0] != '\r') {
3382 printf("Invalid input %s", menuin);
3383 }
3384 keystroke_help();
3385 break;
3386 }
3387 }
3388
3389on_exit:
3390 ;
3391}
3392
3393
3394/*****************************************************************************
3395 * Public API
3396 */
3397
3398pj_status_t app_init(int argc, char *argv[])
3399{
Benny Prijonoe93e2872006-06-28 16:46:49 +00003400 pjsua_transport_id transport_id = -1;
Benny Prijonoe347cb02007-02-14 14:36:13 +00003401 pjsua_transport_config tcp_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003402 unsigned i;
3403 pj_status_t status;
3404
3405 /* Create pjsua */
3406 status = pjsua_create();
3407 if (status != PJ_SUCCESS)
3408 return status;
3409
3410 /* Create pool for application */
Benny Prijonod5696da2007-07-17 16:25:45 +00003411 app_config.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003412
3413 /* Initialize default config */
3414 default_config(&app_config);
3415
3416 /* Parse the arguments */
3417 status = parse_args(argc, argv, &app_config, &uri_arg);
3418 if (status != PJ_SUCCESS)
3419 return status;
3420
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003421 /* Initialize application callbacks */
3422 app_config.cfg.cb.on_call_state = &on_call_state;
3423 app_config.cfg.cb.on_call_media_state = &on_call_media_state;
3424 app_config.cfg.cb.on_incoming_call = &on_incoming_call;
Benny Prijonofeb69f42007-10-05 09:12:26 +00003425 app_config.cfg.cb.on_call_tsx_state = &on_call_tsx_state;
Benny Prijono0875ae82006-12-26 00:11:48 +00003426 app_config.cfg.cb.on_dtmf_digit = &call_on_dtmf_callback;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003427 app_config.cfg.cb.on_reg_state = &on_reg_state;
3428 app_config.cfg.cb.on_buddy_state = &on_buddy_state;
3429 app_config.cfg.cb.on_pager = &on_pager;
3430 app_config.cfg.cb.on_typing = &on_typing;
Benny Prijono4ddad2c2006-10-18 17:16:34 +00003431 app_config.cfg.cb.on_call_transfer_status = &on_call_transfer_status;
Benny Prijonof7b1c392006-11-11 16:46:34 +00003432 app_config.cfg.cb.on_call_replaced = &on_call_replaced;
Benny Prijono6ba8c542007-10-16 01:34:14 +00003433 app_config.cfg.cb.on_nat_detect = &on_nat_detect;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003434
3435 /* Initialize pjsua */
3436 status = pjsua_init(&app_config.cfg, &app_config.log_cfg,
3437 &app_config.media_cfg);
3438 if (status != PJ_SUCCESS)
3439 return status;
3440
Benny Prijonoe909eac2006-07-27 22:04:56 +00003441#ifdef STEREO_DEMO
3442 stereo_demo();
3443#endif
3444
Benny Prijono804ff0a2006-09-14 11:17:48 +00003445 /* Initialize calls data */
3446 for (i=0; i<PJ_ARRAY_SIZE(app_config.call_data); ++i) {
3447 app_config.call_data[i].timer.id = PJSUA_INVALID_ID;
3448 app_config.call_data[i].timer.cb = &call_timeout_callback;
3449 }
3450
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003451 /* Optionally registers WAV file */
Benny Prijono32e4f492007-01-24 00:44:26 +00003452 for (i=0; i<app_config.wav_count; ++i) {
3453 pjsua_player_id wav_id;
3454
3455 status = pjsua_player_create(&app_config.wav_files[i], 0,
3456 &wav_id);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003457 if (status != PJ_SUCCESS)
3458 goto on_error;
Benny Prijono22a300a2006-06-14 20:04:55 +00003459
Benny Prijono72c04d22007-02-17 22:20:58 +00003460 if (app_config.wav_id == PJSUA_INVALID_ID) {
Benny Prijono32e4f492007-01-24 00:44:26 +00003461 app_config.wav_id = wav_id;
3462 app_config.wav_port = pjsua_player_get_conf_port(app_config.wav_id);
3463 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003464 }
3465
Benny Prijono4af234b2007-01-24 02:02:09 +00003466 /* Optionally registers tone players */
3467 for (i=0; i<app_config.tone_count; ++i) {
3468 pjmedia_port *tport;
3469 char name[80];
3470 pj_str_t label;
3471 pj_status_t status;
3472
3473 pj_ansi_snprintf(name, sizeof(name), "tone-%d,%d",
3474 app_config.tones[i].freq1,
3475 app_config.tones[i].freq2);
3476 label = pj_str(name);
3477 status = pjmedia_tonegen_create2(app_config.pool, &label,
3478 8000, 1, 160, 16,
3479 PJMEDIA_TONEGEN_LOOP, &tport);
3480 if (status != PJ_SUCCESS) {
3481 pjsua_perror(THIS_FILE, "Unable to create tone generator", status);
3482 goto on_error;
3483 }
3484
3485 status = pjsua_conf_add_port(app_config.pool, tport,
3486 &app_config.tone_slots[i]);
3487 pj_assert(status == PJ_SUCCESS);
3488
3489 status = pjmedia_tonegen_play(tport, 1, &app_config.tones[i], 0);
3490 pj_assert(status == PJ_SUCCESS);
3491 }
3492
Benny Prijono1ebd6142006-10-19 15:48:02 +00003493 /* Optionally create recorder file, if any. */
3494 if (app_config.rec_file.slen) {
3495 status = pjsua_recorder_create(&app_config.rec_file, 0, NULL, 0, 0,
3496 &app_config.rec_id);
3497 if (status != PJ_SUCCESS)
3498 goto on_error;
3499
3500 app_config.rec_port = pjsua_recorder_get_conf_port(app_config.rec_id);
3501 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003502
Benny Prijonoe347cb02007-02-14 14:36:13 +00003503 pj_memcpy(&tcp_cfg, &app_config.udp_cfg, sizeof(tcp_cfg));
3504
Benny Prijono87ef89a2007-01-14 00:39:45 +00003505 /* Add UDP transport unless it's disabled. */
3506 if (!app_config.no_udp) {
3507 pjsua_acc_id aid;
3508
3509 status = pjsua_transport_create(PJSIP_TRANSPORT_UDP,
3510 &app_config.udp_cfg,
3511 &transport_id);
3512 if (status != PJ_SUCCESS)
3513 goto on_error;
3514
3515 /* Add local account */
3516 pjsua_acc_add_local(transport_id, PJ_TRUE, &aid);
3517 //pjsua_acc_set_transport(aid, transport_id);
3518 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
Benny Prijonoe347cb02007-02-14 14:36:13 +00003519
3520 if (app_config.udp_cfg.port == 0) {
3521 pjsua_transport_info ti;
3522 pj_sockaddr_in *a;
3523
3524 pjsua_transport_get_info(transport_id, &ti);
3525 a = (pj_sockaddr_in*)&ti.local_addr;
3526
3527 tcp_cfg.port = pj_ntohs(a->sin_port);
3528 }
Benny Prijono87ef89a2007-01-14 00:39:45 +00003529 }
3530
Benny Prijonoe93e2872006-06-28 16:46:49 +00003531 /* Add TCP transport unless it's disabled */
3532 if (!app_config.no_tcp) {
3533 status = pjsua_transport_create(PJSIP_TRANSPORT_TCP,
Benny Prijonoe347cb02007-02-14 14:36:13 +00003534 &tcp_cfg,
Benny Prijonoe93e2872006-06-28 16:46:49 +00003535 &transport_id);
3536 if (status != PJ_SUCCESS)
3537 goto on_error;
3538
3539 /* Add local account */
Benny Prijono21b9ad92006-08-15 13:11:22 +00003540 pjsua_acc_add_local(transport_id, PJ_TRUE, NULL);
Benny Prijonoe93e2872006-06-28 16:46:49 +00003541 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3542
3543 }
3544
Benny Prijonoe93e2872006-06-28 16:46:49 +00003545
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003546#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
3547 /* Add TLS transport when application wants one */
3548 if (app_config.use_tls) {
Benny Prijonof3bbc132006-12-25 06:43:59 +00003549
3550 pjsua_acc_id acc_id;
3551
3552 /* Set TLS port as TCP port+1 */
Benny Prijonoafc47be2007-02-14 14:44:55 +00003553 tcp_cfg.port++;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003554 status = pjsua_transport_create(PJSIP_TRANSPORT_TLS,
Benny Prijonoafc47be2007-02-14 14:44:55 +00003555 &tcp_cfg,
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003556 &transport_id);
Benny Prijonoafc47be2007-02-14 14:44:55 +00003557 tcp_cfg.port--;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003558 if (status != PJ_SUCCESS)
3559 goto on_error;
Benny Prijonof3bbc132006-12-25 06:43:59 +00003560
3561 /* Add local account */
3562 pjsua_acc_add_local(transport_id, PJ_FALSE, &acc_id);
3563 pjsua_acc_set_online_status(acc_id, PJ_TRUE);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00003564 }
3565#endif
3566
Benny Prijonoe93e2872006-06-28 16:46:49 +00003567 if (transport_id == -1) {
3568 PJ_LOG(3,(THIS_FILE, "Error: no transport is configured"));
3569 status = -1;
3570 goto on_error;
3571 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003572
3573
3574 /* Add accounts */
3575 for (i=0; i<app_config.acc_cnt; ++i) {
Benny Prijono21b9ad92006-08-15 13:11:22 +00003576 status = pjsua_acc_add(&app_config.acc_cfg[i], PJ_TRUE, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003577 if (status != PJ_SUCCESS)
3578 goto on_error;
3579 pjsua_acc_set_online_status(current_acc, PJ_TRUE);
3580 }
3581
3582 /* Add buddies */
3583 for (i=0; i<app_config.buddy_cnt; ++i) {
3584 status = pjsua_buddy_add(&app_config.buddy_cfg[i], NULL);
3585 if (status != PJ_SUCCESS)
3586 goto on_error;
3587 }
3588
3589 /* Optionally set codec orders */
3590 for (i=0; i<app_config.codec_cnt; ++i) {
3591 pjsua_codec_set_priority(&app_config.codec_arg[i],
3592 (pj_uint8_t)(PJMEDIA_CODEC_PRIO_NORMAL+i+9));
3593 }
3594
Benny Prijonofce28542007-12-09 15:41:10 +00003595 /* Optionally disable some codec */
3596 for (i=0; i<app_config.codec_dis_cnt; ++i) {
3597 pjsua_codec_set_priority(&app_config.codec_dis[i],PJMEDIA_CODEC_PRIO_DISABLED);
3598 }
3599
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003600 /* Add RTP transports */
3601 status = pjsua_media_transports_create(&app_config.rtp_cfg);
3602 if (status != PJ_SUCCESS)
3603 goto on_error;
3604
Benny Prijono22a300a2006-06-14 20:04:55 +00003605 /* Use null sound device? */
Benny Prijonoe909eac2006-07-27 22:04:56 +00003606#ifndef STEREO_DEMO
Benny Prijono22a300a2006-06-14 20:04:55 +00003607 if (app_config.null_audio) {
3608 status = pjsua_set_null_snd_dev();
3609 if (status != PJ_SUCCESS)
3610 return status;
3611 }
Benny Prijonoe909eac2006-07-27 22:04:56 +00003612#endif
Benny Prijono22a300a2006-06-14 20:04:55 +00003613
Benny Prijono4e5d5512007-03-06 18:11:30 +00003614 if (app_config.capture_dev != PJSUA_INVALID_ID
3615 || app_config.playback_dev != PJSUA_INVALID_ID) {
3616 status
3617 = pjsua_set_snd_dev(app_config.capture_dev, app_config.playback_dev);
3618 if (status != PJ_SUCCESS)
3619 goto on_error;
3620 }
3621
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003622 return PJ_SUCCESS;
3623
3624on_error:
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00003625 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003626 return status;
3627}
3628
3629
3630pj_status_t app_main(void)
3631{
3632 pj_status_t status;
3633
3634 /* Start pjsua */
3635 status = pjsua_start();
3636 if (status != PJ_SUCCESS) {
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00003637 app_destroy();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003638 return status;
3639 }
3640
3641 console_app_main(&uri_arg);
3642
3643 return PJ_SUCCESS;
3644}
3645
3646pj_status_t app_destroy(void)
3647{
Benny Prijonof762ee72006-12-01 11:14:37 +00003648 pj_status_t status;
Benny Prijono4af234b2007-01-24 02:02:09 +00003649 unsigned i;
Benny Prijonof762ee72006-12-01 11:14:37 +00003650
Benny Prijonoe909eac2006-07-27 22:04:56 +00003651#ifdef STEREO_DEMO
3652 if (app_config.snd) {
3653 pjmedia_snd_port_destroy(app_config.snd);
3654 app_config.snd = NULL;
3655 }
3656#endif
3657
Benny Prijono4af234b2007-01-24 02:02:09 +00003658 /* Close tone generators */
3659 for (i=0; i<app_config.tone_count; ++i) {
3660 pjsua_conf_remove_port(app_config.tone_slots[i]);
3661 }
3662
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003663 if (app_config.pool) {
3664 pj_pool_release(app_config.pool);
3665 app_config.pool = NULL;
3666 }
3667
Benny Prijonof762ee72006-12-01 11:14:37 +00003668 status = pjsua_destroy();
3669
3670 pj_bzero(&app_config, sizeof(app_config));
3671
3672 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003673}
Benny Prijonoe909eac2006-07-27 22:04:56 +00003674
3675
3676#ifdef STEREO_DEMO
3677static void stereo_demo()
3678{
3679 pjmedia_port *conf, *splitter, *ch1;
Benny Prijonoe909eac2006-07-27 22:04:56 +00003680 pj_status_t status;
3681
3682 /* Disable existing sound device */
3683 conf = pjsua_set_no_snd_dev();
3684
Benny Prijonoe909eac2006-07-27 22:04:56 +00003685 /* Create stereo-mono splitter/combiner */
3686 status = pjmedia_splitcomb_create(app_config.pool,
Benny Prijono478cf7c2007-06-02 00:12:29 +00003687 conf->info.clock_rate /* clock rate */,
Benny Prijonoe909eac2006-07-27 22:04:56 +00003688 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00003689 2 * conf->info.samples_per_frame,
3690 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00003691 0 /* options */,
3692 &splitter);
3693 pj_assert(status == PJ_SUCCESS);
3694
3695 /* Connect channel0 (left channel?) to conference port slot0 */
3696 status = pjmedia_splitcomb_set_channel(splitter, 0 /* ch0 */,
3697 0 /*options*/,
3698 conf);
3699 pj_assert(status == PJ_SUCCESS);
3700
3701 /* Create reverse channel for channel1 (right channel?)... */
3702 status = pjmedia_splitcomb_create_rev_channel(app_config.pool,
3703 splitter,
3704 1 /* ch1 */,
3705 0 /* options */,
3706 &ch1);
3707 pj_assert(status == PJ_SUCCESS);
3708
3709 /* .. and register it to conference bridge (it would be slot1
3710 * if there's no other devices connected to the bridge)
3711 */
3712 status = pjsua_conf_add_port(app_config.pool, ch1, NULL);
3713 pj_assert(status == PJ_SUCCESS);
3714
3715 /* Create sound device */
3716 status = pjmedia_snd_port_create(app_config.pool, -1, -1,
Benny Prijono478cf7c2007-06-02 00:12:29 +00003717 conf->info.clock_rate,
Benny Prijonoe909eac2006-07-27 22:04:56 +00003718 2 /* stereo */,
Benny Prijono478cf7c2007-06-02 00:12:29 +00003719 2 * conf->info.samples_per_frame,
3720 conf->info.bits_per_sample,
Benny Prijonoe909eac2006-07-27 22:04:56 +00003721 0, &app_config.snd);
3722 pj_assert(status == PJ_SUCCESS);
3723
3724
3725 /* Connect the splitter to the sound device */
3726 status = pjmedia_snd_port_connect(app_config.snd, splitter);
3727 pj_assert(status == PJ_SUCCESS);
3728
3729}
3730#endif
3731