qstream 117 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 20
use NetPacket::TCP;
#use Net::RTP;
Olav Kvittem's avatar
Olav Kvittem committed
21 22 23
use Net::RTP::Packet;
use Time::HiRes qw/ sleep time tv_interval gettimeofday/;
use Data::Dumper;
24 25 26
use threads;
use threads::shared;
use Pod::Usage;
Olav Kvittem's avatar
Olav Kvittem committed
27 28 29
#use bigint qw/ hex oct/;
use Math::BigInt;
use Math::BigFloat;
30
use Carp;
31 32
#RTMP implementation *experimental*
use rtmp::client qw(rtmp_connect rtmp_play rtmp_call get_chunk_ids);
Olav Kvittem's avatar
Olav Kvittem committed
33
use constant MTU => 1580;
34 35 36
#XML
use XML::DOM;
use XML::Twig;
Olav Kvittem's avatar
Olav Kvittem committed
37
#use mpegpackets;
Olav Kvittem's avatar
Olav Kvittem committed
38 39 40
$DEFAULT_PORT = 1955;
my $timeout=1 ; # frac seconds to wait for receiving packets

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

Olav Kvittem's avatar
Olav Kvittem committed
44
require "newgetopt.pl";
45

46
@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');
47 48 49

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

Olav Kvittem's avatar
Olav Kvittem committed
56 57
my @streams=@ARGV;

Olav Kvittem's avatar
Olav Kvittem committed
58 59 60
if ($opt_bins){
    @bins=split(/,/, $opt_bins);
}
61 62 63
if ($opt_report){
    @filter=split(/,/, $opt_report);
}
Olav Kvittem's avatar
Olav Kvittem committed
64 65 66 67 68 69 70 71 72
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;
73 74 75 76 77 78 79 80
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;
81
my $offline;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

#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;


my $start_time = time();
Olav Kvittem's avatar
Olav Kvittem committed
109 110 111
#MPEG-TS measurement globals (most are reset in init)
my $CHECK_NUM_TSP = 2;
#globals for mpeg ts 
112 113
#a tabel for transport streams pids. aleternativ to PAT impl.
my $TS;
Olav Kvittem's avatar
Olav Kvittem committed
114 115 116 117 118 119 120 121 122 123 124 125
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 = ();
126 127
my %jitter_stats = ();
	
Olav Kvittem's avatar
Olav Kvittem committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141
#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
142 143
$file_flag = 1 if $opt_filename;

144
#MPEG-TS END
Olav Kvittem's avatar
Olav Kvittem committed
145 146


147 148
$SIG{USR2} = sub {confess "Caught by SIGUSR2"; };

Olav Kvittem's avatar
Olav Kvittem committed
149
$SIG{INT} = sub { $uninterrupted=0; # return if $nint++  < 1;
150 151 152
		  &display_stats();
		  &end_xml() if $opt_xml;
		  &handle_threads()  if ($opt_tcp and $opt_rtmp);
153
		  
154
		  die "End after interrupt.\n";exit(0) };
Olav Kvittem's avatar
Olav Kvittem committed
155 156 157
$SIG{KILL} = sub { $uninterrupted=0; # return if $nkill++ < 1;
		   &display_stats();  die "End after kill.\n";exit(0) };
$SIG{ALRM} = sub { 
158
#   Writes stats to file if option XML is given. -PMM
Olav Kvittem's avatar
Olav Kvittem committed
159 160
#    die "No packets received" if $n_packets < 1;
#    $uninterrupted=0;
161
    &display_stats() if  !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
162 163
#	die "End after alarm\n"; 
#	return(0);
Olav Kvittem's avatar
Olav Kvittem committed
164 165
    $endstream=1;
    print "endstream=1" if $opt_debug;
Olav Kvittem's avatar
Olav Kvittem committed
166
};
Olav Kvittem's avatar
Olav Kvittem committed
167 168 169 170

if ($opt_dump){
    open DUMP, ">$opt_dump";
}
171
&unit_test if $opt_test;
Olav Kvittem's avatar
Olav Kvittem committed
172

173
if ($opt_pcap || ($opt_tcp && !$opt_rtmp)){ # listen in parallell
Olav Kvittem's avatar
Olav Kvittem committed
174 175 176
    alarm($opt_last) if $opt_last; 
    $uninterrupted=1;
    $endstream=0;
177
    eat_pcap_streams(@streams);
178
    &display_stats() if !$opt_xml;
Olav Kvittem's avatar
Olav Kvittem committed
179
} else { # listen serially
180
	my $stream_count;
181
    my $ln = @streams;
Olav Kvittem's avatar
Olav Kvittem committed
182
    foreach $id (@streams) {
183 184 185 186
	alarm($opt_last) if $opt_last;
	++$stream_count;
	$last_stream = 1 if $stream_count == ($ln);
	#print "count $stream_count  lengde  $ln \n";
Olav Kvittem's avatar
Olav Kvittem committed
187 188 189
	$uninterrupted=1;
	$endstream=0;
	init_file($id);
190 191
	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;
192
	#&eat_tcp($id,$opt_port) if $opt_tcp && !$opt_rtmp;
Olav Kvittem's avatar
Olav Kvittem committed
193 194 195 196 197
	if (! ( $opt_list || $opt_sum)){
	    &display_stats() if !$endstream;
	}
	last if ! $uninterrupted;
    }
198
   
Olav Kvittem's avatar
Olav Kvittem committed
199 200 201
}
&list_flows if $opt_list;
&display_stats() if $opt_sum;
Olav Kvittem's avatar
Olav Kvittem committed
202
&display_bins if $opt_bins;
203
&end_xml() if $opt_xml;
Olav Kvittem's avatar
Olav Kvittem committed
204

