Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include "pjsua_app_common.h" |
| 22 | |
| 23 | #define THIS_FILE "pjsua_app_common.c" |
| 24 | |
| 25 | #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0) |
| 26 | # define SOME_BUF_SIZE (1024 * 10) |
| 27 | #else |
| 28 | # define SOME_BUF_SIZE (1024 * 3) |
| 29 | #endif |
| 30 | |
| 31 | static char some_buf[SOME_BUF_SIZE]; |
| 32 | |
| 33 | /** Variable definition **/ |
| 34 | int stdout_refresh = -1; |
| 35 | pj_bool_t stdout_refresh_quit = PJ_FALSE; |
| 36 | pjsua_call_id current_call = PJSUA_INVALID_ID; |
| 37 | pjsua_app_config app_config; |
| 38 | pjsua_call_setting call_opt; |
| 39 | pjsua_msg_data msg_data; |
| 40 | |
| 41 | int my_atoi(const char *cs) |
| 42 | { |
| 43 | pj_str_t s; |
| 44 | |
| 45 | pj_cstr(&s, cs); |
| 46 | if (cs[0] == '-') { |
| 47 | s.ptr++, s.slen--; |
| 48 | return 0 - (int)pj_strtoul(&s); |
| 49 | } else if (cs[0] == '+') { |
| 50 | s.ptr++, s.slen--; |
| 51 | return pj_strtoul(&s); |
| 52 | } else { |
| 53 | return pj_strtoul(&s); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Find next call when current call is disconnected or when user |
| 59 | * press ']' |
| 60 | */ |
| 61 | pj_bool_t find_next_call() |
| 62 | { |
| 63 | int i, max; |
| 64 | |
| 65 | max = pjsua_call_get_max_count(); |
| 66 | for (i=current_call+1; i<max; ++i) { |
| 67 | if (pjsua_call_is_active(i)) { |
| 68 | current_call = i; |
| 69 | return PJ_TRUE; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | for (i=0; i<current_call; ++i) { |
| 74 | if (pjsua_call_is_active(i)) { |
| 75 | current_call = i; |
| 76 | return PJ_TRUE; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | current_call = PJSUA_INVALID_ID; |
| 81 | return PJ_FALSE; |
| 82 | } |
| 83 | |
| 84 | pj_bool_t find_prev_call() |
| 85 | { |
| 86 | int i, max; |
| 87 | |
| 88 | max = pjsua_call_get_max_count(); |
| 89 | for (i=current_call-1; i>=0; --i) { |
| 90 | if (pjsua_call_is_active(i)) { |
| 91 | current_call = i; |
| 92 | return PJ_TRUE; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | for (i=max-1; i>current_call; --i) { |
| 97 | if (pjsua_call_is_active(i)) { |
| 98 | current_call = i; |
| 99 | return PJ_TRUE; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | current_call = PJSUA_INVALID_ID; |
| 104 | return PJ_FALSE; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Send arbitrary request to remote host |
| 109 | */ |
| 110 | void send_request(char *cstr_method, const pj_str_t *dst_uri) |
| 111 | { |
| 112 | pj_str_t str_method; |
| 113 | pjsip_method method; |
| 114 | pjsip_tx_data *tdata; |
| 115 | pjsip_endpoint *endpt; |
| 116 | pj_status_t status; |
| 117 | |
| 118 | endpt = pjsua_get_pjsip_endpt(); |
| 119 | |
| 120 | str_method = pj_str(cstr_method); |
| 121 | pjsip_method_init_np(&method, &str_method); |
| 122 | |
| 123 | status = pjsua_acc_create_request(current_acc, &method, dst_uri, &tdata); |
| 124 | |
| 125 | status = pjsip_endpt_send_request(endpt, tdata, -1, NULL, NULL); |
| 126 | if (status != PJ_SUCCESS) { |
| 127 | pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 128 | return; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Print log of call states. Since call states may be too long for logger, |
| 134 | * printing it is a bit tricky, it should be printed part by part as long |
| 135 | * as the logger can accept. |
| 136 | */ |
| 137 | void log_call_dump(int call_id) |
| 138 | { |
| 139 | unsigned call_dump_len; |
| 140 | unsigned part_len; |
| 141 | unsigned part_idx; |
| 142 | unsigned log_decor; |
| 143 | |
| 144 | pjsua_call_dump(call_id, PJ_TRUE, some_buf, sizeof(some_buf), " "); |
| 145 | call_dump_len = (unsigned)strlen(some_buf); |
| 146 | |
| 147 | log_decor = pj_log_get_decor(); |
| 148 | pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR)); |
| 149 | PJ_LOG(3,(THIS_FILE, "\n")); |
| 150 | pj_log_set_decor(0); |
| 151 | |
| 152 | part_idx = 0; |
| 153 | part_len = PJ_LOG_MAX_SIZE-80; |
| 154 | while (part_idx < call_dump_len) { |
| 155 | char p_orig, *p; |
| 156 | |
| 157 | p = &some_buf[part_idx]; |
| 158 | if (part_idx + part_len > call_dump_len) |
| 159 | part_len = call_dump_len - part_idx; |
| 160 | p_orig = p[part_len]; |
| 161 | p[part_len] = '\0'; |
| 162 | PJ_LOG(3,(THIS_FILE, "%s", p)); |
| 163 | p[part_len] = p_orig; |
| 164 | part_idx += part_len; |
| 165 | } |
| 166 | pj_log_set_decor(log_decor); |
| 167 | } |
| 168 | |
| 169 | #ifdef PJSUA_HAS_VIDEO |
| 170 | void app_config_init_video(pjsua_acc_config *acc_cfg) |
| 171 | { |
| 172 | acc_cfg->vid_in_auto_show = app_config.vid.in_auto_show; |
| 173 | acc_cfg->vid_out_auto_transmit = app_config.vid.out_auto_transmit; |
| 174 | /* Note that normally GUI application will prefer a borderless |
| 175 | * window. |
| 176 | */ |
| 177 | acc_cfg->vid_wnd_flags = PJMEDIA_VID_DEV_WND_BORDER | |
| 178 | PJMEDIA_VID_DEV_WND_RESIZABLE; |
| 179 | acc_cfg->vid_cap_dev = app_config.vid.vcapture_dev; |
| 180 | acc_cfg->vid_rend_dev = app_config.vid.vrender_dev; |
| 181 | |
| 182 | if (app_config.avi_auto_play && |
| 183 | app_config.avi_def_idx != PJSUA_INVALID_ID && |
| 184 | app_config.avi[app_config.avi_def_idx].dev_id != PJMEDIA_VID_INVALID_DEV) |
| 185 | { |
| 186 | acc_cfg->vid_cap_dev = app_config.avi[app_config.avi_def_idx].dev_id; |
| 187 | } |
| 188 | } |
| 189 | #else |
| 190 | void app_config_init_video(pjsua_acc_config *acc_cfg) |
| 191 | { |
| 192 | PJ_UNUSED_ARG(acc_cfg); |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | #ifdef HAVE_MULTIPART_TEST |
| 197 | /* |
| 198 | * Enable multipart in msg_data and add a dummy body into the |
| 199 | * multipart bodies. |
| 200 | */ |
| 201 | void add_multipart(pjsua_msg_data *msg_data) |
| 202 | { |
| 203 | static pjsip_multipart_part *alt_part; |
| 204 | |
| 205 | if (!alt_part) { |
| 206 | pj_str_t type, subtype, content; |
| 207 | |
| 208 | alt_part = pjsip_multipart_create_part(app_config.pool); |
| 209 | |
| 210 | type = pj_str("text"); |
| 211 | subtype = pj_str("plain"); |
| 212 | content = pj_str("Sample text body of a multipart bodies"); |
| 213 | alt_part->body = pjsip_msg_body_create(app_config.pool, &type, |
| 214 | &subtype, &content); |
| 215 | } |
| 216 | |
| 217 | msg_data->multipart_ctype.type = pj_str("multipart"); |
| 218 | msg_data->multipart_ctype.subtype = pj_str("mixed"); |
| 219 | pj_list_push_back(&msg_data->multipart_parts, alt_part); |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | /* arrange windows. arg: |
| 224 | * -1: arrange all windows |
| 225 | * != -1: arrange only this window id |
| 226 | */ |
| 227 | void arrange_window(pjsua_vid_win_id wid) |
| 228 | { |
| 229 | #if PJSUA_HAS_VIDEO |
| 230 | pjmedia_coord pos; |
| 231 | int i, last; |
| 232 | |
| 233 | pos.x = 0; |
| 234 | pos.y = 10; |
| 235 | last = (wid == PJSUA_INVALID_ID) ? PJSUA_MAX_VID_WINS : wid; |
| 236 | |
| 237 | for (i=0; i<last; ++i) { |
| 238 | pjsua_vid_win_info wi; |
| 239 | pj_status_t status; |
| 240 | |
| 241 | status = pjsua_vid_win_get_info(i, &wi); |
| 242 | if (status != PJ_SUCCESS) |
| 243 | continue; |
| 244 | |
| 245 | if (wid == PJSUA_INVALID_ID) |
| 246 | pjsua_vid_win_set_pos(i, &pos); |
| 247 | |
| 248 | if (wi.show) |
| 249 | pos.y += wi.size.h; |
| 250 | } |
| 251 | |
| 252 | if (wid != PJSUA_INVALID_ID) |
| 253 | pjsua_vid_win_set_pos(wid, &pos); |
| 254 | #else |
| 255 | PJ_UNUSED_ARG(wid); |
| 256 | #endif |
| 257 | } |
| 258 | |
| 259 | |
| 260 | #if PJSUA_HAS_VIDEO |
| 261 | void vid_print_dev(int id, const pjmedia_vid_dev_info *vdi, const char *title) |
| 262 | { |
| 263 | char capnames[120]; |
| 264 | char formats[120]; |
| 265 | const char *dirname; |
| 266 | unsigned i; |
| 267 | |
| 268 | if (vdi->dir == PJMEDIA_DIR_CAPTURE_RENDER) { |
| 269 | dirname = "capture, render"; |
| 270 | } else if (vdi->dir == PJMEDIA_DIR_CAPTURE) { |
| 271 | dirname = "capture"; |
| 272 | } else { |
| 273 | dirname = "render"; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | capnames[0] = '\0'; |
| 278 | for (i=0; i<sizeof(int)*8 && (1 << i) < PJMEDIA_VID_DEV_CAP_MAX; ++i) { |
| 279 | if (vdi->caps & (1 << i)) { |
| 280 | const char *capname = pjmedia_vid_dev_cap_name(1 << i, NULL); |
| 281 | if (capname) { |
| 282 | if (*capnames) |
| 283 | strcat(capnames, ", "); |
| 284 | strncat(capnames, capname, |
| 285 | sizeof(capnames)-strlen(capnames)-1); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | formats[0] = '\0'; |
| 291 | for (i=0; i<vdi->fmt_cnt; ++i) { |
| 292 | const pjmedia_video_format_info *vfi = |
| 293 | pjmedia_get_video_format_info(NULL, vdi->fmt[i].id); |
| 294 | if (vfi) { |
| 295 | if (*formats) |
| 296 | strcat(formats, ", "); |
| 297 | strncat(formats, vfi->name, sizeof(formats)-strlen(formats)-1); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | PJ_LOG(3,(THIS_FILE, "%3d %s [%s][%s] %s", id, vdi->name, vdi->driver, |
| 302 | dirname, title)); |
| 303 | PJ_LOG(3,(THIS_FILE, " Supported capabilities: %s", capnames)); |
| 304 | PJ_LOG(3,(THIS_FILE, " Supported formats: %s", formats)); |
| 305 | } |
| 306 | |
| 307 | void vid_list_devs() |
| 308 | { |
| 309 | unsigned i, count; |
| 310 | pjmedia_vid_dev_info vdi; |
| 311 | pj_status_t status; |
| 312 | |
| 313 | PJ_LOG(3,(THIS_FILE, "Video device list:")); |
| 314 | count = pjsua_vid_dev_count(); |
| 315 | if (count == 0) { |
| 316 | PJ_LOG(3,(THIS_FILE, " - no device detected -")); |
| 317 | return; |
| 318 | } else { |
| 319 | PJ_LOG(3,(THIS_FILE, "%d device(s) detected:", count)); |
| 320 | } |
| 321 | |
| 322 | status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi); |
| 323 | if (status == PJ_SUCCESS) |
| 324 | vid_print_dev(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi, |
| 325 | "(default renderer device)"); |
| 326 | |
| 327 | status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi); |
| 328 | if (status == PJ_SUCCESS) |
| 329 | vid_print_dev(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi, |
| 330 | "(default capture device)"); |
| 331 | |
| 332 | for (i=0; i<count; ++i) { |
| 333 | status = pjsua_vid_dev_get_info(i, &vdi); |
| 334 | if (status == PJ_SUCCESS) |
| 335 | vid_print_dev(i, &vdi, ""); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void app_config_show_video(int acc_id, const pjsua_acc_config *acc_cfg) |
| 340 | { |
| 341 | PJ_LOG(3,(THIS_FILE, |
| 342 | "Account %d:\n" |
| 343 | " RX auto show: %d\n" |
| 344 | " TX auto transmit: %d\n" |
| 345 | " Capture dev: %d\n" |
| 346 | " Render dev: %d", |
| 347 | acc_id, |
| 348 | acc_cfg->vid_in_auto_show, |
| 349 | acc_cfg->vid_out_auto_transmit, |
| 350 | acc_cfg->vid_cap_dev, |
| 351 | acc_cfg->vid_rend_dev)); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | #endif |