From 5df88d5670b8860937acf820ec4483bf24def352 Mon Sep 17 00:00:00 2001 From: Riza Sulistyo Date: Fri, 19 Apr 2013 06:05:06 +0000 Subject: Re #1643: Code restructure + modification to handle non blocking mode git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4476 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip-apps/src/pjsua/main.c | 104 +- pjsip-apps/src/pjsua/pjsua_app.c | 333 ++-- pjsip-apps/src/pjsua/pjsua_app.h | 88 + pjsip-apps/src/pjsua/pjsua_cli.c | 3117 ++++++++++++++++++++++++++++++++-- pjsip-apps/src/pjsua/pjsua_cli_cmd.c | 2967 -------------------------------- pjsip-apps/src/pjsua/pjsua_common.h | 74 +- pjsip-apps/src/pjsua/pjsua_config.c | 88 +- pjsip-apps/src/pjsua/pjsua_legacy.c | 52 +- 8 files changed, 3374 insertions(+), 3449 deletions(-) create mode 100644 pjsip-apps/src/pjsua/pjsua_app.h delete mode 100644 pjsip-apps/src/pjsua/pjsua_cli_cmd.c (limited to 'pjsip-apps/src') diff --git a/pjsip-apps/src/pjsua/main.c b/pjsip-apps/src/pjsua/main.c index 715ab349..4df0a56e 100644 --- a/pjsip-apps/src/pjsua/main.c +++ b/pjsip-apps/src/pjsua/main.c @@ -17,18 +17,108 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include - +#include "pjsua_app.h" #define THIS_FILE "main.c" +static pj_bool_t running = PJ_TRUE; +static pj_status_t receive_end_sig; +static pj_thread_t *sig_thread; +static app_cfg_t cfg; -/* - * These are defined in pjsua_app.c. - */ -int main_func(int argc, char *argv[]); +/* Called when CLI (re)started */ +void on_app_started(pj_status_t status, const char *msg) +{ + pj_perror(3, THIS_FILE, status, (msg)?msg:""); +} + +pj_bool_t on_app_stopped(pj_bool_t restart, int argc, char** argv) +{ + cfg.argc = argc; + cfg.argv = argv; + + running = restart; + return PJ_TRUE; +} + +#if defined(PJ_WIN32) && PJ_WIN32!=0 +#include + +static pj_thread_desc handler_desc; + +static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) +{ + switch (fdwCtrlType) + { + // Handle the CTRL+C signal. + + case CTRL_C_EVENT: + case CTRL_CLOSE_EVENT: + case CTRL_BREAK_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + pj_thread_register("ctrlhandler", handler_desc, &sig_thread); + PJ_LOG(3,(THIS_FILE, "Ctrl-C detected, quitting..")); + receive_end_sig = PJ_TRUE; + app_destroy(); + ExitProcess(1); + PJ_UNREACHED(return TRUE;) + + default: + + return FALSE; + } +} + +static void setup_socket_signal() +{ +} + +static void setup_signal_handler(void) +{ + SetConsoleCtrlHandler(&CtrlHandler, TRUE); +} + +#else +#include + +static void setup_socket_signal() +{ + signal(SIGPIPE, SIG_IGN); +} + +static void setup_signal_handler(void) {} +#endif int main(int argc, char *argv[]) { - return pj_run_app(&main_func, argc, argv, 0); + pj_status_t status = PJ_TRUE; + + pj_bzero(&cfg, sizeof(cfg)); + cfg.on_started = &on_app_started; + cfg.on_stopped = &on_app_stopped; + cfg.argc = argc; + cfg.argv = argv; + + setup_signal_handler(); + setup_socket_signal(); + + while (running) { + status = app_init(&cfg); + if (status == PJ_SUCCESS) { + status = app_run(PJ_TRUE); + } else { + pj_perror(3, THIS_FILE, status, "Failed init"); + running = PJ_FALSE; + } + + if (!receive_end_sig) { + app_destroy(); + + /* This is on purpose */ + app_destroy(); + } else { + pj_thread_join(sig_thread); + } + } } diff --git a/pjsip-apps/src/pjsua/pjsua_app.c b/pjsip-apps/src/pjsua/pjsua_app.c index 8f63d139..f3945d92 100644 --- a/pjsip-apps/src/pjsua/pjsua_app.c +++ b/pjsip-apps/src/pjsua/pjsua_app.c @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "pjsua_common.h" +#include "pjsua_app.h" #define THIS_FILE "pjsua_app.c" @@ -51,28 +51,12 @@ pj_status_t app_destroy(void); static void ringback_start(pjsua_call_id call_id); static void ring_start(pjsua_call_id call_id); static void ring_stop(pjsua_call_id call_id); +static pj_status_t pjsua_app_init(); +static pj_status_t pjsua_app_destroy(); -static pj_status_t receive_end_sig; -static pj_thread_t *sig_thread; +static app_cfg_t app_cfg; pj_str_t uri_arg; -pj_bool_t app_restart; pj_bool_t app_running = PJ_FALSE; -pj_log_func *log_cb = NULL; - -/** Forward declaration **/ -/** Defined in pjsua_common.c **/ -void app_config_init_video(pjsua_acc_config *acc_cfg); -/** Defined in pjsua_legacy.c **/ -void start_ui_main(pj_str_t *uri_to_call, pj_bool_t *app_restart); -/** Defined in pjsua_cli.c **/ -void start_cli_main(pj_str_t *uri_to_call, pj_bool_t *app_restart); -pj_status_t setup_cli(pj_bool_t with_console, pj_bool_t with_telnet, - pj_uint16_t telnet_port, - pj_cli_telnet_on_started on_started_cb, - pj_cli_on_quit on_quit_cb, - pj_cli_on_destroy on_destroy_cb, - pj_cli_on_restart_pjsua on_restart_pjsua_cb); -void destroy_cli(pj_bool_t app_restart); /***************************************************************************** * Configuration manipulation @@ -177,7 +161,6 @@ static void call_timeout_callback(pj_timer_heap_t *timer_heap, pjsua_call_hangup(call_id, 200, NULL, &msg_data); } - /* * Handler when invite state has changed. */ @@ -229,7 +212,7 @@ static void on_call_state(pjsua_call_id call_id, pjsip_event *e) } else { - if (app_config.duration != NO_LIMIT_DURATION && + if (app_config.duration != PJSUA_APP_NO_LIMIT_DURATION && call_info.state == PJSIP_INV_STATE_CONFIRMED) { /* Schedule timer to hangup call after the specified duration */ @@ -314,7 +297,8 @@ static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, call_opt.aud_cnt = app_config.aud_cnt; call_opt.vid_cnt = app_config.vid.vid_cnt; - pjsua_call_answer2(call_id, &call_opt, app_config.auto_answer, NULL, NULL); + pjsua_call_answer2(call_id, &call_opt, app_config.auto_answer, NULL, + NULL); } if (app_config.auto_answer < 200) { @@ -532,7 +516,9 @@ static void on_call_audio_state(pjsua_call_info *ci, unsigned mi, call_conf_slot); /* Automatically record conversation, if desired */ - if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) { + if (app_config.auto_rec && app_config.rec_port != + PJSUA_INVALID_ID) + { pjsua_conf_connect(pjsua_call_get_conf_port(call_ids[i]), app_config.rec_port); } @@ -550,7 +536,8 @@ static void on_call_audio_state(pjsua_call_info *ci, unsigned mi, pjsua_conf_connect(0, call_conf_slot); /* Automatically record conversation, if desired */ - if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) { + if (app_config.auto_rec && app_config.rec_port != PJSUA_INVALID_ID) + { pjsua_conf_connect(call_conf_slot, app_config.rec_port); pjsua_conf_connect(0, app_config.rec_port); } @@ -615,8 +602,8 @@ static void on_call_media_state(pjsua_call_id call_id) if (vid_idx == -1 || call_info.media[vid_idx].dir == PJMEDIA_DIR_NONE) { PJ_LOG(3,(THIS_FILE, "Just rejected incoming video offer on call %d, " - "use \"vid call enable %d\" or \"vid call add\" to enable video!", - call_id, vid_idx)); + "use \"vid call enable %d\" or \"vid call add\" to " + "enable video!", call_id, vid_idx)); } } #endif @@ -895,8 +882,8 @@ static void on_transport_state(pjsip_transport *tp, { char buf[100]; - snprintf(buf, sizeof(buf), "SIP %s transport is disconnected from %s", - tp->type_name, host_port); + snprintf(buf, sizeof(buf), "SIP %s transport is disconnected " + "from %s", tp->type_name, host_port); pjsua_perror(THIS_FILE, buf, info->status); } break; @@ -1226,81 +1213,54 @@ static pjsip_module mod_default_handler = }; -#if defined(PJ_WIN32) && PJ_WIN32!=0 -#include - -static pj_thread_desc handler_desc; - -static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) -{ - switch (fdwCtrlType) - { - // Handle the CTRL+C signal. - - case CTRL_C_EVENT: - case CTRL_CLOSE_EVENT: - case CTRL_BREAK_EVENT: - case CTRL_LOGOFF_EVENT: - case CTRL_SHUTDOWN_EVENT: - pj_thread_register("ctrlhandler", handler_desc, &sig_thread); - PJ_LOG(3,(THIS_FILE, "Ctrl-C detected, quitting..")); - receive_end_sig = PJ_TRUE; - app_destroy(); - ExitProcess(1); - PJ_UNREACHED(return TRUE;) - - default: - - return FALSE; - } -} +/** CLI callback **/ -static void setup_socket_signal() +/* Called on CLI (re)started, e.g: initial start, after iOS bg */ +PJ_DEF(void) cli_on_started(pj_status_t status) { + /* Notify app */ + if (app_cfg.on_started) { + if (status == PJ_SUCCESS) { + char info[128]; + cli_get_info(info, sizeof(info)); + if (app_cfg.on_started) { + (*app_cfg.on_started)(status, info); + } + } else { + if (app_cfg.on_started) { + (*app_cfg.on_started)(status, NULL); + } + } + } } -#else -#include - -static void setup_socket_signal() +/* Called on CLI quit */ +PJ_DEF(pj_bool_t) cli_on_stopped(pj_bool_t restart, int argc, char* argv[]) { - signal(SIGPIPE, SIG_IGN); + /* Notify app */ + if (app_cfg.on_stopped) + return (*app_cfg.on_stopped)(restart, argc, argv); + + return PJ_SUCCESS; } -#endif -static pj_status_t setup_pjsua() +/* Called on pjsua legacy quit */ +PJ_DEF(pj_bool_t) legacy_on_stopped(pj_bool_t restart) { - pj_status_t status = pjsua_destroy(); - if (status != PJ_SUCCESS) - return status; + /* Notify app */ + if (app_cfg.on_stopped) + return (*app_cfg.on_stopped)(restart, 0, NULL); - /* Create pjsua */ - status = pjsua_create(); - if (status != PJ_SUCCESS) - return status; + return PJ_SUCCESS; +} - /* Create pool for application */ - app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000); - return status; -} /***************************************************************************** * Public API */ -#if defined(PJ_WIN32) && PJ_WIN32!=0 -PJ_DEF(void) setup_signal_handler(void) -{ - SetConsoleCtrlHandler(&CtrlHandler, TRUE); -} -#else -PJ_DEF(void) setup_signal_handler(void) -{ -} -#endif - int stdout_refresh_proc(void *arg) { extern char *stdout_refresh_text; @@ -1322,32 +1282,33 @@ int stdout_refresh_proc(void *arg) return 0; } -PJ_DEF(pj_status_t) app_init(pj_cli_telnet_on_started on_started_cb, - pj_cli_on_quit on_quit_cb, - pj_cli_on_destroy on_destroy_cb, - pj_cli_on_restart_pjsua on_restart_pjsua_cb) +static pj_status_t pjsua_app_init() { pjsua_transport_id transport_id = -1; pjsua_transport_config tcp_cfg; unsigned i; pj_status_t status; - /** Setup pjsua **/ - status = setup_pjsua(); + /** Create pjsua **/ + status = pjsua_create(); if (status != PJ_SUCCESS) return status; - /** Load config **/ - status = load_config(&app_config, &uri_arg, app_running); - if (status != PJ_SUCCESS) - return status; + /* Create pool for application */ + app_config.pool = pjsua_pool_create("pjsua-app", 1000, 1000); -#if defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0 - /* Disable threading on Symbian */ - app_config.cfg.thread_cnt = 0; - app_config.media_cfg.thread_cnt = 0; - app_config.media_cfg.has_ioqueue = PJ_FALSE; -#endif + /* Init CLI & its FE settings */ + if (!app_running) { + pj_cli_cfg_default(&app_config.cli_cfg.cfg); + pj_cli_telnet_cfg_default(&app_config.cli_cfg.telnet_cfg); + pj_cli_console_cfg_default(&app_config.cli_cfg.console_cfg); + app_config.cli_cfg.telnet_cfg.on_started = cli_on_started; + } + + /** Parse args **/ + status = load_config(app_cfg.argc, app_cfg.argv, &uri_arg); + if (status != PJ_SUCCESS) + return status; /* Initialize application callbacks */ app_config.cfg.cb.on_call_state = &on_call_state; @@ -1373,7 +1334,6 @@ PJ_DEF(pj_status_t) app_init(pj_cli_telnet_on_started on_started_cb, #ifdef TRANSPORT_ADAPTER_SAMPLE app_config.cfg.cb.on_create_media_transport = &on_create_media_transport; #endif - app_config.log_cfg.cb = log_cb; /* Set sound device latency */ if (app_config.capture_lat > 0) @@ -1381,6 +1341,9 @@ PJ_DEF(pj_status_t) app_init(pj_cli_telnet_on_started on_started_cb, if (app_config.playback_lat) app_config.media_cfg.snd_play_latency = app_config.playback_lat; + if (app_cfg.on_config_init) + (*app_cfg.on_config_init)(&app_config); + /* Initialize pjsua */ status = pjsua_init(&app_config.cfg, &app_config.log_cfg, &app_config.media_cfg); @@ -1833,9 +1796,11 @@ PJ_DEF(pj_status_t) app_init(pj_cli_telnet_on_started on_started_cb, /* Optionally disable some codec */ for (i=0; i= 0, port, - on_started_cb, on_quit_cb, on_destroy_cb, - on_restart_pjsua_cb); - if (status != PJ_SUCCESS) - goto on_error; - } - } + call_opt.vid_cnt = app_config.vid.vid_cnt; return PJ_SUCCESS; on_error: - app_restart = PJ_FALSE; app_destroy(); return status; } -pj_status_t app_main(void) +PJ_DEF(pj_status_t) app_init(const app_cfg_t *cfg) +{ + pj_status_t status; + pj_memcpy(&app_cfg, cfg, sizeof(app_cfg)); + + status = pjsua_app_init(); + if (status != PJ_SUCCESS) + return status; + + /* Init CLI if configured */ + if (app_config.use_cli) { + status = cli_init(); + } + return status; +} + +pj_status_t app_run(pj_bool_t wait_telnet_cli) { pj_thread_t *stdout_refresh_thread = NULL; + pj_status_t status; /* Start console refresh thread */ if (stdout_refresh > 0) { @@ -1903,23 +1871,53 @@ pj_status_t app_main(void) NULL, 0, 0, &stdout_refresh_thread); } + status = pjsua_start(); + if (status != PJ_SUCCESS) + goto on_return; + + if (app_config.use_cli && (app_config.cli_cfg.cli_fe & CLI_FE_TELNET)) { + char info[128]; + cli_get_info(info, sizeof(info)); + if (app_cfg.on_started) { + (*app_cfg.on_started)(status, info); + } + } else { + if (app_cfg.on_started) { + (*app_cfg.on_started)(status, "Ready"); + } + } + + /* If user specifies URI to call, then call the URI */ + if (uri_arg.slen) { + pjsua_call_setting_default(&call_opt); + call_opt.aud_cnt = app_config.aud_cnt; + call_opt.vid_cnt = app_config.vid.vid_cnt; + + pjsua_call_make_call(current_acc, &uri_arg, &call_opt, NULL, + NULL, NULL); + } + + app_running = PJ_TRUE; + if (app_config.use_cli) - start_cli_main(&uri_arg, &app_restart); + cli_main(wait_telnet_cli); else - start_ui_main(&uri_arg, &app_restart); + legacy_main(); + status = PJ_SUCCESS; + +on_return: if (stdout_refresh_thread) { stdout_refresh_quit = PJ_TRUE; pj_thread_join(stdout_refresh_thread); pj_thread_destroy(stdout_refresh_thread); } - - return PJ_SUCCESS; + return status; } -pj_status_t app_destroy() +static pj_status_t pjsua_app_destroy() { - pj_status_t status; + pj_status_t status = PJ_SUCCESS; unsigned i; #ifdef STEREO_DEMO @@ -1972,88 +1970,31 @@ pj_status_t app_destroy() pjsua_conf_remove_port(app_config.tone_slots[i]); } - if (app_config.use_cli) { - destroy_cli(app_restart); - } - if (app_config.pool) { pj_pool_release(app_config.pool); app_config.pool = NULL; } - - status = pjsua_destroy(); - - if (!app_restart) - pj_bzero(&app_config, sizeof(app_config)); - - return status; -} -/** === CLI Callback == **/ + status = pjsua_destroy(); -static void cli_telnet_started(pj_cli_telnet_info *telnet_info) -{ - PJ_LOG(3,(THIS_FILE, "Telnet daemon listening at %.*s:%d", - telnet_info->ip_address.slen, telnet_info->ip_address.ptr, - telnet_info->port)); + return status; } -static void cli_on_quit (pj_bool_t is_restarted) +pj_status_t app_destroy() { - PJ_LOG(3,(THIS_FILE, "CLI quit, restart(%d)", is_restarted)); -} + pj_status_t status; -static void cli_on_destroy(void) -{ - PJ_LOG(3,(THIS_FILE, "CLI destroyed")); -} + status = pjsua_app_destroy(); -static void cli_on_restart_pjsua(void) -{ - PJ_LOG(3,(THIS_FILE, "Restart pjsua")); + if (app_config.use_cli) { + cli_destroy(); + } + + return status; } -pj_cli_telnet_on_started on_started_cb = &cli_telnet_started; -pj_cli_on_quit on_quit_cb = &cli_on_quit; -pj_cli_on_destroy on_destroy_cb = &cli_on_destroy; -pj_cli_on_restart_pjsua on_restart_pjsua_cb = &cli_on_restart_pjsua; - /** ======================= **/ -int main_func(int argc, char *argv[]) -{ - pj_status_t status; - - setup_socket_signal(); - - receive_end_sig = PJ_FALSE; - app_restart = PJ_TRUE; - - add_startup_config(argc, argv); - - do { - if (app_restart) { - status = app_init(on_started_cb, on_quit_cb, - on_destroy_cb, on_restart_pjsua_cb); - if (status != PJ_SUCCESS) - return 1; - } - - app_running = PJ_TRUE; - - app_main(); - if (!receive_end_sig) { - app_destroy(); - - /* This is on purpose */ - app_destroy(); - } else { - pj_thread_join(sig_thread); - } - } while (app_restart); - return 0; -} - #ifdef STEREO_DEMO /* * In this stereo demo, we open the sound device in stereo mode and diff --git a/pjsip-apps/src/pjsua/pjsua_app.h b/pjsip-apps/src/pjsua/pjsua_app.h new file mode 100644 index 00000000..46dcd937 --- /dev/null +++ b/pjsip-apps/src/pjsua/pjsua_app.h @@ -0,0 +1,88 @@ +/* $Id$ */ +/* + * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __PJSUA_APP_H__ +#define __PJSUA_APP_H__ + +/** + * Interface for user application to use pjsua with CLI/menu based UI. + */ + +#include "pjsua_common.h" + +PJ_BEGIN_DECL + +/** + * This structure contains the configuration of application. + */ +typedef struct app_cfg_t +{ + /** + * The number of runtime arguments passed to the application. + */ + int argc; + + /** + * The array of arguments string passed to the application. + */ + char **argv; + + /** + * Tell app that CLI (and pjsua) is (re)started. + * msg will contain start error message such as “Telnet to X:Y”, + * “failed to start pjsua lib”, “port busy”.. + */ + void (*on_started)(pj_status_t status, const char* title); + + /** + * Tell app that library request to stopped/restart. + * GUI app needs to use a timer mechanism to wait before invoking the + * cleanup procedure. + */ + pj_bool_t (*on_stopped)(pj_bool_t restart, int argc, char** argv); + + /** + * This will enable application to supply customize configuration other than + * the basic configuration provided by pjsua. + */ + void (*on_config_init)(pjsua_app_config *cfg); +} app_cfg_t; + +/** + * This will initiate the pjsua and the user interface (CLI/menu UI) based on + * the provided configuration. + */ +PJ_DECL(pj_status_t) app_init(const app_cfg_t *app_cfg); + +/** + * This will run the CLI/menu based UI. + * wait_telnet_cli is used for CLI based UI. It will tell the library to block + * or wait until user invoke the "shutdown"/"restart" command. GUI based app + * should define this param as PJ_FALSE. + */ +PJ_DECL(pj_status_t) app_run(pj_bool_t wait_telnet_cli); + +/** + * This will destroy/cleanup the application library. + */ +PJ_DECL(pj_status_t) app_destroy(); + +PJ_END_DECL + +#endif /* __PJSUA_APP_H__ */ diff --git a/pjsip-apps/src/pjsua/pjsua_cli.c b/pjsip-apps/src/pjsua/pjsua_cli.c index 32935ffb..1a384143 100644 --- a/pjsip-apps/src/pjsua/pjsua_cli.c +++ b/pjsip-apps/src/pjsua/pjsua_cli.c @@ -19,73 +19,162 @@ */ #include "pjsua_common.h" -#include -#include -#include -#include -#include #define THIS_FILE "pjsua_cli.c" -pj_cli_telnet_on_started cli_telnet_on_started_cb = NULL; -pj_cli_on_quit cli_on_quit_cb = NULL; -pj_cli_on_destroy cli_on_destroy_cb = NULL; -pj_cli_on_restart_pjsua cli_on_restart_pjsua_cb = NULL; +#define CHECK_PJSUA_RUNNING() if (pjsua_get_state()!=PJSUA_STATE_RUNNING) \ + return PJ_EINVALIDOP + +/* CLI command id */ +/* level 1 command */ +#define CMD_CALL 100 +#define CMD_PRESENCE 200 +#define CMD_ACCOUNT 300 +#define CMD_MEDIA 400 +#define CMD_CONFIG 500 +#define CMD_VIDEO 600 +#define CMD_SLEEP 700 +#define CMD_ECHO 800 +#define CMD_NETWORK 900 +#define CMD_QUIT 110 +#define CMD_RESTART 120 + +/* call level 2 command */ +#define CMD_CALL_NEW ((CMD_CALL*10)+1) +#define CMD_CALL_MULTI ((CMD_CALL*10)+2) +#define CMD_CALL_ANSWER ((CMD_CALL*10)+3) +#define CMD_CALL_HANGUP ((CMD_CALL*10)+4) +#define CMD_CALL_HANGUP_ALL ((CMD_CALL*10)+5) +#define CMD_CALL_HOLD ((CMD_CALL*10)+6) +#define CMD_CALL_REINVITE ((CMD_CALL*10)+7) +#define CMD_CALL_UPDATE ((CMD_CALL*10)+8) +#define CMD_CALL_NEXT ((CMD_CALL*10)+9) +#define CMD_CALL_PREVIOUS ((CMD_CALL*10)+10) +#define CMD_CALL_TRANSFER ((CMD_CALL*10)+11) +#define CMD_CALL_TRANSFER_REPLACE ((CMD_CALL*10)+12) +#define CMD_CALL_REDIRECT ((CMD_CALL*10)+13) +#define CMD_CALL_D2833 ((CMD_CALL*10)+14) +#define CMD_CALL_INFO ((CMD_CALL*10)+15) +#define CMD_CALL_DUMP_Q ((CMD_CALL*10)+16) +#define CMD_CALL_SEND_ARB ((CMD_CALL*10)+17) +#define CMD_CALL_LIST ((CMD_CALL*10)+18) + +/* im & presence level 2 command */ +#define CMD_PRESENCE_ADD_BUDDY ((CMD_PRESENCE*10)+1) +#define CMD_PRESENCE_DEL_BUDDY ((CMD_PRESENCE*10)+2) +#define CMD_PRESENCE_SEND_IM ((CMD_PRESENCE*10)+3) +#define CMD_PRESENCE_SUB ((CMD_PRESENCE*10)+4) +#define CMD_PRESENCE_UNSUB ((CMD_PRESENCE*10)+5) +#define CMD_PRESENCE_TOG_STATE ((CMD_PRESENCE*10)+6) +#define CMD_PRESENCE_TEXT ((CMD_PRESENCE*10)+7) +#define CMD_PRESENCE_LIST ((CMD_PRESENCE*10)+8) + +/* account level 2 command */ +#define CMD_ACCOUNT_ADD ((CMD_ACCOUNT*10)+1) +#define CMD_ACCOUNT_DEL ((CMD_ACCOUNT*10)+2) +#define CMD_ACCOUNT_MOD ((CMD_ACCOUNT*10)+3) +#define CMD_ACCOUNT_REG ((CMD_ACCOUNT*10)+4) +#define CMD_ACCOUNT_UNREG ((CMD_ACCOUNT*10)+5) +#define CMD_ACCOUNT_NEXT ((CMD_ACCOUNT*10)+6) +#define CMD_ACCOUNT_PREV ((CMD_ACCOUNT*10)+7) +#define CMD_ACCOUNT_SHOW ((CMD_ACCOUNT*10)+8) + +/* conference & media level 2 command */ +#define CMD_MEDIA_LIST ((CMD_MEDIA*10)+1) +#define CMD_MEDIA_CONF_CONNECT ((CMD_MEDIA*10)+2) +#define CMD_MEDIA_CONF_DISCONNECT ((CMD_MEDIA*10)+3) +#define CMD_MEDIA_ADJUST_VOL ((CMD_MEDIA*10)+4) +#define CMD_MEDIA_CODEC_PRIO ((CMD_MEDIA*10)+5) + +/* status & config level 2 command */ +#define CMD_CONFIG_DUMP_STAT ((CMD_CONFIG*10)+1) +#define CMD_CONFIG_DUMP_DETAIL ((CMD_CONFIG*10)+2) +#define CMD_CONFIG_DUMP_CONF ((CMD_CONFIG*10)+3) +#define CMD_CONFIG_WRITE_SETTING ((CMD_CONFIG*10)+4) + +/* video level 2 command */ +#define CMD_VIDEO_ENABLE ((CMD_VIDEO*10)+1) +#define CMD_VIDEO_DISABLE ((CMD_VIDEO*10)+2) +#define CMD_VIDEO_ACC ((CMD_VIDEO*10)+3) +#define CMD_VIDEO_CALL ((CMD_VIDEO*10)+4) +#define CMD_VIDEO_DEVICE ((CMD_VIDEO*10)+5) +#define CMD_VIDEO_CODEC ((CMD_VIDEO*10)+6) +#define CMD_VIDEO_WIN ((CMD_VIDEO*10)+7) + +/* video level 3 command */ +#define CMD_VIDEO_ACC_SHOW ((CMD_VIDEO_ACC*10)+1) +#define CMD_VIDEO_ACC_AUTORX ((CMD_VIDEO_ACC*10)+2) +#define CMD_VIDEO_ACC_AUTOTX ((CMD_VIDEO_ACC*10)+3) +#define CMD_VIDEO_ACC_CAP_ID ((CMD_VIDEO_ACC*10)+4) +#define CMD_VIDEO_ACC_REN_ID ((CMD_VIDEO_ACC*10)+5) +#define CMD_VIDEO_CALL_RX ((CMD_VIDEO_CALL*10)+1) +#define CMD_VIDEO_CALL_TX ((CMD_VIDEO_CALL*10)+2) +#define CMD_VIDEO_CALL_ADD ((CMD_VIDEO_CALL*10)+3) +#define CMD_VIDEO_CALL_ENABLE ((CMD_VIDEO_CALL*10)+4) +#define CMD_VIDEO_CALL_DISABLE ((CMD_VIDEO_CALL*10)+5) +#define CMD_VIDEO_CALL_CAP ((CMD_VIDEO_CALL*10)+6) +#define CMD_VIDEO_DEVICE_LIST ((CMD_VIDEO_DEVICE*10)+1) +#define CMD_VIDEO_DEVICE_REFRESH ((CMD_VIDEO_DEVICE*10)+2) +#define CMD_VIDEO_DEVICE_PREVIEW ((CMD_VIDEO_DEVICE*10)+3) +#define CMD_VIDEO_CODEC_LIST ((CMD_VIDEO_CODEC*10)+1) +#define CMD_VIDEO_CODEC_PRIO ((CMD_VIDEO_CODEC*10)+2) +#define CMD_VIDEO_CODEC_FPS ((CMD_VIDEO_CODEC*10)+3) +#define CMD_VIDEO_CODEC_BITRATE ((CMD_VIDEO_CODEC*10)+4) +#define CMD_VIDEO_CODEC_SIZE ((CMD_VIDEO_CODEC*10)+5) +#define CMD_VIDEO_WIN_LIST ((CMD_VIDEO_WIN*10)+1) +#define CMD_VIDEO_WIN_ARRANGE ((CMD_VIDEO_WIN*10)+2) +#define CMD_VIDEO_WIN_SHOW ((CMD_VIDEO_WIN*10)+3) +#define CMD_VIDEO_WIN_HIDE ((CMD_VIDEO_WIN*10)+4) +#define CMD_VIDEO_WIN_MOVE ((CMD_VIDEO_WIN*10)+5) +#define CMD_VIDEO_WIN_RESIZE ((CMD_VIDEO_WIN*10)+6) + +/* dynamic choice argument list */ +#define DYN_CHOICE_START 9900 +#define DYN_CHOICE_BUDDY_ID (DYN_CHOICE_START)+1 +#define DYN_CHOICE_ACCOUNT_ID (DYN_CHOICE_START)+2 +#define DYN_CHOICE_MEDIA_PORT (DYN_CHOICE_START)+3 +#define DYN_CHOICE_AUDIO_CODEC_ID (DYN_CHOICE_START)+4 +#define DYN_CHOICE_CAP_DEV_ID (DYN_CHOICE_START)+5 +#define DYN_CHOICE_REN_DEV_ID (DYN_CHOICE_START)+6 +#define DYN_CHOICE_VID_DEV_ID (DYN_CHOICE_START)+7 +#define DYN_CHOICE_STREAM_ID (DYN_CHOICE_START)+8 +#define DYN_CHOICE_VIDEO_CODEC_ID (DYN_CHOICE_START)+9 +#define DYN_CHOICE_WIN_ID (DYN_CHOICE_START)+10 +#define DYN_CHOICE_CALL_ID (DYN_CHOICE_START)+11 +#define DYN_CHOICE_ADDED_BUDDY_ID (DYN_CHOICE_START)+12 -pj_bool_t pjsua_restarted = PJ_TRUE; static pj_bool_t pj_inited = PJ_FALSE; static pj_caching_pool cli_cp; +static pj_bool_t cli_cp_inited = PJ_FALSE; static pj_cli_t *cli = NULL; static pj_cli_sess *cli_cons_sess = NULL; +static pj_cli_front_end *telnet_front_end = NULL; /** Forward declaration **/ -pj_status_t setup_command(pj_cli_t *cli); +pj_status_t cli_setup_command(pj_cli_t *cli); +void cli_destroy(); -static void log_writer(int level, const char *buffer, int len) +PJ_DEF(void) cli_get_info(char *info, pj_size_t size) { - if (cli) - pj_cli_write_log(cli, level, buffer, len); + pj_cli_telnet_info telnet_info; + pj_cli_telnet_get_info(telnet_front_end, &telnet_info); - if (app_config.disable_cli_console) - pj_log_write(level, buffer, len); + pj_ansi_snprintf(info, size, "Telnet to %.*s:%d", + telnet_info.ip_address.slen, telnet_info.ip_address.ptr, + telnet_info.port); } -void destroy_cli(pj_bool_t app_restart) -{ - pj_log_set_log_func(&pj_log_write); - - if (cli) { - pj_cli_destroy(cli); - cli = NULL; - } - - if (cli_cp.factory.create_pool) { - pj_caching_pool_destroy(&cli_cp); - pj_bzero(&cli_cp, sizeof(cli_cp)); - } - - if (pj_inited) { - pj_shutdown(); - pj_inited = PJ_FALSE; - } - if (!app_restart) { - if (cli_on_destroy_cb) - (*cli_on_destroy_cb)(); - } +static void cli_log_writer(int level, const char *buffer, int len) +{ + if (cli) + pj_cli_write_log(cli, level, buffer, len); } -pj_status_t setup_cli(pj_bool_t with_console, pj_bool_t with_telnet, - pj_uint16_t telnet_port, - pj_cli_telnet_on_started on_started_cb, - pj_cli_on_quit on_quit_cb, - pj_cli_on_destroy on_destroy_cb, - pj_cli_on_restart_pjsua on_restart_pjsua_cb) +pj_status_t cli_init() { - pj_cli_cfg cli_cfg; pj_status_t status; - /* Destroy CLI if initialized */ - destroy_cli(PJ_TRUE); + pj_cli_cfg *cfg = &app_config.cli_cfg.cfg; /* Init PJLIB */ status = pj_init(); @@ -101,55 +190,37 @@ pj_status_t setup_cli(pj_bool_t with_console, pj_bool_t with_telnet, /* Init CLI */ pj_caching_pool_init(&cli_cp, NULL, 0); - pj_cli_cfg_default(&cli_cfg); - cli_cfg.pf = &cli_cp.factory; - cli_cfg.name = pj_str("pjsua_cli"); - cli_cfg.title = pj_str("Pjsua CLI Application"); - status = pj_cli_create(&cli_cfg, &cli); + cli_cp_inited = PJ_TRUE; + cfg->pf = &cli_cp.factory; + cfg->name = pj_str("pjsua_cli"); + cfg->title = pj_str("Pjsua CLI Application"); + status = pj_cli_create(cfg, &cli); if (status != PJ_SUCCESS) goto on_error; - status = setup_command(cli); + status = cli_setup_command(cli); if (status != PJ_SUCCESS) goto on_error; - if (on_destroy_cb) - cli_on_destroy_cb = on_destroy_cb; - - if (on_restart_pjsua_cb) - cli_on_restart_pjsua_cb = on_restart_pjsua_cb; - - if (on_quit_cb) - cli_on_quit_cb = on_quit_cb; - /* Init telnet frontend */ - if (with_telnet) { - pj_cli_telnet_cfg telnet_cfg; + if (app_config.cli_cfg.cli_fe & CLI_FE_TELNET) { + pj_cli_telnet_cfg *fe_cfg = &app_config.cli_cfg.telnet_cfg; pj_pool_t *pool; - pool = pj_pool_create(&cli_cp.factory, "cli_cp", 128, 128, NULL); + pool = pj_pool_create(cfg->pf, "cli_cp", 128, 128, NULL); pj_assert(pool); - pj_cli_telnet_cfg_default(&telnet_cfg); - telnet_cfg.log_level = 5; - telnet_cfg.port = telnet_port; - if (on_started_cb) - cli_telnet_on_started_cb = on_started_cb; - - telnet_cfg.on_started = cli_telnet_on_started_cb; - - status = pj_cli_telnet_create(cli, &telnet_cfg, NULL); + status = pj_cli_telnet_create(cli, fe_cfg, &telnet_front_end); if (status != PJ_SUCCESS) goto on_error; } /* Init console frontend */ - if (with_console) { - pj_cli_console_cfg console_cfg; + if (app_config.cli_cfg.cli_fe & CLI_FE_CONSOLE) { + pj_cli_console_cfg *fe_cfg = &app_config.cli_cfg.console_cfg; - pj_cli_console_cfg_default(&console_cfg); - console_cfg.quit_command = pj_str("shutdown"); - status = pj_cli_console_create(cli, &console_cfg, + fe_cfg->quit_command = pj_str("shutdown"); + status = pj_cli_console_create(cli, fe_cfg, &cli_cons_sess, NULL); if (status != PJ_SUCCESS) goto on_error; @@ -158,112 +229,2866 @@ pj_status_t setup_cli(pj_bool_t with_console, pj_bool_t with_telnet, return PJ_SUCCESS; on_error: - destroy_cli(PJ_FALSE); + cli_destroy(); return status; } -PJ_DEF(pj_bool_t) is_cli_inited() +pj_status_t cli_main(pj_bool_t wait_telnet_cli) { - return (cli != NULL); + char cmdline[PJ_CLI_MAX_CMDBUF]; + + /* ReInit logging */ + app_config.log_cfg.cb = &cli_log_writer; + pjsua_reconfigure_logging(&app_config.log_cfg); + + if (app_config.cli_cfg.cli_fe & CLI_FE_CONSOLE) { + /* Main loop for CLI FE console */ + while (!pj_cli_is_quitting(cli)) { + pj_cli_console_process(cli_cons_sess, &cmdline[0], sizeof(cmdline)); + } + } else if (wait_telnet_cli) { + /* Just wait for CLI quit */ + while (!pj_cli_is_quitting(cli)) { + pj_thread_sleep(200); + } + } + + return PJ_SUCCESS; } -static pj_status_t setup_timer(pj_timer_heap_t **timer, - pj_ioqueue_t **ioqueue) +void cli_destroy() { - pj_status_t status = pj_timer_heap_create(app_config.pool, 16, timer); + /* Destroy CLI, it will automatically destroy any FEs */ + if (cli) { + pj_cli_destroy(cli); + cli = NULL; + } - if (status != PJ_SUCCESS) - return status; + /* Destroy CLI caching pool factory */ + if (cli_cp_inited) { + pj_caching_pool_destroy(&cli_cp); + cli_cp_inited = PJ_FALSE; + } - status = pj_ioqueue_create(app_config.pool, 16, ioqueue); + /* Shutdown PJLIB */ + if (pj_inited) { + pj_shutdown(); + pj_inited = PJ_FALSE; + } +} - return status; +/* Get input URL */ +static void get_input_url(char *buf, + int len, + pj_cli_cmd_val *cval, + struct input_result *result) +{ + static const pj_str_t err_invalid_input = {"Invalid input\n", 15}; + result->nb_result = PJSUA_APP_NO_NB; + result->uri_result = NULL; + + len = strlen(buf); + + /* Left trim */ + while (pj_isspace(*buf)) { + ++buf; + --len; + } + + /* Remove trailing newlines */ + while (len && (buf[len-1] == '\r' || buf[len-1] == '\n')) + buf[--len] = '\0'; + + if (len == 0 || buf[0]=='q') + return; + + if (pj_isdigit(*buf) || *buf=='-') { + + int i; + + if (*buf=='-') + i = 1; + else + i = 0; + + for (; isess, err_invalid_input.ptr, + err_invalid_input.slen); + return; + } + } + + result->nb_result = my_atoi(buf); + + if (result->nb_result >= 0 && + result->nb_result <= (int)pjsua_get_buddy_count()) + { + return; + } + if (result->nb_result == -1) + return; + + pj_cli_sess_write_msg(cval->sess, err_invalid_input.ptr, + err_invalid_input.slen); + result->nb_result = PJSUA_APP_NO_NB; + return; + + } else { + pj_status_t status; + + if ((status=pjsua_verify_url(buf)) != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Invalid URL", status); + return; + } + + result->uri_result = buf; + } } -static pj_status_t stop_timer(pj_timer_heap_t *timer, - pj_ioqueue_t *ioqueue) +/* CLI dynamic choice handler */ +/* Get buddy id */ +static void get_buddy_id(pj_cli_dyn_choice_param *param) { - if ((!timer) || (!ioqueue)) - return PJ_SUCCESS; + if (param->cnt < param->max_cnt) { + pjsua_buddy_id ids[64]; + int i = 0; + unsigned count = PJ_ARRAY_SIZE(ids); + char data_out[64]; + + pjsua_enum_buddies(ids, &count); + + if (count > 0) { + for (i=0; i<(int)count; ++i) { + pjsua_buddy_info info; + + if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS) + continue; - pj_timer_heap_destroy(timer); + /* Fill buddy id */ + pj_ansi_snprintf(data_out, sizeof(data_out), "%d", ids[i]+1); + pj_strdup2(param->pool, ¶m->choice[i].value, data_out); + pj_bzero(data_out, PJ_ARRAY_SIZE(data_out)); + + /* Format & fill description */ + pj_ansi_snprintf(data_out, + sizeof(data_out), + "<%.*s> %.*s", + (int)info.status_text.slen, + info.status_text.ptr, + (int)info.uri.slen, + info.uri.ptr); - return pj_ioqueue_destroy(ioqueue); + pj_strdup2(param->pool, ¶m->choice[i].desc, data_out); + if (++param->cnt >= (param->max_cnt-1)) + break; + } + } + if (param->arg_id == DYN_CHOICE_BUDDY_ID) { + /* Add URL input option */ + pj_ansi_snprintf(data_out, sizeof(data_out), "URL"); + pj_strdup2(param->pool, ¶m->choice[i].value, data_out); + pj_ansi_snprintf(data_out, sizeof(data_out), "An URL"); + pj_strdup2(param->pool, ¶m->choice[i].desc, data_out); + ++param->cnt; + } + } } -pj_status_t cli_pjsua_start(pj_str_t *uri_to_call, - pj_timer_heap_t **main_timer_heap, - pj_ioqueue_t **main_ioqueue) +static void get_account_id(pj_cli_dyn_choice_param *param) { - pj_status_t status = PJ_SUCCESS; + if (param->cnt < param->max_cnt) { + char buf[8]; + char buf_out[80]; + pjsua_acc_info info; - pjsua_restarted = PJ_FALSE; + pjsua_acc_id acc_ids[16]; + unsigned count = PJ_ARRAY_SIZE(acc_ids); + int i; - if (app_config.disable_cli_console) { - status = setup_timer(main_timer_heap, main_ioqueue); - if (status != PJ_SUCCESS) - return status; + pjsua_enum_accs(acc_ids, &count); + + for (i=0; i<(int)count; ++i) { + pj_bzero(&buf_out[0], PJ_ARRAY_SIZE(buf_out)); + + pjsua_acc_get_info(acc_ids[i], &info); + + pj_ansi_snprintf(buf_out, + sizeof(buf_out), + "%c%.*s", + (acc_ids[i]==current_acc?'*':' '), + (int)info.acc_uri.slen, + info.acc_uri.ptr); + + pj_bzero(buf, sizeof(buf)); + pj_ansi_snprintf(buf, sizeof(buf), "%d", acc_ids[i]); + pj_strdup2(param->pool, ¶m->choice[i].value, buf); + pj_strdup2(param->pool, ¶m->choice[i].desc, buf_out); + if (++param->cnt >= param->max_cnt) + break; + } } +} - status = pjsua_start(); - if (status != PJ_SUCCESS) - return status; +static void get_media_port(pj_cli_dyn_choice_param *param) +{ + unsigned i, count; + pjsua_conf_port_id id[PJSUA_MAX_CONF_PORTS]; + + count = PJ_ARRAY_SIZE(id); + pjsua_enum_conf_ports(id, &count); + + for (i=0; ipool, ¶m->choice[i].value, slot_id); - setup_signal_handler(); + txlist[0] = '\0'; + for (j=0; jslen) { - pjsua_call_make_call(current_acc, uri_to_call, &call_opt, NULL, - NULL, NULL); + pj_ansi_snprintf(desc, + sizeof(desc), + "[%2dKHz/%dms/%d] %20.*s transmitting to: %s", + info.clock_rate/1000, + info.samples_per_frame*1000/info.channel_count/info.clock_rate, + info.channel_count, + (int)info.name.slen, + info.name.ptr, + txlist); + + pj_strdup2(param->pool, ¶m->choice[i].desc, desc); + if (++param->cnt >= param->max_cnt) + break; } +} - if (!app_config.disable_cli_console) - PJ_LOG(3,(THIS_FILE, "CLI console is ready, press '?' for help")); +static void get_audio_codec_id(pj_cli_dyn_choice_param *param) +{ + if (param->cnt < param->max_cnt) { + pjsua_codec_info c[32]; + unsigned i, count = PJ_ARRAY_SIZE(c); + char codec_id[64]; + char desc[128]; + + pjsua_enum_codecs(c, &count); + for (i=0; ipool, ¶m->choice[param->cnt].value, codec_id); + + pj_ansi_snprintf(desc, sizeof(desc), + "Audio, prio: %d%s%.*s", + c[i].priority, + c[i].desc.slen? " - ":"", + (int)c[i].desc.slen, + c[i].desc.ptr); + + pj_strdup2(param->pool, ¶m->choice[param->cnt].desc, desc); + if (++param->cnt >= param->max_cnt) + break; + } + } +} + +#if PJSUA_HAS_VIDEO +static void get_video_stream_id(pj_cli_dyn_choice_param *param) +{ + if (param->cnt < param->max_cnt) { + pjsua_call_info call_info; + + if (current_call != PJSUA_INVALID_ID) { + unsigned i; + pjsua_call_get_info(current_call, &call_info); + for (i=0; ipool, ¶m->choice[i].value, med_idx); + + switch (call_info.media[i].status) { + case PJSUA_CALL_MEDIA_NONE: + pj_strdup2(param->pool, ¶m->choice[i].desc, + "Status:None"); + break; + case PJSUA_CALL_MEDIA_ACTIVE: + pj_strdup2(param->pool, ¶m->choice[i].desc, + "Status:Active"); + break; + case PJSUA_CALL_MEDIA_LOCAL_HOLD: + pj_strdup2(param->pool, ¶m->choice[i].desc, + "Status:Local Hold"); + break; + case PJSUA_CALL_MEDIA_REMOTE_HOLD: + pj_strdup2(param->pool, ¶m->choice[i].desc, + "Status:Remote Hold"); + break; + case PJSUA_CALL_MEDIA_ERROR: + pj_strdup2(param->pool, ¶m->choice[i].desc, + "Status:Media Error"); + break; + } + if (++param->cnt >= param->max_cnt) + break; + } + } + } + } +} + +static void get_video_dev_hint(pj_cli_dyn_choice_param *param, + pjmedia_vid_dev_info *vdi, + unsigned vid_dev_id) +{ + char desc[128]; + char dev_id[8]; + pj_ansi_snprintf(dev_id, sizeof(dev_id), + "%d", vid_dev_id); + pj_ansi_snprintf(desc, sizeof(desc), "%s [%s]", + vdi->name, vdi->driver); + + pj_strdup2(param->pool, ¶m->choice[param->cnt].value, + dev_id); + pj_strdup2(param->pool, ¶m->choice[param->cnt].desc, + desc); +} + +static void get_video_dev_id(pj_cli_dyn_choice_param *param, + pj_bool_t all, + pj_bool_t capture) +{ + if (param->cnt < param->max_cnt) { + unsigned i, count; + pjmedia_vid_dev_info vdi; + pj_status_t status; + + count = pjsua_vid_dev_count(); + if (count == 0) { + return; + } + + for (i=0; icnt >= param->max_cnt) + break; + } + } + } + } } -void start_cli_main(pj_str_t *uri_to_call, pj_bool_t *app_restart) +static void get_video_codec_id(pj_cli_dyn_choice_param *param) { + if (param->cnt < param->max_cnt) { + pjsua_codec_info ci[32]; + unsigned i, count = PJ_ARRAY_SIZE(ci); + char codec_id[64]; + char desc[128]; + + pjsua_vid_enum_codecs(ci, &count); + for (i=0; ipool, ¶m->choice[param->cnt].value, codec_id); + + pj_ansi_snprintf(desc, sizeof(desc), + "Video, p[%d], f[%.2f], b[%d/%d], s[%dx%d]", + ci[i].priority, + (vfd->fps.num*1.0/vfd->fps.denum), + vfd->avg_bps/1000, vfd->max_bps/1000, + vfd->size.w, vfd->size.h); + + pj_strdup2(param->pool, ¶m->choice[param->cnt].desc, desc); + if (++param->cnt >= param->max_cnt) + break; + } + } +} + +static void get_video_window_id(pj_cli_dyn_choice_param *param) +{ + if (param->cnt < param->max_cnt) { + pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS]; + unsigned i, cnt = PJ_ARRAY_SIZE(wids); + char win_id[64]; + char desc[128]; + + pjsua_vid_enum_wins(wids, &cnt); + + for (i = 0; i < cnt; ++i) { + pjsua_vid_win_info wi; + + pjsua_vid_win_get_info(wids[i], &wi); + pj_ansi_snprintf(win_id, sizeof(win_id), "%d", wids[i]); + pj_strdup2(param->pool, ¶m->choice[i].value, win_id); + + pj_ansi_snprintf(desc, sizeof(desc), + "Show:%c Pos(%d,%d) Size(%dx%d)", + (wi.show?'Y':'N'), wi.pos.x, wi.pos.y, + wi.size.w, wi.size.h); + + pj_strdup2(param->pool, ¶m->choice[i].desc, desc); + if (++param->cnt >= param->max_cnt) + break; + } + } +} + +static void get_call_id(pj_cli_dyn_choice_param *param) +{ + if (param->cnt < param->max_cnt) { + char call_id[64]; + char desc[128]; + unsigned i, count; + pjsua_call_id ids[PJSUA_MAX_CALLS]; + int call = current_call; + + count = PJ_ARRAY_SIZE(ids); + pjsua_enum_calls(ids, &count); + + if (count > 1) { + for (i=0; ipool, ¶m->choice[i].value, call_id); + pj_ansi_snprintf(desc, sizeof(desc), "%.*s [%.*s]", + (int)call_info.remote_info.slen, + call_info.remote_info.ptr, + (int)call_info.state_text.slen, + call_info.state_text.ptr); + pj_strdup2(param->pool, ¶m->choice[i].desc, desc); + if (++param->cnt >= param->max_cnt) + break; + + } + } + } +} + +#endif + +static void get_choice_value(pj_cli_dyn_choice_param *param) +{ + switch (param->arg_id) { + case DYN_CHOICE_BUDDY_ID: + case DYN_CHOICE_ADDED_BUDDY_ID: + get_buddy_id(param); + break; + case DYN_CHOICE_ACCOUNT_ID: + get_account_id(param); + break; + case DYN_CHOICE_MEDIA_PORT: + get_media_port(param); + break; + case DYN_CHOICE_AUDIO_CODEC_ID: + get_audio_codec_id(param); + break; +#if PJSUA_HAS_VIDEO + case DYN_CHOICE_CAP_DEV_ID: + case DYN_CHOICE_REN_DEV_ID: + case DYN_CHOICE_VID_DEV_ID: + get_video_dev_id(param, + (param->arg_id==DYN_CHOICE_VID_DEV_ID), + (param->arg_id==DYN_CHOICE_CAP_DEV_ID)); + break; + case DYN_CHOICE_STREAM_ID: + get_video_stream_id(param); + break; + case DYN_CHOICE_VIDEO_CODEC_ID: + get_video_codec_id(param); + break; + case DYN_CHOICE_WIN_ID: + get_video_window_id(param); + break; + case DYN_CHOICE_CALL_ID: + get_call_id(param); + break; +#endif + default: + param->cnt = 0; + break; + } +} + +/* + * CLI command handler + */ + +/* Add account */ +static pj_status_t cmd_add_account(pj_cli_cmd_val *cval) +{ + pjsua_acc_config acc_cfg; pj_status_t status; - char cmdline[PJ_CLI_MAX_CMDBUF]; - pj_timer_heap_t *main_timer_heap = NULL; - pj_ioqueue_t *main_ioqueue = NULL; - *app_restart = PJ_FALSE; - pjsua_restarted = PJ_TRUE; + pjsua_acc_config_default(&acc_cfg); + acc_cfg.id = cval->argv[1]; + acc_cfg.reg_uri = cval->argv[2]; + acc_cfg.cred_count = 1; + acc_cfg.cred_info[0].scheme = pj_str("Digest"); + acc_cfg.cred_info[0].realm = cval->argv[3]; + acc_cfg.cred_info[0].username = cval->argv[4]; + acc_cfg.cred_info[0].data_type = 0; + acc_cfg.cred_info[0].data = cval->argv[5]; - do { - if (pjsua_restarted) { - status = cli_pjsua_start(uri_to_call, &main_timer_heap, - &main_ioqueue); + acc_cfg.rtp_cfg = app_config.rtp_cfg; + app_config_init_video(&acc_cfg); + + status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Error adding new account", status); + } + + return status; +} + +/* Delete account */ +static pj_status_t cmd_del_account(pj_cli_cmd_val *cval) +{ + char out_str[64]; + unsigned str_len; + + int i = my_atoi(cval->argv[1].ptr); + + if (!pjsua_acc_is_valid(i)) { + pj_ansi_snprintf(out_str, sizeof(out_str), + "Invalid account id %d\n", i); + str_len = pj_ansi_strlen(out_str); + pj_cli_sess_write_msg(cval->sess, out_str, str_len); + } else { + pjsua_acc_del(i); + pj_ansi_snprintf(out_str, sizeof(out_str), + "Account %d deleted\n", i); + str_len = pj_ansi_strlen(out_str); + pj_cli_sess_write_msg(cval->sess, out_str, str_len); + } + return PJ_SUCCESS; +} + +/* Modify account */ +static pj_status_t cmd_mod_account(pj_cli_cmd_val *cval) +{ + PJ_UNUSED_ARG(cval); + return PJ_SUCCESS; +} + +/* Register account */ +static pj_status_t cmd_reg_account() +{ + pjsua_acc_set_registration(current_acc, PJ_TRUE); + return PJ_SUCCESS; +} + +/* Unregister account */ +static pj_status_t cmd_unreg_account() +{ + pjsua_acc_set_registration(current_acc, PJ_FALSE); + return PJ_SUCCESS; +} + +/* Select account to be used for sending outgoing request */ +static pj_status_t cmd_next_account(pj_cli_cmd_val *cval) +{ + int i = my_atoi(cval->argv[1].ptr); + if (pjsua_acc_is_valid(i)) { + pjsua_acc_set_default(i); + PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i)); + } else { + PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i)); + } + return PJ_SUCCESS; +} + +/* Show account list */ +static pj_status_t cmd_show_account(pj_cli_cmd_val *cval) +{ + pjsua_acc_id acc_ids[16]; + unsigned count = PJ_ARRAY_SIZE(acc_ids); + int i; + static const pj_str_t header = {"Account list:\n", 15}; + + pjsua_enum_accs(acc_ids, &count); + + pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen); + for (i=0; i<(int)count; ++i) { + char acc_info[80]; + char out_str[160]; + pjsua_acc_info info; + + pjsua_acc_get_info(acc_ids[i], &info); + + if (!info.has_registration) { + pj_ansi_snprintf(acc_info, sizeof(acc_info), "%.*s", + (int)info.status_text.slen, + info.status_text.ptr); + + } else { + pj_ansi_snprintf(acc_info, sizeof(acc_info), + "%d/%.*s (expires=%d)", + info.status, + (int)info.status_text.slen, + info.status_text.ptr, + info.expires); - if (status != PJ_SUCCESS) - return; } - if (app_config.disable_cli_console) { - pj_time_val delay = {0, 10}; - pj_ioqueue_poll(main_ioqueue, &delay); - if (pj_cli_is_quitting(cli)) - continue; - pj_timer_heap_poll(main_timer_heap, NULL); + pj_ansi_snprintf(out_str, sizeof(out_str), + " %c[%2d] %.*s: %s\n", + (acc_ids[i]==current_acc?'*':' '), acc_ids[i], + (int)info.acc_uri.slen, info.acc_uri.ptr, + acc_info); + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + + pj_bzero(out_str, sizeof(out_str)); + pj_ansi_snprintf(out_str, sizeof(out_str), + " Online status: %.*s\n", + (int)info.online_status_text.slen, + info.online_status_text.ptr); + + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + } + + return PJ_SUCCESS; +} + +/* Account command handler */ +pj_status_t cmd_account_handler(pj_cli_cmd_val *cval) +{ + pj_status_t status = PJ_SUCCESS; + + CHECK_PJSUA_RUNNING(); + + switch(pj_cli_get_cmd_id(cval->cmd)) { + case CMD_ACCOUNT_ADD: + status = cmd_add_account(cval); + break; + case CMD_ACCOUNT_DEL: + status = cmd_del_account(cval); + break; + case CMD_ACCOUNT_MOD: + status = cmd_mod_account(cval); + break; + case CMD_ACCOUNT_REG: + status = cmd_reg_account(); + break; + case CMD_ACCOUNT_UNREG: + status = cmd_unreg_account(); + break; + case CMD_ACCOUNT_NEXT: + case CMD_ACCOUNT_PREV: + status = cmd_next_account(cval); + break; + case CMD_ACCOUNT_SHOW: + status = cmd_show_account(cval); + break; + } + return status; +} + +/* Add buddy */ +static pj_status_t cmd_add_buddy(pj_cli_cmd_val *cval) +{ + char out_str[80]; + pjsua_buddy_config buddy_cfg; + pjsua_buddy_id buddy_id; + pj_status_t status = PJ_SUCCESS; + cval->argv[1].ptr[cval->argv[1].slen] = 0; + + if (pjsua_verify_url(cval->argv[1].ptr) != PJ_SUCCESS) { + pj_ansi_snprintf(out_str, sizeof(out_str), + "Invalid URI '%s'\n", cval->argv[1].ptr); + } else { + pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config)); + + buddy_cfg.uri = pj_str(cval->argv[1].ptr); + buddy_cfg.subscribe = PJ_TRUE; + + status = pjsua_buddy_add(&buddy_cfg, &buddy_id); + if (status == PJ_SUCCESS) { + pj_ansi_snprintf(out_str, sizeof(out_str), + "New buddy '%s' added at index %d\n", + cval->argv[1].ptr, buddy_id+1); + } + } + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + return status; +} + +/* Delete buddy */ +static pj_status_t cmd_del_buddy(pj_cli_cmd_val *cval) +{ + int i = my_atoi(cval->argv[1].ptr) - 1; + char out_str[80]; + + if (!pjsua_buddy_is_valid(i)) { + pj_ansi_snprintf(out_str, sizeof(out_str), + "Invalid buddy id %d\n", i); + } else { + pjsua_buddy_del(i); + pj_ansi_snprintf(out_str, sizeof(out_str), + "Buddy %d deleted\n", i); + } + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + return PJ_SUCCESS; +} + +/* Send IM */ +static pj_status_t cmd_send_im(pj_cli_cmd_val *cval) +{ + int i = -1; + struct input_result result; + char dest[64]; + pj_str_t tmp = pj_str(dest); + + /* make compiler happy. */ + char *uri = NULL; + + pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest)); + + /* input destination. */ + get_input_url(tmp.ptr, tmp.slen, cval, &result); + if (result.nb_result != PJSUA_APP_NO_NB) { + + if (result.nb_result == -1) { + static const pj_str_t err_msg = {"you can't send broadcast im " + "like that!\n", 40 }; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + return PJ_SUCCESS; + } else if (result.nb_result == 0) { + i = current_call; } else { - pj_cli_console_process(cli_cons_sess, &cmdline[0], sizeof(cmdline)); + pjsua_buddy_info binfo; + pjsua_buddy_get_info(result.nb_result-1, &binfo); + pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(dest)); + uri = tmp.ptr; } - if (pjsua_restarted) { - status = stop_timer(main_timer_heap, main_ioqueue); - if (status != PJ_SUCCESS) - return; - - status = app_init(NULL, NULL, NULL, NULL); - if (status != PJ_SUCCESS) - return; + + } else if (result.uri_result) { + uri = result.uri_result; + } + + /* send typing indication. */ + if (i != -1) + pjsua_call_send_typing_ind(i, PJ_TRUE, NULL); + else { + pj_str_t tmp_uri = pj_str(uri); + pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL); + } + + /* send the im */ + if (i != -1) + pjsua_call_send_im(i, NULL, &cval->argv[2], NULL, NULL); + else { + pj_str_t tmp_uri = pj_str(uri); + pjsua_im_send(current_acc, &tmp_uri, NULL, &cval->argv[2], NULL, NULL); + } + return PJ_SUCCESS; +} + +/* Subscribe/unsubscribe presence */ +static pj_status_t cmd_subs_pres(pj_cli_cmd_val *cval, pj_bool_t subscribe) +{ + struct input_result result; + char dest[64] = {0}; + pj_str_t tmp = pj_str(dest); + + pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest)); + get_input_url(tmp.ptr, tmp.slen, cval, &result); + if (result.nb_result != PJSUA_APP_NO_NB) { + if (result.nb_result == -1) { + int i, count; + count = pjsua_get_buddy_count(); + for (i=0; isess, err_msg.ptr, err_msg.slen); + } else { + pjsua_buddy_subscribe_pres(result.nb_result-1, subscribe); } - } while (!pj_cli_is_quitting(cli)); - stop_timer(main_timer_heap, main_ioqueue); - *app_restart = pj_cli_is_restarting(cli); + } else if (result.uri_result) { + static const pj_str_t err_msg = {"Sorry, can only subscribe to " + "buddy's presence, not arbitrary " + "URL (for now)\n", 76}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + } + return PJ_SUCCESS; +} + +/* Toggle online state */ +static pj_status_t cmd_toggle_state(pj_cli_cmd_val *cval) +{ + char out_str[128]; + pjsua_acc_info acc_info; + + pjsua_acc_get_info(current_acc, &acc_info); + acc_info.online_status = !acc_info.online_status; + pjsua_acc_set_online_status(current_acc, acc_info.online_status); + pj_ansi_snprintf(out_str, sizeof(out_str), + "Setting %s online status to %s\n", + acc_info.acc_uri.ptr, + (acc_info.online_status?"online":"offline")); + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + return PJ_SUCCESS; +} + +/* Set presence text */ +static pj_status_t cmd_set_presence_text(pj_cli_cmd_val *cval) +{ + pjrpid_element elem; + int choice; + pj_bool_t online_status; + + enum { + AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX + }; + + choice = pj_strtol(&cval->argv[1]) - 1; + + pj_bzero(&elem, sizeof(elem)); + elem.type = PJRPID_ELEMENT_TYPE_PERSON; + + online_status = PJ_TRUE; + + switch (choice) { + case AVAILABLE: + break; + case BUSY: + elem.activity = PJRPID_ACTIVITY_BUSY; + elem.note = pj_str("Busy"); + break; + case OTP: + elem.activity = PJRPID_ACTIVITY_BUSY; + elem.note = pj_str("On the phone"); + break; + case IDLE: + elem.activity = PJRPID_ACTIVITY_UNKNOWN; + elem.note = pj_str("Idle"); + break; + case AWAY: + elem.activity = PJRPID_ACTIVITY_AWAY; + elem.note = pj_str("Away"); + break; + case BRB: + elem.activity = PJRPID_ACTIVITY_UNKNOWN; + elem.note = pj_str("Be right back"); + break; + case OFFLINE: + online_status = PJ_FALSE; + break; + } + pjsua_acc_set_online_status2(current_acc, online_status, &elem); + return PJ_SUCCESS; +} + +/* Show buddy list */ +static pj_status_t cmd_show_buddy(pj_cli_cmd_val *cval) +{ + pjsua_buddy_id ids[64]; + int i; + unsigned count = PJ_ARRAY_SIZE(ids); + static const pj_str_t header = {"Buddy list:\n", 13}; + char out_str[64]; + + pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen); + + pjsua_enum_buddies(ids, &count); + + if (count == 0) { + pj_ansi_snprintf(out_str, sizeof(out_str), " -none-\n"); + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + } else { + for (i=0; i<(int)count; ++i) { + pjsua_buddy_info info; + pj_bzero(out_str, sizeof(out_str)); + + if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS) + continue; + + pj_ansi_snprintf(out_str, sizeof(out_str), + " [%2d] <%.*s> %.*s\n", + ids[i]+1, + (int)info.status_text.slen, + info.status_text.ptr, + (int)info.uri.slen, + info.uri.ptr); + + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + } + } + return PJ_SUCCESS; +} + +/* Presence/buddy command handler */ +pj_status_t cmd_presence_handler(pj_cli_cmd_val *cval) +{ + pj_status_t status = PJ_SUCCESS; + + CHECK_PJSUA_RUNNING(); + + switch(pj_cli_get_cmd_id(cval->cmd)) { + case CMD_PRESENCE_ADD_BUDDY: + status = cmd_add_buddy(cval); + break; + case CMD_PRESENCE_DEL_BUDDY: + status = cmd_del_buddy(cval); + break; + case CMD_PRESENCE_SEND_IM: + status = cmd_send_im(cval); + break; + case CMD_PRESENCE_SUB: + case CMD_PRESENCE_UNSUB: + status = cmd_subs_pres(cval, + pj_cli_get_cmd_id(cval->cmd)==CMD_PRESENCE_SUB); + break; + case CMD_PRESENCE_TOG_STATE: + status = cmd_toggle_state(cval); + break; + case CMD_PRESENCE_TEXT: + status = cmd_set_presence_text(cval); + break; + case CMD_PRESENCE_LIST: + status = cmd_show_buddy(cval); + break; + } + + return status; +} + +/* Show conference list */ +static pj_status_t cmd_media_list(pj_cli_cmd_val *cval) +{ + unsigned i, count; + pjsua_conf_port_id id[PJSUA_MAX_CONF_PORTS]; + static const pj_str_t header = {"Conference ports:\n", 19}; + + pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen); + + count = PJ_ARRAY_SIZE(id); + pjsua_enum_conf_ports(id, &count); + + for (i=0; isess, out_str, pj_ansi_strlen(out_str)); + } + return PJ_SUCCESS; +} + +/* Conference connect/disconnect */ +static pj_status_t cmd_media_connect(pj_cli_cmd_val *cval, pj_bool_t connect) +{ + pj_status_t status; + + if (connect) + status = pjsua_conf_connect(pj_strtol(&cval->argv[1]), + pj_strtol(&cval->argv[2])); + else + status = pjsua_conf_disconnect(pj_strtol(&cval->argv[1]), + pj_strtol(&cval->argv[2])); + + if (status == PJ_SUCCESS) { + static const pj_str_t success_msg = {"Success\n", 9}; + pj_cli_sess_write_msg(cval->sess, success_msg.ptr, success_msg.slen); + } else { + static const pj_str_t err_msg = {"ERROR!!\n", 9}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + } + return status; +} + +/* Adjust audio volume */ +static pj_status_t cmd_adjust_vol(pj_cli_cmd_val *cval) +{ + char buf[80]; + float orig_level; + char *err; + char level_val[16] = {0}; + pj_str_t tmp = pj_str(level_val); + + /* Adjust mic level */ + orig_level = app_config.mic_level; + pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(level_val)); + app_config.mic_level = (float)strtod(level_val, &err); + pjsua_conf_adjust_rx_level(0, app_config.mic_level); + + pj_ansi_snprintf(buf, sizeof(buf), + "Adjust mic level: [%4.1fx] -> [%4.1fx]\n", + orig_level, app_config.mic_level); + + pj_cli_sess_write_msg(cval->sess, buf, pj_ansi_strlen(buf)); + + /* Adjust speaker level */ + orig_level = app_config.speaker_level; + pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(level_val)); + app_config.speaker_level = (float)strtod(level_val, &err); + pjsua_conf_adjust_tx_level(0, app_config.speaker_level); + + pj_ansi_snprintf(buf, sizeof(buf), + "Adjust speaker level: [%4.1fx] -> [%4.1fx]\n", + orig_level, app_config.speaker_level); + + pj_cli_sess_write_msg(cval->sess, buf, pj_ansi_strlen(buf)); + + return PJ_SUCCESS; +} + +/* Set codec priority */ +static pj_status_t cmd_set_codec_prio(pj_cli_cmd_val *cval) +{ + int new_prio; + pj_status_t status; + + new_prio = pj_strtol(&cval->argv[2]); + if (new_prio < 0) + new_prio = 0; + else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST) + new_prio = PJMEDIA_CODEC_PRIO_HIGHEST; + + status = pjsua_codec_set_priority(&cval->argv[1], + (pj_uint8_t)new_prio); +#if PJSUA_HAS_VIDEO + if (status != PJ_SUCCESS) { + status = pjsua_vid_codec_set_priority(&cval->argv[1], + (pj_uint8_t)new_prio); + } +#endif + if (status != PJ_SUCCESS) + pjsua_perror(THIS_FILE, "Error setting codec priority", status); + + return status; +} + +/* Conference/media command handler */ +pj_status_t cmd_media_handler(pj_cli_cmd_val *cval) +{ + pj_status_t status = PJ_SUCCESS; + + CHECK_PJSUA_RUNNING(); + + switch(pj_cli_get_cmd_id(cval->cmd)) { + case CMD_MEDIA_LIST: + status = cmd_media_list(cval); + break; + case CMD_MEDIA_CONF_CONNECT: + case CMD_MEDIA_CONF_DISCONNECT: + status = cmd_media_connect(cval, + pj_cli_get_cmd_id(cval->cmd)==CMD_MEDIA_CONF_CONNECT); + break; + case CMD_MEDIA_ADJUST_VOL: + status = cmd_adjust_vol(cval); + break; + case CMD_MEDIA_CODEC_PRIO: + status = cmd_set_codec_prio(cval); + break; + } + + return status; +} + +/* Dump status */ +static pj_status_t cmd_stat_dump(pj_bool_t detail) +{ + pjsua_dump(detail); + return PJ_SUCCESS; +} + +static pj_status_t cmd_show_config() +{ + char settings[2000]; + int len; + + len = write_settings(&app_config, settings, sizeof(settings)); + if (len < 1) + PJ_LOG(1,(THIS_FILE, "Error: not enough buffer")); + else + PJ_LOG(3,(THIS_FILE, + "Dumping configuration (%d bytes):\n%s\n", + len, settings)); + + return PJ_SUCCESS; +} + +static pj_status_t cmd_write_config(pj_cli_cmd_val *cval) +{ + char settings[2000]; + char buf[128] = {0}; + int len; + pj_str_t tmp = pj_str(buf); + + pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(buf)); + + len = write_settings(&app_config, settings, sizeof(settings)); + if (len < 1) + PJ_LOG(1,(THIS_FILE, "Error: not enough buffer")); + else { + pj_oshandle_t fd; + pj_status_t status; + + status = pj_file_open(app_config.pool, buf, PJ_O_WRONLY, &fd); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Unable to open file", status); + } else { + char out_str[256]; + pj_ssize_t size = len; + pj_file_write(fd, settings, &size); + pj_file_close(fd); + + pj_ansi_snprintf(out_str, sizeof(out_str), + "Settings successfully written to '%s'\n", buf); + + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + } + } + + return PJ_SUCCESS; +} + +/* Status and config command handler */ +pj_status_t cmd_config_handler(pj_cli_cmd_val *cval) +{ + pj_status_t status = PJ_SUCCESS; + + CHECK_PJSUA_RUNNING(); + + switch(pj_cli_get_cmd_id(cval->cmd)) { + case CMD_CONFIG_DUMP_STAT: + status = cmd_stat_dump(PJ_FALSE); + break; + case CMD_CONFIG_DUMP_DETAIL: + status = cmd_stat_dump(PJ_TRUE); + break; + case CMD_CONFIG_DUMP_CONF: + status = cmd_show_config(); + break; + case CMD_CONFIG_WRITE_SETTING: + status = cmd_write_config(cval); + break; + } + + return status; +} + +/* Make single call */ +static pj_status_t cmd_make_single_call(pj_cli_cmd_val *cval) +{ + struct input_result result; + char dest[64] = {0}; + char out_str[128]; + pj_str_t tmp = pj_str(dest); + + pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest)); + + pj_ansi_snprintf(out_str, + sizeof(out_str), + "(You currently have %d calls)\n", + pjsua_call_get_count()); + + pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); + + /* input destination. */ + get_input_url(tmp.ptr, tmp.slen, cval, &result); + if (result.nb_result != PJSUA_APP_NO_NB) { + pjsua_buddy_info binfo; + if (result.nb_result == -1 || result.nb_result == 0) { + static const pj_str_t err_msg = + {"You can't do that with make call!\n", 35}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + return PJ_SUCCESS; + } + pjsua_buddy_get_info(result.nb_result-1, &binfo); + pj_strncpy(&tmp, &binfo.uri, sizeof(dest)); + } else if (result.uri_result) { + tmp = pj_str(result.uri_result); + } else { + tmp.slen = 0; + } + + pjsua_msg_data_init(&msg_data); + TEST_MULTIPART(&msg_data); + pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL, + &msg_data, ¤t_call); + return PJ_SUCCESS; +} + +/* Make multi call */ +static pj_status_t cmd_make_multi_call(pj_cli_cmd_val *cval) +{ + struct input_result result; + char dest[64] = {0}; + char out_str[128]; + int i, count; + pj_str_t tmp = pj_str(dest); + + pj_ansi_snprintf(out_str, + sizeof(out_str), + "(You currently have %d calls)\n", + pjsua_call_get_count()); + + count = pj_strtol(&cval->argv[1]); + if (count < 1) + return PJ_SUCCESS; + + pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(dest)); + + /* input destination. */ + get_input_url(tmp.ptr, tmp.slen, cval, &result); + if (result.nb_result != PJSUA_APP_NO_NB) { + pjsua_buddy_info binfo; + if (result.nb_result == -1 || result.nb_result == 0) { + static const pj_str_t err_msg = + {"You can't do that with make call!\n", 35}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + return PJ_SUCCESS; + } + pjsua_buddy_get_info(result.nb_result-1, &binfo); + pj_strncpy(&tmp, &binfo.uri, sizeof(dest)); + } else { + tmp = pj_str(result.uri_result); + } + + for (i=0; i= PJSIP_INV_STATE_CONNECTING) + { + static const pj_str_t err_msg = {"No pending incoming call\n", 26}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + + } else { + int st_code; + char contact[120]; + pj_str_t hname = { "Contact", 7 }; + pj_str_t hvalue; + pjsip_generic_string_hdr hcontact; + + st_code = pj_strtol(&cval->argv[1]); + if ((st_code < 100) || (st_code > 699)) + return PJ_SUCCESS; + + pjsua_msg_data_init(&msg_data); + + if (st_code/100 == 3) { + if (cval->argc < 3) { + static const pj_str_t err_msg = {"Enter URL to be put " + "in Contact\n", 32}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + return PJ_SUCCESS; + } + + hvalue = pj_str(contact); + pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue); + + pj_list_push_back(&msg_data.hdr_list, &hcontact); + } + + /* + * Must check again! + * Call may have been disconnected while we're waiting for + * keyboard input. + */ + if (current_call == PJSUA_INVALID_ID) { + static const pj_str_t err_msg = {"Call has been disconnected\n", + 28}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + } + + pjsua_call_answer2(current_call, &call_opt, st_code, NULL, &msg_data); + } + return PJ_SUCCESS; +} + +/* Hangup call */ +static pj_status_t cmd_hangup_call(pj_cli_cmd_val *cval, pj_bool_t all) +{ + if (current_call == PJSUA_INVALID_ID) { + static const pj_str_t err_msg = {"No current call\n", 17}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + } else { + if (all) + pjsua_call_hangup_all(); + else + pjsua_call_hangup(current_call, 0, NULL, NULL); + } + return PJ_SUCCESS; +} + +/* Hold call */ +static pj_status_t cmd_hold_call() +{ + if (current_call != PJSUA_INVALID_ID) { + pjsua_call_set_hold(current_call, NULL); + + } else { + PJ_LOG(3,(THIS_FILE, "No current call")); + } + return PJ_SUCCESS; +} + +/* Call reinvite */ +static pj_status_t cmd_call_reinvite() +{ + if (current_call != PJSUA_INVALID_ID) { + /* + * re-INVITE + */ + call_opt.flag |= PJSUA_CALL_UNHOLD; + pjsua_call_reinvite2(current_call, &call_opt, NULL); + + } else { + PJ_LOG(3,(THIS_FILE, "No current call")); + } + return PJ_SUCCESS; +} + +/* Send update */ +static pj_status_t cmd_call_update() +{ + if (current_call != PJSUA_INVALID_ID) { + pjsua_call_update2(current_call, &call_opt, NULL); + } else { + PJ_LOG(3,(THIS_FILE, "No current call")); + } + return PJ_SUCCESS; +} + +/* Select next call */ +static pj_status_t cmd_next_call(pj_bool_t next) +{ + /* + * Cycle next/prev dialog. + */ + if (next) { + find_next_call(current_call); + } else { + find_prev_call(current_call); + } + + if (current_call != PJSUA_INVALID_ID) { + pjsua_call_info call_info; + + pjsua_call_get_info(current_call, &call_info); + PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s", + (int)call_info.remote_info.slen, + call_info.remote_info.ptr)); + + } else { + PJ_LOG(3,(THIS_FILE,"No current dialog")); + } + return PJ_SUCCESS; +} + +/* Transfer call */ +static pj_status_t cmd_transfer_call(pj_cli_cmd_val *cval) +{ + if (current_call == PJSUA_INVALID_ID) { + + PJ_LOG(3,(THIS_FILE, "No current call")); + + } else { + char out_str[64]; + int call = current_call; + char dest[64] = {0}; + pj_str_t tmp = pj_str(dest); + struct input_result result; + pjsip_generic_string_hdr refer_sub; + pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 }; + pj_str_t STR_FALSE = { "false", 5 }; + pjsua_call_info ci; + + pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest)); + + pjsua_call_get_info(current_call, &ci); + pj_ansi_snprintf(out_str, + sizeof(out_str), + "Transfering current call [%d] %.*s\n", + current_call, + (int)ci.remote_info.slen, + ci.remote_info.ptr); + + get_input_url(tmp.ptr, tmp.slen, cval, &result); + + /* Check if call is still there. */ + + if (call != current_call) { + puts("Call has been disconnected"); + return PJ_SUCCESS; + } + + pjsua_msg_data_init(&msg_data); + if (app_config.no_refersub) { + /* Add Refer-Sub: false in outgoing REFER request */ + pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB, + &STR_FALSE); + pj_list_push_back(&msg_data.hdr_list, &refer_sub); + } + if (result.nb_result != PJSUA_APP_NO_NB) { + if (result.nb_result == -1 || result.nb_result == 0) { + static const pj_str_t err_msg = {"You can't do that with " + "transfer call!\n", 39}; + + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + } else { + pjsua_buddy_info binfo; + pjsua_buddy_get_info(result.nb_result-1, &binfo); + pjsua_call_xfer( current_call, &binfo.uri, &msg_data); + } + } else if (result.uri_result) { + pj_str_t tmp; + tmp = pj_str(result.uri_result); + pjsua_call_xfer( current_call, &tmp, &msg_data); + } + } + return PJ_SUCCESS; +} + +/* Transfer call */ +static pj_status_t cmd_transfer_replace_call(pj_cli_cmd_val *cval) +{ + if (current_call == -1) { + PJ_LOG(3,(THIS_FILE, "No current call")); + } else { + int call = current_call; + int dst_call; + pjsip_generic_string_hdr refer_sub; + pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 }; + pj_str_t STR_FALSE = { "false", 5 }; + pjsua_call_id ids[PJSUA_MAX_CALLS]; + pjsua_msg_data msg_data; + char buf[8] = {0}; + pj_str_t tmp = pj_str(buf); + unsigned count; + static const pj_str_t err_invalid_num = + {"Invalid destination call number\n", 32 }; + count = PJ_ARRAY_SIZE(ids); + pjsua_enum_calls(ids, &count); + + if (count <= 1) { + static const pj_str_t err_no_other_call = + {"There are no other calls\n", 25}; + + pj_cli_sess_write_msg(cval->sess, err_no_other_call.ptr, + err_no_other_call.slen); + return PJ_SUCCESS; + } + + pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(buf)); + dst_call = my_atoi(tmp.ptr); + + /* Check if call is still there. */ + if (call != current_call) { + static pj_str_t err_call_dc = + {"Call has been disconnected\n", 27}; + + pj_cli_sess_write_msg(cval->sess, err_call_dc.ptr, + err_call_dc.slen); + return PJ_SUCCESS; + } + + /* Check that destination call is valid. */ + if (dst_call == call) { + static pj_str_t err_same_num = + {"Destination call number must not be the " + "same as the call being transfered\n", 74}; + + pj_cli_sess_write_msg(cval->sess, err_same_num.ptr, + err_same_num.slen); + return PJ_SUCCESS; + } + + if (dst_call >= PJSUA_MAX_CALLS) { + pj_cli_sess_write_msg(cval->sess, err_invalid_num.ptr, + err_invalid_num.slen); + return PJ_SUCCESS; + } + + if (!pjsua_call_is_active(dst_call)) { + pj_cli_sess_write_msg(cval->sess, err_invalid_num.ptr, + err_invalid_num.slen); + return PJ_SUCCESS; + } + + pjsua_msg_data_init(&msg_data); + if (app_config.no_refersub) { + /* Add Refer-Sub: false in outgoing REFER request */ + pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB, + &STR_FALSE); + pj_list_push_back(&msg_data.hdr_list, &refer_sub); + } + + pjsua_call_xfer_replaces(call, dst_call, + PJSUA_XFER_NO_REQUIRE_REPLACES, + &msg_data); + } + return PJ_SUCCESS; +} + +static pj_status_t cmd_redirect_call(pj_cli_cmd_val *cval) +{ + if (current_call == PJSUA_INVALID_ID) { + PJ_LOG(3,(THIS_FILE, "No current call")); + return PJ_SUCCESS; + } + if (!pjsua_call_is_active(current_call)) { + PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call)); + } else { + enum { + ACCEPT_REPLACE, ACCEPT, REJECT, STOP + }; + int choice = pj_strtol(&cval->argv[1]); + + switch (choice) { + case ACCEPT_REPLACE: + pjsua_call_process_redirect(current_call, + PJSIP_REDIRECT_ACCEPT_REPLACE); + break; + case ACCEPT: + pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_ACCEPT); + break; + case REJECT: + pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_REJECT); + break; + default: + pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_STOP); + break; + } + } + return PJ_SUCCESS; +} + +/* Send DTMF (RFC2833) */ +static pj_status_t cmd_dtmf_2833(pj_cli_cmd_val *cval) +{ + if (current_call == PJSUA_INVALID_ID) { + + PJ_LOG(3,(THIS_FILE, "No current call")); + + } else if (!pjsua_call_has_media(current_call)) { + + PJ_LOG(3,(THIS_FILE, "Media is not established yet!")); + + } else { + int call = current_call; + pj_status_t status; + + if (call != current_call) { + static const pj_str_t err_msg = {"Call has been disconnected\n", + 28}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + return PJ_SUCCESS;; + } + + status = pjsua_call_dial_dtmf(current_call, &cval->argv[1]); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Unable to send DTMF", status); + } else { + static const pj_str_t msg = {"DTMF digits enqueued " + "for transmission\n", 39}; + pj_cli_sess_write_msg(cval->sess, msg.ptr, msg.slen); + } + } + return PJ_SUCCESS; +} + +/* Send DTMF with SIP Info */ +static pj_status_t cmd_call_info(pj_cli_cmd_val *cval) +{ + if (current_call == PJSUA_INVALID_ID) { + + PJ_LOG(3,(THIS_FILE, "No current call")); + + } else { + const pj_str_t SIP_INFO = pj_str("INFO"); + int call = current_call; + int i; + pj_status_t status; + + if (call != current_call) { + static const pj_str_t err_msg = {"Call has been disconnected\n", + 28}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + return PJ_SUCCESS;; + } + + for (i=0; iargv[1].slen; ++i) { + char body[64]; + + pjsua_msg_data_init(&msg_data); + msg_data.content_type = pj_str("application/dtmf-relay"); + + pj_ansi_snprintf(body, + sizeof(body), + "Signal=%c\n" + "Duration=160", + cval->argv[1].ptr[i]); + + msg_data.msg_body = pj_str(body); + + status = pjsua_call_send_request(current_call, &SIP_INFO, + &msg_data); + if (status != PJ_SUCCESS) { + break; + } + } + } + return PJ_SUCCESS; +} + +/* Dump call quality */ +static pj_status_t cmd_call_quality() +{ + if (current_call != PJSUA_INVALID_ID) { + log_call_dump(current_call); + } else { + PJ_LOG(3,(THIS_FILE, "No current call")); + } + return PJ_SUCCESS; +} + +/* Send arbitrary request */ +static pj_status_t cmd_send_arbitrary(pj_cli_cmd_val *cval) +{ + if (pjsua_acc_get_count() == 0) { + static const pj_str_t err_msg = {"Sorry, need at least one " + "account configured\n", 45}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + } else { + char *uri; + char dest[64] = {0}; + pj_str_t tmp = pj_str(dest); + struct input_result result; + static const pj_str_t header = {"Send arbitrary request to " + "remote host\n", 39}; + + pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen); + + pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(dest)); + /* Input destination URI */ + uri = NULL; + get_input_url(tmp.ptr, tmp.slen, cval, &result); + if (result.nb_result != PJSUA_APP_NO_NB) { + if (result.nb_result == -1) { + static const pj_str_t err_msg = {"Sorry you can't do that!\n", + 26}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); + return PJ_SUCCESS; + } else if (result.nb_result == 0) { + uri = NULL; + if (current_call == PJSUA_INVALID_ID) { + static const pj_str_t err_msg = {"No current call\n", + 17}; + pj_cli_sess_write_msg(cval->sess, err_msg.ptr, + err_msg.slen); + + return PJ_SUCCESS; + } + } else { + pjsua_buddy_info binfo; + pjsua_buddy_get_info(result.nb_result-1, &binfo); + pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(dest)); + uri = tmp.ptr; + } + } else if (result.uri_result) { + uri = result.uri_result; + } else { + return PJ_SUCCESS;; + } + + if (uri) { + char method[64] = {0}; + pj_str_t tmp_method = pj_str(method); + pj_strncpy_with_null(&tmp_method, &cval->argv[1], sizeof(method)); + tmp = pj_str(uri); + send_request(method, &tmp); + } else { + /* If you send call control request using this method + * (such requests includes BYE, CANCEL, etc.), it will + * not go well with the call state, so don't do it + * unless it's for testing. + */ + pjsua_call_send_request(current_call, &cval->argv[1], NULL); + } + } + return PJ_SUCCESS; +} + +static pj_status_t cmd_show_current_call(pj_cli_cmd_val *cval) +{ + char out_str[128]; + int i = pjsua_call_get_count(); + pj_ansi_snprintf(out_str, sizeof(out_str), + "You have %d active call%s\n", i, (i>1?"s":"")); + + pj_cli_sess_write_msg(cval->sess, out_str, + pj_ansi_strlen(out_str)); + + if (current_call != PJSUA_INVALID_ID) { + pjsua_call_info ci; + if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS) { + pj_ansi_snprintf(out_str, sizeof(out_str), + "Current call id=%d to %.*s [%.*s]\n", current_call, + (int)ci.remote_info.slen, ci.remote_info.ptr, + (int)ci.state_text.slen, ci.state_text.ptr); + + pj_cli_sess_write_msg(cval->sess, out_str, + pj_ansi_strlen(out_str)); + } + } + return PJ_SUCCESS; +} + +/* Call handler */ +pj_status_t cmd_call_handler(pj_cli_cmd_val *cval) +{ + pj_status_t status = PJ_SUCCESS; + pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); + + CHECK_PJSUA_RUNNING(); + + switch(cmd_id) { + case CMD_CALL_NEW: + status = cmd_make_single_call(cval); + break; + case CMD_CALL_MULTI: + status = cmd_make_multi_call(cval); + break; + case CMD_CALL_ANSWER: + status = cmd_answer_call(cval); + break; + case CMD_CALL_HANGUP: + case CMD_CALL_HANGUP_ALL: + status = cmd_hangup_call(cval, (cmd_id==CMD_CALL_HANGUP_ALL)); + break; + case CMD_CALL_HOLD: + status = cmd_hold_call(); + break; + case CMD_CALL_REINVITE: + status = cmd_call_reinvite(); + break; + case CMD_CALL_UPDATE: + status = cmd_call_update(); + break; + case CMD_CALL_NEXT: + case CMD_CALL_PREVIOUS: + status = cmd_next_call(cmd_id==CMD_CALL_NEXT); + break; + case CMD_CALL_TRANSFER: + status = cmd_transfer_call(cval); + break; + case CMD_CALL_TRANSFER_REPLACE: + status = cmd_transfer_replace_call(cval); + break; + case CMD_CALL_REDIRECT: + status = cmd_redirect_call(cval); + break; + case CMD_CALL_D2833: + status = cmd_dtmf_2833(cval); + break; + case CMD_CALL_INFO: + status = cmd_call_info(cval); + break; + case CMD_CALL_DUMP_Q: + status = cmd_call_quality(); + break; + case CMD_CALL_SEND_ARB: + status = cmd_send_arbitrary(cval); + break; + case CMD_CALL_LIST: + status = cmd_show_current_call(cval); + break; + } + + return status; +} + +#if PJSUA_HAS_VIDEO +static pj_status_t cmd_set_video_enable(pj_bool_t enabled) +{ + app_config.vid.vid_cnt = (enabled ? 1 : 0); + PJ_LOG(3,(THIS_FILE, "Video will be %s in next offer/answer", + (enabled?"enabled":"disabled"))); + + return PJ_SUCCESS; +} + +static pj_status_t modify_video_account(pjsua_acc_config *acc_cfg) +{ + pj_status_t status = pjsua_acc_modify(current_acc, acc_cfg); + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Error modifying account %d", + current_acc)); + + return status; +} + +static pj_status_t cmd_show_account_video() +{ + pjsua_acc_config acc_cfg; + + pjsua_acc_get_config(current_acc, &acc_cfg); + app_config_show_video(current_acc, &acc_cfg); + return PJ_SUCCESS; +} + +static pj_status_t cmd_video_acc_handler(pj_cli_cmd_val *cval) +{ + pjsua_acc_config acc_cfg; + pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); + + CHECK_PJSUA_RUNNING(); + + pjsua_acc_get_config(current_acc, &acc_cfg); + + switch(cmd_id) { + case CMD_VIDEO_ACC_AUTORX: + case CMD_VIDEO_ACC_AUTOTX: + { + int on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2)==0); + + if (cmd_id == CMD_VIDEO_ACC_AUTORX) + acc_cfg.vid_in_auto_show = on; + else + acc_cfg.vid_out_auto_transmit = on; + } + break; + case CMD_VIDEO_ACC_CAP_ID: + case CMD_VIDEO_ACC_REN_ID: + { + int dev = pj_strtol(&cval->argv[1]); + + if (cmd_id == CMD_VIDEO_ACC_CAP_ID) + acc_cfg.vid_cap_dev = dev; + else + acc_cfg.vid_rend_dev = dev; + } + break; + } + modify_video_account(&acc_cfg); + return PJ_SUCCESS; +} + +static pj_status_t cmd_add_vid_strm() +{ + return pjsua_call_set_vid_strm(current_call, + PJSUA_CALL_VID_STRM_ADD, NULL); +} + +static pj_status_t cmd_enable_vid_rx(pj_cli_cmd_val *cval) +{ + pjsua_call_vid_strm_op_param param; + pjsua_stream_info si; + pj_status_t status = PJ_SUCCESS; + pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0); + + pjsua_call_vid_strm_op_param_default(¶m); + + param.med_idx = pj_strtol(&cval->argv[2]); + if (pjsua_call_get_stream_info(current_call, param.med_idx, &si) || + si.type != PJMEDIA_TYPE_VIDEO) + { + PJ_PERROR(1,(THIS_FILE, PJ_EINVAL, "Invalid stream")); + return status; + } + + if (on) param.dir = (si.info.vid.dir | PJMEDIA_DIR_DECODING); + else param.dir = (si.info.vid.dir & PJMEDIA_DIR_ENCODING); + + status = pjsua_call_set_vid_strm(current_call, + PJSUA_CALL_VID_STRM_CHANGE_DIR, + ¶m); + return status; +} + +static pj_status_t cmd_enable_vid_tx(pj_cli_cmd_val *cval) +{ + pjsua_call_vid_strm_op_param param; + pj_status_t status = PJ_SUCCESS; + pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0); + + pjsua_call_vid_strm_op op = on? PJSUA_CALL_VID_STRM_START_TRANSMIT : + PJSUA_CALL_VID_STRM_STOP_TRANSMIT; + + pjsua_call_vid_strm_op_param_default(¶m); + + param.med_idx = pj_strtol(&cval->argv[2]); + + status = pjsua_call_set_vid_strm(current_call, op, ¶m); + return status; +} + +static pj_status_t cmd_enable_vid_stream(pj_cli_cmd_val *cval, + pj_bool_t enable) +{ + pjsua_call_vid_strm_op_param param; + pjsua_call_vid_strm_op op = enable? PJSUA_CALL_VID_STRM_CHANGE_DIR : + PJSUA_CALL_VID_STRM_REMOVE; + + pjsua_call_vid_strm_op_param_default(¶m); + + param.med_idx = cval->argc > 1 ? pj_strtol(&cval->argv[1]) : -1; + param.dir = PJMEDIA_DIR_ENCODING_DECODING; + return pjsua_call_set_vid_strm(current_call, op, ¶m); +} + +static pj_status_t cmd_set_cap_dev_id(pj_cli_cmd_val *cval) +{ + pjsua_call_vid_strm_op_param param; + + pjsua_call_vid_strm_op_param_default(¶m); + param.med_idx = cval->argc > 1? pj_strtol(&cval->argv[1]) : -1; + param.cap_dev = cval->argc > 2? pj_strtol(&cval->argv[2]) : + PJMEDIA_VID_DEFAULT_CAPTURE_DEV; + + return pjsua_call_set_vid_strm(current_call, + PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV, + ¶m); +} + +static pj_status_t cmd_list_vid_dev() +{ + vid_list_devs(); + return PJ_SUCCESS; +} + +static pj_status_t cmd_vid_device_refresh() +{ + pjmedia_vid_dev_refresh(); + return PJ_SUCCESS; +} + +static pj_status_t cmd_vid_device_preview(pj_cli_cmd_val *cval) +{ + int dev_id = pj_strtol(&cval->argv[2]); + pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0); + + if (on) { + pjsua_vid_preview_param param; + + pjsua_vid_preview_param_default(¶m); + param.wnd_flags = PJMEDIA_VID_DEV_WND_BORDER | + PJMEDIA_VID_DEV_WND_RESIZABLE; + pjsua_vid_preview_start(dev_id, ¶m); + arrange_window(pjsua_vid_preview_get_win(dev_id)); + } else { + pjsua_vid_win_id wid; + wid = pjsua_vid_preview_get_win(dev_id); + if (wid != PJSUA_INVALID_ID) { + /* Preview window hiding once it is stopped is + * responsibility of app */ + pjsua_vid_win_set_show(wid, PJ_FALSE); + pjsua_vid_preview_stop(dev_id); + } + } + return PJ_SUCCESS; +} + +static pj_status_t cmd_vid_codec_list() +{ + pjsua_codec_info ci[PJMEDIA_CODEC_MGR_MAX_CODECS]; + unsigned count = PJ_ARRAY_SIZE(ci); + pj_status_t status = pjsua_vid_enum_codecs(ci, &count); + if (status != PJ_SUCCESS) { + PJ_PERROR(1,(THIS_FILE, status, "Error enumerating codecs")); + } else { + unsigned i; + PJ_LOG(3,(THIS_FILE, "Found %d video codecs:", count)); + PJ_LOG(3,(THIS_FILE, "codec id prio fps bw(kbps) size")); + PJ_LOG(3,(THIS_FILE, "------------------------------------------")); + for (i=0; ifps.num*1.0/vfd->fps.denum), + vfd->avg_bps/1000, vfd->max_bps/1000, + vfd->size.w, vfd->size.h)); + } + } + return PJ_SUCCESS; +} + +static pj_status_t cmd_set_vid_codec_prio(pj_cli_cmd_val *cval) +{ + int prio = pj_strtol(&cval->argv[2]); + pj_status_t status; + + status = pjsua_vid_codec_set_priority(&cval->argv[1], (pj_uint8_t)prio); + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Set codec priority error")); + + return PJ_SUCCESS; +} + +static pj_status_t cmd_set_vid_codec_fps(pj_cli_cmd_val *cval) +{ + pjmedia_vid_codec_param cp; + int M, N; + pj_status_t status; + + M = pj_strtol(&cval->argv[2]); + N = pj_strtol(&cval->argv[3]); + status = pjsua_vid_codec_get_param(&cval->argv[1], &cp); + if (status == PJ_SUCCESS) { + cp.enc_fmt.det.vid.fps.num = M; + cp.enc_fmt.det.vid.fps.denum = N; + status = pjsua_vid_codec_set_param(&cval->argv[1], &cp); + } + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Set codec framerate error")); + + return PJ_SUCCESS; +} + +static pj_status_t cmd_set_vid_codec_bitrate(pj_cli_cmd_val *cval) +{ + pjmedia_vid_codec_param cp; + int M, N; + pj_status_t status; + + M = pj_strtol(&cval->argv[2]); + N = pj_strtol(&cval->argv[3]); + status = pjsua_vid_codec_get_param(&cval->argv[1], &cp); + if (status == PJ_SUCCESS) { + cp.enc_fmt.det.vid.avg_bps = M * 1000; + cp.enc_fmt.det.vid.max_bps = N * 1000; + status = pjsua_vid_codec_set_param(&cval->argv[1], &cp); + } + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error")); + + return status; +} + +static pj_status_t cmd_set_vid_codec_size(pj_cli_cmd_val *cval) +{ + pjmedia_vid_codec_param cp; + int M, N; + pj_status_t status; + + M = pj_strtol(&cval->argv[2]); + N = pj_strtol(&cval->argv[3]); + status = pjsua_vid_codec_get_param(&cval->argv[1], &cp); + if (status == PJ_SUCCESS) { + cp.enc_fmt.det.vid.size.w = M; + cp.enc_fmt.det.vid.size.h = N; + status = pjsua_vid_codec_set_param(&cval->argv[1], &cp); + } + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Set codec size error")); + + return status; +} + +static pj_status_t cmd_vid_win_list() +{ + pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS]; + unsigned i, cnt = PJ_ARRAY_SIZE(wids); + + pjsua_vid_enum_wins(wids, &cnt); + + PJ_LOG(3,(THIS_FILE, "Found %d video windows:", cnt)); + PJ_LOG(3,(THIS_FILE, "WID show pos size")); + PJ_LOG(3,(THIS_FILE, "------------------------------")); + for (i = 0; i < cnt; ++i) { + pjsua_vid_win_info wi; + pjsua_vid_win_get_info(wids[i], &wi); + PJ_LOG(3,(THIS_FILE, "%3d %c (%d,%d) %dx%d", + wids[i], (wi.show?'Y':'N'), wi.pos.x, wi.pos.y, + wi.size.w, wi.size.h)); + } + return PJ_SUCCESS; +} + +static pj_status_t cmd_arrange_vid_win() +{ + arrange_window(PJSUA_INVALID_ID); + return PJ_SUCCESS; +} + +static pj_status_t cmd_show_vid_win(pj_cli_cmd_val *cval, pj_bool_t show) +{ + pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]); + return pjsua_vid_win_set_show(wid, show); +} + +static pj_status_t cmd_move_vid_win(pj_cli_cmd_val *cval) +{ + pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]); + pjmedia_coord pos; + + pos.x = pj_strtol(&cval->argv[2]); + pos.y = pj_strtol(&cval->argv[3]); + return pjsua_vid_win_set_pos(wid, &pos); +} + +static pj_status_t cmd_resize_vid_win(pj_cli_cmd_val *cval) +{ + pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]); + pjmedia_rect_size size; + + size.w = pj_strtol(&cval->argv[2]); + size.h = pj_strtol(&cval->argv[3]); + return pjsua_vid_win_set_size(wid, &size); +} + +/* Video handler */ +static pj_status_t cmd_video_handler(pj_cli_cmd_val *cval) +{ + pj_status_t status = PJ_SUCCESS; + pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); + + CHECK_PJSUA_RUNNING(); + + switch(cmd_id) { + case CMD_VIDEO_ENABLE: + status = cmd_set_video_enable(PJ_TRUE); + break; + case CMD_VIDEO_DISABLE: + status = cmd_set_video_enable(PJ_FALSE); + break; + case CMD_VIDEO_ACC_SHOW: + status = cmd_show_account_video(); + break; + case CMD_VIDEO_ACC_AUTORX: + case CMD_VIDEO_ACC_AUTOTX: + case CMD_VIDEO_ACC_CAP_ID: + case CMD_VIDEO_ACC_REN_ID: + status = cmd_video_acc_handler(cval); + break; + case CMD_VIDEO_CALL_ADD: + status = cmd_add_vid_strm(); + break; + case CMD_VIDEO_CALL_RX: + status = cmd_enable_vid_rx(cval); + break; + case CMD_VIDEO_CALL_TX: + status = cmd_enable_vid_tx(cval); + break; + case CMD_VIDEO_CALL_ENABLE: + case CMD_VIDEO_CALL_DISABLE: + status = cmd_enable_vid_stream(cval, (cmd_id==CMD_VIDEO_CALL_ENABLE)); + break; + case CMD_VIDEO_CALL_CAP: + status = cmd_set_cap_dev_id(cval); + break; + case CMD_VIDEO_DEVICE_LIST: + status = cmd_list_vid_dev(); + break; + case CMD_VIDEO_DEVICE_REFRESH: + status = cmd_vid_device_refresh(); + break; + case CMD_VIDEO_DEVICE_PREVIEW: + status = cmd_vid_device_preview(cval); + break; + case CMD_VIDEO_CODEC_LIST: + status = cmd_vid_codec_list(); + break; + case CMD_VIDEO_CODEC_PRIO: + status = cmd_set_vid_codec_prio(cval); + break; + case CMD_VIDEO_CODEC_FPS: + status = cmd_set_vid_codec_fps(cval); + break; + case CMD_VIDEO_CODEC_BITRATE: + status = cmd_set_vid_codec_bitrate(cval); + break; + case CMD_VIDEO_CODEC_SIZE: + status = cmd_set_vid_codec_size(cval); + break; + case CMD_VIDEO_WIN_LIST: + status = cmd_vid_win_list(); + break; + case CMD_VIDEO_WIN_ARRANGE: + status = cmd_arrange_vid_win(cval); + break; + case CMD_VIDEO_WIN_SHOW: + case CMD_VIDEO_WIN_HIDE: + status = cmd_show_vid_win(cval, (cmd_id==CMD_VIDEO_WIN_SHOW)); + break; + case CMD_VIDEO_WIN_MOVE: + status = cmd_move_vid_win(cval); + break; + case CMD_VIDEO_WIN_RESIZE: + status = cmd_resize_vid_win(cval); + break; + } + + return status; +} +#endif + +/* Other command handler */ +static pj_status_t cmd_sleep_handler(pj_cli_cmd_val *cval) +{ + int delay; + + delay = pj_strtoul(&cval->argv[1]); + if (delay < 0) delay = 0; + pj_thread_sleep(delay); + + return PJ_SUCCESS; +} + +static pj_status_t cmd_network_handler(pj_cli_cmd_val *cval) +{ + pj_status_t status = PJ_SUCCESS; + PJ_UNUSED_ARG(cval); + + CHECK_PJSUA_RUNNING(); + + status = pjsua_detect_nat_type(); + if (status != PJ_SUCCESS) + pjsua_perror(THIS_FILE, "Error", status); + + return status; +} + +static pj_status_t cmd_quit_handler(pj_cli_cmd_val *cval) +{ + PJ_LOG(3,(THIS_FILE, "Quitting app..")); + pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_FALSE); + + /* Invoke CLI stop callback (defined in pjsua_app.c) */ + cli_on_stopped(PJ_FALSE, 0, NULL); + + return PJ_SUCCESS; +} + +/* + * Syntax error handler for parser. + */ +static void on_syntax_error(pj_scanner *scanner) +{ + PJ_UNUSED_ARG(scanner); + PJ_THROW(PJ_EINVAL); +} + +/* + * This method will parse buffer string info array of argument string + * @argc On input, maximum array size of argument. On output, number of argument + * parsed + * @argv Array of argument string + */ +static pj_status_t get_options(pj_str_t *options, unsigned *argc, + pj_str_t argv[]) +{ + pj_scanner scanner; + unsigned max_argc = *argc; + + PJ_USE_EXCEPTION; + + if (!options) + return PJ_SUCCESS; + + pj_scan_init(&scanner, options->ptr, options->slen, PJ_SCAN_AUTOSKIP_WS, + &on_syntax_error); + PJ_TRY { + *argc = 0; + while (!pj_scan_is_eof(&scanner) && (max_argc > *argc)) { + pj_str_t str; + + pj_scan_get_until_chr(&scanner, " \t\r\n", &str); + argv[*argc] = str; + ++(*argc); + } + } + PJ_CATCH_ANY { + pj_scan_fini(&scanner); + return PJ_GET_EXCEPTION(); + } + PJ_END; + return PJ_SUCCESS; +} + +static pj_status_t cmd_restart_handler(pj_cli_cmd_val *cval) +{ + enum { MAX_ARGC = 64 }; + int i; + unsigned argc = 1; + static char argv_buffer[PJ_CLI_MAX_CMDBUF]; + static char *argv[MAX_ARGC] = {NULL}; + char *pbuf = argv_buffer; + + PJ_LOG(3,(THIS_FILE, "Restarting app..")); + pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_TRUE); + + /** Get the pjsua option **/ + for (i=1; i < cval->argc; i++) { + pj_str_t argvst[MAX_ARGC]; + unsigned j, ac; + + ac = MAX_ARGC - argc; + get_options(&cval->argv[i], &ac, argvst); + for (j = 0; j < ac; j++) { + pj_ansi_strncpy(pbuf, argvst[j].ptr, argvst[j].slen); + pbuf[argvst[j].slen] = '\0'; + argv[argc + j] = pbuf; + pbuf += argvst[j].slen + 1; + } + argc += ac; + } + + /* Invoke CLI stop callback (defined in pjsua_app.c) */ + cli_on_stopped(PJ_TRUE, argc, (char**)argv); + + return PJ_SUCCESS; +} + +static pj_status_t add_call_command(pj_cli_t *cli) +{ + char* call_command = + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + + pj_str_t xml = pj_str(call_command); + return pj_cli_add_cmd_from_xml(cli, NULL, + &xml, cmd_call_handler, + NULL, get_choice_value); +} + +static pj_status_t add_presence_command(pj_cli_t *cli) +{ + char* presence_command = + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + + pj_str_t xml = pj_str(presence_command); + + return pj_cli_add_cmd_from_xml(cli, NULL, + &xml, cmd_presence_handler, + NULL, get_choice_value); +} + +static pj_status_t add_account_command(pj_cli_t *cli) +{ + char* account_command = + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + + pj_str_t xml = pj_str(account_command); + return pj_cli_add_cmd_from_xml(cli, NULL, + &xml, cmd_account_handler, + NULL, get_choice_value); +} + +static pj_status_t add_media_command(pj_cli_t *cli) +{ + char* media_command = + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + + pj_str_t xml = pj_str(media_command); + return pj_cli_add_cmd_from_xml(cli, NULL, + &xml, cmd_media_handler, + NULL, get_choice_value); +} + +static pj_status_t add_config_command(pj_cli_t *cli) +{ + char* config_command = + "" + " " + " " + " " + " " + " " + " " + ""; + + pj_str_t xml = pj_str(config_command); + return pj_cli_add_cmd_from_xml(cli, NULL, + &xml, cmd_config_handler, + NULL, get_choice_value); +} + +#if PJSUA_HAS_VIDEO +static pj_status_t add_video_command(pj_cli_t *cli) +{ + char* video_command = + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + + pj_str_t xml = pj_str(video_command); + return pj_cli_add_cmd_from_xml(cli, NULL, + &xml, cmd_video_handler, + NULL, get_choice_value); +} +#endif + +static pj_status_t add_other_command(pj_cli_t *cli) +{ + char* sleep_command = + "" + " " + ""; + + char* network_command = + ""; + + char* shutdown_command = + ""; + + char* restart_command = + "" + " " + " " + " " + " " + ""; + + pj_status_t status; + pj_str_t sleep_xml = pj_str(sleep_command); + pj_str_t network_xml = pj_str(network_command); + pj_str_t shutdown_xml = pj_str(shutdown_command); + pj_str_t restart_xml = pj_str(restart_command); + + status = pj_cli_add_cmd_from_xml(cli, NULL, + &sleep_xml, cmd_sleep_handler, + NULL, NULL); + if (status != PJ_SUCCESS) + return status; + + status = pj_cli_add_cmd_from_xml(cli, NULL, + &network_xml, cmd_network_handler, + NULL, NULL); + if (status != PJ_SUCCESS) + return status; + + status = pj_cli_add_cmd_from_xml(cli, NULL, + &shutdown_xml, cmd_quit_handler, + NULL, NULL); + + if (status != PJ_SUCCESS) + return status; + + status = pj_cli_add_cmd_from_xml(cli, NULL, + &restart_xml, cmd_restart_handler, + NULL, NULL); + + return status; +} + +pj_status_t cli_setup_command(pj_cli_t *cli) +{ + pj_status_t status; + + status = add_call_command(cli); + if (status != PJ_SUCCESS) + return status; + + status = add_presence_command(cli); + if (status != PJ_SUCCESS) + return status; + + status = add_account_command(cli); + if (status != PJ_SUCCESS) + return status; + + status = add_media_command(cli); + if (status != PJ_SUCCESS) + return status; + + status = add_config_command(cli); + if (status != PJ_SUCCESS) + return status; + +#if PJSUA_HAS_VIDEO + status = add_video_command(cli); + if (status != PJ_SUCCESS) + return status; +#endif + + status = add_other_command(cli); + + return status; } diff --git a/pjsip-apps/src/pjsua/pjsua_cli_cmd.c b/pjsip-apps/src/pjsua/pjsua_cli_cmd.c deleted file mode 100644 index fa5d874e..00000000 --- a/pjsip-apps/src/pjsua/pjsua_cli_cmd.c +++ /dev/null @@ -1,2967 +0,0 @@ -/* $Id$ */ -/* - * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) - * Copyright (C) 2003-2008 Benny Prijono - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "pjsua_common.h" -#include -#include -#include -#include -#include - -#define THIS_FILE "pjsua_cli_cmd.c" - -#define CHECK_PJSUA_RUNNING() if (pjsua_get_state()!=PJSUA_STATE_RUNNING) \ - return PJ_EINVALIDOP - -/* CLI command id */ -/* level 1 command */ -#define CMD_CALL 100 -#define CMD_PRESENCE 200 -#define CMD_ACCOUNT 300 -#define CMD_MEDIA 400 -#define CMD_CONFIG 500 -#define CMD_VIDEO 600 -#define CMD_SLEEP 700 -#define CMD_ECHO 800 -#define CMD_NETWORK 900 -#define CMD_QUIT 110 -#define CMD_RESTART 120 -#define CMD_RELOAD 130 - -/* call level 2 command */ -#define CMD_CALL_NEW ((CMD_CALL*10)+1) -#define CMD_CALL_MULTI ((CMD_CALL*10)+2) -#define CMD_CALL_ANSWER ((CMD_CALL*10)+3) -#define CMD_CALL_HANGUP ((CMD_CALL*10)+4) -#define CMD_CALL_HANGUP_ALL ((CMD_CALL*10)+5) -#define CMD_CALL_HOLD ((CMD_CALL*10)+6) -#define CMD_CALL_REINVITE ((CMD_CALL*10)+7) -#define CMD_CALL_UPDATE ((CMD_CALL*10)+8) -#define CMD_CALL_NEXT ((CMD_CALL*10)+9) -#define CMD_CALL_PREVIOUS ((CMD_CALL*10)+10) -#define CMD_CALL_TRANSFER ((CMD_CALL*10)+11) -#define CMD_CALL_TRANSFER_REPLACE ((CMD_CALL*10)+12) -#define CMD_CALL_REDIRECT ((CMD_CALL*10)+13) -#define CMD_CALL_D2833 ((CMD_CALL*10)+14) -#define CMD_CALL_INFO ((CMD_CALL*10)+15) -#define CMD_CALL_DUMP_Q ((CMD_CALL*10)+16) -#define CMD_CALL_SEND_ARB ((CMD_CALL*10)+17) -#define CMD_CALL_LIST ((CMD_CALL*10)+18) - -/* im & presence level 2 command */ -#define CMD_PRESENCE_ADD_BUDDY ((CMD_PRESENCE*10)+1) -#define CMD_PRESENCE_DEL_BUDDY ((CMD_PRESENCE*10)+2) -#define CMD_PRESENCE_SEND_IM ((CMD_PRESENCE*10)+3) -#define CMD_PRESENCE_SUB ((CMD_PRESENCE*10)+4) -#define CMD_PRESENCE_UNSUB ((CMD_PRESENCE*10)+5) -#define CMD_PRESENCE_TOG_STATE ((CMD_PRESENCE*10)+6) -#define CMD_PRESENCE_TEXT ((CMD_PRESENCE*10)+7) -#define CMD_PRESENCE_LIST ((CMD_PRESENCE*10)+8) - -/* account level 2 command */ -#define CMD_ACCOUNT_ADD ((CMD_ACCOUNT*10)+1) -#define CMD_ACCOUNT_DEL ((CMD_ACCOUNT*10)+2) -#define CMD_ACCOUNT_MOD ((CMD_ACCOUNT*10)+3) -#define CMD_ACCOUNT_REG ((CMD_ACCOUNT*10)+4) -#define CMD_ACCOUNT_UNREG ((CMD_ACCOUNT*10)+5) -#define CMD_ACCOUNT_NEXT ((CMD_ACCOUNT*10)+6) -#define CMD_ACCOUNT_PREV ((CMD_ACCOUNT*10)+7) -#define CMD_ACCOUNT_SHOW ((CMD_ACCOUNT*10)+8) - -/* conference & media level 2 command */ -#define CMD_MEDIA_LIST ((CMD_MEDIA*10)+1) -#define CMD_MEDIA_CONF_CONNECT ((CMD_MEDIA*10)+2) -#define CMD_MEDIA_CONF_DISCONNECT ((CMD_MEDIA*10)+3) -#define CMD_MEDIA_ADJUST_VOL ((CMD_MEDIA*10)+4) -#define CMD_MEDIA_CODEC_PRIO ((CMD_MEDIA*10)+5) - -/* status & config level 2 command */ -#define CMD_CONFIG_DUMP_STAT ((CMD_CONFIG*10)+1) -#define CMD_CONFIG_DUMP_DETAIL ((CMD_CONFIG*10)+2) -#define CMD_CONFIG_DUMP_CONF ((CMD_CONFIG*10)+3) -#define CMD_CONFIG_WRITE_SETTING ((CMD_CONFIG*10)+4) - -/* video level 2 command */ -#define CMD_VIDEO_ENABLE ((CMD_VIDEO*10)+1) -#define CMD_VIDEO_DISABLE ((CMD_VIDEO*10)+2) -#define CMD_VIDEO_ACC ((CMD_VIDEO*10)+3) -#define CMD_VIDEO_CALL ((CMD_VIDEO*10)+4) -#define CMD_VIDEO_DEVICE ((CMD_VIDEO*10)+5) -#define CMD_VIDEO_CODEC ((CMD_VIDEO*10)+6) -#define CMD_VIDEO_WIN ((CMD_VIDEO*10)+7) - -/* video level 3 command */ -#define CMD_VIDEO_ACC_SHOW ((CMD_VIDEO_ACC*10)+1) -#define CMD_VIDEO_ACC_AUTORX ((CMD_VIDEO_ACC*10)+2) -#define CMD_VIDEO_ACC_AUTOTX ((CMD_VIDEO_ACC*10)+3) -#define CMD_VIDEO_ACC_CAP_ID ((CMD_VIDEO_ACC*10)+4) -#define CMD_VIDEO_ACC_REN_ID ((CMD_VIDEO_ACC*10)+5) -#define CMD_VIDEO_CALL_RX ((CMD_VIDEO_CALL*10)+1) -#define CMD_VIDEO_CALL_TX ((CMD_VIDEO_CALL*10)+2) -#define CMD_VIDEO_CALL_ADD ((CMD_VIDEO_CALL*10)+3) -#define CMD_VIDEO_CALL_ENABLE ((CMD_VIDEO_CALL*10)+4) -#define CMD_VIDEO_CALL_DISABLE ((CMD_VIDEO_CALL*10)+5) -#define CMD_VIDEO_CALL_CAP ((CMD_VIDEO_CALL*10)+6) -#define CMD_VIDEO_DEVICE_LIST ((CMD_VIDEO_DEVICE*10)+1) -#define CMD_VIDEO_DEVICE_REFRESH ((CMD_VIDEO_DEVICE*10)+2) -#define CMD_VIDEO_DEVICE_PREVIEW ((CMD_VIDEO_DEVICE*10)+3) -#define CMD_VIDEO_CODEC_LIST ((CMD_VIDEO_CODEC*10)+1) -#define CMD_VIDEO_CODEC_PRIO ((CMD_VIDEO_CODEC*10)+2) -#define CMD_VIDEO_CODEC_FPS ((CMD_VIDEO_CODEC*10)+3) -#define CMD_VIDEO_CODEC_BITRATE ((CMD_VIDEO_CODEC*10)+4) -#define CMD_VIDEO_CODEC_SIZE ((CMD_VIDEO_CODEC*10)+5) -#define CMD_VIDEO_WIN_LIST ((CMD_VIDEO_WIN*10)+1) -#define CMD_VIDEO_WIN_ARRANGE ((CMD_VIDEO_WIN*10)+2) -#define CMD_VIDEO_WIN_SHOW ((CMD_VIDEO_WIN*10)+3) -#define CMD_VIDEO_WIN_HIDE ((CMD_VIDEO_WIN*10)+4) -#define CMD_VIDEO_WIN_MOVE ((CMD_VIDEO_WIN*10)+5) -#define CMD_VIDEO_WIN_RESIZE ((CMD_VIDEO_WIN*10)+6) - -/* dynamic choice argument list */ -#define DYN_CHOICE_START 9900 -#define DYN_CHOICE_BUDDY_ID (DYN_CHOICE_START)+1 -#define DYN_CHOICE_ACCOUNT_ID (DYN_CHOICE_START)+2 -#define DYN_CHOICE_MEDIA_PORT (DYN_CHOICE_START)+3 -#define DYN_CHOICE_AUDIO_CODEC_ID (DYN_CHOICE_START)+4 -#define DYN_CHOICE_CAP_DEV_ID (DYN_CHOICE_START)+5 -#define DYN_CHOICE_REN_DEV_ID (DYN_CHOICE_START)+6 -#define DYN_CHOICE_VID_DEV_ID (DYN_CHOICE_START)+7 -#define DYN_CHOICE_STREAM_ID (DYN_CHOICE_START)+8 -#define DYN_CHOICE_VIDEO_CODEC_ID (DYN_CHOICE_START)+9 -#define DYN_CHOICE_WIN_ID (DYN_CHOICE_START)+10 -#define DYN_CHOICE_CALL_ID (DYN_CHOICE_START)+11 -#define DYN_CHOICE_ADDED_BUDDY_ID (DYN_CHOICE_START)+12 - -/* Get input URL */ -static void get_input_url(char *buf, - int len, - pj_cli_cmd_val *cval, - struct input_result *result) -{ - static const pj_str_t err_invalid_input = {"Invalid input\n", 15}; - result->nb_result = NO_NB; - result->uri_result = NULL; - - len = strlen(buf); - - /* Left trim */ - while (pj_isspace(*buf)) { - ++buf; - --len; - } - - /* Remove trailing newlines */ - while (len && (buf[len-1] == '\r' || buf[len-1] == '\n')) - buf[--len] = '\0'; - - if (len == 0 || buf[0]=='q') - return; - - if (pj_isdigit(*buf) || *buf=='-') { - - int i; - - if (*buf=='-') - i = 1; - else - i = 0; - - for (; isess, err_invalid_input.ptr, - err_invalid_input.slen); - return; - } - } - - result->nb_result = my_atoi(buf); - - if (result->nb_result >= 0 && - result->nb_result <= (int)pjsua_get_buddy_count()) - { - return; - } - if (result->nb_result == -1) - return; - - pj_cli_sess_write_msg(cval->sess, err_invalid_input.ptr, - err_invalid_input.slen); - result->nb_result = NO_NB; - return; - - } else { - pj_status_t status; - - if ((status=pjsua_verify_url(buf)) != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Invalid URL", status); - return; - } - - result->uri_result = buf; - } -} - -/* CLI dynamic choice handler */ -/* Get buddy id */ -static void get_buddy_id(pj_cli_dyn_choice_param *param) -{ - if (param->cnt < param->max_cnt) { - pjsua_buddy_id ids[64]; - int i = 0; - unsigned count = PJ_ARRAY_SIZE(ids); - char data_out[64]; - - pjsua_enum_buddies(ids, &count); - - if (count > 0) { - for (i=0; i<(int)count; ++i) { - pjsua_buddy_info info; - - if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS) - continue; - - /* Fill buddy id */ - pj_ansi_snprintf(data_out, sizeof(data_out), "%d", ids[i]+1); - pj_strdup2(param->pool, ¶m->choice[i].value, data_out); - pj_bzero(data_out, PJ_ARRAY_SIZE(data_out)); - - /* Format & fill description */ - pj_ansi_snprintf(data_out, - sizeof(data_out), - "<%.*s> %.*s", - (int)info.status_text.slen, - info.status_text.ptr, - (int)info.uri.slen, - info.uri.ptr); - - pj_strdup2(param->pool, ¶m->choice[i].desc, data_out); - if (++param->cnt >= (param->max_cnt-1)) - break; - } - } - if (param->arg_id == DYN_CHOICE_BUDDY_ID) { - /* Add URL input option */ - pj_ansi_snprintf(data_out, sizeof(data_out), "URL"); - pj_strdup2(param->pool, ¶m->choice[i].value, data_out); - pj_ansi_snprintf(data_out, sizeof(data_out), "An URL"); - pj_strdup2(param->pool, ¶m->choice[i].desc, data_out); - ++param->cnt; - } - } -} - -static void get_account_id(pj_cli_dyn_choice_param *param) -{ - if (param->cnt < param->max_cnt) { - char buf[8]; - char buf_out[80]; - pjsua_acc_info info; - - pjsua_acc_id acc_ids[16]; - unsigned count = PJ_ARRAY_SIZE(acc_ids); - int i; - - pjsua_enum_accs(acc_ids, &count); - - for (i=0; i<(int)count; ++i) { - pj_bzero(&buf_out[0], PJ_ARRAY_SIZE(buf_out)); - - pjsua_acc_get_info(acc_ids[i], &info); - - pj_ansi_snprintf(buf_out, - sizeof(buf_out), - "%c%.*s", - (acc_ids[i]==current_acc?'*':' '), - (int)info.acc_uri.slen, - info.acc_uri.ptr); - - pj_bzero(buf, sizeof(buf)); - pj_ansi_snprintf(buf, sizeof(buf), "%d", acc_ids[i]); - pj_strdup2(param->pool, ¶m->choice[i].value, buf); - pj_strdup2(param->pool, ¶m->choice[i].desc, buf_out); - if (++param->cnt >= param->max_cnt) - break; - } - } -} - -static void get_media_port(pj_cli_dyn_choice_param *param) -{ - unsigned i, count; - pjsua_conf_port_id id[PJSUA_MAX_CALLS]; - - count = PJ_ARRAY_SIZE(id); - pjsua_enum_conf_ports(id, &count); - - for (i=0; ipool, ¶m->choice[i].value, slot_id); - - txlist[0] = '\0'; - for (j=0; jpool, ¶m->choice[i].desc, desc); - if (++param->cnt >= param->max_cnt) - break; - } -} - -static void get_audio_codec_id(pj_cli_dyn_choice_param *param) -{ - if (param->cnt < param->max_cnt) { - pjsua_codec_info c[32]; - unsigned i, count = PJ_ARRAY_SIZE(c); - char codec_id[64]; - char desc[128]; - - pjsua_enum_codecs(c, &count); - for (i=0; ipool, ¶m->choice[param->cnt].value, codec_id); - - pj_ansi_snprintf(desc, sizeof(desc), - "Audio, prio: %d%s%.*s", - c[i].priority, - c[i].desc.slen? " - ":"", - (int)c[i].desc.slen, - c[i].desc.ptr); - - pj_strdup2(param->pool, ¶m->choice[param->cnt].desc, desc); - if (++param->cnt >= param->max_cnt) - break; - } - } -} - -#if PJSUA_HAS_VIDEO -static void get_video_stream_id(pj_cli_dyn_choice_param *param) -{ - if (param->cnt < param->max_cnt) { - pjsua_call_info call_info; - - if (current_call != PJSUA_INVALID_ID) { - unsigned i; - pjsua_call_get_info(current_call, &call_info); - for (i=0; ipool, ¶m->choice[i].value, med_idx); - - switch (call_info.media[i].status) { - case PJSUA_CALL_MEDIA_NONE: - pj_strdup2(param->pool, ¶m->choice[i].desc, - "Status:None"); - break; - case PJSUA_CALL_MEDIA_ACTIVE: - pj_strdup2(param->pool, ¶m->choice[i].desc, - "Status:Active"); - break; - case PJSUA_CALL_MEDIA_LOCAL_HOLD: - pj_strdup2(param->pool, ¶m->choice[i].desc, - "Status:Local Hold"); - break; - case PJSUA_CALL_MEDIA_REMOTE_HOLD: - pj_strdup2(param->pool, ¶m->choice[i].desc, - "Status:Remote Hold"); - break; - case PJSUA_CALL_MEDIA_ERROR: - pj_strdup2(param->pool, ¶m->choice[i].desc, - "Status:Media Error"); - break; - } - if (++param->cnt >= param->max_cnt) - break; - } - } - } - } -} - -static void get_video_dev_hint(pj_cli_dyn_choice_param *param, - pjmedia_vid_dev_info *vdi, - unsigned vid_dev_id) -{ - char desc[128]; - char dev_id[8]; - pj_ansi_snprintf(dev_id, sizeof(dev_id), - "%d", vid_dev_id); - pj_ansi_snprintf(desc, sizeof(desc), "%s [%s]", - vdi->name, vdi->driver); - - pj_strdup2(param->pool, ¶m->choice[param->cnt].value, - dev_id); - pj_strdup2(param->pool, ¶m->choice[param->cnt].desc, - desc); -} - -static void get_video_dev_id(pj_cli_dyn_choice_param *param, - pj_bool_t all, - pj_bool_t capture) -{ - if (param->cnt < param->max_cnt) { - unsigned i, count; - pjmedia_vid_dev_info vdi; - pj_status_t status; - - count = pjsua_vid_dev_count(); - if (count == 0) { - return; - } - - for (i=0; icnt >= param->max_cnt) - break; - } - } - } - } -} - -static void get_video_codec_id(pj_cli_dyn_choice_param *param) -{ - if (param->cnt < param->max_cnt) { - pjsua_codec_info ci[32]; - unsigned i, count = PJ_ARRAY_SIZE(ci); - char codec_id[64]; - char desc[128]; - - pjsua_vid_enum_codecs(ci, &count); - for (i=0; ipool, ¶m->choice[param->cnt].value, codec_id); - - pj_ansi_snprintf(desc, sizeof(desc), - "Video, p[%d], f[%.2f], b[%d/%d], s[%dx%d]", - ci[i].priority, - (vfd->fps.num*1.0/vfd->fps.denum), - vfd->avg_bps/1000, vfd->max_bps/1000, - vfd->size.w, vfd->size.h); - - pj_strdup2(param->pool, ¶m->choice[param->cnt].desc, desc); - if (++param->cnt >= param->max_cnt) - break; - } - } -} - -static void get_video_window_id(pj_cli_dyn_choice_param *param) -{ - if (param->cnt < param->max_cnt) { - pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS]; - unsigned i, cnt = PJ_ARRAY_SIZE(wids); - char win_id[64]; - char desc[128]; - - pjsua_vid_enum_wins(wids, &cnt); - - for (i = 0; i < cnt; ++i) { - pjsua_vid_win_info wi; - - pjsua_vid_win_get_info(wids[i], &wi); - pj_ansi_snprintf(win_id, sizeof(win_id), "%d", wids[i]); - pj_strdup2(param->pool, ¶m->choice[i].value, win_id); - - pj_ansi_snprintf(desc, sizeof(desc), - "Show:%c Pos(%d,%d) Size(%dx%d)", - (wi.show?'Y':'N'), wi.pos.x, wi.pos.y, - wi.size.w, wi.size.h); - - pj_strdup2(param->pool, ¶m->choice[i].desc, desc); - if (++param->cnt >= param->max_cnt) - break; - } - } -} - -static void get_call_id(pj_cli_dyn_choice_param *param) -{ - if (param->cnt < param->max_cnt) { - char call_id[64]; - char desc[128]; - unsigned i, count; - pjsua_call_id ids[PJSUA_MAX_CALLS]; - int call = current_call; - - count = PJ_ARRAY_SIZE(ids); - pjsua_enum_calls(ids, &count); - - if (count > 1) { - for (i=0; ipool, ¶m->choice[i].value, call_id); - pj_ansi_snprintf(desc, sizeof(desc), "%.*s [%.*s]", - (int)call_info.remote_info.slen, - call_info.remote_info.ptr, - (int)call_info.state_text.slen, - call_info.state_text.ptr); - pj_strdup2(param->pool, ¶m->choice[i].desc, desc); - if (++param->cnt >= param->max_cnt) - break; - - } - } - } -} - -#endif - -static void get_choice_value(pj_cli_dyn_choice_param *param) -{ - switch (param->arg_id) { - case DYN_CHOICE_BUDDY_ID: - case DYN_CHOICE_ADDED_BUDDY_ID: - get_buddy_id(param); - break; - case DYN_CHOICE_ACCOUNT_ID: - get_account_id(param); - break; - case DYN_CHOICE_MEDIA_PORT: - get_media_port(param); - break; - case DYN_CHOICE_AUDIO_CODEC_ID: - get_audio_codec_id(param); - break; -#if PJSUA_HAS_VIDEO - case DYN_CHOICE_CAP_DEV_ID: - case DYN_CHOICE_REN_DEV_ID: - case DYN_CHOICE_VID_DEV_ID: - get_video_dev_id(param, - (param->arg_id==DYN_CHOICE_VID_DEV_ID), - (param->arg_id==DYN_CHOICE_CAP_DEV_ID)); - break; - case DYN_CHOICE_STREAM_ID: - get_video_stream_id(param); - break; - case DYN_CHOICE_VIDEO_CODEC_ID: - get_video_codec_id(param); - break; - case DYN_CHOICE_WIN_ID: - get_video_window_id(param); - break; - case DYN_CHOICE_CALL_ID: - get_call_id(param); - break; -#endif - default: - param->cnt = 0; - break; - } -} - -/* - * CLI command handler - */ - -/* Add account */ -static pj_status_t cmd_add_account(pj_cli_cmd_val *cval) -{ - pjsua_acc_config acc_cfg; - pj_status_t status; - - pjsua_acc_config_default(&acc_cfg); - acc_cfg.id = cval->argv[1]; - acc_cfg.reg_uri = cval->argv[2]; - acc_cfg.cred_count = 1; - acc_cfg.cred_info[0].scheme = pj_str("Digest"); - acc_cfg.cred_info[0].realm = cval->argv[3]; - acc_cfg.cred_info[0].username = cval->argv[4]; - acc_cfg.cred_info[0].data_type = 0; - acc_cfg.cred_info[0].data = cval->argv[5]; - - acc_cfg.rtp_cfg = app_config.rtp_cfg; - app_config_init_video(&acc_cfg); - - status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL); - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Error adding new account", status); - } - - return status; -} - -/* Delete account */ -static pj_status_t cmd_del_account(pj_cli_cmd_val *cval) -{ - char out_str[64]; - unsigned str_len; - - int i = my_atoi(cval->argv[1].ptr); - - if (!pjsua_acc_is_valid(i)) { - pj_ansi_snprintf(out_str, sizeof(out_str), - "Invalid account id %d\n", i); - str_len = pj_ansi_strlen(out_str); - pj_cli_sess_write_msg(cval->sess, out_str, str_len); - } else { - pjsua_acc_del(i); - pj_ansi_snprintf(out_str, sizeof(out_str), - "Account %d deleted\n", i); - str_len = pj_ansi_strlen(out_str); - pj_cli_sess_write_msg(cval->sess, out_str, str_len); - } - return PJ_SUCCESS; -} - -/* Modify account */ -static pj_status_t cmd_mod_account(pj_cli_cmd_val *cval) -{ - PJ_UNUSED_ARG(cval); - return PJ_SUCCESS; -} - -/* Register account */ -static pj_status_t cmd_reg_account() -{ - pjsua_acc_set_registration(current_acc, PJ_TRUE); - return PJ_SUCCESS; -} - -/* Unregister account */ -static pj_status_t cmd_unreg_account() -{ - pjsua_acc_set_registration(current_acc, PJ_FALSE); - return PJ_SUCCESS; -} - -/* Select account to be used for sending outgoing request */ -static pj_status_t cmd_next_account(pj_cli_cmd_val *cval) -{ - int i = my_atoi(cval->argv[1].ptr); - if (pjsua_acc_is_valid(i)) { - pjsua_acc_set_default(i); - PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i)); - } else { - PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i)); - } - return PJ_SUCCESS; -} - -/* Show account list */ -static pj_status_t cmd_show_account(pj_cli_cmd_val *cval) -{ - pjsua_acc_id acc_ids[16]; - unsigned count = PJ_ARRAY_SIZE(acc_ids); - int i; - static const pj_str_t header = {"Account list:\n", 15}; - - pjsua_enum_accs(acc_ids, &count); - - pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen); - for (i=0; i<(int)count; ++i) { - char acc_info[80]; - char out_str[160]; - pjsua_acc_info info; - - pjsua_acc_get_info(acc_ids[i], &info); - - if (!info.has_registration) { - pj_ansi_snprintf(acc_info, sizeof(acc_info), "%.*s", - (int)info.status_text.slen, - info.status_text.ptr); - - } else { - pj_ansi_snprintf(acc_info, sizeof(acc_info), - "%d/%.*s (expires=%d)", - info.status, - (int)info.status_text.slen, - info.status_text.ptr, - info.expires); - - } - - pj_ansi_snprintf(out_str, sizeof(out_str), - " %c[%2d] %.*s: %s\n", - (acc_ids[i]==current_acc?'*':' '), acc_ids[i], - (int)info.acc_uri.slen, info.acc_uri.ptr, - acc_info); - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - - pj_bzero(out_str, sizeof(out_str)); - pj_ansi_snprintf(out_str, sizeof(out_str), - " Online status: %.*s\n", - (int)info.online_status_text.slen, - info.online_status_text.ptr); - - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - } - - return PJ_SUCCESS; -} - -/* Account command handler */ -pj_status_t cmd_account_handler(pj_cli_cmd_val *cval) -{ - pj_status_t status = PJ_SUCCESS; - - CHECK_PJSUA_RUNNING(); - - switch(pj_cli_get_cmd_id(cval->cmd)) { - case CMD_ACCOUNT_ADD: - status = cmd_add_account(cval); - break; - case CMD_ACCOUNT_DEL: - status = cmd_del_account(cval); - break; - case CMD_ACCOUNT_MOD: - status = cmd_mod_account(cval); - break; - case CMD_ACCOUNT_REG: - status = cmd_reg_account(); - break; - case CMD_ACCOUNT_UNREG: - status = cmd_unreg_account(); - break; - case CMD_ACCOUNT_NEXT: - case CMD_ACCOUNT_PREV: - status = cmd_next_account(cval); - break; - case CMD_ACCOUNT_SHOW: - status = cmd_show_account(cval); - break; - } - return status; -} - -/* Add buddy */ -static pj_status_t cmd_add_buddy(pj_cli_cmd_val *cval) -{ - char out_str[80]; - pjsua_buddy_config buddy_cfg; - pjsua_buddy_id buddy_id; - pj_status_t status = PJ_SUCCESS; - cval->argv[1].ptr[cval->argv[1].slen] = 0; - - if (pjsua_verify_url(cval->argv[1].ptr) != PJ_SUCCESS) { - pj_ansi_snprintf(out_str, sizeof(out_str), - "Invalid URI '%s'\n", cval->argv[1].ptr); - } else { - pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config)); - - buddy_cfg.uri = pj_str(cval->argv[1].ptr); - buddy_cfg.subscribe = PJ_TRUE; - - status = pjsua_buddy_add(&buddy_cfg, &buddy_id); - if (status == PJ_SUCCESS) { - pj_ansi_snprintf(out_str, sizeof(out_str), - "New buddy '%s' added at index %d\n", - cval->argv[1].ptr, buddy_id+1); - } - } - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - return status; -} - -/* Delete buddy */ -static pj_status_t cmd_del_buddy(pj_cli_cmd_val *cval) -{ - int i = my_atoi(cval->argv[1].ptr) - 1; - char out_str[80]; - - if (!pjsua_buddy_is_valid(i)) { - pj_ansi_snprintf(out_str, sizeof(out_str), - "Invalid buddy id %d\n", i); - } else { - pjsua_buddy_del(i); - pj_ansi_snprintf(out_str, sizeof(out_str), - "Buddy %d deleted\n", i); - } - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - return PJ_SUCCESS; -} - -/* Send IM */ -static pj_status_t cmd_send_im(pj_cli_cmd_val *cval) -{ - int i = -1; - struct input_result result; - char dest[64]; - pj_str_t tmp = pj_str(dest); - - /* make compiler happy. */ - char *uri = NULL; - - pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest)); - - /* input destination. */ - get_input_url(tmp.ptr, tmp.slen, cval, &result); - if (result.nb_result != NO_NB) { - - if (result.nb_result == -1) { - static const pj_str_t err_msg = {"you can't send broadcast im " - "like that!\n", 40 }; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - return PJ_SUCCESS; - } else if (result.nb_result == 0) { - i = current_call; - } else { - pjsua_buddy_info binfo; - pjsua_buddy_get_info(result.nb_result-1, &binfo); - pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(dest)); - uri = tmp.ptr; - } - - } else if (result.uri_result) { - uri = result.uri_result; - } - - /* send typing indication. */ - if (i != -1) - pjsua_call_send_typing_ind(i, PJ_TRUE, NULL); - else { - pj_str_t tmp_uri = pj_str(uri); - pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL); - } - - /* send the im */ - if (i != -1) - pjsua_call_send_im(i, NULL, &cval->argv[2], NULL, NULL); - else { - pj_str_t tmp_uri = pj_str(uri); - pjsua_im_send(current_acc, &tmp_uri, NULL, &cval->argv[2], NULL, NULL); - } - return PJ_SUCCESS; -} - -/* Subscribe/unsubscribe presence */ -static pj_status_t cmd_subs_pres(pj_cli_cmd_val *cval, pj_bool_t subscribe) -{ - struct input_result result; - char dest[64] = {0}; - pj_str_t tmp = pj_str(dest); - - pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest)); - get_input_url(tmp.ptr, tmp.slen, cval, &result); - if (result.nb_result != NO_NB) { - if (result.nb_result == -1) { - int i, count; - count = pjsua_get_buddy_count(); - for (i=0; isess, err_msg.ptr, err_msg.slen); - } else { - pjsua_buddy_subscribe_pres(result.nb_result-1, subscribe); - } - - } else if (result.uri_result) { - static const pj_str_t err_msg = {"Sorry, can only subscribe to " - "buddy's presence, not arbitrary " - "URL (for now)\n", 76}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - } - return PJ_SUCCESS; -} - -/* Toggle online state */ -static pj_status_t cmd_toggle_state(pj_cli_cmd_val *cval) -{ - char out_str[128]; - pjsua_acc_info acc_info; - - pjsua_acc_get_info(current_acc, &acc_info); - acc_info.online_status = !acc_info.online_status; - pjsua_acc_set_online_status(current_acc, acc_info.online_status); - pj_ansi_snprintf(out_str, sizeof(out_str), - "Setting %s online status to %s\n", - acc_info.acc_uri.ptr, - (acc_info.online_status?"online":"offline")); - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - return PJ_SUCCESS; -} - -/* Set presence text */ -static pj_status_t cmd_set_presence_text(pj_cli_cmd_val *cval) -{ - pjrpid_element elem; - int choice; - pj_bool_t online_status; - - enum { - AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX - }; - - choice = pj_strtol(&cval->argv[1]) - 1; - - pj_bzero(&elem, sizeof(elem)); - elem.type = PJRPID_ELEMENT_TYPE_PERSON; - - online_status = PJ_TRUE; - - switch (choice) { - case AVAILABLE: - break; - case BUSY: - elem.activity = PJRPID_ACTIVITY_BUSY; - elem.note = pj_str("Busy"); - break; - case OTP: - elem.activity = PJRPID_ACTIVITY_BUSY; - elem.note = pj_str("On the phone"); - break; - case IDLE: - elem.activity = PJRPID_ACTIVITY_UNKNOWN; - elem.note = pj_str("Idle"); - break; - case AWAY: - elem.activity = PJRPID_ACTIVITY_AWAY; - elem.note = pj_str("Away"); - break; - case BRB: - elem.activity = PJRPID_ACTIVITY_UNKNOWN; - elem.note = pj_str("Be right back"); - break; - case OFFLINE: - online_status = PJ_FALSE; - break; - } - pjsua_acc_set_online_status2(current_acc, online_status, &elem); - return PJ_SUCCESS; -} - -/* Show buddy list */ -static pj_status_t cmd_show_buddy(pj_cli_cmd_val *cval) -{ - pjsua_buddy_id ids[64]; - int i; - unsigned count = PJ_ARRAY_SIZE(ids); - static const pj_str_t header = {"Buddy list:\n", 13}; - char out_str[64]; - - pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen); - - pjsua_enum_buddies(ids, &count); - - if (count == 0) { - pj_ansi_snprintf(out_str, sizeof(out_str), " -none-\n"); - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - } else { - for (i=0; i<(int)count; ++i) { - pjsua_buddy_info info; - pj_bzero(out_str, sizeof(out_str)); - - if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS) - continue; - - pj_ansi_snprintf(out_str, sizeof(out_str), - " [%2d] <%.*s> %.*s\n", - ids[i]+1, - (int)info.status_text.slen, - info.status_text.ptr, - (int)info.uri.slen, - info.uri.ptr); - - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - } - } - return PJ_SUCCESS; -} - -/* Presence/buddy command handler */ -pj_status_t cmd_presence_handler(pj_cli_cmd_val *cval) -{ - pj_status_t status = PJ_SUCCESS; - - CHECK_PJSUA_RUNNING(); - - switch(pj_cli_get_cmd_id(cval->cmd)) { - case CMD_PRESENCE_ADD_BUDDY: - status = cmd_add_buddy(cval); - break; - case CMD_PRESENCE_DEL_BUDDY: - status = cmd_del_buddy(cval); - break; - case CMD_PRESENCE_SEND_IM: - status = cmd_send_im(cval); - break; - case CMD_PRESENCE_SUB: - case CMD_PRESENCE_UNSUB: - status = cmd_subs_pres(cval, - pj_cli_get_cmd_id(cval->cmd)==CMD_PRESENCE_SUB); - break; - case CMD_PRESENCE_TOG_STATE: - status = cmd_toggle_state(cval); - break; - case CMD_PRESENCE_TEXT: - status = cmd_set_presence_text(cval); - break; - case CMD_PRESENCE_LIST: - status = cmd_show_buddy(cval); - break; - } - - return status; -} - -/* Show conference list */ -static pj_status_t cmd_media_list(pj_cli_cmd_val *cval) -{ - unsigned i, count; - pjsua_conf_port_id id[PJSUA_MAX_CALLS]; - static const pj_str_t header = {"Conference ports:\n", 19}; - - pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen); - - count = PJ_ARRAY_SIZE(id); - pjsua_enum_conf_ports(id, &count); - - for (i=0; isess, out_str, pj_ansi_strlen(out_str)); - } - return PJ_SUCCESS; -} - -/* Conference connect/disconnect */ -static pj_status_t cmd_media_connect(pj_cli_cmd_val *cval, pj_bool_t connect) -{ - pj_status_t status; - - if (connect) - status = pjsua_conf_connect(pj_strtol(&cval->argv[1]), - pj_strtol(&cval->argv[2])); - else - status = pjsua_conf_disconnect(pj_strtol(&cval->argv[1]), - pj_strtol(&cval->argv[2])); - - if (status == PJ_SUCCESS) { - static const pj_str_t success_msg = {"Success\n", 9}; - pj_cli_sess_write_msg(cval->sess, success_msg.ptr, success_msg.slen); - } else { - static const pj_str_t err_msg = {"ERROR!!\n", 9}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - } - return status; -} - -/* Adjust audio volume */ -static pj_status_t cmd_adjust_vol(pj_cli_cmd_val *cval) -{ - char buf[80]; - float orig_level; - char *err; - char level_val[16] = {0}; - pj_str_t tmp = pj_str(level_val); - - /* Adjust mic level */ - orig_level = app_config.mic_level; - pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(level_val)); - app_config.mic_level = (float)strtod(level_val, &err); - pjsua_conf_adjust_rx_level(0, app_config.mic_level); - - pj_ansi_snprintf(buf, sizeof(buf), - "Adjust mic level: [%4.1fx] -> [%4.1fx]\n", - orig_level, app_config.mic_level); - - pj_cli_sess_write_msg(cval->sess, buf, pj_ansi_strlen(buf)); - - /* Adjust speaker level */ - orig_level = app_config.speaker_level; - pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(level_val)); - app_config.speaker_level = (float)strtod(level_val, &err); - pjsua_conf_adjust_tx_level(0, app_config.speaker_level); - - pj_ansi_snprintf(buf, sizeof(buf), - "Adjust speaker level: [%4.1fx] -> [%4.1fx]\n", - orig_level, app_config.speaker_level); - - pj_cli_sess_write_msg(cval->sess, buf, pj_ansi_strlen(buf)); - - return PJ_SUCCESS; -} - -/* Set codec priority */ -static pj_status_t cmd_set_codec_prio(pj_cli_cmd_val *cval) -{ - int new_prio; - pj_status_t status; - - new_prio = pj_strtol(&cval->argv[2]); - if (new_prio < 0) - new_prio = 0; - else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST) - new_prio = PJMEDIA_CODEC_PRIO_HIGHEST; - - status = pjsua_codec_set_priority(&cval->argv[1], - (pj_uint8_t)new_prio); -#if PJSUA_HAS_VIDEO - if (status != PJ_SUCCESS) { - status = pjsua_vid_codec_set_priority(&cval->argv[1], - (pj_uint8_t)new_prio); - } -#endif - if (status != PJ_SUCCESS) - pjsua_perror(THIS_FILE, "Error setting codec priority", status); - - return status; -} - -/* Conference/media command handler */ -pj_status_t cmd_media_handler(pj_cli_cmd_val *cval) -{ - pj_status_t status = PJ_SUCCESS; - - CHECK_PJSUA_RUNNING(); - - switch(pj_cli_get_cmd_id(cval->cmd)) { - case CMD_MEDIA_LIST: - status = cmd_media_list(cval); - break; - case CMD_MEDIA_CONF_CONNECT: - case CMD_MEDIA_CONF_DISCONNECT: - status = cmd_media_connect(cval, - pj_cli_get_cmd_id(cval->cmd)==CMD_MEDIA_CONF_CONNECT); - break; - case CMD_MEDIA_ADJUST_VOL: - status = cmd_adjust_vol(cval); - break; - case CMD_MEDIA_CODEC_PRIO: - status = cmd_set_codec_prio(cval); - break; - } - - return status; -} - -/* Dump status */ -static pj_status_t cmd_stat_dump(pj_bool_t detail) -{ - pjsua_dump(detail); - return PJ_SUCCESS; -} - -static pj_status_t cmd_show_config() -{ - char settings[2000]; - int len; - - len = write_settings(&app_config, settings, sizeof(settings)); - if (len < 1) - PJ_LOG(1,(THIS_FILE, "Error: not enough buffer")); - else - PJ_LOG(3,(THIS_FILE, - "Dumping configuration (%d bytes):\n%s\n", - len, settings)); - - return PJ_SUCCESS; -} - -static pj_status_t cmd_write_config(pj_cli_cmd_val *cval) -{ - char settings[2000]; - char buf[128] = {0}; - int len; - pj_str_t tmp = pj_str(buf); - - pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(buf)); - - len = write_settings(&app_config, settings, sizeof(settings)); - if (len < 1) - PJ_LOG(1,(THIS_FILE, "Error: not enough buffer")); - else { - pj_oshandle_t fd; - pj_status_t status; - - status = pj_file_open(app_config.pool, buf, PJ_O_WRONLY, &fd); - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Unable to open file", status); - } else { - char out_str[256]; - pj_ssize_t size = len; - pj_file_write(fd, settings, &size); - pj_file_close(fd); - - pj_ansi_snprintf(out_str, sizeof(out_str), - "Settings successfully written to '%s'\n", buf); - - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - } - } - - return PJ_SUCCESS; -} - -/* Status and config command handler */ -pj_status_t cmd_config_handler(pj_cli_cmd_val *cval) -{ - pj_status_t status = PJ_SUCCESS; - - CHECK_PJSUA_RUNNING(); - - switch(pj_cli_get_cmd_id(cval->cmd)) { - case CMD_CONFIG_DUMP_STAT: - status = cmd_stat_dump(PJ_FALSE); - break; - case CMD_CONFIG_DUMP_DETAIL: - status = cmd_stat_dump(PJ_TRUE); - break; - case CMD_CONFIG_DUMP_CONF: - status = cmd_show_config(); - break; - case CMD_CONFIG_WRITE_SETTING: - status = cmd_write_config(cval); - break; - } - - return status; -} - -/* Make single call */ -static pj_status_t cmd_make_single_call(pj_cli_cmd_val *cval) -{ - struct input_result result; - char dest[64] = {0}; - char out_str[128]; - pj_str_t tmp = pj_str(dest); - - pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest)); - - pj_ansi_snprintf(out_str, - sizeof(out_str), - "(You currently have %d calls)\n", - pjsua_call_get_count()); - - pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str)); - - /* input destination. */ - get_input_url(tmp.ptr, tmp.slen, cval, &result); - if (result.nb_result != NO_NB) { - pjsua_buddy_info binfo; - if (result.nb_result == -1 || result.nb_result == 0) { - static const pj_str_t err_msg = - {"You can't do that with make call!\n", 35}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - return PJ_SUCCESS; - } - pjsua_buddy_get_info(result.nb_result-1, &binfo); - pj_strncpy(&tmp, &binfo.uri, sizeof(dest)); - } else if (result.uri_result) { - tmp = pj_str(result.uri_result); - } else { - tmp.slen = 0; - } - - pjsua_msg_data_init(&msg_data); - TEST_MULTIPART(&msg_data); - pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL, - &msg_data, ¤t_call); - return PJ_SUCCESS; -} - -/* Make multi call */ -static pj_status_t cmd_make_multi_call(pj_cli_cmd_val *cval) -{ - struct input_result result; - char dest[64] = {0}; - char out_str[128]; - int i, count; - pj_str_t tmp = pj_str(dest); - - pj_ansi_snprintf(out_str, - sizeof(out_str), - "(You currently have %d calls)\n", - pjsua_call_get_count()); - - count = pj_strtol(&cval->argv[1]); - if (count < 1) - return PJ_SUCCESS; - - pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(dest)); - - /* input destination. */ - get_input_url(tmp.ptr, tmp.slen, cval, &result); - if (result.nb_result != NO_NB) { - pjsua_buddy_info binfo; - if (result.nb_result == -1 || result.nb_result == 0) { - static const pj_str_t err_msg = - {"You can't do that with make call!\n", 35}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - return PJ_SUCCESS; - } - pjsua_buddy_get_info(result.nb_result-1, &binfo); - pj_strncpy(&tmp, &binfo.uri, sizeof(dest)); - } else { - tmp = pj_str(result.uri_result); - } - - for (i=0; i= PJSIP_INV_STATE_CONNECTING) - { - static const pj_str_t err_msg = {"No pending incoming call\n", 26}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - - } else { - int st_code; - char contact[120]; - pj_str_t hname = { "Contact", 7 }; - pj_str_t hvalue; - pjsip_generic_string_hdr hcontact; - - st_code = pj_strtol(&cval->argv[1]); - if ((st_code < 100) || (st_code > 699)) - return PJ_SUCCESS; - - pjsua_msg_data_init(&msg_data); - - if (st_code/100 == 3) { - if (cval->argc < 3) { - static const pj_str_t err_msg = {"Enter URL to be put " - "in Contact\n", 32}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - return PJ_SUCCESS; - } - - hvalue = pj_str(contact); - pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue); - - pj_list_push_back(&msg_data.hdr_list, &hcontact); - } - - /* - * Must check again! - * Call may have been disconnected while we're waiting for - * keyboard input. - */ - if (current_call == PJSUA_INVALID_ID) { - static const pj_str_t err_msg = {"Call has been disconnected\n", - 28}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - } - - pjsua_call_answer2(current_call, &call_opt, st_code, NULL, &msg_data); - } - return PJ_SUCCESS; -} - -/* Hangup call */ -static pj_status_t cmd_hangup_call(pj_cli_cmd_val *cval, pj_bool_t all) -{ - if (current_call == PJSUA_INVALID_ID) { - static const pj_str_t err_msg = {"No current call\n", 17}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - } else { - if (all) - pjsua_call_hangup_all(); - else - pjsua_call_hangup(current_call, 0, NULL, NULL); - } - return PJ_SUCCESS; -} - -/* Hold call */ -static pj_status_t cmd_hold_call() -{ - if (current_call != PJSUA_INVALID_ID) { - pjsua_call_set_hold(current_call, NULL); - - } else { - PJ_LOG(3,(THIS_FILE, "No current call")); - } - return PJ_SUCCESS; -} - -/* Call reinvite */ -static pj_status_t cmd_call_reinvite() -{ - if (current_call != PJSUA_INVALID_ID) { - /* - * re-INVITE - */ - call_opt.flag |= PJSUA_CALL_UNHOLD; - pjsua_call_reinvite2(current_call, &call_opt, NULL); - - } else { - PJ_LOG(3,(THIS_FILE, "No current call")); - } - return PJ_SUCCESS; -} - -/* Send update */ -static pj_status_t cmd_call_update() -{ - if (current_call != PJSUA_INVALID_ID) { - pjsua_call_update2(current_call, &call_opt, NULL); - } else { - PJ_LOG(3,(THIS_FILE, "No current call")); - } - return PJ_SUCCESS; -} - -/* Select next call */ -static pj_status_t cmd_next_call(pj_bool_t next) -{ - /* - * Cycle next/prev dialog. - */ - if (next) { - find_next_call(current_call); - } else { - find_prev_call(current_call); - } - - if (current_call != PJSUA_INVALID_ID) { - pjsua_call_info call_info; - - pjsua_call_get_info(current_call, &call_info); - PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s", - (int)call_info.remote_info.slen, - call_info.remote_info.ptr)); - - } else { - PJ_LOG(3,(THIS_FILE,"No current dialog")); - } - return PJ_SUCCESS; -} - -/* Transfer call */ -static pj_status_t cmd_transfer_call(pj_cli_cmd_val *cval) -{ - if (current_call == PJSUA_INVALID_ID) { - - PJ_LOG(3,(THIS_FILE, "No current call")); - - } else { - char out_str[64]; - int call = current_call; - char dest[64] = {0}; - pj_str_t tmp = pj_str(dest); - struct input_result result; - pjsip_generic_string_hdr refer_sub; - pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 }; - pj_str_t STR_FALSE = { "false", 5 }; - pjsua_call_info ci; - - pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest)); - - pjsua_call_get_info(current_call, &ci); - pj_ansi_snprintf(out_str, - sizeof(out_str), - "Transfering current call [%d] %.*s\n", - current_call, - (int)ci.remote_info.slen, - ci.remote_info.ptr); - - get_input_url(tmp.ptr, tmp.slen, cval, &result); - - /* Check if call is still there. */ - - if (call != current_call) { - puts("Call has been disconnected"); - return PJ_SUCCESS; - } - - pjsua_msg_data_init(&msg_data); - if (app_config.no_refersub) { - /* Add Refer-Sub: false in outgoing REFER request */ - pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB, - &STR_FALSE); - pj_list_push_back(&msg_data.hdr_list, &refer_sub); - } - if (result.nb_result != NO_NB) { - if (result.nb_result == -1 || result.nb_result == 0) { - static const pj_str_t err_msg = {"You can't do that with " - "transfer call!\n", 39}; - - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - } else { - pjsua_buddy_info binfo; - pjsua_buddy_get_info(result.nb_result-1, &binfo); - pjsua_call_xfer( current_call, &binfo.uri, &msg_data); - } - } else if (result.uri_result) { - pj_str_t tmp; - tmp = pj_str(result.uri_result); - pjsua_call_xfer( current_call, &tmp, &msg_data); - } - } - return PJ_SUCCESS; -} - -/* Transfer call */ -static pj_status_t cmd_transfer_replace_call(pj_cli_cmd_val *cval) -{ - if (current_call == -1) { - PJ_LOG(3,(THIS_FILE, "No current call")); - } else { - int call = current_call; - int dst_call; - pjsip_generic_string_hdr refer_sub; - pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 }; - pj_str_t STR_FALSE = { "false", 5 }; - pjsua_call_id ids[PJSUA_MAX_CALLS]; - pjsua_msg_data msg_data; - char buf[8] = {0}; - pj_str_t tmp = pj_str(buf); - unsigned count; - static const pj_str_t err_invalid_num = - {"Invalid destination call number\n", 32 }; - count = PJ_ARRAY_SIZE(ids); - pjsua_enum_calls(ids, &count); - - if (count <= 1) { - static const pj_str_t err_no_other_call = - {"There are no other calls\n", 25}; - - pj_cli_sess_write_msg(cval->sess, err_no_other_call.ptr, - err_no_other_call.slen); - return PJ_SUCCESS; - } - - pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(buf)); - dst_call = my_atoi(tmp.ptr); - - /* Check if call is still there. */ - if (call != current_call) { - static pj_str_t err_call_dc = - {"Call has been disconnected\n", 27}; - - pj_cli_sess_write_msg(cval->sess, err_call_dc.ptr, - err_call_dc.slen); - return PJ_SUCCESS; - } - - /* Check that destination call is valid. */ - if (dst_call == call) { - static pj_str_t err_same_num = - {"Destination call number must not be the " - "same as the call being transfered\n", 74}; - - pj_cli_sess_write_msg(cval->sess, err_same_num.ptr, - err_same_num.slen); - return PJ_SUCCESS; - } - - if (dst_call >= PJSUA_MAX_CALLS) { - pj_cli_sess_write_msg(cval->sess, err_invalid_num.ptr, - err_invalid_num.slen); - return PJ_SUCCESS; - } - - if (!pjsua_call_is_active(dst_call)) { - pj_cli_sess_write_msg(cval->sess, err_invalid_num.ptr, - err_invalid_num.slen); - return PJ_SUCCESS; - } - - pjsua_msg_data_init(&msg_data); - if (app_config.no_refersub) { - /* Add Refer-Sub: false in outgoing REFER request */ - pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB, - &STR_FALSE); - pj_list_push_back(&msg_data.hdr_list, &refer_sub); - } - - pjsua_call_xfer_replaces(call, dst_call, - PJSUA_XFER_NO_REQUIRE_REPLACES, - &msg_data); - } - return PJ_SUCCESS; -} - -static pj_status_t cmd_redirect_call(pj_cli_cmd_val *cval) -{ - if (current_call == PJSUA_INVALID_ID) { - PJ_LOG(3,(THIS_FILE, "No current call")); - return PJ_SUCCESS; - } - if (!pjsua_call_is_active(current_call)) { - PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call)); - } else { - enum { - ACCEPT_REPLACE, ACCEPT, REJECT, STOP - }; - int choice = pj_strtol(&cval->argv[1]); - - switch (choice) { - case ACCEPT_REPLACE: - pjsua_call_process_redirect(current_call, - PJSIP_REDIRECT_ACCEPT_REPLACE); - break; - case ACCEPT: - pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_ACCEPT); - break; - case REJECT: - pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_REJECT); - break; - default: - pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_STOP); - break; - } - } - return PJ_SUCCESS; -} - -/* Send DTMF (RFC2833) */ -static pj_status_t cmd_dtmf_2833(pj_cli_cmd_val *cval) -{ - if (current_call == PJSUA_INVALID_ID) { - - PJ_LOG(3,(THIS_FILE, "No current call")); - - } else if (!pjsua_call_has_media(current_call)) { - - PJ_LOG(3,(THIS_FILE, "Media is not established yet!")); - - } else { - int call = current_call; - pj_status_t status; - - if (call != current_call) { - static const pj_str_t err_msg = {"Call has been disconnected\n", - 28}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - return PJ_SUCCESS;; - } - - status = pjsua_call_dial_dtmf(current_call, &cval->argv[1]); - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Unable to send DTMF", status); - } else { - static const pj_str_t msg = {"DTMF digits enqueued " - "for transmission\n", 39}; - pj_cli_sess_write_msg(cval->sess, msg.ptr, msg.slen); - } - } - return PJ_SUCCESS; -} - -/* Send DTMF with SIP Info */ -static pj_status_t cmd_call_info(pj_cli_cmd_val *cval) -{ - if (current_call == PJSUA_INVALID_ID) { - - PJ_LOG(3,(THIS_FILE, "No current call")); - - } else { - const pj_str_t SIP_INFO = pj_str("INFO"); - int call = current_call; - int i; - pj_status_t status; - - if (call != current_call) { - static const pj_str_t err_msg = {"Call has been disconnected\n", - 28}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - return PJ_SUCCESS;; - } - - for (i=0; iargv[1].slen; ++i) { - char body[64]; - - pjsua_msg_data_init(&msg_data); - msg_data.content_type = pj_str("application/dtmf-relay"); - - pj_ansi_snprintf(body, - sizeof(body), - "Signal=%c\n" - "Duration=160", - cval->argv[1].ptr[i]); - - msg_data.msg_body = pj_str(body); - - status = pjsua_call_send_request(current_call, &SIP_INFO, - &msg_data); - if (status != PJ_SUCCESS) { - break; - } - } - } - return PJ_SUCCESS; -} - -/* Dump call quality */ -static pj_status_t cmd_call_quality() -{ - if (current_call != PJSUA_INVALID_ID) { - log_call_dump(current_call); - } else { - PJ_LOG(3,(THIS_FILE, "No current call")); - } - return PJ_SUCCESS; -} - -/* Send arbitrary request */ -static pj_status_t cmd_send_arbitrary(pj_cli_cmd_val *cval) -{ - if (pjsua_acc_get_count() == 0) { - static const pj_str_t err_msg = {"Sorry, need at least one " - "account configured\n", 45}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - } else { - char *uri; - char dest[64] = {0}; - pj_str_t tmp = pj_str(dest); - struct input_result result; - static const pj_str_t header = {"Send arbitrary request to " - "remote host\n", 39}; - - pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen); - - pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(dest)); - /* Input destination URI */ - uri = NULL; - get_input_url(tmp.ptr, tmp.slen, cval, &result); - if (result.nb_result != NO_NB) { - if (result.nb_result == -1) { - static const pj_str_t err_msg = {"Sorry you can't do that!\n", - 26}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen); - return PJ_SUCCESS; - } else if (result.nb_result == 0) { - uri = NULL; - if (current_call == PJSUA_INVALID_ID) { - static const pj_str_t err_msg = {"No current call\n", - 17}; - pj_cli_sess_write_msg(cval->sess, err_msg.ptr, - err_msg.slen); - - return PJ_SUCCESS; - } - } else { - pjsua_buddy_info binfo; - pjsua_buddy_get_info(result.nb_result-1, &binfo); - pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(dest)); - uri = tmp.ptr; - } - } else if (result.uri_result) { - uri = result.uri_result; - } else { - return PJ_SUCCESS;; - } - - if (uri) { - char method[64] = {0}; - pj_str_t tmp_method = pj_str(method); - pj_strncpy_with_null(&tmp_method, &cval->argv[1], sizeof(method)); - tmp = pj_str(uri); - send_request(method, &tmp); - } else { - /* If you send call control request using this method - * (such requests includes BYE, CANCEL, etc.), it will - * not go well with the call state, so don't do it - * unless it's for testing. - */ - pjsua_call_send_request(current_call, &cval->argv[1], NULL); - } - } - return PJ_SUCCESS; -} - -static pj_status_t cmd_show_current_call(pj_cli_cmd_val *cval) -{ - char out_str[128]; - int i = pjsua_call_get_count(); - pj_ansi_snprintf(out_str, sizeof(out_str), - "You have %d active call%s\n", i, (i>1?"s":"")); - - pj_cli_sess_write_msg(cval->sess, out_str, - pj_ansi_strlen(out_str)); - - if (current_call != PJSUA_INVALID_ID) { - pjsua_call_info ci; - if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS) { - pj_ansi_snprintf(out_str, sizeof(out_str), - "Current call id=%d to %.*s [%.*s]\n", current_call, - (int)ci.remote_info.slen, ci.remote_info.ptr, - (int)ci.state_text.slen, ci.state_text.ptr); - - pj_cli_sess_write_msg(cval->sess, out_str, - pj_ansi_strlen(out_str)); - } - } - return PJ_SUCCESS; -} - -/* Call handler */ -pj_status_t cmd_call_handler(pj_cli_cmd_val *cval) -{ - pj_status_t status = PJ_SUCCESS; - pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); - - CHECK_PJSUA_RUNNING(); - - switch(cmd_id) { - case CMD_CALL_NEW: - status = cmd_make_single_call(cval); - break; - case CMD_CALL_MULTI: - status = cmd_make_multi_call(cval); - break; - case CMD_CALL_ANSWER: - status = cmd_answer_call(cval); - break; - case CMD_CALL_HANGUP: - case CMD_CALL_HANGUP_ALL: - status = cmd_hangup_call(cval, (cmd_id==CMD_CALL_HANGUP_ALL)); - break; - case CMD_CALL_HOLD: - status = cmd_hold_call(); - break; - case CMD_CALL_REINVITE: - status = cmd_call_reinvite(); - break; - case CMD_CALL_UPDATE: - status = cmd_call_update(); - break; - case CMD_CALL_NEXT: - case CMD_CALL_PREVIOUS: - status = cmd_next_call(cmd_id==CMD_CALL_NEXT); - break; - case CMD_CALL_TRANSFER: - status = cmd_transfer_call(cval); - break; - case CMD_CALL_TRANSFER_REPLACE: - status = cmd_transfer_replace_call(cval); - break; - case CMD_CALL_REDIRECT: - status = cmd_redirect_call(cval); - break; - case CMD_CALL_D2833: - status = cmd_dtmf_2833(cval); - break; - case CMD_CALL_INFO: - status = cmd_call_info(cval); - break; - case CMD_CALL_DUMP_Q: - status = cmd_call_quality(); - break; - case CMD_CALL_SEND_ARB: - status = cmd_send_arbitrary(cval); - break; - case CMD_CALL_LIST: - status = cmd_show_current_call(cval); - break; - } - - return status; -} - -#if PJSUA_HAS_VIDEO -static pj_status_t cmd_set_video_enable(pj_bool_t enabled) -{ - app_config.vid.vid_cnt = (enabled ? 1 : 0); - PJ_LOG(3,(THIS_FILE, "Video will be %s in next offer/answer", - (enabled?"enabled":"disabled"))); - - return PJ_SUCCESS; -} - -static pj_status_t modify_video_account(pjsua_acc_config *acc_cfg) -{ - pj_status_t status = pjsua_acc_modify(current_acc, acc_cfg); - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Error modifying account %d", - current_acc)); - - return status; -} - -static pj_status_t cmd_show_account_video() -{ - pjsua_acc_config acc_cfg; - - pjsua_acc_get_config(current_acc, &acc_cfg); - app_config_show_video(current_acc, &acc_cfg); - return PJ_SUCCESS; -} - -static pj_status_t cmd_video_acc_handler(pj_cli_cmd_val *cval) -{ - pjsua_acc_config acc_cfg; - pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); - - CHECK_PJSUA_RUNNING(); - - pjsua_acc_get_config(current_acc, &acc_cfg); - - switch(cmd_id) { - case CMD_VIDEO_ACC_AUTORX: - case CMD_VIDEO_ACC_AUTOTX: - { - int on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2)==0); - - if (cmd_id == CMD_VIDEO_ACC_AUTORX) - acc_cfg.vid_in_auto_show = on; - else - acc_cfg.vid_out_auto_transmit = on; - } - break; - case CMD_VIDEO_ACC_CAP_ID: - case CMD_VIDEO_ACC_REN_ID: - { - int dev = pj_strtol(&cval->argv[1]); - - if (cmd_id == CMD_VIDEO_ACC_CAP_ID) - acc_cfg.vid_cap_dev = dev; - else - acc_cfg.vid_rend_dev = dev; - } - break; - } - modify_video_account(&acc_cfg); - return PJ_SUCCESS; -} - -static pj_status_t cmd_add_vid_strm() -{ - return pjsua_call_set_vid_strm(current_call, - PJSUA_CALL_VID_STRM_ADD, NULL); -} - -static pj_status_t cmd_enable_vid_rx(pj_cli_cmd_val *cval) -{ - pjsua_call_vid_strm_op_param param; - pjsua_stream_info si; - pj_status_t status = PJ_SUCCESS; - pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0); - - pjsua_call_vid_strm_op_param_default(¶m); - - param.med_idx = pj_strtol(&cval->argv[2]); - if (pjsua_call_get_stream_info(current_call, param.med_idx, &si) || - si.type != PJMEDIA_TYPE_VIDEO) - { - PJ_PERROR(1,(THIS_FILE, PJ_EINVAL, "Invalid stream")); - return status; - } - - if (on) param.dir = (si.info.vid.dir | PJMEDIA_DIR_DECODING); - else param.dir = (si.info.vid.dir & PJMEDIA_DIR_ENCODING); - - status = pjsua_call_set_vid_strm(current_call, - PJSUA_CALL_VID_STRM_CHANGE_DIR, - ¶m); - return status; -} - -static pj_status_t cmd_enable_vid_tx(pj_cli_cmd_val *cval) -{ - pjsua_call_vid_strm_op_param param; - pj_status_t status = PJ_SUCCESS; - pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0); - - pjsua_call_vid_strm_op op = on? PJSUA_CALL_VID_STRM_START_TRANSMIT : - PJSUA_CALL_VID_STRM_STOP_TRANSMIT; - - pjsua_call_vid_strm_op_param_default(¶m); - - param.med_idx = pj_strtol(&cval->argv[2]); - - status = pjsua_call_set_vid_strm(current_call, op, ¶m); - return status; -} - -static pj_status_t cmd_enable_vid_stream(pj_cli_cmd_val *cval, - pj_bool_t enable) -{ - pjsua_call_vid_strm_op_param param; - pjsua_call_vid_strm_op op = enable? PJSUA_CALL_VID_STRM_CHANGE_DIR : - PJSUA_CALL_VID_STRM_REMOVE; - - pjsua_call_vid_strm_op_param_default(¶m); - - param.med_idx = cval->argc > 1 ? pj_strtol(&cval->argv[1]) : -1; - param.dir = PJMEDIA_DIR_ENCODING_DECODING; - return pjsua_call_set_vid_strm(current_call, op, ¶m); -} - -static pj_status_t cmd_set_cap_dev_id(pj_cli_cmd_val *cval) -{ - pjsua_call_vid_strm_op_param param; - - pjsua_call_vid_strm_op_param_default(¶m); - param.med_idx = cval->argc > 1? pj_strtol(&cval->argv[1]) : -1; - param.cap_dev = cval->argc > 2? pj_strtol(&cval->argv[2]) : - PJMEDIA_VID_DEFAULT_CAPTURE_DEV; - - return pjsua_call_set_vid_strm(current_call, - PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV, - ¶m); -} - -static pj_status_t cmd_list_vid_dev() -{ - vid_list_devs(); - return PJ_SUCCESS; -} - -static pj_status_t cmd_vid_device_refresh() -{ - pjmedia_vid_dev_refresh(); - return PJ_SUCCESS; -} - -static pj_status_t cmd_vid_device_preview(pj_cli_cmd_val *cval) -{ - int dev_id = pj_strtol(&cval->argv[2]); - pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0); - - if (on) { - pjsua_vid_preview_param param; - - pjsua_vid_preview_param_default(¶m); - param.wnd_flags = PJMEDIA_VID_DEV_WND_BORDER | - PJMEDIA_VID_DEV_WND_RESIZABLE; - pjsua_vid_preview_start(dev_id, ¶m); - arrange_window(pjsua_vid_preview_get_win(dev_id)); - } else { - pjsua_vid_win_id wid; - wid = pjsua_vid_preview_get_win(dev_id); - if (wid != PJSUA_INVALID_ID) { - /* Preview window hiding once it is stopped is - * responsibility of app */ - pjsua_vid_win_set_show(wid, PJ_FALSE); - pjsua_vid_preview_stop(dev_id); - } - } - return PJ_SUCCESS; -} - -static pj_status_t cmd_vid_codec_list() -{ - pjsua_codec_info ci[PJMEDIA_CODEC_MGR_MAX_CODECS]; - unsigned count = PJ_ARRAY_SIZE(ci); - pj_status_t status = pjsua_vid_enum_codecs(ci, &count); - if (status != PJ_SUCCESS) { - PJ_PERROR(1,(THIS_FILE, status, "Error enumerating codecs")); - } else { - unsigned i; - PJ_LOG(3,(THIS_FILE, "Found %d video codecs:", count)); - PJ_LOG(3,(THIS_FILE, "codec id prio fps bw(kbps) size")); - PJ_LOG(3,(THIS_FILE, "------------------------------------------")); - for (i=0; ifps.num*1.0/vfd->fps.denum), - vfd->avg_bps/1000, vfd->max_bps/1000, - vfd->size.w, vfd->size.h)); - } - } - return PJ_SUCCESS; -} - -static pj_status_t cmd_set_vid_codec_prio(pj_cli_cmd_val *cval) -{ - int prio = pj_strtol(&cval->argv[2]); - pj_status_t status; - - status = pjsua_vid_codec_set_priority(&cval->argv[1], (pj_uint8_t)prio); - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Set codec priority error")); - - return PJ_SUCCESS; -} - -static pj_status_t cmd_set_vid_codec_fps(pj_cli_cmd_val *cval) -{ - pjmedia_vid_codec_param cp; - int M, N; - pj_status_t status; - - M = pj_strtol(&cval->argv[2]); - N = pj_strtol(&cval->argv[3]); - status = pjsua_vid_codec_get_param(&cval->argv[1], &cp); - if (status == PJ_SUCCESS) { - cp.enc_fmt.det.vid.fps.num = M; - cp.enc_fmt.det.vid.fps.denum = N; - status = pjsua_vid_codec_set_param(&cval->argv[1], &cp); - } - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Set codec framerate error")); - - return PJ_SUCCESS; -} - -static pj_status_t cmd_set_vid_codec_bitrate(pj_cli_cmd_val *cval) -{ - pjmedia_vid_codec_param cp; - int M, N; - pj_status_t status; - - M = pj_strtol(&cval->argv[2]); - N = pj_strtol(&cval->argv[3]); - status = pjsua_vid_codec_get_param(&cval->argv[1], &cp); - if (status == PJ_SUCCESS) { - cp.enc_fmt.det.vid.avg_bps = M * 1000; - cp.enc_fmt.det.vid.max_bps = N * 1000; - status = pjsua_vid_codec_set_param(&cval->argv[1], &cp); - } - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error")); - - return status; -} - -static pj_status_t cmd_set_vid_codec_size(pj_cli_cmd_val *cval) -{ - pjmedia_vid_codec_param cp; - int M, N; - pj_status_t status; - - M = pj_strtol(&cval->argv[2]); - N = pj_strtol(&cval->argv[3]); - status = pjsua_vid_codec_get_param(&cval->argv[1], &cp); - if (status == PJ_SUCCESS) { - cp.enc_fmt.det.vid.size.w = M; - cp.enc_fmt.det.vid.size.h = N; - status = pjsua_vid_codec_set_param(&cval->argv[1], &cp); - } - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Set codec size error")); - - return status; -} - -static pj_status_t cmd_vid_win_list() -{ - pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS]; - unsigned i, cnt = PJ_ARRAY_SIZE(wids); - - pjsua_vid_enum_wins(wids, &cnt); - - PJ_LOG(3,(THIS_FILE, "Found %d video windows:", cnt)); - PJ_LOG(3,(THIS_FILE, "WID show pos size")); - PJ_LOG(3,(THIS_FILE, "------------------------------")); - for (i = 0; i < cnt; ++i) { - pjsua_vid_win_info wi; - pjsua_vid_win_get_info(wids[i], &wi); - PJ_LOG(3,(THIS_FILE, "%3d %c (%d,%d) %dx%d", - wids[i], (wi.show?'Y':'N'), wi.pos.x, wi.pos.y, - wi.size.w, wi.size.h)); - } - return PJ_SUCCESS; -} - -static pj_status_t cmd_arrange_vid_win() -{ - arrange_window(PJSUA_INVALID_ID); - return PJ_SUCCESS; -} - -static pj_status_t cmd_show_vid_win(pj_cli_cmd_val *cval, pj_bool_t show) -{ - pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]); - return pjsua_vid_win_set_show(wid, show); -} - -static pj_status_t cmd_move_vid_win(pj_cli_cmd_val *cval) -{ - pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]); - pjmedia_coord pos; - - pos.x = pj_strtol(&cval->argv[2]); - pos.y = pj_strtol(&cval->argv[3]); - return pjsua_vid_win_set_pos(wid, &pos); -} - -static pj_status_t cmd_resize_vid_win(pj_cli_cmd_val *cval) -{ - pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]); - pjmedia_rect_size size; - - size.w = pj_strtol(&cval->argv[2]); - size.h = pj_strtol(&cval->argv[3]); - return pjsua_vid_win_set_size(wid, &size); -} - -/* Video handler */ -static pj_status_t cmd_video_handler(pj_cli_cmd_val *cval) -{ - pj_status_t status = PJ_SUCCESS; - pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); - - CHECK_PJSUA_RUNNING(); - - switch(cmd_id) { - case CMD_VIDEO_ENABLE: - status = cmd_set_video_enable(PJ_TRUE); - break; - case CMD_VIDEO_DISABLE: - status = cmd_set_video_enable(PJ_FALSE); - break; - case CMD_VIDEO_ACC_SHOW: - status = cmd_show_account_video(); - break; - case CMD_VIDEO_ACC_AUTORX: - case CMD_VIDEO_ACC_AUTOTX: - case CMD_VIDEO_ACC_CAP_ID: - case CMD_VIDEO_ACC_REN_ID: - status = cmd_video_acc_handler(cval); - break; - case CMD_VIDEO_CALL_ADD: - status = cmd_add_vid_strm(); - break; - case CMD_VIDEO_CALL_RX: - status = cmd_enable_vid_rx(cval); - break; - case CMD_VIDEO_CALL_TX: - status = cmd_enable_vid_tx(cval); - break; - case CMD_VIDEO_CALL_ENABLE: - case CMD_VIDEO_CALL_DISABLE: - status = cmd_enable_vid_stream(cval, (cmd_id==CMD_VIDEO_CALL_ENABLE)); - break; - case CMD_VIDEO_CALL_CAP: - status = cmd_set_cap_dev_id(cval); - break; - case CMD_VIDEO_DEVICE_LIST: - status = cmd_list_vid_dev(); - break; - case CMD_VIDEO_DEVICE_REFRESH: - status = cmd_vid_device_refresh(); - break; - case CMD_VIDEO_DEVICE_PREVIEW: - status = cmd_vid_device_preview(cval); - break; - case CMD_VIDEO_CODEC_LIST: - status = cmd_vid_codec_list(); - break; - case CMD_VIDEO_CODEC_PRIO: - status = cmd_set_vid_codec_prio(cval); - break; - case CMD_VIDEO_CODEC_FPS: - status = cmd_set_vid_codec_fps(cval); - break; - case CMD_VIDEO_CODEC_BITRATE: - status = cmd_set_vid_codec_bitrate(cval); - break; - case CMD_VIDEO_CODEC_SIZE: - status = cmd_set_vid_codec_size(cval); - break; - case CMD_VIDEO_WIN_LIST: - status = cmd_vid_win_list(); - break; - case CMD_VIDEO_WIN_ARRANGE: - status = cmd_arrange_vid_win(cval); - break; - case CMD_VIDEO_WIN_SHOW: - case CMD_VIDEO_WIN_HIDE: - status = cmd_show_vid_win(cval, (cmd_id==CMD_VIDEO_WIN_SHOW)); - break; - case CMD_VIDEO_WIN_MOVE: - status = cmd_move_vid_win(cval); - break; - case CMD_VIDEO_WIN_RESIZE: - status = cmd_resize_vid_win(cval); - break; - } - - return status; -} -#endif - -/* Other command handler */ -static pj_status_t cmd_sleep_handler(pj_cli_cmd_val *cval) -{ - int delay; - - delay = pj_strtoul(&cval->argv[1]); - if (delay < 0) delay = 0; - pj_thread_sleep(delay); - - return PJ_SUCCESS; -} - -static pj_status_t cmd_network_handler(pj_cli_cmd_val *cval) -{ - pj_status_t status = PJ_SUCCESS; - PJ_UNUSED_ARG(cval); - - CHECK_PJSUA_RUNNING(); - - status = pjsua_detect_nat_type(); - if (status != PJ_SUCCESS) - pjsua_perror(THIS_FILE, "Error", status); - - return status; -} - -static pj_status_t cmd_quit_handler(pj_cli_cmd_val *cval) -{ - extern pj_cli_on_quit cli_on_quit_cb; - PJ_LOG(3,(THIS_FILE, "Quitting app..")); - pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_FALSE); - /** Call pj_cli_on_quit callback **/ - if (cli_on_quit_cb) - (*cli_on_quit_cb)(PJ_FALSE); - - return PJ_SUCCESS; -} - -static pj_status_t cmd_restart_handler(pj_cli_cmd_val *cval) -{ - extern pj_cli_on_quit cli_on_quit_cb; - PJ_LOG(3,(THIS_FILE, "Restarting app..")); - pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_TRUE); - /** Call pj_cli_on_quit callback **/ - if (cli_on_quit_cb) - (*cli_on_quit_cb)(PJ_TRUE); - - return PJ_SUCCESS; -} - -/* - * Syntax error handler for parser. - */ -static void on_syntax_error(pj_scanner *scanner) -{ - PJ_UNUSED_ARG(scanner); - PJ_THROW(PJ_EINVAL); -} - -static pj_status_t get_options(pj_str_t *options, unsigned *cmd_count) -{ - pj_scanner scanner; - pj_str_t str; - - PJ_USE_EXCEPTION; - - if (!options) - return PJ_SUCCESS; - - pj_scan_init(&scanner, options->ptr, options->slen, PJ_SCAN_AUTOSKIP_WS, - &on_syntax_error); - PJ_TRY { - while (!pj_scan_is_eof(&scanner)) { - ++(*cmd_count); - pj_scan_get_until_chr(&scanner, " \t\r\n", &str); - /** Store to command arguments **/ - add_reload_config(*cmd_count, &str); - } - } - PJ_CATCH_ANY { - pj_scan_fini(&scanner); - return PJ_GET_EXCEPTION(); - } - PJ_END; - return PJ_SUCCESS; -} - -static pj_status_t cmd_reload_handler(pj_cli_cmd_val *cval) -{ - int i; - unsigned cmd_count = 0; - extern pj_bool_t pjsua_restarted; - extern pj_cli_on_restart_pjsua cli_on_restart_pjsua_cb; - - PJ_LOG(3,(THIS_FILE, "Reloading Pjsua..")); - /** Get the pjsua option **/ - - for (i=1;iargc;i++) { - get_options(&cval->argv[i], &cmd_count); - } - pjsua_restarted = PJ_TRUE; - - if (cli_on_restart_pjsua_cb) - (*cli_on_restart_pjsua_cb)(); - - return PJ_SUCCESS; -} - -static pj_status_t add_call_command(pj_cli_t *cli) -{ - char* call_command = - "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - ""; - - pj_str_t xml = pj_str(call_command); - return pj_cli_add_cmd_from_xml(cli, NULL, - &xml, cmd_call_handler, - NULL, get_choice_value); -} - -static pj_status_t add_presence_command(pj_cli_t *cli) -{ - char* presence_command = - "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - ""; - - pj_str_t xml = pj_str(presence_command); - - return pj_cli_add_cmd_from_xml(cli, NULL, - &xml, cmd_presence_handler, - NULL, get_choice_value); -} - -static pj_status_t add_account_command(pj_cli_t *cli) -{ - char* account_command = - "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - ""; - - pj_str_t xml = pj_str(account_command); - return pj_cli_add_cmd_from_xml(cli, NULL, - &xml, cmd_account_handler, - NULL, get_choice_value); -} - -static pj_status_t add_media_command(pj_cli_t *cli) -{ - char* media_command = - "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - ""; - - pj_str_t xml = pj_str(media_command); - return pj_cli_add_cmd_from_xml(cli, NULL, - &xml, cmd_media_handler, - NULL, get_choice_value); -} - -static pj_status_t add_config_command(pj_cli_t *cli) -{ - char* config_command = - "" - " " - " " - " " - " " - " " - " " - ""; - - pj_str_t xml = pj_str(config_command); - return pj_cli_add_cmd_from_xml(cli, NULL, - &xml, cmd_config_handler, - NULL, get_choice_value); -} - -#if PJSUA_HAS_VIDEO -static pj_status_t add_video_command(pj_cli_t *cli) -{ - char* video_command = - "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - ""; - - pj_str_t xml = pj_str(video_command); - return pj_cli_add_cmd_from_xml(cli, NULL, - &xml, cmd_video_handler, - NULL, get_choice_value); -} -#endif - -static pj_status_t add_other_command(pj_cli_t *cli) -{ - char* sleep_command = - "" - " " - ""; - - char* network_command = - ""; - - char* shutdown_command = - ""; - - char* restart_command = - ""; - - char* reload_command = - "" - " " - " " - " " - " " - ""; - - pj_status_t status; - pj_str_t sleep_xml = pj_str(sleep_command); - pj_str_t network_xml = pj_str(network_command); - pj_str_t shutdown_xml = pj_str(shutdown_command); - pj_str_t restart_xml = pj_str(restart_command); - pj_str_t reload_xml = pj_str(reload_command); - - status = pj_cli_add_cmd_from_xml(cli, NULL, - &sleep_xml, cmd_sleep_handler, - NULL, NULL); - if (status != PJ_SUCCESS) - return status; - - status = pj_cli_add_cmd_from_xml(cli, NULL, - &network_xml, cmd_network_handler, - NULL, NULL); - if (status != PJ_SUCCESS) - return status; - - status = pj_cli_add_cmd_from_xml(cli, NULL, - &shutdown_xml, cmd_quit_handler, - NULL, NULL); - - if (status != PJ_SUCCESS) - return status; - - status = pj_cli_add_cmd_from_xml(cli, NULL, - &restart_xml, cmd_restart_handler, - NULL, NULL); - if (status != PJ_SUCCESS) - return status; - - status = pj_cli_add_cmd_from_xml(cli, NULL, - &reload_xml, cmd_reload_handler, - NULL, NULL); - - return status; -} - -pj_status_t setup_command(pj_cli_t *cli) -{ - pj_status_t status; - - status = add_call_command(cli); - if (status != PJ_SUCCESS) - return status; - - status = add_presence_command(cli); - if (status != PJ_SUCCESS) - return status; - - status = add_account_command(cli); - if (status != PJ_SUCCESS) - return status; - - status = add_media_command(cli); - if (status != PJ_SUCCESS) - return status; - - status = add_config_command(cli); - if (status != PJ_SUCCESS) - return status; - -#if PJSUA_HAS_VIDEO - status = add_video_command(cli); - if (status != PJ_SUCCESS) - return status; -#endif - - status = add_other_command(cli); - - return status; -} diff --git a/pjsip-apps/src/pjsua/pjsua_common.h b/pjsip-apps/src/pjsua/pjsua_common.h index 42646e8d..9e51a8f4 100644 --- a/pjsip-apps/src/pjsua/pjsua_common.h +++ b/pjsip-apps/src/pjsua/pjsua_common.h @@ -25,9 +25,9 @@ PJ_BEGIN_DECL #define current_acc pjsua_acc_get_default() -#define NO_LIMIT_DURATION (int)0x7FFFFFFF -#define MAX_AVI 4 -#define NO_NB -2 +#define PJSUA_APP_NO_LIMIT_DURATION (int)0x7FFFFFFF +#define PJSUA_APP_MAX_AVI 4 +#define PJSUA_APP_NO_NB -2 typedef struct input_result { @@ -53,6 +53,22 @@ typedef struct app_vid pj_bool_t out_auto_transmit; } app_vid; +/* Enumeration of CLI frontends */ +typedef enum { + CLI_FE_CONSOLE = 1, + CLI_FE_TELNET = 2 +} CLI_FE; + +/** CLI config **/ +typedef struct cli_cfg_t +{ + /** Bitmask of CLI_FE **/ + int cli_fe; + pj_cli_cfg cfg; + pj_cli_telnet_cfg telnet_cfg; + pj_cli_console_cfg console_cfg; +} cli_cfg_t; + /* Pjsua application data */ typedef struct pjsua_app_config { @@ -133,24 +149,15 @@ typedef struct pjsua_app_config pj_str_t path; pjmedia_vid_dev_index dev_id; pjsua_conf_port_id slot; - } avi[MAX_AVI]; + } avi[PJSUA_APP_MAX_AVI]; pj_bool_t avi_auto_play; int avi_def_idx; /* CLI setting */ pj_bool_t use_cli; - int cli_telnet_port; - pj_bool_t disable_cli_console; + cli_cfg_t cli_cfg; } pjsua_app_config; -/** CLI callback **/ -/** This specifies the callback called when cli quit is called. **/ -typedef void (*pj_cli_on_quit)(pj_bool_t is_restarting); -/** This callback is called when the cli is completely destroyed **/ -typedef void (*pj_cli_on_destroy)(void); -/** This callback is called when pjsua restart command is invode by cli **/ -typedef void (*pj_cli_on_restart_pjsua)(void); - /** Extern variable declaration **/ extern pjsua_call_id current_call; extern pjsua_app_config app_config; @@ -158,6 +165,7 @@ extern int stdout_refresh; extern pj_bool_t stdout_refresh_quit; extern pjsua_call_setting call_opt; extern pjsua_msg_data msg_data; +extern pj_bool_t app_running; PJ_DECL(int) my_atoi(const char *cs); PJ_DECL(pj_bool_t) find_next_call(); @@ -171,22 +179,30 @@ PJ_DECL(void) arrange_window(pjsua_vid_win_id wid); /** Defined in pjsua_cli_cmd.c **/ PJ_DECL(pj_bool_t) is_cli_inited(); -/** Defined in pjsua_app.c **/ -PJ_DECL(pj_status_t) app_init(pj_cli_telnet_on_started on_started_cb, - pj_cli_on_quit on_quit_cb, - pj_cli_on_destroy on_destroy_cb, - pj_cli_on_restart_pjsua on_restart_pjsua_cb); -PJ_DECL(void) setup_signal_handler(void); - /** Defined in pjsua_config.c **/ -/** This is to store the app runtime/startup options **/ -PJ_DECL(void) add_startup_config(int argc, char *argv[]); -/** This is to store the app reload options **/ -PJ_DECL(void) add_reload_config(unsigned idx, pj_str_t *option); /** This is to load the configuration **/ -PJ_DECL(pj_status_t) load_config(pjsua_app_config *app_config, - pj_str_t *uri_arg, - pj_bool_t app_running); +PJ_DECL(pj_status_t) load_config(int argc, + char **argv, + pj_str_t *uri_arg); + +/** Pjsua app callback **/ +/** This callback is called when CLI is started. **/ +PJ_DECL(void) cli_on_started(pj_status_t status); + +/** This callback is called when "shutdown"/"restart" command is invoked **/ +PJ_DECL(pj_bool_t) cli_on_stopped(pj_bool_t restart, int argc, char **argv); + +/** This callback is called when "quit"/"restart" command is invoked **/ +PJ_DECL(pj_bool_t) legacy_on_stopped(pj_bool_t restart); + +/** Pjsua cli method **/ +PJ_DECL(pj_status_t) cli_init(); +PJ_DECL(pj_status_t) cli_main(pj_bool_t wait_telnet_cli); +PJ_DECL(void) cli_destroy(); +PJ_DECL(void) cli_get_info(char *info, pj_size_t size); + +/** Legacy method **/ +PJ_DECL(void) legacy_main(); #if PJSUA_HAS_VIDEO PJ_DECL(void) vid_print_dev(int id, const pjmedia_vid_dev_info *vdi, @@ -210,5 +226,5 @@ PJ_DECL(void) app_config_show_video(int acc_id, PJ_END_DECL -#endif /* __PJSUA_CMD_H__ */ +#endif /* __PJSUA_COMMON_H__ */ diff --git a/pjsip-apps/src/pjsua/pjsua_config.c b/pjsip-apps/src/pjsua/pjsua_config.c index e8722086..d29ee714 100644 --- a/pjsip-apps/src/pjsua/pjsua_config.c +++ b/pjsip-apps/src/pjsua/pjsua_config.c @@ -17,7 +17,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include #include "pjsua_common.h" #define THIS_FILE "pjsua_config.c" @@ -26,10 +25,6 @@ char *stdout_refresh_text = "STDOUT_REFRESH"; -/** This is for storing console runtime options **/ -static int cmd_argc; -static char cmd_argv[MAX_APP_OPTIONS][MAX_APP_OPTIONS]; - /* Show usage */ static void usage(void) { @@ -326,12 +321,12 @@ static int read_config_file(pj_pool_t *pool, const char *filename, } /* Parse arguments. */ -static pj_status_t parse_args(int argc, char *argv[], - pjsua_app_config *cfg, +static pj_status_t parse_args(int argc, char *argv[], pj_str_t *uri_to_call) { int c; int option_index; + pjsua_app_config *cfg = &app_config; enum { OPT_CONFIG_FILE=127, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL, OPT_LOG_APPEND, OPT_COLOR, OPT_NO_COLOR, OPT_LIGHT_BG, OPT_NO_STDERR, OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO, OPT_SND_AUTO_CLOSE, @@ -1313,7 +1308,7 @@ static pj_status_t parse_args(int argc, char *argv[], break; case OPT_PLAY_AVI: - if (app_config.avi_cnt >= MAX_AVI) { + if (app_config.avi_cnt >= PJSUA_APP_MAX_AVI) { PJ_LOG(1,(THIS_FILE, "Too many AVIs")); return -1; } @@ -1329,11 +1324,12 @@ static pj_status_t parse_args(int argc, char *argv[], break; case OPT_CLI_TELNET_PORT: - cfg->cli_telnet_port = atoi(pj_optarg); + cfg->cli_cfg.telnet_cfg.port = (pj_uint16_t)atoi(pj_optarg); + cfg->cli_cfg.cli_fe |= CLI_FE_TELNET; break; case OPT_DISABLE_CLI_CONSOLE: - cfg->disable_cli_console = PJ_TRUE; + cfg->cli_cfg.cli_fe &= (~CLI_FE_CONSOLE); break; default: @@ -1420,10 +1416,11 @@ static pj_status_t parse_args(int argc, char *argv[], } /* Set default config. */ -static void default_config(pjsua_app_config *cfg) +static void default_config() { char tmp[80]; unsigned i; + pjsua_app_config *cfg = &app_config; pjsua_config_default(&cfg->cfg); pj_ansi_sprintf(tmp, "PJSUA v%s %s", pj_get_version(), @@ -1437,7 +1434,7 @@ static void default_config(pjsua_app_config *cfg) pjsua_transport_config_default(&cfg->rtp_cfg); cfg->rtp_cfg.port = 4000; cfg->redir_op = PJSIP_REDIRECT_ACCEPT_REPLACE; - cfg->duration = NO_LIMIT_DURATION; + cfg->duration = PJSUA_APP_NO_LIMIT_DURATION; cfg->wav_id = PJSUA_INVALID_ID; cfg->rec_id = PJSUA_INVALID_ID; cfg->wav_port = PJSUA_INVALID_ID; @@ -1463,12 +1460,11 @@ static void default_config(pjsua_app_config *cfg) cfg->avi_def_idx = PJSUA_INVALID_ID; cfg->use_cli = PJ_FALSE; - cfg->disable_cli_console = PJ_FALSE; - cfg->cli_telnet_port = -1; + cfg->cli_cfg.cli_fe = CLI_FE_CONSOLE; + cfg->cli_cfg.telnet_cfg.port = 0; } -static pj_status_t parse_config(int argc, char *argv[], - pjsua_app_config *app_config, pj_str_t *uri_arg) +static pj_status_t parse_config(int argc, char *argv[], pj_str_t *uri_arg) { pj_status_t status; @@ -1476,66 +1472,34 @@ static pj_status_t parse_config(int argc, char *argv[], default_config(app_config); /* Parse the arguments */ - status = parse_args(argc, argv, app_config, uri_arg); + status = parse_args(argc, argv, uri_arg); return status; } -PJ_DEF(void) add_startup_config(int argc, char *argv[]) -{ - int i; - cmd_argc = argc; - - for (i=0;iuse_cli; - disable_cli_console = app_config->disable_cli_console; - cli_telnet_port = app_config->cli_telnet_port; - } - - for (i=0;iuse_cli = use_cli; - app_config->disable_cli_console = disable_cli_console; - app_config->cli_telnet_port = cli_telnet_port; + app_config.use_cli = use_cli; + app_config.cli_cfg.cli_fe = cli_fe; + app_config.cli_cfg.telnet_cfg.port = cli_telnet_port; } return status; @@ -2155,7 +2119,7 @@ PJ_DEF(int) write_settings(pjsua_app_config *config, char *buf, pj_size_t max) pj_strcat2(&cfg, line); /* Uas-duration. */ - if (config->duration != NO_LIMIT_DURATION) { + if (config->duration != PJSUA_APP_NO_LIMIT_DURATION) { pj_ansi_sprintf(line, "--duration %d\n", config->duration); pj_strcat2(&cfg, line); diff --git a/pjsip-apps/src/pjsua/pjsua_legacy.c b/pjsip-apps/src/pjsua/pjsua_legacy.c index 1b0de505..6f2a5160 100644 --- a/pjsip-apps/src/pjsua/pjsua_legacy.c +++ b/pjsip-apps/src/pjsua/pjsua_legacy.c @@ -64,7 +64,7 @@ static void print_buddy_list() static void ui_input_url(const char *title, char *buf, int len, input_result *result) { - result->nb_result = NO_NB; + result->nb_result = PJSUA_APP_NO_NB; result->uri_result = NULL; print_buddy_list(); @@ -123,7 +123,7 @@ static void ui_input_url(const char *title, char *buf, int len, return; puts("Invalid input"); - result->nb_result = NO_NB; + result->nb_result = PJSUA_APP_NO_NB; return; } else { @@ -596,7 +596,7 @@ static void ui_make_new_call() printf("(You currently have %d calls)\n", pjsua_call_get_count()); ui_input_url("Make call", buf, sizeof(buf), &result); - if (result.nb_result != NO_NB) { + if (result.nb_result != PJSUA_APP_NO_NB) { if (result.nb_result == -1 || result.nb_result == 0) { puts("You can't do that with make call!"); @@ -639,7 +639,7 @@ static void ui_make_multi_call() return; ui_input_url("Make call", buf, sizeof(buf), &result); - if (result.nb_result != NO_NB) { + if (result.nb_result != PJSUA_APP_NO_NB) { pjsua_buddy_info binfo; if (result.nb_result == -1 || result.nb_result == 0) { puts("You can't do that with make call!"); @@ -681,7 +681,7 @@ static void ui_send_instant_message() /* Input destination. */ ui_input_url("Send IM to", buf, sizeof(buf), &result); - if (result.nb_result != NO_NB) { + if (result.nb_result != PJSUA_APP_NO_NB) { if (result.nb_result == -1) { puts("You can't send broadcast IM like that!"); @@ -1084,7 +1084,7 @@ static void ui_call_transfer(pj_bool_t no_refersub) &STR_FALSE); pj_list_push_back(&msg_data.hdr_list, &refer_sub); } - if (result.nb_result != NO_NB) { + if (result.nb_result != PJSUA_APP_NO_NB) { if (result.nb_result == -1 || result.nb_result == 0) puts("You can't do that with transfer call!"); else { @@ -1287,7 +1287,7 @@ static void ui_send_arbitrary_request() /* Input destination URI */ uri = NULL; ui_input_url("Destination URI", buf, sizeof(buf), &result); - if (result.nb_result != NO_NB) { + if (result.nb_result != PJSUA_APP_NO_NB) { if (result.nb_result == -1) { puts("Sorry you can't do that!"); @@ -1368,7 +1368,7 @@ static void ui_subscribe(char menuin[]) input_result result; ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result); - if (result.nb_result != NO_NB) { + if (result.nb_result != PJSUA_APP_NO_NB) { if (result.nb_result == -1) { int i, count; count = pjsua_get_buddy_count(); @@ -1669,21 +1669,11 @@ static void ui_call_redirect(char menuin[]) /* * Main "user interface" loop. */ -void console_app_main(const pj_str_t *uri_to_call, pj_bool_t *app_restart) +PJ_DEF(void) legacy_main() { char menuin[80]; char buf[128]; - pjsua_call_setting_default(&call_opt); - call_opt.aud_cnt = app_config.aud_cnt; - call_opt.vid_cnt = app_config.vid.vid_cnt; - - /* If user specifies URI to call, then call the URI */ - if (uri_to_call->slen) { - pjsua_call_make_call( current_acc, uri_to_call, &call_opt, - NULL, NULL, NULL); - } - keystroke_help(current_call); for (;;) { @@ -1914,10 +1904,8 @@ void console_app_main(const pj_str_t *uri_to_call, pj_bool_t *app_restart) break; case 'L': /* Restart */ - *app_restart = PJ_TRUE; - /* Continues below */ - case 'q': + legacy_on_stopped(menuin[0]=='L'); goto on_exit; case 'R': @@ -1936,23 +1924,3 @@ void console_app_main(const pj_str_t *uri_to_call, pj_bool_t *app_restart) on_exit: ; } - -void start_ui_main(pj_str_t *uri_to_call, pj_bool_t *app_restart) -{ - pj_status_t status; - *app_restart = PJ_FALSE; - - status = pjsua_start(); - if (status != PJ_SUCCESS) - return; - - setup_signal_handler(); - - /* If user specifies URI to call, then call the URI */ - if (uri_to_call->slen) { - pjsua_call_make_call(current_acc, uri_to_call, &call_opt, NULL, - NULL, NULL); - } - - console_app_main(uri_to_call, app_restart); -} -- cgit v1.2.3