Olav Kvittem's avatar
Olav Kvittem committed
205
exit(0);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
#
sub unit_test {
	my $test_data = shift;
	#streams =  "rtp_158.38.130.110_6711_1.pcap";
	@streams =  "testing/testdata_mpeg.pcap" if $opt_mpeg;
	@streams = "testing/testdata_rtp.pcap" if $opt_rtp;
	#@streams = "testing/testdata_tcp.pcap";
	$opt_xml = "tmp_testdata.xml";
	$opt_q =  1;
	$opt_v = 1;
}
sub unit_test_2{
	#my ($orgnial_test) = &feed_data("file",rtp_file.xml);
	my $test_data;
	$test_data = "mpeg_testdata.xml" if $opt_mpeg;
	$test_data = "rtp_testdata.xml" if $opt_rtp;
	#$test_data = "udp_testdata" if $opt_net;
	$test_data = "tcp_testdata_" 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. 
	for my $k1(keys %$orginal_test){
		if ($orginal_test->{ $k1} != $new_test->{ $k1} and $orginal_test->{ $k1} !~ m/HASH/) {
			print "fail @ $k1, value: $orginal_test->{$k1} unlike value: $new_test->{$k1}\n";
		}
		for my $k2 ( keys %{$orginal_test->{ $k1}} ) {
			if ( ($orginal_test->{ $k1}{$k2} != $new_test->{ $k1}{$k2}) and ($k2 !~ m/$ignore_pattern/)){
				print "fail @ $k1-> $k2, value: $orginal_test->{ $k1}{$k2} unlike value: $new_test->{ $k1}{$k2} \n";
			}
		}		
	}
}
sub xml_parser {
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 ($@){
					};
				 }
				}
			}
			else {
        		$test_hash{'flow'}{$child->getNodeName()} =  $child->getFirstChild()->getData();
     		}	
     	 }
      } 
	}
	return (\%test_hash)
}
paalmm's avatar
paalmm committed
280

281 282 283 284 285 286 287 288 289
sub http_stream_client {
	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){
paalmm's avatar
paalmm committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		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);
						}
307 308 309 310 311 312 313 314
					}
				}
			}
		}
	}
}
sub get_stream {
	my $uri = shift;
paalmm's avatar
paalmm committed
315
	my $data = get("$uri");
316
}
paalmm's avatar
paalmm committed
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
##TCP impl. -PMM
sub handle_threads {
	$tcp_stop = 1;
	$thr->join();
}
sub end_xml{
		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";
    	open (MYFILE, '>'.$filename);
		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;
337
		&unit_test_2($filename) if $opt_test;
338 339 340
}

sub tcp_packet_stats {
341 342 343
	my $f = shift;
	my ($pkt) = shift;
	my $us = shift;
344 345
	my $ip_obj = NetPacket::IP->decode(eth_strip($pkt));
	my $tcp_obj = NetPacket::TCP->decode(ip_strip(eth_strip($pkt)));
346
	#my $f = $ip_obj->{src_ip}.":".$tcp_obj->{src_port} if !$offline;
347 348 349
	my $seq_num = $tcp_obj->{seqnum};
	my $options = $tcp_obj->{options}; #12 bytes
	if (!exists $tcp_packet_stats{$f}){
350
		&init_stats($f);
351 352 353 354 355 356 357 358
		$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;
		
	}
359
	print $tcp_packet_stats{$f}{'num_packets'}, "\n";
360
	++$tcp_packet_stats{$f}{'num_packets'};
361 362 363 364 365 366 367 368 369
	#$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;
370 371
	&tcp_jitter_stats($f,$options);
	#Bandwidth messurements
372
	&pkt_stats($f, $tcp_obj->{data}, $us, $ip_obj->{'len'});
373 374 375
	$tcp_packet_stats{$f}{'num_bytes'} += $ip_obj->{'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'};
376
	print $seq_stats{$f}{'seq_diff'}, "\n", if $opt_debug;
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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
   	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'};
   			}
   		}
   		else {
   			++$tcp_packet_stats{$f}{'pkt_lost'};
    		}
   	}
   	if($seq_stats{$f}{'p_seq_num'} < $seq_num){
   		$seq_stats{$f}{'n_seq_num'} = ($seq_num+$ip_obj->{len} -($ip_obj->{hlen}*4)-($tcp_obj->{hlen}*4))
   	} 
   	$seq_stats{$f}{'p_seq_num'} = $seq_num;
   	$seq_stats{$f}{'p_packet_len'} = $ip_obj->{len}-($ip_obj->{hlen}*4)-($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'};
}
sub tcp_jitter_stats {
		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;
		}
}
sub check_period {
	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);
	}
}
sub eat_tcp {
	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;
		    }
		    printf "setup $f : %.3f\n",  $setuptime{$f} if $opt_debug;
		}
		}
	}
	$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);
			}
	}
}
##RTMP impl. -PMM
sub rtmp_eat_stream {
	($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;
		    }
		    printf "setup $f : %.3f\n",  $setuptime{$f} if $opt_debug;
		}
	 }
		$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;
		
	}
}
Olav Kvittem's avatar
Olav Kvittem committed
526 527


