blob: 0818aa86c66426c054c86ae27a427c18b7f89784 [file] [log] [blame]
Benny Prijono8e4a1132013-04-03 22:41:23 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <pjsua-lib/pjsua.h>
Riza Sulistyobc9c6772013-04-05 03:02:19 +000022#include "pjsua_common.h"
Benny Prijono8e4a1132013-04-03 22:41:23 +000023
Riza Sulistyobc9c6772013-04-05 03:02:19 +000024#define THIS_FILE "pjsua_legacy.c"
Benny Prijono8e4a1132013-04-03 22:41:23 +000025
26static pj_bool_t cmd_echo;
27
28/*
29 * Print buddy list.
30 */
31static void print_buddy_list()
32{
33 pjsua_buddy_id ids[64];
34 int i;
35 unsigned count = PJ_ARRAY_SIZE(ids);
36
37 puts("Buddy list:");
38
39 pjsua_enum_buddies(ids, &count);
40
41 if (count == 0)
42 puts(" -none-");
43 else {
44 for (i=0; i<(int)count; ++i) {
45 pjsua_buddy_info info;
46
47 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
48 continue;
49
50 printf(" [%2d] <%.*s> %.*s\n",
51 ids[i]+1,
52 (int)info.status_text.slen,
53 info.status_text.ptr,
54 (int)info.uri.slen,
55 info.uri.ptr);
56 }
57 }
58 puts("");
59}
60
61/*
62 * Input URL.
63 */
64static void ui_input_url(const char *title, char *buf, int len,
65 input_result *result)
66{
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +000067 result->nb_result = PJSUA_APP_NO_NB;
Benny Prijono8e4a1132013-04-03 22:41:23 +000068 result->uri_result = NULL;
69
70 print_buddy_list();
71
72 printf("Choices:\n"
73 " 0 For current dialog.\n"
74 " -1 All %d buddies in buddy list\n"
75 " [1 -%2d] Select from buddy list\n"
76 " URL An URL\n"
77 " <Enter> Empty input (or 'q') to cancel\n"
78 , pjsua_get_buddy_count(), pjsua_get_buddy_count());
79 printf("%s: ", title);
80
81 fflush(stdout);
82 if (fgets(buf, len, stdin) == NULL)
83 return;
84 len = strlen(buf);
85
86 /* Left trim */
87 while (pj_isspace(*buf)) {
88 ++buf;
89 --len;
90 }
91
92 /* Remove trailing newlines */
93 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
94 buf[--len] = '\0';
95
96 if (len == 0 || buf[0]=='q')
97 return;
98
99 if (pj_isdigit(*buf) || *buf=='-') {
100
101 int i;
102
103 if (*buf=='-')
104 i = 1;
105 else
106 i = 0;
107
108 for (; i<len; ++i) {
109 if (!pj_isdigit(buf[i])) {
110 puts("Invalid input");
111 return;
112 }
113 }
114
115 result->nb_result = my_atoi(buf);
116
117 if (result->nb_result >= 0 &&
118 result->nb_result <= (int)pjsua_get_buddy_count())
119 {
120 return;
121 }
122 if (result->nb_result == -1)
123 return;
124
125 puts("Invalid input");
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +0000126 result->nb_result = PJSUA_APP_NO_NB;
Benny Prijono8e4a1132013-04-03 22:41:23 +0000127 return;
128
129 } else {
130 pj_status_t status;
131
132 if ((status=pjsua_verify_url(buf)) != PJ_SUCCESS) {
133 pjsua_perror(THIS_FILE, "Invalid URL", status);
134 return;
135 }
136
137 result->uri_result = buf;
138 }
139}
140
141static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
142{
143 char *p;
144
145 printf("%s (empty to cancel): ", title); fflush(stdout);
146 if (fgets(buf, len, stdin) == NULL)
147 return PJ_FALSE;
148
149 /* Remove trailing newlines. */
150 for (p=buf; ; ++p) {
151 if (*p=='\r' || *p=='\n') *p='\0';
152 else if (!*p) break;
153 }
154
155 if (!*buf)
156 return PJ_FALSE;
157
158 return PJ_TRUE;
159}
160
161/*
162 * Print account status.
163 */
164static void print_acc_status(int acc_id)
165{
166 char buf[80];
167 pjsua_acc_info info;
168
169 pjsua_acc_get_info(acc_id, &info);
170
171 if (!info.has_registration) {
172 pj_ansi_snprintf(buf, sizeof(buf), "%.*s",
173 (int)info.status_text.slen,
174 info.status_text.ptr);
175
176 } else {
177 pj_ansi_snprintf(buf, sizeof(buf),
178 "%d/%.*s (expires=%d)",
179 info.status,
180 (int)info.status_text.slen,
181 info.status_text.ptr,
182 info.expires);
183
184 }
185
186 printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '),
187 acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf);
188 printf(" Online status: %.*s\n",
189 (int)info.online_status_text.slen,
190 info.online_status_text.ptr);
191}
192
193/*
194 * Show a bit of help.
195 */
196static void keystroke_help()
197{
198 pjsua_acc_id acc_ids[16];
199 unsigned count = PJ_ARRAY_SIZE(acc_ids);
200 int i;
201
202 printf(">>>>\n");
203
204 pjsua_enum_accs(acc_ids, &count);
205
206 printf("Account list:\n");
207 for (i=0; i<(int)count; ++i)
208 print_acc_status(acc_ids[i]);
209
210 print_buddy_list();
211
212 //puts("Commands:");
213 puts("+=============================================================================+");
214 puts("| Call Commands: | Buddy, IM & Presence: | Account: |");
215 puts("| | | |");
216 puts("| m Make new call | +b Add new buddy .| +a Add new accnt |");
217 puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |");
218 puts("| a Answer call | i Send IM | !a Modify accnt. |");
219 puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |");
220 puts("| H Hold call | u Unsubscribe presence | ru Unregister |");
221 puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|");
222 puts("| U send UPDATE | T Set online status | < Cycle prev ac.|");
223 puts("| ],[ Select next/prev call +--------------------------+-------------------+");
224 puts("| x Xfer call | Media Commands: | Status & Config: |");
225 puts("| X Xfer with Replaces | | |");
226 puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |");
227 puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |");
228 puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |");
229 puts("| | V Adjust audio Volume | f Save config |");
230 puts("| S Send arbitrary REQUEST | Cp Codec priorities | |");
231 puts("+-----------------------------------------------------------------------------+");
232#if PJSUA_HAS_VIDEO
233 puts("| Video: \"vid help\" for more info |");
234 puts("+-----------------------------------------------------------------------------+");
235#endif
236 puts("| q QUIT L ReLoad sleep MS echo [0|1|txt] n: detect NAT type |");
237 puts("+=============================================================================+");
238
239 i = pjsua_call_get_count();
240 printf("You have %d active call%s\n", i, (i>1?"s":""));
241
242 if (current_call != PJSUA_INVALID_ID) {
243 pjsua_call_info ci;
244 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS)
245 printf("Current call id=%d to %.*s [%.*s]\n", current_call,
246 (int)ci.remote_info.slen, ci.remote_info.ptr,
247 (int)ci.state_text.slen, ci.state_text.ptr);
248 }
249}
250
251/* Help screen for video */
252#if PJSUA_HAS_VIDEO
253static void vid_show_help()
254{
255 pj_bool_t vid_enabled = (app_config.vid.vid_cnt > 0);
256
257 puts("+=============================================================================+");
258 puts("| Video commands: |");
259 puts("| |");
260 puts("| vid help Show this help screen |");
261 puts("| vid enable|disable Enable or disable video in next offer/answer |");
262 puts("| vid acc show Show current account video settings |");
263 puts("| vid acc autorx on|off Automatically show incoming video on/off |");
264 puts("| vid acc autotx on|off Automatically offer video on/off |");
265 puts("| vid acc cap ID Set default capture device for current acc |");
266 puts("| vid acc rend ID Set default renderer device for current acc |");
267 puts("| vid call rx on|off N Enable/disable video RX for stream N in curr call |");
268 puts("| vid call tx on|off N Enable/disable video TX for stream N in curr call |");
269 puts("| vid call add Add video stream for current call |");
270 puts("| vid call enable|disable N Enable/disable stream #N in current call |");
271 puts("| vid call cap N ID Set capture dev ID for stream #N in current call |");
272 puts("| vid dev list List all video devices |");
273 puts("| vid dev refresh Refresh video device list |");
274 puts("| vid dev prev on|off ID Enable/disable preview for specified device ID |");
275 puts("| vid codec list List video codecs |");
276 puts("| vid codec prio ID PRIO Set codec ID priority to PRIO |");
277 puts("| vid codec fps ID NUM DEN Set codec ID framerate to (NUM/DEN) fps |");
278 puts("| vid codec bw ID AVG MAX Set codec ID bitrate to AVG & MAX kbps |");
279 puts("| vid codec size ID W H Set codec ID size/resolution to W x H |");
280 puts("| vid win list List all active video windows |");
281 puts("| vid win arrange Auto arrange windows |");
282 puts("| vid win show|hide ID Show/hide the specified video window ID |");
283 puts("| vid win move ID X Y Move window ID to position X,Y |");
284 puts("| vid win resize ID w h Resize window ID to the specified width, height |");
285 puts("+=============================================================================+");
286 printf("| Video will be %s in the next offer/answer %s |\n",
287 (vid_enabled? "enabled" : "disabled"), (vid_enabled? " " : ""));
288 puts("+=============================================================================+");
289}
290
291static void vid_handle_menu(char *menuin)
292{
293 char *argv[8];
294 int argc = 0;
295
296 /* Tokenize */
297 argv[argc] = strtok(menuin, " \t\r\n");
298 while (argv[argc] && *argv[argc]) {
299 argc++;
300 argv[argc] = strtok(NULL, " \t\r\n");
301 }
302
303 if (argc == 1 || strcmp(argv[1], "help")==0) {
304 vid_show_help();
305 } else if (argc == 2 && (strcmp(argv[1], "enable")==0 ||
306 strcmp(argv[1], "disable")==0))
307 {
308 pj_bool_t enabled = (strcmp(argv[1], "enable")==0);
309 app_config.vid.vid_cnt = (enabled ? 1 : 0);
310 PJ_LOG(3,(THIS_FILE, "Video will be %s in next offer/answer",
311 (enabled?"enabled":"disabled")));
312 } else if (strcmp(argv[1], "acc")==0) {
313 pjsua_acc_config acc_cfg;
314 pj_bool_t changed = PJ_FALSE;
315
316 pjsua_acc_get_config(current_acc, &acc_cfg);
317
318 if (argc == 3 && strcmp(argv[2], "show")==0) {
319 app_config_show_video(current_acc, &acc_cfg);
320 } else if (argc == 4 && strcmp(argv[2], "autorx")==0) {
321 int on = (strcmp(argv[3], "on")==0);
322 acc_cfg.vid_in_auto_show = on;
323 changed = PJ_TRUE;
324 } else if (argc == 4 && strcmp(argv[2], "autotx")==0) {
325 int on = (strcmp(argv[3], "on")==0);
326 acc_cfg.vid_out_auto_transmit = on;
327 changed = PJ_TRUE;
328 } else if (argc == 4 && strcmp(argv[2], "cap")==0) {
329 int dev = atoi(argv[3]);
330 acc_cfg.vid_cap_dev = dev;
331 changed = PJ_TRUE;
332 } else if (argc == 4 && strcmp(argv[2], "rend")==0) {
333 int dev = atoi(argv[3]);
334 acc_cfg.vid_rend_dev = dev;
335 changed = PJ_TRUE;
336 } else {
337 goto on_error;
338 }
339
340 if (changed) {
341 pj_status_t status = pjsua_acc_modify(current_acc, &acc_cfg);
342 if (status != PJ_SUCCESS)
343 PJ_PERROR(1,(THIS_FILE, status, "Error modifying account %d",
344 current_acc));
345 }
346
347 } else if (strcmp(argv[1], "call")==0) {
348 pjsua_call_vid_strm_op_param param;
349 pj_status_t status = PJ_SUCCESS;
350
351 pjsua_call_vid_strm_op_param_default(&param);
352
353 if (argc == 5 && strcmp(argv[2], "rx")==0) {
354 pjsua_stream_info si;
355 pj_bool_t on = (strcmp(argv[3], "on") == 0);
356
357 param.med_idx = atoi(argv[4]);
358 if (pjsua_call_get_stream_info(current_call, param.med_idx, &si) ||
359 si.type != PJMEDIA_TYPE_VIDEO)
360 {
361 PJ_PERROR(1,(THIS_FILE, PJ_EINVAL, "Invalid stream"));
362 return;
363 }
364
365 if (on) param.dir = (si.info.vid.dir | PJMEDIA_DIR_DECODING);
366 else param.dir = (si.info.vid.dir & PJMEDIA_DIR_ENCODING);
367
368 status = pjsua_call_set_vid_strm(current_call,
369 PJSUA_CALL_VID_STRM_CHANGE_DIR,
370 &param);
371 }
372 else if (argc == 5 && strcmp(argv[2], "tx")==0) {
373 pj_bool_t on = (strcmp(argv[3], "on") == 0);
374 pjsua_call_vid_strm_op op = on? PJSUA_CALL_VID_STRM_START_TRANSMIT :
375 PJSUA_CALL_VID_STRM_STOP_TRANSMIT;
376
377 param.med_idx = atoi(argv[4]);
378
379 status = pjsua_call_set_vid_strm(current_call, op, &param);
380 }
381 else if (argc == 3 && strcmp(argv[2], "add")==0) {
382 status = pjsua_call_set_vid_strm(current_call,
383 PJSUA_CALL_VID_STRM_ADD, NULL);
384 }
385 else if (argc >= 3 &&
386 (strcmp(argv[2], "disable")==0 || strcmp(argv[2], "enable")==0))
387 {
388 pj_bool_t enable = (strcmp(argv[2], "enable") == 0);
389 pjsua_call_vid_strm_op op = enable? PJSUA_CALL_VID_STRM_CHANGE_DIR :
390 PJSUA_CALL_VID_STRM_REMOVE;
391
392 param.med_idx = argc >= 4? atoi(argv[3]) : -1;
393 param.dir = PJMEDIA_DIR_ENCODING_DECODING;
394 status = pjsua_call_set_vid_strm(current_call, op, &param);
395 }
396 else if (argc >= 3 && strcmp(argv[2], "cap")==0) {
397 param.med_idx = argc >= 4? atoi(argv[3]) : -1;
398 param.cap_dev = argc >= 5? atoi(argv[4]) : PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
399 status = pjsua_call_set_vid_strm(current_call,
400 PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV,
401 &param);
402 } else
403 goto on_error;
404
405 if (status != PJ_SUCCESS) {
406 PJ_PERROR(1,(THIS_FILE, status, "Error modifying video stream"));
407 }
408
409 } else if (argc >= 3 && strcmp(argv[1], "dev")==0) {
410 if (strcmp(argv[2], "list")==0) {
411 vid_list_devs();
412 } else if (strcmp(argv[2], "refresh")==0) {
413 pjmedia_vid_dev_refresh();
414 } else if (strcmp(argv[2], "prev")==0) {
415 if (argc != 5) {
416 goto on_error;
417 } else {
418 pj_bool_t on = (strcmp(argv[3], "on") == 0);
419 int dev_id = atoi(argv[4]);
420 if (on) {
421 pjsua_vid_preview_param param;
422
423 pjsua_vid_preview_param_default(&param);
424 param.wnd_flags = PJMEDIA_VID_DEV_WND_BORDER |
425 PJMEDIA_VID_DEV_WND_RESIZABLE;
426 pjsua_vid_preview_start(dev_id, &param);
427 arrange_window(pjsua_vid_preview_get_win(dev_id));
428 } else {
429 pjsua_vid_win_id wid;
430 wid = pjsua_vid_preview_get_win(dev_id);
431 if (wid != PJSUA_INVALID_ID) {
432 /* Preview window hiding once it is stopped is
433 * responsibility of app */
434 pjsua_vid_win_set_show(wid, PJ_FALSE);
435 pjsua_vid_preview_stop(dev_id);
436 }
437 }
438 }
439 } else
440 goto on_error;
441 } else if (strcmp(argv[1], "win")==0) {
442 pj_status_t status = PJ_SUCCESS;
443
444 if (argc==3 && strcmp(argv[2], "list")==0) {
445 pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS];
446 unsigned i, cnt = PJ_ARRAY_SIZE(wids);
447
448 pjsua_vid_enum_wins(wids, &cnt);
449
450 PJ_LOG(3,(THIS_FILE, "Found %d video windows:", cnt));
451 PJ_LOG(3,(THIS_FILE, "WID show pos size"));
452 PJ_LOG(3,(THIS_FILE, "------------------------------"));
453 for (i = 0; i < cnt; ++i) {
454 pjsua_vid_win_info wi;
455 pjsua_vid_win_get_info(wids[i], &wi);
456 PJ_LOG(3,(THIS_FILE, "%3d %c (%d,%d) %dx%d",
457 wids[i], (wi.show?'Y':'N'), wi.pos.x, wi.pos.y,
458 wi.size.w, wi.size.h));
459 }
460 } else if (argc==4 && (strcmp(argv[2], "show")==0 ||
461 strcmp(argv[2], "hide")==0))
462 {
463 pj_bool_t show = (strcmp(argv[2], "show")==0);
464 pjsua_vid_win_id wid = atoi(argv[3]);
465 status = pjsua_vid_win_set_show(wid, show);
466 } else if (argc==6 && strcmp(argv[2], "move")==0) {
467 pjsua_vid_win_id wid = atoi(argv[3]);
468 pjmedia_coord pos;
469
470 pos.x = atoi(argv[4]);
471 pos.y = atoi(argv[5]);
472 status = pjsua_vid_win_set_pos(wid, &pos);
473 } else if (argc==6 && strcmp(argv[2], "resize")==0) {
474 pjsua_vid_win_id wid = atoi(argv[3]);
475 pjmedia_rect_size size;
476
477 size.w = atoi(argv[4]);
478 size.h = atoi(argv[5]);
479 status = pjsua_vid_win_set_size(wid, &size);
480 } else if (argc==3 && strcmp(argv[2], "arrange")==0) {
481 arrange_window(PJSUA_INVALID_ID);
482 } else
483 goto on_error;
484
485 if (status != PJ_SUCCESS) {
486 PJ_PERROR(1,(THIS_FILE, status, "Window operation error"));
487 }
488
489 } else if (strcmp(argv[1], "codec")==0) {
490 pjsua_codec_info ci[PJMEDIA_CODEC_MGR_MAX_CODECS];
491 unsigned count = PJ_ARRAY_SIZE(ci);
492 pj_status_t status;
493
494 if (argc==3 && strcmp(argv[2], "list")==0) {
495 status = pjsua_vid_enum_codecs(ci, &count);
496 if (status != PJ_SUCCESS) {
497 PJ_PERROR(1,(THIS_FILE, status, "Error enumerating codecs"));
498 } else {
499 unsigned i;
500 PJ_LOG(3,(THIS_FILE, "Found %d video codecs:", count));
501 PJ_LOG(3,(THIS_FILE, "codec id prio fps bw(kbps) size"));
502 PJ_LOG(3,(THIS_FILE, "------------------------------------------"));
503 for (i=0; i<count; ++i) {
504 pjmedia_vid_codec_param cp;
505 pjmedia_video_format_detail *vfd;
506
507 status = pjsua_vid_codec_get_param(&ci[i].codec_id, &cp);
508 if (status != PJ_SUCCESS)
509 continue;
510
511 vfd = pjmedia_format_get_video_format_detail(&cp.enc_fmt,
512 PJ_TRUE);
513 PJ_LOG(3,(THIS_FILE, "%.*s%.*s %3d %7.2f %4d/%4d %dx%d",
514 (int)ci[i].codec_id.slen, ci[i].codec_id.ptr,
515 13-(int)ci[i].codec_id.slen, " ",
516 ci[i].priority,
517 (vfd->fps.num*1.0/vfd->fps.denum),
518 vfd->avg_bps/1000, vfd->max_bps/1000,
519 vfd->size.w, vfd->size.h));
520 }
521 }
522 } else if (argc==5 && strcmp(argv[2], "prio")==0) {
523 pj_str_t cid;
524 int prio;
525 cid = pj_str(argv[3]);
526 prio = atoi(argv[4]);
527 status = pjsua_vid_codec_set_priority(&cid, (pj_uint8_t)prio);
528 if (status != PJ_SUCCESS)
529 PJ_PERROR(1,(THIS_FILE, status, "Set codec priority error"));
530 } else if (argc==6 && strcmp(argv[2], "fps")==0) {
531 pjmedia_vid_codec_param cp;
532 pj_str_t cid;
533 int M, N;
534 cid = pj_str(argv[3]);
535 M = atoi(argv[4]);
536 N = atoi(argv[5]);
537 status = pjsua_vid_codec_get_param(&cid, &cp);
538 if (status == PJ_SUCCESS) {
539 cp.enc_fmt.det.vid.fps.num = M;
540 cp.enc_fmt.det.vid.fps.denum = N;
541 status = pjsua_vid_codec_set_param(&cid, &cp);
542 }
543 if (status != PJ_SUCCESS)
544 PJ_PERROR(1,(THIS_FILE, status, "Set codec framerate error"));
545 } else if (argc==6 && strcmp(argv[2], "bw")==0) {
546 pjmedia_vid_codec_param cp;
547 pj_str_t cid;
548 int M, N;
549 cid = pj_str(argv[3]);
550 M = atoi(argv[4]);
551 N = atoi(argv[5]);
552 status = pjsua_vid_codec_get_param(&cid, &cp);
553 if (status == PJ_SUCCESS) {
554 cp.enc_fmt.det.vid.avg_bps = M * 1000;
555 cp.enc_fmt.det.vid.max_bps = N * 1000;
556 status = pjsua_vid_codec_set_param(&cid, &cp);
557 }
558 if (status != PJ_SUCCESS)
559 PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error"));
560 } else if (argc==6 && strcmp(argv[2], "size")==0) {
561 pjmedia_vid_codec_param cp;
562 pj_str_t cid;
563 int M, N;
564 cid = pj_str(argv[3]);
565 M = atoi(argv[4]);
566 N = atoi(argv[5]);
567 status = pjsua_vid_codec_get_param(&cid, &cp);
568 if (status == PJ_SUCCESS) {
569 cp.enc_fmt.det.vid.size.w = M;
570 cp.enc_fmt.det.vid.size.h = N;
571 status = pjsua_vid_codec_set_param(&cid, &cp);
572 }
573 if (status != PJ_SUCCESS)
574 PJ_PERROR(1,(THIS_FILE, status, "Set codec size error"));
575 } else
576 goto on_error;
577 } else
578 goto on_error;
579
580 return;
581
582on_error:
583 PJ_LOG(1,(THIS_FILE, "Invalid command, use 'vid help'"));
584}
585
586#endif /* PJSUA_HAS_VIDEO */
587
588/** UI Command **/
589static void ui_make_new_call()
590{
591 char buf[128];
592 pjsua_msg_data msg_data;
593 input_result result;
594 pj_str_t tmp;
595
596 printf("(You currently have %d calls)\n", pjsua_call_get_count());
597
598 ui_input_url("Make call", buf, sizeof(buf), &result);
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +0000599 if (result.nb_result != PJSUA_APP_NO_NB) {
Benny Prijono8e4a1132013-04-03 22:41:23 +0000600
601 if (result.nb_result == -1 || result.nb_result == 0) {
602 puts("You can't do that with make call!");
603 return;
604 } else {
605 pjsua_buddy_info binfo;
606 pjsua_buddy_get_info(result.nb_result-1, &binfo);
607 tmp.ptr = buf;
608 pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
609 }
610
611 } else if (result.uri_result) {
612 tmp = pj_str(result.uri_result);
613 } else {
614 tmp.slen = 0;
615 }
616
617 pjsua_msg_data_init(&msg_data);
618 TEST_MULTIPART(&msg_data);
619 pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL,
620 &msg_data, &current_call);
621}
622
623static void ui_make_multi_call()
624{
625 char menuin[32];
626 int count;
627 char buf[128];
628 input_result result;
629 pj_str_t tmp;
630 int i;
631
632 printf("(You currently have %d calls)\n", pjsua_call_get_count());
633
634 if (!simple_input("Number of calls", menuin, sizeof(menuin)))
635 return;
636
637 count = my_atoi(menuin);
638 if (count < 1)
639 return;
640
641 ui_input_url("Make call", buf, sizeof(buf), &result);
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +0000642 if (result.nb_result != PJSUA_APP_NO_NB) {
Benny Prijono8e4a1132013-04-03 22:41:23 +0000643 pjsua_buddy_info binfo;
644 if (result.nb_result == -1 || result.nb_result == 0) {
645 puts("You can't do that with make call!");
646 return;
647 }
648 pjsua_buddy_get_info(result.nb_result-1, &binfo);
649 tmp.ptr = buf;
650 pj_strncpy(&tmp, &binfo.uri, sizeof(buf));
651 } else {
652 tmp = pj_str(result.uri_result);
653 }
654
655 for (i=0; i<my_atoi(menuin); ++i) {
656 pj_status_t status;
657
658 status = pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL,
659 NULL, NULL);
660 if (status != PJ_SUCCESS)
661 break;
662 }
663}
664
665static void ui_detect_nat_type()
666{
667 int i = pjsua_detect_nat_type();
668 if (i != PJ_SUCCESS)
669 pjsua_perror(THIS_FILE, "Error", i);
670}
671
672static void ui_send_instant_message()
673{
674 char *uri = NULL;
675 /* i is for call index to send message, if any */
676 int i = -1;
677 input_result result;
678 char buf[128];
679 char text[128];
680 pj_str_t tmp;
681
682 /* Input destination. */
683 ui_input_url("Send IM to", buf, sizeof(buf), &result);
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +0000684 if (result.nb_result != PJSUA_APP_NO_NB) {
Benny Prijono8e4a1132013-04-03 22:41:23 +0000685
686 if (result.nb_result == -1) {
687 puts("You can't send broadcast IM like that!");
688 return;
689
690 } else if (result.nb_result == 0) {
691 i = current_call;
692 } else {
693 pjsua_buddy_info binfo;
694 pjsua_buddy_get_info(result.nb_result-1, &binfo);
695 tmp.ptr = buf;
696 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
697 uri = buf;
698 }
699
700 } else if (result.uri_result) {
701 uri = result.uri_result;
702 }
703
704
705 /* Send typing indication. */
706 if (i != -1)
707 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
708 else {
709 pj_str_t tmp_uri = pj_str(uri);
710 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
711 }
712
713 /* Input the IM . */
714 if (!simple_input("Message", text, sizeof(text))) {
715 /*
716 * Cancelled.
717 * Send typing notification too, saying we're not typing.
718 */
719 if (i != -1)
720 pjsua_call_send_typing_ind(i, PJ_FALSE, NULL);
721 else {
722 pj_str_t tmp_uri = pj_str(uri);
723 pjsua_im_typing(current_acc, &tmp_uri, PJ_FALSE, NULL);
724 }
725 return;
726 }
727
728 tmp = pj_str(text);
729
730 /* Send the IM */
731 if (i != -1)
732 pjsua_call_send_im(i, NULL, &tmp, NULL, NULL);
733 else {
734 pj_str_t tmp_uri = pj_str(uri);
735 pjsua_im_send(current_acc, &tmp_uri, NULL, &tmp, NULL, NULL);
736 }
737}
738
739static void ui_answer_call()
740{
741 pjsua_call_info call_info;
742 char buf[128];
743 pjsua_msg_data msg_data;
744
745 if (current_call != -1) {
746 pjsua_call_get_info(current_call, &call_info);
747 } else {
748 /* Make compiler happy */
749 call_info.role = PJSIP_ROLE_UAC;
750 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
751 }
752
753 if (current_call == -1 ||
754 call_info.role != PJSIP_ROLE_UAS ||
755 call_info.state >= PJSIP_INV_STATE_CONNECTING)
756 {
757 puts("No pending incoming call");
758 fflush(stdout);
759 return;
760
761 } else {
762 int st_code;
763 char contact[120];
764 pj_str_t hname = { "Contact", 7 };
765 pj_str_t hvalue;
766 pjsip_generic_string_hdr hcontact;
767
768 if (!simple_input("Answer with code (100-699)", buf, sizeof(buf)))
769 return;
770
771 st_code = my_atoi(buf);
772 if (st_code < 100)
773 return;
774
775 pjsua_msg_data_init(&msg_data);
776
777 if (st_code/100 == 3) {
778 if (!simple_input("Enter URL to be put in Contact",
779 contact, sizeof(contact)))
780 return;
781 hvalue = pj_str(contact);
782 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
783
784 pj_list_push_back(&msg_data.hdr_list, &hcontact);
785 }
786
787 /*
788 * Must check again!
789 * Call may have been disconnected while we're waiting for
790 * keyboard input.
791 */
792 if (current_call == -1) {
793 puts("Call has been disconnected");
794 fflush(stdout);
795 return;
796 }
797
798 pjsua_call_answer2(current_call, &call_opt, st_code, NULL, &msg_data);
799 }
800}
801
802static void ui_hangup_call(char menuin[])
803{
804 if (current_call == -1) {
805 puts("No current call");
806 fflush(stdout);
807 return;
808
809 } else if (menuin[1] == 'a') {
810 /* Hangup all calls */
811 pjsua_call_hangup_all();
812 } else {
813 /* Hangup current calls */
814 pjsua_call_hangup(current_call, 0, NULL, NULL);
815 }
816}
817
818static void ui_cycle_dialog(char menuin[])
819{
820 if (menuin[0] == ']') {
821 find_next_call();
822
823 } else {
824 find_prev_call();
825 }
826
827 if (current_call != -1) {
828 pjsua_call_info call_info;
829
830 pjsua_call_get_info(current_call, &call_info);
831 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
832 (int)call_info.remote_info.slen,
833 call_info.remote_info.ptr));
834
835 } else {
836 PJ_LOG(3,(THIS_FILE,"No current dialog"));
837 }
838}
839
840static void ui_cycle_account()
841{
842 int i;
843 char buf[128];
844
845 if (!simple_input("Enter account ID to select", buf, sizeof(buf)))
846 return;
847
848 i = my_atoi(buf);
849 if (pjsua_acc_is_valid(i)) {
850 pjsua_acc_set_default(i);
851 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
852 } else {
853 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
854 }
855}
856
857static void ui_add_buddy()
858{
859 char buf[128];
860 pjsua_buddy_config buddy_cfg;
861 pjsua_buddy_id buddy_id;
862 pj_status_t status;
863
864 if (!simple_input("Enter buddy's URI:", buf, sizeof(buf)))
865 return;
866
867 if (pjsua_verify_url(buf) != PJ_SUCCESS) {
868 printf("Invalid URI '%s'\n", buf);
869 return;
870 }
871
872 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
873
874 buddy_cfg.uri = pj_str(buf);
875 buddy_cfg.subscribe = PJ_TRUE;
876
877 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
878 if (status == PJ_SUCCESS) {
879 printf("New buddy '%s' added at index %d\n",
880 buf, buddy_id+1);
881 }
882}
883
884static void ui_add_account(pjsua_transport_config *rtp_cfg)
885{
886 char id[80], registrar[80], realm[80], uname[80], passwd[30];
887 pjsua_acc_config acc_cfg;
888 pj_status_t status;
889
890 if (!simple_input("Your SIP URL:", id, sizeof(id)))
891 return;
892 if (!simple_input("URL of the registrar:", registrar, sizeof(registrar)))
893 return;
894 if (!simple_input("Auth Realm:", realm, sizeof(realm)))
895 return;
896 if (!simple_input("Auth Username:", uname, sizeof(uname)))
897 return;
898 if (!simple_input("Auth Password:", passwd, sizeof(passwd)))
899 return;
900
901 pjsua_acc_config_default(&acc_cfg);
902 acc_cfg.id = pj_str(id);
903 acc_cfg.reg_uri = pj_str(registrar);
904 acc_cfg.cred_count = 1;
905 acc_cfg.cred_info[0].scheme = pj_str("Digest");
906 acc_cfg.cred_info[0].realm = pj_str(realm);
907 acc_cfg.cred_info[0].username = pj_str(uname);
908 acc_cfg.cred_info[0].data_type = 0;
909 acc_cfg.cred_info[0].data = pj_str(passwd);
910
911 acc_cfg.rtp_cfg = *rtp_cfg;
912 app_config_init_video(&acc_cfg);
913
914 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
915 if (status != PJ_SUCCESS) {
916 pjsua_perror(THIS_FILE, "Error adding new account", status);
917 }
918}
919
920static void ui_delete_buddy()
921{
922 char buf[128];
923 int i;
924
925 if (!simple_input("Enter buddy ID to delete", buf, sizeof(buf)))
926 return;
927
928 i = my_atoi(buf) - 1;
929
930 if (!pjsua_buddy_is_valid(i)) {
931 printf("Invalid buddy id %d\n", i);
932 } else {
933 pjsua_buddy_del(i);
934 printf("Buddy %d deleted\n", i);
935 }
936}
937
938static void ui_delete_account()
939{
940 char buf[128];
941 int i;
942
943 if (!simple_input("Enter account ID to delete", buf, sizeof(buf)))
944 return;
945
946 i = my_atoi(buf);
947
948 if (!pjsua_acc_is_valid(i)) {
949 printf("Invalid account id %d\n", i);
950 } else {
951 pjsua_acc_del(i);
952 printf("Account %d deleted\n", i);
953 }
954}
955
956static void ui_call_hold()
957{
958 if (current_call != -1) {
959 pjsua_call_set_hold(current_call, NULL);
960 } else {
961 PJ_LOG(3,(THIS_FILE, "No current call"));
962 }
963}
964
965static void ui_call_reinvite()
966{
967 call_opt.flag |= PJSUA_CALL_UNHOLD;
968 pjsua_call_reinvite2(current_call, &call_opt, NULL);
969}
970
971static void ui_send_update()
972{
Riza Sulistyobc9c6772013-04-05 03:02:19 +0000973 if (current_call != -1) {
Benny Prijono8e4a1132013-04-03 22:41:23 +0000974 pjsua_call_update2(current_call, &call_opt, NULL);
975 } else {
976 PJ_LOG(3,(THIS_FILE, "No current call"));
977 }
978}
979
980/*
981 * Change codec priorities.
982 */
983static void ui_manage_codec_prio()
984{
985 pjsua_codec_info c[32];
986 unsigned i, count = PJ_ARRAY_SIZE(c);
987 char input[32];
988 char *codec, *prio;
989 pj_str_t id;
990 int new_prio;
991 pj_status_t status;
992
993 printf("List of audio codecs:\n");
994 pjsua_enum_codecs(c, &count);
995 for (i=0; i<count; ++i) {
996 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
997 c[i].codec_id.ptr);
998 }
999
1000#if PJSUA_HAS_VIDEO
1001 puts("");
1002 printf("List of video codecs:\n");
1003 pjsua_vid_enum_codecs(c, &count);
1004 for (i=0; i<count; ++i) {
1005 printf(" %d\t%.*s%s%.*s\n", c[i].priority,
1006 (int)c[i].codec_id.slen,
1007 c[i].codec_id.ptr,
1008 c[i].desc.slen? " - ":"",
1009 (int)c[i].desc.slen,
1010 c[i].desc.ptr);
1011 }
1012#endif
1013
1014 puts("");
1015 puts("Enter codec id and its new priority (e.g. \"speex/16000 200\", "
1016 """\"H263 200\"),");
1017 puts("or empty to cancel.");
1018
1019 printf("Codec name (\"*\" for all) and priority: ");
1020 if (fgets(input, sizeof(input), stdin) == NULL)
1021 return;
1022 if (input[0]=='\r' || input[0]=='\n') {
1023 puts("Done");
1024 return;
1025 }
1026
1027 codec = strtok(input, " \t\r\n");
1028 prio = strtok(NULL, " \r\n");
1029
1030 if (!codec || !prio) {
1031 puts("Invalid input");
1032 return;
1033 }
1034
1035 new_prio = atoi(prio);
1036 if (new_prio < 0)
1037 new_prio = 0;
1038 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
1039 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
1040
1041 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
1042 (pj_uint8_t)new_prio);
1043#if PJSUA_HAS_VIDEO
1044 if (status != PJ_SUCCESS) {
1045 status = pjsua_vid_codec_set_priority(pj_cstr(&id, codec),
1046 (pj_uint8_t)new_prio);
1047 }
1048#endif
1049 if (status != PJ_SUCCESS)
1050 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
1051}
1052
1053static void ui_call_transfer(pj_bool_t no_refersub)
1054{
1055 if (current_call == -1) {
1056 PJ_LOG(3,(THIS_FILE, "No current call"));
1057 } else {
1058 int call = current_call;
1059 char buf[128];
1060 pjsip_generic_string_hdr refer_sub;
1061 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
1062 pj_str_t STR_FALSE = { "false", 5 };
1063 pjsua_call_info ci;
1064 input_result result;
1065 pjsua_msg_data msg_data;
1066
1067 pjsua_call_get_info(current_call, &ci);
1068 printf("Transfering current call [%d] %.*s\n", current_call,
1069 (int)ci.remote_info.slen, ci.remote_info.ptr);
1070
1071 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
1072
1073 /* Check if call is still there. */
1074
1075 if (call != current_call) {
1076 puts("Call has been disconnected");
1077 return;
1078 }
1079
1080 pjsua_msg_data_init(&msg_data);
1081 if (no_refersub) {
1082 /* Add Refer-Sub: false in outgoing REFER request */
1083 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
1084 &STR_FALSE);
1085 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
1086 }
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +00001087 if (result.nb_result != PJSUA_APP_NO_NB) {
Benny Prijono8e4a1132013-04-03 22:41:23 +00001088 if (result.nb_result == -1 || result.nb_result == 0)
1089 puts("You can't do that with transfer call!");
1090 else {
1091 pjsua_buddy_info binfo;
1092 pjsua_buddy_get_info(result.nb_result-1, &binfo);
1093 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
1094 }
1095
1096 } else if (result.uri_result) {
1097 pj_str_t tmp;
1098 tmp = pj_str(result.uri_result);
1099 pjsua_call_xfer( current_call, &tmp, &msg_data);
1100 }
1101 }
1102}
1103
1104static void ui_call_transfer_replaces(pj_bool_t no_refersub)
1105{
1106 if (current_call == -1) {
1107 PJ_LOG(3,(THIS_FILE, "No current call"));
1108 } else {
1109 int call = current_call;
1110 int dst_call;
1111 pjsip_generic_string_hdr refer_sub;
1112 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
1113 pj_str_t STR_FALSE = { "false", 5 };
1114 pjsua_call_id ids[PJSUA_MAX_CALLS];
1115 pjsua_call_info ci;
1116 pjsua_msg_data msg_data;
1117 char buf[128];
1118 unsigned i, count;
1119
1120 count = PJ_ARRAY_SIZE(ids);
1121 pjsua_enum_calls(ids, &count);
1122
1123 if (count <= 1) {
1124 puts("There are no other calls");
1125 return;
1126 }
1127
1128 pjsua_call_get_info(current_call, &ci);
1129 printf("Transfer call [%d] %.*s to one of the following:\n",
1130 current_call,
1131 (int)ci.remote_info.slen, ci.remote_info.ptr);
1132
1133 for (i=0; i<count; ++i) {
1134 pjsua_call_info call_info;
1135
1136 if (ids[i] == call)
1137 return;
1138
1139 pjsua_call_get_info(ids[i], &call_info);
1140 printf("%d %.*s [%.*s]\n",
1141 ids[i],
1142 (int)call_info.remote_info.slen,
1143 call_info.remote_info.ptr,
1144 (int)call_info.state_text.slen,
1145 call_info.state_text.ptr);
1146 }
1147
1148 if (!simple_input("Enter call number to be replaced", buf, sizeof(buf)))
1149 return;
1150
1151 dst_call = my_atoi(buf);
1152
1153 /* Check if call is still there. */
1154
1155 if (call != current_call) {
1156 puts("Call has been disconnected");
1157 return;
1158 }
1159
1160 /* Check that destination call is valid. */
1161 if (dst_call == call) {
1162 puts("Destination call number must not be the same "
1163 "as the call being transfered");
1164 return;
1165 }
1166 if (dst_call >= PJSUA_MAX_CALLS) {
1167 puts("Invalid destination call number");
1168 return;
1169 }
1170 if (!pjsua_call_is_active(dst_call)) {
1171 puts("Invalid destination call number");
1172 return;
1173 }
1174
1175 pjsua_msg_data_init(&msg_data);
1176 if (no_refersub) {
1177 /* Add Refer-Sub: false in outgoing REFER request */
1178 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
1179 &STR_FALSE);
1180 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
1181 }
1182
1183 pjsua_call_xfer_replaces(call, dst_call,
1184 PJSUA_XFER_NO_REQUIRE_REPLACES,
1185 &msg_data);
1186 }
1187}
1188
1189static void ui_send_dtmf_2833()
1190{
1191 if (current_call == -1) {
1192 PJ_LOG(3,(THIS_FILE, "No current call"));
1193 } else if (!pjsua_call_has_media(current_call)) {
1194 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
1195 } else {
1196 pj_str_t digits;
1197 int call = current_call;
1198 pj_status_t status;
1199 char buf[128];
1200
1201 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
1202 sizeof(buf)))
1203 {
1204 return;
1205 }
1206
1207 if (call != current_call) {
1208 puts("Call has been disconnected");
1209 return;
1210 }
1211
1212 digits = pj_str(buf);
1213 status = pjsua_call_dial_dtmf(current_call, &digits);
1214 if (status != PJ_SUCCESS) {
1215 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
1216 } else {
1217 puts("DTMF digits enqueued for transmission");
1218 }
1219 }
1220}
1221
1222static void ui_send_dtmf_info()
1223{
1224 if (current_call == -1) {
1225 PJ_LOG(3,(THIS_FILE, "No current call"));
1226 } else {
1227 const pj_str_t SIP_INFO = pj_str("INFO");
1228 pj_str_t digits;
1229 int call = current_call;
1230 int i;
1231 pj_status_t status;
1232 char buf[128];
1233
1234 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
1235 sizeof(buf)))
1236 {
1237 return;
1238 }
1239
1240 if (call != current_call) {
1241 puts("Call has been disconnected");
1242 return;
1243 }
1244
1245 digits = pj_str(buf);
1246 for (i=0; i<digits.slen; ++i) {
1247 char body[80];
1248 pjsua_msg_data msg_data;
1249
1250 pjsua_msg_data_init(&msg_data);
1251 msg_data.content_type = pj_str("application/dtmf-relay");
1252
1253 pj_ansi_snprintf(body, sizeof(body),
1254 "Signal=%c\r\n"
1255 "Duration=160",
1256 buf[i]);
1257 msg_data.msg_body = pj_str(body);
1258
1259 status = pjsua_call_send_request(current_call, &SIP_INFO,
1260 &msg_data);
1261 if (status != PJ_SUCCESS) {
1262 return;
1263 }
1264 }
1265 }
1266}
1267
1268static void ui_send_arbitrary_request()
1269{
1270 char text[128];
1271 char buf[128];
1272 char *uri;
1273 input_result result;
1274 pj_str_t tmp;
1275
1276 if (pjsua_acc_get_count() == 0) {
1277 puts("Sorry, need at least one account configured");
1278 return;
1279 }
1280
1281 puts("Send arbitrary request to remote host");
1282
1283 /* Input METHOD */
1284 if (!simple_input("Request method:",text,sizeof(text)))
1285 return;
1286
1287 /* Input destination URI */
1288 uri = NULL;
1289 ui_input_url("Destination URI", buf, sizeof(buf), &result);
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +00001290 if (result.nb_result != PJSUA_APP_NO_NB) {
Benny Prijono8e4a1132013-04-03 22:41:23 +00001291
1292 if (result.nb_result == -1) {
1293 puts("Sorry you can't do that!");
1294 return;
1295 } else if (result.nb_result == 0) {
1296 uri = NULL;
1297 if (current_call == PJSUA_INVALID_ID) {
1298 puts("No current call");
1299 return;
1300 }
1301 } else {
1302 pjsua_buddy_info binfo;
1303 pjsua_buddy_get_info(result.nb_result-1, &binfo);
1304 tmp.ptr = buf;
1305 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
1306 uri = buf;
1307 }
1308
1309 } else if (result.uri_result) {
1310 uri = result.uri_result;
1311 } else {
1312 return;
1313 }
1314
1315 if (uri) {
1316 tmp = pj_str(uri);
1317 send_request(text, &tmp);
1318 } else {
1319 /* If you send call control request using this method
1320 * (such requests includes BYE, CANCEL, etc.), it will
1321 * not go well with the call state, so don't do it
1322 * unless it's for testing.
1323 */
1324 pj_str_t method = pj_str(text);
1325 pjsua_call_send_request(current_call, &method, NULL);
1326 }
1327}
1328
1329static void ui_echo(char menuin[])
1330{
1331 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
1332 pj_str_t tmp;
1333
1334 tmp.ptr = menuin+5;
1335 tmp.slen = pj_ansi_strlen(menuin)-6;
1336
1337 if (tmp.slen < 1) {
1338 puts("Usage: echo [0|1]");
1339 return;
1340 }
1341 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
1342 }
1343}
1344
1345static void ui_sleep(char menuin[])
1346{
1347 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
1348 pj_str_t tmp;
1349 int delay;
1350
1351 tmp.ptr = menuin+6;
1352 tmp.slen = pj_ansi_strlen(menuin)-7;
1353
1354 if (tmp.slen < 1) {
1355 puts("Usage: sleep MSEC");
1356 return;
1357 }
1358
1359 delay = pj_strtoul(&tmp);
1360 if (delay < 0) delay = 0;
1361 pj_thread_sleep(delay);
1362 }
1363}
1364
1365static void ui_subscribe(char menuin[])
1366{
1367 char buf[128];
1368 input_result result;
1369
1370 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +00001371 if (result.nb_result != PJSUA_APP_NO_NB) {
Benny Prijono8e4a1132013-04-03 22:41:23 +00001372 if (result.nb_result == -1) {
1373 int i, count;
1374 count = pjsua_get_buddy_count();
1375 for (i=0; i<count; ++i)
1376 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
1377 } else if (result.nb_result == 0) {
1378 puts("Sorry, can only subscribe to buddy's presence, "
1379 "not from existing call");
1380 } else {
1381 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
1382 }
1383
1384 } else if (result.uri_result) {
1385 puts("Sorry, can only subscribe to buddy's presence, "
1386 "not arbitrary URL (for now)");
1387 }
1388}
1389
1390static void ui_register(char menuin[])
1391{
1392 switch (menuin[1]) {
1393 case 'r':
1394 /*
1395 * Re-Register.
1396 */
1397 pjsua_acc_set_registration(current_acc, PJ_TRUE);
1398 break;
1399 case 'u':
1400 /*
1401 * Unregister
1402 */
1403 pjsua_acc_set_registration(current_acc, PJ_FALSE);
1404 break;
1405 }
1406}
1407
1408static void ui_toggle_state()
1409{
1410 pjsua_acc_info acc_info;
1411
1412 pjsua_acc_get_info(current_acc, &acc_info);
1413 acc_info.online_status = !acc_info.online_status;
1414 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
1415 printf("Setting %s online status to %s\n",
1416 acc_info.acc_uri.ptr,
1417 (acc_info.online_status?"online":"offline"));
1418}
1419
1420/*
1421 * Change extended online status.
1422 */
1423static void ui_change_online_status()
1424{
1425 char menuin[32];
1426 pj_bool_t online_status;
1427 pjrpid_element elem;
1428 int i, choice;
1429
1430 enum {
1431 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
1432 };
1433
1434 struct opt {
1435 int id;
1436 char *name;
1437 } opts[] = {
1438 { AVAILABLE, "Available" },
1439 { BUSY, "Busy"},
1440 { OTP, "On the phone"},
1441 { IDLE, "Idle"},
1442 { AWAY, "Away"},
1443 { BRB, "Be right back"},
1444 { OFFLINE, "Offline"}
1445 };
1446
1447 printf("\n"
1448 "Choices:\n");
1449 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
1450 printf(" %d %s\n", opts[i].id+1, opts[i].name);
1451 }
1452
1453 if (!simple_input("Select status", menuin, sizeof(menuin)))
1454 return;
1455
1456 choice = atoi(menuin) - 1;
1457 if (choice < 0 || choice >= OPT_MAX) {
1458 puts("Invalid selection");
1459 return;
1460 }
1461
1462 pj_bzero(&elem, sizeof(elem));
1463 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
1464
1465 online_status = PJ_TRUE;
1466
1467 switch (choice) {
1468 case AVAILABLE:
1469 break;
1470 case BUSY:
1471 elem.activity = PJRPID_ACTIVITY_BUSY;
1472 elem.note = pj_str("Busy");
1473 break;
1474 case OTP:
1475 elem.activity = PJRPID_ACTIVITY_BUSY;
1476 elem.note = pj_str("On the phone");
1477 break;
1478 case IDLE:
1479 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
1480 elem.note = pj_str("Idle");
1481 break;
1482 case AWAY:
1483 elem.activity = PJRPID_ACTIVITY_AWAY;
1484 elem.note = pj_str("Away");
1485 break;
1486 case BRB:
1487 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
1488 elem.note = pj_str("Be right back");
1489 break;
1490 case OFFLINE:
1491 online_status = PJ_FALSE;
1492 break;
1493 }
1494
1495 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
1496}
1497
1498/*
1499 * List the ports in conference bridge
1500 */
1501static void ui_conf_list()
1502{
1503 unsigned i, count;
1504 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
1505
1506 printf("Conference ports:\n");
1507
1508 count = PJ_ARRAY_SIZE(id);
1509 pjsua_enum_conf_ports(id, &count);
1510
1511 for (i=0; i<count; ++i) {
1512 char txlist[PJSUA_MAX_CALLS*4+10];
1513 unsigned j;
1514 pjsua_conf_port_info info;
1515
1516 pjsua_conf_get_port_info(id[i], &info);
1517
1518 txlist[0] = '\0';
1519 for (j=0; j<info.listener_cnt; ++j) {
1520 char s[10];
1521 pj_ansi_snprintf(s, sizeof(s), "#%d ", info.listeners[j]);
1522 pj_ansi_strcat(txlist, s);
1523 }
1524 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
1525 info.slot_id,
1526 info.clock_rate/1000,
1527 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
1528 info.channel_count,
1529 (int)info.name.slen,
1530 info.name.ptr,
1531 txlist);
1532
1533 }
1534 puts("");
1535}
1536
1537static void ui_conf_connect(char menuin[])
1538{
1539 char tmp[10], src_port[10], dst_port[10];
1540 pj_status_t status;
1541 int cnt;
1542 const char *src_title, *dst_title;
1543
1544 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
1545
1546 if (cnt != 3) {
1547 ui_conf_list();
1548
1549 src_title = (menuin[1]=='c'? "Connect src port #":
1550 "Disconnect src port #");
1551 dst_title = (menuin[1]=='c'? "To dst port #":"From dst port #");
1552
1553 if (!simple_input(src_title, src_port, sizeof(src_port)))
1554 return;
1555
1556 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
1557 return;
1558 }
1559
1560 if (menuin[1]=='c') {
1561 status = pjsua_conf_connect(my_atoi(src_port), my_atoi(dst_port));
1562 } else {
1563 status = pjsua_conf_disconnect(my_atoi(src_port), my_atoi(dst_port));
1564 }
1565 if (status == PJ_SUCCESS) {
1566 puts("Success");
1567 } else {
1568 puts("ERROR!!");
1569 }
1570}
1571
1572static void ui_adjust_volume()
1573{
1574 char buf[128];
1575 char text[128];
1576 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
1577 if (simple_input(buf,text,sizeof(text))) {
1578 char *err;
1579 app_config.mic_level = (float)strtod(text, &err);
1580 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
1581 }
1582 sprintf(buf, "Adjust speaker level: [%4.1fx] ", app_config.speaker_level);
1583 if (simple_input(buf,text,sizeof(text))) {
1584 char *err;
1585 app_config.speaker_level = (float)strtod(text, &err);
1586 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
1587 }
1588}
1589
1590static void ui_dump_call_quality()
1591{
1592 if (current_call != PJSUA_INVALID_ID) {
1593 log_call_dump(current_call);
1594 } else {
1595 PJ_LOG(3,(THIS_FILE, "No current call"));
1596 }
1597}
1598
1599static void ui_dump_configuration()
1600{
1601 char settings[2000];
1602 int len;
1603
1604 len = write_settings(&app_config, settings, sizeof(settings));
1605 if (len < 1)
1606 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
1607 else
1608 PJ_LOG(3,(THIS_FILE, "Dumping configuration (%d bytes):\n%s\n",
1609 len, settings));
1610}
1611
1612static void ui_write_settings()
1613{
1614 char settings[2000];
1615 int len;
1616 char buf[128];
1617
1618 len = write_settings(&app_config, settings, sizeof(settings));
1619 if (len < 1)
1620 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
1621 else {
1622 pj_oshandle_t fd;
1623 pj_status_t status;
1624
1625 status = pj_file_open(app_config.pool, buf, PJ_O_WRONLY, &fd);
1626 if (status != PJ_SUCCESS) {
1627 pjsua_perror(THIS_FILE, "Unable to open file", status);
1628 } else {
1629 pj_ssize_t size = len;
1630 pj_file_write(fd, settings, &size);
1631 pj_file_close(fd);
1632
1633 printf("Settings successfully written to '%s'\n", buf);
1634 }
1635 }
1636}
1637
1638/*
1639 * Dump application states.
1640 */
1641static void ui_app_dump(pj_bool_t detail)
1642{
1643 pjsua_dump(detail);
1644}
1645
1646static void ui_call_redirect(char menuin[])
1647{
1648 if (current_call == PJSUA_INVALID_ID) {
1649 PJ_LOG(3,(THIS_FILE, "No current call"));
1650 } else {
1651 if (!pjsua_call_is_active(current_call)) {
1652 PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
1653 } else if (menuin[1] == 'a') {
1654 pjsua_call_process_redirect(current_call,
1655 PJSIP_REDIRECT_ACCEPT_REPLACE);
1656 } else if (menuin[1] == 'A') {
1657 pjsua_call_process_redirect(current_call,
1658 PJSIP_REDIRECT_ACCEPT);
1659 } else if (menuin[1] == 'r') {
1660 pjsua_call_process_redirect(current_call,
1661 PJSIP_REDIRECT_REJECT);
1662 } else {
1663 pjsua_call_process_redirect(current_call,
1664 PJSIP_REDIRECT_STOP);
1665 }
1666 }
1667}
1668
1669/*
1670 * Main "user interface" loop.
1671 */
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +00001672PJ_DEF(void) legacy_main()
Benny Prijono8e4a1132013-04-03 22:41:23 +00001673{
1674 char menuin[80];
1675 char buf[128];
1676
Benny Prijono8e4a1132013-04-03 22:41:23 +00001677 keystroke_help(current_call);
1678
1679 for (;;) {
1680
1681 printf(">>> ");
1682 fflush(stdout);
1683
1684 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
1685 /*
1686 * Be friendly to users who redirect commands into
1687 * program, when file ends, resume with kbd.
1688 * If exit is desired end script with q for quit
1689 */
1690 /* Reopen stdin/stdout/stderr to /dev/console */
Nanang Izzuddinae5394d2013-04-19 10:36:11 +00001691#if defined(PJ_WIN32) && PJ_WIN32!=0 && \
1692 (!defined(PJ_WIN32_WINCE) || PJ_WIN32_WINCE==0)
Benny Prijono8e4a1132013-04-03 22:41:23 +00001693 if (freopen ("CONIN$", "r", stdin) == NULL) {
1694#else
1695 if (1) {
1696#endif
1697 puts("Cannot switch back to console from file redirection");
1698 menuin[0] = 'q';
1699 menuin[1] = '\0';
1700 } else {
1701 puts("Switched back to console from file redirection");
1702 continue;
1703 }
1704 }
1705
1706 if (cmd_echo) {
1707 printf("%s", menuin);
1708 }
1709
1710 /* Update call setting */
1711 pjsua_call_setting_default(&call_opt);
1712 call_opt.aud_cnt = app_config.aud_cnt;
1713 call_opt.vid_cnt = app_config.vid.vid_cnt;
1714
1715 switch (menuin[0]) {
1716
1717 case 'm':
1718 /* Make call! : */
1719 ui_make_new_call();
1720 break;
1721
1722 case 'M':
1723 /* Make multiple calls! : */
1724 ui_make_multi_call();
1725 break;
1726
1727 case 'n':
1728 ui_detect_nat_type();
1729 break;
1730
1731 case 'i':
1732 /* Send instant messaeg */
1733 ui_send_instant_message();
1734 break;
1735
1736 case 'a':
1737 ui_answer_call();
1738 break;
1739
1740 case 'h':
1741 ui_hangup_call(menuin);
1742 break;
1743
1744 case ']':
1745 case '[':
1746 /*
1747 * Cycle next/prev dialog.
1748 */
1749 ui_cycle_dialog(menuin);
1750 break;
1751
1752 case '>':
1753 case '<':
1754 ui_cycle_account();
1755 break;
1756
1757 case '+':
1758 if (menuin[1] == 'b') {
1759 ui_add_buddy();
1760 } else if (menuin[1] == 'a') {
1761 ui_add_account(&app_config.rtp_cfg);
1762 } else {
1763 printf("Invalid input %s\n", menuin);
1764 }
1765 break;
1766
1767 case '-':
1768 if (menuin[1] == 'b') {
1769 ui_delete_buddy();
1770 } else if (menuin[1] == 'a') {
1771 ui_delete_account();
1772 } else {
1773 printf("Invalid input %s\n", menuin);
1774 }
1775 break;
1776
1777 case 'H':
1778 /*
1779 * Hold call.
1780 */
1781 ui_call_hold();
1782 break;
1783
1784 case 'v':
1785#if PJSUA_HAS_VIDEO
1786 if (menuin[1]=='i' && menuin[2]=='d' && menuin[3]==' ') {
1787 vid_handle_menu(menuin);
1788 } else
1789#endif
1790 if (current_call != -1) {
1791 /*
1792 * re-INVITE
1793 */
1794 ui_call_reinvite();
1795 } else {
1796 PJ_LOG(3,(THIS_FILE, "No current call"));
1797 }
1798 break;
1799
1800 case 'U':
1801 /*
1802 * Send UPDATE
1803 */
1804 ui_send_update();
1805 break;
1806
1807 case 'C':
1808 if (menuin[1] == 'p') {
1809 ui_manage_codec_prio();
1810 }
1811 break;
1812
1813 case 'x':
1814 /*
1815 * Transfer call.
1816 */
1817 ui_call_transfer(app_config.no_refersub);
1818 break;
1819
1820 case 'X':
1821 /*
1822 * Transfer call with replaces.
1823 */
1824 ui_call_transfer_replaces(app_config.no_refersub);
1825 break;
1826
1827 case '#':
1828 /*
1829 * Send DTMF strings.
1830 */
1831 ui_send_dtmf_2833();
1832 break;
1833
1834 case '*':
1835 /* Send DTMF with INFO */
1836 ui_send_dtmf_info();
1837 break;
1838
1839 case 'S':
1840 /*
1841 * Send arbitrary request
1842 */
1843 ui_send_arbitrary_request();
1844 break;
1845
1846 case 'e':
1847 ui_echo(menuin);
1848 break;
1849
1850 case 's':
1851 ui_sleep(menuin);
1852 break;
1853 /* Continue below */
1854
1855 case 'u':
1856 /*
1857 * Subscribe/unsubscribe presence.
1858 */
1859 ui_subscribe(menuin);
1860 break;
1861
1862 case 'r':
1863 ui_register(menuin);
1864 break;
1865
1866 case 't':
1867 ui_toggle_state();
1868 break;
1869
1870 case 'T':
1871 ui_change_online_status();
1872 break;
1873
1874 case 'c':
1875 switch (menuin[1]) {
1876 case 'l':
1877 ui_conf_list();
1878 break;
1879 case 'c':
1880 case 'd':
1881 ui_conf_connect(menuin);
1882 break;
1883 }
1884 break;
1885
1886 case 'V':
1887 /* Adjust audio volume */
1888 ui_adjust_volume();
1889 break;
1890
1891 case 'd':
1892 if (menuin[1] == 'c') {
1893 ui_dump_configuration();
1894 } else if (menuin[1] == 'q') {
1895 ui_dump_call_quality();
1896 } else {
1897 ui_app_dump(menuin[1]=='d');
1898 }
1899 break;
1900
1901 case 'f':
1902 if (simple_input("Enter output filename", buf, sizeof(buf))) {
1903 ui_write_settings();
1904 }
1905 break;
1906
1907 case 'L': /* Restart */
Benny Prijono8e4a1132013-04-03 22:41:23 +00001908 case 'q':
Riza Sulistyo0c5d8f72013-04-19 06:05:06 +00001909 legacy_on_stopped(menuin[0]=='L');
Benny Prijono8e4a1132013-04-03 22:41:23 +00001910 goto on_exit;
1911
1912 case 'R':
1913 ui_call_redirect(menuin);
1914 break;
1915
1916 default:
1917 if (menuin[0] != '\n' && menuin[0] != '\r') {
1918 printf("Invalid input %s", menuin);
1919 }
1920 keystroke_help();
1921 break;
1922 }
1923 }
1924
1925on_exit:
1926 ;
Riza Sulistyobc9c6772013-04-05 03:02:19 +00001927}