qstream 123 KB
Newer Older
Olav Kvittem's avatar
Olav Kvittem committed
1
2
3
#!/usr/bin/perl

# compute packet stream quality statistics for UDP/RTP from network or captured files(pcap)
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
4
5
6
# Base Olav Kvittem
# Mpeg support Odd Rune Mykkeltveit Lykkbo,  2009-2010
# Mos values computation Gurvinder Singh, 2010-05-07
paalmm's avatar
paalmm committed
7
# XML, RTMP, TCP support, Pål Moen Møst, 2011.
Olav Kvittem's avatar
Olav Kvittem committed
8
9
10
11
12
13
14
15
16

# use Socket;
use IO::Socket qw(:DEFAULT :crlf);
use IO::Socket::INET;
use IO::Socket::Multicast;
use IO::Select;
use Net::Pcap;
use NetPacket::Ethernet qw(:strip);
use NetPacket::IP;
17
use NetPacket::IP qw(:strip  :protos);;
Olav Kvittem's avatar
Olav Kvittem committed
18
use NetPacket::UDP;
19
use NetPacket::TCP;
20
use Net::IPv6Addr;
21
#use NetPacket::IPv6 qw(ipv6_strip);
22
#use Net::RTP;
Olav Kvittem's avatar
Olav Kvittem committed
23
24
25
use Net::RTP::Packet;
use Time::HiRes qw/ sleep time tv_interval gettimeofday/;
use Data::Dumper;
26
27
28
use threads;
use threads::shared;
use Pod::Usage;
Olav Kvittem's avatar
Olav Kvittem committed
29
30
31
#use bigint qw/ hex oct/;
use Math::BigInt;
use Math::BigFloat;
32
use Carp;
33
34
#RTMP implementation *experimental*
use rtmp::client qw(rtmp_connect rtmp_play rtmp_call get_chunk_ids);
Olav Kvittem's avatar
Olav Kvittem committed
35
use constant MTU => 1580;
36
37
38
#XML
use XML::DOM;
use XML::Twig;
Olav Kvittem's avatar
Olav Kvittem committed
39
#use mpegpackets;
Olav Kvittem's avatar
Olav Kvittem committed
40
41
42
$DEFAULT_PORT = 1955;
my $timeout=1 ; # frac seconds to wait for receiving packets

43
44
45
#my oh my,more globals
my ($rtmp,$period,$last,$debug,$swfurl,$pageurl,$flash_version,$reconnect);

Olav Kvittem's avatar
Olav Kvittem committed
46
require "newgetopt.pl";
47

48
@opts=('list', 'man','fullformat', 'net', 'rtp', 'mpeg', 'pcap', 'crude', 'bins=s', 'log', 'nperiod=s', 'period=s', 'last=s', 'packets=s', 'src=s', 'flow_key=s', 'flow_no=s', 'flow_min=s', 'nohead', 'dump=s', 'format=s', 'id=s', 'sum=s', 'ttl', 'rtt=s', 'codec=s', 'log', 'verbose', 'v', 'h', 'debug', 'av','xml=s', 'rtmp','tcp', 'port=s','swfurl=s','pageurl=s','flashversion=s','new','q','exclude=s','report=s', 'filename','test','ipv6','p=s','name=s','tcp_t');
49
50
51

&NGetOpt(@opts) || die pod2usage(1);
die pod2usage(1)  if $opt_h;
Olav Kvittem's avatar
Olav Kvittem committed
52
53
$continous= ! $opt_packets;
$opt_v=$opt_v or $opt_verbose;
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
54
55
my $codec = $opt_codec || 'G.711';
my $rtt = $opt_rtt || 10;		# ms round trip time of the connection
56
pod2usage(-verbose => 2 ) if $opt_man;
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
57

Olav Kvittem's avatar
Olav Kvittem committed
58
59
my @streams=@ARGV;

Olav Kvittem's avatar
Olav Kvittem committed
60
61
62
if ($opt_bins){
    @bins=split(/,/, $opt_bins);
}
63
64
65
if ($opt_report){
    @filter=split(/,/, $opt_report);
}
Olav Kvittem's avatar
Olav Kvittem committed
66
67
68
69
70
71
72
73
74
my $format = $opt_format || "pretty";

my %hdr=();
my $err="";
my $us=0; # current packet time in us
my $nkill=0;
my $nint=0;
my $n_packets=0;
my $endstream=0;
75
76
77
78
79
80
81
82
my $period_num = 0;
my $pcr_count = 0;
my $pts_count =  0;
my $doc;
my $xml;
my $xml_printed= 0;
my $last_stream;
my $file_flag;
83
my $offline;
84
my $lost_counter  = 0;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

#TCP STUFF
my $thr;
my $rtmp_port:shared = 1935; 
my $rtmp_addr:shared;
my %tcp_packet_stats:shared = ();
my %bw_stats:shared = ();
my %seq_stats:shared = ();
my %seq_stats_n =();
my $n_seq_num:shared=0;
my $rtmp_stream_start:shared;
my $tcp_stop:shared;
my %hash : shared;

my $packet_re_num:shared=0;
$seq_stats{'p_seq_num'} = 0;
$seq_stats{'p_tcp_time_stamp'} = 0;
$seq_stats{'p_tcp_time_stamp_echo'} = 0;
$seq_stats{'real_last_time'} = 0;
#init packetstats
$tcp_packet_stats{'pkt_lost'} = 0;
$tcp_packet_stats{'pkt_late'} = 0;
$tcp_packet_stats{'pkt_dup'} = 0 ;
$tcp_packet_stats{'pkt_retrans'} = 0;

110
111
my %offset_bytes =();
my %network_info =();
112
113

my $start_time = time();
Olav Kvittem's avatar
Olav Kvittem committed
114
115
116
#MPEG-TS measurement globals (most are reset in init)
my $CHECK_NUM_TSP = 2;
#globals for mpeg ts 
117
#a tabel for transport streams pids. aleternativ to PAT impl.
118
my %pcr_pid = ();
119
my $TS;
Olav Kvittem's avatar
Olav Kvittem committed
120
121
122
123
124
125
126
127
128
129
130
131
my @cc = ();
my $discontinuities = 0;
my %last_ccs = ();
my $packets_between_pcr = 0;
#my $PTS_MAX = 95443.7176888888888888
my $PTS_MAX = 95443;
my $PCR_MAX = 95443;
# pcr_stats {filename/handle}
my %pcr_stats = ();
#PTS-stats {filename/handle}
my %pts_stats = ();
my %mpeg_stats = ();
132
my %jitter_stats = ();
133

Olav Kvittem's avatar
Olav Kvittem committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#PTS stuff
#my $real_time = time();
my $late_video_packets = 0;

#CC distribution
my $num_cc = 0;
my %cc_dist = ();
my %pid_next_cc = ();

my %dist1 = ();
my %dist2 = ();

$opt_period = $opt_last if not $opt_period;

paalmm's avatar
paalmm committed
148
149
$file_flag = 1 if $opt_filename;

150
#MPEG-TS END
Olav Kvittem's avatar
Olav Kvittem committed
151
152


153
154
$SIG{USR2} = sub {confess "Caught by SIGUSR2"; };

Olav Kvittem's avatar
Olav Kvittem committed
155
$SIG{INT} = sub { $uninterrupted=0; # return if $nint++  < 1;
156
		  &display_stats() if  !$endstream;
157
158
		  &end_xml() if $opt_xml;
		  &handle_threads()  if ($opt_tcp and $opt_rtmp);
159
		  
160
		  die "End after interrupt.\n";exit(0) };
161
162

$SIG{TERM} = sub { $uninterrupted=0; # return if $nkill++ < 1;
163
		   &display_stats() if  !$endstream;  
164
165
166
167
168
		   &end_xml() if $opt_xml;
		   &handle_threads()  if ($opt_tcp and $opt_rtmp);

		  die "End after sigterm.\n";exit(0) };

Olav Kvittem's avatar
Olav Kvittem committed
169
$SIG{ALRM} = sub { 
170
#   Writes stats to file if option XML is given. -PMM
Olav Kvittem's avatar
Olav Kvittem committed
171
172
#    die "No packets received" if $n_packets < 1;
#    $uninterrupted=0;
173
    &display_stats() if  !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
174
175
#	die "End after alarm\n"; 
#	return(0);
Olav Kvittem's avatar
Olav Kvittem committed
176
177
    $endstream=1;
    print "endstream=1" if $opt_debug;
Olav Kvittem's avatar
Olav Kvittem committed
178
};
Olav Kvittem's avatar
Olav Kvittem committed
179
180
181
182

if ($opt_dump){
    open DUMP, ">$opt_dump";
}
183

184
185
186
187
188
189
190
191
if ($opt_test){
    if($opt_mpeg || $opt_rtp || $opt_tcp_t){
	&unit_test;
    }
    else {
	print "You need to choose a test. (mpeg, tcp or rtp) \n";
    }
}
Olav Kvittem's avatar
Olav Kvittem committed
192

