blob: 60c7dbe12ef2533a6ec636797878e2e8277167f4 [file] [log] [blame]
Riza Sulistyobc9c6772013-04-05 03:02:19 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "pjsua_common.h"
22
23#define THIS_FILE "pjsua_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
31static char some_buf[SOME_BUF_SIZE];
32
33/** Variable definition **/
34int stdout_refresh = -1;
35pj_bool_t stdout_refresh_quit = PJ_FALSE;
36pjsua_call_id current_call = PJSUA_INVALID_ID;
37pjsua_app_config app_config;
38pjsua_call_setting call_opt;
39pjsua_msg_data msg_data;
40
41PJ_DEF(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 */
61PJ_DEF(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
84PJ_DEF(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 */
110PJ_DEF(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 */
137PJ_DEF(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 = 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
170PJ_DEF(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
190PJ_DEF(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 PJ_DEF(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 */
227PJ_DEF(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
261PJ_DEF(void) vid_print_dev(int id, const pjmedia_vid_dev_info *vdi,
262 const char *title)
263{
264 char capnames[120];
265 char formats[120];
266 const char *dirname;
267 unsigned i;
268
269 if (vdi->dir == PJMEDIA_DIR_CAPTURE_RENDER) {
270 dirname = "capture, render";
271 } else if (vdi->dir == PJMEDIA_DIR_CAPTURE) {
272 dirname = "capture";
273 } else {
274 dirname = "render";
275 }
276
277
278 capnames[0] = '\0';
279 for (i=0; i<sizeof(int)*8 && (1 << i) < PJMEDIA_VID_DEV_CAP_MAX; ++i) {
280 if (vdi->caps & (1 << i)) {
281 const char *capname = pjmedia_vid_dev_cap_name(1 << i, NULL);
282 if (capname) {
283 if (*capnames)
284 strcat(capnames, ", ");
285 strncat(capnames, capname,
286 sizeof(capnames)-strlen(capnames)-1);
287 }
288 }
289 }
290
291 formats[0] = '\0';
292 for (i=0; i<vdi->fmt_cnt; ++i) {
293 const pjmedia_video_format_info *vfi =
294 pjmedia_get_video_format_info(NULL, vdi->fmt[i].id);
295 if (vfi) {
296 if (*formats)
297 strcat(formats, ", ");
298 strncat(formats, vfi->name, sizeof(formats)-strlen(formats)-1);
299 }
300 }
301
302 PJ_LOG(3,(THIS_FILE, "%3d %s [%s][%s] %s", id, vdi->name, vdi->driver,
303 dirname, title));
304 PJ_LOG(3,(THIS_FILE, " Supported capabilities: %s", capnames));
305 PJ_LOG(3,(THIS_FILE, " Supported formats: %s", formats));
306}
307
308PJ_DEF(void) vid_list_devs()
309{
310 unsigned i, count;
311 pjmedia_vid_dev_info vdi;
312 pj_status_t status;
313
314 PJ_LOG(3,(THIS_FILE, "Video device list:"));
315 count = pjsua_vid_dev_count();
316 if (count == 0) {
317 PJ_LOG(3,(THIS_FILE, " - no device detected -"));
318 return;
319 } else {
320 PJ_LOG(3,(THIS_FILE, "%d device(s) detected:", count));
321 }
322
323 status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi);
324 if (status == PJ_SUCCESS)
325 vid_print_dev(PJMEDIA_VID_DEFAULT_RENDER_DEV, &vdi,
326 "(default renderer device)");
327
328 status = pjsua_vid_dev_get_info(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi);
329 if (status == PJ_SUCCESS)
330 vid_print_dev(PJMEDIA_VID_DEFAULT_CAPTURE_DEV, &vdi,
331 "(default capture device)");
332
333 for (i=0; i<count; ++i) {
334 status = pjsua_vid_dev_get_info(i, &vdi);
335 if (status == PJ_SUCCESS)
336 vid_print_dev(i, &vdi, "");
337 }
338}
339
340PJ_DEF(void) app_config_show_video(int acc_id, const pjsua_acc_config *acc_cfg)
341{
342 PJ_LOG(3,(THIS_FILE,
343 "Account %d:\n"
344 " RX auto show: %d\n"
345 " TX auto transmit: %d\n"
346 " Capture dev: %d\n"
347 " Render dev: %d",
348 acc_id,
349 acc_cfg->vid_in_auto_show,
350 acc_cfg->vid_out_auto_transmit,
351 acc_cfg->vid_cap_dev,
352 acc_cfg->vid_rend_dev));
353}
354
355
356#endif