528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
sub rtmp_prepare_url{
	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);
    }    
	for (my $i = 1; $i < $length;$i++){
		$app .= $string[$i];
		if ($i > 1 ){$app .="/";}
	}
	my $play = $string[$length];
	return ($address,$app,$play)
}
Olav Kvittem's avatar
Olav Kvittem committed
548 549 550 551 552
#------------------------------------------------------------------------------

# unpack uri's to ip-address
sub prepare_ip {
    my $uri=shift;
553
    $uri =~ s/^(udp|rtp|http)://;
Olav Kvittem's avatar
Olav Kvittem committed
554 555
    $uri =~ s#//##;
    die "Unsupported streaming $1" if $uri =~/^(\w:)/;
Olav Kvittem's avatar
Olav Kvittem committed
556 557 558
    if ($multicast = ($uri =~ /@/)){
	($source, $uri)=split(/@/, $uri);
    }
Olav Kvittem's avatar
Olav Kvittem committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 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

    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());
617
		
Olav Kvittem's avatar
Olav Kvittem committed
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 647 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
		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
683
		$endstream = tv_interval($t0, $tc) >= $opt_last if $opt_last && !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
	    } 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 : $!";
	}
702 703
	if ($opt_crude){
	    my $flow_key;
Olav Kvittem's avatar
Olav Kvittem committed
704
	    open CRUDE, "<$pcap_file" || die "Could not open $f : $err";
705
	    while(<CRUDE>){  # crude.sourceforge.net
Olav Kvittem's avatar
Olav Kvittem committed
706 707
		my $tx0;
		
708
		if (($fid, $seq, $src, $dst, $tx, $rx, $size)=
709
			 /ID=(\d+)\s+SEQ=(\d+)\s+SRC=([\d.:]+)\s+DST=([\d.:]+)\s+Tx=([\d.,]+)\s+Rx=([\d.,]+)\s+SIZE=(\d+)/){
710 711 712

		    $flow_key="$src->$dst";
		    $flow_pkts{$flow_key}++;
Olav Kvittem's avatar
Olav Kvittem committed
713
		    $tc=[int($rx), ($tx-int($rx))*10^6] ; # tval
714 715 716 717 718
		    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
719 720 721 722 723
			$rx0{$flow_key}=$rx;
			$tx0{$flow_key}=$tx;
			$tjoined{$flow_key}=$tc;
		    } 
		    
724 725 726 727 728 729 730 731 732
		    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
733 734 735 736 737 738 739
		    $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);
740
		}
Olav Kvittem's avatar
Olav Kvittem committed
741
	    }
742 743 744 745 746 747
	    
	} 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) ) ) {
paalmm's avatar
paalmm committed
748
			#$file_flag =1;
749
			$offline =1;
750 751 752
		    eat_pcap($f, $pcap, $pkt);
		    $n_packets++;
		}
Olav Kvittem's avatar
Olav Kvittem committed
753 754
		printf "%s\n", $ttl_log if  $opt_ttl;

755 756 757 758
	    } 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
759 760 761 762 763 764 765 766 767 768 769 770
	    }
	}
	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="( ";
    my $mc = IO::Socket::Multicast->new(LocalPort=>$DEFAULT_PORT, ReuseAddr=>1) ||
Olav Kvittem's avatar
Olav Kvittem committed
771 772
	die "Could not connect to port $port : $!";

Olav Kvittem's avatar
Olav Kvittem committed
773
    foreach $id (@streams){ # listen to all groups
Olav Kvittem's avatar
Olav Kvittem committed
774 775 776 777 778 779 780 781
	my ($address, $port, $multicast) = prepare_ip($id);
	$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)"; 
	}	
Olav Kvittem's avatar
Olav Kvittem committed
782 783 784 785 786 787 788
    }
    $tjoined{$f}=[gettimeofday];
    $filter .= " )";
#    $filter .= " and ip multicast";
#    $filter ="ip";
    my ($net, $netmask, $filter_t, $err, $dev);
#    printf $filter;
789
    $dev = Net::Pcap::lookupdev(\$err) || die "Could not lookupdev $dev : $err : $! .. Needs to run as sudo ..";
Olav Kvittem's avatar
Olav Kvittem committed
790 791
    my $r = Net::Pcap::lookupnet( $dev, \$net, \$netmask, \$err);
    die "Failed lookupnet for $dev : $r : $err : $!" if $r != 0;
792 793 794
    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
795 796 797 798 799 800 801 802 803 804 805
    $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
806 807
    printf "%s\n", $ttl_log if  $opt_ttl;

Olav Kvittem's avatar
Olav Kvittem committed
808 809
}

