Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/streamutil.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
21 /**
22 * \page page_pjmedia_samples_streamutil_c Samples: Remote Streaming
23 *
24 * This example mainly demonstrates how to stream media file to remote
25 * peer using RTP.
26 *
27 * This file is pjsip-apps/src/samples/streamutil.c
28 *
29 * \includelineno streamutil.c
30 */
31
32 #include <pjlib.h>
33 #include <pjlib-util.h>
34 #include <pjmedia.h>
35 #include <pjmedia-codec.h>
36 #include <pjmedia/transport_srtp.h>
37
38 #include <stdlib.h> /* atoi() */
39 #include <stdio.h>
40
41 #include "util.h"
42
43 /* An experimental feature to add multicast option to this app for providing
44 * the capability to send IP datagrams from a single source to more than
45 * one receivers.
46 */
47 #define HAVE_MULTICAST 1
48
49 static const char *desc =
50 " streamutil \n"
51 " \n"
52 " PURPOSE: \n"
53 " Demonstrate how to use pjmedia stream component to transmit/receive \n"
54 " RTP packets to/from sound device. \n"
55 "\n"
56 "\n"
57 " USAGE: \n"
58 " streamutil [options] \n"
59 "\n"
60 "\n"
61 " Options:\n"
62 " --codec=CODEC Set the codec name. \n"
63 " --local-port=PORT Set local RTP port (default=4000) \n"
64 " --remote=IP:PORT Set the remote peer. If this option is set, \n"
65 " the program will transmit RTP audio to the \n"
66 " specified address. (default: recv only) \n"
67 #if HAVE_MULTICAST
68 " --mcast-add=IP Joins the multicast group as specified by \n"
69 " the address. Sample usage: \n"
70 " Sender: \n"
71 " streamutil --remote=[multicast_addr]:[port] \n"
72 " Receivers: \n"
73 " streamutil --local-port=[port] \n"
74 " --mcast-add=[multicast_addr] \n"
75 #endif
76 " --play-file=WAV Send audio from the WAV file instead of from \n"
77 " the sound device. \n"
78 " --record-file=WAV Record incoming audio to WAV file instead of \n"
79 " playing it to sound device. \n"
80 " --send-recv Set stream direction to bidirectional. \n"
81 " --send-only Set stream direction to send only \n"
82 " --recv-only Set stream direction to recv only (default) \n"
83
84 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
85 " --use-srtp[=NAME] Enable SRTP with crypto suite NAME \n"
86 " e.g: AES_CM_128_HMAC_SHA1_80 (default), \n"
87 " AES_CM_128_HMAC_SHA1_32 \n"
88 " Use this option along with the TX & RX keys, \n"
89 " formated of 60 hex digits (e.g: E148DA..) \n"
90 " --srtp-tx-key SRTP key for transmiting \n"
91 " --srtp-rx-key SRTP key for receiving \n"
92 " --srtp-dtls-client Use DTLS for SRTP keying, as DTLS client \n"
93 " --srtp-dtls-server Use DTLS for SRTP keying, as DTLS server \n"
94 #endif
95
96 #if defined(PJMEDIA_HAS_OPUS_CODEC) && (PJMEDIA_HAS_OPUS_CODEC != 0)
97 " --opus-pt=PT Opus payload type \n"
98 " --opus-ch=CH Opus channel count \n"
99 " --opus-clock-rate=CR Opus clock rate \n"
100 #endif
101
102 "\n"
103 ;
104
105
106
107
108 #define THIS_FILE "streamutil.c"
109
110
111
112 /* Prototype */
113 static void print_stream_stat(pjmedia_stream *stream,
114 const pjmedia_codec_param *codec_param);
115
116 /* Hexa string to octet array */
117 int my_hex_string_to_octet_string(char *raw, char *hex, int len)
118 {
119 int i;
120 for (i = 0; i < len; i+=2) {
121 int tmp;
122 if (i+1 >= len || !pj_isxdigit(hex[i]) || !pj_isxdigit(hex[i+1]))
123 return i;
124 tmp = pj_hex_digit_to_val((unsigned char)hex[i]) << 4;
125 tmp |= pj_hex_digit_to_val((unsigned char)hex[i+1]);
126 raw[i/2] = (char)(tmp & 0xFF);
127 }
128 return len;
129 }
130
131 /*
132 * Register all codecs.
133 */
134 static pj_status_t init_codecs(pjmedia_endpt *med_endpt)
135 {
136 return pjmedia_codec_register_audio_codecs(med_endpt, NULL);
137 }
138
139
140 /*
141 * Create stream based on the codec, dir, remote address, etc.
142 */
143 static pj_status_t create_stream( pj_pool_t *pool,
144 pjmedia_endpt *med_endpt,
145 const pjmedia_codec_info *codec_info,
146 pjmedia_dir dir,
147 pj_uint16_t local_port,
148 const pj_sockaddr_in *rem_addr,
149 pj_bool_t mcast,
150 const pj_sockaddr_in *mcast_addr,
151 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
152 pj_bool_t use_srtp,
153 const pj_str_t *crypto_suite,
154 const pj_str_t *srtp_tx_key,
155 const pj_str_t *srtp_rx_key,
156 pj_bool_t is_dtls_client,
157 pj_bool_t is_dtls_server,
158 #endif
159 pjmedia_stream **p_stream )
160 {
161 pjmedia_stream_info info;
162 pjmedia_transport *transport = NULL;
163 pj_status_t status;
164 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
165 pjmedia_transport *srtp_tp = NULL;
166 #endif
167
168
169 /* Reset stream info. */
170 pj_bzero(&info, sizeof(info));
171
172
173 /* Initialize stream info formats */
174 info.type = PJMEDIA_TYPE_AUDIO;
175 info.dir = dir;
176 pj_memcpy(&info.fmt, codec_info, sizeof(pjmedia_codec_info));
177 info.tx_pt = codec_info->pt;
178 info.rx_pt = codec_info->pt;
179 info.ssrc = pj_rand();
180
181 #if PJMEDIA_HAS_RTCP_XR && PJMEDIA_STREAM_ENABLE_XR
182 /* Set default RTCP XR enabled/disabled */
183 info.rtcp_xr_enabled = PJ_TRUE;
184 #endif
185
186 /* Copy remote address */
187 pj_memcpy(&info.rem_addr, rem_addr, sizeof(pj_sockaddr_in));
188
189 /* If remote address is not set, set to an arbitrary address
190 * (otherwise stream will assert).
191 */
192 if (info.rem_addr.addr.sa_family == 0) {
193 const pj_str_t addr = pj_str("127.0.0.1");
194 pj_sockaddr_in_init(&info.rem_addr.ipv4, &addr, 0);
195 }
196
197 pj_sockaddr_cp(&info.rem_rtcp, &info.rem_addr);
198 pj_sockaddr_set_port(&info.rem_rtcp,
199 pj_sockaddr_get_port(&info.rem_rtcp)+1);
200
201 if (mcast) {
202 pjmedia_sock_info si;
203 int reuse = 1;
204
205 pj_bzero(&si, sizeof(pjmedia_sock_info));
206 si.rtp_sock = si.rtcp_sock = PJ_INVALID_SOCKET;
207
208 /* Create RTP socket */
209 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0,
210 &si.rtp_sock);
211 if (status != PJ_SUCCESS)
212 return status;
213
214 status = pj_sock_setsockopt(si.rtp_sock, pj_SOL_SOCKET(),
215 pj_SO_REUSEADDR(), &reuse, sizeof(reuse));
216 if (status != PJ_SUCCESS)
217 return status;
218
219 /* Bind RTP socket */
220 status = pj_sockaddr_init(pj_AF_INET(), &si.rtp_addr_name,
221 NULL, local_port);
222 if (status != PJ_SUCCESS)
223 return status;
224
225 status = pj_sock_bind(si.rtp_sock, &si.rtp_addr_name,
226 pj_sockaddr_get_len(&si.rtp_addr_name));
227 if (status != PJ_SUCCESS)
228 return status;
229
230 /* Create RTCP socket */
231 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0,
232 &si.rtcp_sock);
233 if (status != PJ_SUCCESS)
234 return status;
235
236 status = pj_sock_setsockopt(si.rtcp_sock, pj_SOL_SOCKET(),
237 pj_SO_REUSEADDR(), &reuse, sizeof(reuse));
238 if (status != PJ_SUCCESS)
239 return status;
240
241 /* Bind RTCP socket */
242 status = pj_sockaddr_init(pj_AF_INET(), &si.rtcp_addr_name,
243 NULL, local_port+1);
244 if (status != PJ_SUCCESS)
245 return status;
246
247 status = pj_sock_bind(si.rtcp_sock, &si.rtcp_addr_name,
248 pj_sockaddr_get_len(&si.rtcp_addr_name));
249 if (status != PJ_SUCCESS)
250 return status;
251
252 #ifdef HAVE_MULTICAST
253 {
254 unsigned char loop;
255 struct pj_ip_mreq imr;
256
257 pj_memset(&imr, 0, sizeof(struct pj_ip_mreq));
258 imr.imr_multiaddr.s_addr = mcast_addr->sin_addr.s_addr;
259 imr.imr_interface.s_addr = pj_htonl(PJ_INADDR_ANY);
260 status = pj_sock_setsockopt(si.rtp_sock, pj_SOL_IP(),
261 pj_IP_ADD_MEMBERSHIP(),
262 &imr, sizeof(struct pj_ip_mreq));
263 if (status != PJ_SUCCESS)
264 return status;
265
266 status = pj_sock_setsockopt(si.rtcp_sock, pj_SOL_IP(),
267 pj_IP_ADD_MEMBERSHIP(),
268 &imr, sizeof(struct pj_ip_mreq));
269 if (status != PJ_SUCCESS)
270 return status;
271
272 /* Disable local reception of local sent packets */
273 loop = 0;
274 pj_sock_setsockopt(si.rtp_sock, pj_SOL_IP(),
275 pj_IP_MULTICAST_LOOP(), &loop, sizeof(loop));
276 pj_sock_setsockopt(si.rtcp_sock, pj_SOL_IP(),
277 pj_IP_MULTICAST_LOOP(), &loop, sizeof(loop));
278 }
279 #endif
280
281 /* Create media transport from existing sockets */
282 status = pjmedia_transport_udp_attach( med_endpt, NULL, &si,
283 PJMEDIA_UDP_NO_SRC_ADDR_CHECKING, &transport);
284 if (status != PJ_SUCCESS)
285 return status;
286
287 } else {
288 /* Create media transport */
289 status = pjmedia_transport_udp_create(med_endpt, NULL, local_port,
290 0, &transport);
291 if (status != PJ_SUCCESS)
292 return status;
293 }
294
295 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
296 /* Check if SRTP enabled */
297 if (use_srtp) {
298 status = pjmedia_transport_srtp_create(med_endpt, transport,
299 NULL, &srtp_tp);
300 if (status != PJ_SUCCESS)
301 return status;
302
303 if (is_dtls_client || is_dtls_server) {
304 char fp[128];
305 pj_size_t fp_len = sizeof(fp);
306 pjmedia_srtp_dtls_nego_param dtls_param;
307
308 pjmedia_transport_srtp_dtls_get_fingerprint(srtp_tp, "SHA-256", fp, &fp_len);
309 PJ_LOG(3, (THIS_FILE, "Local cert fingerprint: %s", fp));
310
311 pj_bzero(&dtls_param, sizeof(dtls_param));
312 pj_sockaddr_cp(&dtls_param.rem_addr, rem_addr);
313 pj_sockaddr_cp(&dtls_param.rem_rtcp, rem_addr);
314 dtls_param.is_role_active = is_dtls_client;
315
316 status = pjmedia_transport_srtp_dtls_start_nego(srtp_tp, &dtls_param);
317 if (status != PJ_SUCCESS)
318 return status;
319 } else {
320 pjmedia_srtp_crypto tx_plc, rx_plc;
321
322 pj_bzero(&tx_plc, sizeof(pjmedia_srtp_crypto));
323 pj_bzero(&rx_plc, sizeof(pjmedia_srtp_crypto));
324
325 tx_plc.key = *srtp_tx_key;
326 tx_plc.name = *crypto_suite;
327 rx_plc.key = *srtp_rx_key;
328 rx_plc.name = *crypto_suite;
329
330 status = pjmedia_transport_srtp_start(srtp_tp, &tx_plc, &rx_plc);
331 if (status != PJ_SUCCESS)
332 return status;
333 }
334
335 transport = srtp_tp;
336 }
337 #endif
338
339 /* Now that the stream info is initialized, we can create the
340 * stream.
341 */
342
343 status = pjmedia_stream_create( med_endpt, pool, &info,
344 transport,
345 NULL, p_stream);
346
347 if (status != PJ_SUCCESS) {
348 app_perror(THIS_FILE, "Error creating stream", status);
349 pjmedia_transport_close(transport);
350 return status;
351 }
352
353 /* Start media transport */
354 pjmedia_transport_media_start(transport, 0, 0, 0, 0);
355
356
357 return PJ_SUCCESS;
358 }
359
360
361 /*
362 * usage()
363 */
364 static void usage()
365 {
366 puts(desc);
367 }
368
369 /*
370 * main()
371 */
372 int main(int argc, char *argv[])
373 {
374 pj_caching_pool cp;
375 pjmedia_endpt *med_endpt;
376 pj_pool_t *pool;
377 pjmedia_port *rec_file_port = NULL, *play_file_port = NULL;
378 pjmedia_master_port *master_port = NULL;
379 pjmedia_snd_port *snd_port = NULL;
380 pjmedia_stream *stream = NULL;
381 pjmedia_port *stream_port;
382 char tmp[10];
383 char addr[PJ_INET_ADDRSTRLEN];
384 pj_status_t status;
385
386 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
387 /* SRTP variables */
388 pj_bool_t use_srtp = PJ_FALSE;
389 char tmp_tx_key[64];
390 char tmp_rx_key[64];
391 pj_str_t srtp_tx_key = {NULL, 0};
392 pj_str_t srtp_rx_key = {NULL, 0};
393 pj_str_t srtp_crypto_suite = {NULL, 0};
394 pj_bool_t is_dtls_client = PJ_FALSE;
395 pj_bool_t is_dtls_server = PJ_FALSE;
396 int tmp_key_len;
397 #endif
398
399 #if defined(PJMEDIA_HAS_OPUS_CODEC) && (PJMEDIA_HAS_OPUS_CODEC != 0)
400 int opus_pt = -1;
401 int opus_clock_rate = -1;
402 int opus_ch = -1;
403 #endif
404
405 /* Default values */
406 const pjmedia_codec_info *codec_info;
407 pjmedia_codec_param codec_param;
408 pjmedia_dir dir = PJMEDIA_DIR_DECODING;
409 pj_sockaddr_in remote_addr;
410 pj_sockaddr_in mcast_addr;
411 pj_uint16_t local_port = 4000;
412 char *codec_id = NULL;
413 char *rec_file = NULL;
414 char *play_file = NULL;
415 int mcast = 0;
416
417 enum {
418 OPT_CODEC = 'c',
419 OPT_LOCAL_PORT = 'p',
420 OPT_REMOTE = 'r',
421 OPT_MCAST = 'm',
422 OPT_PLAY_FILE = 'w',
423 OPT_RECORD_FILE = 'R',
424 OPT_SEND_RECV = 'b',
425 OPT_SEND_ONLY = 's',
426 OPT_RECV_ONLY = 'i',
427 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
428 OPT_USE_SRTP = 'S',
429 #endif
430 #if defined(PJMEDIA_HAS_OPUS_CODEC) && (PJMEDIA_HAS_OPUS_CODEC != 0)
431 OPT_OPUS_PT = 'P',
432 OPT_OPUS_CH = 'C',
433 OPT_OPUS_CLOCK_RATE = 'K',
434 #endif
435 OPT_SRTP_TX_KEY = 'x',
436 OPT_SRTP_RX_KEY = 'y',
437 OPT_SRTP_DTLS_CLIENT = 'd',
438 OPT_SRTP_DTLS_SERVER = 'D',
439 OPT_HELP = 'h',
440 };
441
442 struct pj_getopt_option long_options[] = {
443 { "codec", 1, 0, OPT_CODEC },
444 { "local-port", 1, 0, OPT_LOCAL_PORT },
445 { "remote", 1, 0, OPT_REMOTE },
446 { "mcast-add", 1, 0, OPT_MCAST },
447 { "play-file", 1, 0, OPT_PLAY_FILE },
448 { "record-file", 1, 0, OPT_RECORD_FILE },
449 { "send-recv", 0, 0, OPT_SEND_RECV },
450 { "send-only", 0, 0, OPT_SEND_ONLY },
451 { "recv-only", 0, 0, OPT_RECV_ONLY },
452 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
453 { "use-srtp", 2, 0, OPT_USE_SRTP },
454 { "srtp-tx-key", 1, 0, OPT_SRTP_TX_KEY },
455 { "srtp-rx-key", 1, 0, OPT_SRTP_RX_KEY },
456 { "srtp-dtls-client", 0, 0, OPT_SRTP_DTLS_CLIENT },
457 { "srtp-dtls-server", 0, 0, OPT_SRTP_DTLS_SERVER },
458 #endif
459 #if defined(PJMEDIA_HAS_OPUS_CODEC) && (PJMEDIA_HAS_OPUS_CODEC != 0)
460 { "opus-pt", 1, 0, OPT_OPUS_PT },
461 { "opus-ch", 1, 0, OPT_OPUS_CH },
462 { "opus-clock-rate", 1, 0, OPT_OPUS_CLOCK_RATE },
463 #endif
464 { "help", 0, 0, OPT_HELP },
465 { NULL, 0, 0, 0 },
466 };
467
468 int c;
469 int option_index;
470
471
472 pj_bzero(&remote_addr, sizeof(remote_addr));
473
474
475 /* init PJLIB : */
476 status = pj_init();
477 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
478
479
480 /* Parse arguments */
481 pj_optind = 0;
482 while((c=pj_getopt_long(argc,argv, "h", long_options, &option_index))!=-1) {
483
484 switch (c) {
485 case OPT_CODEC:
486 codec_id = pj_optarg;
487 break;
488
489 case OPT_LOCAL_PORT:
490 local_port = (pj_uint16_t) atoi(pj_optarg);
491 if (local_port < 1) {
492 printf("Error: invalid local port %s\n", pj_optarg);
493 return 1;
494 }
495 break;
496
497 case OPT_REMOTE:
498 {
499 pj_str_t ip = pj_str(strtok(pj_optarg, ":"));
500 pj_uint16_t port = (pj_uint16_t) atoi(strtok(NULL, ":"));
501
502 status = pj_sockaddr_in_init(&remote_addr, &ip, port);
503 if (status != PJ_SUCCESS) {
504 app_perror(THIS_FILE, "Invalid remote address", status);
505 return 1;
506 }
507 }
508 break;
509
510 case OPT_MCAST:
511 {
512 pj_str_t ip = pj_str(pj_optarg);
513
514 mcast = 1;
515 status = pj_sockaddr_in_init(&mcast_addr, &ip, 0);
516 if (status != PJ_SUCCESS) {
517 app_perror(THIS_FILE, "Invalid mcast address", status);
518 return 1;
519 }
520 }
521 break;
522
523 case OPT_PLAY_FILE:
524 play_file = pj_optarg;
525 break;
526
527 case OPT_RECORD_FILE:
528 rec_file = pj_optarg;
529 break;
530
531 case OPT_SEND_RECV:
532 dir = PJMEDIA_DIR_ENCODING_DECODING;
533 break;
534
535 case OPT_SEND_ONLY:
536 dir = PJMEDIA_DIR_ENCODING;
537 break;
538
539 case OPT_RECV_ONLY:
540 dir = PJMEDIA_DIR_DECODING;
541 break;
542
543 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
544 case OPT_USE_SRTP:
545 use_srtp = PJ_TRUE;
546 if (pj_optarg) {
547 pj_strset(&srtp_crypto_suite, pj_optarg, strlen(pj_optarg));
548 } else {
549 srtp_crypto_suite = pj_str("AES_CM_128_HMAC_SHA1_80");
550 }
551 break;
552
553 case OPT_SRTP_TX_KEY:
554 tmp_key_len = my_hex_string_to_octet_string(tmp_tx_key, pj_optarg,
555 (int)strlen(pj_optarg));
556 pj_strset(&srtp_tx_key, tmp_tx_key, tmp_key_len/2);
557 break;
558
559 case OPT_SRTP_RX_KEY:
560 tmp_key_len = my_hex_string_to_octet_string(tmp_rx_key, pj_optarg,
561 (int)strlen(pj_optarg));
562 pj_strset(&srtp_rx_key, tmp_rx_key, tmp_key_len/2);
563 break;
564 case OPT_SRTP_DTLS_CLIENT:
565 is_dtls_client = PJ_TRUE;
566 if (is_dtls_server) {
567 printf("Error: Cannot be as both DTLS server & client\n");
568 return 1;
569 }
570 break;
571 case OPT_SRTP_DTLS_SERVER:
572 is_dtls_server = PJ_TRUE;
573 if (is_dtls_client) {
574 printf("Error: Cannot be as both DTLS server & client\n");
575 return 1;
576 }
577 break;
578 #endif
579
580 #if defined(PJMEDIA_HAS_OPUS_CODEC) && (PJMEDIA_HAS_OPUS_CODEC != 0)
581 case OPT_OPUS_PT:
582 opus_pt = atoi(pj_optarg);
583 break;
584 case OPT_OPUS_CLOCK_RATE:
585 opus_clock_rate = atoi(pj_optarg);
586 break;
587 case OPT_OPUS_CH:
588 opus_ch = atoi(pj_optarg);
589 break;
590 #endif
591
592 case OPT_HELP:
593 usage();
594 return 1;
595
596 default:
597 printf("Invalid options %s\n", argv[pj_optind]);
598 return 1;
599 }
600
601 }
602
603
604 /* Verify arguments. */
605 if (dir & PJMEDIA_DIR_ENCODING
606 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
607 || is_dtls_client || is_dtls_server
608 #endif
609 ) {
610 if (remote_addr.sin_addr.s_addr == 0) {
611 printf("Error: remote address must be set\n");
612 return 1;
613 }
614 }
615
616 if (play_file != NULL && dir != PJMEDIA_DIR_ENCODING) {
617 printf("Direction is set to --send-only because of --play-file\n");
618 dir = PJMEDIA_DIR_ENCODING;
619 }
620
621 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
622 /* SRTP validation */
623 if (use_srtp) {
624 if (!is_dtls_client && !is_dtls_server &&
625 (!srtp_tx_key.slen || !srtp_rx_key.slen))
626 {
627 printf("Error: Key for each SRTP stream direction must be set\n");
628 return 1;
629 }
630 }
631 #endif
632
633 /* Must create a pool factory before we can allocate any memory. */
634 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
635
636 /*
637 * Initialize media endpoint.
638 * This will implicitly initialize PJMEDIA too.
639 */
640 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
641 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
642
643 /* Create memory pool for application purpose */
644 pool = pj_pool_create( &cp.factory, /* pool factory */
645 "app", /* pool name. */
646 4000, /* init size */
647 4000, /* increment size */
648 NULL /* callback on error */
649 );
650
651
652 /* Register all supported codecs */
653 status = init_codecs(med_endpt);
654 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
655
656
657 /* Find which codec to use. */
658 if (codec_id) {
659 unsigned count = 1;
660 pj_str_t str_codec_id = pj_str(codec_id);
661 pjmedia_codec_mgr *codec_mgr = pjmedia_endpt_get_codec_mgr(med_endpt);
662 status = pjmedia_codec_mgr_find_codecs_by_id( codec_mgr,
663 &str_codec_id, &count,
664 &codec_info, NULL);
665 if (status != PJ_SUCCESS) {
666 printf("Error: unable to find codec %s\n", codec_id);
667 return 1;
668 }
669 } else {
670 /* Default to pcmu */
671 pjmedia_codec_mgr_get_codec_info( pjmedia_endpt_get_codec_mgr(med_endpt),
672 0, &codec_info);
673 }
674
675 /* Create event manager */
676 status = pjmedia_event_mgr_create(pool, 0, NULL);
677 if (status != PJ_SUCCESS)
678 goto on_exit;
679
680 #if defined(PJMEDIA_HAS_OPUS_CODEC) && (PJMEDIA_HAS_OPUS_CODEC != 0)
681 /* Opus custom settings */
682 if (!pj_stricmp2(&codec_info->encoding_name, "opus")) {
683 /* Alloc temp codec info to update settings */
684 pjmedia_codec_info *tmp = PJ_POOL_ALLOC_T(pool, pjmedia_codec_info);
685 pj_memcpy(tmp, codec_info, sizeof(pjmedia_codec_info));
686
687 if (opus_pt > 0)
688 tmp->pt = opus_pt;
689 if (opus_clock_rate > 0)
690 tmp->clock_rate = opus_clock_rate;
691 if (opus_ch > 0)
692 tmp->channel_cnt = opus_ch;
693
694 /* Use the temp codec info */
695 codec_info = tmp;
696 }
697 #endif
698
699 /* Create stream based on program arguments */
700 status = create_stream(pool, med_endpt, codec_info, dir, local_port,
701 &remote_addr, mcast, &mcast_addr,
702 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
703 use_srtp, &srtp_crypto_suite,
704 &srtp_tx_key, &srtp_rx_key,
705 is_dtls_client, is_dtls_server,
706 #endif
707 &stream);
708 if (status != PJ_SUCCESS)
709 goto on_exit;
710
711 /* Get codec default param for info */
712 status = pjmedia_codec_mgr_get_default_param(
713 pjmedia_endpt_get_codec_mgr(med_endpt),
714 codec_info,
715 &codec_param);
716 /* Should be ok, as create_stream() above succeeded */
717 pj_assert(status == PJ_SUCCESS);
718
719 /* Get the port interface of the stream */
720 status = pjmedia_stream_get_port( stream, &stream_port);
721 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
722
723
724 if (play_file) {
725 unsigned wav_ptime;
726
727 wav_ptime = PJMEDIA_PIA_PTIME(&stream_port->info);
728 status = pjmedia_wav_player_port_create(pool, play_file, wav_ptime,
729 0, -1, &play_file_port);
730 if (status != PJ_SUCCESS) {
731 app_perror(THIS_FILE, "Unable to use file", status);
732 goto on_exit;
733 }
734
735 status = pjmedia_master_port_create(pool, play_file_port, stream_port,
736 0, &master_port);
737 if (status != PJ_SUCCESS) {
738 app_perror(THIS_FILE, "Unable to create master port", status);
739 goto on_exit;
740 }
741
742 status = pjmedia_master_port_start(master_port);
743 if (status != PJ_SUCCESS) {
744 app_perror(THIS_FILE, "Error starting master port", status);
745 goto on_exit;
746 }
747
748 printf("Playing from WAV file %s..\n", play_file);
749
750 } else if (rec_file) {
751
752 status = pjmedia_wav_writer_port_create(pool, rec_file,
753 PJMEDIA_PIA_SRATE(&stream_port->info),
754 PJMEDIA_PIA_CCNT(&stream_port->info),
755 PJMEDIA_PIA_SPF(&stream_port->info),
756 PJMEDIA_PIA_BITS(&stream_port->info),
757 0, 0, &rec_file_port);
758 if (status != PJ_SUCCESS) {
759 app_perror(THIS_FILE, "Unable to use file", status);
760 goto on_exit;
761 }
762
763 status = pjmedia_master_port_create(pool, stream_port, rec_file_port,
764 0, &master_port);
765 if (status != PJ_SUCCESS) {
766 app_perror(THIS_FILE, "Unable to create master port", status);
767 goto on_exit;
768 }
769
770 status = pjmedia_master_port_start(master_port);
771 if (status != PJ_SUCCESS) {
772 app_perror(THIS_FILE, "Error starting master port", status);
773 goto on_exit;
774 }
775
776 printf("Recording to WAV file %s..\n", rec_file);
777
778 } else {
779
780 /* Create sound device port. */
781 if (dir == PJMEDIA_DIR_ENCODING_DECODING)
782 status = pjmedia_snd_port_create(pool, -1, -1,
783 PJMEDIA_PIA_SRATE(&stream_port->info),
784 PJMEDIA_PIA_CCNT(&stream_port->info),
785 PJMEDIA_PIA_SPF(&stream_port->info),
786 PJMEDIA_PIA_BITS(&stream_port->info),
787 0, &snd_port);
788 else if (dir == PJMEDIA_DIR_ENCODING)
789 status = pjmedia_snd_port_create_rec(pool, -1,
790 PJMEDIA_PIA_SRATE(&stream_port->info),
791 PJMEDIA_PIA_CCNT(&stream_port->info),
792 PJMEDIA_PIA_SPF(&stream_port->info),
793 PJMEDIA_PIA_BITS(&stream_port->info),
794 0, &snd_port);
795 else
796 status = pjmedia_snd_port_create_player(pool, -1,
797 PJMEDIA_PIA_SRATE(&stream_port->info),
798 PJMEDIA_PIA_CCNT(&stream_port->info),
799 PJMEDIA_PIA_SPF(&stream_port->info),
800 PJMEDIA_PIA_BITS(&stream_port->info),
801 0, &snd_port);
802
803
804 if (status != PJ_SUCCESS) {
805 app_perror(THIS_FILE, "Unable to create sound port", status);
806 goto on_exit;
807 }
808
809 /* Connect sound port to stream */
810 status = pjmedia_snd_port_connect( snd_port, stream_port );
811 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
812
813 }
814
815 /* Start streaming */
816 pjmedia_stream_start(stream);
817
818
819 /* Done */
820
821 if (dir == PJMEDIA_DIR_DECODING)
822 printf("Stream is active, dir is recv-only, local port is %d\n",
823 local_port);
824 else if (dir == PJMEDIA_DIR_ENCODING)
825 printf("Stream is active, dir is send-only, sending to %s:%d\n",
826 pj_inet_ntop2(pj_AF_INET(), &remote_addr.sin_addr, addr,
827 sizeof(addr)),
828 pj_ntohs(remote_addr.sin_port));
829 else
830 printf("Stream is active, send/recv, local port is %d, "
831 "sending to %s:%d\n",
832 local_port,
833 pj_inet_ntop2(pj_AF_INET(), &remote_addr.sin_addr, addr,
834 sizeof(addr)),
835 pj_ntohs(remote_addr.sin_port));
836
837
838 for (;;) {
839
840 puts("");
841 puts("Commands:");
842 puts(" s Display media statistics");
843 puts(" q Quit");
844 puts("");
845
846 printf("Command: "); fflush(stdout);
847
848 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
849 puts("EOF while reading stdin, will quit now..");
850 break;
851 }
852
853 if (tmp[0] == 's')
854 print_stream_stat(stream, &codec_param);
855 else if (tmp[0] == 'q')
856 break;
857
858 }
859
860 /* Start deinitialization: */
861 on_exit:
862
863 /* Destroy sound device */
864 if (snd_port) {
865 pjmedia_snd_port_destroy( snd_port );
866 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
867 }
868
869 /* Destroy master port */
870 if (master_port) {
871 pjmedia_master_port_destroy(master_port, PJ_FALSE);
872 }
873
874 /* Destroy stream */
875 if (stream) {
876 pjmedia_transport *tp;
877
878 tp = pjmedia_stream_get_transport(stream);
879 pjmedia_stream_destroy(stream);
880
881 pjmedia_transport_media_stop(tp);
882 pjmedia_transport_close(tp);
883 }
884
885 /* Destroy file ports */
886 if (play_file_port)
887 pjmedia_port_destroy( play_file_port );
888 if (rec_file_port)
889 pjmedia_port_destroy( rec_file_port );
890
891 /* Destroy event manager */
892 pjmedia_event_mgr_destroy(NULL);
893
894 /* Release application pool */
895 pj_pool_release( pool );
896
897 /* Destroy media endpoint. */
898 pjmedia_endpt_destroy( med_endpt );
899
900 /* Destroy pool factory */
901 pj_caching_pool_destroy( &cp );
902
903 /* Shutdown PJLIB */
904 pj_shutdown();
905
906
907 return (status == PJ_SUCCESS) ? 0 : 1;
908 }
909
910
911
912
913 static const char *good_number(char *buf, unsigned buf_size, pj_int32_t val)
914 {
915 if (val < 1000) {
916 pj_ansi_snprintf(buf, buf_size, "%d", val);
917 } else if (val < 1000000) {
918 pj_ansi_snprintf(buf, buf_size, "%d.%dK",
919 val / 1000,
920 (val % 1000) / 100);
921 } else {
922 pj_ansi_snprintf(buf, buf_size, "%d.%02dM",
923 val / 1000000,
924 (val % 1000000) / 10000);
925 }
926
927 return buf;
928 }
929
930
931 #define SAMPLES_TO_USEC(usec, samples, clock_rate) \
932 do { \
933 if (samples <= 4294) \
934 usec = samples * 1000000 / clock_rate; \
935 else { \
936 usec = samples * 1000 / clock_rate; \
937 usec *= 1000; \
938 } \
939 } while(0)
940
941 #define PRINT_VOIP_MTC_VAL(s, v) \
942 if (v == 127) \
943 snprintf(s, sizeof(s), "(na)"); \
944 else \
945 snprintf(s, sizeof(s), "%d", v)
946
947
948 /*
949 * Print stream statistics
950 */
951 static void print_stream_stat(pjmedia_stream *stream,
952 const pjmedia_codec_param *codec_param)
953 {
954 char duration[80], last_update[80];
955 char bps[16], ipbps[16], packets[16], bytes[16], ipbytes[16];
956 pjmedia_port *port;
957 pjmedia_rtcp_stat stat;
958 pj_time_val now;
959
960 #define SNPRINTF2(s,val) pj_ansi_strxcpy(s, val, sizeof(s))
961 #define SNPRINTF3(s,fmt,val) snprintf(s, sizeof(s), fmt, val)
962
963 pj_gettimeofday(&now);
964 pjmedia_stream_get_stat(stream, &stat);
965 pjmedia_stream_get_port(stream, &port);
966
967 puts("Stream statistics:");
968
969 /* Print duration */
970 PJ_TIME_VAL_SUB(now, stat.start);
971 snprintf(duration, sizeof(duration),
972 " Duration: %02ld:%02ld:%02ld.%03ld",
973 now.sec / 3600,
974 (now.sec % 3600) / 60,
975 (now.sec % 60),
976 now.msec);
977
978
979 printf(" Info: audio %dHz, %dms/frame, %sB/s (%sB/s +IP hdr)\n",
980 PJMEDIA_PIA_SRATE(&port->info),
981 PJMEDIA_PIA_PTIME(&port->info),
982 good_number(bps, sizeof(bps), (codec_param->info.avg_bps+7)/8),
983 good_number(ipbps, sizeof(ipbps), ((codec_param->info.avg_bps+7)/8) +
984 (40 * 1000 /
985 codec_param->setting.frm_per_pkt /
986 codec_param->info.frm_ptime)));
987
988 if (stat.rx.update_cnt == 0)
989 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
990 else {
991 pj_gettimeofday(&now);
992 PJ_TIME_VAL_SUB(now, stat.rx.update);
993 pj_ansi_snprintf(last_update, sizeof(last_update),
994 "%02ldh:%02ldm:%02ld.%03lds ago",
995 now.sec / 3600,
996 (now.sec % 3600) / 60,
997 now.sec % 60,
998 now.msec);
999 }
1000
1001 printf(" RX stat last update: %s\n"
1002 " total %s packets %sB received (%sB +IP hdr)%s\n"
1003 " pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)%s\n"
1004 " (msec) min avg max last dev\n"
1005 " loss period: %7.3f %7.3f %7.3f %7.3f %7.3f%s\n"
1006 " jitter : %7.3f %7.3f %7.3f %7.3f %7.3f%s\n",
1007 last_update,
1008 good_number(packets, sizeof(packets), stat.rx.pkt),
1009 good_number(bytes, sizeof(bytes), stat.rx.bytes),
1010 good_number(ipbytes, sizeof(ipbytes), stat.rx.bytes + stat.rx.pkt * 32),
1011 "",
1012 stat.rx.loss,
1013 stat.rx.loss * 100.0 / (stat.rx.pkt + stat.rx.loss),
1014 stat.rx.dup,
1015 stat.rx.dup * 100.0 / (stat.rx.pkt + stat.rx.loss),
1016 stat.rx.reorder,
1017 stat.rx.reorder * 100.0 / (stat.rx.pkt + stat.rx.loss),
1018 "",
1019 stat.rx.loss_period.min / 1000.0,
1020 stat.rx.loss_period.mean / 1000.0,
1021 stat.rx.loss_period.max / 1000.0,
1022 stat.rx.loss_period.last / 1000.0,
1023 pj_math_stat_get_stddev(&stat.rx.loss_period) / 1000.0,
1024 "",
1025 stat.rx.jitter.min / 1000.0,
1026 stat.rx.jitter.mean / 1000.0,
1027 stat.rx.jitter.max / 1000.0,
1028 stat.rx.jitter.last / 1000.0,
1029 pj_math_stat_get_stddev(&stat.rx.jitter) / 1000.0,
1030 ""
1031 );
1032
1033
1034 if (stat.tx.update_cnt == 0)
1035 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
1036 else {
1037 pj_gettimeofday(&now);
1038 PJ_TIME_VAL_SUB(now, stat.tx.update);
1039 pj_ansi_snprintf(last_update, sizeof(last_update),
1040 "%02ldh:%02ldm:%02ld.%03lds ago",
1041 now.sec / 3600,
1042 (now.sec % 3600) / 60,
1043 now.sec % 60,
1044 now.msec);
1045 }
1046
1047 printf(" TX stat last update: %s\n"
1048 " total %s packets %sB sent (%sB +IP hdr)%s\n"
1049 " pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)%s\n"
1050 " (msec) min avg max last dev\n"
1051 " loss period: %7.3f %7.3f %7.3f %7.3f %7.3f%s\n"
1052 " jitter : %7.3f %7.3f %7.3f %7.3f %7.3f%s\n",
1053 last_update,
1054 good_number(packets, sizeof(packets), stat.tx.pkt),
1055 good_number(bytes, sizeof(bytes), stat.tx.bytes),
1056 good_number(ipbytes, sizeof(ipbytes), stat.tx.bytes + stat.tx.pkt * 32),
1057 "",
1058 stat.tx.loss,
1059 stat.tx.loss * 100.0 / (stat.tx.pkt),
1060 stat.tx.dup,
1061 stat.tx.dup * 100.0 / (stat.tx.pkt),
1062 stat.tx.reorder,
1063 stat.tx.reorder * 100.0 / (stat.tx.pkt),
1064 "",
1065 stat.tx.loss_period.min / 1000.0,
1066 stat.tx.loss_period.mean / 1000.0,
1067 stat.tx.loss_period.max / 1000.0,
1068 stat.tx.loss_period.last / 1000.0,
1069 pj_math_stat_get_stddev(&stat.tx.loss_period) / 1000.0,
1070 "",
1071 stat.tx.jitter.min / 1000.0,
1072 stat.tx.jitter.mean / 1000.0,
1073 stat.tx.jitter.max / 1000.0,
1074 stat.tx.jitter.last / 1000.0,
1075 pj_math_stat_get_stddev(&stat.tx.jitter) / 1000.0,
1076 ""
1077 );
1078
1079
1080 printf(" RTT delay : %7.3f %7.3f %7.3f %7.3f %7.3f%s\n",
1081 stat.rtt.min / 1000.0,
1082 stat.rtt.mean / 1000.0,
1083 stat.rtt.max / 1000.0,
1084 stat.rtt.last / 1000.0,
1085 pj_math_stat_get_stddev(&stat.rtt) / 1000.0,
1086 ""
1087 );
1088
1089 #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
1090 /* RTCP XR Reports */
1091 do {
1092 char loss[16], dup[16];
1093 char jitter[80];
1094 char toh[80];
1095 char plc[16], jba[16], jbr[16];
1096 char signal_lvl[16], noise_lvl[16], rerl[16];
1097 char r_factor[16], ext_r_factor[16], mos_lq[16], mos_cq[16];
1098 pjmedia_rtcp_xr_stat xr_stat;
1099
1100 if (pjmedia_stream_get_stat_xr(stream, &xr_stat) != PJ_SUCCESS)
1101 break;
1102
1103 puts("\nExtended reports:");
1104
1105 /* Statistics Summary */
1106 puts(" Statistics Summary");
1107
1108 if (xr_stat.rx.stat_sum.l)
1109 SNPRINTF3(loss, "%d", xr_stat.rx.stat_sum.lost);
1110 else
1111 SNPRINTF2(loss, "(na)");
1112
1113 if (xr_stat.rx.stat_sum.d)
1114 SNPRINTF3(dup, "%d", xr_stat.rx.stat_sum.dup);
1115 else
1116 SNPRINTF2(dup, "(na)");
1117
1118 if (xr_stat.rx.stat_sum.j) {
1119 unsigned jmin, jmax, jmean, jdev;
1120
1121 SAMPLES_TO_USEC(jmin, xr_stat.rx.stat_sum.jitter.min,
1122 port->info.fmt.det.aud.clock_rate);
1123 SAMPLES_TO_USEC(jmax, xr_stat.rx.stat_sum.jitter.max,
1124 port->info.fmt.det.aud.clock_rate);
1125 SAMPLES_TO_USEC(jmean, xr_stat.rx.stat_sum.jitter.mean,
1126 port->info.fmt.det.aud.clock_rate);
1127 SAMPLES_TO_USEC(jdev,
1128 pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.jitter),
1129 port->info.fmt.det.aud.clock_rate);
1130 snprintf(jitter, sizeof(jitter), "%7.3f %7.3f %7.3f %7.3f",
1131 jmin/1000.0, jmean/1000.0, jmax/1000.0, jdev/1000.0);
1132 } else
1133 SNPRINTF2(jitter, "(report not available)");
1134
1135 if (xr_stat.rx.stat_sum.t) {
1136 snprintf(toh, sizeof(toh), "%11d %11d %11d %11d",
1137 xr_stat.rx.stat_sum.toh.min,
1138 xr_stat.rx.stat_sum.toh.mean,
1139 xr_stat.rx.stat_sum.toh.max,
1140 pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.toh));
1141 } else
1142 SNPRINTF2(toh, "(report not available)");
1143
1144 if (xr_stat.rx.stat_sum.update.sec == 0)
1145 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
1146 else {
1147 pj_gettimeofday(&now);
1148 PJ_TIME_VAL_SUB(now, xr_stat.rx.stat_sum.update);
1149 pj_ansi_snprintf(last_update, sizeof(last_update),
1150 "%02ldh:%02ldm:%02ld.%03lds ago",
1151 now.sec / 3600,
1152 (now.sec % 3600) / 60,
1153 now.sec % 60,
1154 now.msec);
1155 }
1156
1157 printf(" RX last update: %s\n"
1158 " begin seq=%d, end seq=%d%s\n"
1159 " pkt loss=%s, dup=%s%s\n"
1160 " (msec) min avg max dev\n"
1161 " jitter : %s\n"
1162 " toh : %s\n",
1163 last_update,
1164 xr_stat.rx.stat_sum.begin_seq, xr_stat.rx.stat_sum.end_seq,
1165 "",
1166 loss, dup,
1167 "",
1168 jitter,
1169 toh
1170 );
1171
1172 if (xr_stat.tx.stat_sum.l)
1173 SNPRINTF3(loss, "%d", xr_stat.tx.stat_sum.lost);
1174 else
1175 SNPRINTF2(loss, "(na)");
1176
1177 if (xr_stat.tx.stat_sum.d)
1178 SNPRINTF3(dup, "%d", xr_stat.tx.stat_sum.dup);
1179 else
1180 SNPRINTF2(dup, "(na)");
1181
1182 if (xr_stat.tx.stat_sum.j) {
1183 unsigned jmin, jmax, jmean, jdev;
1184
1185 SAMPLES_TO_USEC(jmin, xr_stat.tx.stat_sum.jitter.min,
1186 port->info.fmt.det.aud.clock_rate);
1187 SAMPLES_TO_USEC(jmax, xr_stat.tx.stat_sum.jitter.max,
1188 port->info.fmt.det.aud.clock_rate);
1189 SAMPLES_TO_USEC(jmean, xr_stat.tx.stat_sum.jitter.mean,
1190 port->info.fmt.det.aud.clock_rate);
1191 SAMPLES_TO_USEC(jdev,
1192 pj_math_stat_get_stddev(&xr_stat.tx.stat_sum.jitter),
1193 port->info.fmt.det.aud.clock_rate);
1194 snprintf(jitter, sizeof(jitter), "%7.3f %7.3f %7.3f %7.3f",
1195 jmin/1000.0, jmean/1000.0, jmax/1000.0, jdev/1000.0);
1196 } else
1197 SNPRINTF2(jitter, "(report not available)");
1198
1199 if (xr_stat.tx.stat_sum.t) {
1200 snprintf(toh, sizeof(toh), "%11d %11d %11d %11d",
1201 xr_stat.tx.stat_sum.toh.min,
1202 xr_stat.tx.stat_sum.toh.mean,
1203 xr_stat.tx.stat_sum.toh.max,
1204 pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.toh));
1205 } else
1206 SNPRINTF2(toh, "(report not available)");
1207
1208 if (xr_stat.tx.stat_sum.update.sec == 0)
1209 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
1210 else {
1211 pj_gettimeofday(&now);
1212 PJ_TIME_VAL_SUB(now, xr_stat.tx.stat_sum.update);
1213 pj_ansi_snprintf(last_update, sizeof(last_update),
1214 "%02ldh:%02ldm:%02ld.%03lds ago",
1215 now.sec / 3600,
1216 (now.sec % 3600) / 60,
1217 now.sec % 60,
1218 now.msec);
1219 }
1220
1221 printf(" TX last update: %s\n"
1222 " begin seq=%d, end seq=%d%s\n"
1223 " pkt loss=%s, dup=%s%s\n"
1224 " (msec) min avg max dev\n"
1225 " jitter : %s\n"
1226 " toh : %s\n",
1227 last_update,
1228 xr_stat.tx.stat_sum.begin_seq, xr_stat.tx.stat_sum.end_seq,
1229 "",
1230 loss, dup,
1231 "",
1232 jitter,
1233 toh
1234 );
1235
1236 /* VoIP Metrics */
1237 puts(" VoIP Metrics");
1238
1239 PRINT_VOIP_MTC_VAL(signal_lvl, xr_stat.rx.voip_mtc.signal_lvl);
1240 PRINT_VOIP_MTC_VAL(noise_lvl, xr_stat.rx.voip_mtc.noise_lvl);
1241 PRINT_VOIP_MTC_VAL(rerl, xr_stat.rx.voip_mtc.rerl);
1242 PRINT_VOIP_MTC_VAL(r_factor, xr_stat.rx.voip_mtc.r_factor);
1243 PRINT_VOIP_MTC_VAL(ext_r_factor, xr_stat.rx.voip_mtc.ext_r_factor);
1244 PRINT_VOIP_MTC_VAL(mos_lq, xr_stat.rx.voip_mtc.mos_lq);
1245 PRINT_VOIP_MTC_VAL(mos_cq, xr_stat.rx.voip_mtc.mos_cq);
1246
1247 switch ((xr_stat.rx.voip_mtc.rx_config>>6) & 3) {
1248 case PJMEDIA_RTCP_XR_PLC_DIS:
1249 SNPRINTF2(plc, "DISABLED");
1250 break;
1251 case PJMEDIA_RTCP_XR_PLC_ENH:
1252 SNPRINTF2(plc, "ENHANCED");
1253 break;
1254 case PJMEDIA_RTCP_XR_PLC_STD:
1255 SNPRINTF2(plc, "STANDARD");
1256 break;
1257 case PJMEDIA_RTCP_XR_PLC_UNK:
1258 default:
1259 SNPRINTF2(plc, "UNKNOWN");
1260 break;
1261 }
1262
1263 switch ((xr_stat.rx.voip_mtc.rx_config>>4) & 3) {
1264 case PJMEDIA_RTCP_XR_JB_FIXED:
1265 SNPRINTF2(jba, "FIXED");
1266 break;
1267 case PJMEDIA_RTCP_XR_JB_ADAPTIVE:
1268 SNPRINTF2(jba, "ADAPTIVE");
1269 break;
1270 default:
1271 SNPRINTF2(jba, "UNKNOWN");
1272 break;
1273 }
1274
1275 SNPRINTF3(jbr, "%d", xr_stat.rx.voip_mtc.rx_config & 0x0F);
1276
1277 if (xr_stat.rx.voip_mtc.update.sec == 0)
1278 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
1279 else {
1280 pj_gettimeofday(&now);
1281 PJ_TIME_VAL_SUB(now, xr_stat.rx.voip_mtc.update);
1282 pj_ansi_snprintf(last_update, sizeof(last_update),
1283 "%02ldh:%02ldm:%02ld.%03lds ago",
1284 now.sec / 3600,
1285 (now.sec % 3600) / 60,
1286 now.sec % 60,
1287 now.msec);
1288 }
1289
1290 printf(" RX last update: %s\n"
1291 " packets : loss rate=%d (%.2f%%), discard rate=%d (%.2f%%)\n"
1292 " burst : density=%d (%.2f%%), duration=%d%s\n"
1293 " gap : density=%d (%.2f%%), duration=%d%s\n"
1294 " delay : round trip=%d%s, end system=%d%s\n"
1295 " level : signal=%s%s, noise=%s%s, RERL=%s%s\n"
1296 " quality : R factor=%s, ext R factor=%s\n"
1297 " MOS LQ=%s, MOS CQ=%s\n"
1298 " config : PLC=%s, JB=%s, JB rate=%s, Gmin=%d\n"
1299 " JB delay : cur=%d%s, max=%d%s, abs max=%d%s\n",
1300 last_update,
1301 /* pakcets */
1302 xr_stat.rx.voip_mtc.loss_rate, xr_stat.rx.voip_mtc.loss_rate*100.0/256,
1303 xr_stat.rx.voip_mtc.discard_rate, xr_stat.rx.voip_mtc.discard_rate*100.0/256,
1304 /* burst */
1305 xr_stat.rx.voip_mtc.burst_den, xr_stat.rx.voip_mtc.burst_den*100.0/256,
1306 xr_stat.rx.voip_mtc.burst_dur, "ms",
1307 /* gap */
1308 xr_stat.rx.voip_mtc.gap_den, xr_stat.rx.voip_mtc.gap_den*100.0/256,
1309 xr_stat.rx.voip_mtc.gap_dur, "ms",
1310 /* delay */
1311 xr_stat.rx.voip_mtc.rnd_trip_delay, "ms",
1312 xr_stat.rx.voip_mtc.end_sys_delay, "ms",
1313 /* level */
1314 signal_lvl, "dB",
1315 noise_lvl, "dB",
1316 rerl, "",
1317 /* quality */
1318 r_factor, ext_r_factor, mos_lq, mos_cq,
1319 /* config */
1320 plc, jba, jbr, xr_stat.rx.voip_mtc.gmin,
1321 /* JB delay */
1322 xr_stat.rx.voip_mtc.jb_nom, "ms",
1323 xr_stat.rx.voip_mtc.jb_max, "ms",
1324 xr_stat.rx.voip_mtc.jb_abs_max, "ms"
1325 );
1326
1327 PRINT_VOIP_MTC_VAL(signal_lvl, xr_stat.tx.voip_mtc.signal_lvl);
1328 PRINT_VOIP_MTC_VAL(noise_lvl, xr_stat.tx.voip_mtc.noise_lvl);
1329 PRINT_VOIP_MTC_VAL(rerl, xr_stat.tx.voip_mtc.rerl);
1330 PRINT_VOIP_MTC_VAL(r_factor, xr_stat.tx.voip_mtc.r_factor);
1331 PRINT_VOIP_MTC_VAL(ext_r_factor, xr_stat.tx.voip_mtc.ext_r_factor);
1332 PRINT_VOIP_MTC_VAL(mos_lq, xr_stat.tx.voip_mtc.mos_lq);
1333 PRINT_VOIP_MTC_VAL(mos_cq, xr_stat.tx.voip_mtc.mos_cq);
1334
1335 switch ((xr_stat.tx.voip_mtc.rx_config>>6) & 3) {
1336 case PJMEDIA_RTCP_XR_PLC_DIS:
1337 SNPRINTF2(plc, "DISABLED");
1338 break;
1339 case PJMEDIA_RTCP_XR_PLC_ENH:
1340 SNPRINTF2(plc, "ENHANCED");
1341 break;
1342 case PJMEDIA_RTCP_XR_PLC_STD:
1343 SNPRINTF2(plc, "STANDARD");
1344 break;
1345 case PJMEDIA_RTCP_XR_PLC_UNK:
1346 default:
1347 SNPRINTF2(plc, "unknown");
1348 break;
1349 }
1350
1351 switch ((xr_stat.tx.voip_mtc.rx_config>>4) & 3) {
1352 case PJMEDIA_RTCP_XR_JB_FIXED:
1353 SNPRINTF2(jba, "FIXED");
1354 break;
1355 case PJMEDIA_RTCP_XR_JB_ADAPTIVE:
1356 SNPRINTF2(jba, "ADAPTIVE");
1357 break;
1358 default:
1359 SNPRINTF2(jba, "unknown");
1360 break;
1361 }
1362
1363 SNPRINTF3(jbr, "%d", xr_stat.tx.voip_mtc.rx_config & 0x0F);
1364
1365 if (xr_stat.tx.voip_mtc.update.sec == 0)
1366 pj_ansi_strxcpy(last_update, "never", sizeof(last_update));
1367 else {
1368 pj_gettimeofday(&now);
1369 PJ_TIME_VAL_SUB(now, xr_stat.tx.voip_mtc.update);
1370 pj_ansi_snprintf(last_update, sizeof(last_update),
1371 "%02ldh:%02ldm:%02ld.%03lds ago",
1372 now.sec / 3600,
1373 (now.sec % 3600) / 60,
1374 now.sec % 60,
1375 now.msec);
1376 }
1377
1378 printf(" TX last update: %s\n"
1379 " packets : loss rate=%d (%.2f%%), discard rate=%d (%.2f%%)\n"
1380 " burst : density=%d (%.2f%%), duration=%d%s\n"
1381 " gap : density=%d (%.2f%%), duration=%d%s\n"
1382 " delay : round trip=%d%s, end system=%d%s\n"
1383 " level : signal=%s%s, noise=%s%s, RERL=%s%s\n"
1384 " quality : R factor=%s, ext R factor=%s\n"
1385 " MOS LQ=%s, MOS CQ=%s\n"
1386 " config : PLC=%s, JB=%s, JB rate=%s, Gmin=%d\n"
1387 " JB delay : cur=%d%s, max=%d%s, abs max=%d%s\n",
1388 last_update,
1389 /* pakcets */
1390 xr_stat.tx.voip_mtc.loss_rate, xr_stat.tx.voip_mtc.loss_rate*100.0/256,
1391 xr_stat.tx.voip_mtc.discard_rate, xr_stat.tx.voip_mtc.discard_rate*100.0/256,
1392 /* burst */
1393 xr_stat.tx.voip_mtc.burst_den, xr_stat.tx.voip_mtc.burst_den*100.0/256,
1394 xr_stat.tx.voip_mtc.burst_dur, "ms",
1395 /* gap */
1396 xr_stat.tx.voip_mtc.gap_den, xr_stat.tx.voip_mtc.gap_den*100.0/256,
1397 xr_stat.tx.voip_mtc.gap_dur, "ms",
1398 /* delay */
1399 xr_stat.tx.voip_mtc.rnd_trip_delay, "ms",
1400 xr_stat.tx.voip_mtc.end_sys_delay, "ms",
1401 /* level */
1402 signal_lvl, "dB",
1403 noise_lvl, "dB",
1404 rerl, "",
1405 /* quality */
1406 r_factor, ext_r_factor, mos_lq, mos_cq,
1407 /* config */
1408 plc, jba, jbr, xr_stat.tx.voip_mtc.gmin,
1409 /* JB delay */
1410 xr_stat.tx.voip_mtc.jb_nom, "ms",
1411 xr_stat.tx.voip_mtc.jb_max, "ms",
1412 xr_stat.tx.voip_mtc.jb_abs_max, "ms"
1413 );
1414
1415
1416 /* RTT delay (by receiver side) */
1417 printf(" (msec) min avg max last dev\n");
1418 printf(" RTT delay : %7.3f %7.3f %7.3f %7.3f %7.3f%s\n",
1419 xr_stat.rtt.min / 1000.0,
1420 xr_stat.rtt.mean / 1000.0,
1421 xr_stat.rtt.max / 1000.0,
1422 xr_stat.rtt.last / 1000.0,
1423 pj_math_stat_get_stddev(&xr_stat.rtt) / 1000.0,
1424 ""
1425 );
1426 } while (0);
1427 #endif /* PJMEDIA_HAS_RTCP_XR */
1428
1429 }
1430