From 43e18b0315cd729fbeea03c636e7d41b1d78d317 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Wed, 3 Apr 2013 22:41:23 +0000 Subject: More re #1643: changed svn file type property to native git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4459 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip-apps/src/pjsua/pjsua_cli_cmd.c | 5907 +++++++++++++++++----------------- pjsip-apps/src/pjsua/pjsua_cmd.c | 2055 ++++++------ pjsip-apps/src/pjsua/pjsua_cmd.h | 359 +-- pjsip-apps/src/pjsua/pjsua_ui_cmd.c | 3880 +++++++++++----------- 4 files changed, 6102 insertions(+), 6099 deletions(-) (limited to 'pjsip-apps/src/pjsua') diff --git a/pjsip-apps/src/pjsua/pjsua_cli_cmd.c b/pjsip-apps/src/pjsua/pjsua_cli_cmd.c index f0d3fc58..1781f4f2 100644 --- a/pjsip-apps/src/pjsua/pjsua_cli_cmd.c +++ b/pjsip-apps/src/pjsua/pjsua_cli_cmd.c @@ -1,2953 +1,2954 @@ -/* $Id: pjsua_cli_cmd.c 3664 2011-07-19 03:42:28Z nanang $ */ -/* - * 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_cmd.h" -#include -#include -#include -#include - -#define THIS_FILE "pjsua_cli_cmd.c" - -/* 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 - -/* 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 - -static pj_cli_cfg cli_cfg; -static pj_cli_telnet_cfg telnet_cfg; -static pj_cli_t *cli = NULL; -static pj_cli_sess *sess = NULL; -static pj_cli_console_cfg console_cfg; - -/* 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; - } -} - -void cli_console_app_main(const pj_str_t *uri_to_call, pj_bool_t *app_restart) -{ - 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); - } - - /* - * Main loop. - */ - for (;;) { - char cmdline[PJ_CLI_MAX_CMDBUF]; - pj_status_t status; - - status = pj_cli_console_process(sess, &cmdline[0], sizeof(cmdline)); - - if (status == PJ_CLI_EEXIT) { - /* exit is called */ - break; - } else if (status != PJ_SUCCESS) { - /* Something wrong with the cmdline */ - PJ_PERROR(1,(THIS_FILE, status, "Exec error")); - } - } - *app_restart = pj_cli_is_restarting(cli); -} - -/* - * 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; - - 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; - - 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; - - 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; - - 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) { - call_opt.flag |= PJSUA_CALL_UNHOLD; - 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); - - 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); - - 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); - - 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); - 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); - return PJ_SUCCESS; -} - -static pj_status_t cmd_restart_handler(pj_cli_cmd_val *cval) -{ - PJ_LOG(3,(THIS_FILE, "Restarting app..")); - pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_TRUE); - 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; -} - -static pj_status_t setup_command() -{ - 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; -} - -static void 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_status_t status; - pj_cli_cfg_default(&cli_cfg); - cli_cfg.pf = app_config.pool->factory; - cli_cfg.name = pj_str("pjsua_cli"); - cli_cfg.title = pj_str("Pjsua CLI Application"); - - status = pj_cli_create(&cli_cfg, &cli); - if (status != PJ_SUCCESS) - return status; - - status = setup_command(); - if (status != PJ_SUCCESS) - return status; - - pj_log_set_log_func(&log_writer); - - /* - * Init telnet - */ - pj_cli_telnet_cfg_default(&telnet_cfg); - telnet_cfg.log_level = 5; - if (app_config.cli_telnet_port) - telnet_cfg.port = (pj_uint16_t)app_config.cli_telnet_port; - - status = pj_cli_telnet_create(cli, &telnet_cfg, NULL); - if (status != PJ_SUCCESS) - return status; - - app_config.cli_telnet_port = telnet_cfg.port;; - - /* - * Init console - */ - pj_cli_console_cfg_default(&console_cfg); - console_cfg.quit_command = pj_str("shutdown"); - status = pj_cli_console_create(cli, &console_cfg, &sess, NULL); - return status; -} - -void destroy_cli() -{ - pj_log_set_log_func(&pj_log_write); - pj_cli_destroy(cli); - cli = NULL; -} +/* $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_cmd.h" +#include +#include +#include +#include + +#define THIS_FILE "pjsua_cli_cmd.c" + +/* 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 + +/* 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 + +static pj_cli_cfg cli_cfg; +static pj_cli_telnet_cfg telnet_cfg; +static pj_cli_t *cli = NULL; +static pj_cli_sess *sess = NULL; +static pj_cli_console_cfg console_cfg; + +/* 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; + } +} + +void cli_console_app_main(const pj_str_t *uri_to_call, pj_bool_t *app_restart) +{ + 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); + } + + /* + * Main loop. + */ + for (;;) { + char cmdline[PJ_CLI_MAX_CMDBUF]; + pj_status_t status; + + status = pj_cli_console_process(sess, &cmdline[0], sizeof(cmdline)); + + if (status == PJ_CLI_EEXIT) { + /* exit is called */ + break; + } else if (status != PJ_SUCCESS) { + /* Something wrong with the cmdline */ + PJ_PERROR(1,(THIS_FILE, status, "Exec error")); + } + } + *app_restart = pj_cli_is_restarting(cli); +} + +/* + * 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; + + 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; + + 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; + + 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; + + 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) { + call_opt.flag |= PJSUA_CALL_UNHOLD; + 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); + + 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); + + 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); + + 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); + 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); + return PJ_SUCCESS; +} + +static pj_status_t cmd_restart_handler(pj_cli_cmd_val *cval) +{ + PJ_LOG(3,(THIS_FILE, "Restarting app..")); + pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_TRUE); + 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; +} + +static pj_status_t setup_command() +{ + 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; +} + +static void 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_status_t status; + pj_cli_cfg_default(&cli_cfg); + cli_cfg.pf = app_config.pool->factory; + cli_cfg.name = pj_str("pjsua_cli"); + cli_cfg.title = pj_str("Pjsua CLI Application"); + + status = pj_cli_create(&cli_cfg, &cli); + if (status != PJ_SUCCESS) + return status; + + status = setup_command(); + if (status != PJ_SUCCESS) + return status; + + pj_log_set_log_func(&log_writer); + + /* + * Init telnet + */ + pj_cli_telnet_cfg_default(&telnet_cfg); + telnet_cfg.log_level = 5; + if (app_config.cli_telnet_port) + telnet_cfg.port = (pj_uint16_t)app_config.cli_telnet_port; + + status = pj_cli_telnet_create(cli, &telnet_cfg, NULL); + if (status != PJ_SUCCESS) + return status; + + app_config.cli_telnet_port = telnet_cfg.port;; + + /* + * Init console + */ + pj_cli_console_cfg_default(&console_cfg); + console_cfg.quit_command = pj_str("shutdown"); + status = pj_cli_console_create(cli, &console_cfg, &sess, NULL); + return status; +} + +void destroy_cli() +{ + pj_log_set_log_func(&pj_log_write); + pj_cli_destroy(cli); + cli = NULL; +} + diff --git a/pjsip-apps/src/pjsua/pjsua_cmd.c b/pjsip-apps/src/pjsua/pjsua_cmd.c index ea829b54..03a858ea 100644 --- a/pjsip-apps/src/pjsua/pjsua_cmd.c +++ b/pjsip-apps/src/pjsua/pjsua_cmd.c @@ -1,1027 +1,1028 @@ -/* $Id: pjsua_cmd.c 4347 2013-02-13 10:19:25Z nanang $ */ -/* - * 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_cmd.h" - -#define THIS_FILE "pjsua_cmd.c" - -#if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0) -# define SOME_BUF_SIZE (1024 * 10) -#else -# define SOME_BUF_SIZE (1024 * 3) -#endif - -static char some_buf[SOME_BUF_SIZE]; - -/** Variable definition **/ -int stdout_refresh = -1; -pj_bool_t stdout_refresh_quit = PJ_FALSE; -pjsua_call_id current_call = PJSUA_INVALID_ID; -pjsua_app_config app_config; -pjsua_call_setting call_opt; -pjsua_msg_data msg_data; - -PJ_DEF(int) my_atoi(const char *cs) -{ - pj_str_t s; - - pj_cstr(&s, cs); - if (cs[0] == '-') { - s.ptr++, s.slen--; - return 0 - (int)pj_strtoul(&s); - } else if (cs[0] == '+') { - s.ptr++, s.slen--; - return pj_strtoul(&s); - } else { - return pj_strtoul(&s); - } -} - -/* - * Find next call when current call is disconnected or when user - * press ']' - */ -PJ_DEF(pj_bool_t) find_next_call() -{ - int i, max; - - max = pjsua_call_get_max_count(); - for (i=current_call+1; i=0; --i) { - if (pjsua_call_is_active(i)) { - current_call = i; - return PJ_TRUE; - } - } - - for (i=max-1; i>current_call; --i) { - if (pjsua_call_is_active(i)) { - current_call = i; - return PJ_TRUE; - } - } - - current_call = PJSUA_INVALID_ID; - return PJ_FALSE; -} - -/* - * Send arbitrary request to remote host - */ -PJ_DEF(void) send_request(char *cstr_method, const pj_str_t *dst_uri) -{ - pj_str_t str_method; - pjsip_method method; - pjsip_tx_data *tdata; - pjsip_endpoint *endpt; - pj_status_t status; - - endpt = pjsua_get_pjsip_endpt(); - - str_method = pj_str(cstr_method); - pjsip_method_init_np(&method, &str_method); - - status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata); - - status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL); - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Unable to send request", status); - return; - } -} - -/* - * Print log of call states. Since call states may be too long for logger, - * printing it is a bit tricky, it should be printed part by part as long - * as the logger can accept. - */ -PJ_DEF(void) log_call_dump(int call_id) -{ - unsigned call_dump_len; - unsigned part_len; - unsigned part_idx; - unsigned log_decor; - - pjsua_call_dump(call_id, PJ_TRUE, some_buf, sizeof(some_buf), " "); - call_dump_len = strlen(some_buf); - - log_decor = pj_log_get_decor(); - pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR)); - PJ_LOG(3,(THIS_FILE, "\n")); - pj_log_set_decor(0); - - part_idx = 0; - part_len = PJ_LOG_MAX_SIZE-80; - while (part_idx < call_dump_len) { - char p_orig, *p; - - p = &some_buf[part_idx]; - if (part_idx + part_len > call_dump_len) - part_len = call_dump_len - part_idx; - p_orig = p[part_len]; - p[part_len] = '\0'; - PJ_LOG(3,(THIS_FILE, "%s", p)); - p[part_len] = p_orig; - part_idx += part_len; - } - pj_log_set_decor(log_decor); -} - -/* - * Save account settings - */ -static void write_account_settings(int acc_index, pj_str_t *result) -{ - unsigned i; - char line[128]; - pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index]; - - - pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index); - pj_strcat2(result, line); - - - /* Identity */ - if (acc_cfg->id.slen) { - pj_ansi_sprintf(line, "--id %.*s\n", - (int)acc_cfg->id.slen, - acc_cfg->id.ptr); - pj_strcat2(result, line); - } - - /* Registrar server */ - if (acc_cfg->reg_uri.slen) { - pj_ansi_sprintf(line, "--registrar %.*s\n", - (int)acc_cfg->reg_uri.slen, - acc_cfg->reg_uri.ptr); - pj_strcat2(result, line); - - pj_ansi_sprintf(line, "--reg-timeout %u\n", - acc_cfg->reg_timeout); - pj_strcat2(result, line); - } - - /* Contact */ - if (acc_cfg->force_contact.slen) { - pj_ansi_sprintf(line, "--contact %.*s\n", - (int)acc_cfg->force_contact.slen, - acc_cfg->force_contact.ptr); - pj_strcat2(result, line); - } - - /* Contact header parameters */ - if (acc_cfg->contact_params.slen) { - pj_ansi_sprintf(line, "--contact-params %.*s\n", - (int)acc_cfg->contact_params.slen, - acc_cfg->contact_params.ptr); - pj_strcat2(result, line); - } - - /* Contact URI parameters */ - if (acc_cfg->contact_uri_params.slen) { - pj_ansi_sprintf(line, "--contact-uri-params %.*s\n", - (int)acc_cfg->contact_uri_params.slen, - acc_cfg->contact_uri_params.ptr); - pj_strcat2(result, line); - } - - /* */ - if (acc_cfg->allow_contact_rewrite!=1) - { - pj_ansi_sprintf(line, "--auto-update-nat %i\n", - (int)acc_cfg->allow_contact_rewrite); - pj_strcat2(result, line); - } - -#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) - /* SRTP */ - if (acc_cfg->use_srtp) { - int use_srtp = (int)acc_cfg->use_srtp; - if (use_srtp == PJMEDIA_SRTP_OPTIONAL && - acc_cfg->srtp_optional_dup_offer) - { - use_srtp = 3; - } - pj_ansi_sprintf(line, "--use-srtp %i\n", use_srtp); - pj_strcat2(result, line); - } - if (acc_cfg->srtp_secure_signaling != - PJSUA_DEFAULT_SRTP_SECURE_SIGNALING) - { - pj_ansi_sprintf(line, "--srtp-secure %d\n", - acc_cfg->srtp_secure_signaling); - pj_strcat2(result, line); - } -#endif - - /* Proxy */ - for (i=0; iproxy_cnt; ++i) { - pj_ansi_sprintf(line, "--proxy %.*s\n", - (int)acc_cfg->proxy[i].slen, - acc_cfg->proxy[i].ptr); - pj_strcat2(result, line); - } - - /* Credentials */ - for (i=0; icred_count; ++i) { - if (acc_cfg->cred_info[i].realm.slen) { - pj_ansi_sprintf(line, "--realm %.*s\n", - (int)acc_cfg->cred_info[i].realm.slen, - acc_cfg->cred_info[i].realm.ptr); - pj_strcat2(result, line); - } - - if (acc_cfg->cred_info[i].username.slen) { - pj_ansi_sprintf(line, "--username %.*s\n", - (int)acc_cfg->cred_info[i].username.slen, - acc_cfg->cred_info[i].username.ptr); - pj_strcat2(result, line); - } - - if (acc_cfg->cred_info[i].data.slen) { - pj_ansi_sprintf(line, "--password %.*s\n", - (int)acc_cfg->cred_info[i].data.slen, - acc_cfg->cred_info[i].data.ptr); - pj_strcat2(result, line); - } - - if (i != acc_cfg->cred_count - 1) - pj_strcat2(result, "--next-cred\n"); - } - - /* reg-use-proxy */ - if (acc_cfg->reg_use_proxy != 3) { - pj_ansi_sprintf(line, "--reg-use-proxy %d\n", - acc_cfg->reg_use_proxy); - pj_strcat2(result, line); - } - - /* rereg-delay */ - if (acc_cfg->reg_retry_interval != PJSUA_REG_RETRY_INTERVAL) { - pj_ansi_sprintf(line, "--rereg-delay %d\n", - acc_cfg->reg_retry_interval); - pj_strcat2(result, line); - } - - /* 100rel extension */ - if (acc_cfg->require_100rel) { - pj_strcat2(result, "--use-100rel\n"); - } - - /* Session Timer extension */ - if (acc_cfg->use_timer) { - pj_ansi_sprintf(line, "--use-timer %d\n", - acc_cfg->use_timer); - pj_strcat2(result, line); - } - if (acc_cfg->timer_setting.min_se != 90) { - pj_ansi_sprintf(line, "--timer-min-se %d\n", - acc_cfg->timer_setting.min_se); - pj_strcat2(result, line); - } - if (acc_cfg->timer_setting.sess_expires != PJSIP_SESS_TIMER_DEF_SE) { - pj_ansi_sprintf(line, "--timer-se %d\n", - acc_cfg->timer_setting.sess_expires); - pj_strcat2(result, line); - } - - /* Publish */ - if (acc_cfg->publish_enabled) - pj_strcat2(result, "--publish\n"); - - /* MWI */ - if (acc_cfg->mwi_enabled) - pj_strcat2(result, "--mwi\n"); - - if (acc_cfg->sip_stun_use != PJSUA_STUN_USE_DEFAULT || - acc_cfg->media_stun_use != PJSUA_STUN_USE_DEFAULT) - { - pj_strcat2(result, "--disable-stun\n"); - } - - /* Media Transport*/ - if (acc_cfg->ice_cfg.enable_ice) - pj_strcat2(result, "--use-ice\n"); - - if (acc_cfg->ice_cfg.ice_opt.aggressive == PJ_FALSE) - pj_strcat2(result, "--ice-regular\n"); - - if (acc_cfg->turn_cfg.enable_turn) - pj_strcat2(result, "--use-turn\n"); - - if (acc_cfg->ice_cfg.ice_max_host_cands >= 0) { - pj_ansi_sprintf(line, "--ice_max_host_cands %d\n", - acc_cfg->ice_cfg.ice_max_host_cands); - pj_strcat2(result, line); - } - - if (acc_cfg->ice_cfg.ice_no_rtcp) - pj_strcat2(result, "--ice-no-rtcp\n"); - - if (acc_cfg->turn_cfg.turn_server.slen) { - pj_ansi_sprintf(line, "--turn-srv %.*s\n", - (int)acc_cfg->turn_cfg.turn_server.slen, - acc_cfg->turn_cfg.turn_server.ptr); - pj_strcat2(result, line); - } - - if (acc_cfg->turn_cfg.turn_conn_type == PJ_TURN_TP_TCP) - pj_strcat2(result, "--turn-tcp\n"); - - if (acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.slen) { - pj_ansi_sprintf(line, "--turn-user %.*s\n", - (int)acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.slen, - acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.ptr); - pj_strcat2(result, line); - } - - if (acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.slen) { - pj_ansi_sprintf(line, "--turn-passwd %.*s\n", - (int)acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.slen, - acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.ptr); - pj_strcat2(result, line); - } -} - -/* - * Write settings. - */ -PJ_DEF(int) write_settings(pjsua_app_config *config, char *buf, pj_size_t max) -{ - unsigned acc_index; - unsigned i; - pj_str_t cfg; - char line[128]; - extern pj_bool_t pjsip_use_compact_form; - - PJ_UNUSED_ARG(max); - - cfg.ptr = buf; - cfg.slen = 0; - - /* Logging. */ - pj_strcat2(&cfg, "#\n# Logging options:\n#\n"); - pj_ansi_sprintf(line, "--log-level %d\n", - config->log_cfg.level); - pj_strcat2(&cfg, line); - - pj_ansi_sprintf(line, "--app-log-level %d\n", - config->log_cfg.console_level); - pj_strcat2(&cfg, line); - - if (config->log_cfg.log_filename.slen) { - pj_ansi_sprintf(line, "--log-file %.*s\n", - (int)config->log_cfg.log_filename.slen, - config->log_cfg.log_filename.ptr); - pj_strcat2(&cfg, line); - } - - if (config->log_cfg.log_file_flags & PJ_O_APPEND) { - pj_strcat2(&cfg, "--log-append\n"); - } - - /* Save account settings. */ - for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) { - - write_account_settings(acc_index, &cfg); - - if (acc_index < config->acc_cnt-1) - pj_strcat2(&cfg, "--next-account\n"); - } - - pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n"); - - /* Nameservers */ - for (i=0; icfg.nameserver_count; ++i) { - pj_ansi_sprintf(line, "--nameserver %.*s\n", - (int)config->cfg.nameserver[i].slen, - config->cfg.nameserver[i].ptr); - pj_strcat2(&cfg, line); - } - - /* Outbound proxy */ - for (i=0; icfg.outbound_proxy_cnt; ++i) { - pj_ansi_sprintf(line, "--outbound %.*s\n", - (int)config->cfg.outbound_proxy[i].slen, - config->cfg.outbound_proxy[i].ptr); - pj_strcat2(&cfg, line); - } - - /* Transport options */ - if (config->ipv6) { - pj_strcat2(&cfg, "--ipv6\n"); - } - if (config->enable_qos) { - pj_strcat2(&cfg, "--set-qos\n"); - } - - /* UDP Transport. */ - pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port); - pj_strcat2(&cfg, line); - - /* IP address, if any. */ - if (config->udp_cfg.public_addr.slen) { - pj_ansi_sprintf(line, "--ip-addr %.*s\n", - (int)config->udp_cfg.public_addr.slen, - config->udp_cfg.public_addr.ptr); - pj_strcat2(&cfg, line); - } - - /* Bound IP address, if any. */ - if (config->udp_cfg.bound_addr.slen) { - pj_ansi_sprintf(line, "--bound-addr %.*s\n", - (int)config->udp_cfg.bound_addr.slen, - config->udp_cfg.bound_addr.ptr); - pj_strcat2(&cfg, line); - } - - /* No TCP ? */ - if (config->no_tcp) { - pj_strcat2(&cfg, "--no-tcp\n"); - } - - /* No UDP ? */ - if (config->no_udp) { - pj_strcat2(&cfg, "--no-udp\n"); - } - - /* STUN */ - for (i=0; icfg.stun_srv_cnt; ++i) { - pj_ansi_sprintf(line, "--stun-srv %.*s\n", - (int)config->cfg.stun_srv[i].slen, - config->cfg.stun_srv[i].ptr); - pj_strcat2(&cfg, line); - } - -#if defined(PJSIP_HAS_TLS_TRANSPORT) && (PJSIP_HAS_TLS_TRANSPORT != 0) - /* TLS */ - if (config->use_tls) - pj_strcat2(&cfg, "--use-tls\n"); - if (config->udp_cfg.tls_setting.ca_list_file.slen) { - pj_ansi_sprintf(line, "--tls-ca-file %.*s\n", - (int)config->udp_cfg.tls_setting.ca_list_file.slen, - config->udp_cfg.tls_setting.ca_list_file.ptr); - pj_strcat2(&cfg, line); - } - if (config->udp_cfg.tls_setting.cert_file.slen) { - pj_ansi_sprintf(line, "--tls-cert-file %.*s\n", - (int)config->udp_cfg.tls_setting.cert_file.slen, - config->udp_cfg.tls_setting.cert_file.ptr); - pj_strcat2(&cfg, line); - } - if (config->udp_cfg.tls_setting.privkey_file.slen) { - pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n", - (int)config->udp_cfg.tls_setting.privkey_file.slen, - config->udp_cfg.tls_setting.privkey_file.ptr); - pj_strcat2(&cfg, line); - } - - if (config->udp_cfg.tls_setting.password.slen) { - pj_ansi_sprintf(line, "--tls-password %.*s\n", - (int)config->udp_cfg.tls_setting.password.slen, - config->udp_cfg.tls_setting.password.ptr); - pj_strcat2(&cfg, line); - } - - if (config->udp_cfg.tls_setting.verify_server) - pj_strcat2(&cfg, "--tls-verify-server\n"); - - if (config->udp_cfg.tls_setting.verify_client) - pj_strcat2(&cfg, "--tls-verify-client\n"); - - if (config->udp_cfg.tls_setting.timeout.sec) { - pj_ansi_sprintf(line, "--tls-neg-timeout %d\n", - (int)config->udp_cfg.tls_setting.timeout.sec); - pj_strcat2(&cfg, line); - } - - for (i=0; iudp_cfg.tls_setting.ciphers_num; ++i) { - pj_ansi_sprintf(line, "--tls-cipher 0x%06X # %s\n", - config->udp_cfg.tls_setting.ciphers[i], - pj_ssl_cipher_name(config->udp_cfg.tls_setting.ciphers[i])); - pj_strcat2(&cfg, line); - } -#endif - - pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n"); - - /* Video & extra audio */ - for (i=0; ivid.vid_cnt; ++i) { - pj_strcat2(&cfg, "--video\n"); - } - for (i=1; iaud_cnt; ++i) { - pj_strcat2(&cfg, "--extra-audio\n"); - } - - /* SRTP */ -#if PJMEDIA_HAS_SRTP - if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) { - int use_srtp = (int)app_config.cfg.use_srtp; - if (use_srtp == PJMEDIA_SRTP_OPTIONAL && - app_config.cfg.srtp_optional_dup_offer) - { - use_srtp = 3; - } - pj_ansi_sprintf(line, "--use-srtp %d\n", use_srtp); - pj_strcat2(&cfg, line); - } - if (app_config.cfg.srtp_secure_signaling != - PJSUA_DEFAULT_SRTP_SECURE_SIGNALING) - { - pj_ansi_sprintf(line, "--srtp-secure %d\n", - app_config.cfg.srtp_secure_signaling); - pj_strcat2(&cfg, line); - } -#endif - - /* Media */ - if (config->null_audio) - pj_strcat2(&cfg, "--null-audio\n"); - if (config->auto_play) - pj_strcat2(&cfg, "--auto-play\n"); - if (config->auto_loop) - pj_strcat2(&cfg, "--auto-loop\n"); - if (config->auto_conf) - pj_strcat2(&cfg, "--auto-conf\n"); - for (i=0; iwav_count; ++i) { - pj_ansi_sprintf(line, "--play-file %s\n", - config->wav_files[i].ptr); - pj_strcat2(&cfg, line); - } - for (i=0; itone_count; ++i) { - pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n", - config->tones[i].freq1, config->tones[i].freq2, - config->tones[i].on_msec, config->tones[i].off_msec); - pj_strcat2(&cfg, line); - } - if (config->rec_file.slen) { - pj_ansi_sprintf(line, "--rec-file %s\n", - config->rec_file.ptr); - pj_strcat2(&cfg, line); - } - if (config->auto_rec) - pj_strcat2(&cfg, "--auto-rec\n"); - if (config->capture_dev != PJSUA_INVALID_ID) { - pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev); - pj_strcat2(&cfg, line); - } - if (config->playback_dev != PJSUA_INVALID_ID) { - pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev); - pj_strcat2(&cfg, line); - } - if (config->media_cfg.snd_auto_close_time != -1) { - pj_ansi_sprintf(line, "--snd-auto-close %d\n", - config->media_cfg.snd_auto_close_time); - pj_strcat2(&cfg, line); - } - if (config->no_tones) { - pj_strcat2(&cfg, "--no-tones\n"); - } - if (config->media_cfg.jb_max != -1) { - pj_ansi_sprintf(line, "--jb-max-size %d\n", - config->media_cfg.jb_max); - pj_strcat2(&cfg, line); - } - - /* Sound device latency */ - if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) { - pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat); - pj_strcat2(&cfg, line); - } - if (config->playback_lat != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) { - pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat); - pj_strcat2(&cfg, line); - } - - /* Media clock rate. */ - if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) { - pj_ansi_sprintf(line, "--clock-rate %d\n", - config->media_cfg.clock_rate); - pj_strcat2(&cfg, line); - } else { - pj_ansi_sprintf(line, "#using default --clock-rate %d\n", - config->media_cfg.clock_rate); - pj_strcat2(&cfg, line); - } - - if (config->media_cfg.snd_clock_rate && - config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate) - { - pj_ansi_sprintf(line, "--snd-clock-rate %d\n", - config->media_cfg.snd_clock_rate); - pj_strcat2(&cfg, line); - } - - /* Stereo mode. */ - if (config->media_cfg.channel_count == 2) { - pj_ansi_sprintf(line, "--stereo\n"); - pj_strcat2(&cfg, line); - } - - /* quality */ - if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) { - pj_ansi_sprintf(line, "--quality %d\n", - config->media_cfg.quality); - pj_strcat2(&cfg, line); - } else { - pj_ansi_sprintf(line, "#using default --quality %d\n", - config->media_cfg.quality); - pj_strcat2(&cfg, line); - } - - if (config->vid.vcapture_dev != PJMEDIA_VID_DEFAULT_CAPTURE_DEV) { - pj_ansi_sprintf(line, "--vcapture-dev %d\n", config->vid.vcapture_dev); - pj_strcat2(&cfg, line); - } - if (config->vid.vrender_dev != PJMEDIA_VID_DEFAULT_RENDER_DEV) { - pj_ansi_sprintf(line, "--vrender-dev %d\n", config->vid.vrender_dev); - pj_strcat2(&cfg, line); - } - for (i=0; iavi_cnt; ++i) { - pj_ansi_sprintf(line, "--play-avi %s\n", config->avi[i].path.ptr); - pj_strcat2(&cfg, line); - } - if (config->avi_auto_play) { - pj_ansi_sprintf(line, "--auto-play-avi\n"); - pj_strcat2(&cfg, line); - } - - /* ptime */ - if (config->media_cfg.ptime) { - pj_ansi_sprintf(line, "--ptime %d\n", - config->media_cfg.ptime); - pj_strcat2(&cfg, line); - } - - /* no-vad */ - if (config->media_cfg.no_vad) { - pj_strcat2(&cfg, "--no-vad\n"); - } - - /* ec-tail */ - if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) { - pj_ansi_sprintf(line, "--ec-tail %d\n", - config->media_cfg.ec_tail_len); - pj_strcat2(&cfg, line); - } else { - pj_ansi_sprintf(line, "#using default --ec-tail %d\n", - config->media_cfg.ec_tail_len); - pj_strcat2(&cfg, line); - } - - /* ec-opt */ - if (config->media_cfg.ec_options != 0) { - pj_ansi_sprintf(line, "--ec-opt %d\n", - config->media_cfg.ec_options); - pj_strcat2(&cfg, line); - } - - /* ilbc-mode */ - if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) { - pj_ansi_sprintf(line, "--ilbc-mode %d\n", - config->media_cfg.ilbc_mode); - pj_strcat2(&cfg, line); - } else { - pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n", - config->media_cfg.ilbc_mode); - pj_strcat2(&cfg, line); - } - - /* RTP drop */ - if (config->media_cfg.tx_drop_pct) { - pj_ansi_sprintf(line, "--tx-drop-pct %d\n", - config->media_cfg.tx_drop_pct); - pj_strcat2(&cfg, line); - - } - if (config->media_cfg.rx_drop_pct) { - pj_ansi_sprintf(line, "--rx-drop-pct %d\n", - config->media_cfg.rx_drop_pct); - pj_strcat2(&cfg, line); - - } - - /* Start RTP port. */ - pj_ansi_sprintf(line, "--rtp-port %d\n", - config->rtp_cfg.port); - pj_strcat2(&cfg, line); - - /* Disable codec */ - for (i=0; icodec_dis_cnt; ++i) { - pj_ansi_sprintf(line, "--dis-codec %s\n", - config->codec_dis[i].ptr); - pj_strcat2(&cfg, line); - } - /* Add codec. */ - for (i=0; icodec_cnt; ++i) { - pj_ansi_sprintf(line, "--add-codec %s\n", - config->codec_arg[i].ptr); - pj_strcat2(&cfg, line); - } - - pj_strcat2(&cfg, "\n#\n# User agent:\n#\n"); - - /* Auto-answer. */ - if (config->auto_answer != 0) { - pj_ansi_sprintf(line, "--auto-answer %d\n", - config->auto_answer); - pj_strcat2(&cfg, line); - } - - /* accept-redirect */ - if (config->redir_op != PJSIP_REDIRECT_ACCEPT_REPLACE) { - pj_ansi_sprintf(line, "--accept-redirect %d\n", - config->redir_op); - pj_strcat2(&cfg, line); - } - - /* Max calls. */ - pj_ansi_sprintf(line, "--max-calls %d\n", - config->cfg.max_calls); - pj_strcat2(&cfg, line); - - /* Uas-duration. */ - if (config->duration != NO_LIMIT_DURATION) { - pj_ansi_sprintf(line, "--duration %d\n", - config->duration); - pj_strcat2(&cfg, line); - } - - /* norefersub ? */ - if (config->no_refersub) { - pj_strcat2(&cfg, "--norefersub\n"); - } - - if (pjsip_use_compact_form) - { - pj_strcat2(&cfg, "--use-compact-form\n"); - } - - if (!config->cfg.force_lr) { - pj_strcat2(&cfg, "--no-force-lr\n"); - } - - pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n"); - - /* Add buddies. */ - for (i=0; ibuddy_cnt; ++i) { - pj_ansi_sprintf(line, "--add-buddy %.*s\n", - (int)config->buddy_cfg[i].uri.slen, - config->buddy_cfg[i].uri.ptr); - pj_strcat2(&cfg, line); - } - - /* SIP extensions. */ - pj_strcat2(&cfg, "\n#\n# SIP extensions:\n#\n"); - /* 100rel extension */ - if (config->cfg.require_100rel) { - pj_strcat2(&cfg, "--use-100rel\n"); - } - /* Session Timer extension */ - if (config->cfg.use_timer) { - pj_ansi_sprintf(line, "--use-timer %d\n", - config->cfg.use_timer); - pj_strcat2(&cfg, line); - } - if (config->cfg.timer_setting.min_se != 90) { - pj_ansi_sprintf(line, "--timer-min-se %d\n", - config->cfg.timer_setting.min_se); - pj_strcat2(&cfg, line); - } - if (config->cfg.timer_setting.sess_expires != PJSIP_SESS_TIMER_DEF_SE) { - pj_ansi_sprintf(line, "--timer-se %d\n", - config->cfg.timer_setting.sess_expires); - pj_strcat2(&cfg, line); - } - - *(cfg.ptr + cfg.slen) = '\0'; - return cfg.slen; -} - -#ifdef PJSUA_HAS_VIDEO -PJ_DEF(void) app_config_init_video(pjsua_acc_config *acc_cfg) -{ - acc_cfg->vid_in_auto_show = app_config.vid.in_auto_show; - acc_cfg->vid_out_auto_transmit = app_config.vid.out_auto_transmit; - /* Note that normally GUI application will prefer a borderless - * window. - */ - acc_cfg->vid_wnd_flags = PJMEDIA_VID_DEV_WND_BORDER | - PJMEDIA_VID_DEV_WND_RESIZABLE; - acc_cfg->vid_cap_dev = app_config.vid.vcapture_dev; - acc_cfg->vid_rend_dev = app_config.vid.vrender_dev; - - if (app_config.avi_auto_play && - app_config.avi_def_idx != PJSUA_INVALID_ID && - app_config.avi[app_config.avi_def_idx].dev_id != PJMEDIA_VID_INVALID_DEV) - { - acc_cfg->vid_cap_dev = app_config.avi[app_config.avi_def_idx].dev_id; - } -} -#else -PJ_DEF(void) app_config_init_video(pjsua_acc_config *acc_cfg) -{ - PJ_UNUSED_ARG(acc_cfg); -} -#endif - -#ifdef HAVE_MULTIPART_TEST - /* - * Enable multipart in msg_data and add a dummy body into the - * multipart bodies. - */ - PJ_DEF(void) add_multipart(pjsua_msg_data *msg_data) - { - static pjsip_multipart_part *alt_part; - - if (!alt_part) { - pj_str_t type, subtype, content; - - alt_part = pjsip_multipart_create_part(app_config.pool); - - type = pj_str("text"); - subtype = pj_str("plain"); - content = pj_str("Sample text body of a multipart bodies"); - alt_part->body = pjsip_msg_body_create(app_config.pool, &type, - &subtype, &content); - } - - msg_data->multipart_ctype.type = pj_str("multipart"); - msg_data->multipart_ctype.subtype = pj_str("mixed"); - pj_list_push_back(&msg_data->multipart_parts, alt_part); - } -#endif - -/* arrange windows. arg: - * -1: arrange all windows - * != -1: arrange only this window id - */ -PJ_DEF(void) arrange_window(pjsua_vid_win_id wid) -{ -#if PJSUA_HAS_VIDEO - pjmedia_coord pos; - int i, last; - - pos.x = 0; - pos.y = 10; - last = (wid == PJSUA_INVALID_ID) ? PJSUA_MAX_VID_WINS : wid; - - for (i=0; idir == PJMEDIA_DIR_CAPTURE_RENDER) { - dirname = "capture, render"; - } else if (vdi->dir == PJMEDIA_DIR_CAPTURE) { - dirname = "capture"; - } else { - dirname = "render"; - } - - - capnames[0] = '\0'; - for (i=0; icaps & (1 << i)) { - const char *capname = pjmedia_vid_dev_cap_name(1 << i, NULL); - if (capname) { - if (*capnames) - strcat(capnames, ", "); - strncat(capnames, capname, - sizeof(capnames)-strlen(capnames)-1); - } - } - } - - formats[0] = '\0'; - for (i=0; ifmt_cnt; ++i) { - const pjmedia_video_format_info *vfi = - pjmedia_get_video_format_info(NULL, vdi->fmt[i].id); - if (vfi) { - if (*formats) - strcat(formats, ", "); - strncat(formats, vfi->name, sizeof(formats)-strlen(formats)-1); - } - } - - PJ_LOG(3,(THIS_FILE, "%3d %s [%s][%s] %s", id, vdi->name, vdi->driver, - dirname, title)); - PJ_LOG(3,(THIS_FILE, " Supported capabilities: %s", capnames)); - PJ_LOG(3,(THIS_FILE, " Supported formats: %s", formats)); -} - -PJ_DEF(void) vid_list_devs() -{ - unsigned i, count; - pjmedia_vid_dev_info vdi; - pj_status_t status; - - PJ_LOG(3,(THIS_FILE, "Video device list:")); - count = pjsua_vid_dev_count(); - if (count == 0) { - PJ_LOG(3,(THIS_FILE, " - no device detected -")); - return; - } else { - PJ_LOG(3,(THIS_FILE, "%d device(s) detected:", count)); - } - - status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi); - if (status == PJ_SUCCESS) - vid_print_dev(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi, - "(default renderer device)"); - - status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi); - if (status == PJ_SUCCESS) - vid_print_dev(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi, - "(default capture device)"); - - for (i=0; ivid_in_auto_show, - acc_cfg->vid_out_auto_transmit, - acc_cfg->vid_cap_dev, - acc_cfg->vid_rend_dev)); -} - - -#endif +/* $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_cmd.h" + +#define THIS_FILE "pjsua_cmd.c" + +#if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0) +# define SOME_BUF_SIZE (1024 * 10) +#else +# define SOME_BUF_SIZE (1024 * 3) +#endif + +static char some_buf[SOME_BUF_SIZE]; + +/** Variable definition **/ +int stdout_refresh = -1; +pj_bool_t stdout_refresh_quit = PJ_FALSE; +pjsua_call_id current_call = PJSUA_INVALID_ID; +pjsua_app_config app_config; +pjsua_call_setting call_opt; +pjsua_msg_data msg_data; + +PJ_DEF(int) my_atoi(const char *cs) +{ + pj_str_t s; + + pj_cstr(&s, cs); + if (cs[0] == '-') { + s.ptr++, s.slen--; + return 0 - (int)pj_strtoul(&s); + } else if (cs[0] == '+') { + s.ptr++, s.slen--; + return pj_strtoul(&s); + } else { + return pj_strtoul(&s); + } +} + +/* + * Find next call when current call is disconnected or when user + * press ']' + */ +PJ_DEF(pj_bool_t) find_next_call() +{ + int i, max; + + max = pjsua_call_get_max_count(); + for (i=current_call+1; i=0; --i) { + if (pjsua_call_is_active(i)) { + current_call = i; + return PJ_TRUE; + } + } + + for (i=max-1; i>current_call; --i) { + if (pjsua_call_is_active(i)) { + current_call = i; + return PJ_TRUE; + } + } + + current_call = PJSUA_INVALID_ID; + return PJ_FALSE; +} + +/* + * Send arbitrary request to remote host + */ +PJ_DEF(void) send_request(char *cstr_method, const pj_str_t *dst_uri) +{ + pj_str_t str_method; + pjsip_method method; + pjsip_tx_data *tdata; + pjsip_endpoint *endpt; + pj_status_t status; + + endpt = pjsua_get_pjsip_endpt(); + + str_method = pj_str(cstr_method); + pjsip_method_init_np(&method, &str_method); + + status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata); + + status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Unable to send request", status); + return; + } +} + +/* + * Print log of call states. Since call states may be too long for logger, + * printing it is a bit tricky, it should be printed part by part as long + * as the logger can accept. + */ +PJ_DEF(void) log_call_dump(int call_id) +{ + unsigned call_dump_len; + unsigned part_len; + unsigned part_idx; + unsigned log_decor; + + pjsua_call_dump(call_id, PJ_TRUE, some_buf, sizeof(some_buf), " "); + call_dump_len = strlen(some_buf); + + log_decor = pj_log_get_decor(); + pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR)); + PJ_LOG(3,(THIS_FILE, "\n")); + pj_log_set_decor(0); + + part_idx = 0; + part_len = PJ_LOG_MAX_SIZE-80; + while (part_idx < call_dump_len) { + char p_orig, *p; + + p = &some_buf[part_idx]; + if (part_idx + part_len > call_dump_len) + part_len = call_dump_len - part_idx; + p_orig = p[part_len]; + p[part_len] = '\0'; + PJ_LOG(3,(THIS_FILE, "%s", p)); + p[part_len] = p_orig; + part_idx += part_len; + } + pj_log_set_decor(log_decor); +} + +/* + * Save account settings + */ +static void write_account_settings(int acc_index, pj_str_t *result) +{ + unsigned i; + char line[128]; + pjsua_acc_config *acc_cfg = &app_config.acc_cfg[acc_index]; + + + pj_ansi_sprintf(line, "\n#\n# Account %d:\n#\n", acc_index); + pj_strcat2(result, line); + + + /* Identity */ + if (acc_cfg->id.slen) { + pj_ansi_sprintf(line, "--id %.*s\n", + (int)acc_cfg->id.slen, + acc_cfg->id.ptr); + pj_strcat2(result, line); + } + + /* Registrar server */ + if (acc_cfg->reg_uri.slen) { + pj_ansi_sprintf(line, "--registrar %.*s\n", + (int)acc_cfg->reg_uri.slen, + acc_cfg->reg_uri.ptr); + pj_strcat2(result, line); + + pj_ansi_sprintf(line, "--reg-timeout %u\n", + acc_cfg->reg_timeout); + pj_strcat2(result, line); + } + + /* Contact */ + if (acc_cfg->force_contact.slen) { + pj_ansi_sprintf(line, "--contact %.*s\n", + (int)acc_cfg->force_contact.slen, + acc_cfg->force_contact.ptr); + pj_strcat2(result, line); + } + + /* Contact header parameters */ + if (acc_cfg->contact_params.slen) { + pj_ansi_sprintf(line, "--contact-params %.*s\n", + (int)acc_cfg->contact_params.slen, + acc_cfg->contact_params.ptr); + pj_strcat2(result, line); + } + + /* Contact URI parameters */ + if (acc_cfg->contact_uri_params.slen) { + pj_ansi_sprintf(line, "--contact-uri-params %.*s\n", + (int)acc_cfg->contact_uri_params.slen, + acc_cfg->contact_uri_params.ptr); + pj_strcat2(result, line); + } + + /* */ + if (acc_cfg->allow_contact_rewrite!=1) + { + pj_ansi_sprintf(line, "--auto-update-nat %i\n", + (int)acc_cfg->allow_contact_rewrite); + pj_strcat2(result, line); + } + +#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) + /* SRTP */ + if (acc_cfg->use_srtp) { + int use_srtp = (int)acc_cfg->use_srtp; + if (use_srtp == PJMEDIA_SRTP_OPTIONAL && + acc_cfg->srtp_optional_dup_offer) + { + use_srtp = 3; + } + pj_ansi_sprintf(line, "--use-srtp %i\n", use_srtp); + pj_strcat2(result, line); + } + if (acc_cfg->srtp_secure_signaling != + PJSUA_DEFAULT_SRTP_SECURE_SIGNALING) + { + pj_ansi_sprintf(line, "--srtp-secure %d\n", + acc_cfg->srtp_secure_signaling); + pj_strcat2(result, line); + } +#endif + + /* Proxy */ + for (i=0; iproxy_cnt; ++i) { + pj_ansi_sprintf(line, "--proxy %.*s\n", + (int)acc_cfg->proxy[i].slen, + acc_cfg->proxy[i].ptr); + pj_strcat2(result, line); + } + + /* Credentials */ + for (i=0; icred_count; ++i) { + if (acc_cfg->cred_info[i].realm.slen) { + pj_ansi_sprintf(line, "--realm %.*s\n", + (int)acc_cfg->cred_info[i].realm.slen, + acc_cfg->cred_info[i].realm.ptr); + pj_strcat2(result, line); + } + + if (acc_cfg->cred_info[i].username.slen) { + pj_ansi_sprintf(line, "--username %.*s\n", + (int)acc_cfg->cred_info[i].username.slen, + acc_cfg->cred_info[i].username.ptr); + pj_strcat2(result, line); + } + + if (acc_cfg->cred_info[i].data.slen) { + pj_ansi_sprintf(line, "--password %.*s\n", + (int)acc_cfg->cred_info[i].data.slen, + acc_cfg->cred_info[i].data.ptr); + pj_strcat2(result, line); + } + + if (i != acc_cfg->cred_count - 1) + pj_strcat2(result, "--next-cred\n"); + } + + /* reg-use-proxy */ + if (acc_cfg->reg_use_proxy != 3) { + pj_ansi_sprintf(line, "--reg-use-proxy %d\n", + acc_cfg->reg_use_proxy); + pj_strcat2(result, line); + } + + /* rereg-delay */ + if (acc_cfg->reg_retry_interval != PJSUA_REG_RETRY_INTERVAL) { + pj_ansi_sprintf(line, "--rereg-delay %d\n", + acc_cfg->reg_retry_interval); + pj_strcat2(result, line); + } + + /* 100rel extension */ + if (acc_cfg->require_100rel) { + pj_strcat2(result, "--use-100rel\n"); + } + + /* Session Timer extension */ + if (acc_cfg->use_timer) { + pj_ansi_sprintf(line, "--use-timer %d\n", + acc_cfg->use_timer); + pj_strcat2(result, line); + } + if (acc_cfg->timer_setting.min_se != 90) { + pj_ansi_sprintf(line, "--timer-min-se %d\n", + acc_cfg->timer_setting.min_se); + pj_strcat2(result, line); + } + if (acc_cfg->timer_setting.sess_expires != PJSIP_SESS_TIMER_DEF_SE) { + pj_ansi_sprintf(line, "--timer-se %d\n", + acc_cfg->timer_setting.sess_expires); + pj_strcat2(result, line); + } + + /* Publish */ + if (acc_cfg->publish_enabled) + pj_strcat2(result, "--publish\n"); + + /* MWI */ + if (acc_cfg->mwi_enabled) + pj_strcat2(result, "--mwi\n"); + + if (acc_cfg->sip_stun_use != PJSUA_STUN_USE_DEFAULT || + acc_cfg->media_stun_use != PJSUA_STUN_USE_DEFAULT) + { + pj_strcat2(result, "--disable-stun\n"); + } + + /* Media Transport*/ + if (acc_cfg->ice_cfg.enable_ice) + pj_strcat2(result, "--use-ice\n"); + + if (acc_cfg->ice_cfg.ice_opt.aggressive == PJ_FALSE) + pj_strcat2(result, "--ice-regular\n"); + + if (acc_cfg->turn_cfg.enable_turn) + pj_strcat2(result, "--use-turn\n"); + + if (acc_cfg->ice_cfg.ice_max_host_cands >= 0) { + pj_ansi_sprintf(line, "--ice_max_host_cands %d\n", + acc_cfg->ice_cfg.ice_max_host_cands); + pj_strcat2(result, line); + } + + if (acc_cfg->ice_cfg.ice_no_rtcp) + pj_strcat2(result, "--ice-no-rtcp\n"); + + if (acc_cfg->turn_cfg.turn_server.slen) { + pj_ansi_sprintf(line, "--turn-srv %.*s\n", + (int)acc_cfg->turn_cfg.turn_server.slen, + acc_cfg->turn_cfg.turn_server.ptr); + pj_strcat2(result, line); + } + + if (acc_cfg->turn_cfg.turn_conn_type == PJ_TURN_TP_TCP) + pj_strcat2(result, "--turn-tcp\n"); + + if (acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.slen) { + pj_ansi_sprintf(line, "--turn-user %.*s\n", + (int)acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.slen, + acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.username.ptr); + pj_strcat2(result, line); + } + + if (acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.slen) { + pj_ansi_sprintf(line, "--turn-passwd %.*s\n", + (int)acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.slen, + acc_cfg->turn_cfg.turn_auth_cred.data.static_cred.data.ptr); + pj_strcat2(result, line); + } +} + +/* + * Write settings. + */ +PJ_DEF(int) write_settings(pjsua_app_config *config, char *buf, pj_size_t max) +{ + unsigned acc_index; + unsigned i; + pj_str_t cfg; + char line[128]; + extern pj_bool_t pjsip_use_compact_form; + + PJ_UNUSED_ARG(max); + + cfg.ptr = buf; + cfg.slen = 0; + + /* Logging. */ + pj_strcat2(&cfg, "#\n# Logging options:\n#\n"); + pj_ansi_sprintf(line, "--log-level %d\n", + config->log_cfg.level); + pj_strcat2(&cfg, line); + + pj_ansi_sprintf(line, "--app-log-level %d\n", + config->log_cfg.console_level); + pj_strcat2(&cfg, line); + + if (config->log_cfg.log_filename.slen) { + pj_ansi_sprintf(line, "--log-file %.*s\n", + (int)config->log_cfg.log_filename.slen, + config->log_cfg.log_filename.ptr); + pj_strcat2(&cfg, line); + } + + if (config->log_cfg.log_file_flags & PJ_O_APPEND) { + pj_strcat2(&cfg, "--log-append\n"); + } + + /* Save account settings. */ + for (acc_index=0; acc_index < config->acc_cnt; ++acc_index) { + + write_account_settings(acc_index, &cfg); + + if (acc_index < config->acc_cnt-1) + pj_strcat2(&cfg, "--next-account\n"); + } + + pj_strcat2(&cfg, "\n#\n# Network settings:\n#\n"); + + /* Nameservers */ + for (i=0; icfg.nameserver_count; ++i) { + pj_ansi_sprintf(line, "--nameserver %.*s\n", + (int)config->cfg.nameserver[i].slen, + config->cfg.nameserver[i].ptr); + pj_strcat2(&cfg, line); + } + + /* Outbound proxy */ + for (i=0; icfg.outbound_proxy_cnt; ++i) { + pj_ansi_sprintf(line, "--outbound %.*s\n", + (int)config->cfg.outbound_proxy[i].slen, + config->cfg.outbound_proxy[i].ptr); + pj_strcat2(&cfg, line); + } + + /* Transport options */ + if (config->ipv6) { + pj_strcat2(&cfg, "--ipv6\n"); + } + if (config->enable_qos) { + pj_strcat2(&cfg, "--set-qos\n"); + } + + /* UDP Transport. */ + pj_ansi_sprintf(line, "--local-port %d\n", config->udp_cfg.port); + pj_strcat2(&cfg, line); + + /* IP address, if any. */ + if (config->udp_cfg.public_addr.slen) { + pj_ansi_sprintf(line, "--ip-addr %.*s\n", + (int)config->udp_cfg.public_addr.slen, + config->udp_cfg.public_addr.ptr); + pj_strcat2(&cfg, line); + } + + /* Bound IP address, if any. */ + if (config->udp_cfg.bound_addr.slen) { + pj_ansi_sprintf(line, "--bound-addr %.*s\n", + (int)config->udp_cfg.bound_addr.slen, + config->udp_cfg.bound_addr.ptr); + pj_strcat2(&cfg, line); + } + + /* No TCP ? */ + if (config->no_tcp) { + pj_strcat2(&cfg, "--no-tcp\n"); + } + + /* No UDP ? */ + if (config->no_udp) { + pj_strcat2(&cfg, "--no-udp\n"); + } + + /* STUN */ + for (i=0; icfg.stun_srv_cnt; ++i) { + pj_ansi_sprintf(line, "--stun-srv %.*s\n", + (int)config->cfg.stun_srv[i].slen, + config->cfg.stun_srv[i].ptr); + pj_strcat2(&cfg, line); + } + +#if defined(PJSIP_HAS_TLS_TRANSPORT) && (PJSIP_HAS_TLS_TRANSPORT != 0) + /* TLS */ + if (config->use_tls) + pj_strcat2(&cfg, "--use-tls\n"); + if (config->udp_cfg.tls_setting.ca_list_file.slen) { + pj_ansi_sprintf(line, "--tls-ca-file %.*s\n", + (int)config->udp_cfg.tls_setting.ca_list_file.slen, + config->udp_cfg.tls_setting.ca_list_file.ptr); + pj_strcat2(&cfg, line); + } + if (config->udp_cfg.tls_setting.cert_file.slen) { + pj_ansi_sprintf(line, "--tls-cert-file %.*s\n", + (int)config->udp_cfg.tls_setting.cert_file.slen, + config->udp_cfg.tls_setting.cert_file.ptr); + pj_strcat2(&cfg, line); + } + if (config->udp_cfg.tls_setting.privkey_file.slen) { + pj_ansi_sprintf(line, "--tls-privkey-file %.*s\n", + (int)config->udp_cfg.tls_setting.privkey_file.slen, + config->udp_cfg.tls_setting.privkey_file.ptr); + pj_strcat2(&cfg, line); + } + + if (config->udp_cfg.tls_setting.password.slen) { + pj_ansi_sprintf(line, "--tls-password %.*s\n", + (int)config->udp_cfg.tls_setting.password.slen, + config->udp_cfg.tls_setting.password.ptr); + pj_strcat2(&cfg, line); + } + + if (config->udp_cfg.tls_setting.verify_server) + pj_strcat2(&cfg, "--tls-verify-server\n"); + + if (config->udp_cfg.tls_setting.verify_client) + pj_strcat2(&cfg, "--tls-verify-client\n"); + + if (config->udp_cfg.tls_setting.timeout.sec) { + pj_ansi_sprintf(line, "--tls-neg-timeout %d\n", + (int)config->udp_cfg.tls_setting.timeout.sec); + pj_strcat2(&cfg, line); + } + + for (i=0; iudp_cfg.tls_setting.ciphers_num; ++i) { + pj_ansi_sprintf(line, "--tls-cipher 0x%06X # %s\n", + config->udp_cfg.tls_setting.ciphers[i], + pj_ssl_cipher_name(config->udp_cfg.tls_setting.ciphers[i])); + pj_strcat2(&cfg, line); + } +#endif + + pj_strcat2(&cfg, "\n#\n# Media settings:\n#\n"); + + /* Video & extra audio */ + for (i=0; ivid.vid_cnt; ++i) { + pj_strcat2(&cfg, "--video\n"); + } + for (i=1; iaud_cnt; ++i) { + pj_strcat2(&cfg, "--extra-audio\n"); + } + + /* SRTP */ +#if PJMEDIA_HAS_SRTP + if (app_config.cfg.use_srtp != PJSUA_DEFAULT_USE_SRTP) { + int use_srtp = (int)app_config.cfg.use_srtp; + if (use_srtp == PJMEDIA_SRTP_OPTIONAL && + app_config.cfg.srtp_optional_dup_offer) + { + use_srtp = 3; + } + pj_ansi_sprintf(line, "--use-srtp %d\n", use_srtp); + pj_strcat2(&cfg, line); + } + if (app_config.cfg.srtp_secure_signaling != + PJSUA_DEFAULT_SRTP_SECURE_SIGNALING) + { + pj_ansi_sprintf(line, "--srtp-secure %d\n", + app_config.cfg.srtp_secure_signaling); + pj_strcat2(&cfg, line); + } +#endif + + /* Media */ + if (config->null_audio) + pj_strcat2(&cfg, "--null-audio\n"); + if (config->auto_play) + pj_strcat2(&cfg, "--auto-play\n"); + if (config->auto_loop) + pj_strcat2(&cfg, "--auto-loop\n"); + if (config->auto_conf) + pj_strcat2(&cfg, "--auto-conf\n"); + for (i=0; iwav_count; ++i) { + pj_ansi_sprintf(line, "--play-file %s\n", + config->wav_files[i].ptr); + pj_strcat2(&cfg, line); + } + for (i=0; itone_count; ++i) { + pj_ansi_sprintf(line, "--play-tone %d,%d,%d,%d\n", + config->tones[i].freq1, config->tones[i].freq2, + config->tones[i].on_msec, config->tones[i].off_msec); + pj_strcat2(&cfg, line); + } + if (config->rec_file.slen) { + pj_ansi_sprintf(line, "--rec-file %s\n", + config->rec_file.ptr); + pj_strcat2(&cfg, line); + } + if (config->auto_rec) + pj_strcat2(&cfg, "--auto-rec\n"); + if (config->capture_dev != PJSUA_INVALID_ID) { + pj_ansi_sprintf(line, "--capture-dev %d\n", config->capture_dev); + pj_strcat2(&cfg, line); + } + if (config->playback_dev != PJSUA_INVALID_ID) { + pj_ansi_sprintf(line, "--playback-dev %d\n", config->playback_dev); + pj_strcat2(&cfg, line); + } + if (config->media_cfg.snd_auto_close_time != -1) { + pj_ansi_sprintf(line, "--snd-auto-close %d\n", + config->media_cfg.snd_auto_close_time); + pj_strcat2(&cfg, line); + } + if (config->no_tones) { + pj_strcat2(&cfg, "--no-tones\n"); + } + if (config->media_cfg.jb_max != -1) { + pj_ansi_sprintf(line, "--jb-max-size %d\n", + config->media_cfg.jb_max); + pj_strcat2(&cfg, line); + } + + /* Sound device latency */ + if (config->capture_lat != PJMEDIA_SND_DEFAULT_REC_LATENCY) { + pj_ansi_sprintf(line, "--capture-lat %d\n", config->capture_lat); + pj_strcat2(&cfg, line); + } + if (config->playback_lat != PJMEDIA_SND_DEFAULT_PLAY_LATENCY) { + pj_ansi_sprintf(line, "--playback-lat %d\n", config->playback_lat); + pj_strcat2(&cfg, line); + } + + /* Media clock rate. */ + if (config->media_cfg.clock_rate != PJSUA_DEFAULT_CLOCK_RATE) { + pj_ansi_sprintf(line, "--clock-rate %d\n", + config->media_cfg.clock_rate); + pj_strcat2(&cfg, line); + } else { + pj_ansi_sprintf(line, "#using default --clock-rate %d\n", + config->media_cfg.clock_rate); + pj_strcat2(&cfg, line); + } + + if (config->media_cfg.snd_clock_rate && + config->media_cfg.snd_clock_rate != config->media_cfg.clock_rate) + { + pj_ansi_sprintf(line, "--snd-clock-rate %d\n", + config->media_cfg.snd_clock_rate); + pj_strcat2(&cfg, line); + } + + /* Stereo mode. */ + if (config->media_cfg.channel_count == 2) { + pj_ansi_sprintf(line, "--stereo\n"); + pj_strcat2(&cfg, line); + } + + /* quality */ + if (config->media_cfg.quality != PJSUA_DEFAULT_CODEC_QUALITY) { + pj_ansi_sprintf(line, "--quality %d\n", + config->media_cfg.quality); + pj_strcat2(&cfg, line); + } else { + pj_ansi_sprintf(line, "#using default --quality %d\n", + config->media_cfg.quality); + pj_strcat2(&cfg, line); + } + + if (config->vid.vcapture_dev != PJMEDIA_VID_DEFAULT_CAPTURE_DEV) { + pj_ansi_sprintf(line, "--vcapture-dev %d\n", config->vid.vcapture_dev); + pj_strcat2(&cfg, line); + } + if (config->vid.vrender_dev != PJMEDIA_VID_DEFAULT_RENDER_DEV) { + pj_ansi_sprintf(line, "--vrender-dev %d\n", config->vid.vrender_dev); + pj_strcat2(&cfg, line); + } + for (i=0; iavi_cnt; ++i) { + pj_ansi_sprintf(line, "--play-avi %s\n", config->avi[i].path.ptr); + pj_strcat2(&cfg, line); + } + if (config->avi_auto_play) { + pj_ansi_sprintf(line, "--auto-play-avi\n"); + pj_strcat2(&cfg, line); + } + + /* ptime */ + if (config->media_cfg.ptime) { + pj_ansi_sprintf(line, "--ptime %d\n", + config->media_cfg.ptime); + pj_strcat2(&cfg, line); + } + + /* no-vad */ + if (config->media_cfg.no_vad) { + pj_strcat2(&cfg, "--no-vad\n"); + } + + /* ec-tail */ + if (config->media_cfg.ec_tail_len != PJSUA_DEFAULT_EC_TAIL_LEN) { + pj_ansi_sprintf(line, "--ec-tail %d\n", + config->media_cfg.ec_tail_len); + pj_strcat2(&cfg, line); + } else { + pj_ansi_sprintf(line, "#using default --ec-tail %d\n", + config->media_cfg.ec_tail_len); + pj_strcat2(&cfg, line); + } + + /* ec-opt */ + if (config->media_cfg.ec_options != 0) { + pj_ansi_sprintf(line, "--ec-opt %d\n", + config->media_cfg.ec_options); + pj_strcat2(&cfg, line); + } + + /* ilbc-mode */ + if (config->media_cfg.ilbc_mode != PJSUA_DEFAULT_ILBC_MODE) { + pj_ansi_sprintf(line, "--ilbc-mode %d\n", + config->media_cfg.ilbc_mode); + pj_strcat2(&cfg, line); + } else { + pj_ansi_sprintf(line, "#using default --ilbc-mode %d\n", + config->media_cfg.ilbc_mode); + pj_strcat2(&cfg, line); + } + + /* RTP drop */ + if (config->media_cfg.tx_drop_pct) { + pj_ansi_sprintf(line, "--tx-drop-pct %d\n", + config->media_cfg.tx_drop_pct); + pj_strcat2(&cfg, line); + + } + if (config->media_cfg.rx_drop_pct) { + pj_ansi_sprintf(line, "--rx-drop-pct %d\n", + config->media_cfg.rx_drop_pct); + pj_strcat2(&cfg, line); + + } + + /* Start RTP port. */ + pj_ansi_sprintf(line, "--rtp-port %d\n", + config->rtp_cfg.port); + pj_strcat2(&cfg, line); + + /* Disable codec */ + for (i=0; icodec_dis_cnt; ++i) { + pj_ansi_sprintf(line, "--dis-codec %s\n", + config->codec_dis[i].ptr); + pj_strcat2(&cfg, line); + } + /* Add codec. */ + for (i=0; icodec_cnt; ++i) { + pj_ansi_sprintf(line, "--add-codec %s\n", + config->codec_arg[i].ptr); + pj_strcat2(&cfg, line); + } + + pj_strcat2(&cfg, "\n#\n# User agent:\n#\n"); + + /* Auto-answer. */ + if (config->auto_answer != 0) { + pj_ansi_sprintf(line, "--auto-answer %d\n", + config->auto_answer); + pj_strcat2(&cfg, line); + } + + /* accept-redirect */ + if (config->redir_op != PJSIP_REDIRECT_ACCEPT_REPLACE) { + pj_ansi_sprintf(line, "--accept-redirect %d\n", + config->redir_op); + pj_strcat2(&cfg, line); + } + + /* Max calls. */ + pj_ansi_sprintf(line, "--max-calls %d\n", + config->cfg.max_calls); + pj_strcat2(&cfg, line); + + /* Uas-duration. */ + if (config->duration != NO_LIMIT_DURATION) { + pj_ansi_sprintf(line, "--duration %d\n", + config->duration); + pj_strcat2(&cfg, line); + } + + /* norefersub ? */ + if (config->no_refersub) { + pj_strcat2(&cfg, "--norefersub\n"); + } + + if (pjsip_use_compact_form) + { + pj_strcat2(&cfg, "--use-compact-form\n"); + } + + if (!config->cfg.force_lr) { + pj_strcat2(&cfg, "--no-force-lr\n"); + } + + pj_strcat2(&cfg, "\n#\n# Buddies:\n#\n"); + + /* Add buddies. */ + for (i=0; ibuddy_cnt; ++i) { + pj_ansi_sprintf(line, "--add-buddy %.*s\n", + (int)config->buddy_cfg[i].uri.slen, + config->buddy_cfg[i].uri.ptr); + pj_strcat2(&cfg, line); + } + + /* SIP extensions. */ + pj_strcat2(&cfg, "\n#\n# SIP extensions:\n#\n"); + /* 100rel extension */ + if (config->cfg.require_100rel) { + pj_strcat2(&cfg, "--use-100rel\n"); + } + /* Session Timer extension */ + if (config->cfg.use_timer) { + pj_ansi_sprintf(line, "--use-timer %d\n", + config->cfg.use_timer); + pj_strcat2(&cfg, line); + } + if (config->cfg.timer_setting.min_se != 90) { + pj_ansi_sprintf(line, "--timer-min-se %d\n", + config->cfg.timer_setting.min_se); + pj_strcat2(&cfg, line); + } + if (config->cfg.timer_setting.sess_expires != PJSIP_SESS_TIMER_DEF_SE) { + pj_ansi_sprintf(line, "--timer-se %d\n", + config->cfg.timer_setting.sess_expires); + pj_strcat2(&cfg, line); + } + + *(cfg.ptr + cfg.slen) = '\0'; + return cfg.slen; +} + +#ifdef PJSUA_HAS_VIDEO +PJ_DEF(void) app_config_init_video(pjsua_acc_config *acc_cfg) +{ + acc_cfg->vid_in_auto_show = app_config.vid.in_auto_show; + acc_cfg->vid_out_auto_transmit = app_config.vid.out_auto_transmit; + /* Note that normally GUI application will prefer a borderless + * window. + */ + acc_cfg->vid_wnd_flags = PJMEDIA_VID_DEV_WND_BORDER | + PJMEDIA_VID_DEV_WND_RESIZABLE; + acc_cfg->vid_cap_dev = app_config.vid.vcapture_dev; + acc_cfg->vid_rend_dev = app_config.vid.vrender_dev; + + if (app_config.avi_auto_play && + app_config.avi_def_idx != PJSUA_INVALID_ID && + app_config.avi[app_config.avi_def_idx].dev_id != PJMEDIA_VID_INVALID_DEV) + { + acc_cfg->vid_cap_dev = app_config.avi[app_config.avi_def_idx].dev_id; + } +} +#else +PJ_DEF(void) app_config_init_video(pjsua_acc_config *acc_cfg) +{ + PJ_UNUSED_ARG(acc_cfg); +} +#endif + +#ifdef HAVE_MULTIPART_TEST + /* + * Enable multipart in msg_data and add a dummy body into the + * multipart bodies. + */ + PJ_DEF(void) add_multipart(pjsua_msg_data *msg_data) + { + static pjsip_multipart_part *alt_part; + + if (!alt_part) { + pj_str_t type, subtype, content; + + alt_part = pjsip_multipart_create_part(app_config.pool); + + type = pj_str("text"); + subtype = pj_str("plain"); + content = pj_str("Sample text body of a multipart bodies"); + alt_part->body = pjsip_msg_body_create(app_config.pool, &type, + &subtype, &content); + } + + msg_data->multipart_ctype.type = pj_str("multipart"); + msg_data->multipart_ctype.subtype = pj_str("mixed"); + pj_list_push_back(&msg_data->multipart_parts, alt_part); + } +#endif + +/* arrange windows. arg: + * -1: arrange all windows + * != -1: arrange only this window id + */ +PJ_DEF(void) arrange_window(pjsua_vid_win_id wid) +{ +#if PJSUA_HAS_VIDEO + pjmedia_coord pos; + int i, last; + + pos.x = 0; + pos.y = 10; + last = (wid == PJSUA_INVALID_ID) ? PJSUA_MAX_VID_WINS : wid; + + for (i=0; idir == PJMEDIA_DIR_CAPTURE_RENDER) { + dirname = "capture, render"; + } else if (vdi->dir == PJMEDIA_DIR_CAPTURE) { + dirname = "capture"; + } else { + dirname = "render"; + } + + + capnames[0] = '\0'; + for (i=0; icaps & (1 << i)) { + const char *capname = pjmedia_vid_dev_cap_name(1 << i, NULL); + if (capname) { + if (*capnames) + strcat(capnames, ", "); + strncat(capnames, capname, + sizeof(capnames)-strlen(capnames)-1); + } + } + } + + formats[0] = '\0'; + for (i=0; ifmt_cnt; ++i) { + const pjmedia_video_format_info *vfi = + pjmedia_get_video_format_info(NULL, vdi->fmt[i].id); + if (vfi) { + if (*formats) + strcat(formats, ", "); + strncat(formats, vfi->name, sizeof(formats)-strlen(formats)-1); + } + } + + PJ_LOG(3,(THIS_FILE, "%3d %s [%s][%s] %s", id, vdi->name, vdi->driver, + dirname, title)); + PJ_LOG(3,(THIS_FILE, " Supported capabilities: %s", capnames)); + PJ_LOG(3,(THIS_FILE, " Supported formats: %s", formats)); +} + +PJ_DEF(void) vid_list_devs() +{ + unsigned i, count; + pjmedia_vid_dev_info vdi; + pj_status_t status; + + PJ_LOG(3,(THIS_FILE, "Video device list:")); + count = pjsua_vid_dev_count(); + if (count == 0) { + PJ_LOG(3,(THIS_FILE, " - no device detected -")); + return; + } else { + PJ_LOG(3,(THIS_FILE, "%d device(s) detected:", count)); + } + + status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi); + if (status == PJ_SUCCESS) + vid_print_dev(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi, + "(default renderer device)"); + + status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi); + if (status == PJ_SUCCESS) + vid_print_dev(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi, + "(default capture device)"); + + for (i=0; ivid_in_auto_show, + acc_cfg->vid_out_auto_transmit, + acc_cfg->vid_cap_dev, + acc_cfg->vid_rend_dev)); +} + + +#endif + diff --git a/pjsip-apps/src/pjsua/pjsua_cmd.h b/pjsip-apps/src/pjsua/pjsua_cmd.h index fbc2a9fb..ea32e8aa 100644 --- a/pjsip-apps/src/pjsua/pjsua_cmd.h +++ b/pjsip-apps/src/pjsua/pjsua_cmd.h @@ -1,179 +1,180 @@ -/* $Id: pjsua_cmd.h 3553 2011-05-05 06:14:19Z nanang $ */ -/* - * 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_CMD_H__ -#define __PJSUA_CMD_H__ - -#include - -PJ_BEGIN_DECL - -#define current_acc pjsua_acc_get_default() - -#define NO_LIMIT_DURATION (int)0x7FFFFFFF -#define MAX_AVI 4 -#define NO_NB -2 - -typedef struct input_result -{ - int nb_result; - char *uri_result; -} input_result; - -/* Call specific data */ -typedef struct call_data -{ - pj_timer_entry timer; - pj_bool_t ringback_on; - pj_bool_t ring_on; -} call_data; - -/* Video settings */ -typedef struct app_vid -{ - unsigned vid_cnt; - int vcapture_dev; - int vrender_dev; - pj_bool_t in_auto_show; - pj_bool_t out_auto_transmit; -} app_vid; - -/* Pjsua application data */ -typedef struct pjsua_app_config -{ - pjsua_config cfg; - pjsua_logging_config log_cfg; - pjsua_media_config media_cfg; - pj_bool_t no_refersub; - pj_bool_t ipv6; - pj_bool_t enable_qos; - pj_bool_t no_tcp; - pj_bool_t no_udp; - pj_bool_t use_tls; - pjsua_transport_config udp_cfg; - pjsua_transport_config rtp_cfg; - pjsip_redirect_op redir_op; - - unsigned acc_cnt; - pjsua_acc_config acc_cfg[PJSUA_MAX_ACC]; - - unsigned buddy_cnt; - pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES]; - - call_data call_data[PJSUA_MAX_CALLS]; - - pj_pool_t *pool; - /* Compatibility with older pjsua */ - - unsigned codec_cnt; - pj_str_t codec_arg[32]; - unsigned codec_dis_cnt; - pj_str_t codec_dis[32]; - pj_bool_t null_audio; - unsigned wav_count; - pj_str_t wav_files[32]; - unsigned tone_count; - pjmedia_tone_desc tones[32]; - pjsua_conf_port_id tone_slots[32]; - pjsua_player_id wav_id; - pjsua_conf_port_id wav_port; - pj_bool_t auto_play; - pj_bool_t auto_play_hangup; - pj_timer_entry auto_hangup_timer; - pj_bool_t auto_loop; - pj_bool_t auto_conf; - pj_str_t rec_file; - pj_bool_t auto_rec; - pjsua_recorder_id rec_id; - pjsua_conf_port_id rec_port; - unsigned auto_answer; - unsigned duration; - -#ifdef STEREO_DEMO - pjmedia_snd_port *snd; - pjmedia_port *sc, *sc_ch1; - pjsua_conf_port_id sc_ch1_slot; -#endif - - float mic_level, - speaker_level; - - int capture_dev, playback_dev; - unsigned capture_lat, playback_lat; - - pj_bool_t no_tones; - int ringback_slot; - int ringback_cnt; - pjmedia_port *ringback_port; - int ring_slot; - int ring_cnt; - pjmedia_port *ring_port; - - app_vid vid; - unsigned aud_cnt; - - /* AVI to play */ - unsigned avi_cnt; - struct { - pj_str_t path; - pjmedia_vid_dev_index dev_id; - pjsua_conf_port_id slot; - } avi[MAX_AVI]; - pj_bool_t avi_auto_play; - int avi_def_idx; - - /* CLI setting */ - pj_bool_t use_cli; - unsigned cli_telnet_port; -} pjsua_app_config; - -/** Extern variable declaration **/ -extern pjsua_call_id current_call; -extern pjsua_app_config app_config; -extern int stdout_refresh; -extern pj_bool_t stdout_refresh_quit; -extern pjsua_call_setting call_opt; -extern pjsua_msg_data msg_data; - -PJ_DECL(int) my_atoi(const char *cs); -PJ_DECL(pj_bool_t) find_next_call(); -PJ_DECL(pj_bool_t) find_prev_call(); -PJ_DECL(void) send_request(char *cstr_method, const pj_str_t *dst_uri); -PJ_DECL(void) log_call_dump(int call_id); -PJ_DECL(int) write_settings(pjsua_app_config *cfg, char *buf, pj_size_t max); -PJ_DECL(void) app_config_init_video(pjsua_acc_config *acc_cfg); -PJ_DECL(void) arrange_window(pjsua_vid_win_id wid); - -#if PJSUA_HAS_VIDEO -PJ_DECL(void) vid_print_dev(int id, const pjmedia_vid_dev_info *vdi, const char *title); -PJ_DECL(void) vid_list_devs(); -PJ_DECL(void) app_config_show_video(int acc_id, const pjsua_acc_config *acc_cfg); -#endif - -#ifdef HAVE_MULTIPART_TEST - /* - * Enable multipart in msg_data and add a dummy body into the - * multipart bodies. - */ - PJ_DECL(void) add_multipart(pjsua_msg_data *msg_data); -# define TEST_MULTIPART(msg_data) add_multipart(msg_data) -#else -# define TEST_MULTIPART(msg_data) -#endif - -#endif /* __PJSUA_CMD_H__ */ \ No newline at end of file +/* $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_CMD_H__ +#define __PJSUA_CMD_H__ + +#include + +PJ_BEGIN_DECL + +#define current_acc pjsua_acc_get_default() + +#define NO_LIMIT_DURATION (int)0x7FFFFFFF +#define MAX_AVI 4 +#define NO_NB -2 + +typedef struct input_result +{ + int nb_result; + char *uri_result; +} input_result; + +/* Call specific data */ +typedef struct call_data +{ + pj_timer_entry timer; + pj_bool_t ringback_on; + pj_bool_t ring_on; +} call_data; + +/* Video settings */ +typedef struct app_vid +{ + unsigned vid_cnt; + int vcapture_dev; + int vrender_dev; + pj_bool_t in_auto_show; + pj_bool_t out_auto_transmit; +} app_vid; + +/* Pjsua application data */ +typedef struct pjsua_app_config +{ + pjsua_config cfg; + pjsua_logging_config log_cfg; + pjsua_media_config media_cfg; + pj_bool_t no_refersub; + pj_bool_t ipv6; + pj_bool_t enable_qos; + pj_bool_t no_tcp; + pj_bool_t no_udp; + pj_bool_t use_tls; + pjsua_transport_config udp_cfg; + pjsua_transport_config rtp_cfg; + pjsip_redirect_op redir_op; + + unsigned acc_cnt; + pjsua_acc_config acc_cfg[PJSUA_MAX_ACC]; + + unsigned buddy_cnt; + pjsua_buddy_config buddy_cfg[PJSUA_MAX_BUDDIES]; + + call_data call_data[PJSUA_MAX_CALLS]; + + pj_pool_t *pool; + /* Compatibility with older pjsua */ + + unsigned codec_cnt; + pj_str_t codec_arg[32]; + unsigned codec_dis_cnt; + pj_str_t codec_dis[32]; + pj_bool_t null_audio; + unsigned wav_count; + pj_str_t wav_files[32]; + unsigned tone_count; + pjmedia_tone_desc tones[32]; + pjsua_conf_port_id tone_slots[32]; + pjsua_player_id wav_id; + pjsua_conf_port_id wav_port; + pj_bool_t auto_play; + pj_bool_t auto_play_hangup; + pj_timer_entry auto_hangup_timer; + pj_bool_t auto_loop; + pj_bool_t auto_conf; + pj_str_t rec_file; + pj_bool_t auto_rec; + pjsua_recorder_id rec_id; + pjsua_conf_port_id rec_port; + unsigned auto_answer; + unsigned duration; + +#ifdef STEREO_DEMO + pjmedia_snd_port *snd; + pjmedia_port *sc, *sc_ch1; + pjsua_conf_port_id sc_ch1_slot; +#endif + + float mic_level, + speaker_level; + + int capture_dev, playback_dev; + unsigned capture_lat, playback_lat; + + pj_bool_t no_tones; + int ringback_slot; + int ringback_cnt; + pjmedia_port *ringback_port; + int ring_slot; + int ring_cnt; + pjmedia_port *ring_port; + + app_vid vid; + unsigned aud_cnt; + + /* AVI to play */ + unsigned avi_cnt; + struct { + pj_str_t path; + pjmedia_vid_dev_index dev_id; + pjsua_conf_port_id slot; + } avi[MAX_AVI]; + pj_bool_t avi_auto_play; + int avi_def_idx; + + /* CLI setting */ + pj_bool_t use_cli; + unsigned cli_telnet_port; +} pjsua_app_config; + +/** Extern variable declaration **/ +extern pjsua_call_id current_call; +extern pjsua_app_config app_config; +extern int stdout_refresh; +extern pj_bool_t stdout_refresh_quit; +extern pjsua_call_setting call_opt; +extern pjsua_msg_data msg_data; + +PJ_DECL(int) my_atoi(const char *cs); +PJ_DECL(pj_bool_t) find_next_call(); +PJ_DECL(pj_bool_t) find_prev_call(); +PJ_DECL(void) send_request(char *cstr_method, const pj_str_t *dst_uri); +PJ_DECL(void) log_call_dump(int call_id); +PJ_DECL(int) write_settings(pjsua_app_config *cfg, char *buf, pj_size_t max); +PJ_DECL(void) app_config_init_video(pjsua_acc_config *acc_cfg); +PJ_DECL(void) arrange_window(pjsua_vid_win_id wid); + +#if PJSUA_HAS_VIDEO +PJ_DECL(void) vid_print_dev(int id, const pjmedia_vid_dev_info *vdi, const char *title); +PJ_DECL(void) vid_list_devs(); +PJ_DECL(void) app_config_show_video(int acc_id, const pjsua_acc_config *acc_cfg); +#endif + +#ifdef HAVE_MULTIPART_TEST + /* + * Enable multipart in msg_data and add a dummy body into the + * multipart bodies. + */ + PJ_DECL(void) add_multipart(pjsua_msg_data *msg_data); +# define TEST_MULTIPART(msg_data) add_multipart(msg_data) +#else +# define TEST_MULTIPART(msg_data) +#endif + +#endif /* __PJSUA_CMD_H__ */ + diff --git a/pjsip-apps/src/pjsua/pjsua_ui_cmd.c b/pjsip-apps/src/pjsua/pjsua_ui_cmd.c index 76da2c14..2091158b 100644 --- a/pjsip-apps/src/pjsua/pjsua_ui_cmd.c +++ b/pjsip-apps/src/pjsua/pjsua_ui_cmd.c @@ -1,1940 +1,1940 @@ -/* $Id: pjsua_ui_cmd.c 3664 2011-07-19 03:42:28Z nanang $ */ -/* - * 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 -#include "pjsua_cmd.h" - -#define THIS_FILE "pjsua_ui_cmd.c" - -static pj_bool_t cmd_echo; - -/* - * Print buddy list. - */ -static void print_buddy_list() -{ - pjsua_buddy_id ids[64]; - int i; - unsigned count = PJ_ARRAY_SIZE(ids); - - puts("Buddy list:"); - - pjsua_enum_buddies(ids, &count); - - if (count == 0) - puts(" -none-"); - else { - for (i=0; i<(int)count; ++i) { - pjsua_buddy_info info; - - if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS) - continue; - - printf(" [%2d] <%.*s> %.*s\n", - ids[i]+1, - (int)info.status_text.slen, - info.status_text.ptr, - (int)info.uri.slen, - info.uri.ptr); - } - } - puts(""); -} - -/* - * Input URL. - */ -static void ui_input_url(const char *title, char *buf, int len, - input_result *result) -{ - result->nb_result = NO_NB; - result->uri_result = NULL; - - print_buddy_list(); - - printf("Choices:\n" - " 0 For current dialog.\n" - " -1 All %d buddies in buddy list\n" - " [1 -%2d] Select from buddy list\n" - " URL An URL\n" - " Empty input (or 'q') to cancel\n" - , pjsua_get_buddy_count(), pjsua_get_buddy_count()); - printf("%s: ", title); - - fflush(stdout); - if (fgets(buf, len, stdin) == NULL) - return; - 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 (; inb_result = my_atoi(buf); - - if (result->nb_result >= 0 && - result->nb_result <= (int)pjsua_get_buddy_count()) - { - return; - } - if (result->nb_result == -1) - return; - - puts("Invalid input"); - 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; - } -} - -static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len) -{ - char *p; - - printf("%s (empty to cancel): ", title); fflush(stdout); - if (fgets(buf, len, stdin) == NULL) - return PJ_FALSE; - - /* Remove trailing newlines. */ - for (p=buf; ; ++p) { - if (*p=='\r' || *p=='\n') *p='\0'; - else if (!*p) break; - } - - if (!*buf) - return PJ_FALSE; - - return PJ_TRUE; -} - -/* - * Print account status. - */ -static void print_acc_status(int acc_id) -{ - char buf[80]; - pjsua_acc_info info; - - pjsua_acc_get_info(acc_id, &info); - - if (!info.has_registration) { - pj_ansi_snprintf(buf, sizeof(buf), "%.*s", - (int)info.status_text.slen, - info.status_text.ptr); - - } else { - pj_ansi_snprintf(buf, sizeof(buf), - "%d/%.*s (expires=%d)", - info.status, - (int)info.status_text.slen, - info.status_text.ptr, - info.expires); - - } - - printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '), - acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf); - printf(" Online status: %.*s\n", - (int)info.online_status_text.slen, - info.online_status_text.ptr); -} - -/* - * Show a bit of help. - */ -static void keystroke_help() -{ - pjsua_acc_id acc_ids[16]; - unsigned count = PJ_ARRAY_SIZE(acc_ids); - int i; - - printf(">>>>\n"); - - pjsua_enum_accs(acc_ids, &count); - - printf("Account list:\n"); - for (i=0; i<(int)count; ++i) - print_acc_status(acc_ids[i]); - - print_buddy_list(); - - //puts("Commands:"); - puts("+=============================================================================+"); - puts("| Call Commands: | Buddy, IM & Presence: | Account: |"); - puts("| | | |"); - puts("| m Make new call | +b Add new buddy .| +a Add new accnt |"); - puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |"); - puts("| a Answer call | i Send IM | !a Modify accnt. |"); - puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |"); - puts("| H Hold call | u Unsubscribe presence | ru Unregister |"); - puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|"); - puts("| U send UPDATE | T Set online status | < Cycle prev ac.|"); - puts("| ],[ Select next/prev call +--------------------------+-------------------+"); - puts("| x Xfer call | Media Commands: | Status & Config: |"); - puts("| X Xfer with Replaces | | |"); - puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |"); - puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |"); - puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |"); - puts("| | V Adjust audio Volume | f Save config |"); - puts("| S Send arbitrary REQUEST | Cp Codec priorities | |"); - puts("+-----------------------------------------------------------------------------+"); -#if PJSUA_HAS_VIDEO - puts("| Video: \"vid help\" for more info |"); - puts("+-----------------------------------------------------------------------------+"); -#endif - puts("| q QUIT L ReLoad sleep MS echo [0|1|txt] n: detect NAT type |"); - puts("+=============================================================================+"); - - i = pjsua_call_get_count(); - printf("You have %d active call%s\n", i, (i>1?"s":"")); - - if (current_call != PJSUA_INVALID_ID) { - pjsua_call_info ci; - if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS) - printf("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); - } -} - -/* Help screen for video */ -#if PJSUA_HAS_VIDEO -static void vid_show_help() -{ - pj_bool_t vid_enabled = (app_config.vid.vid_cnt > 0); - - puts("+=============================================================================+"); - puts("| Video commands: |"); - puts("| |"); - puts("| vid help Show this help screen |"); - puts("| vid enable|disable Enable or disable video in next offer/answer |"); - puts("| vid acc show Show current account video settings |"); - puts("| vid acc autorx on|off Automatically show incoming video on/off |"); - puts("| vid acc autotx on|off Automatically offer video on/off |"); - puts("| vid acc cap ID Set default capture device for current acc |"); - puts("| vid acc rend ID Set default renderer device for current acc |"); - puts("| vid call rx on|off N Enable/disable video RX for stream N in curr call |"); - puts("| vid call tx on|off N Enable/disable video TX for stream N in curr call |"); - puts("| vid call add Add video stream for current call |"); - puts("| vid call enable|disable N Enable/disable stream #N in current call |"); - puts("| vid call cap N ID Set capture dev ID for stream #N in current call |"); - puts("| vid dev list List all video devices |"); - puts("| vid dev refresh Refresh video device list |"); - puts("| vid dev prev on|off ID Enable/disable preview for specified device ID |"); - puts("| vid codec list List video codecs |"); - puts("| vid codec prio ID PRIO Set codec ID priority to PRIO |"); - puts("| vid codec fps ID NUM DEN Set codec ID framerate to (NUM/DEN) fps |"); - puts("| vid codec bw ID AVG MAX Set codec ID bitrate to AVG & MAX kbps |"); - puts("| vid codec size ID W H Set codec ID size/resolution to W x H |"); - puts("| vid win list List all active video windows |"); - puts("| vid win arrange Auto arrange windows |"); - puts("| vid win show|hide ID Show/hide the specified video window ID |"); - puts("| vid win move ID X Y Move window ID to position X,Y |"); - puts("| vid win resize ID w h Resize window ID to the specified width, height |"); - puts("+=============================================================================+"); - printf("| Video will be %s in the next offer/answer %s |\n", - (vid_enabled? "enabled" : "disabled"), (vid_enabled? " " : "")); - puts("+=============================================================================+"); -} - -static void vid_handle_menu(char *menuin) -{ - char *argv[8]; - int argc = 0; - - /* Tokenize */ - argv[argc] = strtok(menuin, " \t\r\n"); - while (argv[argc] && *argv[argc]) { - argc++; - argv[argc] = strtok(NULL, " \t\r\n"); - } - - if (argc == 1 || strcmp(argv[1], "help")==0) { - vid_show_help(); - } else if (argc == 2 && (strcmp(argv[1], "enable")==0 || - strcmp(argv[1], "disable")==0)) - { - pj_bool_t enabled = (strcmp(argv[1], "enable")==0); - app_config.vid.vid_cnt = (enabled ? 1 : 0); - PJ_LOG(3,(THIS_FILE, "Video will be %s in next offer/answer", - (enabled?"enabled":"disabled"))); - } else if (strcmp(argv[1], "acc")==0) { - pjsua_acc_config acc_cfg; - pj_bool_t changed = PJ_FALSE; - - pjsua_acc_get_config(current_acc, &acc_cfg); - - if (argc == 3 && strcmp(argv[2], "show")==0) { - app_config_show_video(current_acc, &acc_cfg); - } else if (argc == 4 && strcmp(argv[2], "autorx")==0) { - int on = (strcmp(argv[3], "on")==0); - acc_cfg.vid_in_auto_show = on; - changed = PJ_TRUE; - } else if (argc == 4 && strcmp(argv[2], "autotx")==0) { - int on = (strcmp(argv[3], "on")==0); - acc_cfg.vid_out_auto_transmit = on; - changed = PJ_TRUE; - } else if (argc == 4 && strcmp(argv[2], "cap")==0) { - int dev = atoi(argv[3]); - acc_cfg.vid_cap_dev = dev; - changed = PJ_TRUE; - } else if (argc == 4 && strcmp(argv[2], "rend")==0) { - int dev = atoi(argv[3]); - acc_cfg.vid_rend_dev = dev; - changed = PJ_TRUE; - } else { - goto on_error; - } - - if (changed) { - 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)); - } - - } else if (strcmp(argv[1], "call")==0) { - pjsua_call_vid_strm_op_param param; - pj_status_t status = PJ_SUCCESS; - - pjsua_call_vid_strm_op_param_default(¶m); - - if (argc == 5 && strcmp(argv[2], "rx")==0) { - pjsua_stream_info si; - pj_bool_t on = (strcmp(argv[3], "on") == 0); - - param.med_idx = atoi(argv[4]); - 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; - } - - 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); - } - else if (argc == 5 && strcmp(argv[2], "tx")==0) { - pj_bool_t on = (strcmp(argv[3], "on") == 0); - pjsua_call_vid_strm_op op = on? PJSUA_CALL_VID_STRM_START_TRANSMIT : - PJSUA_CALL_VID_STRM_STOP_TRANSMIT; - - param.med_idx = atoi(argv[4]); - - status = pjsua_call_set_vid_strm(current_call, op, ¶m); - } - else if (argc == 3 && strcmp(argv[2], "add")==0) { - status = pjsua_call_set_vid_strm(current_call, - PJSUA_CALL_VID_STRM_ADD, NULL); - } - else if (argc >= 3 && - (strcmp(argv[2], "disable")==0 || strcmp(argv[2], "enable")==0)) - { - pj_bool_t enable = (strcmp(argv[2], "enable") == 0); - pjsua_call_vid_strm_op op = enable? PJSUA_CALL_VID_STRM_CHANGE_DIR : - PJSUA_CALL_VID_STRM_REMOVE; - - param.med_idx = argc >= 4? atoi(argv[3]) : -1; - param.dir = PJMEDIA_DIR_ENCODING_DECODING; - status = pjsua_call_set_vid_strm(current_call, op, ¶m); - } - else if (argc >= 3 && strcmp(argv[2], "cap")==0) { - param.med_idx = argc >= 4? atoi(argv[3]) : -1; - param.cap_dev = argc >= 5? atoi(argv[4]) : PJMEDIA_VID_DEFAULT_CAPTURE_DEV; - status = pjsua_call_set_vid_strm(current_call, - PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV, - ¶m); - } else - goto on_error; - - if (status != PJ_SUCCESS) { - PJ_PERROR(1,(THIS_FILE, status, "Error modifying video stream")); - } - - } else if (argc >= 3 && strcmp(argv[1], "dev")==0) { - if (strcmp(argv[2], "list")==0) { - vid_list_devs(); - } else if (strcmp(argv[2], "refresh")==0) { - pjmedia_vid_dev_refresh(); - } else if (strcmp(argv[2], "prev")==0) { - if (argc != 5) { - goto on_error; - } else { - pj_bool_t on = (strcmp(argv[3], "on") == 0); - int dev_id = atoi(argv[4]); - 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); - } - } - } - } else - goto on_error; - } else if (strcmp(argv[1], "win")==0) { - pj_status_t status = PJ_SUCCESS; - - if (argc==3 && strcmp(argv[2], "list")==0) { - 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)); - } - } else if (argc==4 && (strcmp(argv[2], "show")==0 || - strcmp(argv[2], "hide")==0)) - { - pj_bool_t show = (strcmp(argv[2], "show")==0); - pjsua_vid_win_id wid = atoi(argv[3]); - status = pjsua_vid_win_set_show(wid, show); - } else if (argc==6 && strcmp(argv[2], "move")==0) { - pjsua_vid_win_id wid = atoi(argv[3]); - pjmedia_coord pos; - - pos.x = atoi(argv[4]); - pos.y = atoi(argv[5]); - status = pjsua_vid_win_set_pos(wid, &pos); - } else if (argc==6 && strcmp(argv[2], "resize")==0) { - pjsua_vid_win_id wid = atoi(argv[3]); - pjmedia_rect_size size; - - size.w = atoi(argv[4]); - size.h = atoi(argv[5]); - status = pjsua_vid_win_set_size(wid, &size); - } else if (argc==3 && strcmp(argv[2], "arrange")==0) { - arrange_window(PJSUA_INVALID_ID); - } else - goto on_error; - - if (status != PJ_SUCCESS) { - PJ_PERROR(1,(THIS_FILE, status, "Window operation error")); - } - - } else if (strcmp(argv[1], "codec")==0) { - pjsua_codec_info ci[PJMEDIA_CODEC_MGR_MAX_CODECS]; - unsigned count = PJ_ARRAY_SIZE(ci); - pj_status_t status; - - if (argc==3 && strcmp(argv[2], "list")==0) { - 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)); - } - } - } else if (argc==5 && strcmp(argv[2], "prio")==0) { - pj_str_t cid; - int prio; - cid = pj_str(argv[3]); - prio = atoi(argv[4]); - status = pjsua_vid_codec_set_priority(&cid, (pj_uint8_t)prio); - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Set codec priority error")); - } else if (argc==6 && strcmp(argv[2], "fps")==0) { - pjmedia_vid_codec_param cp; - pj_str_t cid; - int M, N; - cid = pj_str(argv[3]); - M = atoi(argv[4]); - N = atoi(argv[5]); - status = pjsua_vid_codec_get_param(&cid, &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(&cid, &cp); - } - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Set codec framerate error")); - } else if (argc==6 && strcmp(argv[2], "bw")==0) { - pjmedia_vid_codec_param cp; - pj_str_t cid; - int M, N; - cid = pj_str(argv[3]); - M = atoi(argv[4]); - N = atoi(argv[5]); - status = pjsua_vid_codec_get_param(&cid, &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(&cid, &cp); - } - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error")); - } else if (argc==6 && strcmp(argv[2], "size")==0) { - pjmedia_vid_codec_param cp; - pj_str_t cid; - int M, N; - cid = pj_str(argv[3]); - M = atoi(argv[4]); - N = atoi(argv[5]); - status = pjsua_vid_codec_get_param(&cid, &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(&cid, &cp); - } - if (status != PJ_SUCCESS) - PJ_PERROR(1,(THIS_FILE, status, "Set codec size error")); - } else - goto on_error; - } else - goto on_error; - - return; - -on_error: - PJ_LOG(1,(THIS_FILE, "Invalid command, use 'vid help'")); -} - -#endif /* PJSUA_HAS_VIDEO */ - -/** UI Command **/ -static void ui_make_new_call() -{ - char buf[128]; - pjsua_msg_data msg_data; - input_result result; - pj_str_t tmp; - - 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 == -1 || result.nb_result == 0) { - puts("You can't do that with make call!"); - return; - } else { - pjsua_buddy_info binfo; - pjsua_buddy_get_info(result.nb_result-1, &binfo); - tmp.ptr = buf; - pj_strncpy(&tmp, &binfo.uri, sizeof(buf)); - } - - } 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); -} - -static void ui_make_multi_call() -{ - char menuin[32]; - int count; - char buf[128]; - input_result result; - pj_str_t tmp; - int i; - - printf("(You currently have %d calls)\n", pjsua_call_get_count()); - - if (!simple_input("Number of calls", menuin, sizeof(menuin))) - return; - - count = my_atoi(menuin); - if (count < 1) - return; - - ui_input_url("Make call", buf, sizeof(buf), &result); - if (result.nb_result != NO_NB) { - pjsua_buddy_info binfo; - if (result.nb_result == -1 || result.nb_result == 0) { - puts("You can't do that with make call!"); - return; - } - pjsua_buddy_get_info(result.nb_result-1, &binfo); - tmp.ptr = buf; - pj_strncpy(&tmp, &binfo.uri, sizeof(buf)); - } else { - tmp = pj_str(result.uri_result); - } - - for (i=0; i= PJSIP_INV_STATE_CONNECTING) - { - puts("No pending incoming call"); - fflush(stdout); - return; - - } else { - int st_code; - char contact[120]; - pj_str_t hname = { "Contact", 7 }; - pj_str_t hvalue; - pjsip_generic_string_hdr hcontact; - - if (!simple_input("Answer with code (100-699)", buf, sizeof(buf))) - return; - - st_code = my_atoi(buf); - if (st_code < 100) - return; - - pjsua_msg_data_init(&msg_data); - - if (st_code/100 == 3) { - if (!simple_input("Enter URL to be put in Contact", - contact, sizeof(contact))) - return; - 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 == -1) { - puts("Call has been disconnected"); - fflush(stdout); - return; - } - - pjsua_call_answer2(current_call, &call_opt, st_code, NULL, &msg_data); - } -} - -static void ui_hangup_call(char menuin[]) -{ - if (current_call == -1) { - puts("No current call"); - fflush(stdout); - return; - - } else if (menuin[1] == 'a') { - /* Hangup all calls */ - pjsua_call_hangup_all(); - } else { - /* Hangup current calls */ - pjsua_call_hangup(current_call, 0, NULL, NULL); - } -} - -static void ui_cycle_dialog(char menuin[]) -{ - if (menuin[0] == ']') { - find_next_call(); - - } else { - find_prev_call(); - } - - if (current_call != -1) { - 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")); - } -} - -static void ui_cycle_account() -{ - int i; - char buf[128]; - - if (!simple_input("Enter account ID to select", buf, sizeof(buf))) - return; - - i = my_atoi(buf); - 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)); - } -} - -static void ui_add_buddy() -{ - char buf[128]; - pjsua_buddy_config buddy_cfg; - pjsua_buddy_id buddy_id; - pj_status_t status; - - if (!simple_input("Enter buddy's URI:", buf, sizeof(buf))) - return; - - if (pjsua_verify_url(buf) != PJ_SUCCESS) { - printf("Invalid URI '%s'\n", buf); - return; - } - - pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config)); - - buddy_cfg.uri = pj_str(buf); - buddy_cfg.subscribe = PJ_TRUE; - - status = pjsua_buddy_add(&buddy_cfg, &buddy_id); - if (status == PJ_SUCCESS) { - printf("New buddy '%s' added at index %d\n", - buf, buddy_id+1); - } -} - -static void ui_add_account(pjsua_transport_config *rtp_cfg) -{ - char id[80], registrar[80], realm[80], uname[80], passwd[30]; - pjsua_acc_config acc_cfg; - pj_status_t status; - - if (!simple_input("Your SIP URL:", id, sizeof(id))) - return; - if (!simple_input("URL of the registrar:", registrar, sizeof(registrar))) - return; - if (!simple_input("Auth Realm:", realm, sizeof(realm))) - return; - if (!simple_input("Auth Username:", uname, sizeof(uname))) - return; - if (!simple_input("Auth Password:", passwd, sizeof(passwd))) - return; - - pjsua_acc_config_default(&acc_cfg); - acc_cfg.id = pj_str(id); - acc_cfg.reg_uri = pj_str(registrar); - acc_cfg.cred_count = 1; - acc_cfg.cred_info[0].scheme = pj_str("Digest"); - acc_cfg.cred_info[0].realm = pj_str(realm); - acc_cfg.cred_info[0].username = pj_str(uname); - acc_cfg.cred_info[0].data_type = 0; - acc_cfg.cred_info[0].data = pj_str(passwd); - - acc_cfg.rtp_cfg = *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); - } -} - -static void ui_delete_buddy() -{ - char buf[128]; - int i; - - if (!simple_input("Enter buddy ID to delete", buf, sizeof(buf))) - return; - - i = my_atoi(buf) - 1; - - if (!pjsua_buddy_is_valid(i)) { - printf("Invalid buddy id %d\n", i); - } else { - pjsua_buddy_del(i); - printf("Buddy %d deleted\n", i); - } -} - -static void ui_delete_account() -{ - char buf[128]; - int i; - - if (!simple_input("Enter account ID to delete", buf, sizeof(buf))) - return; - - i = my_atoi(buf); - - if (!pjsua_acc_is_valid(i)) { - printf("Invalid account id %d\n", i); - } else { - pjsua_acc_del(i); - printf("Account %d deleted\n", i); - } -} - -static void ui_call_hold() -{ - if (current_call != -1) { - pjsua_call_set_hold(current_call, NULL); - } else { - PJ_LOG(3,(THIS_FILE, "No current call")); - } -} - -static void ui_call_reinvite() -{ - call_opt.flag |= PJSUA_CALL_UNHOLD; - pjsua_call_reinvite2(current_call, &call_opt, NULL); -} - -static void ui_send_update() -{ - if (current_call != -1) { - call_opt.flag |= PJSUA_CALL_UNHOLD; - pjsua_call_update2(current_call, &call_opt, NULL); - } else { - PJ_LOG(3,(THIS_FILE, "No current call")); - } -} - -/* - * Change codec priorities. - */ -static void ui_manage_codec_prio() -{ - pjsua_codec_info c[32]; - unsigned i, count = PJ_ARRAY_SIZE(c); - char input[32]; - char *codec, *prio; - pj_str_t id; - int new_prio; - pj_status_t status; - - printf("List of audio codecs:\n"); - pjsua_enum_codecs(c, &count); - for (i=0; i PJMEDIA_CODEC_PRIO_HIGHEST) - new_prio = PJMEDIA_CODEC_PRIO_HIGHEST; - - status = pjsua_codec_set_priority(pj_cstr(&id, codec), - (pj_uint8_t)new_prio); -#if PJSUA_HAS_VIDEO - if (status != PJ_SUCCESS) { - status = pjsua_vid_codec_set_priority(pj_cstr(&id, codec), - (pj_uint8_t)new_prio); - } -#endif - if (status != PJ_SUCCESS) - pjsua_perror(THIS_FILE, "Error setting codec priority", status); -} - -static void ui_call_transfer(pj_bool_t no_refersub) -{ - if (current_call == -1) { - PJ_LOG(3,(THIS_FILE, "No current call")); - } else { - int call = current_call; - char buf[128]; - 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; - input_result result; - pjsua_msg_data msg_data; - - pjsua_call_get_info(current_call, &ci); - printf("Transfering current call [%d] %.*s\n", current_call, - (int)ci.remote_info.slen, ci.remote_info.ptr); - - ui_input_url("Transfer to URL", buf, sizeof(buf), &result); - - /* Check if call is still there. */ - - if (call != current_call) { - puts("Call has been disconnected"); - return; - } - - pjsua_msg_data_init(&msg_data); - if (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) - puts("You can't do that with transfer call!"); - 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); - } - } -} - -static void ui_call_transfer_replaces(pj_bool_t no_refersub) -{ - 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_call_info ci; - pjsua_msg_data msg_data; - char buf[128]; - unsigned i, count; - - count = PJ_ARRAY_SIZE(ids); - pjsua_enum_calls(ids, &count); - - if (count <= 1) { - puts("There are no other calls"); - return; - } - - pjsua_call_get_info(current_call, &ci); - printf("Transfer call [%d] %.*s to one of the following:\n", - current_call, - (int)ci.remote_info.slen, ci.remote_info.ptr); - - for (i=0; i= PJSUA_MAX_CALLS) { - puts("Invalid destination call number"); - return; - } - if (!pjsua_call_is_active(dst_call)) { - puts("Invalid destination call number"); - return; - } - - pjsua_msg_data_init(&msg_data); - if (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); - } -} - -static void ui_send_dtmf_2833() -{ - if (current_call == -1) { - 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 { - pj_str_t digits; - int call = current_call; - pj_status_t status; - char buf[128]; - - if (!simple_input("DTMF strings to send (0-9*#A-B)", buf, - sizeof(buf))) - { - return; - } - - if (call != current_call) { - puts("Call has been disconnected"); - return; - } - - digits = pj_str(buf); - status = pjsua_call_dial_dtmf(current_call, &digits); - if (status != PJ_SUCCESS) { - pjsua_perror(THIS_FILE, "Unable to send DTMF", status); - } else { - puts("DTMF digits enqueued for transmission"); - } - } -} - -static void ui_send_dtmf_info() -{ - if (current_call == -1) { - PJ_LOG(3,(THIS_FILE, "No current call")); - } else { - const pj_str_t SIP_INFO = pj_str("INFO"); - pj_str_t digits; - int call = current_call; - int i; - pj_status_t status; - char buf[128]; - - if (!simple_input("DTMF strings to send (0-9*#A-B)", buf, - sizeof(buf))) - { - return; - } - - if (call != current_call) { - puts("Call has been disconnected"); - return; - } - - digits = pj_str(buf); - for (i=0; i= OPT_MAX) { - puts("Invalid selection"); - return; - } - - 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); -} - -/* - * List the ports in conference bridge - */ -static void ui_conf_list() -{ - unsigned i, count; - pjsua_conf_port_id id[PJSUA_MAX_CALLS]; - - printf("Conference ports:\n"); - - count = PJ_ARRAY_SIZE(id); - pjsua_enum_conf_ports(id, &count); - - for (i=0; islen) { - pjsua_call_make_call( current_acc, uri_to_call, &call_opt, - NULL, NULL, NULL); - } - - keystroke_help(current_call); - - for (;;) { - - printf(">>> "); - fflush(stdout); - - if (fgets(menuin, sizeof(menuin), stdin) == NULL) { - /* - * Be friendly to users who redirect commands into - * program, when file ends, resume with kbd. - * If exit is desired end script with q for quit - */ - /* Reopen stdin/stdout/stderr to /dev/console */ -#if defined(PJ_WIN32) && PJ_WIN32!=0 - if (freopen ("CONIN$", "r", stdin) == NULL) { -#else - if (1) { -#endif - puts("Cannot switch back to console from file redirection"); - menuin[0] = 'q'; - menuin[1] = '\0'; - } else { - puts("Switched back to console from file redirection"); - continue; - } - } - - if (cmd_echo) { - printf("%s", menuin); - } - - /* Update call setting */ - pjsua_call_setting_default(&call_opt); - call_opt.aud_cnt = app_config.aud_cnt; - call_opt.vid_cnt = app_config.vid.vid_cnt; - - switch (menuin[0]) { - - case 'm': - /* Make call! : */ - ui_make_new_call(); - break; - - case 'M': - /* Make multiple calls! : */ - ui_make_multi_call(); - break; - - case 'n': - ui_detect_nat_type(); - break; - - case 'i': - /* Send instant messaeg */ - ui_send_instant_message(); - break; - - case 'a': - ui_answer_call(); - break; - - case 'h': - ui_hangup_call(menuin); - break; - - case ']': - case '[': - /* - * Cycle next/prev dialog. - */ - ui_cycle_dialog(menuin); - break; - - case '>': - case '<': - ui_cycle_account(); - break; - - case '+': - if (menuin[1] == 'b') { - ui_add_buddy(); - } else if (menuin[1] == 'a') { - ui_add_account(&app_config.rtp_cfg); - } else { - printf("Invalid input %s\n", menuin); - } - break; - - case '-': - if (menuin[1] == 'b') { - ui_delete_buddy(); - } else if (menuin[1] == 'a') { - ui_delete_account(); - } else { - printf("Invalid input %s\n", menuin); - } - break; - - case 'H': - /* - * Hold call. - */ - ui_call_hold(); - break; - - case 'v': -#if PJSUA_HAS_VIDEO - if (menuin[1]=='i' && menuin[2]=='d' && menuin[3]==' ') { - vid_handle_menu(menuin); - } else -#endif - if (current_call != -1) { - /* - * re-INVITE - */ - ui_call_reinvite(); - } else { - PJ_LOG(3,(THIS_FILE, "No current call")); - } - break; - - case 'U': - /* - * Send UPDATE - */ - ui_send_update(); - break; - - case 'C': - if (menuin[1] == 'p') { - ui_manage_codec_prio(); - } - break; - - case 'x': - /* - * Transfer call. - */ - ui_call_transfer(app_config.no_refersub); - break; - - case 'X': - /* - * Transfer call with replaces. - */ - ui_call_transfer_replaces(app_config.no_refersub); - break; - - case '#': - /* - * Send DTMF strings. - */ - ui_send_dtmf_2833(); - break; - - case '*': - /* Send DTMF with INFO */ - ui_send_dtmf_info(); - break; - - case 'S': - /* - * Send arbitrary request - */ - ui_send_arbitrary_request(); - break; - - case 'e': - ui_echo(menuin); - break; - - case 's': - ui_sleep(menuin); - break; - /* Continue below */ - - case 'u': - /* - * Subscribe/unsubscribe presence. - */ - ui_subscribe(menuin); - break; - - case 'r': - ui_register(menuin); - break; - - case 't': - ui_toggle_state(); - break; - - case 'T': - ui_change_online_status(); - break; - - case 'c': - switch (menuin[1]) { - case 'l': - ui_conf_list(); - break; - case 'c': - case 'd': - ui_conf_connect(menuin); - break; - } - break; - - case 'V': - /* Adjust audio volume */ - ui_adjust_volume(); - break; - - case 'd': - if (menuin[1] == 'c') { - ui_dump_configuration(); - } else if (menuin[1] == 'q') { - ui_dump_call_quality(); - } else { - ui_app_dump(menuin[1]=='d'); - } - break; - - case 'f': - if (simple_input("Enter output filename", buf, sizeof(buf))) { - ui_write_settings(); - } - break; - - case 'L': /* Restart */ - *app_restart = PJ_TRUE; - /* Continues below */ - - case 'q': - goto on_exit; - - case 'R': - ui_call_redirect(menuin); - break; - - default: - if (menuin[0] != '\n' && menuin[0] != '\r') { - printf("Invalid input %s", menuin); - } - keystroke_help(); - break; - } - } - -on_exit: - ; -} - +/* $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 +#include "pjsua_cmd.h" + +#define THIS_FILE "pjsua_ui_cmd.c" + +static pj_bool_t cmd_echo; + +/* + * Print buddy list. + */ +static void print_buddy_list() +{ + pjsua_buddy_id ids[64]; + int i; + unsigned count = PJ_ARRAY_SIZE(ids); + + puts("Buddy list:"); + + pjsua_enum_buddies(ids, &count); + + if (count == 0) + puts(" -none-"); + else { + for (i=0; i<(int)count; ++i) { + pjsua_buddy_info info; + + if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS) + continue; + + printf(" [%2d] <%.*s> %.*s\n", + ids[i]+1, + (int)info.status_text.slen, + info.status_text.ptr, + (int)info.uri.slen, + info.uri.ptr); + } + } + puts(""); +} + +/* + * Input URL. + */ +static void ui_input_url(const char *title, char *buf, int len, + input_result *result) +{ + result->nb_result = NO_NB; + result->uri_result = NULL; + + print_buddy_list(); + + printf("Choices:\n" + " 0 For current dialog.\n" + " -1 All %d buddies in buddy list\n" + " [1 -%2d] Select from buddy list\n" + " URL An URL\n" + " Empty input (or 'q') to cancel\n" + , pjsua_get_buddy_count(), pjsua_get_buddy_count()); + printf("%s: ", title); + + fflush(stdout); + if (fgets(buf, len, stdin) == NULL) + return; + 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 (; inb_result = my_atoi(buf); + + if (result->nb_result >= 0 && + result->nb_result <= (int)pjsua_get_buddy_count()) + { + return; + } + if (result->nb_result == -1) + return; + + puts("Invalid input"); + 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; + } +} + +static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len) +{ + char *p; + + printf("%s (empty to cancel): ", title); fflush(stdout); + if (fgets(buf, len, stdin) == NULL) + return PJ_FALSE; + + /* Remove trailing newlines. */ + for (p=buf; ; ++p) { + if (*p=='\r' || *p=='\n') *p='\0'; + else if (!*p) break; + } + + if (!*buf) + return PJ_FALSE; + + return PJ_TRUE; +} + +/* + * Print account status. + */ +static void print_acc_status(int acc_id) +{ + char buf[80]; + pjsua_acc_info info; + + pjsua_acc_get_info(acc_id, &info); + + if (!info.has_registration) { + pj_ansi_snprintf(buf, sizeof(buf), "%.*s", + (int)info.status_text.slen, + info.status_text.ptr); + + } else { + pj_ansi_snprintf(buf, sizeof(buf), + "%d/%.*s (expires=%d)", + info.status, + (int)info.status_text.slen, + info.status_text.ptr, + info.expires); + + } + + printf(" %c[%2d] %.*s: %s\n", (acc_id==current_acc?'*':' '), + acc_id, (int)info.acc_uri.slen, info.acc_uri.ptr, buf); + printf(" Online status: %.*s\n", + (int)info.online_status_text.slen, + info.online_status_text.ptr); +} + +/* + * Show a bit of help. + */ +static void keystroke_help() +{ + pjsua_acc_id acc_ids[16]; + unsigned count = PJ_ARRAY_SIZE(acc_ids); + int i; + + printf(">>>>\n"); + + pjsua_enum_accs(acc_ids, &count); + + printf("Account list:\n"); + for (i=0; i<(int)count; ++i) + print_acc_status(acc_ids[i]); + + print_buddy_list(); + + //puts("Commands:"); + puts("+=============================================================================+"); + puts("| Call Commands: | Buddy, IM & Presence: | Account: |"); + puts("| | | |"); + puts("| m Make new call | +b Add new buddy .| +a Add new accnt |"); + puts("| M Make multiple calls | -b Delete buddy | -a Delete accnt. |"); + puts("| a Answer call | i Send IM | !a Modify accnt. |"); + puts("| h Hangup call (ha=all) | s Subscribe presence | rr (Re-)register |"); + puts("| H Hold call | u Unsubscribe presence | ru Unregister |"); + puts("| v re-inVite (release hold) | t ToGgle Online status | > Cycle next ac.|"); + puts("| U send UPDATE | T Set online status | < Cycle prev ac.|"); + puts("| ],[ Select next/prev call +--------------------------+-------------------+"); + puts("| x Xfer call | Media Commands: | Status & Config: |"); + puts("| X Xfer with Replaces | | |"); + puts("| # Send RFC 2833 DTMF | cl List ports | d Dump status |"); + puts("| * Send DTMF with INFO | cc Connect port | dd Dump detailed |"); + puts("| dq Dump curr. call quality | cd Disconnect port | dc Dump config |"); + puts("| | V Adjust audio Volume | f Save config |"); + puts("| S Send arbitrary REQUEST | Cp Codec priorities | |"); + puts("+-----------------------------------------------------------------------------+"); +#if PJSUA_HAS_VIDEO + puts("| Video: \"vid help\" for more info |"); + puts("+-----------------------------------------------------------------------------+"); +#endif + puts("| q QUIT L ReLoad sleep MS echo [0|1|txt] n: detect NAT type |"); + puts("+=============================================================================+"); + + i = pjsua_call_get_count(); + printf("You have %d active call%s\n", i, (i>1?"s":"")); + + if (current_call != PJSUA_INVALID_ID) { + pjsua_call_info ci; + if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS) + printf("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); + } +} + +/* Help screen for video */ +#if PJSUA_HAS_VIDEO +static void vid_show_help() +{ + pj_bool_t vid_enabled = (app_config.vid.vid_cnt > 0); + + puts("+=============================================================================+"); + puts("| Video commands: |"); + puts("| |"); + puts("| vid help Show this help screen |"); + puts("| vid enable|disable Enable or disable video in next offer/answer |"); + puts("| vid acc show Show current account video settings |"); + puts("| vid acc autorx on|off Automatically show incoming video on/off |"); + puts("| vid acc autotx on|off Automatically offer video on/off |"); + puts("| vid acc cap ID Set default capture device for current acc |"); + puts("| vid acc rend ID Set default renderer device for current acc |"); + puts("| vid call rx on|off N Enable/disable video RX for stream N in curr call |"); + puts("| vid call tx on|off N Enable/disable video TX for stream N in curr call |"); + puts("| vid call add Add video stream for current call |"); + puts("| vid call enable|disable N Enable/disable stream #N in current call |"); + puts("| vid call cap N ID Set capture dev ID for stream #N in current call |"); + puts("| vid dev list List all video devices |"); + puts("| vid dev refresh Refresh video device list |"); + puts("| vid dev prev on|off ID Enable/disable preview for specified device ID |"); + puts("| vid codec list List video codecs |"); + puts("| vid codec prio ID PRIO Set codec ID priority to PRIO |"); + puts("| vid codec fps ID NUM DEN Set codec ID framerate to (NUM/DEN) fps |"); + puts("| vid codec bw ID AVG MAX Set codec ID bitrate to AVG & MAX kbps |"); + puts("| vid codec size ID W H Set codec ID size/resolution to W x H |"); + puts("| vid win list List all active video windows |"); + puts("| vid win arrange Auto arrange windows |"); + puts("| vid win show|hide ID Show/hide the specified video window ID |"); + puts("| vid win move ID X Y Move window ID to position X,Y |"); + puts("| vid win resize ID w h Resize window ID to the specified width, height |"); + puts("+=============================================================================+"); + printf("| Video will be %s in the next offer/answer %s |\n", + (vid_enabled? "enabled" : "disabled"), (vid_enabled? " " : "")); + puts("+=============================================================================+"); +} + +static void vid_handle_menu(char *menuin) +{ + char *argv[8]; + int argc = 0; + + /* Tokenize */ + argv[argc] = strtok(menuin, " \t\r\n"); + while (argv[argc] && *argv[argc]) { + argc++; + argv[argc] = strtok(NULL, " \t\r\n"); + } + + if (argc == 1 || strcmp(argv[1], "help")==0) { + vid_show_help(); + } else if (argc == 2 && (strcmp(argv[1], "enable")==0 || + strcmp(argv[1], "disable")==0)) + { + pj_bool_t enabled = (strcmp(argv[1], "enable")==0); + app_config.vid.vid_cnt = (enabled ? 1 : 0); + PJ_LOG(3,(THIS_FILE, "Video will be %s in next offer/answer", + (enabled?"enabled":"disabled"))); + } else if (strcmp(argv[1], "acc")==0) { + pjsua_acc_config acc_cfg; + pj_bool_t changed = PJ_FALSE; + + pjsua_acc_get_config(current_acc, &acc_cfg); + + if (argc == 3 && strcmp(argv[2], "show")==0) { + app_config_show_video(current_acc, &acc_cfg); + } else if (argc == 4 && strcmp(argv[2], "autorx")==0) { + int on = (strcmp(argv[3], "on")==0); + acc_cfg.vid_in_auto_show = on; + changed = PJ_TRUE; + } else if (argc == 4 && strcmp(argv[2], "autotx")==0) { + int on = (strcmp(argv[3], "on")==0); + acc_cfg.vid_out_auto_transmit = on; + changed = PJ_TRUE; + } else if (argc == 4 && strcmp(argv[2], "cap")==0) { + int dev = atoi(argv[3]); + acc_cfg.vid_cap_dev = dev; + changed = PJ_TRUE; + } else if (argc == 4 && strcmp(argv[2], "rend")==0) { + int dev = atoi(argv[3]); + acc_cfg.vid_rend_dev = dev; + changed = PJ_TRUE; + } else { + goto on_error; + } + + if (changed) { + 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)); + } + + } else if (strcmp(argv[1], "call")==0) { + pjsua_call_vid_strm_op_param param; + pj_status_t status = PJ_SUCCESS; + + pjsua_call_vid_strm_op_param_default(¶m); + + if (argc == 5 && strcmp(argv[2], "rx")==0) { + pjsua_stream_info si; + pj_bool_t on = (strcmp(argv[3], "on") == 0); + + param.med_idx = atoi(argv[4]); + 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; + } + + 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); + } + else if (argc == 5 && strcmp(argv[2], "tx")==0) { + pj_bool_t on = (strcmp(argv[3], "on") == 0); + pjsua_call_vid_strm_op op = on? PJSUA_CALL_VID_STRM_START_TRANSMIT : + PJSUA_CALL_VID_STRM_STOP_TRANSMIT; + + param.med_idx = atoi(argv[4]); + + status = pjsua_call_set_vid_strm(current_call, op, ¶m); + } + else if (argc == 3 && strcmp(argv[2], "add")==0) { + status = pjsua_call_set_vid_strm(current_call, + PJSUA_CALL_VID_STRM_ADD, NULL); + } + else if (argc >= 3 && + (strcmp(argv[2], "disable")==0 || strcmp(argv[2], "enable")==0)) + { + pj_bool_t enable = (strcmp(argv[2], "enable") == 0); + pjsua_call_vid_strm_op op = enable? PJSUA_CALL_VID_STRM_CHANGE_DIR : + PJSUA_CALL_VID_STRM_REMOVE; + + param.med_idx = argc >= 4? atoi(argv[3]) : -1; + param.dir = PJMEDIA_DIR_ENCODING_DECODING; + status = pjsua_call_set_vid_strm(current_call, op, ¶m); + } + else if (argc >= 3 && strcmp(argv[2], "cap")==0) { + param.med_idx = argc >= 4? atoi(argv[3]) : -1; + param.cap_dev = argc >= 5? atoi(argv[4]) : PJMEDIA_VID_DEFAULT_CAPTURE_DEV; + status = pjsua_call_set_vid_strm(current_call, + PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV, + ¶m); + } else + goto on_error; + + if (status != PJ_SUCCESS) { + PJ_PERROR(1,(THIS_FILE, status, "Error modifying video stream")); + } + + } else if (argc >= 3 && strcmp(argv[1], "dev")==0) { + if (strcmp(argv[2], "list")==0) { + vid_list_devs(); + } else if (strcmp(argv[2], "refresh")==0) { + pjmedia_vid_dev_refresh(); + } else if (strcmp(argv[2], "prev")==0) { + if (argc != 5) { + goto on_error; + } else { + pj_bool_t on = (strcmp(argv[3], "on") == 0); + int dev_id = atoi(argv[4]); + 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); + } + } + } + } else + goto on_error; + } else if (strcmp(argv[1], "win")==0) { + pj_status_t status = PJ_SUCCESS; + + if (argc==3 && strcmp(argv[2], "list")==0) { + 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)); + } + } else if (argc==4 && (strcmp(argv[2], "show")==0 || + strcmp(argv[2], "hide")==0)) + { + pj_bool_t show = (strcmp(argv[2], "show")==0); + pjsua_vid_win_id wid = atoi(argv[3]); + status = pjsua_vid_win_set_show(wid, show); + } else if (argc==6 && strcmp(argv[2], "move")==0) { + pjsua_vid_win_id wid = atoi(argv[3]); + pjmedia_coord pos; + + pos.x = atoi(argv[4]); + pos.y = atoi(argv[5]); + status = pjsua_vid_win_set_pos(wid, &pos); + } else if (argc==6 && strcmp(argv[2], "resize")==0) { + pjsua_vid_win_id wid = atoi(argv[3]); + pjmedia_rect_size size; + + size.w = atoi(argv[4]); + size.h = atoi(argv[5]); + status = pjsua_vid_win_set_size(wid, &size); + } else if (argc==3 && strcmp(argv[2], "arrange")==0) { + arrange_window(PJSUA_INVALID_ID); + } else + goto on_error; + + if (status != PJ_SUCCESS) { + PJ_PERROR(1,(THIS_FILE, status, "Window operation error")); + } + + } else if (strcmp(argv[1], "codec")==0) { + pjsua_codec_info ci[PJMEDIA_CODEC_MGR_MAX_CODECS]; + unsigned count = PJ_ARRAY_SIZE(ci); + pj_status_t status; + + if (argc==3 && strcmp(argv[2], "list")==0) { + 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)); + } + } + } else if (argc==5 && strcmp(argv[2], "prio")==0) { + pj_str_t cid; + int prio; + cid = pj_str(argv[3]); + prio = atoi(argv[4]); + status = pjsua_vid_codec_set_priority(&cid, (pj_uint8_t)prio); + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Set codec priority error")); + } else if (argc==6 && strcmp(argv[2], "fps")==0) { + pjmedia_vid_codec_param cp; + pj_str_t cid; + int M, N; + cid = pj_str(argv[3]); + M = atoi(argv[4]); + N = atoi(argv[5]); + status = pjsua_vid_codec_get_param(&cid, &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(&cid, &cp); + } + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Set codec framerate error")); + } else if (argc==6 && strcmp(argv[2], "bw")==0) { + pjmedia_vid_codec_param cp; + pj_str_t cid; + int M, N; + cid = pj_str(argv[3]); + M = atoi(argv[4]); + N = atoi(argv[5]); + status = pjsua_vid_codec_get_param(&cid, &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(&cid, &cp); + } + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error")); + } else if (argc==6 && strcmp(argv[2], "size")==0) { + pjmedia_vid_codec_param cp; + pj_str_t cid; + int M, N; + cid = pj_str(argv[3]); + M = atoi(argv[4]); + N = atoi(argv[5]); + status = pjsua_vid_codec_get_param(&cid, &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(&cid, &cp); + } + if (status != PJ_SUCCESS) + PJ_PERROR(1,(THIS_FILE, status, "Set codec size error")); + } else + goto on_error; + } else + goto on_error; + + return; + +on_error: + PJ_LOG(1,(THIS_FILE, "Invalid command, use 'vid help'")); +} + +#endif /* PJSUA_HAS_VIDEO */ + +/** UI Command **/ +static void ui_make_new_call() +{ + char buf[128]; + pjsua_msg_data msg_data; + input_result result; + pj_str_t tmp; + + 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 == -1 || result.nb_result == 0) { + puts("You can't do that with make call!"); + return; + } else { + pjsua_buddy_info binfo; + pjsua_buddy_get_info(result.nb_result-1, &binfo); + tmp.ptr = buf; + pj_strncpy(&tmp, &binfo.uri, sizeof(buf)); + } + + } 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); +} + +static void ui_make_multi_call() +{ + char menuin[32]; + int count; + char buf[128]; + input_result result; + pj_str_t tmp; + int i; + + printf("(You currently have %d calls)\n", pjsua_call_get_count()); + + if (!simple_input("Number of calls", menuin, sizeof(menuin))) + return; + + count = my_atoi(menuin); + if (count < 1) + return; + + ui_input_url("Make call", buf, sizeof(buf), &result); + if (result.nb_result != NO_NB) { + pjsua_buddy_info binfo; + if (result.nb_result == -1 || result.nb_result == 0) { + puts("You can't do that with make call!"); + return; + } + pjsua_buddy_get_info(result.nb_result-1, &binfo); + tmp.ptr = buf; + pj_strncpy(&tmp, &binfo.uri, sizeof(buf)); + } else { + tmp = pj_str(result.uri_result); + } + + for (i=0; i= PJSIP_INV_STATE_CONNECTING) + { + puts("No pending incoming call"); + fflush(stdout); + return; + + } else { + int st_code; + char contact[120]; + pj_str_t hname = { "Contact", 7 }; + pj_str_t hvalue; + pjsip_generic_string_hdr hcontact; + + if (!simple_input("Answer with code (100-699)", buf, sizeof(buf))) + return; + + st_code = my_atoi(buf); + if (st_code < 100) + return; + + pjsua_msg_data_init(&msg_data); + + if (st_code/100 == 3) { + if (!simple_input("Enter URL to be put in Contact", + contact, sizeof(contact))) + return; + 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 == -1) { + puts("Call has been disconnected"); + fflush(stdout); + return; + } + + pjsua_call_answer2(current_call, &call_opt, st_code, NULL, &msg_data); + } +} + +static void ui_hangup_call(char menuin[]) +{ + if (current_call == -1) { + puts("No current call"); + fflush(stdout); + return; + + } else if (menuin[1] == 'a') { + /* Hangup all calls */ + pjsua_call_hangup_all(); + } else { + /* Hangup current calls */ + pjsua_call_hangup(current_call, 0, NULL, NULL); + } +} + +static void ui_cycle_dialog(char menuin[]) +{ + if (menuin[0] == ']') { + find_next_call(); + + } else { + find_prev_call(); + } + + if (current_call != -1) { + 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")); + } +} + +static void ui_cycle_account() +{ + int i; + char buf[128]; + + if (!simple_input("Enter account ID to select", buf, sizeof(buf))) + return; + + i = my_atoi(buf); + 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)); + } +} + +static void ui_add_buddy() +{ + char buf[128]; + pjsua_buddy_config buddy_cfg; + pjsua_buddy_id buddy_id; + pj_status_t status; + + if (!simple_input("Enter buddy's URI:", buf, sizeof(buf))) + return; + + if (pjsua_verify_url(buf) != PJ_SUCCESS) { + printf("Invalid URI '%s'\n", buf); + return; + } + + pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config)); + + buddy_cfg.uri = pj_str(buf); + buddy_cfg.subscribe = PJ_TRUE; + + status = pjsua_buddy_add(&buddy_cfg, &buddy_id); + if (status == PJ_SUCCESS) { + printf("New buddy '%s' added at index %d\n", + buf, buddy_id+1); + } +} + +static void ui_add_account(pjsua_transport_config *rtp_cfg) +{ + char id[80], registrar[80], realm[80], uname[80], passwd[30]; + pjsua_acc_config acc_cfg; + pj_status_t status; + + if (!simple_input("Your SIP URL:", id, sizeof(id))) + return; + if (!simple_input("URL of the registrar:", registrar, sizeof(registrar))) + return; + if (!simple_input("Auth Realm:", realm, sizeof(realm))) + return; + if (!simple_input("Auth Username:", uname, sizeof(uname))) + return; + if (!simple_input("Auth Password:", passwd, sizeof(passwd))) + return; + + pjsua_acc_config_default(&acc_cfg); + acc_cfg.id = pj_str(id); + acc_cfg.reg_uri = pj_str(registrar); + acc_cfg.cred_count = 1; + acc_cfg.cred_info[0].scheme = pj_str("Digest"); + acc_cfg.cred_info[0].realm = pj_str(realm); + acc_cfg.cred_info[0].username = pj_str(uname); + acc_cfg.cred_info[0].data_type = 0; + acc_cfg.cred_info[0].data = pj_str(passwd); + + acc_cfg.rtp_cfg = *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); + } +} + +static void ui_delete_buddy() +{ + char buf[128]; + int i; + + if (!simple_input("Enter buddy ID to delete", buf, sizeof(buf))) + return; + + i = my_atoi(buf) - 1; + + if (!pjsua_buddy_is_valid(i)) { + printf("Invalid buddy id %d\n", i); + } else { + pjsua_buddy_del(i); + printf("Buddy %d deleted\n", i); + } +} + +static void ui_delete_account() +{ + char buf[128]; + int i; + + if (!simple_input("Enter account ID to delete", buf, sizeof(buf))) + return; + + i = my_atoi(buf); + + if (!pjsua_acc_is_valid(i)) { + printf("Invalid account id %d\n", i); + } else { + pjsua_acc_del(i); + printf("Account %d deleted\n", i); + } +} + +static void ui_call_hold() +{ + if (current_call != -1) { + pjsua_call_set_hold(current_call, NULL); + } else { + PJ_LOG(3,(THIS_FILE, "No current call")); + } +} + +static void ui_call_reinvite() +{ + call_opt.flag |= PJSUA_CALL_UNHOLD; + pjsua_call_reinvite2(current_call, &call_opt, NULL); +} + +static void ui_send_update() +{ + if (current_call != -1) { + call_opt.flag |= PJSUA_CALL_UNHOLD; + pjsua_call_update2(current_call, &call_opt, NULL); + } else { + PJ_LOG(3,(THIS_FILE, "No current call")); + } +} + +/* + * Change codec priorities. + */ +static void ui_manage_codec_prio() +{ + pjsua_codec_info c[32]; + unsigned i, count = PJ_ARRAY_SIZE(c); + char input[32]; + char *codec, *prio; + pj_str_t id; + int new_prio; + pj_status_t status; + + printf("List of audio codecs:\n"); + pjsua_enum_codecs(c, &count); + for (i=0; i PJMEDIA_CODEC_PRIO_HIGHEST) + new_prio = PJMEDIA_CODEC_PRIO_HIGHEST; + + status = pjsua_codec_set_priority(pj_cstr(&id, codec), + (pj_uint8_t)new_prio); +#if PJSUA_HAS_VIDEO + if (status != PJ_SUCCESS) { + status = pjsua_vid_codec_set_priority(pj_cstr(&id, codec), + (pj_uint8_t)new_prio); + } +#endif + if (status != PJ_SUCCESS) + pjsua_perror(THIS_FILE, "Error setting codec priority", status); +} + +static void ui_call_transfer(pj_bool_t no_refersub) +{ + if (current_call == -1) { + PJ_LOG(3,(THIS_FILE, "No current call")); + } else { + int call = current_call; + char buf[128]; + 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; + input_result result; + pjsua_msg_data msg_data; + + pjsua_call_get_info(current_call, &ci); + printf("Transfering current call [%d] %.*s\n", current_call, + (int)ci.remote_info.slen, ci.remote_info.ptr); + + ui_input_url("Transfer to URL", buf, sizeof(buf), &result); + + /* Check if call is still there. */ + + if (call != current_call) { + puts("Call has been disconnected"); + return; + } + + pjsua_msg_data_init(&msg_data); + if (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) + puts("You can't do that with transfer call!"); + 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); + } + } +} + +static void ui_call_transfer_replaces(pj_bool_t no_refersub) +{ + 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_call_info ci; + pjsua_msg_data msg_data; + char buf[128]; + unsigned i, count; + + count = PJ_ARRAY_SIZE(ids); + pjsua_enum_calls(ids, &count); + + if (count <= 1) { + puts("There are no other calls"); + return; + } + + pjsua_call_get_info(current_call, &ci); + printf("Transfer call [%d] %.*s to one of the following:\n", + current_call, + (int)ci.remote_info.slen, ci.remote_info.ptr); + + for (i=0; i= PJSUA_MAX_CALLS) { + puts("Invalid destination call number"); + return; + } + if (!pjsua_call_is_active(dst_call)) { + puts("Invalid destination call number"); + return; + } + + pjsua_msg_data_init(&msg_data); + if (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); + } +} + +static void ui_send_dtmf_2833() +{ + if (current_call == -1) { + 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 { + pj_str_t digits; + int call = current_call; + pj_status_t status; + char buf[128]; + + if (!simple_input("DTMF strings to send (0-9*#A-B)", buf, + sizeof(buf))) + { + return; + } + + if (call != current_call) { + puts("Call has been disconnected"); + return; + } + + digits = pj_str(buf); + status = pjsua_call_dial_dtmf(current_call, &digits); + if (status != PJ_SUCCESS) { + pjsua_perror(THIS_FILE, "Unable to send DTMF", status); + } else { + puts("DTMF digits enqueued for transmission"); + } + } +} + +static void ui_send_dtmf_info() +{ + if (current_call == -1) { + PJ_LOG(3,(THIS_FILE, "No current call")); + } else { + const pj_str_t SIP_INFO = pj_str("INFO"); + pj_str_t digits; + int call = current_call; + int i; + pj_status_t status; + char buf[128]; + + if (!simple_input("DTMF strings to send (0-9*#A-B)", buf, + sizeof(buf))) + { + return; + } + + if (call != current_call) { + puts("Call has been disconnected"); + return; + } + + digits = pj_str(buf); + for (i=0; i= OPT_MAX) { + puts("Invalid selection"); + return; + } + + 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); +} + +/* + * List the ports in conference bridge + */ +static void ui_conf_list() +{ + unsigned i, count; + pjsua_conf_port_id id[PJSUA_MAX_CALLS]; + + printf("Conference ports:\n"); + + count = PJ_ARRAY_SIZE(id); + pjsua_enum_conf_ports(id, &count); + + for (i=0; islen) { + pjsua_call_make_call( current_acc, uri_to_call, &call_opt, + NULL, NULL, NULL); + } + + keystroke_help(current_call); + + for (;;) { + + printf(">>> "); + fflush(stdout); + + if (fgets(menuin, sizeof(menuin), stdin) == NULL) { + /* + * Be friendly to users who redirect commands into + * program, when file ends, resume with kbd. + * If exit is desired end script with q for quit + */ + /* Reopen stdin/stdout/stderr to /dev/console */ +#if defined(PJ_WIN32) && PJ_WIN32!=0 + if (freopen ("CONIN$", "r", stdin) == NULL) { +#else + if (1) { +#endif + puts("Cannot switch back to console from file redirection"); + menuin[0] = 'q'; + menuin[1] = '\0'; + } else { + puts("Switched back to console from file redirection"); + continue; + } + } + + if (cmd_echo) { + printf("%s", menuin); + } + + /* Update call setting */ + pjsua_call_setting_default(&call_opt); + call_opt.aud_cnt = app_config.aud_cnt; + call_opt.vid_cnt = app_config.vid.vid_cnt; + + switch (menuin[0]) { + + case 'm': + /* Make call! : */ + ui_make_new_call(); + break; + + case 'M': + /* Make multiple calls! : */ + ui_make_multi_call(); + break; + + case 'n': + ui_detect_nat_type(); + break; + + case 'i': + /* Send instant messaeg */ + ui_send_instant_message(); + break; + + case 'a': + ui_answer_call(); + break; + + case 'h': + ui_hangup_call(menuin); + break; + + case ']': + case '[': + /* + * Cycle next/prev dialog. + */ + ui_cycle_dialog(menuin); + break; + + case '>': + case '<': + ui_cycle_account(); + break; + + case '+': + if (menuin[1] == 'b') { + ui_add_buddy(); + } else if (menuin[1] == 'a') { + ui_add_account(&app_config.rtp_cfg); + } else { + printf("Invalid input %s\n", menuin); + } + break; + + case '-': + if (menuin[1] == 'b') { + ui_delete_buddy(); + } else if (menuin[1] == 'a') { + ui_delete_account(); + } else { + printf("Invalid input %s\n", menuin); + } + break; + + case 'H': + /* + * Hold call. + */ + ui_call_hold(); + break; + + case 'v': +#if PJSUA_HAS_VIDEO + if (menuin[1]=='i' && menuin[2]=='d' && menuin[3]==' ') { + vid_handle_menu(menuin); + } else +#endif + if (current_call != -1) { + /* + * re-INVITE + */ + ui_call_reinvite(); + } else { + PJ_LOG(3,(THIS_FILE, "No current call")); + } + break; + + case 'U': + /* + * Send UPDATE + */ + ui_send_update(); + break; + + case 'C': + if (menuin[1] == 'p') { + ui_manage_codec_prio(); + } + break; + + case 'x': + /* + * Transfer call. + */ + ui_call_transfer(app_config.no_refersub); + break; + + case 'X': + /* + * Transfer call with replaces. + */ + ui_call_transfer_replaces(app_config.no_refersub); + break; + + case '#': + /* + * Send DTMF strings. + */ + ui_send_dtmf_2833(); + break; + + case '*': + /* Send DTMF with INFO */ + ui_send_dtmf_info(); + break; + + case 'S': + /* + * Send arbitrary request + */ + ui_send_arbitrary_request(); + break; + + case 'e': + ui_echo(menuin); + break; + + case 's': + ui_sleep(menuin); + break; + /* Continue below */ + + case 'u': + /* + * Subscribe/unsubscribe presence. + */ + ui_subscribe(menuin); + break; + + case 'r': + ui_register(menuin); + break; + + case 't': + ui_toggle_state(); + break; + + case 'T': + ui_change_online_status(); + break; + + case 'c': + switch (menuin[1]) { + case 'l': + ui_conf_list(); + break; + case 'c': + case 'd': + ui_conf_connect(menuin); + break; + } + break; + + case 'V': + /* Adjust audio volume */ + ui_adjust_volume(); + break; + + case 'd': + if (menuin[1] == 'c') { + ui_dump_configuration(); + } else if (menuin[1] == 'q') { + ui_dump_call_quality(); + } else { + ui_app_dump(menuin[1]=='d'); + } + break; + + case 'f': + if (simple_input("Enter output filename", buf, sizeof(buf))) { + ui_write_settings(); + } + break; + + case 'L': /* Restart */ + *app_restart = PJ_TRUE; + /* Continues below */ + + case 'q': + goto on_exit; + + case 'R': + ui_call_redirect(menuin); + break; + + default: + if (menuin[0] != '\n' && menuin[0] != '\r') { + printf("Invalid input %s", menuin); + } + keystroke_help(); + break; + } + } + +on_exit: + ; +} + -- cgit v1.2.3