810
sub eat_pcap { # process pcap packets	
Olav Kvittem's avatar
Olav Kvittem committed
811 812 813 814 815
    my ($f, $pcap, $pkt)=@_;
    my $ip=null;
    my $rtp = new Net::RTP::Packet(); # spare rtp obj
    if (Net::Pcap::datalink($pcap) == 1){ # ethernet
	$ip = NetPacket::IP->decode(eth_strip($pkt));
816
	
Olav Kvittem's avatar
Olav Kvittem committed
817 818 819 820 821 822
    } 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);
    }
823 824 825
    $tc= [$hdr{tv_sec}, $hdr{tv_usec}];
    $us = tv_interval ( $t0, $tc) * 10**6 ; # us relative packet
    &tcp_packet_stats($f,$pkt,$us) if $ip->{proto} == IP_PROTO_TCP; 
826
    $opt_tcp = 1 if $ip->{proto} == IP_PROTO_TCP;
Olav Kvittem's avatar
Olav Kvittem committed
827 828
    next if $ip->{proto} != IP_PROTO_UDP;
    my $udp = NetPacket::UDP->decode($ip->{data});
829
    #my $tcp =  NetPacket::TCP->decode($ip->{data});
Olav Kvittem's avatar
Olav Kvittem committed
830 831 832 833
    my $dlen = $ip->{len} - $ip->{hlen}*4;
    my $flow_key;
    if ($opt_sum){
	$flow_key=$opt_sum;
834 835 836 837 838 839
    }
    elsif ($file_flag){
    	$flow_key = $f;
    }
    else {
		$flow_key=sprintf "%s:%d->%s:%d", 
Olav Kvittem's avatar
Olav Kvittem committed
840 841 842 843 844 845 846 847 848
        $ip->{src_ip}, $udp->{src_port}, $ip->{dest_ip}, $udp->{dest_port};
    }
    $flow_pkts{$flow_key}++;
    if ($flow_pkts{$flow_key} <= 1) { # new flow
	push(@flows, $flow_key);  # 
	$flow_no++; 
	$file{$flow_key}=$f;
	$source{$flow_key} =sprintf "%s:%d", $ip->{src_ip}, $udp->{src_port};
    }
Olav Kvittem's avatar
Olav Kvittem committed
849 850 851 852
    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;		
853
	    print "TTL \n";
Olav Kvittem's avatar
Olav Kvittem committed
854 855 856 857 858 859 860 861 862 863
	}
	$pttl{$flow_key}= $ip->{ttl};
	$nttl{$flow_key}++;
	($sequence_number{$flow_key}, $tx_time_seconds{$flow_key}, $tx_time_useconds{$flow_key}, $flow_id{$flow_key})= unpack('NNNN', $udp->{data});
	$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
864 865 866 867 868 869
    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);
870
		push (@myflows, $flow_key);
Olav Kvittem's avatar
Olav Kvittem committed
871 872 873 874 875 876 877 878 879
    }
    $tjoined{$f}=$tc if !$tjoined{$f};
    $tjoined{$flow_key} = $tjoined{$f} if !exists($tjoined{$flow_key});
# fixate on first flow to appear
#    if ( $first_port) {
#	next if $udp->{dest_port} != $first_port;
#    } else {
#	$first_port =  $udp->{dest_port};
#    }
Olav Kvittem's avatar
Olav Kvittem committed
880
    &pkt_stats($flow_key, $udp->{data}, $us, $dlen);
Olav Kvittem's avatar
Olav Kvittem committed
881 882 883
    
}
sub init_stats {
Olav Kvittem's avatar
Olav Kvittem committed
884 885 886
    my $f=shift;
    $pinterval=0;
    $npkt{$f}=$sumbyte{$f}=$ssbyte{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
887
    $first_sec=0;
Olav Kvittem's avatar
Olav Kvittem committed
888 889 890 891
    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};
892
    $n100ms{$f}=0; $sum100ms{$f}=0; $ss100bps{$f}=0; $akk100ms{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
893 894 895 896 897 898 899 900 901 902
    $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}; 
903
    
Olav Kvittem's avatar
Olav Kvittem committed
904 905

#MPEG-TS initalizing and resetting (new period!)
906
    
Olav Kvittem's avatar
Olav Kvittem committed
907 908
    $discontinuities = 0;
    %last_ccs = ();
909
    %mpeg_stats = ();
Olav Kvittem's avatar
Olav Kvittem committed
910 911 912 913
    @cc = ();
# r_stats {filename/handle}
    @pcr_stats{$f} = ();
#P-stats {filename/handle}
914
    @pts_stats{$f,$TS} = ();
Olav Kvittem's avatar
Olav Kvittem committed
915 916 917 918 919 920 921
    @mpeg_stats{$f} = ();
#P stuff
    $late_video_packets = 0;
#MPEG-TS END
    %dist1 = ();
    %dist2 = ();
    $num_cc = 0;
922 923 924 925
}
sub format_numbers{
	my $value =  shift;
	if ($value =~ /^[a-zA-z][\D]*$/ || $value  !~ /^\d+\.\d+$/) {
926
		return sprintf("%1.0f",$value);
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	}
	else {
		return sprintf("%0.5f",$value);
	}
}
sub xml_dump_rtmp_tcp {
	my $f = shift;
	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";
    if ($period_num <= 1 ){
		$doc = XML::DOM::Document->new;
    	my $decl = $doc->createXMLDecl('1.0');
   		$doc-> setXMLDecl($decl);
   		$qstream = $doc->createElement('qstream');
   		$qstream->setAttribute('version',$xml_version);
   		$doc->appendChild($qstream);
    }
	($address,$app,$play) = rtmp_prepare_url($f) if $rtmp;
947 948 949 950 951 952 953 954
	#($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
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	&do_math($id,$span);
    my $flow = $doc->createElement('flow');
	my $flow_id = $doc->createElement('flow_id');
	my $rtmp_stats_xml  = $doc->createElement('rtmp_stats');
	my $dateXML = $doc->createElement('date');
    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);    

	#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));
		       	}   
	    	}     
	    }
	}
    #bandwidth	
    if ($opt_tcp){
    	$f = $id;
    	my $bw_stats = $doc->createElement('bandwidth_stats');
   		my $avg_bw = $doc->createElement('avg_bandwidth');
    	my $max_bw = $doc->createElement('max_bandwidth_1s');
   		my $max_bw_100ms = $doc->createElement('max_bandwidth_100ms');
    	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);