193
if ($opt_pcap || ($opt_tcp && !$opt_rtmp)){ # listen in parallell
Olav Kvittem's avatar
Olav Kvittem committed
194
195
196
    alarm($opt_last) if $opt_last; 
    $uninterrupted=1;
    $endstream=0;
197
    eat_pcap_streams(@streams);
198
    &display_stats() if !$opt_xml and !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
199
} else { # listen serially
200
    my $stream_count;
201
    #my $ln = @streams;
Olav Kvittem's avatar
Olav Kvittem committed
202
    foreach $id (@streams) {
203
204
	alarm($opt_last) if $opt_last;
	++$stream_count;
205
	#$last_stream = 1 if $stream_count == ($ln);
206
	#print "count $stream_count  lengde  $ln \n";
Olav Kvittem's avatar
Olav Kvittem committed
207
208
209
	$uninterrupted=1;
	$endstream=0;
	init_file($id);
210
211
	eat_stream($id) if (!($opt_tcp || $opt_rtmp));
	&rtmp_eat_stream($id,$opt_period, $opt_last,0,$opt_debug,$opt_swfurl,$opt_pageurl,$opt_flashversion) if $opt_rtmp;
212
	#&eat_tcp($id,$opt_port) if $opt_tcp && !$opt_rtmp;
Olav Kvittem's avatar
Olav Kvittem committed
213
214
215
216
217
	if (! ( $opt_list || $opt_sum)){
	    &display_stats() if !$endstream;
	}
	last if ! $uninterrupted;
    }
218
    
Olav Kvittem's avatar
Olav Kvittem committed
219
220
}
&list_flows if $opt_list;
221
&display_stats() if $opt_sum and !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
222
&display_bins if $opt_bins;
223
&end_xml() if $opt_xml;
Olav Kvittem's avatar
Olav Kvittem committed
224

Olav Kvittem's avatar
Olav Kvittem committed
225
exit(0);
226
227
#
sub unit_test {
228
229
230
231
232
233
234
235
236
237
238
239
    my $test_data = shift;
    #streams =  "rtp_158.38.130.110_6711_1.pcap";
    if ($opt_mpeg){
	@streams = "testing/pcap/testdata_mpeg.pcap";
	@filter = qw "video audio";
    }
    @streams = "testing/pcap/testdata_rtp.pcap" if $opt_rtp;
    @streams = "testing/pcap/testdata_tcp.pcap" if $opt_tcp_t;
    $opt_xml = "tmp_testdata.xml";
    $opt_q =  1;
    $opt_v = 1;
    #print $streams[0], "\n";
240
241
}
sub unit_test_2{
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
    #my ($orgnial_test) = &feed_data("file",rtp_file.xml);
    my $test_data;
    $test_data = "testing/xml/testdata_mpeg.xml" if $opt_mpeg;
    $test_data = "testing/xml/testdata_rtp.xml" if $opt_rtp;
    #$test_data = "udp_testdata" if $opt_net;
    $test_data = "testing/xml/testdata_tcp.xml" if $opt_tcp;
    my ($orginal_test) = &xml_parser($test_data);
    my ($new_test) = &xml_parser("tmp_testdata.xml");
    print Dumper($orginal_test), "\n", Dumper($new_test)if $opt_debug;
    my $ignore_pattern = "jitter";  #PCR jitter is based on delta local time. 
    my $ignore_pattern2  = "dwell";
    my $ignore_pattern3 ="delay_factor";
    my $ignore_pattern4 ="gap";
    print "Checking values ....\n";
    for my $k1(keys %$orginal_test){
	if ($orginal_test->{ $k1} != $new_test->{ $k1} and $orginal_test->{ $k1} !~ m/HASH/) {
	    print "different value @ $k1, value: $orginal_test->{$k1}, differs from value: $new_test->{$k1}\n";
	}
	else {
	    printf "%2s %2s\n", "OK  @ ", "$k1 ";
262
	}
263
264
265
266
267
268
269
270
	for my $k2 ( keys %{$orginal_test->{ $k1}} ) {
	    if ( ($orginal_test->{ $k1}{$k2} != $new_test->{ $k1}{$k2}) and ($k2 !~ m/$ignore_pattern/) and ($k2 !~ m/$ignore_pattern2/) and ($k2 !~ m/$ignore_pattern3/) and ($k2 !~ m/$ignore_pattern4/)){
		print "different value @ $k1-> $k2, value: $orginal_test->{$k1}{$k2} differs from, value: $new_test->{$k1}{$k2}\n";
	    }
	    
	}
    }
    print "Test complete \n";
271
}
272

273
sub xml_parser {
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    my $file = shift;
    my $parser = new XML::DOM::Parser;
    my $doc = $parser->parsefile ($file);
    my $root = $doc->getFirstChild();
    my $nodelist = $root->getChildNodes();
    my $node = $nodelist->item(1);
    my $dup;
    my $flow_childs = $node->getChildNodes();
    my @test_array = ();
    my %test_hash = ();
    foreach my $child ($node->getChildNodes()) {
	if ($child->getNodeType == ELEMENT_NODE) {
	    if ($child->getChildNodes()){
		my @children = $child->getChildNodes();
		my $l = @children;
		if ($l > 1){
		    #print "=== " ,$child->getNodeName(), " === \n";
		    if (exists $test_hash{$child->getNodeName()}){
			$dup +=1;
		    }
		    foreach my $grandchild ($child->getChildNodes()){
			if ($grandchild->getNodeType == ELEMENT_NODE){
			    #print $grandchild->getNodeName(), "\n";
			    eval {
				$test_hash{$child->getNodeName().$dup}{$grandchild->getNodeName()}=  $grandchild->getFirstChild()->getData();
			    };
			    if ($@){
			    };
302
			}
303
304
305
306
		    }
		}
		else {
		    $test_hash{'flow'}{$child->getNodeName()} =  $child->getFirstChild()->getData();
307
     		}	
308
309
310
311
	    }
	} 
    }
    return (\%test_hash)
312
}
paalmm's avatar
paalmm committed
313

314
sub http_stream_client {
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
    my $f = shift;
    my $playlist = get("$f");
    my @list_items  = split(/\n/,$playlist);
    my @streams;
    my $duration;
    my $regex = "http://";
    my $p_chunk;
    while(1){
	foreach my $item (@list_items){
	    my @tmp_streams= get("$item");
	    if ($item=~ /$regex/g){
		foreach my $stream (@tmp_streams){
		    if ($stream =~ /EXT-X-TARGETDURATION/){
			my @temp =  split(/:/,$stream);
			$duration = $temp[1];
		    }
		    if($stream !~ /#/g){
			push(@streams,$stream);
			my $l = @streams;
			my $current_chunk = $streams[$l-1];
			if ($current_chunk ne $p_chunk){
			    my $data  = get("$current_chunk");
			    $p_chunk = $current_chunk;
			    sleep(9);
339
			}
340
		    }
341
		}
342
	    }
343
	}
344
    }
345
346
}
sub get_stream {
347
348
    my $uri = shift;
    my $data = get("$uri");
349
}
paalmm's avatar
paalmm committed
350

351
352
##TCP impl. -PMM
sub handle_threads {
353
354
    $tcp_stop = 1;
    $thr->join();
355
356
}
sub end_xml{
357
358
359
360
361
    my $filename;
    my $twig = XML::Twig->new(pretty_print => 'indented');
    my ($date,$time)  = &get_date_time();
    $filename = $opt_xml;
    $filename = $date."_".$time.".xml" if $opt_xml eq "date_time";
362
    open (MYFILE, '>'.$filename) || die "Could not open $filename : $!";
363
364
365
366
367
368
369
370
    my $final_xml = $doc->toString();
    $twig->parse($final_xml);
    $twig->flush(\*MYFILE);
    #print "</qstream> \n"  if $opt_xml eq "-";
    $doc->dispose;
    close (MYFILE);
    $xml_printed = 1;
    &unit_test_2($filename) if $opt_test;
371
372
373
}

