1
/* $Id: siprtp.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
26
static const char *USAGE =
28
" This program establishes SIP INVITE session and media, and calculate \n"
29
" the media quality (packet lost, jitter, rtt, etc.). Unlike normal \n"
30
" pjmedia applications, this program bypasses all pjmedia stream \n"
31
" framework and transmit encoded RTP packets manually using own thread. \n"
34
" siprtp [options] => to start in server mode\n"
35
" siprtp [options] URL => to start in client mode\n"
38
" --count=N, -c Set number of calls to create (default:1) \n"
39
" --gap=N -g Set call gapping to N msec (default:0)\n"
40
" --duration=SEC, -d Set maximum call duration (default:unlimited) \n"
41
" --auto-quit, -q Quit when calls have been completed (default:no)\n"
42
" --call-report -R Display report on call termination (default:yes)\n"
44
" Address and ports options:\n"
45
" --local-port=PORT,-p Set local SIP port (default: 5060)\n"
46
" --rtp-port=PORT, -r Set start of RTP port (default: 4000)\n"
47
" --ip-addr=IP, -i Set local IP address to use (otherwise it will\n"
48
" try to determine local IP address from hostname)\n"
51
" --log-level=N, -l Set log verbosity level (default=5)\n"
52
" --app-log-level=N Set app screen log verbosity (default=3)\n"
53
" --log-file=FILE Write log to file FILE\n"
54
" --report-file=FILE Write report to file FILE\n"
56
/* Don't support this anymore, because codec is properly examined in
57
pjmedia_session_info_from_sdp() function.
60
" --a-pt=PT Set audio payload type to PT (default=0)\n"
61
" --a-name=NAME Set audio codec name to NAME (default=pcmu)\n"
62
" --a-clock=RATE Set audio codec rate to RATE Hz (default=8000Hz)\n"
63
" --a-bitrate=BPS Set audio codec bitrate to BPS (default=64000bps)\n"
64
" --a-ptime=MS Set audio frame time to MS msec (default=20ms)\n"
69
/* Include all headers. */
72
#include <pjmedia-codec.h>
74
#include <pjsip_simple.h>
75
#include <pjlib-util.h>
80
/* Uncomment these to disable threads.
82
* when threading is disabled, siprtp won't transmit any
87
#define PJ_HAS_THREADS 0
91
#if PJ_HAS_HIGH_RES_TIMER==0
92
# error "High resolution timer is needed for this sample"
95
#define THIS_FILE "siprtp.c"
96
#define MAX_CALLS 1024
97
#define RTP_START_PORT 4000
100
/* Codec descriptor: */
112
/* A bidirectional media stream created when the call is active. */
116
unsigned call_index; /* Call owner. */
117
unsigned media_index; /* Media index in call. */
118
pjmedia_transport *transport; /* To send/recv RTP/RTCP */
121
pj_bool_t active; /* Non-zero if is in call. */
123
/* Current stream info: */
124
pjmedia_stream_info si; /* Current stream info. */
127
unsigned clock_rate; /* clock rate */
128
unsigned samples_per_frame; /* samples per frame */
129
unsigned bytes_per_frame; /* frame size. */
132
pjmedia_rtp_session out_sess; /* outgoing RTP session */
133
pjmedia_rtp_session in_sess; /* incoming RTP session */
136
pjmedia_rtcp_session rtcp; /* incoming RTCP session. */
139
pj_bool_t thread_quit_flag; /* Stop media thread. */
140
pj_thread_t *thread; /* Media thread. */
144
/* This is a call structure that is created when the application starts
145
* and only destroyed when the application quits.
150
pjsip_inv_session *inv;
151
unsigned media_count;
152
struct media_stream media[1];
153
pj_time_val start_time;
154
pj_time_val response_time;
155
pj_time_val connect_time;
157
pj_timer_entry d_timer; /**< Disconnect timer. */
161
/* Application's global variables */
166
pj_bool_t call_report;
170
unsigned thread_count;
175
pj_str_t local_contact;
180
char *report_filename;
182
struct codec audio_codec;
184
pj_str_t uri_to_call;
189
pjsip_endpoint *sip_endpt;
190
pj_bool_t thread_quit;
191
pj_thread_t *sip_thread[1];
193
pjmedia_endpt *med_endpt;
194
struct call call[MAX_CALLS];
203
/* Callback to be called when SDP negotiation is done in the call: */
204
static void call_on_media_update( pjsip_inv_session *inv,
207
/* Callback to be called when invite session's state has changed: */
208
static void call_on_state_changed( pjsip_inv_session *inv,
211
/* Callback to be called when dialog has forked: */
212
static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
214
/* Callback to be called to handle incoming requests outside dialogs: */
215
static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
217
/* Worker thread prototype */
218
static int sip_worker_thread(void *arg);
220
/* Create SDP for call */
221
static pj_status_t create_sdp( pj_pool_t *pool,
223
pjmedia_sdp_session **p_sdp);
226
static void hangup_call(unsigned index);
228
/* Destroy the call's media */
229
static void destroy_call_media(unsigned call_index);
232
static void destroy_media();
234
/* This callback is called by media transport on receipt of RTP packet. */
235
static void on_rx_rtp(void *user_data, void *pkt, pj_ssize_t size);
237
/* This callback is called by media transport on receipt of RTCP packet. */
238
static void on_rx_rtcp(void *user_data, void *pkt, pj_ssize_t size);
241
static void app_perror(const char *sender, const char *title,
245
static void print_call(int call_index);
248
/* This is a PJSIP module to be registered by application to handle
249
* incoming requests outside any dialogs/transactions. The main purpose
250
* here is to handle incoming INVITE request message, where we will
251
* create a dialog and INVITE session for it.
253
static pjsip_module mod_siprtp =
255
NULL, NULL, /* prev, next. */
256
{ "mod-siprtpapp", 13 }, /* Name. */
258
PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
263
&on_rx_request, /* on_rx_request() */
264
NULL, /* on_rx_response() */
265
NULL, /* on_tx_request. */
266
NULL, /* on_tx_response() */
267
NULL, /* on_tsx_state() */
271
/* Codec constants */
272
struct codec audio_codecs[] =
274
{ 0, "PCMU", 8000, 64000, 20, "G.711 ULaw" },
275
{ 3, "GSM", 8000, 13200, 20, "GSM" },
276
{ 4, "G723", 8000, 6400, 30, "G.723.1" },
277
{ 8, "PCMA", 8000, 64000, 20, "G.711 ALaw" },
278
{ 18, "G729", 8000, 8000, 20, "G.729" },
285
static pj_status_t init_sip()
290
/* init PJLIB-UTIL: */
291
status = pjlib_util_init();
292
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
294
/* Must create a pool factory before we can allocate any memory. */
295
pj_caching_pool_init(&app.cp, &pj_pool_factory_default_policy, 0);
297
/* Create application pool for misc. */
298
app.pool = pj_pool_create(&app.cp.factory, "app", 1000, 1000, NULL);
300
/* Create the endpoint: */
301
status = pjsip_endpt_create(&app.cp.factory, pj_gethostname()->ptr,
303
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
306
/* Add UDP transport. */
309
pjsip_host_port addrname;
312
pj_bzero(&addr, sizeof(addr));
313
addr.sin_family = pj_AF_INET();
314
addr.sin_addr.s_addr = 0;
315
addr.sin_port = pj_htons((pj_uint16_t)app.sip_port);
317
if (app.local_addr.slen) {
319
addrname.host = app.local_addr;
320
addrname.port = app.sip_port;
322
status = pj_sockaddr_in_init(&addr, &app.local_addr,
323
(pj_uint16_t)app.sip_port);
324
if (status != PJ_SUCCESS) {
325
app_perror(THIS_FILE, "Unable to resolve IP interface", status);
330
status = pjsip_udp_transport_start( app.sip_endpt, &addr,
331
(app.local_addr.slen ? &addrname:NULL),
333
if (status != PJ_SUCCESS) {
334
app_perror(THIS_FILE, "Unable to start UDP transport", status);
338
PJ_LOG(3,(THIS_FILE, "SIP UDP listening on %.*s:%d",
339
(int)tp->local_name.host.slen, tp->local_name.host.ptr,
340
tp->local_name.port));
344
* Init transaction layer.
345
* This will create/initialize transaction hash tables etc.
347
status = pjsip_tsx_layer_init_module(app.sip_endpt);
348
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
350
/* Initialize UA layer. */
351
status = pjsip_ua_init_module( app.sip_endpt, NULL );
352
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
354
/* Initialize 100rel support */
355
status = pjsip_100rel_init_module(app.sip_endpt);
356
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
358
/* Init invite session module. */
360
pjsip_inv_callback inv_cb;
362
/* Init the callback for INVITE session: */
363
pj_bzero(&inv_cb, sizeof(inv_cb));
364
inv_cb.on_state_changed = &call_on_state_changed;
365
inv_cb.on_new_session = &call_on_forked;
366
inv_cb.on_media_update = &call_on_media_update;
368
/* Initialize invite session module: */
369
status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb);
370
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
373
/* Register our module to receive incoming requests. */
374
status = pjsip_endpt_register_module( app.sip_endpt, &mod_siprtp);
375
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
378
for (i=0; i<app.max_calls; ++i)
379
app.call[i].index = i;
389
static void destroy_sip()
394
for (i=0; i<app.thread_count; ++i) {
395
if (app.sip_thread[i]) {
396
pj_thread_join(app.sip_thread[i]);
397
pj_thread_destroy(app.sip_thread[i]);
398
app.sip_thread[i] = NULL;
403
pjsip_endpt_destroy(app.sip_endpt);
404
app.sip_endpt = NULL;
413
static pj_status_t init_media()
416
pj_uint16_t rtp_port;
420
/* Initialize media endpoint so that at least error subsystem is properly
424
status = pjmedia_endpt_create(&app.cp.factory, NULL, 1, &app.med_endpt);
426
status = pjmedia_endpt_create(&app.cp.factory,
427
pjsip_endpt_get_ioqueue(app.sip_endpt),
430
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
433
/* Must register codecs to be supported */
434
#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
435
pjmedia_codec_g711_init(app.med_endpt);
438
/* RTP port counter */
439
rtp_port = (pj_uint16_t)(app.rtp_start_port & 0xFFFE);
441
/* Init media transport for all calls. */
442
for (i=0, count=0; i<app.max_calls; ++i, ++count) {
446
/* Create transport for each media in the call */
447
for (j=0; j<PJ_ARRAY_SIZE(app.call[0].media); ++j) {
448
/* Repeat binding media socket to next port when fails to bind
449
* to current port number.
453
app.call[i].media[j].call_index = i;
454
app.call[i].media[j].media_index = j;
457
for (retry=0; retry<100; ++retry,rtp_port+=2) {
458
struct media_stream *m = &app.call[i].media[j];
460
status = pjmedia_transport_udp_create2(app.med_endpt,
465
if (status == PJ_SUCCESS) {
472
if (status != PJ_SUCCESS)
488
static void destroy_media()
492
for (i=0; i<app.max_calls; ++i) {
494
for (j=0; j<PJ_ARRAY_SIZE(app.call[0].media); ++j) {
495
struct media_stream *m = &app.call[i].media[j];
498
pjmedia_transport_close(m->transport);
505
pjmedia_endpt_destroy(app.med_endpt);
506
app.med_endpt = NULL;
512
* Make outgoing call.
514
static pj_status_t make_call(const pj_str_t *dst_uri)
519
pjmedia_sdp_session *sdp;
520
pjsip_tx_data *tdata;
524
/* Find unused call slot */
525
for (i=0; i<app.max_calls; ++i) {
526
if (app.call[i].inv == NULL)
530
if (i == app.max_calls)
535
/* Create UAC dialog */
536
status = pjsip_dlg_create_uac( pjsip_ua_instance(),
537
&app.local_uri, /* local URI */
538
&app.local_contact, /* local Contact */
539
dst_uri, /* remote URI */
540
dst_uri, /* remote target */
542
if (status != PJ_SUCCESS) {
548
create_sdp( dlg->pool, call, &sdp);
550
/* Create the INVITE session. */
551
status = pjsip_inv_create_uac( dlg, sdp, 0, &call->inv);
552
if (status != PJ_SUCCESS) {
553
pjsip_dlg_terminate(dlg);
559
/* Attach call data to invite session */
560
call->inv->mod_data[mod_siprtp.id] = call;
562
/* Mark start of call */
563
pj_gettimeofday(&call->start_time);
566
/* Create initial INVITE request.
567
* This INVITE request will contain a perfectly good request and
568
* an SDP body as well.
570
status = pjsip_inv_invite(call->inv, &tdata);
571
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
574
/* Send initial INVITE request.
575
* From now on, the invite session's state will be reported to us
576
* via the invite session callbacks.
578
status = pjsip_inv_send_msg(call->inv, tdata);
579
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
587
* Receive incoming call
589
static void process_incoming_call(pjsip_rx_data *rdata)
594
pjmedia_sdp_session *sdp;
595
pjsip_tx_data *tdata;
598
/* Find free call slot */
599
for (i=0; i<app.max_calls; ++i) {
600
if (app.call[i].inv == NULL)
604
if (i == app.max_calls) {
605
const pj_str_t reason = pj_str("Too many calls");
606
pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
614
/* Verify that we can handle the request. */
616
status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
617
app.sip_endpt, &tdata);
618
if (status != PJ_SUCCESS) {
620
* No we can't handle the incoming INVITE request.
623
pjsip_response_addr res_addr;
625
pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
626
pjsip_endpt_send_response(app.sip_endpt, &res_addr, tdata,
631
/* Respond with 500 (Internal Server Error) */
632
pjsip_endpt_respond_stateless(app.sip_endpt, rdata, 500, NULL,
639
/* Create UAS dialog */
640
status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata,
641
&app.local_contact, &dlg);
642
if (status != PJ_SUCCESS) {
643
const pj_str_t reason = pj_str("Unable to create dialog");
644
pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
651
create_sdp( dlg->pool, call, &sdp);
653
/* Create UAS invite session */
654
status = pjsip_inv_create_uas( dlg, rdata, sdp, 0, &call->inv);
655
if (status != PJ_SUCCESS) {
656
pjsip_dlg_create_response(dlg, rdata, 500, NULL, &tdata);
657
pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), tdata);
662
/* Attach call data to invite session */
663
call->inv->mod_data[mod_siprtp.id] = call;
665
/* Mark start of call */
666
pj_gettimeofday(&call->start_time);
670
/* Create 200 response .*/
671
status = pjsip_inv_initial_answer(call->inv, rdata, 200,
673
if (status != PJ_SUCCESS) {
674
status = pjsip_inv_initial_answer(call->inv, rdata,
675
PJSIP_SC_NOT_ACCEPTABLE,
677
if (status == PJ_SUCCESS)
678
pjsip_inv_send_msg(call->inv, tdata);
680
pjsip_inv_terminate(call->inv, 500, PJ_FALSE);
685
/* Send the 200 response. */
686
status = pjsip_inv_send_msg(call->inv, tdata);
687
PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, return);
694
/* Callback to be called when dialog has forked: */
695
static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
700
PJ_TODO( HANDLE_FORKING );
704
/* Callback to be called to handle incoming requests outside dialogs: */
705
static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
707
/* Ignore strandled ACKs (must not send respone */
708
if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD)
711
/* Respond (statelessly) any non-INVITE requests with 500 */
712
if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
713
pj_str_t reason = pj_str("Unsupported Operation");
714
pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
720
/* Handle incoming INVITE */
721
process_incoming_call(rdata);
728
/* Callback timer to disconnect call (limiting call duration) */
729
static void timer_disconnect_call( pj_timer_heap_t *timer_heap,
730
struct pj_timer_entry *entry)
732
struct call *call = entry->user_data;
734
PJ_UNUSED_ARG(timer_heap);
737
hangup_call(call->index);
741
/* Callback to be called when invite session's state has changed: */
742
static void call_on_state_changed( pjsip_inv_session *inv,
745
struct call *call = inv->mod_data[mod_siprtp.id];
752
if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
754
pj_time_val null_time = {0, 0};
756
if (call->d_timer.id != 0) {
757
pjsip_endpt_cancel_timer(app.sip_endpt, &call->d_timer);
758
call->d_timer.id = 0;
761
PJ_LOG(3,(THIS_FILE, "Call #%d disconnected. Reason=%d (%.*s)",
764
(int)inv->cause_text.slen,
765
inv->cause_text.ptr));
767
if (app.call_report) {
768
PJ_LOG(3,(THIS_FILE, "Call #%d statistics:", call->index));
769
print_call(call->index);
774
inv->mod_data[mod_siprtp.id] = NULL;
776
destroy_call_media(call->index);
778
call->start_time = null_time;
779
call->response_time = null_time;
780
call->connect_time = null_time;
784
} else if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
788
pj_gettimeofday(&call->connect_time);
789
if (call->response_time.sec == 0)
790
call->response_time = call->connect_time;
792
t = call->connect_time;
793
PJ_TIME_VAL_SUB(t, call->start_time);
795
PJ_LOG(3,(THIS_FILE, "Call #%d connected in %d ms", call->index,
796
PJ_TIME_VAL_MSEC(t)));
798
if (app.duration != 0) {
799
call->d_timer.id = 1;
800
call->d_timer.user_data = call;
801
call->d_timer.cb = &timer_disconnect_call;
803
t.sec = app.duration;
806
pjsip_endpt_schedule_timer(app.sip_endpt, &call->d_timer, &t);
809
} else if ( inv->state == PJSIP_INV_STATE_EARLY ||
810
inv->state == PJSIP_INV_STATE_CONNECTING) {
812
if (call->response_time.sec == 0)
813
pj_gettimeofday(&call->response_time);
820
static void app_perror(const char *sender, const char *title,
823
char errmsg[PJ_ERR_MSG_SIZE];
825
pj_strerror(status, errmsg, sizeof(errmsg));
826
PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status));
830
/* Worker thread for SIP */
831
static int sip_worker_thread(void *arg)
835
while (!app.thread_quit) {
836
pj_time_val timeout = {0, 10};
837
pjsip_endpt_handle_events(app.sip_endpt, &timeout);
844
/* Init application options */
845
static pj_status_t init_options(int argc, char *argv[])
847
static char ip_addr[32];
848
static char local_uri[64];
851
OPT_APP_LOG_LEVEL, OPT_LOG_FILE,
852
OPT_A_PT, OPT_A_NAME, OPT_A_CLOCK, OPT_A_BITRATE, OPT_A_PTIME,
855
struct pj_getopt_option long_options[] = {
856
{ "count", 1, 0, 'c' },
857
{ "gap", 1, 0, 'g' },
858
{ "call-report", 0, 0, 'R' },
859
{ "duration", 1, 0, 'd' },
860
{ "auto-quit", 0, 0, 'q' },
861
{ "local-port", 1, 0, 'p' },
862
{ "rtp-port", 1, 0, 'r' },
863
{ "ip-addr", 1, 0, 'i' },
865
{ "log-level", 1, 0, 'l' },
866
{ "app-log-level", 1, 0, OPT_APP_LOG_LEVEL },
867
{ "log-file", 1, 0, OPT_LOG_FILE },
869
{ "report-file", 1, 0, OPT_REPORT_FILE },
871
/* Don't support this anymore, see comments in USAGE above.
872
{ "a-pt", 1, 0, OPT_A_PT },
873
{ "a-name", 1, 0, OPT_A_NAME },
874
{ "a-clock", 1, 0, OPT_A_CLOCK },
875
{ "a-bitrate", 1, 0, OPT_A_BITRATE },
876
{ "a-ptime", 1, 0, OPT_A_PTIME },
884
/* Get local IP address for the default IP address */
886
const pj_str_t *hostname;
887
pj_sockaddr_in tmp_addr;
890
hostname = pj_gethostname();
891
pj_sockaddr_in_init(&tmp_addr, hostname, 0);
892
addr = pj_inet_ntoa(tmp_addr.sin_addr);
893
pj_ansi_strcpy(ip_addr, addr);
898
app.thread_count = 1;
900
app.rtp_start_port = RTP_START_PORT;
901
app.local_addr = pj_str(ip_addr);
903
app.app_log_level = 3;
904
app.log_filename = NULL;
906
/* Default codecs: */
907
app.audio_codec = audio_codecs[0];
911
while((c=pj_getopt_long(argc,argv, "c:d:p:r:i:l:g:qR",
912
long_options, &option_index))!=-1)
916
app.max_calls = atoi(pj_optarg);
917
if (app.max_calls < 0 || app.max_calls > MAX_CALLS) {
918
PJ_LOG(3,(THIS_FILE, "Invalid max calls value %s", pj_optarg));
923
app.call_gap = atoi(pj_optarg);
926
app.call_report = PJ_TRUE;
929
app.duration = atoi(pj_optarg);
936
app.sip_port = atoi(pj_optarg);
939
app.rtp_start_port = atoi(pj_optarg);
942
app.local_addr = pj_str(pj_optarg);
946
app.log_level = atoi(pj_optarg);
948
case OPT_APP_LOG_LEVEL:
949
app.app_log_level = atoi(pj_optarg);
952
app.log_filename = pj_optarg;
956
app.audio_codec.pt = atoi(pj_optarg);
959
app.audio_codec.name = pj_optarg;
962
app.audio_codec.clock_rate = atoi(pj_optarg);
965
app.audio_codec.bit_rate = atoi(pj_optarg);
968
app.audio_codec.ptime = atoi(pj_optarg);
970
case OPT_REPORT_FILE:
971
app.report_filename = pj_optarg;
980
/* Check if URL is specified */
981
if (pj_optind < argc)
982
app.uri_to_call = pj_str(argv[pj_optind]);
984
/* Build local URI and contact */
985
pj_ansi_sprintf( local_uri, "sip:%s:%d", app.local_addr.ptr, app.sip_port);
986
app.local_uri = pj_str(local_uri);
987
app.local_contact = app.local_uri;
994
/*****************************************************************************
999
* Create SDP session for a call.
1001
static pj_status_t create_sdp( pj_pool_t *pool,
1003
pjmedia_sdp_session **p_sdp)
1006
pjmedia_sdp_session *sdp;
1007
pjmedia_sdp_media *m;
1008
pjmedia_sdp_attr *attr;
1009
pjmedia_transport_info tpinfo;
1010
struct media_stream *audio = &call->media[0];
1012
PJ_ASSERT_RETURN(pool && p_sdp, PJ_EINVAL);
1015
/* Get transport info */
1016
pjmedia_transport_info_init(&tpinfo);
1017
pjmedia_transport_get_info(audio->transport, &tpinfo);
1019
/* Create and initialize basic SDP session */
1020
sdp = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_session));
1022
pj_gettimeofday(&tv);
1023
sdp->origin.user = pj_str("pjsip-siprtp");
1024
sdp->origin.version = sdp->origin.id = tv.sec + 2208988800UL;
1025
sdp->origin.net_type = pj_str("IN");
1026
sdp->origin.addr_type = pj_str("IP4");
1027
sdp->origin.addr = *pj_gethostname();
1028
sdp->name = pj_str("pjsip");
1030
/* Since we only support one media stream at present, put the
1031
* SDP connection line in the session level.
1033
sdp->conn = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_conn));
1034
sdp->conn->net_type = pj_str("IN");
1035
sdp->conn->addr_type = pj_str("IP4");
1036
sdp->conn->addr = app.local_addr;
1039
/* SDP time and attributes. */
1040
sdp->time.start = sdp->time.stop = 0;
1041
sdp->attr_count = 0;
1043
/* Create media stream 0: */
1045
sdp->media_count = 1;
1046
m = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_media));
1049
/* Standard media info: */
1050
m->desc.media = pj_str("audio");
1051
m->desc.port = pj_ntohs(tpinfo.sock_info.rtp_addr_name.ipv4.sin_port);
1052
m->desc.port_count = 1;
1053
m->desc.transport = pj_str("RTP/AVP");
1055
/* Add format and rtpmap for each codec. */
1056
m->desc.fmt_count = 1;
1060
pjmedia_sdp_rtpmap rtpmap;
1061
pjmedia_sdp_attr *attr;
1064
sprintf(ptstr, "%d", app.audio_codec.pt);
1065
pj_strdup2(pool, &m->desc.fmt[0], ptstr);
1066
rtpmap.pt = m->desc.fmt[0];
1067
rtpmap.clock_rate = app.audio_codec.clock_rate;
1068
rtpmap.enc_name = pj_str(app.audio_codec.name);
1069
rtpmap.param.slen = 0;
1071
pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
1072
m->attr[m->attr_count++] = attr;
1075
/* Add sendrecv attribute. */
1076
attr = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_attr));
1077
attr->name = pj_str("sendrecv");
1078
m->attr[m->attr_count++] = attr;
1082
* Add support telephony event
1084
m->desc.fmt[m->desc.fmt_count++] = pj_str("121");
1086
attr = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_attr));
1087
attr->name = pj_str("rtpmap");
1088
attr->value = pj_str("121 telephone-event/8000");
1089
m->attr[m->attr_count++] = attr;
1091
attr = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_attr));
1092
attr->name = pj_str("fmtp");
1093
attr->value = pj_str("121 0-15");
1094
m->attr[m->attr_count++] = attr;
1104
#if defined(PJ_WIN32) && PJ_WIN32 != 0
1105
#include <windows.h>
1106
static void boost_priority(void)
1108
SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
1109
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
1112
#elif defined(PJ_LINUX) && PJ_LINUX != 0
1113
#include <pthread.h>
1114
static void boost_priority(void)
1116
#define POLICY SCHED_FIFO
1117
struct sched_param tp;
1122
if (sched_get_priority_min(POLICY) < sched_get_priority_max(POLICY))
1123
max_prio = sched_get_priority_max(POLICY)-1;
1125
max_prio = sched_get_priority_max(POLICY)+1;
1128
* Adjust process scheduling algorithm and priority
1130
rc = sched_getparam(0, &tp);
1132
app_perror( THIS_FILE, "sched_getparam error",
1133
PJ_RETURN_OS_ERROR(rc));
1136
tp.__sched_priority = max_prio;
1138
rc = sched_setscheduler(0, POLICY, &tp);
1140
app_perror( THIS_FILE, "sched_setscheduler error",
1141
PJ_RETURN_OS_ERROR(rc));
1144
PJ_LOG(4, (THIS_FILE, "New process policy=%d, priority=%d",
1145
policy, tp.__sched_priority));
1148
* Adjust thread scheduling algorithm and priority
1150
rc = pthread_getschedparam(pthread_self(), &policy, &tp);
1152
app_perror( THIS_FILE, "pthread_getschedparam error",
1153
PJ_RETURN_OS_ERROR(rc));
1157
PJ_LOG(4, (THIS_FILE, "Old thread policy=%d, priority=%d",
1158
policy, tp.__sched_priority));
1161
tp.__sched_priority = max_prio;
1163
rc = pthread_setschedparam(pthread_self(), policy, &tp);
1165
app_perror( THIS_FILE, "pthread_setschedparam error",
1166
PJ_RETURN_OS_ERROR(rc));
1170
PJ_LOG(4, (THIS_FILE, "New thread policy=%d, priority=%d",
1171
policy, tp.__sched_priority));
1175
# define boost_priority()
1180
* This callback is called by media transport on receipt of RTP packet.
1182
static void on_rx_rtp(void *user_data, void *pkt, pj_ssize_t size)
1184
struct media_stream *strm;
1186
const pjmedia_rtp_hdr *hdr;
1187
const void *payload;
1188
unsigned payload_len;
1192
/* Discard packet if media is inactive */
1196
/* Check for errors */
1198
app_perror(THIS_FILE, "RTP recv() error", -size);
1202
/* Decode RTP packet. */
1203
status = pjmedia_rtp_decode_rtp(&strm->in_sess,
1205
&hdr, &payload, &payload_len);
1206
if (status != PJ_SUCCESS) {
1207
app_perror(THIS_FILE, "RTP decode error", status);
1211
//PJ_LOG(4,(THIS_FILE, "Rx seq=%d", pj_ntohs(hdr->seq)));
1213
/* Update the RTCP session. */
1214
pjmedia_rtcp_rx_rtp(&strm->rtcp, pj_ntohs(hdr->seq),
1215
pj_ntohl(hdr->ts), payload_len);
1217
/* Update RTP session */
1218
pjmedia_rtp_session_update(&strm->in_sess, hdr, NULL);
1223
* This callback is called by media transport on receipt of RTCP packet.
1225
static void on_rx_rtcp(void *user_data, void *pkt, pj_ssize_t size)
1227
struct media_stream *strm;
1231
/* Discard packet if media is inactive */
1235
/* Check for errors */
1237
app_perror(THIS_FILE, "Error receiving RTCP packet", -size);
1241
/* Update RTCP session */
1242
pjmedia_rtcp_rx_rtcp(&strm->rtcp, pkt, size);
1249
* This is the thread to send and receive both RTP and RTCP packets.
1251
static int media_thread(void *arg)
1253
enum { RTCP_INTERVAL = 5000, RTCP_RAND = 2000 };
1254
struct media_stream *strm = arg;
1256
unsigned msec_interval;
1257
pj_timestamp freq, next_rtp, next_rtcp;
1260
/* Boost thread priority if necessary */
1263
/* Let things settle */
1264
pj_thread_sleep(100);
1266
msec_interval = strm->samples_per_frame * 1000 / strm->clock_rate;
1267
pj_get_timestamp_freq(&freq);
1269
pj_get_timestamp(&next_rtp);
1270
next_rtp.u64 += (freq.u64 * msec_interval / 1000);
1272
next_rtcp = next_rtp;
1273
next_rtcp.u64 += (freq.u64 * (RTCP_INTERVAL+(pj_rand()%RTCP_RAND)) / 1000);
1276
while (!strm->thread_quit_flag) {
1277
pj_timestamp now, lesser;
1278
pj_time_val timeout;
1279
pj_bool_t send_rtp, send_rtcp;
1281
send_rtp = send_rtcp = PJ_FALSE;
1283
/* Determine how long to sleep */
1284
if (next_rtp.u64 < next_rtcp.u64) {
1289
send_rtcp = PJ_TRUE;
1292
pj_get_timestamp(&now);
1293
if (lesser.u64 <= now.u64) {
1294
timeout.sec = timeout.msec = 0;
1295
//printf("immediate "); fflush(stdout);
1297
pj_uint64_t tick_delay;
1298
tick_delay = lesser.u64 - now.u64;
1300
timeout.msec = (pj_uint32_t)(tick_delay * 1000 / freq.u64);
1301
pj_time_val_normalize(&timeout);
1303
//printf("%d:%03d ", timeout.sec, timeout.msec); fflush(stdout);
1306
/* Wait for next interval */
1307
//if (timeout.sec!=0 && timeout.msec!=0) {
1308
pj_thread_sleep(PJ_TIME_VAL_MSEC(timeout));
1309
if (strm->thread_quit_flag)
1313
pj_get_timestamp(&now);
1315
if (send_rtp || next_rtp.u64 <= now.u64) {
1317
* Time to send RTP packet.
1321
const pjmedia_rtp_hdr *hdr;
1325
/* Format RTP header */
1326
status = pjmedia_rtp_encode_rtp( &strm->out_sess, strm->si.tx_pt,
1328
strm->bytes_per_frame,
1329
strm->samples_per_frame,
1331
if (status == PJ_SUCCESS) {
1333
//PJ_LOG(4,(THIS_FILE, "\t\tTx seq=%d", pj_ntohs(hdr->seq)));
1335
hdr = (const pjmedia_rtp_hdr*) p_hdr;
1337
/* Copy RTP header to packet */
1338
pj_memcpy(packet, hdr, hdrlen);
1340
/* Zero the payload */
1341
pj_bzero(packet+hdrlen, strm->bytes_per_frame);
1343
/* Send RTP packet */
1344
size = hdrlen + strm->bytes_per_frame;
1345
status = pjmedia_transport_send_rtp(strm->transport,
1347
if (status != PJ_SUCCESS)
1348
app_perror(THIS_FILE, "Error sending RTP packet", status);
1351
pj_assert(!"RTP encode() error");
1354
/* Update RTCP SR */
1355
pjmedia_rtcp_tx_rtp( &strm->rtcp, (pj_uint16_t)strm->bytes_per_frame);
1357
/* Schedule next send */
1358
next_rtp.u64 += (msec_interval * freq.u64 / 1000);
1362
if (send_rtcp || next_rtcp.u64 <= now.u64) {
1364
* Time to send RTCP packet.
1371
/* Build RTCP packet */
1372
pjmedia_rtcp_build_rtcp(&strm->rtcp, &rtcp_pkt, &rtcp_len);
1377
status = pjmedia_transport_send_rtcp(strm->transport,
1379
if (status != PJ_SUCCESS) {
1380
app_perror(THIS_FILE, "Error sending RTCP packet", status);
1383
/* Schedule next send */
1384
next_rtcp.u64 += (freq.u64 * (RTCP_INTERVAL+(pj_rand()%RTCP_RAND)) /
1393
/* Callback to be called when SDP negotiation is done in the call: */
1394
static void call_on_media_update( pjsip_inv_session *inv,
1399
struct media_stream *audio;
1400
const pjmedia_sdp_session *local_sdp, *remote_sdp;
1401
struct codec *codec_desc = NULL;
1404
call = inv->mod_data[mod_siprtp.id];
1405
pool = inv->dlg->pool;
1406
audio = &call->media[0];
1408
/* If this is a mid-call media update, then destroy existing media */
1409
if (audio->thread != NULL)
1410
destroy_call_media(call->index);
1413
/* Do nothing if media negotiation has failed */
1414
if (status != PJ_SUCCESS) {
1415
app_perror(THIS_FILE, "SDP negotiation failed", status);
1420
/* Capture stream definition from the SDP */
1421
pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
1422
pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
1424
status = pjmedia_stream_info_from_sdp(&audio->si, inv->pool, app.med_endpt,
1425
local_sdp, remote_sdp, 0);
1426
if (status != PJ_SUCCESS) {
1427
app_perror(THIS_FILE, "Error creating stream info from SDP", status);
1431
/* Get the remainder of codec information from codec descriptor */
1432
if (audio->si.fmt.pt == app.audio_codec.pt)
1433
codec_desc = &app.audio_codec;
1435
/* Find the codec description in codec array */
1436
for (i=0; i<PJ_ARRAY_SIZE(audio_codecs); ++i) {
1437
if (audio_codecs[i].pt == audio->si.fmt.pt) {
1438
codec_desc = &audio_codecs[i];
1443
if (codec_desc == NULL) {
1444
PJ_LOG(3, (THIS_FILE, "Error: Invalid codec payload type"));
1449
audio->clock_rate = audio->si.fmt.clock_rate;
1450
audio->samples_per_frame = audio->clock_rate * codec_desc->ptime / 1000;
1451
audio->bytes_per_frame = codec_desc->bit_rate * codec_desc->ptime / 1000 / 8;
1454
pjmedia_rtp_session_init(&audio->out_sess, audio->si.tx_pt,
1456
pjmedia_rtp_session_init(&audio->in_sess, audio->si.fmt.pt, 0);
1457
pjmedia_rtcp_init(&audio->rtcp, "rtcp", audio->clock_rate,
1458
audio->samples_per_frame, 0);
1461
/* Attach media to transport */
1462
status = pjmedia_transport_attach(audio->transport, audio,
1463
&audio->si.rem_addr,
1464
&audio->si.rem_rtcp,
1465
sizeof(pj_sockaddr_in),
1468
if (status != PJ_SUCCESS) {
1469
app_perror(THIS_FILE, "Error on pjmedia_transport_attach()", status);
1473
/* Start media thread. */
1474
audio->thread_quit_flag = 0;
1476
status = pj_thread_create( inv->pool, "media", &media_thread, audio,
1477
0, 0, &audio->thread);
1478
if (status != PJ_SUCCESS) {
1479
app_perror(THIS_FILE, "Error creating media thread", status);
1484
/* Set the media as active */
1485
audio->active = PJ_TRUE;
1490
/* Destroy call's media */
1491
static void destroy_call_media(unsigned call_index)
1493
struct media_stream *audio = &app.call[call_index].media[0];
1496
audio->active = PJ_FALSE;
1498
if (audio->thread) {
1499
audio->thread_quit_flag = 1;
1500
pj_thread_join(audio->thread);
1501
pj_thread_destroy(audio->thread);
1502
audio->thread = NULL;
1503
audio->thread_quit_flag = 0;
1506
pjmedia_transport_detach(audio->transport, audio);
1511
/*****************************************************************************
1512
* USER INTERFACE STUFFS
1515
static void call_get_duration(int call_index, pj_time_val *dur)
1517
struct call *call = &app.call[call_index];
1518
pjsip_inv_session *inv;
1520
dur->sec = dur->msec = 0;
1529
if (inv->state >= PJSIP_INV_STATE_CONFIRMED && call->connect_time.sec) {
1531
pj_gettimeofday(dur);
1532
PJ_TIME_VAL_SUB((*dur), call->connect_time);
1537
static const char *good_number(char *buf, pj_int32_t val)
1540
pj_ansi_sprintf(buf, "%d", val);
1541
} else if (val < 1000000) {
1542
pj_ansi_sprintf(buf, "%d.%02dK",
1544
(val % 1000) / 100);
1546
pj_ansi_sprintf(buf, "%d.%02dM",
1548
(val % 1000000) / 10000);
1556
static void print_avg_stat(void)
1558
#define MIN_(var,val) if ((int)val < (int)var) var = val
1559
#define MAX_(var,val) if ((int)val > (int)var) var = val
1560
#define AVG_(var,val) var = ( ((var * count) + val) / (count+1) )
1561
#define BIGVAL 0x7FFFFFFFL
1567
struct stat_entry call_dur, call_pdd;
1568
pjmedia_rtcp_stat min_stat, avg_stat, max_stat;
1570
char srx_min[16], srx_avg[16], srx_max[16];
1571
char brx_min[16], brx_avg[16], brx_max[16];
1572
char stx_min[16], stx_avg[16], stx_max[16];
1573
char btx_min[16], btx_avg[16], btx_max[16];
1578
pj_bzero(&call_dur, sizeof(call_dur));
1579
call_dur.min = BIGVAL;
1581
pj_bzero(&call_pdd, sizeof(call_pdd));
1582
call_pdd.min = BIGVAL;
1584
pj_bzero(&min_stat, sizeof(min_stat));
1585
min_stat.rx.pkt = min_stat.tx.pkt = BIGVAL;
1586
min_stat.rx.bytes = min_stat.tx.bytes = BIGVAL;
1587
min_stat.rx.loss = min_stat.tx.loss = BIGVAL;
1588
min_stat.rx.dup = min_stat.tx.dup = BIGVAL;
1589
min_stat.rx.reorder = min_stat.tx.reorder = BIGVAL;
1590
min_stat.rx.jitter.min = min_stat.tx.jitter.min = BIGVAL;
1591
min_stat.rtt.min = BIGVAL;
1593
pj_bzero(&avg_stat, sizeof(avg_stat));
1594
pj_bzero(&max_stat, sizeof(max_stat));
1597
for (i=0, count=0; i<app.max_calls; ++i) {
1599
struct call *call = &app.call[i];
1600
struct media_stream *audio = &call->media[0];
1604
if (call->inv == NULL ||
1605
call->inv->state < PJSIP_INV_STATE_CONFIRMED ||
1606
call->connect_time.sec == 0)
1612
call_get_duration(i, &dur);
1613
msec_dur = PJ_TIME_VAL_MSEC(dur);
1615
MIN_(call_dur.min, msec_dur);
1616
MAX_(call_dur.max, msec_dur);
1617
AVG_(call_dur.avg, msec_dur);
1620
if (call->connect_time.sec) {
1621
pj_time_val t = call->connect_time;
1622
PJ_TIME_VAL_SUB(t, call->start_time);
1623
msec_dur = PJ_TIME_VAL_MSEC(t);
1628
MIN_(call_pdd.min, msec_dur);
1629
MAX_(call_pdd.max, msec_dur);
1630
AVG_(call_pdd.avg, msec_dur);
1632
/* RX Statistisc: */
1635
MIN_(min_stat.rx.pkt, audio->rtcp.stat.rx.pkt);
1636
MAX_(max_stat.rx.pkt, audio->rtcp.stat.rx.pkt);
1637
AVG_(avg_stat.rx.pkt, audio->rtcp.stat.rx.pkt);
1640
MIN_(min_stat.rx.bytes, audio->rtcp.stat.rx.bytes);
1641
MAX_(max_stat.rx.bytes, audio->rtcp.stat.rx.bytes);
1642
AVG_(avg_stat.rx.bytes, audio->rtcp.stat.rx.bytes);
1646
MIN_(min_stat.rx.loss, audio->rtcp.stat.rx.loss);
1647
MAX_(max_stat.rx.loss, audio->rtcp.stat.rx.loss);
1648
AVG_(avg_stat.rx.loss, audio->rtcp.stat.rx.loss);
1651
MIN_(min_stat.rx.dup, audio->rtcp.stat.rx.dup);
1652
MAX_(max_stat.rx.dup, audio->rtcp.stat.rx.dup);
1653
AVG_(avg_stat.rx.dup, audio->rtcp.stat.rx.dup);
1655
/* Packet reorder */
1656
MIN_(min_stat.rx.reorder, audio->rtcp.stat.rx.reorder);
1657
MAX_(max_stat.rx.reorder, audio->rtcp.stat.rx.reorder);
1658
AVG_(avg_stat.rx.reorder, audio->rtcp.stat.rx.reorder);
1661
MIN_(min_stat.rx.jitter.min, audio->rtcp.stat.rx.jitter.min);
1662
MAX_(max_stat.rx.jitter.max, audio->rtcp.stat.rx.jitter.max);
1663
AVG_(avg_stat.rx.jitter.mean, audio->rtcp.stat.rx.jitter.mean);
1666
/* TX Statistisc: */
1669
MIN_(min_stat.tx.pkt, audio->rtcp.stat.tx.pkt);
1670
MAX_(max_stat.tx.pkt, audio->rtcp.stat.tx.pkt);
1671
AVG_(avg_stat.tx.pkt, audio->rtcp.stat.tx.pkt);
1674
MIN_(min_stat.tx.bytes, audio->rtcp.stat.tx.bytes);
1675
MAX_(max_stat.tx.bytes, audio->rtcp.stat.tx.bytes);
1676
AVG_(avg_stat.tx.bytes, audio->rtcp.stat.tx.bytes);
1679
MIN_(min_stat.tx.loss, audio->rtcp.stat.tx.loss);
1680
MAX_(max_stat.tx.loss, audio->rtcp.stat.tx.loss);
1681
AVG_(avg_stat.tx.loss, audio->rtcp.stat.tx.loss);
1684
MIN_(min_stat.tx.dup, audio->rtcp.stat.tx.dup);
1685
MAX_(max_stat.tx.dup, audio->rtcp.stat.tx.dup);
1686
AVG_(avg_stat.tx.dup, audio->rtcp.stat.tx.dup);
1688
/* Packet reorder */
1689
MIN_(min_stat.tx.reorder, audio->rtcp.stat.tx.reorder);
1690
MAX_(max_stat.tx.reorder, audio->rtcp.stat.tx.reorder);
1691
AVG_(avg_stat.tx.reorder, audio->rtcp.stat.tx.reorder);
1694
MIN_(min_stat.tx.jitter.min, audio->rtcp.stat.tx.jitter.min);
1695
MAX_(max_stat.tx.jitter.max, audio->rtcp.stat.tx.jitter.max);
1696
AVG_(avg_stat.tx.jitter.mean, audio->rtcp.stat.tx.jitter.mean);
1700
MIN_(min_stat.rtt.min, audio->rtcp.stat.rtt.min);
1701
MAX_(max_stat.rtt.max, audio->rtcp.stat.rtt.max);
1702
AVG_(avg_stat.rtt.mean, audio->rtcp.stat.rtt.mean);
1708
puts("No active calls");
1712
printf("Total %d call(s) active.\n"
1713
" Average Statistics\n"
1715
" -----------------------\n"
1716
" call duration: %7d %7d %7d %s\n"
1717
" connect delay: %7d %7d %7d %s\n"
1719
" packets: %7s %7s %7s %s\n"
1720
" payload: %7s %7s %7s %s\n"
1721
" loss: %7d %7d %7d %s\n"
1722
" percent loss: %7.3f %7.3f %7.3f %s\n"
1723
" dup: %7d %7d %7d %s\n"
1724
" reorder: %7d %7d %7d %s\n"
1725
" jitter: %7.3f %7.3f %7.3f %s\n"
1727
" packets: %7s %7s %7s %s\n"
1728
" payload: %7s %7s %7s %s\n"
1729
" loss: %7d %7d %7d %s\n"
1730
" percent loss: %7.3f %7.3f %7.3f %s\n"
1731
" dup: %7d %7d %7d %s\n"
1732
" reorder: %7d %7d %7d %s\n"
1733
" jitter: %7.3f %7.3f %7.3f %s\n"
1734
" RTT : %7.3f %7.3f %7.3f %s\n"
1737
call_dur.min/1000, call_dur.avg/1000, call_dur.max/1000,
1740
call_pdd.min, call_pdd.avg, call_pdd.max,
1745
good_number(srx_min, min_stat.rx.pkt),
1746
good_number(srx_avg, avg_stat.rx.pkt),
1747
good_number(srx_max, max_stat.rx.pkt),
1750
good_number(brx_min, min_stat.rx.bytes),
1751
good_number(brx_avg, avg_stat.rx.bytes),
1752
good_number(brx_max, max_stat.rx.bytes),
1755
min_stat.rx.loss, avg_stat.rx.loss, max_stat.rx.loss,
1758
min_stat.rx.loss*100.0/(min_stat.rx.pkt+min_stat.rx.loss),
1759
avg_stat.rx.loss*100.0/(avg_stat.rx.pkt+avg_stat.rx.loss),
1760
max_stat.rx.loss*100.0/(max_stat.rx.pkt+max_stat.rx.loss),
1764
min_stat.rx.dup, avg_stat.rx.dup, max_stat.rx.dup,
1767
min_stat.rx.reorder, avg_stat.rx.reorder, max_stat.rx.reorder,
1770
min_stat.rx.jitter.min/1000.0,
1771
avg_stat.rx.jitter.mean/1000.0,
1772
max_stat.rx.jitter.max/1000.0,
1777
good_number(stx_min, min_stat.tx.pkt),
1778
good_number(stx_avg, avg_stat.tx.pkt),
1779
good_number(stx_max, max_stat.tx.pkt),
1782
good_number(btx_min, min_stat.tx.bytes),
1783
good_number(btx_avg, avg_stat.tx.bytes),
1784
good_number(btx_max, max_stat.tx.bytes),
1787
min_stat.tx.loss, avg_stat.tx.loss, max_stat.tx.loss,
1790
min_stat.tx.loss*100.0/(min_stat.tx.pkt+min_stat.tx.loss),
1791
avg_stat.tx.loss*100.0/(avg_stat.tx.pkt+avg_stat.tx.loss),
1792
max_stat.tx.loss*100.0/(max_stat.tx.pkt+max_stat.tx.loss),
1795
min_stat.tx.dup, avg_stat.tx.dup, max_stat.tx.dup,
1798
min_stat.tx.reorder, avg_stat.tx.reorder, max_stat.tx.reorder,
1801
min_stat.tx.jitter.min/1000.0,
1802
avg_stat.tx.jitter.mean/1000.0,
1803
max_stat.tx.jitter.max/1000.0,
1807
min_stat.rtt.min/1000.0,
1808
avg_stat.rtt.mean/1000.0,
1809
max_stat.rtt.max/1000.0,
1816
#include "siprtp_report.c"
1819
static void list_calls()
1822
puts("List all calls:");
1823
for (i=0; i<app.max_calls; ++i) {
1824
if (!app.call[i].inv)
1830
static void hangup_call(unsigned index)
1832
pjsip_tx_data *tdata;
1835
if (app.call[index].inv == NULL)
1838
status = pjsip_inv_end_session(app.call[index].inv, 603, NULL, &tdata);
1839
if (status==PJ_SUCCESS && tdata!=NULL)
1840
pjsip_inv_send_msg(app.call[index].inv, tdata);
1843
static void hangup_all_calls()
1846
for (i=0; i<app.max_calls; ++i) {
1847
if (!app.call[i].inv)
1850
pj_thread_sleep(app.call_gap);
1853
/* Wait until all calls are terminated */
1854
for (i=0; i<app.max_calls; ++i) {
1855
while (app.call[i].inv)
1856
pj_thread_sleep(10);
1860
static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len)
1864
printf("%s (empty to cancel): ", title); fflush(stdout);
1865
if (fgets(buf, len, stdin) == NULL)
1868
/* Remove trailing newlines. */
1869
for (p=buf; ; ++p) {
1870
if (*p=='\r' || *p=='\n') *p='\0';
1871
else if (!*p) break;
1881
static const char *MENU =
1883
"Enter menu character:\n"
1885
" l List all calls\n"
1886
" h Hangup a call\n"
1887
" H Hangup all calls\n"
1892
/* Main screen menu */
1893
static void console_main()
1901
printf(">>> "); fflush(stdout);
1902
if (fgets(input1, sizeof(input1), stdin) == NULL) {
1903
puts("EOF while reading stdin, will quit now..");
1907
switch (input1[0]) {
1918
if (!simple_input("Call number to hangup", input1, sizeof(input1)))
1933
puts("Invalid command");
1946
/*****************************************************************************
1947
* Below is a simple module to log all incoming and outgoing SIP messages
1951
/* Notification on incoming messages */
1952
static pj_bool_t logger_on_rx_msg(pjsip_rx_data *rdata)
1954
PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s:%d:\n"
1957
rdata->msg_info.len,
1958
pjsip_rx_data_get_info(rdata),
1959
rdata->pkt_info.src_name,
1960
rdata->pkt_info.src_port,
1961
rdata->msg_info.msg_buf));
1963
/* Always return false, otherwise messages will not get processed! */
1967
/* Notification on outgoing messages */
1968
static pj_status_t logger_on_tx_msg(pjsip_tx_data *tdata)
1972
* tp_info field is only valid after outgoing messages has passed
1973
* transport layer. So don't try to access tp_info when the module
1974
* has lower priority than transport layer.
1977
PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s:%d:\n"
1980
(tdata->buf.cur - tdata->buf.start),
1981
pjsip_tx_data_get_info(tdata),
1982
tdata->tp_info.dst_name,
1983
tdata->tp_info.dst_port,
1986
/* Always return success, otherwise message will not get sent! */
1990
/* The module instance. */
1991
static pjsip_module msg_logger =
1993
NULL, NULL, /* prev, next. */
1994
{ "mod-siprtp-log", 14 }, /* Name. */
1996
PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
2000
NULL, /* unload() */
2001
&logger_on_rx_msg, /* on_rx_request() */
2002
&logger_on_rx_msg, /* on_rx_response() */
2003
&logger_on_tx_msg, /* on_tx_request. */
2004
&logger_on_tx_msg, /* on_tx_response() */
2005
NULL, /* on_tsx_state() */
2011
/*****************************************************************************
2012
* Console application custom logging:
2016
static FILE *log_file;
2019
static void app_log_writer(int level, const char *buffer, int len)
2021
/* Write to both stdout and file. */
2023
if (level <= app.app_log_level)
2024
pj_log_write(level, buffer, len);
2027
int count = fwrite(buffer, len, 1, log_file);
2028
PJ_UNUSED_ARG(count);
2034
pj_status_t app_logging_init(void)
2036
/* Redirect log function to ours */
2038
pj_log_set_log_func( &app_log_writer );
2040
/* If output log file is desired, create the file: */
2042
if (app.log_filename) {
2043
log_file = fopen(app.log_filename, "wt");
2044
if (log_file == NULL) {
2045
PJ_LOG(1,(THIS_FILE, "Unable to open log file %s",
2055
void app_logging_shutdown(void)
2057
/* Close logging file, if any: */
2069
int main(int argc, char *argv[])
2074
/* Must init PJLIB first */
2076
if (status != PJ_SUCCESS)
2079
/* Get command line options */
2080
status = init_options(argc, argv);
2081
if (status != PJ_SUCCESS)
2084
/* Verify options: */
2086
/* Auto-quit can not be specified for UAS */
2087
if (app.auto_quit && app.uri_to_call.slen == 0) {
2088
printf("Error: --auto-quit option only valid for outgoing "
2089
"mode (UAC) only\n");
2094
status = app_logging_init();
2095
if (status != PJ_SUCCESS)
2099
status = init_sip();
2100
if (status != PJ_SUCCESS) {
2101
app_perror(THIS_FILE, "Initialization has failed", status);
2106
/* Register module to log incoming/outgoing messages */
2107
pjsip_endpt_register_module(app.sip_endpt, &msg_logger);
2110
status = init_media();
2111
if (status != PJ_SUCCESS) {
2112
app_perror(THIS_FILE, "Media initialization failed", status);
2117
/* Start worker threads */
2119
for (i=0; i<app.thread_count; ++i) {
2120
pj_thread_create( app.pool, "app", &sip_worker_thread, NULL,
2121
0, 0, &app.sip_thread[i]);
2125
/* If URL is specified, then make call immediately */
2126
if (app.uri_to_call.slen) {
2129
PJ_LOG(3,(THIS_FILE, "Making %d calls to %s..", app.max_calls,
2130
app.uri_to_call.ptr));
2132
for (i=0; i<app.max_calls; ++i) {
2133
status = make_call(&app.uri_to_call);
2134
if (status != PJ_SUCCESS) {
2135
app_perror(THIS_FILE, "Error making call", status);
2138
pj_thread_sleep(app.call_gap);
2141
if (app.auto_quit) {
2142
/* Wait for calls to complete */
2143
while (app.uac_calls < app.max_calls)
2144
pj_thread_sleep(100);
2145
pj_thread_sleep(200);
2148
/* Start user interface loop */
2155
PJ_LOG(3,(THIS_FILE, "Ready for incoming calls (max=%d)",
2159
/* Start user interface loop */
2165
PJ_LOG(3,(THIS_FILE, "Press Ctrl-C to quit"));
2167
pj_time_val t = {0, 10};
2168
pjsip_endpt_handle_events(app.sip_endpt, &t);
2172
/* Shutting down... */
2177
pj_pool_release(app.pool);
2179
pj_caching_pool_destroy(&app.cp);
2182
app_logging_shutdown();
2184
/* Shutdown PJLIB */