1011
    	$avg_bw->addText(&format_numbers( avg( $bw_stats{$f}{'sumbyte'}*8, $span )));
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 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 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
		$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'})));
    	my $child = $doc->createElement('tcp_packet_stats');
    	$flow->appendChild($child);
    		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);
    		}
     } 
    #my $final_xml = $flow->toString();
    &init_stats($id);
}

sub dump_XML_new {
	my ($pcr_stats,$pts_stats,$mpeg_stats) = (\%pcr_stats, \%pts_stats,\%mpeg_stats) if $opt_mpeg;
	my $doc = XML::DOM::Document->new;
    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 " ";
	my $mpeg_stats_parrent = $doc->createElement('mpeg_stats');
    my $pcr_stats_parrent = $doc->createElement('pcr_stats');
    my $transport_streams = $doc->createElement('transport_streams');
    my $pts_stats_parrent = $doc->createElement('pts_stats');

	#append static childs
    $doc->appendChild($flow);
    $flow->appendChild($pcr_stats_parrent);
    $flow->appendChild($transport_streams);
   	$transport_streams->appendChild($pts_stats_parrent);
   	$transport_streams->appendChild($mpeg_stats_parrent);
   	
   	#loop through hashes
   	for my $k1 ( sort keys %mpeg_stats ) {
    	for my $k2 ( keys %{$mpeg_stats->{ $k1 }} ) {
    		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);		
    		}
    	}
   	}
    
    for my $k1 ( sort keys %$pcr_stats ) {
    	for my $k2 ( keys %{$pcr_stats->{ $k1 }} ) {
    		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);		
    			}
    		}
      	}
  	}
  	
    for my $k1 ( sort keys %$pts_stats ) {
    	for my $k2 ( keys %{$pts_stats->{ $k1 }} ) {
    		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);		
    				}	
    			}
    		}
      	}
  	}
}


sub xml_dump {
	my $f = shift;
	return 0 if $xml_printed;
	my ($pcr_stats,$pts_stats) = (\%pcr_stats, \%pts_stats) if $opt_mpeg;
	my $xml_version="1.0";
    if ($period_num <= 1 ){
		$doc = XML::DOM::Document->new;
    	my $decl = $doc->createXMLDecl('1.0');
   		$doc-> setXMLDecl($decl);
   		$qstream = $doc->createElement('qstream');
   		$qstream->setAttribute('version',$xml_version);
   		$doc->appendChild($qstream);
    }
	my $flow = $doc->createElement('flow');
	my $flow_id = $doc->createElement('flow_id');
	my $dateXML = $doc->createElement('date');
    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($spanXML);    
    my $media="file";
    $media = "host:port" if $opt_net;
	my $id=$f;
1119
	next if $npkt{$f} < $opt_flow_min;
1120 1121 1122 1123
	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;
Olav Kvittem's avatar
Olav Kvittem committed
1124

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
	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 {
	    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($f,$span);
	&burst($f); # in case a second is interrupted
	#source and destination ip. 
	$flow_id->addText($f);
	#datetime 
	$dateXML->addText($date);
	$timeXML->addText($time);
	$spanXML->addText($span);
    #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');
    my $min_gapXML = $doc->createElement('min_gap');
    my $max_gapXML = $doc->createElement('max_gap');
    $network_stats->appendChild($setuptimeXML);
    $network_stats->appendChild($gap_avgXML);
    $network_stats->appendChild($gap_sdvXML);
    $network_stats->appendChild($min_gapXML);
    $network_stats->appendChild($max_gapXML); 
    $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');
    my $avg_bw = $doc->createElement('avg_bandwidth');
    my $max_bw = $doc->createElement('max_bandwidth');
    my $max_bw_100ms = $doc->createElement('max_bandwidth_100ms');
    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);

	#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));
	$min_gapXML->addText(&format_numbers($mingap{$f}/1000));
	$max_gapXML->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');
    	my $packets_lateXML = $doc->createElement('packet_late');
    	my $packets_lostXML = $doc->createElement('packet_lost');
	    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);
    	$packet_stats->appendChild($packets_lateXML);
    	$packet_stats->appendChild($packets_lostXML);
    	$packet_stats->appendChild($packets_sizeXML);
    	