sub tcp_packet_stats {
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    my $f = shift;
    my $ip = shift;
    my ($tcp_obj) = shift;
    my $len,$ip_hlen = 0;
    #ipv6.pm uses different variables. 
    if($opt_ipv6){
	$len = plen;
    }
    else {
	$len = len;
	$ip_hlen = 20; 
    }
    #my $ip = NetPacket::IP->decode(eth_strip($pkt));
    #my $tcp_obj = NetPacket::TCP->decode(ip_strip(eth_strip($pkt)));
    #my $f = $ip->{src_ip}.":".$tcp_obj->{src_port} if !$offline;
    my $seq_num = $tcp_obj->{seqnum};
    my $options = $tcp_obj->{options}; #12 bytes
    #print "$tcp_obj->{src_port} \n";
    if (!exists $tcp_packet_stats{$f}){
	&init_stats($f);
	$tcp_packet_stats{$f} = &share( {} );
	$seq_stats{$f} = &share( {} );
	$seq_stats_n{$f} = &share( {} );
	$tcp_packet_stats{$f}{'pkt_late'} = 0;
	$tcp_packet_stats{$f}{'pkt_retrans'} =0;
	$tcp_packet_stats{$f}{'pkt_dup'} = 0;
	
    }
    ++$tcp_packet_stats{$f}{'num_packets'};
    &tcp_jitter_stats($f,$options);
    #Bandwidth messurements
    $tcp_packet_stats{$f}{'num_bytes'} += $ip->{$len}; #counts the bytes
    #Check for duplicated,out-of-order, late and lost packets.
    $seq_stats{$f}{'seq_diff'} = $seq_num - $seq_stats{$f}{'p_seq_num'} - $seq_stats{$f}{'p_packet_len'};
    if ($seq_stats_n{$f}{'seq_num_p'} == $seq_num && $seq_stats{$f}{'p_seq_num'} != 0) {
	++$tcp_packet_stats{$f}{'pkt_dup'};
    }
    if ($seq_stats{$f}{'seq_diff'} != 0 && $seq_stats{$f}{'p_seq_num'} != 0){
	if ($seq_stats{$f}{'seq_diff'} < 0){
	    ++$tcp_packet_stats{$f}{'pkt_late'};
	    if ($seq_stats_n{$f}{'seq_num_n'} == $seq_num){
		++$tcp_packet_stats{$f}{'pkt_retrans'};
	    }
417
418
	}
	else {
419
	    ++$tcp_packet_stats{$f}{'pkt_lost'};
420
	}
421
422
423
424
425
426
427
428
    }
    if($seq_stats{$f}{'p_seq_num'} < $seq_num){
	$seq_stats{$f}{'n_seq_num'} = ($seq_num+$ip->{$len} -($ip_hlen)-($tcp_obj->{hlen}*4))
    } 
    $seq_stats{$f}{'p_seq_num'} = $seq_num;
    $seq_stats{$f}{'p_packet_len'} = $ip->{$len}-($ip_hlen)-($tcp_obj->{hlen}*4);
    $seq_stats_n{$f}{'seq_num_p'} = $seq_num;
    $seq_stats_n{$f}{'seq_num_n'} = $seq_stats{$f}{'n_seq_num'};
429
430
}
sub tcp_jitter_stats {
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
    my $f = shift;
    my $options = shift;
    my $tcp_time_stamp_echo = unpack("N",substr($options,8,4));#from our client
    my $tcp_time_stamp_delta = $tcp_time_stamp - $seq_stats{$f}{'p_tcp_time_stamp'};
    my $tcp_time_stamp_echo_delta = $tcp_time_stamp_echo - $seq_stats{$f}{'p_tcp_time_stamp_echo'}; 
    $seq_stats{$f}{'p_tcp_time_stamp'} = $tcp_time_stamp;
    $seq_stats{$f}{'p_tcp_time_stamp_echo'} = $tcp_time_stamp_echo;
    my $real_time_delta = time() - $seq_stats{$f}{'real_last_time'};
    $seq_stats{$f}{'real_last_time'} = time();
    
    #After AV streams starts
    #$first_av_packet = &get_stream_start() if $opt_rtmp;
    #$first_av_packet = $tcp_packet_stats{'num_packets'} if !$opt_rtmp and $opt_tcp;
    
    if ($tcp_packet_stats{$f}{'num_packets'} > 1){
	#gaps
	$tcp_packet_stats{$f}{'gap_sum'} += $real_time_delta;
	$tcp_packet_stats{$f}{'gap_square'} +=$real_time_delta**2;
	if ($tcp_packet_stats{$f}{'gap_max'} < $real_time_delta){
	    $tcp_packet_stats{$f}{'gap_max'} = (10**3)*$real_time_delta;
	}
	if ($tcp_packet_stats{$f}{'gap_min'} > $real_time_delta){
	    $tcp_packet_stats{$f}{'gap_min'} = (10**3)*$real_time_delta;
	}
	my $jitter  = abs($tcp_time_stamp_delta - $real_time_delta*10**3);
	if($jitter  > $tcp_packet_stats{$f}{'jitter_max'} ){
	    $tcp_packet_stats{$f}{'jitter_max'}  = $jitter; 
	}
	if($jitter  < $tcp_packet_stats{$f}{'jitter_min'} ){
	    $tcp_packet_stats{$f}{'jitter_min'}  = $jitter;
	}
	$tcp_packet_stats{$f}{'jitter_sum'} += $jitter;
	$tcp_packet_stats{$f}{'jitter_square'} += $jitter**2;
    }
    else {
	$tcp_packet_stats{$f}{'jitter_min'} = 2**31;
	$tcp_packet_stats{$f}{'jitter_max'} =  0;
	$tcp_packet_stats{$f}{'gap_max'} =  0;
	$tcp_packet_stats{$f}{'gap_min'} = 2*31;
    }
471
472
}
sub check_period {
473
474
475
476
477
478
    my $f =  shift;
    if((($opt_last)+$start_time) < time() || (($opt_period)+$start_time) < time()){
	$tcp_stop=1;
	my $span = time() - $start_time;
	&do_math($f,$span);
    }
479
480
}
sub eat_tcp {
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    my $ip;
    my $port;
    if (!$opt_rtmp) {
	$uri = shift;
	$port = shift;
	if ($ip  !~ /^\d+\.\d+\.\d+\.\d+$/){
	    ($ip,$port) =  prepare_ip($uri);
	}
	my $f = $ip.":".$port;
	if (! exists($tstart{$f})){
	    $tstart{$f} = [gettimeofday]; 
	    $first_us{$f}=$prev1s_us{$f}=$prevt100ms_us{$f}=$us;
	    if (! exists($setuptime{$f}) ){
		if ( $pinterval){
		    $setuptime{$f}=0;
		} else {
		    $setuptime{$f}=tv_interval ( $tjoined{$f}, $tstart{$f}) * 10**3;
498
		}
499
500
		printf "setup $f : %.3f\n",  $setuptime{$f} if $opt_debug;
	    }
501
	}
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
    }
    $ip = $rtmp_addr if $opt_rtmp;
    $port = 1935 unless $port;
    my ($net, $netmask, $filter_t, $err, $dev);
    #my $time_out_ms = ($opt_last*10**3) || ($opt_period*10**3) if !$opt_last;
    my $time_out_ms = 2000;
    my $filter_str = "( (src $ip and port $port ) )";
    $dev = Net::Pcap::lookupdev(\$err);  # find a device	
    my $r = Net::Pcap::lookupnet( $dev, \$net, \$netmask, \$err);
    my $pcap = Net::Pcap::open_live($dev, 1514, 1, $time_out_ms, \$err) || die "You need to run this as sudo $err";
    $r=Net::Pcap::compile($pcap, \$filter_t, $filter_str, 0,$netmask);
    $r=Net::Pcap::setfilter($pcap, $filter_t);
    while (my $pkt=Net::Pcap::next($pcap,\%hdr)) { # get all packets
	
	my $ip_obj = NetPacket::IP->decode(eth_strip($pkt));
	my $tcp_obj = NetPacket::TCP->decode(ip_strip(eth_strip($pkt)));
	if($tcp_obj->{src_port} == $port && $ip_obj->{src_ip} eq $ip ){
	    my $f = $ip_obj->{src_ip}.":".$tcp_obj->{src_port};
	    check_period($f) if $opt_last;
	    last if $tcp_stop==1;
	    &tcp_packet_stats($pkt);
523
	}
524
    }
525
526
527
}
##RTMP impl. -PMM
sub rtmp_eat_stream {
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
    ($rtmp,$period,$last,$reconnect,$debug,$swfurl,$pageurl,$flash_version) = @_;
    ($address, $app, $play) = rtmp_prepare_url($rtmp);
    $rtmp_addr = $address;
    $port = 1935 unless $opt_port;
    my $f = $address.":".$port;
    while(!$endstream){
	$start_time = time();
	if (! exists($tstart{$f})){
	    $tstart{$f} = [gettimeofday]; 
	    $first_us{$f}=$prev1s_us{$f}=$prevt100ms_us{$f}=$us;
	    if (! exists($setuptime{$f}) ){
		if ( $pinterval){
		    $setuptime{$f}=0;
		} else {
		    $setuptime{$f}=tv_interval ( $tjoined{$f}, $tstart{$f}) * 10**3;
543
		}
544
545
		printf "setup $f : %.3f\n",  $setuptime{$f} if $opt_debug;
	    }
546
	}
547
548
549
550
551
552
553
554
	$thr = threads->new(\&eat_tcp) if $opt_tcp and !$reconnect;
	rtmp_connect($address, '1935', $app , $swfurl, $pageurl,$flash_version) if $opt_rtmp;
	rtmp_play($play, '-1.0', '-1.0', $period, $last,$debug)  if $opt_rtmp;
	&display_stats() if ($opt_period != $opt_last) and !$endstream;
	my $pkt_stat = $thr->join if $opt_tcp;
	last if  $endstream;
	
    }
555
}
Olav Kvittem's avatar
Olav Kvittem committed
556
557


