Debian Codegraph @

Xpjproject/pjsip/src/pjsua-lib/pjsua_dump.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjsua-lib/pjsua.h>
19 #include <pjsua-lib/pjsua_internal.h>
20
21 #define SNPRINTF2(s,val) pj_ansi_strxcpy(s, val, sizeof(s))
22 #define SNPRINTF3(s,fmt,val) pj_ansi_snprintf(s, sizeof(s), fmt, val)
23
24
25 const char *good_number(char *buf, unsigned buf_size, pj_int32_t val)
26 {
27 if (val < 1000) {
28 pj_ansi_snprintf(buf, buf_size, "%d", val);
29 } else if (val < 1000000) {
30 pj_ansi_snprintf(buf, buf_size, "%d.%dK",
31 val / 1000,
32 (val % 1000) / 100);
33 } else {
34 pj_ansi_snprintf(buf, buf_size, "%d.%02dM",
35 val / 1000000,
36 (val % 1000000) / 10000);
37 }
38
39 return buf;
40 }
41
42 static unsigned dump_media_stat(const char *indent,
43 char *buf, unsigned maxlen,
44 const pjmedia_rtcp_stat *stat,
45 const char *rx_info, const char *tx_info)
46 {
47 char last_update[64];
48 char packets[32], bytes[32], ipbytes[32], avg_bps[32], avg_ipbps[32];
49 pj_time_val media_duration, now;
50 char *p = buf, *end = buf+maxlen;
51 int len;
52
53 if (stat->rx.update_cnt == 0)
54 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
55 else {
56 pj_gettimeofday(&now);
57 PJ_TIME_VAL_SUB(now, stat->rx.update);
58 pj_ansi_snprintf(last_update, sizeof(last_update),
59 "%02ldh:%02ldm:%02ld.%03lds ago",
60 now.sec / 3600,
61 (now.sec % 3600) / 60,
62 now.sec % 60,
63 now.msec);
64 }
65
66 pj_gettimeofday(&media_duration);
67 PJ_TIME_VAL_SUB(media_duration, stat->start);
68 if (PJ_TIME_VAL_MSEC(media_duration) == 0)
69 media_duration.msec = 1;
70
71 len = pj_ansi_snprintf(p, end-p,
72 "%s RX %s last update:%s\n"
73 "%s total %spkt %sB (%sB +IP hdr) @avg=%sbps/%sbps\n"
74 "%s pkt loss=%d (%3.1f%%), discrd=%d (%3.1f%%), dup=%d (%2.1f%%), reord=%d (%3.1f%%)\n"
75 "%s (msec) min avg max last dev\n"
76 "%s loss period: %7.3f %7.3f %7.3f %7.3f %7.3f\n"
77 "%s jitter : %7.3f %7.3f %7.3f %7.3f %7.3f\n"
78 #if defined(PJMEDIA_RTCP_STAT_HAS_RAW_JITTER) && PJMEDIA_RTCP_STAT_HAS_RAW_JITTER!=0
79 "%s raw jitter : %7.3f %7.3f %7.3f %7.3f %7.3f\n"
80 #endif
81 #if defined(PJMEDIA_RTCP_STAT_HAS_IPDV) && PJMEDIA_RTCP_STAT_HAS_IPDV!=0
82 "%s IPDV : %7.3f %7.3f %7.3f %7.3f %7.3f\n"
83 #endif
84 "%s",
85 indent,
86 rx_info? rx_info : "",
87 last_update,
88
89 indent,
90 good_number(packets, sizeof(packets), stat->rx.pkt),
91 good_number(bytes, sizeof(bytes), stat->rx.bytes),
92 good_number(ipbytes, sizeof(ipbytes), stat->rx.bytes + stat->rx.pkt * 40),
93 good_number(avg_bps, sizeof(avg_bps), (pj_int32_t)((pj_int64_t)stat->rx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))),
94 good_number(avg_ipbps, sizeof(avg_ipbps), (pj_int32_t)(((pj_int64_t)stat->rx.bytes + stat->rx.pkt * 40) * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))),
95 indent,
96 stat->rx.loss,
97 (stat->rx.loss? stat->rx.loss * 100.0 / (stat->rx.pkt + stat->rx.loss) : 0),
98 stat->rx.discard,
99 (stat->rx.discard? stat->rx.discard * 100.0 / (stat->rx.pkt + stat->rx.loss) : 0),
100 stat->rx.dup,
101 (stat->rx.dup? stat->rx.dup * 100.0 / (stat->rx.pkt + stat->rx.loss) : 0),
102 stat->rx.reorder,
103 (stat->rx.reorder? stat->rx.reorder * 100.0 / (stat->rx.pkt + stat->rx.loss) : 0),
104 indent, indent,
105 stat->rx.loss_period.min / 1000.0,
106 stat->rx.loss_period.mean / 1000.0,
107 stat->rx.loss_period.max / 1000.0,
108 stat->rx.loss_period.last / 1000.0,
109 pj_math_stat_get_stddev(&stat->rx.loss_period) / 1000.0,
110 indent,
111 stat->rx.jitter.min / 1000.0,
112 stat->rx.jitter.mean / 1000.0,
113 stat->rx.jitter.max / 1000.0,
114 stat->rx.jitter.last / 1000.0,
115 pj_math_stat_get_stddev(&stat->rx.jitter) / 1000.0,
116 #if defined(PJMEDIA_RTCP_STAT_HAS_RAW_JITTER) && PJMEDIA_RTCP_STAT_HAS_RAW_JITTER!=0
117 indent,
118 stat->rx_raw_jitter.min / 1000.0,
119 stat->rx_raw_jitter.mean / 1000.0,
120 stat->rx_raw_jitter.max / 1000.0,
121 stat->rx_raw_jitter.last / 1000.0,
122 pj_math_stat_get_stddev(&stat->rx_raw_jitter) / 1000.0,
123 #endif
124 #if defined(PJMEDIA_RTCP_STAT_HAS_IPDV) && PJMEDIA_RTCP_STAT_HAS_IPDV!=0
125 indent,
126 stat->rx_ipdv.min / 1000.0,
127 stat->rx_ipdv.mean / 1000.0,
128 stat->rx_ipdv.max / 1000.0,
129 stat->rx_ipdv.last / 1000.0,
130 pj_math_stat_get_stddev(&stat->rx_ipdv) / 1000.0,
131 #endif
132 ""
133 );
134
135 if (len < 1 || len >= end-p) {
136 *p = '\0';
137 return (unsigned)(p-buf);
138 }
139 p += len;
140
141 if (stat->tx.update_cnt == 0)
142 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
143 else {
144 pj_gettimeofday(&now);
145 PJ_TIME_VAL_SUB(now, stat->tx.update);
146 pj_ansi_snprintf(last_update, sizeof(last_update),
147 "%02ldh:%02ldm:%02ld.%03lds ago",
148 now.sec / 3600,
149 (now.sec % 3600) / 60,
150 now.sec % 60,
151 now.msec);
152 }
153
154 len = pj_ansi_snprintf(p, end-p,
155 "%s TX %s last update:%s\n"
156 "%s total %spkt %sB (%sB +IP hdr) @avg=%sbps/%sbps\n"
157 "%s pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)\n"
158 "%s (msec) min avg max last dev \n"
159 "%s loss period: %7.3f %7.3f %7.3f %7.3f %7.3f\n"
160 "%s jitter : %7.3f %7.3f %7.3f %7.3f %7.3f\n",
161 indent,
162 tx_info,
163 last_update,
164
165 indent,
166 good_number(packets, sizeof(packets), stat->tx.pkt),
167 good_number(bytes, sizeof(bytes), stat->tx.bytes),
168 good_number(ipbytes, sizeof(ipbytes),
169 stat->tx.bytes + stat->tx.pkt * 40),
170 good_number(avg_bps, sizeof(avg_bps),
171 (pj_int32_t)((pj_int64_t)stat->tx.bytes * 8 * 1000 /
172 PJ_TIME_VAL_MSEC(media_duration))
173 ),
174 good_number(avg_ipbps, sizeof(avg_ipbps),
175 (pj_int32_t)(((pj_int64_t)stat->tx.bytes + stat->tx.pkt * 40) * 8 * 1000 /
176 PJ_TIME_VAL_MSEC(media_duration))
177 ),
178
179 indent,
180 stat->tx.loss,
181 (stat->tx.loss? stat->tx.loss * 100.0 / stat->tx.pkt : 0),
182 stat->tx.dup,
183 (stat->tx.dup? stat->tx.dup * 100.0 / stat->tx.pkt : 0),
184 stat->tx.reorder,
185 (stat->tx.reorder? stat->tx.reorder * 100.0 / stat->tx.pkt : 0),
186
187 indent, indent,
188 stat->tx.loss_period.min / 1000.0,
189 stat->tx.loss_period.mean / 1000.0,
190 stat->tx.loss_period.max / 1000.0,
191 stat->tx.loss_period.last / 1000.0,
192 pj_math_stat_get_stddev(&stat->tx.loss_period) / 1000.0,
193 indent,
194 stat->tx.jitter.min / 1000.0,
195 stat->tx.jitter.mean / 1000.0,
196 stat->tx.jitter.max / 1000.0,
197 stat->tx.jitter.last / 1000.0,
198 pj_math_stat_get_stddev(&stat->tx.jitter) / 1000.0
199 );
200
201 if (len < 1 || len >= end-p) {
202 *p = '\0';
203 return (unsigned)(p-buf);
204 }
205 p += len;
206
207 len = pj_ansi_snprintf(p, end-p,
208 "%s RTT msec : %7.3f %7.3f %7.3f %7.3f %7.3f\n",
209 indent,
210 stat->rtt.min / 1000.0,
211 stat->rtt.mean / 1000.0,
212 stat->rtt.max / 1000.0,
213 stat->rtt.last / 1000.0,
214 pj_math_stat_get_stddev(&stat->rtt) / 1000.0
215 );
216 if (len < 1 || len >= end-p) {
217 *p = '\0';
218 return (unsigned)(p-buf);
219 }
220 p += len;
221
222 return (unsigned)(p-buf);
223 }
224
225
226 /* Dump media session */
227 #if PJSUA_MEDIA_HAS_PJMEDIA || \
228 (PJSUA_THIRD_PARTY_STREAM_HAS_GET_INFO && \
229 PJSUA_THIRD_PARTY_STREAM_HAS_GET_STAT)
230
231 static void dump_media_session(const char *indent,
232 char *buf, unsigned maxlen,
233 pjsua_call *call)
234 {
235 unsigned i;
236 char *p = buf, *end = buf+maxlen;
237 int len;
238
239 for (i=0; i<call->med_cnt; ++i) {
240 pjsua_call_media *call_med = &call->media[i];
241 pjmedia_rtcp_stat stat;
242 pj_bool_t has_stat;
243 pjmedia_transport_info tp_info;
244 char rem_addr_buf[80];
245 char codec_info[32] = {'0'};
246 char rx_info[80] = {'\0'};
247 char tx_info[80] = {'\0'};
248 const char *rem_addr;
249 const char *dir_str;
250 const char *media_type_str;
251
252 switch (call_med->type) {
253 case PJMEDIA_TYPE_AUDIO:
254 media_type_str = "audio";
255 break;
256 case PJMEDIA_TYPE_VIDEO:
257 media_type_str = "video";
258 break;
259 case PJMEDIA_TYPE_TEXT:
260 media_type_str = "text";
261 break;
262 case PJMEDIA_TYPE_APPLICATION:
263 media_type_str = "application";
264 break;
265 default:
266 media_type_str = "unknown";
267 break;
268 }
269
270 /* Check if the stream is deactivated */
271 if (call_med->tp == NULL ||
272 (!call_med->strm.a.stream && !call_med->strm.v.stream &&
273 !call_med->strm.t.stream))
274 {
275 len = pj_ansi_snprintf(p, end-p,
276 "%s #%d %s deactivated\n",
277 indent, i, media_type_str);
278 if (len < 1 || len >= end-p) {
279 *p = '\0';
280 return;
281 }
282
283 p += len;
284 continue;
285 }
286
287 pjmedia_transport_info_init(&tp_info);
288 pjmedia_transport_get_info(call_med->tp, &tp_info);
289
290 // rem_addr will contain actual address of RTP originator, instead of
291 // remote RTP address specified by stream which is fetched from the SDP.
292 // Please note that we are assuming only one stream per call.
293 //rem_addr = pj_sockaddr_print(&info.stream_info[i].rem_addr,
294 // rem_addr_buf, sizeof(rem_addr_buf), 3);
295 if (pj_sockaddr_has_addr(&tp_info.src_rtp_name)) {
296 rem_addr = pj_sockaddr_print(&tp_info.src_rtp_name, rem_addr_buf,
297 sizeof(rem_addr_buf), 3);
298 } else {
299 pj_ansi_snprintf(rem_addr_buf, sizeof(rem_addr_buf), "-");
300 rem_addr = rem_addr_buf;
301 }
302
303 if (call_med->dir == PJMEDIA_DIR_NONE) {
304 /* To handle when the stream that is currently being paused
305 * (https://github.com/pjsip/pjproject/issues/1079)
306 */
307 dir_str = "inactive";
308 } else if (call_med->dir == PJMEDIA_DIR_ENCODING)
309 dir_str = "sendonly";
310 else if (call_med->dir == PJMEDIA_DIR_DECODING)
311 dir_str = "recvonly";
312 else if (call_med->dir == PJMEDIA_DIR_ENCODING_DECODING)
313 dir_str = "sendrecv";
314 else
315 dir_str = "inactive";
316
317 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
318 pjmedia_stream *stream = call_med->strm.a.stream;
319 pjmedia_stream_info info;
320
321 pjmedia_stream_get_stat(stream, &stat);
322 has_stat = PJ_TRUE;
323
324 pjmedia_stream_get_info(stream, &info);
325 pj_ansi_snprintf(codec_info, sizeof(codec_info), " %.*s @%dkHz",
326 (int)info.fmt.encoding_name.slen,
327 info.fmt.encoding_name.ptr,
328 info.fmt.clock_rate / 1000);
329 pj_ansi_snprintf(rx_info, sizeof(rx_info), "pt=%d,",
330 info.rx_pt);
331 pj_ansi_snprintf(tx_info, sizeof(tx_info), "pt=%d, ptime=%d,",
332 info.tx_pt,
333 info.param->setting.frm_per_pkt*
334 info.param->info.frm_ptime);
335
336 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
337 } else if (call_med->type == PJMEDIA_TYPE_VIDEO) {
338 pjmedia_vid_stream *stream = call_med->strm.v.stream;
339 pjmedia_vid_stream_info info;
340
341 pjmedia_vid_stream_get_stat(stream, &stat);
342 has_stat = PJ_TRUE;
343
344 pjmedia_vid_stream_get_info(stream, &info);
345 pj_ansi_snprintf(codec_info, sizeof(codec_info), " %.*s",
346 (int)info.codec_info.encoding_name.slen,
347 info.codec_info.encoding_name.ptr);
348 if (call_med->dir & PJMEDIA_DIR_DECODING) {
349 pjmedia_video_format_detail *vfd;
350 vfd = pjmedia_format_get_video_format_detail(
351 &info.codec_param->dec_fmt, PJ_TRUE);
352 pj_ansi_snprintf(rx_info, sizeof(rx_info),
353 "pt=%d, size=%dx%d, fps=%.2f,",
354 info.rx_pt,
355 vfd->size.w, vfd->size.h,
356 vfd->fps.num*1.0/vfd->fps.denum);
357 }
358 if (call_med->dir & PJMEDIA_DIR_ENCODING) {
359 pjmedia_video_format_detail *vfd;
360 vfd = pjmedia_format_get_video_format_detail(
361 &info.codec_param->enc_fmt, PJ_TRUE);
362 pj_ansi_snprintf(tx_info, sizeof(tx_info),
363 "pt=%d, size=%dx%d, fps=%.2f,",
364 info.tx_pt,
365 vfd->size.w, vfd->size.h,
366 vfd->fps.num*1.0/vfd->fps.denum);
367 }
368 #endif /* PJMEDIA_HAS_VIDEO */
369
370 } else if (call_med->type == PJMEDIA_TYPE_TEXT) {
371 pjmedia_txt_stream *stream = call_med->strm.t.stream;
372 pjmedia_txt_stream_info info;
373
374 pjmedia_stream_common_get_stat((pjmedia_stream_common *)stream,
375 &stat);
376 has_stat = PJ_TRUE;
377
378 pjmedia_txt_stream_get_info(stream, &info);
379 pj_ansi_snprintf(codec_info, sizeof(codec_info), " %.*s",
380 (int)info.fmt.encoding_name.slen,
381 info.fmt.encoding_name.ptr);
382 pj_ansi_snprintf(rx_info, sizeof(rx_info), "pt=%d, red=%d(lvl=%d)",
383 info.rx_pt, info.rx_red_pt, info.rx_red_level);
384 pj_ansi_snprintf(tx_info, sizeof(tx_info), "pt=%d, red=%d(lvl=%d),",
385 info.tx_pt, info.tx_red_pt, info.tx_red_level);
386
387 } else {
388 has_stat = PJ_FALSE;
389 }
390
391 len = pj_ansi_snprintf(p, end-p,
392 "%s #%d %s%s, %s, peer=%s\n",
393 indent,
394 call_med->idx,
395 media_type_str,
396 codec_info,
397 dir_str,
398 rem_addr);
399 if (len < 1 || len >= end-p) {
400 *p = '\0';
401 return;
402 }
403 p += len;
404
405 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
406 if (pjsua_snd_is_active()) {
407 pjmedia_echo_stat ec_stat;
408 pj_status_t status;
409
410 status = pjsua_get_ec_stat(&ec_stat);
411 if (status == PJ_SUCCESS) {
412 len = pj_ansi_snprintf(p, end-p, " %s EC stat: %.*s\n",
413 indent,
414 (int)ec_stat.stat_info.slen,
415 ec_stat.stat_info.ptr);
416 if (len < 1 || len >= end-p) {
417 *p = '\0';
418 return;
419 }
420 p += len;
421 }
422 }
423 }
424
425 /* Get and ICE SRTP status */
426 if (call_med->tp) {
427 if (tp_info.specific_info_cnt > 0) {
428 unsigned j;
429 for (j = 0; j < tp_info.specific_info_cnt; ++j) {
430 if (tp_info.spc_info[j].type == PJMEDIA_TRANSPORT_TYPE_SRTP)
431 {
432 pjmedia_srtp_info *srtp_info =
433 (pjmedia_srtp_info*) tp_info.spc_info[j].buffer;
434 const char *policy_name = srtp_info->tx_policy.name.ptr;
435
436 if (!policy_name)
437 policy_name = "";
438
439 len = pj_ansi_snprintf(p, end-p,
440 " %s SRTP status: %s Crypto-suite: %s",
441 indent,
442 (srtp_info->active?"Active":"Not active"),
443 policy_name);
444 if (len > 0 && len < end-p) {
445 p += len;
446 *p++ = '\n';
447 *p = '\0';
448 }
449 } else if (tp_info.spc_info[j].type==PJMEDIA_TRANSPORT_TYPE_ICE) {
450 const pjmedia_ice_transport_info *ii;
451 unsigned jj;
452
453 ii = (const pjmedia_ice_transport_info*)
454 tp_info.spc_info[j].buffer;
455
456 len = pj_ansi_snprintf(p, end-p,
457 " %s ICE role: %s, state: %s, comp_cnt: %u",
458 indent,
459 pj_ice_sess_role_name(ii->role),
460 pj_ice_strans_state_name(ii->sess_state),
461 ii->comp_cnt);
462 if (len > 0 && len < end-p) {
463 p += len;
464 *p++ = '\n';
465 *p = '\0';
466 }
467
468 for (jj=0; ii->sess_state==PJ_ICE_STRANS_STATE_RUNNING && jj<2; ++jj) {
469 const char *type1 = pj_ice_get_cand_type_name(ii->comp[jj].lcand_type);
470 const char *type2 = pj_ice_get_cand_type_name(ii->comp[jj].rcand_type);
471 char addr1[PJ_INET6_ADDRSTRLEN+10];
472 char addr2[PJ_INET6_ADDRSTRLEN+10];
473
474 if (pj_sockaddr_has_addr(&ii->comp[jj].lcand_addr))
475 pj_sockaddr_print(&ii->comp[jj].lcand_addr, addr1, sizeof(addr1), 3);
476 else
477 pj_ansi_strxcpy(addr1, "0.0.0.0:0", sizeof(addr1));
478 if (pj_sockaddr_has_addr(&ii->comp[jj].rcand_addr))
479 pj_sockaddr_print(&ii->comp[jj].rcand_addr, addr2, sizeof(addr2), 3);
480 else
481 pj_ansi_strxcpy(addr2, "0.0.0.0:0", sizeof(addr2));
482 len = pj_ansi_snprintf(p, end-p,
483 " %s [%d]: L:%s (%c) --> R:%s (%c)\n",
484 indent, jj,
485 addr1, type1[0],
486 addr2, type2[0]);
487 if (len > 0 && len < end-p) {
488 p += len;
489 *p = '\0';
490 }
491 }
492 }
493 }
494 }
495 }
496
497
498 if (has_stat) {
499 len = dump_media_stat(indent, p, (unsigned)(end-p), &stat,
500 rx_info, tx_info);
501 p += len;
502 }
503
504 #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
505 # define SAMPLES_TO_USEC(usec, samples, clock_rate) \
506 do { \
507 if (samples <= 4294) \
508 usec = samples * 1000000 / clock_rate; \
509 else { \
510 usec = samples * 1000 / clock_rate; \
511 usec *= 1000; \
512 } \
513 } while(0)
514
515 # define PRINT_VOIP_MTC_VAL(s, v) \
516 if (v == 127) \
517 pj_ansi_snprintf(s, sizeof(s), "(na)"); \
518 else \
519 pj_ansi_snprintf(s, sizeof(s), "%d", v)
520
521 # define VALIDATE_PRINT_BUF() \
522 if (len < 1 || len >= end-p) { *p = '\0'; return; } \
523 p += len; *p++ = '\n'; *p = '\0'
524
525
526 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
527 pjmedia_stream_info info;
528 char last_update[64];
529 char loss[16], dup[16];
530 char jitter[80];
531 char toh[80];
532 char plc[16], jba[16], jbr[16];
533 char signal_lvl[16], noise_lvl[16], rerl[16];
534 char r_factor[16], ext_r_factor[16], mos_lq[16], mos_cq[16];
535 pjmedia_rtcp_xr_stat xr_stat;
536 unsigned clock_rate;
537 pj_time_val now;
538
539 if (pjmedia_stream_get_stat_xr(call_med->strm.a.stream,
540 &xr_stat) != PJ_SUCCESS)
541 {
542 continue;
543 }
544
545 if (pjmedia_stream_get_info(call_med->strm.a.stream, &info)
546 != PJ_SUCCESS)
547 {
548 continue;
549 }
550
551 clock_rate = info.fmt.clock_rate;
552 pj_gettimeofday(&now);
553
554 len = pj_ansi_snprintf(p, end-p, "\n%s Extended reports:", indent);
555 VALIDATE_PRINT_BUF();
556
557 /* Statistics Summary */
558 len = pj_ansi_snprintf(p, end-p, "%s Statistics Summary", indent);
559 VALIDATE_PRINT_BUF();
560
561 if (xr_stat.rx.stat_sum.l)
562 SNPRINTF3(loss, "%d", xr_stat.rx.stat_sum.lost);
563 else
564 SNPRINTF2(loss, "(na)");
565
566 if (xr_stat.rx.stat_sum.d)
567 SNPRINTF3(dup, "%d", xr_stat.rx.stat_sum.dup);
568 else
569 SNPRINTF2(dup, "(na)");
570
571 if (xr_stat.rx.stat_sum.j) {
572 unsigned jmin, jmax, jmean, jdev;
573
574 SAMPLES_TO_USEC(jmin, xr_stat.rx.stat_sum.jitter.min,
575 clock_rate);
576 SAMPLES_TO_USEC(jmax, xr_stat.rx.stat_sum.jitter.max,
577 clock_rate);
578 SAMPLES_TO_USEC(jmean, xr_stat.rx.stat_sum.jitter.mean,
579 clock_rate);
580 SAMPLES_TO_USEC(jdev,
581 pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.jitter),
582 clock_rate);
583 pj_ansi_snprintf(jitter, sizeof(jitter),
584 "%7.3f %7.3f %7.3f %7.3f",
585 jmin/1000.0, jmean/1000.0, jmax/1000.0, jdev/1000.0);
586 } else
587 SNPRINTF2(jitter, "(report not available)");
588
589 if (xr_stat.rx.stat_sum.t) {
590 pj_ansi_snprintf(toh, sizeof(toh),
591 "%11d %11d %11d %11d",
592 xr_stat.rx.stat_sum.toh.min,
593 xr_stat.rx.stat_sum.toh.mean,
594 xr_stat.rx.stat_sum.toh.max,
595 pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.toh));
596 } else
597 SNPRINTF2(toh, "(report not available)");
598
599 if (xr_stat.rx.stat_sum.update.sec == 0)
600 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
601 else {
602 pj_gettimeofday(&now);
603 PJ_TIME_VAL_SUB(now, xr_stat.rx.stat_sum.update);
604 pj_ansi_snprintf(last_update, sizeof(last_update),
605 "%02ldh:%02ldm:%02ld.%03lds ago",
606 now.sec / 3600,
607 (now.sec % 3600) / 60,
608 now.sec % 60,
609 now.msec);
610 }
611
612 len = pj_ansi_snprintf(p, end-p,
613 "%s RX last update: %s\n"
614 "%s begin seq=%d, end seq=%d\n"
615 "%s pkt loss=%s, dup=%s\n"
616 "%s (msec) min avg max dev\n"
617 "%s jitter : %s\n"
618 "%s toh : %s",
619 indent, last_update,
620 indent,
621 xr_stat.rx.stat_sum.begin_seq, xr_stat.rx.stat_sum.end_seq,
622 indent, loss, dup,
623 indent,
624 indent, jitter,
625 indent, toh
626 );
627 VALIDATE_PRINT_BUF();
628
629 if (xr_stat.tx.stat_sum.l)
630 SNPRINTF3(loss, "%d", xr_stat.tx.stat_sum.lost);
631 else
632 SNPRINTF2(loss, "(na)");
633
634 if (xr_stat.tx.stat_sum.d)
635 SNPRINTF3(dup, "%d", xr_stat.tx.stat_sum.dup);
636 else
637 SNPRINTF2(dup, "(na)");
638
639 if (xr_stat.tx.stat_sum.j) {
640 unsigned jmin, jmax, jmean, jdev;
641
642 SAMPLES_TO_USEC(jmin, xr_stat.tx.stat_sum.jitter.min,
643 clock_rate);
644 SAMPLES_TO_USEC(jmax, xr_stat.tx.stat_sum.jitter.max,
645 clock_rate);
646 SAMPLES_TO_USEC(jmean, xr_stat.tx.stat_sum.jitter.mean,
647 clock_rate);
648 SAMPLES_TO_USEC(jdev,
649 pj_math_stat_get_stddev(&xr_stat.tx.stat_sum.jitter),
650 clock_rate);
651 pj_ansi_snprintf(jitter, sizeof(jitter),
652 "%7.3f %7.3f %7.3f %7.3f",
653 jmin/1000.0, jmean/1000.0, jmax/1000.0, jdev/1000.0);
654 } else
655 SNPRINTF2(jitter, "(report not available)");
656
657 if (xr_stat.tx.stat_sum.t) {
658 pj_ansi_snprintf(toh, sizeof(toh),
659 "%11d %11d %11d %11d",
660 xr_stat.tx.stat_sum.toh.min,
661 xr_stat.tx.stat_sum.toh.mean,
662 xr_stat.tx.stat_sum.toh.max,
663 pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.toh));
664 } else
665 SNPRINTF2(toh, "(report not available)");
666
667 if (xr_stat.tx.stat_sum.update.sec == 0)
668 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
669 else {
670 pj_gettimeofday(&now);
671 PJ_TIME_VAL_SUB(now, xr_stat.tx.stat_sum.update);
672 pj_ansi_snprintf(last_update, sizeof(last_update),
673 "%02ldh:%02ldm:%02ld.%03lds ago",
674 now.sec / 3600,
675 (now.sec % 3600) / 60,
676 now.sec % 60,
677 now.msec);
678 }
679
680 len = pj_ansi_snprintf(p, end-p,
681 "%s TX last update: %s\n"
682 "%s begin seq=%d, end seq=%d\n"
683 "%s pkt loss=%s, dup=%s\n"
684 "%s (msec) min avg max dev\n"
685 "%s jitter : %s\n"
686 "%s toh : %s",
687 indent, last_update,
688 indent,
689 xr_stat.tx.stat_sum.begin_seq, xr_stat.tx.stat_sum.end_seq,
690 indent, loss, dup,
691 indent,
692 indent, jitter,
693 indent, toh
694 );
695 VALIDATE_PRINT_BUF();
696
697
698 /* VoIP Metrics */
699 len = pj_ansi_snprintf(p, end-p, "%s VoIP Metrics", indent);
700 VALIDATE_PRINT_BUF();
701
702 PRINT_VOIP_MTC_VAL(signal_lvl, xr_stat.rx.voip_mtc.signal_lvl);
703 PRINT_VOIP_MTC_VAL(noise_lvl, xr_stat.rx.voip_mtc.noise_lvl);
704 PRINT_VOIP_MTC_VAL(rerl, xr_stat.rx.voip_mtc.rerl);
705 PRINT_VOIP_MTC_VAL(r_factor, xr_stat.rx.voip_mtc.r_factor);
706 PRINT_VOIP_MTC_VAL(ext_r_factor, xr_stat.rx.voip_mtc.ext_r_factor);
707 PRINT_VOIP_MTC_VAL(mos_lq, xr_stat.rx.voip_mtc.mos_lq);
708 PRINT_VOIP_MTC_VAL(mos_cq, xr_stat.rx.voip_mtc.mos_cq);
709
710 switch ((xr_stat.rx.voip_mtc.rx_config>>6) & 3) {
711 case PJMEDIA_RTCP_XR_PLC_DIS:
712 SNPRINTF2(plc, "DISABLED");
713 break;
714 case PJMEDIA_RTCP_XR_PLC_ENH:
715 SNPRINTF2(plc, "ENHANCED");
716 break;
717 case PJMEDIA_RTCP_XR_PLC_STD:
718 SNPRINTF2(plc, "STANDARD");
719 break;
720 case PJMEDIA_RTCP_XR_PLC_UNK:
721 default:
722 SNPRINTF2(plc, "UNKNOWN");
723 break;
724 }
725
726 switch ((xr_stat.rx.voip_mtc.rx_config>>4) & 3) {
727 case PJMEDIA_RTCP_XR_JB_FIXED:
728 SNPRINTF2(jba, "FIXED");
729 break;
730 case PJMEDIA_RTCP_XR_JB_ADAPTIVE:
731 SNPRINTF2(jba, "ADAPTIVE");
732 break;
733 default:
734 SNPRINTF2(jba, "UNKNOWN");
735 break;
736 }
737
738 SNPRINTF3(jbr, "%d", xr_stat.rx.voip_mtc.rx_config & 0x0F);
739
740 if (xr_stat.rx.voip_mtc.update.sec == 0)
741 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
742 else {
743 pj_gettimeofday(&now);
744 PJ_TIME_VAL_SUB(now, xr_stat.rx.voip_mtc.update);
745 pj_ansi_snprintf(last_update, sizeof(last_update),
746 "%02ldh:%02ldm:%02ld.%03lds ago",
747 now.sec / 3600,
748 (now.sec % 3600) / 60,
749 now.sec % 60,
750 now.msec);
751 }
752
753 len = pj_ansi_snprintf(p, end-p,
754 "%s RX last update: %s\n"
755 "%s packets : loss rate=%d (%.2f%%), discard rate=%d (%.2f%%)\n"
756 "%s burst : density=%d (%.2f%%), duration=%d%s\n"
757 "%s gap : density=%d (%.2f%%), duration=%d%s\n"
758 "%s delay : round trip=%d%s, end system=%d%s\n"
759 "%s level : signal=%s%s, noise=%s%s, RERL=%s%s\n"
760 "%s quality : R factor=%s, ext R factor=%s\n"
761 "%s MOS LQ=%s, MOS CQ=%s\n"
762 "%s config : PLC=%s, JB=%s, JB rate=%s, Gmin=%d\n"
763 "%s JB delay : cur=%d%s, max=%d%s, abs max=%d%s",
764 indent,
765 last_update,
766 /* packets */
767 indent,
768 xr_stat.rx.voip_mtc.loss_rate, xr_stat.rx.voip_mtc.loss_rate*100.0/256,
769 xr_stat.rx.voip_mtc.discard_rate, xr_stat.rx.voip_mtc.discard_rate*100.0/256,
770 /* burst */
771 indent,
772 xr_stat.rx.voip_mtc.burst_den, xr_stat.rx.voip_mtc.burst_den*100.0/256,
773 xr_stat.rx.voip_mtc.burst_dur, "ms",
774 /* gap */
775 indent,
776 xr_stat.rx.voip_mtc.gap_den, xr_stat.rx.voip_mtc.gap_den*100.0/256,
777 xr_stat.rx.voip_mtc.gap_dur, "ms",
778 /* delay */
779 indent,
780 xr_stat.rx.voip_mtc.rnd_trip_delay, "ms",
781 xr_stat.rx.voip_mtc.end_sys_delay, "ms",
782 /* level */
783 indent,
784 signal_lvl, "dB",
785 noise_lvl, "dB",
786 rerl, "",
787 /* quality */
788 indent,
789 r_factor, ext_r_factor,
790 indent,
791 mos_lq, mos_cq,
792 /* config */
793 indent,
794 plc, jba, jbr, xr_stat.rx.voip_mtc.gmin,
795 /* JB delay */
796 indent,
797 xr_stat.rx.voip_mtc.jb_nom, "ms",
798 xr_stat.rx.voip_mtc.jb_max, "ms",
799 xr_stat.rx.voip_mtc.jb_abs_max, "ms"
800 );
801 VALIDATE_PRINT_BUF();
802
803 PRINT_VOIP_MTC_VAL(signal_lvl, xr_stat.tx.voip_mtc.signal_lvl);
804 PRINT_VOIP_MTC_VAL(noise_lvl, xr_stat.tx.voip_mtc.noise_lvl);
805 PRINT_VOIP_MTC_VAL(rerl, xr_stat.tx.voip_mtc.rerl);
806 PRINT_VOIP_MTC_VAL(r_factor, xr_stat.tx.voip_mtc.r_factor);
807 PRINT_VOIP_MTC_VAL(ext_r_factor, xr_stat.tx.voip_mtc.ext_r_factor);
808 PRINT_VOIP_MTC_VAL(mos_lq, xr_stat.tx.voip_mtc.mos_lq);
809 PRINT_VOIP_MTC_VAL(mos_cq, xr_stat.tx.voip_mtc.mos_cq);
810
811 switch ((xr_stat.tx.voip_mtc.rx_config>>6) & 3) {
812 case PJMEDIA_RTCP_XR_PLC_DIS:
813 SNPRINTF2(plc, "DISABLED");
814 break;
815 case PJMEDIA_RTCP_XR_PLC_ENH:
816 SNPRINTF2(plc, "ENHANCED");
817 break;
818 case PJMEDIA_RTCP_XR_PLC_STD:
819 SNPRINTF2(plc, "STANDARD");
820 break;
821 case PJMEDIA_RTCP_XR_PLC_UNK:
822 default:
823 SNPRINTF2(plc, "unknown");
824 break;
825 }
826
827 switch ((xr_stat.tx.voip_mtc.rx_config>>4) & 3) {
828 case PJMEDIA_RTCP_XR_JB_FIXED:
829 SNPRINTF2(jba, "FIXED");
830 break;
831 case PJMEDIA_RTCP_XR_JB_ADAPTIVE:
832 SNPRINTF2(jba, "ADAPTIVE");
833 break;
834 default:
835 SNPRINTF2(jba, "unknown");
836 break;
837 }
838
839 SNPRINTF3(jbr, "%d", xr_stat.tx.voip_mtc.rx_config & 0x0F);
840
841 if (xr_stat.tx.voip_mtc.update.sec == 0)
842 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
843 else {
844 pj_gettimeofday(&now);
845 PJ_TIME_VAL_SUB(now, xr_stat.tx.voip_mtc.update);
846 pj_ansi_snprintf(last_update, sizeof(last_update),
847 "%02ldh:%02ldm:%02ld.%03lds ago",
848 now.sec / 3600,
849 (now.sec % 3600) / 60,
850 now.sec % 60,
851 now.msec);
852 }
853
854 len = pj_ansi_snprintf(p, end-p,
855 "%s TX last update: %s\n"
856 "%s packets : loss rate=%d (%.2f%%), discard rate=%d (%.2f%%)\n"
857 "%s burst : density=%d (%.2f%%), duration=%d%s\n"
858 "%s gap : density=%d (%.2f%%), duration=%d%s\n"
859 "%s delay : round trip=%d%s, end system=%d%s\n"
860 "%s level : signal=%s%s, noise=%s%s, RERL=%s%s\n"
861 "%s quality : R factor=%s, ext R factor=%s\n"
862 "%s MOS LQ=%s, MOS CQ=%s\n"
863 "%s config : PLC=%s, JB=%s, JB rate=%s, Gmin=%d\n"
864 "%s JB delay : cur=%d%s, max=%d%s, abs max=%d%s",
865 indent,
866 last_update,
867 /* pakcets */
868 indent,
869 xr_stat.tx.voip_mtc.loss_rate, xr_stat.tx.voip_mtc.loss_rate*100.0/256,
870 xr_stat.tx.voip_mtc.discard_rate, xr_stat.tx.voip_mtc.discard_rate*100.0/256,
871 /* burst */
872 indent,
873 xr_stat.tx.voip_mtc.burst_den, xr_stat.tx.voip_mtc.burst_den*100.0/256,
874 xr_stat.tx.voip_mtc.burst_dur, "ms",
875 /* gap */
876 indent,
877 xr_stat.tx.voip_mtc.gap_den, xr_stat.tx.voip_mtc.gap_den*100.0/256,
878 xr_stat.tx.voip_mtc.gap_dur, "ms",
879 /* delay */
880 indent,
881 xr_stat.tx.voip_mtc.rnd_trip_delay, "ms",
882 xr_stat.tx.voip_mtc.end_sys_delay, "ms",
883 /* level */
884 indent,
885 signal_lvl, "dB",
886 noise_lvl, "dB",
887 rerl, "",
888 /* quality */
889 indent,
890 r_factor, ext_r_factor,
891 indent,
892 mos_lq, mos_cq,
893 /* config */
894 indent,
895 plc, jba, jbr, xr_stat.tx.voip_mtc.gmin,
896 /* JB delay */
897 indent,
898 xr_stat.tx.voip_mtc.jb_nom, "ms",
899 xr_stat.tx.voip_mtc.jb_max, "ms",
900 xr_stat.tx.voip_mtc.jb_abs_max, "ms"
901 );
902 VALIDATE_PRINT_BUF();
903
904
905 /* RTT delay (by receiver side) */
906 len = pj_ansi_snprintf(p, end-p,
907 "%s RTT (from recv) min avg max last dev",
908 indent);
909 VALIDATE_PRINT_BUF();
910 len = pj_ansi_snprintf(p, end-p,
911 "%s RTT msec : %7.3f %7.3f %7.3f %7.3f %7.3f",
912 indent,
913 xr_stat.rtt.min / 1000.0,
914 xr_stat.rtt.mean / 1000.0,
915 xr_stat.rtt.max / 1000.0,
916 xr_stat.rtt.last / 1000.0,
917 pj_math_stat_get_stddev(&xr_stat.rtt) / 1000.0
918 );
919 VALIDATE_PRINT_BUF();
920 } /* if audio */;
921 #endif
922
923 }
924 }
925
926 #else /* PJSUA_MEDIA_HAS_PJMEDIA ||
927 (PJSUA_THIRD_PARTY_STREAM_HAS_GET_INFO &&
928 PJSUA_THIRD_PARTY_STREAM_HAS_GET_STAT) */
929
930 static void dump_media_session(const char *indent,
931 char *buf, unsigned maxlen,
932 pjsua_call *call)
933 {
934 PJ_UNUSED_ARG(indent);
935 PJ_UNUSED_ARG(buf);
936 PJ_UNUSED_ARG(maxlen);
937 PJ_UNUSED_ARG(call);
938 }
939
940 #endif /* PJSUA_MEDIA_HAS_PJMEDIA ||
941 (PJSUA_THIRD_PARTY_STREAM_HAS_GET_INFO &&
942 PJSUA_THIRD_PARTY_STREAM_HAS_GET_STAT) */
943
944
945 /* Print call info */
946 void print_call(const char *title,
947 int call_id,
948 char *buf, pj_size_t size)
949 {
950 int len;
951 pjsua_call *call = &pjsua_var.calls[call_id];
952 pjsip_inv_session *inv = call->inv;
953 pjsip_dialog *dlg;
954 char userinfo[PJSIP_MAX_URL_SIZE];
955
956 /* Dump invite sesion info. */
957
958 dlg = (inv? inv->dlg: call->async_call.dlg);
959 len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo));
960 if (len < 0)
961 pj_ansi_strxcpy(userinfo, "<--uri too long-->", sizeof(userinfo));
962 else
963 userinfo[len] = '\0';
964
965 len = pj_ansi_snprintf(buf, size, "%s[%s] %s",
966 title,
967 pjsip_inv_state_name((call->hanging_up || !inv)?
968 PJSIP_INV_STATE_DISCONNECTED:
969 inv->state),
970 userinfo);
971 if (len < 1 || len >= (int)size) {
972 pj_ansi_strxcpy(buf, "<--uri too long-->", size);
973 len = 18;
974 } else
975 buf[len] = '\0';
976 }
977
978
979 /*
980 * Dump call and media statistics to string.
981 */
982 PJ_DEF(pj_status_t) pjsua_call_dump( pjsua_call_id call_id,
983 pj_bool_t with_media,
984 char *buffer,
985 unsigned maxlen,
986 const char *indent)
987 {
988 pjsua_call *call;
989 pjsip_dialog *dlg;
990 pj_time_val duration, res_delay, con_delay;
991 char tmp[128];
992 char *p, *end;
993 pj_status_t status;
994 int len;
995
996 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
997 PJ_EINVAL);
998 PJ_ASSERT_RETURN(maxlen > 3, PJ_ETOOSMALL);
999
1000 status = acquire_call("pjsua_call_dump()", call_id, &call, &dlg);
1001 if (status != PJ_SUCCESS)
1002 return status;
1003
1004 *buffer = '\0';
1005 p = buffer;
1006 end = buffer + maxlen;
1007 len = 0;
1008
1009 print_call(indent, call_id, tmp, sizeof(tmp));
1010
1011 len = (int)pj_ansi_strlen(tmp);
1012 if (len + 3 > (int)maxlen) len = maxlen - 3;
1013 pj_memcpy(buffer, tmp, len);
1014
1015 p += len;
1016 *p++ = '\r';
1017 *p++ = '\n';
1018 *p = '\0';
1019
1020 /* Calculate call duration */
1021 if (call->conn_time.sec != 0) {
1022 pj_gettimeofday(&duration);
1023 PJ_TIME_VAL_SUB(duration, call->conn_time);
1024 con_delay = call->conn_time;
1025 PJ_TIME_VAL_SUB(con_delay, call->start_time);
1026 } else {
1027 duration.sec = duration.msec = 0;
1028 con_delay.sec = con_delay.msec = 0;
1029 }
1030
1031 /* Calculate first response delay */
1032 if (call->res_time.sec != 0) {
1033 res_delay = call->res_time;
1034 PJ_TIME_VAL_SUB(res_delay, call->start_time);
1035 } else {
1036 res_delay.sec = res_delay.msec = 0;
1037 }
1038
1039 /* Print duration */
1040 len = pj_ansi_snprintf(p, end-p,
1041 "%s Call time: %02dh:%02dm:%02ds, "
1042 "1st res in %d ms, conn in %dms",
1043 indent,
1044 (int)(duration.sec / 3600),
1045 (int)((duration.sec % 3600)/60),
1046 (int)(duration.sec % 60),
1047 (int)PJ_TIME_VAL_MSEC(res_delay),
1048 (int)PJ_TIME_VAL_MSEC(con_delay));
1049
1050 if (len > 0 && len < end-p) {
1051 p += len;
1052 *p++ = '\n';
1053 *p = '\0';
1054 }
1055
1056 /* Dump session statistics */
1057 if (with_media)
1058 dump_media_session(indent, p, (unsigned)(end-p), call);
1059
1060 pjsip_dlg_dec_lock(dlg);
1061
1062 return PJ_SUCCESS;
1063 }
1064