1224 1225
    	$packets_dupXML->addText(&format_numbers($dup{$f}));
		$packets_lateXML->addText(&format_numbers($late{$f}));
1226 1227
		$packets_lostXML->addText(&format_numbers($nloss{$f}));
		my $n=$npkt{$f}; $n=1 if $npkt{$f} <1; # divide by zero protection
1228
		$packets_sizeXML->addText(&format_numbers($sumbyte{$f}/$n));
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
		
	    #adds values
	    $MOS_XML->addText(&format_numbers(&mos_r($codec, $rtt,  avg( $sumgap{$f}, $ngap{$f}) / 1000,
					avg($sumjitter{$f}, $njitter{$f})/1000, 
					100*$nloss{$f}/$npkt{$f})));	
	    $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{$f}/1000));
	    $rtp_jitter_sdvXML->addText(&format_numbers($sumjitter{$f}/$njitter{$f}/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
	
1250
	$packets_numbXML->addText(&format_numbers($npkt{$f}));
1251 1252 1253 1254 1255 1256 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 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	#$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) {			
	#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');
	$pcr_jitter_avg_XML->addText(&format_numbers( (10**3) * avg($pcr_stats{$f}{'pcr_jitter_sum'}, $pcr_stats{$f}{'num'})));
	$pcr_jitter_sdv_XML->addText(&format_numbers( (10**3) * sdv($pcr_stats{$f}{'num'}, $pcr_stats{$f}{'pcr_jitter_sum'},$pcr_stats{$f}{'pcr_jitter_square'})));
	$pcr_jitter_min_XML->addText(&format_numbers( (10**3) * $pcr_stats{$f}{'min_jit'}));
	$pcr_jitter_max_XML->addText(&format_numbers( (10**3) * $pcr_stats{$f}{'max_jit'}));
	
		$flow->appendChild($pcr_jitter_avg_XML);
		$flow->appendChild($pcr_jitter_sdv_XML);
		$flow->appendChild($pcr_jitter_min_XML);
		$flow->appendChild($pcr_jitter_max_XML);
	}
	#Transport stream, goes throu @transport_streams for vidoe and audiostreams.
	if ($opt_mpeg){
	   for my $pid ( sort keys %{$pts_stats->{ $f }}  ) {
	 	if ( grep { $_ eq $pts_stats->{ $f }{ $pid }{ 'stream_id_type'} || $_ eq 'all'} @filter) {
		    #create elements
		    my $transport_stream = $doc->createElement('transport_stream');
		    my $pid_XML = $doc->createElement('pid');
		    my $stream_id_XML = $doc->createElement('stream_id');
		    my $stream_id_type_XML = $doc->createElement('stream_id_type');
		    my $num_packets_XML = $doc->createElement('mpeg_packets');
		    my $lost_packets_XML = $doc->createElement('mpeg_lost_packets');
		    my $media_loss_rate_XML = $doc->createElement('media_loss_rate');
		    my $delay_factor_XML = $doc->createElement('delay_factor');
		    my $media_bandwidth_XML = $doc->createElement('media_bandwidth');
		    #my $lost_packets_XML = $doc->createElement('mpeg_lost_packets');
		    my $dwell_avg_XML = $doc->createElement('dwell_avg');
		    my $dwell_sdv_XML = $doc->createElement('dwell_sdv');
		    my $dwell_min_XML = $doc->createElement('dwell_min');
		    my $dwell_max_XML = $doc->createElement('dwell_max');

		    #append childs.
		    $flow->appendChild($transport_stream);
		    $transport_stream->appendChild($pid_XML);
		    $transport_stream->appendChild($stream_id_XML);
		    $transport_stream->appendChild($stream_id_type_XML);
		    $transport_stream->appendChild($num_packets_XML);
		    $transport_stream->appendChild($lost_packets_XML);
		    $transport_stream->appendChild($media_loss_rate_XML);
		    $transport_stream->appendChild($media_bandwidth_XML);
		    $transport_stream->appendChild($dwell_avg_XML);
		    $transport_stream->appendChild($dwell_sdv_XML);
		    $transport_stream->appendChild($dwell_min_XML);
		    $transport_stream->appendChild($dwell_max_XML);
		    $transport_stream->appendChild($delay_factor_XML);
		    #set id 
		    $pid_XML->addText($pid);	    
		    #PES and PTS_stats
		    $stream_id_XML->addText($pts_stats{$f}{$pid}{'stream_id'});
		    $stream_id_type_XML->addText($pts_stats{$f}{$pid}{'stream_id_type'});
		    #mpeg
		    $num_packets_XML->addText($mpeg_stats{$f}{$pid}{'num'});
paalmm's avatar
paalmm committed
1317
		    $lost_packets_XML->addText(&format_numbers($mpeg_stats{$f}{$pid}{'lost_packets'}));
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
		    $media_loss_rate_XML->addText($mpeg_stats{$f}{$pid}{'media_loss_rate'});
		    $delay_factor_XML->addText(&format_numbers($mpeg_stats{$f}{$pid}{'delay_factor'}));
		    $media_bandwidth_XML->addText($mpeg_stats{$f}{$pid}{'media_bandwidth'});
		    #dwell time
		    $dwell_avg_XML->addText(&format_numbers( (10**3) * avg($pts_stats{$f}{$pid}{'dwell_sum'}, $pts_stats{$f}{$pid}{'num'})));
		    $dwell_sdv_XML->addText(&format_numbers( (10**3) * sdv($pts_stats{$f}{$pid}{'num'}, 
						  $pts_stats{$f}{$pid}{'dwell_sum'}, 
						  $pts_stats{$f}{$pid}{'dwell_squared'}))),
		    $dwell_min_XML->addText(&format_numbers( (10**3) * $pts_stats{$f}{$pid}{'min_dwell'}));
		    $dwell_max_XML->addText(&format_numbers( (10**3) * $pts_stats{$f}{$pid}{'max_dwell'}));
	 	}
	  } 		
	 }
Olav Kvittem's avatar
Olav Kvittem committed
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
}