558
sub rtmp_prepare_url{
559
560
561
562
563
564
565
566
567
568
569
    my $uri = shift;
    #	return 0  if $uri != m/^(rtmp)://; 
    $uri =~ s/^(rtmp)://;
    $uri =~ s#//##;
    @string = split(/\//, $uri);
    my $length = $#string;
    my $host = $string[0];
    my $app = '';
    if ($host !~ /^\d+\.\d+\.\d+\.\d+$/){ # hostname
	my $padr=gethostbyname($host) || die "Gethostbyname - $host - $padr : $!";
	$address=Socket::inet_ntoa($padr);
570
    }    
571
572
573
574
575
576
    for (my $i = 1; $i < $length;$i++){
	$app .= $string[$i];
	if ($i > 1 ){$app .="/";}
    }
    my $play = $string[$length];
    return ($address,$app,$play)
577
}
Olav Kvittem's avatar
Olav Kvittem committed
578
579
580
581
582
#------------------------------------------------------------------------------

# unpack uri's to ip-address
sub prepare_ip {
    my $uri=shift;
583
    $uri =~ s/^(udp|rtp|http)://;
Olav Kvittem's avatar
Olav Kvittem committed
584
585
    $uri =~ s#//##;
    die "Unsupported streaming $1" if $uri =~/^(\w:)/;
Olav Kvittem's avatar
Olav Kvittem committed
586
587
588
    if ($multicast = ($uri =~ /@/)){
	($source, $uri)=split(/@/, $uri);
    }
Olav Kvittem's avatar
Olav Kvittem committed
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646

    my ($host, $port)=split(/[\/:]/, $uri);
    die $usage . "undefined address inn $host" unless (defined $host);
    $port = $DEFAULT_PORT unless (defined $port);
    my $address=$host;
    if ($host !~ /^\d+\.\d+\.\d+\.\d+$/){ # hostname
	my $padr=gethostbyname($host) || die "Gethostbyname - $host - $padr : $!";
	$address=Socket::inet_ntoa($padr);
    }   
    my @nib=split(/\./,$address);
    $multicast = $multicast || $nib[0] >= 224 && $nib[0] <= 240;
    return ($address, $port, $multicast);

}

sub eat_stream {
    my $id=shift;
    $tstart{$id}=[gettimeofday];
    if ($opt_net && $opt_rtp){
	# Create RTP socket
	my $elapsed=0;
	my $t0=0.0;
	$n_packets=0;
	my ($address, $port, $multicast) = prepare_ip($id);
	my $rtp = new Net::RTP( LocalPort=>$port,
				LocalAddr=>$address ) || die "Failed to create RTP socket: $!";
	my $select=IO::Select->new($rtp->fileno);

	my $tjoined=[gettimeofday];
	if ($opt_align){ # align to $opt_period
	}

	while ($uninterrupted && !($opt_packets && $n_packets > $opt_packets)) {
	    my $packet;
	    last if ! $uninterrupted || $endstream;
	    if ($select->can_read($timeout) && ($packet = $rtp->recv())){
		my $ssrc = $packet->ssrc();
		$cur_id=$packet->source_ip . ", $address:$port";

		if ( !$file{$cur_id}){
		    push(@myflows, $cur_id);
		    $file{$cur_id}=$id;
		}
		$n_packets++;
		next if $opt_src and $opt_src ne $cur_id;

		$tc=[gettimeofday]; # current flow time
		if( $t0){
		    $elapsed=tv_interval ( $t0, $tc);
		} else {
		    $t0 = $tc;
		    $elapsed=0.0;
		    &init_stats($cur_id);
		}
		$us=$elapsed*10**6;
		$tjoined{$cur_id} = $tjoined if !$tjoined{$cur_id};
		# No stats for that SSRC yet?
		&pkt_stats($cur_id, $packet->encode, $elapsed * 10**6, $packet->size());
647
		
Olav Kvittem's avatar
Olav Kvittem committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
		if ($opt_dump){
		    print DUMP $packet;
		}
	    } else { # anticipate interrupted by alarm	
#		warn "Failed to receive $n_packets packets from $address:$port : $!";
#		next; #### exit
	    }
	}
#	&display_stats();

#	$mc->shutdown; # gives error message

    } elsif ($opt_net) {			# udp stream

	# Create UDP socket
	my $elapsed=0;
	my $t0=0.0;
        $n_packets=0;
	my ($address, $port, $multicast) = prepare_ip($id);
	my $mc;

	if ( $multicast){
	    $mc = IO::Socket::Multicast->new(LocalPort=>$port, LocalAddr=>$address, ReuseAddr=>1) ||
		die "Could not connect to port $port : $!";
	    $mc->mcast_add($address) || die "could not join $address:$port : $!";
	} else { # unicast
	    $mc = IO::Socket::INET->new( LocalAddr=> $address, LocalPort => $port);
	}
	my $tjoined=[gettimeofday];

	my $select=IO::Select->new($mc->fileno);

	if ($opt_align){ # align to $opt_period
	}

	while ($uninterrupted && ! $endstream && !($opt_packets && $n_packets > $opt_packets)){
	    
	    if ($select->can_read($timeout) && $mc->recv($packet, 1560)) {
		last if ! $uninterrupted || $endstream;
		$n_packets++;
		my $ssrc = $mc->peerhost;
		my $cur_id = $mc->peerhost . ":" . $mc->peerport  . "->$address:$port";
		if ( !$file{$cur_id}){
		    push(@myflows, $cur_id);
		    $file{$cur_id}=$id;
		}
		
		next if $opt_src and $opt_src ne $cur_id;

		$tc=[gettimeofday]; # current flow time
		if( $t0){
		    $elapsed=tv_interval ( $t0, $tc);
		} else {
		    $t0 = $tc;
		    $elapsed=0.0;
		    &init_stats($cur_id);
		}
		$us=$elapsed*10**6;
		
		$tjoined{$cur_id} = $tjoined if !$tjoined{$cur_id};
		# No stats for that SSRC yet?
		&pkt_stats($cur_id, $packet, $elapsed * 10**6, length($packet));
		if ($opt_dump){
		    print DUMP $packet;
		}
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
713
		$endstream = tv_interval($t0, $tc) >= $opt_last if $opt_last && !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
	    } else { # assume that we got an interrupt
#		die "Failed to receive packet: $!"; 
	    }
	}
#	&display_stats();
#        $mc->mcast_drop($address);

    } else {			# files
	my $f=$id;
	my $pcap; 
	my $tmp_file;

	my $pcap_file=$f;
	if ($f =~ /\.gz$|\.Z/){ # gzipped ?
	    $tmp_file="/tmp/pcap$$";
	    $pcap_file=$tmp_file;
	    system( "/bin/zcat $f > $tmp_file") ; # rc code ? || die "Could not unpack $f to $tmp_file : $!";
	}
732
733
	if ($opt_crude){
	    my $flow_key;
Olav Kvittem's avatar
Olav Kvittem committed
734
	    open CRUDE, "<$pcap_file" || die "Could not open $f : $err";
735
	    while(<CRUDE>){  # crude.sourceforge.net
Olav Kvittem's avatar
Olav Kvittem committed
736
737
		my $tx0;
		
738
		if (($fid, $seq, $src, $dst, $tx, $rx, $size)=
739
		    /ID=(\d+)\s+SEQ=(\d+)\s+SRC=([\d.:]+)\s+DST=([\d.:]+)\s+Tx=([\d.,]+)\s+Rx=([\d.,]+)\s+SIZE=(\d+)/){
740
741
742

		    $flow_key="$src->$dst";
		    $flow_pkts{$flow_key}++;
Olav Kvittem's avatar
Olav Kvittem committed
743
		    $tc=[int($rx), ($tx-int($rx))*10^6] ; # tval
744
745
746
747
748
		    if ($flow_pkts{$flow_key} <= 1) { # new flow
			push(@flows, $flow_key);  # 
			$flow_no++; 
			$file{$flow_key}=$f;
			$source{$flow_key}=$src;
Olav Kvittem's avatar
Olav Kvittem committed
749
750
751
752
753
			$rx0{$flow_key}=$rx;
			$tx0{$flow_key}=$tx;
			$tjoined{$flow_key}=$tc;
		    } 
		    
754
755
756
757
758
759
760
761
762
		    next if $opt_list;  # just count packets per flow
		    next if $opt_flow_key && $flow_key !~ $wanted_flow;
		    next if $opt_flow_no && $opt_flow_no !~ /\b$flow_no\b/;
		    
		    if ($flow_pkts{$flow_key} <= 1) { #
			&init_stats($flow_key);
			push (@myflows, $flow_key);
		    }

Olav Kvittem's avatar
Olav Kvittem committed
763
764
765
766
767
768
769
		    $seq_num{$flow_key}=$seq;
		    $ptx{$flow_key}=$tx{$flow_key}; # previous
		    $tx{$flow_key}=$tx;
		    $rx{$flow_key}=$rx;
		    $mtime{$flow_key}=($tx-$tx0{$flow_key})*10**6;
		    $us=($rx-$rx0{$flow_key})*10**6; # us
		    &pkt_stats($flow_key, 0, $us, $size);
770
		}
Olav Kvittem's avatar
Olav Kvittem committed
771
	    }
772
773
774
775
776
777
	    
	} else {
	    if ($pcap=Net::Pcap::open_offline($pcap_file, \$err)){ 
		#	my $select=IO::Select->new(Net::Pcap::fileno($pcap));
		until ( ! $uninterrupted || $endstream || ($opt_packets && $n_packets > $opt_packets) 
			|| ! (my $pkt=Net::Pcap::next($pcap, \%hdr) ) ) {
778
779
		    #$file_flag =1;
		    $offline =1;
780
781
782
		    eat_pcap($f, $pcap, $pkt);
		    $n_packets++;
		}
Olav Kvittem's avatar
Olav Kvittem committed
783
784
		printf "%s\n", $ttl_log if  $opt_ttl;

785
786
787
788
	    } else { # open failed 
		if ($format ne "full"){ # always put out data when format full
		    die "Could not open $f : $err";
		}
Olav Kvittem's avatar
Olav Kvittem committed
789
790
791
792
793
794
795
796
797
798
799
	    }
	}
	undef $t0;
	unlink $tmpfile if $tmp_file;
    } # of files
} # of eat_stream


sub eat_pcap_streams{ # open groups and listen on pcap
    my @streams=@_; 
    my $filter="( ";
800
    my ($address, $port, $multicast);
Olav Kvittem's avatar
Olav Kvittem committed
801
    my $mc = IO::Socket::Multicast->new(LocalPort=>$DEFAULT_PORT, ReuseAddr=>1) ||
Olav Kvittem's avatar
Olav Kvittem committed
802
	die "Could not connect to port $port : $!";
Olav Kvittem's avatar
Olav Kvittem committed
803
    foreach $id (@streams){ # listen to all groups
804
	if ($opt_ipv6){
805
806
	    $address = Net::IPv6Addr::ipv6_parse($id);
	    $port =  $opt_p;
807
808
	}
	else {
809
	    ($address, $port, $multicast) = prepare_ip($id);
810
	}
Olav Kvittem's avatar
Olav Kvittem committed
811
812
813
814
815
816
	$filter .= " or " if $filter =~ /\(\s+\(/;
	if ($multicast){
	    $mc->mcast_add($address) || die "could not join $address:$port : $!";
	    $filter .= "(dst $address and port $port)"; 	
	} else {
	    $filter .= "(src $address and port $port)"; 
817
	}
Olav Kvittem's avatar
Olav Kvittem committed
818
819
820
821
822
823
824
    }
    $tjoined{$f}=[gettimeofday];
    $filter .= " )";
#    $filter .= " and ip multicast";
#    $filter ="ip";
    my ($net, $netmask, $filter_t, $err, $dev);
#    printf $filter;
825
    $dev = Net::Pcap::lookupdev(\$err) || die "Could not lookupdev $dev : $err : $! .. Needs to run as sudo ..";
Olav Kvittem's avatar
Olav Kvittem committed
826
827
    my $r = Net::Pcap::lookupnet( $dev, \$net, \$netmask, \$err);
    die "Failed lookupnet for $dev : $r : $err : $!" if $r != 0;
828
829
830
    my $snap_length = 100;
    $snap_length = 1514 if $opt_mpeg;
    $pcap=Net::Pcap::open_live( $dev, $snap_length, 1, 0,\$err) || die "Could not open $dev  : $err";
Olav Kvittem's avatar
Olav Kvittem committed
831
832
833
834
835
836
837
838
839
840
841
    $r=Net::Pcap::compile($pcap, \$filter_t, $filter, 0, $netmask);
    die "Could not compile filter : $r : $filter : $!)" if $r != 0;
    $r=Net::Pcap::setfilter($pcap, $filter_t);
    die "Could not set pcap filter : $r : $filter : $! " if $r != 0 ;
    my $select=IO::Select->new( Net::Pcap::fileno($pcap));
    while ($select->can_read($timeout) && (my $pkt=Net::Pcap::next($pcap, \%hdr)) ) { # get all packets
	last if ! $uninterrupted || $endstream;
	eat_pcap("pcap", $pcap, $pkt);
	$n_packets++;
	last if  $opt_packets && $n_packets > $opt_packets;
    }
Olav Kvittem's avatar
Olav Kvittem committed
842
843
    printf "%s\n", $ttl_log if  $opt_ttl;

Olav Kvittem's avatar
Olav Kvittem committed
844
845
}

846
sub set_network_info{
847
848
849
850
851
    my ($f,$src_ip,$src_port,$dst_ip,$dst_port) = @_;
    $network_info{$f}{'src_ip'} = $src_ip;
    $network_info{$f}{'dst_ip'} = $dst_ip;
    $network_info{$f}{'src_port'} = $src_port;
    $network_info{$f}{'dst_port'} = $dst_port;	
852
}
853
sub eat_pcap { # process pcap packets	
Olav Kvittem's avatar
Olav Kvittem committed
854
855
    my ($f, $pcap, $pkt)=@_;
    my $ip=null;
856
    my $ip_payload;# udp or tcp packet.
Olav Kvittem's avatar
Olav Kvittem committed
857
    my $rtp = new Net::RTP::Packet(); # spare rtp obj
858
    my $proto,$len=null;
Olav Kvittem's avatar
Olav Kvittem committed
859
    if (Net::Pcap::datalink($pcap) == 1){ # ethernet
860
    	if ($opt_ipv6){
861
862
863
864
865
866
867
868
869
870
871
	    $ip = NetPacket::IPv6->decode(eth_strip($pkt));
	    $proto = $ip->{nxt};
	    $len = plen;
	    $hlen =  0;
	}
	else {
	    $ip = NetPacket::IP->decode(eth_strip($pkt));
	    $proto = $ip->{proto};
	    $len = len;
	    $hlen = ($ip->{hlen}*4);
	}
Olav Kvittem's avatar
Olav Kvittem committed
872
873
874
875
876
877
    } elsif (Net::Pcap::datalink($pcap) == 113) { # DLT_LINUX_SLL
	my ($head, $data)=unpack('a16a*', $pkt);  
	$ip = NetPacket::IP->decode($data);
    } else {
	die "Invalid link layer type : ".Net::Pcap::datalink($pcap);
    }
878
    
879
880
    if ($proto == IP_PROTO_TCP){
    	$opt_tcp = 1; 
881
	$ip_payload = NetPacket::TCP->decode($ip->{data});
882
883
884
885
886
    }
    else {
    	$ip_payload = NetPacket::UDP->decode($ip->{data});
    }
    my $dlen = $ip->{$len} - $hlen;
Olav Kvittem's avatar
Olav Kvittem committed
887
888
889
    my $flow_key;
    if ($opt_sum){
	$flow_key=$opt_sum;
890
891
892
893
894
    }
    elsif ($file_flag){
    	$flow_key = $f;
    }
    else {
895
	$flow_key=sprintf "%s:%d->%s:%d", 
896
        $ip->{src_ip}, $ip_payload->{src_port}, $ip->{dest_ip}, $ip_payload->{dest_port};
Olav Kvittem's avatar
Olav Kvittem committed
897
    }
898
    set_network_info($f,$ip->{src_ip}, $ip_payload->{src_port}, $ip->{dest_ip}, $ip_payload->{dest_port});
Olav Kvittem's avatar
Olav Kvittem committed
899
    $flow_pkts{$flow_key}++;
900
    #next if $ip->{proto} != IP_PROTO_UDP;
Olav Kvittem's avatar
Olav Kvittem committed
901
902
903
904
    if ($flow_pkts{$flow_key} <= 1) { # new flow
	push(@flows, $flow_key);  # 
	$flow_no++; 
	$file{$flow_key}=$f;
905
	$source{$flow_key} =sprintf "%s:%d", $ip->{src_ip}, $ip_payload->{src_port};
Olav Kvittem's avatar
Olav Kvittem committed
906
    }
Olav Kvittem's avatar
Olav Kvittem committed
907
908
909
910
911
912
913
    if ($opt_ttl){
	if ($pttl{$flow_key} && ($opt_log ||  ($pttl{$flow_key} !=  $ip->{ttl}))){ # change in ttl
	    printf "%s\n",  $ttl_log;
	    $nttl{$flow_key}=0;		
	}
	$pttl{$flow_key}= $ip->{ttl};
	$nttl{$flow_key}++;
914
	($sequence_number{$flow_key}, $tx_time_seconds{$flow_key}, $tx_time_useconds{$flow_key}, $flow_id{$flow_key})= unpack('NNNN', $ip_payload->{data});
Olav Kvittem's avatar
Olav Kvittem committed
915
916
917
918
919
920
	$ttl_log=sprintf "ID=%u SEQ=%u SRC=%s DST=%s Tx=%u.%06u  Rx=%u.%06u TTL=%u",  
	$flow_id{$flow_key}, $sequence_number{$flow_key}, 
	$ip->{src_ip}, $ip->{dest_ip}, $tx_time_seconds{$flow_key}, 
	$tx_time_useconds{$flow_key}, $hdr{tv_sec}, $hdr{tv_usec}, $pttl{$flow_key};
	$ttl_log.= sprintf(" No=%08d\n",  $nttl{$flow_key},) if !$opt_log;
    }
Olav Kvittem's avatar
Olav Kvittem committed
921
922
923
924
925
926
    next if $opt_list;  # just count packets per flow
    next if $opt_flow_key && $flow_key !~ $wanted_flow;
    next if $opt_flow_no && $opt_flow_no !~ /\b$flow_no\b/;

    if ($flow_pkts{$flow_key} <= 1) { #
	&init_stats($flow_key);
927
	push (@myflows, $flow_key);
Olav Kvittem's avatar
Olav Kvittem committed
928
    }
929
930
931
932
    $tc= [$hdr{tv_sec}, $hdr{tv_usec}];
    if (! defined($t0)) {
	$t0=$tc;
    }
Olav Kvittem's avatar
Olav Kvittem committed
933
934
    $tjoined{$f}=$tc if !$tjoined{$f};
    $tjoined{$flow_key} = $tjoined{$f} if !exists($tjoined{$flow_key});
935
    $us = tv_interval ( $t0, $tc) * 10**6 ; # us relative packet time 	  
Olav Kvittem's avatar
Olav Kvittem committed
936
937
# fixate on first flow to appear
#    if ( $first_port) {
938
#	next if $ip_payload->{dest_port} != $first_port;
Olav Kvittem's avatar
Olav Kvittem committed
939
#    } else {
940
#	$first_port =  $ip_payload->{dest_port};
Olav Kvittem's avatar
Olav Kvittem committed
941
#    }
942
943
944
    if ($opt_tcp){
	&tcp_packet_stats($flow_key,$ip,$ip_payload);
    }
945
    &pkt_stats($flow_key, $ip_payload->{data}, $us, $dlen);
Olav Kvittem's avatar
Olav Kvittem committed
946
}
947
948

#--------------------------------------------------------------------------------
Olav Kvittem's avatar
Olav Kvittem committed
949
sub init_stats {
Olav Kvittem's avatar
Olav Kvittem committed
950
951
952
    my $f=shift;
    $pinterval=0;
    $npkt{$f}=$sumbyte{$f}=$ssbyte{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
953
    $first_sec=0;
Olav Kvittem's avatar
Olav Kvittem committed
954
955
956
957
    delete $pmtime{$f}; delete $pus{$f} ; delete $first_us{$f};  delete $last_us{$f}; 
    delete $prev_1s{$f}; delete $prev_100ms{$f};
    delete $per1s{$f}; delete $per100ms{$f};
    delete $sum1s{$f}; delete $sum100ms{$f};
958
    $n100ms{$f}=0; $sum100ms{$f}=0; $ss100bps{$f}=0; $akk100ms{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
959
960
961
962
963
964
965
966
967
968
    $sumbyte{$f}=$sumps{$f}=0;			# sum byte per second
    $maxbps{$f}=0; $max100ms{$f}=0;
    $dup{$f}=$lost{$f}=$late{$f}=0;
    $maxbyte{$f}=0; $minbyte{$f}=2**31;
    $sumgap{$f}=$ssgap{$f}=0;
    $ngap{$f}=0; $maxgap{$f}=0, $mingap{$f}=2**63-5;
    $njitter{$f}=$sumjitter{$f}=$ssjitter{$f}=0;
    $maxjitter{$f}=-2**63-5; $minjitter{$f}=2**63-5; delete $est_jitter{$f}; delete $pest_jitter{$f}; 
    $first_port = 0;  $n_seq=-1;
    delete $tstart{$f}; delete $tend{$f}; 
969
    
Olav Kvittem's avatar
Olav Kvittem committed
970
971

#MPEG-TS initalizing and resetting (new period!)
972
    
Olav Kvittem's avatar
Olav Kvittem committed
973
974
    $discontinuities = 0;
    %last_ccs = ();
975
    %mpeg_stats = ();
Olav Kvittem's avatar
Olav Kvittem committed
976
977
978
979
    @cc = ();
# r_stats {filename/handle}
    @pcr_stats{$f} = ();
#P-stats {filename/handle}
980
    @pts_stats{$f,$TS} = ();
Olav Kvittem's avatar
Olav Kvittem committed
981
982
983
984
985
986
987
    @mpeg_stats{$f} = ();
#P stuff
    $late_video_packets = 0;
#MPEG-TS END
    %dist1 = ();
    %dist2 = ();
    $num_cc = 0;
988
989
}
sub format_numbers{
990
991
992
993
994
995
996
    my $value =  shift;
    if ($value =~ /^[a-zA-z][\D]*$/ || $value  !~ /^\d+\.\d+$/) {
	return sprintf("%1.0f",$value);
    }
    else {
	return sprintf("%0.5f",$value);
    }
997
998
}
sub xml_dump_rtmp_tcp {
999
    my $f = $_[0];  # *Not* shift. This gets called from a signal handler.
1000
1001
1002
1003
    return 0 if $xml_printed;
    my ($rtmp_jitter_stats,$rtmp_stats,$span)  = get_rtmp_data() if $opt_rtmp;
    my ($tcp_packet_stats) = (\%tcp_packet_stats) if $opt_tcp;
    my $xml_version="1.0";
1004
    if ($period_num <= 1 ){
1005
	$doc = XML::DOM::Document->new;
1006
    	my $decl = $doc->createXMLDecl('1.0');
1007
1008
1009
1010
	$doc-> setXMLDecl($decl);
	$qstream = $doc->createElement('qstream');
	$qstream->setAttribute('version',$xml_version);
	$doc->appendChild($qstream);
1011
    }
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
    ($address,$app,$play) = rtmp_prepare_url($f) if $rtmp;
    #($address, $port) = prepare_ip($f) if $opt_tcp && !$opt_rtmp;
    #$port  = 1935 unless $opt_port;
    #my $id=$address.":".$port;
    my $id = $f;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tstart{$f}->[0]);
    my $time=sprintf "%02d:%02d:%02d", $hour, $min, $sec;
    my $date=sprintf "%4d-%02d-%02d", $year+1900, $mon+1, $mday;
    my $span=&tv_interval($tstart{$f}, $tend{$f}); # second
    &do_math($id,$span);
1022
    my $flow = $doc->createElement('flow');
1023
1024
1025
    my $flow_id = $doc->createElement('flow_id');
    my $rtmp_stats_xml  = $doc->createElement('rtmp_stats');
    my $dateXML = $doc->createElement('date');
1026
1027
1028
1029
1030
1031
1032
1033
1034
    my $timeXML = $doc->createElement('time');
    my $spanXML = $doc->createElement('span'); 
    $qstream->appendChild($flow);
    $flow->appendChild($flow_id);
    $flow->appendChild($dateXML);
    $flow->appendChild($timeXML);
    $flow->appendChild($rtmp_stats_xml) if $opt_rtmp;
    $rtmp_stats_xml->appendChild($spanXML);    

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
    #source and destination ip. 
    $flow_id->addText($id);
    
    #datetime 
    $dateXML->addText($date);
    $timeXML->addText($time);
    $spanXML->addText($span);

    if ($opt_rtmp) {
	for my $k1 ( sort keys %$rtmp_stats ) {
	    my $element = $doc->createElement($k1);	
	    $rtmp_stats_xml->appendChild($element);
	    $element->addText(&format_numbers($rtmp_stats->{ $k1 }));	
	}
	foreach $k1 (sort keys %$rtmp_jitter_stats ){
	    if ($rtmp_jitter_stats->{ $k1 }{ 'type' } eq "Video" || $rtmp_jitter_stats->{ $k1 }{ 'type'} eq "Audio" ){
		my $chunk = $doc->createElement('chunk');
		$chunk->setAttribute('id', $k1);
		$rtmp_stats_xml->appendChild($chunk);
		for my $k2 (sort keys %{$rtmp_jitter_stats->{ $k1 }} ) {
		    my $element = $doc->createElement($k2);
		    $chunk->appendChild($element);
		    $value = &format_numbers($rtmp_jitter_stats->{ $k1 }{ $k2 }); 
		    $element->addText(&format_numbers($value));
		}   
	    }     
1061
	}
1062
    }
1063
1064
1065
1066
    #bandwidth	
    if ($opt_tcp){
    	$f = $id;
    	my $bw_stats = $doc->createElement('bandwidth_stats');
1067
1068
1069
	my $avg_bw = $doc->createElement('bandwidth_avg');
    	my $max_bw = $doc->createElement('bandwidth_max_1s');
	my $max_bw_100ms = $doc->createElement('bandwidth_max_100ms');
1070
1071
1072
1073
1074
1075
    	my $sdv_bw = $doc->createElement('bandwidth_sdv');
    	$flow->appendChild($bw_stats);
    	$bw_stats->appendChild($avg_bw);
    	$bw_stats->appendChild($max_bw);
    	$bw_stats->appendChild($max_bw_100ms);
    	$bw_stats->appendChild($sdv_bw);
1076
    	$avg_bw->addText(&format_numbers( avg( $bw_stats{$f}{'sumbyte'}*8, $span )));
1077
1078
1079
1080
1081
	$max_bw->addText(&format_numbers($bw_stats{$f}{'maxbps'}));
	$max_bw_100ms->addText(&format_numbers($bw_stats{$f}{'max100ms'}));
	$sdv_bw->addText(&format_numbers(sdv($bw_stats{$f}{'n100ms'},
					     $bw_stats{$f}{'akk100ms'}, 
					     $bw_stats{$f}{'ss100bps'})));
1082
1083
    	my $child = $doc->createElement('tcp_packet_stats');
    	$flow->appendChild($child);
1084
1085
1086
1087
1088
1089
1090
	for my $k2 (sort keys %{$tcp_packet_stats->{ $id }} ) {
	    my $grandchild = $doc->createElement($k2);
	    $value = &format_numbers($tcp_packet_stats->{$id}{$k2});
	    $grandchild->addText($value);	
	    $child->appendChild($grandchild);
	}
    } 
1091
1092
1093
1094
    #my $final_xml = $flow->toString();
    &init_stats($id);
}

1095
sub xml_new {
1096
1097
    my ($pcr_stats,$pts_stats,$mpeg_stats) = (\%pcr_stats, \%pts_stats,\%mpeg_stats) if $opt_mpeg;
    my $doc = XML::DOM::Document->new;
1098
1099
1100
1101
1102
1103
1104
1105
1106
    my $decl = $doc->createXMLDecl('1.0');
    my $flow = $doc->createElement('flow');
    my $src_dst = $doc->createElement('src_dst');
    my $dateXML = $doc->createElement('date');
    my $timeXML = $doc->createElement('time');
    my @elements_network_stats  = qw "src_dst date time network_stats setuptime gap";
    my @elements_pcr  = qw "pcr_avg_jitter pcr_sdv_jitter pcr_jitter_min pcr_jitter_max";
    my @elements_pts  = qw "dwell_sum dwell_num";
    my @elements_rtp = qw " ";
1107
    my $mpeg_stats_parrent = $doc->createElement('mpeg_stats');
1108
1109
1110
1111
    my $pcr_stats_parrent = $doc->createElement('pcr_stats');
    my $transport_streams = $doc->createElement('transport_streams');
    my $pts_stats_parrent = $doc->createElement('pts_stats');

1112
    #append static childs
1113
1114
1115
    $doc->appendChild($flow);
    $flow->appendChild($pcr_stats_parrent);
    $flow->appendChild($transport_streams);
1116
1117
1118
1119
1120
    $transport_streams->appendChild($pts_stats_parrent);
    $transport_streams->appendChild($mpeg_stats_parrent);
    
    #loop through hashes
    for my $k1 ( sort keys %mpeg_stats ) {
1121
    	for my $k2 ( keys %{$mpeg_stats->{ $k1 }} ) {
1122
1123
1124
1125
1126
	    for my $k3 (keys %{$mpeg_stats->{ $k1 } { $k2 }}){
		#$child = $doc->createElement($k3);
		#$child->addText(&lesbar($mpeg_stats->{ $k1 }{ $k2 }{ $k3 }));
		#$mpeg_stats_parrent->appendChild($child);		
	    }
1127
    	}
1128
    }
1129
1130
1131
    
    for my $k1 ( sort keys %$pcr_stats ) {
    	for my $k2 ( keys %{$pcr_stats->{ $k1 }} ) {
1132
1133
1134
1135
1136
1137
1138
	    foreach my $element (@elements_pcr){
		if ($element eq $k2){
		    $child = $doc->createElement($k2);
		    $child->addText(&lesbar($pcr_stats->{ $k1 }{ $k2 }));
		    $pcr_stats_parrent->appendChild($child);		
		}
	    }
1139
      	}
1140
1141
    }
    
1142
1143
    for my $k1 ( sort keys %$pts_stats ) {
    	for my $k2 ( keys %{$pts_stats->{ $k1 }} ) {
1144
1145
1146
1147
1148
1149
1150
1151
1152
	    for my $k3 (keys %{$pts_stats->{ $k1 } { $k2 }}){
		foreach my $element (@elements_pts){
		    if ($element eq $k3){
			$child = $doc->createElement($k3);
			$child->addText(&lesbar($pts_stats->{ $k1 }{ $k2 }{ $k3 }));
			$pcr_stats_parrent->appendChild($child);		
		    }	
		}
	    }
1153
      	}
1154
    }
1155
1156
1157
1158
}


sub xml_dump {
1159
    my $f = $_[0];  # *Not* shift. This gets called from a signal handler.
1160
1161
1162
1163
    return 0 if $xml_printed;
    my ($network_info) = (\%network_info);
    my ($pcr_stats,$pts_stats) = (\%pcr_stats, \%pts_stats) if $opt_mpeg;
    my $xml_version="1.0";
1164
    if ($period_num <= 1 ){
1165
	$doc = XML::DOM::Document->new;
1166
    	my $decl = $doc->createXMLDecl('1.0');
1167
1168
1169
1170
	$doc-> setXMLDecl($decl);
	$qstream = $doc->createElement('qstream');
	$qstream->setAttribute('version',$xml_version);
	$doc->appendChild($qstream);
1171
    }
1172
1173
1174
1175
    my $flow = $doc->createElement('flow');
    my $flow_id = $doc->createElement('flow_id');
    my $flow_nameXML = $doc->createElement('flow_name');
    my $dateXML = $doc->createElement('date');
1176
    my $timeXML = $doc->createElement('time');
1177
    my $spanXML = $doc->createElement('span');
1178
    my $pcr_jitter_parrent = $doc->createElement('pcr_jitter') if $opt_mpeg;
1179
    my $network_info_parrent = $doc->createElement('network_info');
1180
1181
    $qstream->appendChild($flow);
    $flow->appendChild($flow_id);
1182
    $flow->appendChild($flow_nameXML);
1183
1184
    $flow->appendChild($dateXML);
    $flow->appendChild($timeXML);
1185
1186
    $flow->appendChild($spanXML);
    $flow->appendChild($network_info_parrent);
1187
    $flow->appendChild($pcr_jitter_parrent) if $opt_mpeg;
1188
1189
    my $media="file";
    $media = "host:port" if $opt_net;
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
    my $id=$f;
    $flow_name = $id;
    $id = $opt_id if $opt_id;
    $flow_name = $opt_name if $opt_name;
    next if $npkt{$f} < $opt_flow_min;
    my $source=$f; 
    $source=$source{$f} if  ! $opt_v && $source{$f} ne "";
    $source=$opt_id if $opt_id;
    my $jitter_stat= $opt_rtp || $opt_crude;

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tstart{$f}->[0]);
    my $time=sprintf "%02d:%02d:%02d", $hour, $min, $sec;
    if ($format eq "full"){
	$time.=sprintf ".%06d", $tstart{$f}->[1];
    } else {
1205
	my $time=sprintf "%02d:%02d:%02d", $hour, $min, $sec;
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
    }
    my $date=sprintf "%4d-%02d-%02d", $year+1900, $mon+1, $mday;
    my $span=&tv_interval($tstart{$f}, $tend{$f}); # second
    &do_math($f,$span);
    &burst($f); # in case a second is interrupted
    #source and destination ip. 
    $flow_id->addText($id);
    $flow_nameXML->addText($flow_name);
    #datetime 
    $dateXML->addText($date);
    $timeXML->addText($time);
    $spanXML->addText($span);
    for my $k1 ( sort keys %network_info ) {
1219
    	for my $k2 ( keys %{$network_info->{ $k1 }} ) {
1220
1221
1222
	    $child = $doc->createElement($k2);
	    $child->addText($network_info->{ $k1 }{ $k2 });
	    $network_info_parrent->appendChild($child);
1223
    	}
1224
    }
1225
1226
1227
1228
1229
    #network_stats
    my $network_stats = $doc->createElement('network_stats');
    my $setuptimeXML = $doc->createElement('setuptime');
    my $gap_avgXML = $doc->createElement('gap_avg');
    my $gap_sdvXML = $doc->createElement('gap_sdv');
1230
1231
    my $gap_minXML = $doc->createElement('gap_min');
    my $gap_maxXML = $doc->createElement('gap_max');
1232
1233
1234
    $network_stats->appendChild($setuptimeXML);
    $network_stats->appendChild($gap_avgXML);
    $network_stats->appendChild($gap_sdvXML);
1235
1236
    $network_stats->appendChild($gap_minXML);
    $network_stats->appendChild($gap_maxXML); 
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
    $flow->appendChild($network_stats);
    #packets_stats
    my $packet_stats = $doc->createElement('packet_stats');
    my $packets_numbXML = $doc->createElement('packet_count');
    my $packets_sizeXML = $doc->createElement('packets_size');
    $flow->appendChild($packet_stats);
    $packet_stats->appendChild($packets_numbXML);
    
    #bandwidth
    my $bw_stats = $doc->createElement('bandwidth_stats');
1247
1248
1249
    my $avg_bw = $doc->createElement('bandwidth_avg');
    my $max_bw = $doc->createElement('bandwidth_max');
    my $max_bw_100ms = $doc->createElement('bandwidth_max_100ms');
1250
1251
1252
1253
1254
1255
1256
    my $sdv_bw = $doc->createElement('bandwidth_sdv');
    $bw_stats->appendChild($avg_bw);
    $bw_stats->appendChild($max_bw);
    $bw_stats->appendChild($max_bw_100ms);
    $bw_stats->appendChild($sdv_bw);
    $flow->appendChild($bw_stats);

1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
    #network_stats
    $setuptimeXML->addText(&format_numbers($setuptime{$f}));
    $gap_avgXML->addText(&format_numbers(avg( $sumgap{$f}, $ngap{$f}) / 1000 ));
    $gap_sdvXML->addText(&format_numbers(sdv($ngap{$f}, $sumgap{$f},$ssgap{$f})/1000));
    $gap_minXML->addText(&format_numbers($mingap{$f}/1000));
    $gap_maxXML->addText(&format_numbers($mingap{$f}/1000));
    
    #Jitter / $opt_crue or $opt_rtp
    if ($jitter_stat){
	#create elements
	my $MOS_XML = $doc->createElement('MOS');
	my $rtp_timestamp = $doc->createElement('rtp_timestamp');
	my $rtp_dup = $doc->createElement('rtp_duplicated');
	my $rtp_late = $doc->createElement('rtp_pkt_late');
	my $rtp_lost = $doc->createElement('rtp_pkt_lost');
	my $rtp_gaps = $doc->createElement('rtp_pkt_gaps');
	my $rtp_avg = $doc->createElement('rtp_pkt_lost_avg');
	my $rtp_sdv = $doc->createElement('rtp_pkt_lost_sdv');
	my $rtp_jitter_numb = $doc->createElement('rtp_jitter_numb');
	my $rtp_jitter_avgXML = $doc->createElement('rtp_jitter_avg');
	my $rtp_jitter_sdvXML = $doc->createElement('rtp_jitter_sdv');
	my $rtp_jitter_minXML = $doc->createElement('rtp_jitter_min');
	my $rtp_jitter_maxXML = $doc->createElement('rtp_jitter_max');
	my $rtp_jitter_rfc = $doc->createElement('rtp_jitter_rfc');
	my $packets_dupXML = $doc->createElement('packet_duplicated');
1282
1283
    	my $packets_lateXML = $doc->createElement('packet_late');
    	my $packets_lostXML = $doc->createElement('packet_lost');
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
	my $packets_sizeXML = $doc->createElement('packet_size');
	
	#append childs
	$network_stats->appendChild($MOS_XML);
	$network_stats->appendChild($rtp_timestamp);
	$network_stats->appendChild($rtp_dup);
	$network_stats->appendChild($rtp_late);
	$network_stats->appendChild($rtp_lost);
	$network_stats->appendChild($rtp_gaps);
	$network_stats->appendChild($rtp_avg);
	$network_stats->appendChild($rtp_sdv);
	$network_stats->appendChild($rtp_jitter_numb);
	$network_stats->appendChild($rtp_jitter_avgXML);
	$network_stats->appendChild($rtp_jitter_sdvXML);
	$network_stats->appendChild($rtp_jitter_minXML);
	$network_stats->appendChild($rtp_jitter_maxXML);
	$network_stats->appendChild($rtp_jitter_rfc);
	
	$packet_stats->appendChild($packets_dupXML);
1303
1304
1305
1306
    	$packet_stats->appendChild($packets_lateXML);
    	$packet_stats->appendChild($packets_lostXML);
    	$packet_stats->appendChild($packets_sizeXML);
    	
1307
    	$packets_dupXML->addText(&format_numbers($dup{$f}));
1308
1309
1310
1311
1312
	$packets_lateXML->addText(&format_numbers($late{$f}));
	$packets_lostXML->addText(&format_numbers($nloss{$f}));
	my $n=$npkt{$f}; $n=1 if $npkt{$f} < 1; # divide by zero protection
	my $njitter = $njitter{$f}; $njitter =1 if $njitter{$f} < 1;
	$packets_sizeXML->addText(&format_numbers($sumbyte{$f}/$n));
1313
	
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
	#adds values
	$MOS_XML->addText(&format_numbers(&mos_r($codec, $rtt,  avg( $sumgap{$f}, $ngap{$f}) / 1000,
						 avg($sumjitter{$f}, $njitter)/1000, 
						 100*$nloss{$f}/$n)));
	
	$rtp_timestamp->addText(&format_numbers($ntimeerr{$f}));
	$rtp_dup->addText(&format_numbers($dup{$f}));
	$rtp_late->addText(&format_numbers($late{$f}));
	$rtp_lost->addText(&format_numbers($lost{$f}));
	$rtp_gaps->addText(&format_numbers($nloss{$f}));
	$rtp_avg->addText(&format_numbers( avg( $sumloss{$f}, $nloss{$f}) ));
	$rtp_sdv->addText(&format_numbers( sdv($lost{$f}, $sumloss{$f},$ssumloss{$f})));
	$rtp_jitter_numb->addText(&format_numbers($njitter{$f}));			
	$rtp_jitter_avgXML->addText(&format_numbers($sumjitter{$f}/$njitter/1000));
	$rtp_jitter_sdvXML->addText(&format_numbers($sumjitter{$f}/$njitter/1000));
	$rtp_jitter_minXML->addText(&format_numbers($minjitter{$f}/1000));
	$rtp_jitter_maxXML->addText(&format_numbers($maxjitter{$f}/1000));
	$rtp_jitter_rfc->addText(&format_numbers($maxjitter{$f}/1000));
    }	
    #packet stats
    
    $packets_numbXML->addText(&format_numbers($npkt{$f}));
    #$packets_sizeXML->addText();

    #bandwidth	
    $avg_bw->addText(&format_numbers( avg( $sumbyte{$f}*8, $span )));
    $max_bw->addText(&format_numbers($maxbps{$f}));
    $max_bw_100ms->addText(&format_numbers($max100ms{$f}));
    $sdv_bw->addText(&format_numbers(sdv($n100ms{$f},
					 $akk100ms{$f}, 
					 $ss100bps{$f})));	
    if ($opt_mpeg) {			
1346
1347
1348
1349
1350
	#PCR_stats
	my $pcr_jitter_avg_XML = $doc->createElement('pcr_jitter_avg');
	my $pcr_jitter_sdv_XML = $doc->createElement('pcr_jitter_sdv');
	my $pcr_jitter_min_XML = $doc->createElement('pcr_jitter_min');
	my $pcr_jitter_max_XML = $doc->createElement('pcr_jitter_max');
1351
1352
1353
1354
	$pcr_jitter_avg_XML->addText(&format_numbers( (10**3) * avg($pcr_stats{$f}{$pcr_pid{$f}}{'pcr_jitter_sum'}, $pcr_stats{$f}{$pcr_pid{$f}}{'num'})));
	$pcr_jitter_sdv_XML->addText(&format_numbers( (10**3) * sdv($pcr_stats{$f}{$pcr_pid{$f}}{'num'}, $pcr_stats{$f}{$pcr_pid{$f}}{'pcr_jitter_sum'},$pcr_stats{$f}{$pcr_pid{$f}}{'pcr_jitter_square'})));
	$pcr_jitter_min_XML->addText(&format_numbers( (10**3) * $pcr_stats{$f}{$pcr_pid{$f}}{'min_jit'}));
	$pcr_jitter_max_XML->addText(&format_numbers( (10**3) * $pcr_stats{$f}{$pcr_pid{$f}}{'max_jit'}));
1355
	
1356
1357