/* $Id$ */ /* * Copyright (C) 2003-2006 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 #include /* atoi */ #include #define THIS_FILE "main.c" /* Current dialog */ static int current_acc; static int current_call = -1; /* * Find next call. */ static pj_bool_t find_next_call(void) { int i, max; max = pjsua_get_max_calls(); 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 = -1; return PJ_FALSE; } /* * Notify UI when invite state has changed. */ static void console_on_call_state(int call_index, pjsip_event *e) { pjsua_call_info call_info; PJ_UNUSED_ARG(e); pjsua_get_call_info(call_index, &call_info); if (call_info.state == PJSIP_INV_STATE_DISCONNECTED) { PJ_LOG(3,(THIS_FILE, "Call %d is DISCONNECTED [reason=%d (%s)]", call_index, call_info.cause, call_info.cause_text.ptr)); if ((int)call_index == current_call) { find_next_call(); } } else { PJ_LOG(3,(THIS_FILE, "Call %d state changed to %s", call_index, call_info.state_text.ptr)); if (current_call==-1) current_call = call_index; } } /** * Notify UI when registration status has changed. */ static void console_on_reg_state(int acc_index) { PJ_UNUSED_ARG(acc_index); // Log already written. } /** * Notify UI on buddy state changed. */ static void console_on_buddy_state(int buddy_index) { pjsua_buddy_info info; pjsua_buddy_get_info(buddy_index, &info); PJ_LOG(3,(THIS_FILE, "%.*s status is %.*s", (int)info.uri.slen, info.uri.ptr, (int)info.status_text.slen, info.status_text.ptr)); } /** * Incoming IM message (i.e. MESSAGE request)! */ static void console_on_pager(int call_index, const pj_str_t *from, const pj_str_t *to, const pj_str_t *text) { /* Note: call index may be -1 */ PJ_UNUSED_ARG(call_index); PJ_UNUSED_ARG(to); PJ_LOG(3,(THIS_FILE,"MESSAGE from %.*s: %.*s", (int)from->slen, from->ptr, (int)text->slen, text->ptr)); } /** * Typing indication */ static void console_on_typing(int call_index, const pj_str_t *from, const pj_str_t *to, pj_bool_t is_typing) { PJ_UNUSED_ARG(call_index); PJ_UNUSED_ARG(to); PJ_LOG(3,(THIS_FILE, "IM indication: %.*s %s", (int)from->slen, from->ptr, (is_typing?"is typing..":"has stopped typing"))); } /* * Print buddy list. */ static void print_buddy_list(void) { int i, count; puts("Buddy list:"); count = pjsua_get_buddy_count(); if (count == 0) puts(" -none-"); else { for (i=0; i %.*s\n", i+1, info.status_text.ptr, (int)info.uri.slen, info.uri.ptr); } } puts(""); } /* * Print account status. */ static void print_acc_status(int acc_index) { char buf[80]; pjsua_acc_info info; pjsua_acc_get_info(acc_index, &info); if (!info.has_registration) { pj_ansi_strcpy(buf, " -not registered to server-"); } else { pj_ansi_snprintf(buf, sizeof(buf), "%.*s (%.*s;expires=%d)", (int)info.status_text.slen, info.status_text.ptr, (int)info.acc_id.slen, info.acc_id.ptr, info.expires); } printf("[%2d] Registration status: %s\n", acc_index, buf); printf(" Online status: %s\n", (info.online_status ? "Online" : "Invisible")); } /* * Show a bit of help. */ static void keystroke_help(void) { int i; printf(">>>>\n"); for (i=0; i<(int)pjsua_get_acc_count(); ++i) print_acc_status(i); print_buddy_list(); //puts("Commands:"); puts("+=============================================================================+"); puts("| Call Commands: | IM & Presence: | Misc: |"); puts("| | | |"); puts("| m Make new call | i Send IM | o Send OPTIONS |"); puts("| M Make multiple calls | s Subscribe presence | rr (Re-)register |"); puts("| a Answer call | u Unsubscribe presence | ru Unregister |"); puts("| h Hangup call (ha=all) | t ToGgle Online status | |"); puts("| H Hold call | | |"); puts("| v re-inVite (release hold) +--------------------------+-------------------+"); puts("| ] Select next dialog | Conference Command | |"); puts("| [ Select previous dialog | cl List ports | d Dump status |"); puts("| x Xfer call | cc Connect port | dd Dump detailed |"); puts("| # Send DTMF string | cd Disconnect port | dc Dump config |"); puts("+------------------------------+--------------------------+-------------------+"); puts("| q QUIT |"); puts("+=============================================================================+"); } /* * Input simple string */ 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); fgets(buf, len, stdin); /* 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; } #define NO_NB -2 struct input_result { int nb_result; char *uri_result; }; /* * Input URL. */ static void ui_input_url(const char *title, char *buf, int len, struct 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); fgets(buf, len, stdin); 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 = 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_sip_url(buf)) != PJ_SUCCESS) { pjsua_perror(THIS_FILE, "Invalid URL", status); return; } result->uri_result = buf; } } static void conf_list(void) { unsigned i, count; pjmedia_conf_port_info info[PJSUA_MAX_CALLS]; printf("Conference ports:\n"); count = PJ_ARRAY_SIZE(info); pjsua_conf_enum_ports(&count, info); for (i=0; ilistener[j]) { pj_ansi_sprintf(s, "#%d ", j); pj_ansi_strcat(txlist, s); } } printf("Port #%02d[%2dKHz/%dms] %20.*s transmitting to: %s\n", port_info->slot, port_info->clock_rate/1000, port_info->samples_per_frame * 1000 / port_info->clock_rate, (int)port_info->name.slen, port_info->name.ptr, txlist); } puts(""); } void pjsua_console_app_main(void) { char menuin[10]; char buf[128]; char text[128]; int i, count; char *uri; pj_str_t tmp; struct input_result result; pjsua_call_info call_info; pjsua_acc_info acc_info; /* If user specifies URI to call, then call the URI */ if (pjsua_get_config()->uri_to_call.slen) { pjsua_make_call( current_acc, &pjsua_get_config()->uri_to_call, NULL); } keystroke_help(); for (;;) { printf(">>> "); fflush(stdout); fgets(menuin, sizeof(menuin), stdin); switch (menuin[0]) { case 'm': /* Make call! : */ printf("(You currently have %d calls)\n", pjsua_get_call_count()); uri = NULL; 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!"); continue; } else { pjsua_buddy_info binfo; pjsua_buddy_get_info(result.nb_result-1, &binfo); uri = binfo.uri.ptr; } } else if (result.uri_result) { uri = result.uri_result; } tmp = pj_str(uri); pjsua_make_call( current_acc, &tmp, NULL); break; case 'M': /* Make multiple calls! : */ printf("(You currently have %d calls)\n", pjsua_get_call_count()); if (!simple_input("Number of calls", menuin, sizeof(menuin))) continue; count = atoi(menuin); if (count < 1) continue; 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!"); continue; } pjsua_buddy_get_info(result.nb_result-1, &binfo); uri = binfo.uri.ptr; } else { uri = result.uri_result; } for (i=0; i= PJSIP_INV_STATE_CONNECTING) { puts("No pending incoming call"); fflush(stdout); continue; } else { if (!simple_input("Answer with code (100-699)", buf, sizeof(buf))) continue; if (atoi(buf) < 100) continue; /* * 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); continue; } pjsua_call_answer(current_call, atoi(buf)); } break; case 'h': if (current_call == -1) { puts("No current call"); fflush(stdout); continue; } else if (menuin[1] == 'a') { /* Hangup all calls */ pjsua_call_hangup_all(); } else { /* Hangup current calls */ pjsua_call_hangup(current_call); } break; case ']': case '[': /* * Cycle next/prev dialog. */ if (menuin[0] == ']') { find_next_call(); } else { find_prev_call(); } if (current_call != -1) { pjsua_get_call_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")); } break; case 'H': /* * Hold call. */ if (current_call != -1) { pjsua_call_set_hold(current_call); } else { PJ_LOG(3,(THIS_FILE, "No current call")); } break; case 'v': /* * Send re-INVITE (to release hold, etc). */ if (current_call != -1) { pjsua_call_reinvite(current_call); } else { PJ_LOG(3,(THIS_FILE, "No current call")); } break; case 'x': /* * Transfer call. */ if (current_call == -1) { PJ_LOG(3,(THIS_FILE, "No current call")); } else { int call = current_call; 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"); continue; } 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); } } else if (result.uri_result) { pj_str_t tmp; tmp = pj_str(result.uri_result); pjsua_call_xfer( current_call, &tmp); } } break; case '#': /* * Send DTMF strings. */ 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; if (!simple_input("DTMF strings to send (0-9*#A-B)", buf, sizeof(buf))) { break; } if (call != current_call) { puts("Call has been disconnected"); continue; } 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"); } } break; case 's': case 'u': /* * Subscribe/unsubscribe presence. */ ui_input_url("(un)Subscribe presence of", buf, sizeof(buf), &result); if (result.nb_result != NO_NB) { if (result.nb_result == -1) { int i, count; count = pjsua_get_buddy_count(); for (i=0; imsg_info.len, pjsip_rx_data_get_info(rdata), rdata->pkt_info.src_name, rdata->pkt_info.src_port, rdata->msg_info.msg_buf)); /* Always return false, otherwise messages will not get processed! */ return PJ_FALSE; } /* Notification on outgoing messages */ static pj_status_t console_on_tx_msg(pjsip_tx_data *tdata) { /* Important note: * tp_info field is only valid after outgoing messages has passed * transport layer. So don't try to access tp_info when the module * has lower priority than transport layer. */ PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s:%d:\n" "%s\n" "--end msg--", (tdata->buf.cur - tdata->buf.start), pjsip_tx_data_get_info(tdata), tdata->tp_info.dst_name, tdata->tp_info.dst_port, tdata->buf.start)); /* Always return success, otherwise message will not get sent! */ return PJ_SUCCESS; } /* The module instance. */ pjsip_module pjsua_console_app_msg_logger = { NULL, NULL, /* prev, next. */ { "mod-pjsua-log", 13 }, /* Name. */ -1, /* Id */ PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */ NULL, /* load() */ NULL, /* start() */ NULL, /* stop() */ NULL, /* unload() */ &console_on_rx_msg, /* on_rx_request() */ &console_on_rx_msg, /* on_rx_response() */ &console_on_tx_msg, /* on_tx_request. */ &console_on_tx_msg, /* on_tx_response() */ NULL, /* on_tsx_state() */ }; /***************************************************************************** * Error display: */ /* * Display error message for the specified error code. */ void pjsua_perror(const char *sender, const char *title, pj_status_t status) { char errmsg[PJ_ERR_MSG_SIZE]; pj_strerror(status, errmsg, sizeof(errmsg)); PJ_LOG(1,(sender, "%s: %s [code=%d]", title, errmsg, status)); } pjsua_callback console_callback = { &console_on_call_state, NULL, /* default accept transfer */ &console_on_reg_state, &console_on_buddy_state, &console_on_pager, &console_on_typing, };