sub init_file {
    undef $t0;
    my $f=shift;
    undef $tjoined; delete $tjoined{$f}; delete $setuptime{$f}; delete($joined{$file{$f}});
    delete $file{$f};
    @myflows=() if !$opt_sum;
    $flow_pkts{$f}=0;  # new flow on new file

    undef $pus;
    undef $psec;
    delete($pmtime{$f});
    delete($pseq_num{$f});
    delete($pus{$f});
}

Olav Kvittem's avatar
Olav Kvittem committed
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
sub seq_stat {
    my ($f, $seq_num, $mtime)=@_;

    my $seq_diff= $seq_num - $pseq_num{$f};
    unless ($opt_crude && $seq_diff == 1){ #
	$gap = $us - $pus{$f};		# packet time gap
	$ngap{$f}++;
	$sumgap{$f} +=  $gap;
	$ssgap{$f} += $gap**2;
	$maxgap{$f}=  $gap if $gap > $maxgap{$f};
	$mingap{$f}=  $gap if $gap < $mingap{$f};

	foreach $i (0 .. $#bins){
	    if( $gap < ($bins[$i]*1000)){
		$nbin[$i]++;
		last;
	    }
	}
	printf "Tx=%.6f gap=%d gap=%.1fms\n", $ptx{$f}, $seq_diff-1, $gap/1000, if $opt_log;
    }
Olav Kvittem's avatar
Olav Kvittem committed
1368

Olav Kvittem's avatar
Olav Kvittem committed
1369
    if ($seq_diff != 1) {
Olav Kvittem's avatar
Olav Kvittem committed
1370 1371
#	printf "seq %d,", $rtp->seq_num;

Olav Kvittem's avatar
Olav Kvittem committed
1372 1373 1374 1375
	if ($seq_diff == 0) { # Duplicated
	    $dup{$f} ++;
	} elsif ($seq_diff < 0) { # Out Of Order
	    $late{$f}++;
Olav Kvittem's avatar
Olav Kvittem committed
1376
#		$lost{$f}--;
Olav Kvittem's avatar
Olav Kvittem committed
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	} else {			# Lost                   
	    $lost{$f}+= $seq_diff - 1;
	    $nloss{$f}++;
	    $sumloss{$f} += $seq_diff;
	    $ssumloss{$f} += $seq_diff**2;
	    $maxloss{$f} = $seq_diff if  $seq_diff >  $maxloss{$f};
	}
    } elsif ( $mtime <= $pmtime{$f}){
	$ntimeerr{$f}++;
    } else { # the jitter can be measured
Olav Kvittem's avatar
Olav Kvittem committed
1387
#  compute jitter relative to previous packet
Olav Kvittem's avatar
Olav Kvittem committed
1388 1389
	$mgap= ($mtime - $pmtime{$f} ); # media time gap in samples 8khz=us
	$jitter=abs($gap - $mgap);
Olav Kvittem's avatar
Olav Kvittem committed
1390 1391 1392
#ok compute jitter relative to start
#ok	    $mgap= ($mtime - $first_mtime); # media time gap in us
#ok	    $jitter=abs($us - $mtime - $first_jitter);
Olav Kvittem's avatar
Olav Kvittem committed
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
	$njitter{$f}++;
	$sumjitter{$f} +=  $jitter;
	$ssjitter{$f} += $jitter**2;
	$maxjitter{$f}=  $jitter if $jitter > $maxjitter{$f};
	$minjitter{$f}=  $jitter if $jitter < $minjitter{$f};

	# rfc 3550 
	if (defined($est_jitter{$f})){
	    $est_jitter{$f} = $est_jitter{$f} + ( $jitter - $est_jitter{$f} )/16;
	} else { # inital value
	    $est_jitter{$f} = $jitter;
	}
    }

}

sub pkt_stats {
    my $f = shift; # file or stream name
    my $packet = shift;
    my $us = shift;
    my $dlen = shift;
    my $rtp = new Net::RTP::Packet();
    if (! exists($tstart{$f})){
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
    	#print "$tc \n"; 
	   	$tstart{$f} = $tc;
		$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;
		    }
		    printf "setup $f : %.3f\n",  $setuptime{$f} if $opt_debug;
		}
