Debian Codegraph @

Xpjproject/pjsip-apps/src/pjsua/pjsua_app_common.c

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