blob: 2091158b156c20445732665cbd1c65fbb163dc01 [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>
22#include "pjsua_cmd.h"
23
24#define THIS_FILE "pjsua_ui_cmd.c"
25
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{
67 result->nb_result = NO_NB;
68 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");
126 result->nb_result = NO_NB;
127 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);
599 if (result.nb_result != NO_NB) {
600
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);
642 if (result.nb_result != NO_NB) {
643 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);
684 if (result.nb_result != NO_NB) {
685
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{
973 if (current_call != -1) {
974 call_opt.flag |= PJSUA_CALL_UNHOLD;
975 pjsua_call_update2(current_call, &call_opt, NULL);
976 } else {
977 PJ_LOG(3,(THIS_FILE, "No current call"));
978 }
979}
980
981/*
982 * Change codec priorities.
983 */
984static void ui_manage_codec_prio()
985{
986 pjsua_codec_info c[32];
987 unsigned i, count = PJ_ARRAY_SIZE(c);
988 char input[32];
989 char *codec, *prio;
990 pj_str_t id;
991 int new_prio;
992 pj_status_t status;
993
994 printf("List of audio codecs:\n");
995 pjsua_enum_codecs(c, &count);
996 for (i=0; i<count; ++i) {
997 printf(" %d\t%.*s\n", c[i].priority, (int)c[i].codec_id.slen,
998 c[i].codec_id.ptr);
999 }
1000
1001#if PJSUA_HAS_VIDEO
1002 puts("");
1003 printf("List of video codecs:\n");
1004 pjsua_vid_enum_codecs(c, &count);
1005 for (i=0; i<count; ++i) {
1006 printf(" %d\t%.*s%s%.*s\n", c[i].priority,
1007 (int)c[i].codec_id.slen,
1008 c[i].codec_id.ptr,
1009 c[i].desc.slen? " - ":"",
1010 (int)c[i].desc.slen,
1011 c[i].desc.ptr);
1012 }
1013#endif
1014
1015 puts("");
1016 puts("Enter codec id and its new priority (e.g. \"speex/16000 200\", "
1017 """\"H263 200\"),");
1018 puts("or empty to cancel.");
1019
1020 printf("Codec name (\"*\" for all) and priority: ");
1021 if (fgets(input, sizeof(input), stdin) == NULL)
1022 return;
1023 if (input[0]=='\r' || input[0]=='\n') {
1024 puts("Done");
1025 return;
1026 }
1027
1028 codec = strtok(input, " \t\r\n");
1029 prio = strtok(NULL, " \r\n");
1030
1031 if (!codec || !prio) {
1032 puts("Invalid input");
1033 return;
1034 }
1035
1036 new_prio = atoi(prio);
1037 if (new_prio < 0)
1038 new_prio = 0;
1039 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
1040 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
1041
1042 status = pjsua_codec_set_priority(pj_cstr(&id, codec),
1043 (pj_uint8_t)new_prio);
1044#if PJSUA_HAS_VIDEO
1045 if (status != PJ_SUCCESS) {
1046 status = pjsua_vid_codec_set_priority(pj_cstr(&id, codec),
1047 (pj_uint8_t)new_prio);
1048 }
1049#endif
1050 if (status != PJ_SUCCESS)
1051 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
1052}
1053
1054static void ui_call_transfer(pj_bool_t no_refersub)
1055{
1056 if (current_call == -1) {
1057 PJ_LOG(3,(THIS_FILE, "No current call"));
1058 } else {
1059 int call = current_call;
1060 char buf[128];
1061 pjsip_generic_string_hdr refer_sub;
1062 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
1063 pj_str_t STR_FALSE = { "false", 5 };
1064 pjsua_call_info ci;
1065 input_result result;
1066 pjsua_msg_data msg_data;
1067
1068 pjsua_call_get_info(current_call, &ci);
1069 printf("Transfering current call [%d] %.*s\n", current_call,
1070 (int)ci.remote_info.slen, ci.remote_info.ptr);
1071
1072 ui_input_url("Transfer to URL", buf, sizeof(buf), &result);
1073
1074 /* Check if call is still there. */
1075
1076 if (call != current_call) {
1077 puts("Call has been disconnected");
1078 return;
1079 }
1080
1081 pjsua_msg_data_init(&msg_data);
1082 if (no_refersub) {
1083 /* Add Refer-Sub: false in outgoing REFER request */
1084 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
1085 &STR_FALSE);
1086 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
1087 }
1088 if (result.nb_result != NO_NB) {
1089 if (result.nb_result == -1 || result.nb_result == 0)
1090 puts("You can't do that with transfer call!");
1091 else {
1092 pjsua_buddy_info binfo;
1093 pjsua_buddy_get_info(result.nb_result-1, &binfo);
1094 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
1095 }
1096
1097 } else if (result.uri_result) {
1098 pj_str_t tmp;
1099 tmp = pj_str(result.uri_result);
1100 pjsua_call_xfer( current_call, &tmp, &msg_data);
1101 }
1102 }
1103}
1104
1105static void ui_call_transfer_replaces(pj_bool_t no_refersub)
1106{
1107 if (current_call == -1) {
1108 PJ_LOG(3,(THIS_FILE, "No current call"));
1109 } else {
1110 int call = current_call;
1111 int dst_call;
1112 pjsip_generic_string_hdr refer_sub;
1113 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
1114 pj_str_t STR_FALSE = { "false", 5 };
1115 pjsua_call_id ids[PJSUA_MAX_CALLS];
1116 pjsua_call_info ci;
1117 pjsua_msg_data msg_data;
1118 char buf[128];
1119 unsigned i, count;
1120
1121 count = PJ_ARRAY_SIZE(ids);
1122 pjsua_enum_calls(ids, &count);
1123
1124 if (count <= 1) {
1125 puts("There are no other calls");
1126 return;
1127 }
1128
1129 pjsua_call_get_info(current_call, &ci);
1130 printf("Transfer call [%d] %.*s to one of the following:\n",
1131 current_call,
1132 (int)ci.remote_info.slen, ci.remote_info.ptr);
1133
1134 for (i=0; i<count; ++i) {
1135 pjsua_call_info call_info;
1136
1137 if (ids[i] == call)
1138 return;
1139
1140 pjsua_call_get_info(ids[i], &call_info);
1141 printf("%d %.*s [%.*s]\n",
1142 ids[i],
1143 (int)call_info.remote_info.slen,
1144 call_info.remote_info.ptr,
1145 (int)call_info.state_text.slen,
1146 call_info.state_text.ptr);
1147 }
1148
1149 if (!simple_input("Enter call number to be replaced", buf, sizeof(buf)))
1150 return;
1151
1152 dst_call = my_atoi(buf);
1153
1154 /* Check if call is still there. */
1155
1156 if (call != current_call) {
1157 puts("Call has been disconnected");
1158 return;
1159 }
1160
1161 /* Check that destination call is valid. */
1162 if (dst_call == call) {
1163 puts("Destination call number must not be the same "
1164 "as the call being transfered");
1165 return;
1166 }
1167 if (dst_call >= PJSUA_MAX_CALLS) {
1168 puts("Invalid destination call number");
1169 return;
1170 }
1171 if (!pjsua_call_is_active(dst_call)) {
1172 puts("Invalid destination call number");
1173 return;
1174 }
1175
1176 pjsua_msg_data_init(&msg_data);
1177 if (no_refersub) {
1178 /* Add Refer-Sub: false in outgoing REFER request */
1179 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
1180 &STR_FALSE);
1181 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
1182 }
1183
1184 pjsua_call_xfer_replaces(call, dst_call,
1185 PJSUA_XFER_NO_REQUIRE_REPLACES,
1186 &msg_data);
1187 }
1188}
1189
1190static void ui_send_dtmf_2833()
1191{
1192 if (current_call == -1) {
1193 PJ_LOG(3,(THIS_FILE, "No current call"));
1194 } else if (!pjsua_call_has_media(current_call)) {
1195 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
1196 } else {
1197 pj_str_t digits;
1198 int call = current_call;
1199 pj_status_t status;
1200 char buf[128];
1201
1202 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
1203 sizeof(buf)))
1204 {
1205 return;
1206 }
1207
1208 if (call != current_call) {
1209 puts("Call has been disconnected");
1210 return;
1211 }
1212
1213 digits = pj_str(buf);
1214 status = pjsua_call_dial_dtmf(current_call, &digits);
1215 if (status != PJ_SUCCESS) {
1216 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
1217 } else {
1218 puts("DTMF digits enqueued for transmission");
1219 }
1220 }
1221}
1222
1223static void ui_send_dtmf_info()
1224{
1225 if (current_call == -1) {
1226 PJ_LOG(3,(THIS_FILE, "No current call"));
1227 } else {
1228 const pj_str_t SIP_INFO = pj_str("INFO");
1229 pj_str_t digits;
1230 int call = current_call;
1231 int i;
1232 pj_status_t status;
1233 char buf[128];
1234
1235 if (!simple_input("DTMF strings to send (0-9*#A-B)", buf,
1236 sizeof(buf)))
1237 {
1238 return;
1239 }
1240
1241 if (call != current_call) {
1242 puts("Call has been disconnected");
1243 return;
1244 }
1245
1246 digits = pj_str(buf);
1247 for (i=0; i<digits.slen; ++i) {
1248 char body[80];
1249 pjsua_msg_data msg_data;
1250
1251 pjsua_msg_data_init(&msg_data);
1252 msg_data.content_type = pj_str("application/dtmf-relay");
1253
1254 pj_ansi_snprintf(body, sizeof(body),
1255 "Signal=%c\r\n"
1256 "Duration=160",
1257 buf[i]);
1258 msg_data.msg_body = pj_str(body);
1259
1260 status = pjsua_call_send_request(current_call, &SIP_INFO,
1261 &msg_data);
1262 if (status != PJ_SUCCESS) {
1263 return;
1264 }
1265 }
1266 }
1267}
1268
1269static void ui_send_arbitrary_request()
1270{
1271 char text[128];
1272 char buf[128];
1273 char *uri;
1274 input_result result;
1275 pj_str_t tmp;
1276
1277 if (pjsua_acc_get_count() == 0) {
1278 puts("Sorry, need at least one account configured");
1279 return;
1280 }
1281
1282 puts("Send arbitrary request to remote host");
1283
1284 /* Input METHOD */
1285 if (!simple_input("Request method:",text,sizeof(text)))
1286 return;
1287
1288 /* Input destination URI */
1289 uri = NULL;
1290 ui_input_url("Destination URI", buf, sizeof(buf), &result);
1291 if (result.nb_result != NO_NB) {
1292
1293 if (result.nb_result == -1) {
1294 puts("Sorry you can't do that!");
1295 return;
1296 } else if (result.nb_result == 0) {
1297 uri = NULL;
1298 if (current_call == PJSUA_INVALID_ID) {
1299 puts("No current call");
1300 return;
1301 }
1302 } else {
1303 pjsua_buddy_info binfo;
1304 pjsua_buddy_get_info(result.nb_result-1, &binfo);
1305 tmp.ptr = buf;
1306 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(buf));
1307 uri = buf;
1308 }
1309
1310 } else if (result.uri_result) {
1311 uri = result.uri_result;
1312 } else {
1313 return;
1314 }
1315
1316 if (uri) {
1317 tmp = pj_str(uri);
1318 send_request(text, &tmp);
1319 } else {
1320 /* If you send call control request using this method
1321 * (such requests includes BYE, CANCEL, etc.), it will
1322 * not go well with the call state, so don't do it
1323 * unless it's for testing.
1324 */
1325 pj_str_t method = pj_str(text);
1326 pjsua_call_send_request(current_call, &method, NULL);
1327 }
1328}
1329
1330static void ui_echo(char menuin[])
1331{
1332 if (pj_ansi_strnicmp(menuin, "echo", 4)==0) {
1333 pj_str_t tmp;
1334
1335 tmp.ptr = menuin+5;
1336 tmp.slen = pj_ansi_strlen(menuin)-6;
1337
1338 if (tmp.slen < 1) {
1339 puts("Usage: echo [0|1]");
1340 return;
1341 }
1342 cmd_echo = *tmp.ptr != '0' || tmp.slen!=1;
1343 }
1344}
1345
1346static void ui_sleep(char menuin[])
1347{
1348 if (pj_ansi_strnicmp(menuin, "sleep", 5)==0) {
1349 pj_str_t tmp;
1350 int delay;
1351
1352 tmp.ptr = menuin+6;
1353 tmp.slen = pj_ansi_strlen(menuin)-7;
1354
1355 if (tmp.slen < 1) {
1356 puts("Usage: sleep MSEC");
1357 return;
1358 }
1359
1360 delay = pj_strtoul(&tmp);
1361 if (delay < 0) delay = 0;
1362 pj_thread_sleep(delay);
1363 }
1364}
1365
1366static void ui_subscribe(char menuin[])
1367{
1368 char buf[128];
1369 input_result result;
1370
1371 ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result);
1372 if (result.nb_result != NO_NB) {
1373 if (result.nb_result == -1) {
1374 int i, count;
1375 count = pjsua_get_buddy_count();
1376 for (i=0; i<count; ++i)
1377 pjsua_buddy_subscribe_pres(i, menuin[0]=='s');
1378 } else if (result.nb_result == 0) {
1379 puts("Sorry, can only subscribe to buddy's presence, "
1380 "not from existing call");
1381 } else {
1382 pjsua_buddy_subscribe_pres(result.nb_result-1, (menuin[0]=='s'));
1383 }
1384
1385 } else if (result.uri_result) {
1386 puts("Sorry, can only subscribe to buddy's presence, "
1387 "not arbitrary URL (for now)");
1388 }
1389}
1390
1391static void ui_register(char menuin[])
1392{
1393 switch (menuin[1]) {
1394 case 'r':
1395 /*
1396 * Re-Register.
1397 */
1398 pjsua_acc_set_registration(current_acc, PJ_TRUE);
1399 break;
1400 case 'u':
1401 /*
1402 * Unregister
1403 */
1404 pjsua_acc_set_registration(current_acc, PJ_FALSE);
1405 break;
1406 }
1407}
1408
1409static void ui_toggle_state()
1410{
1411 pjsua_acc_info acc_info;
1412
1413 pjsua_acc_get_info(current_acc, &acc_info);
1414 acc_info.online_status = !acc_info.online_status;
1415 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
1416 printf("Setting %s online status to %s\n",
1417 acc_info.acc_uri.ptr,
1418 (acc_info.online_status?"online":"offline"));
1419}
1420
1421/*
1422 * Change extended online status.
1423 */
1424static void ui_change_online_status()
1425{
1426 char menuin[32];
1427 pj_bool_t online_status;
1428 pjrpid_element elem;
1429 int i, choice;
1430
1431 enum {
1432 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
1433 };
1434
1435 struct opt {
1436 int id;
1437 char *name;
1438 } opts[] = {
1439 { AVAILABLE, "Available" },
1440 { BUSY, "Busy"},
1441 { OTP, "On the phone"},
1442 { IDLE, "Idle"},
1443 { AWAY, "Away"},
1444 { BRB, "Be right back"},
1445 { OFFLINE, "Offline"}
1446 };
1447
1448 printf("\n"
1449 "Choices:\n");
1450 for (i=0; i<PJ_ARRAY_SIZE(opts); ++i) {
1451 printf(" %d %s\n", opts[i].id+1, opts[i].name);
1452 }
1453
1454 if (!simple_input("Select status", menuin, sizeof(menuin)))
1455 return;
1456
1457 choice = atoi(menuin) - 1;
1458 if (choice < 0 || choice >= OPT_MAX) {
1459 puts("Invalid selection");
1460 return;
1461 }
1462
1463 pj_bzero(&elem, sizeof(elem));
1464 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
1465
1466 online_status = PJ_TRUE;
1467
1468 switch (choice) {
1469 case AVAILABLE:
1470 break;
1471 case BUSY:
1472 elem.activity = PJRPID_ACTIVITY_BUSY;
1473 elem.note = pj_str("Busy");
1474 break;
1475 case OTP:
1476 elem.activity = PJRPID_ACTIVITY_BUSY;
1477 elem.note = pj_str("On the phone");
1478 break;
1479 case IDLE:
1480 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
1481 elem.note = pj_str("Idle");
1482 break;
1483 case AWAY:
1484 elem.activity = PJRPID_ACTIVITY_AWAY;
1485 elem.note = pj_str("Away");
1486 break;
1487 case BRB:
1488 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
1489 elem.note = pj_str("Be right back");
1490 break;
1491 case OFFLINE:
1492 online_status = PJ_FALSE;
1493 break;
1494 }
1495
1496 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
1497}
1498
1499/*
1500 * List the ports in conference bridge
1501 */
1502static void ui_conf_list()
1503{
1504 unsigned i, count;
1505 pjsua_conf_port_id id[PJSUA_MAX_CALLS];
1506
1507 printf("Conference ports:\n");
1508
1509 count = PJ_ARRAY_SIZE(id);
1510 pjsua_enum_conf_ports(id, &count);
1511
1512 for (i=0; i<count; ++i) {
1513 char txlist[PJSUA_MAX_CALLS*4+10];
1514 unsigned j;
1515 pjsua_conf_port_info info;
1516
1517 pjsua_conf_get_port_info(id[i], &info);
1518
1519 txlist[0] = '\0';
1520 for (j=0; j<info.listener_cnt; ++j) {
1521 char s[10];
1522 pj_ansi_snprintf(s, sizeof(s), "#%d ", info.listeners[j]);
1523 pj_ansi_strcat(txlist, s);
1524 }
1525 printf("Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
1526 info.slot_id,
1527 info.clock_rate/1000,
1528 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
1529 info.channel_count,
1530 (int)info.name.slen,
1531 info.name.ptr,
1532 txlist);
1533
1534 }
1535 puts("");
1536}
1537
1538static void ui_conf_connect(char menuin[])
1539{
1540 char tmp[10], src_port[10], dst_port[10];
1541 pj_status_t status;
1542 int cnt;
1543 const char *src_title, *dst_title;
1544
1545 cnt = sscanf(menuin, "%s %s %s", tmp, src_port, dst_port);
1546
1547 if (cnt != 3) {
1548 ui_conf_list();
1549
1550 src_title = (menuin[1]=='c'? "Connect src port #":
1551 "Disconnect src port #");
1552 dst_title = (menuin[1]=='c'? "To dst port #":"From dst port #");
1553
1554 if (!simple_input(src_title, src_port, sizeof(src_port)))
1555 return;
1556
1557 if (!simple_input(dst_title, dst_port, sizeof(dst_port)))
1558 return;
1559 }
1560
1561 if (menuin[1]=='c') {
1562 status = pjsua_conf_connect(my_atoi(src_port), my_atoi(dst_port));
1563 } else {
1564 status = pjsua_conf_disconnect(my_atoi(src_port), my_atoi(dst_port));
1565 }
1566 if (status == PJ_SUCCESS) {
1567 puts("Success");
1568 } else {
1569 puts("ERROR!!");
1570 }
1571}
1572
1573static void ui_adjust_volume()
1574{
1575 char buf[128];
1576 char text[128];
1577 sprintf(buf, "Adjust mic level: [%4.1fx] ", app_config.mic_level);
1578 if (simple_input(buf,text,sizeof(text))) {
1579 char *err;
1580 app_config.mic_level = (float)strtod(text, &err);
1581 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
1582 }
1583 sprintf(buf, "Adjust speaker level: [%4.1fx] ", app_config.speaker_level);
1584 if (simple_input(buf,text,sizeof(text))) {
1585 char *err;
1586 app_config.speaker_level = (float)strtod(text, &err);
1587 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
1588 }
1589}
1590
1591static void ui_dump_call_quality()
1592{
1593 if (current_call != PJSUA_INVALID_ID) {
1594 log_call_dump(current_call);
1595 } else {
1596 PJ_LOG(3,(THIS_FILE, "No current call"));
1597 }
1598}
1599
1600static void ui_dump_configuration()
1601{
1602 char settings[2000];
1603 int len;
1604
1605 len = write_settings(&app_config, settings, sizeof(settings));
1606 if (len < 1)
1607 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
1608 else
1609 PJ_LOG(3,(THIS_FILE, "Dumping configuration (%d bytes):\n%s\n",
1610 len, settings));
1611}
1612
1613static void ui_write_settings()
1614{
1615 char settings[2000];
1616 int len;
1617 char buf[128];
1618
1619 len = write_settings(&app_config, settings, sizeof(settings));
1620 if (len < 1)
1621 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
1622 else {
1623 pj_oshandle_t fd;
1624 pj_status_t status;
1625
1626 status = pj_file_open(app_config.pool, buf, PJ_O_WRONLY, &fd);
1627 if (status != PJ_SUCCESS) {
1628 pjsua_perror(THIS_FILE, "Unable to open file", status);
1629 } else {
1630 pj_ssize_t size = len;
1631 pj_file_write(fd, settings, &size);
1632 pj_file_close(fd);
1633
1634 printf("Settings successfully written to '%s'\n", buf);
1635 }
1636 }
1637}
1638
1639/*
1640 * Dump application states.
1641 */
1642static void ui_app_dump(pj_bool_t detail)
1643{
1644 pjsua_dump(detail);
1645}
1646
1647static void ui_call_redirect(char menuin[])
1648{
1649 if (current_call == PJSUA_INVALID_ID) {
1650 PJ_LOG(3,(THIS_FILE, "No current call"));
1651 } else {
1652 if (!pjsua_call_is_active(current_call)) {
1653 PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
1654 } else if (menuin[1] == 'a') {
1655 pjsua_call_process_redirect(current_call,
1656 PJSIP_REDIRECT_ACCEPT_REPLACE);
1657 } else if (menuin[1] == 'A') {
1658 pjsua_call_process_redirect(current_call,
1659 PJSIP_REDIRECT_ACCEPT);
1660 } else if (menuin[1] == 'r') {
1661 pjsua_call_process_redirect(current_call,
1662 PJSIP_REDIRECT_REJECT);
1663 } else {
1664 pjsua_call_process_redirect(current_call,
1665 PJSIP_REDIRECT_STOP);
1666 }
1667 }
1668}
1669
1670/*
1671 * Main "user interface" loop.
1672 */
1673void console_app_main(const pj_str_t *uri_to_call, pj_bool_t *app_restart)
1674{
1675 char menuin[80];
1676 char buf[128];
1677
1678 pjsua_call_setting_default(&call_opt);
1679 call_opt.aud_cnt = app_config.aud_cnt;
1680 call_opt.vid_cnt = app_config.vid.vid_cnt;
1681
1682 /* If user specifies URI to call, then call the URI */
1683 if (uri_to_call->slen) {
1684 pjsua_call_make_call( current_acc, uri_to_call, &call_opt,
1685 NULL, NULL, NULL);
1686 }
1687
1688 keystroke_help(current_call);
1689
1690 for (;;) {
1691
1692 printf(">>> ");
1693 fflush(stdout);
1694
1695 if (fgets(menuin, sizeof(menuin), stdin) == NULL) {
1696 /*
1697 * Be friendly to users who redirect commands into
1698 * program, when file ends, resume with kbd.
1699 * If exit is desired end script with q for quit
1700 */
1701 /* Reopen stdin/stdout/stderr to /dev/console */
1702#if defined(PJ_WIN32) && PJ_WIN32!=0
1703 if (freopen ("CONIN$", "r", stdin) == NULL) {
1704#else
1705 if (1) {
1706#endif
1707 puts("Cannot switch back to console from file redirection");
1708 menuin[0] = 'q';
1709 menuin[1] = '\0';
1710 } else {
1711 puts("Switched back to console from file redirection");
1712 continue;
1713 }
1714 }
1715
1716 if (cmd_echo) {
1717 printf("%s", menuin);
1718 }
1719
1720 /* Update call setting */
1721 pjsua_call_setting_default(&call_opt);
1722 call_opt.aud_cnt = app_config.aud_cnt;
1723 call_opt.vid_cnt = app_config.vid.vid_cnt;
1724
1725 switch (menuin[0]) {
1726
1727 case 'm':
1728 /* Make call! : */
1729 ui_make_new_call();
1730 break;
1731
1732 case 'M':
1733 /* Make multiple calls! : */
1734 ui_make_multi_call();
1735 break;
1736
1737 case 'n':
1738 ui_detect_nat_type();
1739 break;
1740
1741 case 'i':
1742 /* Send instant messaeg */
1743 ui_send_instant_message();
1744 break;
1745
1746 case 'a':
1747 ui_answer_call();
1748 break;
1749
1750 case 'h':
1751 ui_hangup_call(menuin);
1752 break;
1753
1754 case ']':
1755 case '[':
1756 /*
1757 * Cycle next/prev dialog.
1758 */
1759 ui_cycle_dialog(menuin);
1760 break;
1761
1762 case '>':
1763 case '<':
1764 ui_cycle_account();
1765 break;
1766
1767 case '+':
1768 if (menuin[1] == 'b') {
1769 ui_add_buddy();
1770 } else if (menuin[1] == 'a') {
1771 ui_add_account(&app_config.rtp_cfg);
1772 } else {
1773 printf("Invalid input %s\n", menuin);
1774 }
1775 break;
1776
1777 case '-':
1778 if (menuin[1] == 'b') {
1779 ui_delete_buddy();
1780 } else if (menuin[1] == 'a') {
1781 ui_delete_account();
1782 } else {
1783 printf("Invalid input %s\n", menuin);
1784 }
1785 break;
1786
1787 case 'H':
1788 /*
1789 * Hold call.
1790 */
1791 ui_call_hold();
1792 break;
1793
1794 case 'v':
1795#if PJSUA_HAS_VIDEO
1796 if (menuin[1]=='i' && menuin[2]=='d' && menuin[3]==' ') {
1797 vid_handle_menu(menuin);
1798 } else
1799#endif
1800 if (current_call != -1) {
1801 /*
1802 * re-INVITE
1803 */
1804 ui_call_reinvite();
1805 } else {
1806 PJ_LOG(3,(THIS_FILE, "No current call"));
1807 }
1808 break;
1809
1810 case 'U':
1811 /*
1812 * Send UPDATE
1813 */
1814 ui_send_update();
1815 break;
1816
1817 case 'C':
1818 if (menuin[1] == 'p') {
1819 ui_manage_codec_prio();
1820 }
1821 break;
1822
1823 case 'x':
1824 /*
1825 * Transfer call.
1826 */
1827 ui_call_transfer(app_config.no_refersub);
1828 break;
1829
1830 case 'X':
1831 /*
1832 * Transfer call with replaces.
1833 */
1834 ui_call_transfer_replaces(app_config.no_refersub);
1835 break;
1836
1837 case '#':
1838 /*
1839 * Send DTMF strings.
1840 */
1841 ui_send_dtmf_2833();
1842 break;
1843
1844 case '*':
1845 /* Send DTMF with INFO */
1846 ui_send_dtmf_info();
1847 break;
1848
1849 case 'S':
1850 /*
1851 * Send arbitrary request
1852 */
1853 ui_send_arbitrary_request();
1854 break;
1855
1856 case 'e':
1857 ui_echo(menuin);
1858 break;
1859
1860 case 's':
1861 ui_sleep(menuin);
1862 break;
1863 /* Continue below */
1864
1865 case 'u':
1866 /*
1867 * Subscribe/unsubscribe presence.
1868 */
1869 ui_subscribe(menuin);
1870 break;
1871
1872 case 'r':
1873 ui_register(menuin);
1874 break;
1875
1876 case 't':
1877 ui_toggle_state();
1878 break;
1879
1880 case 'T':
1881 ui_change_online_status();
1882 break;
1883
1884 case 'c':
1885 switch (menuin[1]) {
1886 case 'l':
1887 ui_conf_list();
1888 break;
1889 case 'c':
1890 case 'd':
1891 ui_conf_connect(menuin);
1892 break;
1893 }
1894 break;
1895
1896 case 'V':
1897 /* Adjust audio volume */
1898 ui_adjust_volume();
1899 break;
1900
1901 case 'd':
1902 if (menuin[1] == 'c') {
1903 ui_dump_configuration();
1904 } else if (menuin[1] == 'q') {
1905 ui_dump_call_quality();
1906 } else {
1907 ui_app_dump(menuin[1]=='d');
1908 }
1909 break;
1910
1911 case 'f':
1912 if (simple_input("Enter output filename", buf, sizeof(buf))) {
1913 ui_write_settings();
1914 }
1915 break;
1916
1917 case 'L': /* Restart */
1918 *app_restart = PJ_TRUE;
1919 /* Continues below */
1920
1921 case 'q':
1922 goto on_exit;
1923
1924 case 'R':
1925 ui_call_redirect(menuin);
1926 break;
1927
1928 default:
1929 if (menuin[0] != '\n' && menuin[0] != '\r') {
1930 printf("Invalid input %s", menuin);
1931 }
1932 keystroke_help();
1933 break;
1934 }
1935 }
1936
1937on_exit:
1938 ;
1939}
1940