Olav Kvittem's avatar
Olav Kvittem committed
1427 1428 1429 1430 1431 1432
    }
    $tend{$f} = $tc;
    $last_us{$f}=$us;
    #	next if $dlen < 20;  # typically small background noice packets added
    $npkt{$f}++;
    $sumbyte{$f}+=$dlen; $sum1s{$f}+=$dlen; $sum100ms{$f}+=$dlen;
1433 1434 1435 1436
    if (!exists $bw_stats{$f}){
    	$bw_stats{$f} = &share( {} );
    }
    $bw_stats{$f}{'sumbyte'} +=$dlen; $sum1s{$f}+=$dlen; $sum100ms{$f}+=$dlen;
Olav Kvittem's avatar
Olav Kvittem committed
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
    $ssbyte{$f}+=$dlen**2;
    $maxbyte{$f}=  $dlen if $dlen > $maxbyte{$f};
    $minbyte{$f}=  $dlen if $dlen < $minbyte{$f};

    $sumps{$f}+=$dlen;		# sum per second

    if ($opt_dump){
	print DUMP $packet;
    }

    if ($opt_mpeg){ 
	mpeg_stats($f, $packet, $us);
    }

    if ($opt_rtp){
	if ( $rtp->decode($packet) ) { #assume RTP
#	    printf "RTP: %10s %5d %5d\n", $rtp->ssrc(), $rtp->timestamp(), $rtp->size();
#	    $dlen = $rtp->size();
	} else {
	    print "??? $f # $npkt{$f} $dlen B\n";
		if ($opt_debug){
		    my $n=0;
		    foreach $w( unpack("C*", $packet)) {
			printf "%0X",$w;
			if ($n > 0){
			    print " " if $n % 4 == 0;
			    print "\n" if $n % 32 == 0;
			}
			$n++;
		    }
		    printf "\n";
		}
	    next;
	}
	# Calculate next number in sequence
	$n_seq = $rtp->seq_num()+1;
	if ($n_seq > 65535) {
	    $n_seq=0;
	}
	$mtime{$f}=$rtp->timestamp() * &time_unit($rtp->payload_type);  # mediatime in us
	$seq_num{$f}=$rtp->seq_num;
    }

    if (defined($pus{$f})) {
	$gap = $us - $pus{$f};		# packet time gap
	$ngap{$f}++;
	$sumgap{$f} +=  $gap;
	$ssgap{$f} += $gap**2;
	$maxgap{$f}=  $gap if $gap > $maxgap{$f};
	$mingap{$f}=  $gap if $gap < $mingap{$f};

	if ($opt_rtp){
	    &seq_stat($f, $seq_num{$f}, $mtime{$f});
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
1490 1491 1492 1493 1494 1495
	}
	if ($adapt == 1 ) { # adaption field coming
	    my ($length, $aflags, $rest2)=unpack "C1C1C*", $rest;
	    my ($dif, $raif, $espif, $pcrf, $opcrf, $splicef, $tpdf, $afextf)=split(//, $aflags); 
	    if ($pcrf == 1){
		;
Olav Kvittem's avatar
Olav Kvittem committed
1496
	    }
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
1497
	    printf "%5d %6d %5d %5d %5d %5d %5d\n",$rtp->seq_num,$us,$rtp->timestamp(), $gap,  $mgap, $jitter, $first_jitter if $opt_debug;
Olav Kvittem's avatar
Olav Kvittem committed
1498 1499 1500 1501
	    
	} elsif ($opt_crude){
	    &seq_stat($f, $seq_num{$f}, $mtime{$f});

1502
	} 
1503
	&burst($f);
Olav Kvittem's avatar
Olav Kvittem committed
1504
    }
Olav Kvittem's avatar
Olav Kvittem committed
1505 1506 1507 1508
    if ($opt_period){
	$interval=int($us/10**6 / $opt_period);
	if ($interval > $pinterval){
	    &mpeg_debug() if $opt_debug;
1509 1510
	    &display_stats() if !$opt_rtmp;
	    &init_stats($f);
Olav Kvittem's avatar
Olav Kvittem committed
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	    $pinterval=$interval;
	    $pinterval{$f}=$interval;
	    $endstream = $opt_nperiod && $interval >= $opt_nperiod;
	}
    }
    $pus=$us;
    $psec=$sec;
    $pmtime{$f}=$mtime{$f} if $opt_rtp || $opt_crude;
    $pseq_num{$f}= $seq_num{$f} if $opt_rtp ||  $opt_crude;
    $pus{$f}=$us;
} # of pkt_stats
Olav Kvittem's avatar
Olav Kvittem committed
1522

1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
sub do_math{
	my $f = shift;
	my $span = shift;
	my $pts_stats = (\%pts_stats);
	if ($opt_mpeg){
		$pcr_stats{$f}{'pcr_avg_jitter'} = (10**3)*avg($pcr_stats{$f}{'pcr_jitter_sum'}, $pcr_stats{$f}{'num'});
		$pcr_stats{$f}{'pcr_sdv_jitter'} = (10**3)*sdv($pcr_stats{$f}{'num'}, $pcr_stats{$f}{'pcr_jitter_sum'},$pcr_stats{$f}{'pcr_jitter_square'});
		for my $k1 ( sort keys %$pts_stats