qstream 121 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 21
use Net::IPv6Addr;
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');
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 133
my %jitter_stats = ();
	
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 157 158
		  &display_stats();
		  &end_xml() if $opt_xml;
		  &handle_threads()  if ($opt_tcp and $opt_rtmp);
159
		  
160
		  die "End after interrupt.\n";exit(0) };
Olav Kvittem's avatar
Olav Kvittem committed
161 162 163
$SIG{KILL} = sub { $uninterrupted=0; # return if $nkill++ < 1;
		   &display_stats();  die "End after kill.\n";exit(0) };
$SIG{ALRM} = sub { 
164
#   Writes stats to file if option XML is given. -PMM
Olav Kvittem's avatar
Olav Kvittem committed
165 166
#    die "No packets received" if $n_packets < 1;
#    $uninterrupted=0;
167
    &display_stats() if  !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
168 169
#	die "End after alarm\n"; 
#	return(0);
Olav Kvittem's avatar
Olav Kvittem committed
170 171
    $endstream=1;
    print "endstream=1" if $opt_debug;
Olav Kvittem's avatar
Olav Kvittem committed
172
};
Olav Kvittem's avatar
Olav Kvittem committed
173 174 175 176

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

178
&unit_test if $opt_test;
Olav Kvittem's avatar
Olav Kvittem committed
179

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

Olav Kvittem's avatar
Olav Kvittem committed
212
exit(0);
213 214 215 216
#
sub unit_test {
	my $test_data = shift;
	#streams =  "rtp_158.38.130.110_6711_1.pcap";
217
	@streams = "testing/testdata_mpeg.pcap" if $opt_mpeg;
218
	@streams = "testing/testdata_rtp.pcap" if $opt_rtp;
219
	@streams = "testing/testdata_tcp.pcap" if $opt_tcp;
220 221 222
	$opt_xml = "tmp_testdata.xml";
	$opt_q =  1;
	$opt_v = 1;
223
	#print $streams[0], "\n";
224 225 226 227 228 229 230
}
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;
231
	$test_data = "tcp_testdata.xml" if $opt_tcp;
232 233 234 235 236 237
	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/) {
238
			print "different value @ $k1, value: $orginal_test->{$k1}, differs from value: $new_test->{$k1}\n";
239 240 241
		}
		for my $k2 ( keys %{$orginal_test->{ $k1}} ) {
			if ( ($orginal_test->{ $k1}{$k2} != $new_test->{ $k1}{$k2}) and ($k2 !~ m/$ignore_pattern/)){
242
				print "different value @ $k1-> $k2, value: $orginal_test->{$k1}{$k2} differs from, value: $new_test->{$k1}{$k2}\n";
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 280 281 282 283 284 285 286 287
			}
		}		
	}
}
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
288

289 290 291 292 293 294 295 296 297
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
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
		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);
						}
315 316 317 318 319 320 321 322
					}
				}
			}
		}
	}
}
sub get_stream {
	my $uri = shift;
paalmm's avatar
paalmm committed
323
	my $data = get("$uri");
324
}
paalmm's avatar
paalmm committed
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
##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;
345
		&unit_test_2($filename) if $opt_test;
346 347 348
}

sub tcp_packet_stats {
349
	my $f = shift;
350 351 352 353 354 355 356 357 358 359 360 361 362 363
	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;
364 365
	my $seq_num = $tcp_obj->{seqnum};
	my $options = $tcp_obj->{options}; #12 bytes
366
	#print "$tcp_obj->{src_port} \n";
367
	if (!exists $tcp_packet_stats{$f}){
368
		&init_stats($f);
369 370 371 372 373 374 375 376 377 378 379
		$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
380
	$tcp_packet_stats{$f}{'num_bytes'} += $ip->{$len}; #counts the bytes
381
	#Check for duplicated,out-of-order, late and lost packets.
382
	$seq_stats{$f}{'seq_diff'} = $seq_num - $seq_stats{$f}{'p_seq_num'} - $seq_stats{$f}{'p_packet_len'};
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
   	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){
398
   		$seq_stats{$f}{'n_seq_num'} = ($seq_num+$ip->{$len} -($ip_hlen)-($tcp_obj->{hlen}*4))
399 400
   	} 
   	$seq_stats{$f}{'p_seq_num'} = $seq_num;
401
   	$seq_stats{$f}{'p_packet_len'} = $ip->{$len}-($ip_hlen)-($tcp_obj->{hlen}*4);
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 526 527 528 529 530
	$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
531 532


533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
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
553 554 555 556 557
#------------------------------------------------------------------------------

# unpack uri's to ip-address
sub prepare_ip {
    my $uri=shift;
558
    $uri =~ s/^(udp|rtp|http)://;
Olav Kvittem's avatar
Olav Kvittem committed
559 560
    $uri =~ s#//##;
    die "Unsupported streaming $1" if $uri =~/^(\w:)/;
Olav Kvittem's avatar
Olav Kvittem committed
561 562 563
    if ($multicast = ($uri =~ /@/)){
	($source, $uri)=split(/@/, $uri);
    }
Olav Kvittem's avatar
Olav Kvittem committed
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 617 618 619 620 621

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

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

760 761 762 763
	    } 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
764 765 766 767 768 769 770 771 772 773 774
	    }
	}
	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="( ";
775
    my ($address, $port, $multicast);
Olav Kvittem's avatar
Olav Kvittem committed
776
    my $mc = IO::Socket::Multicast->new(LocalPort=>$DEFAULT_PORT, ReuseAddr=>1) ||
Olav Kvittem's avatar
Olav Kvittem committed
777
	die "Could not connect to port $port : $!";
Olav Kvittem's avatar
Olav Kvittem committed
778
    foreach $id (@streams){ # listen to all groups
779 780 781 782 783 784 785
	if ($opt_ipv6){
		$address = Net::IPv6Addr::ipv6_parse($id);
		$port =  $opt_p;
	}
	else {
		($address, $port, $multicast) = prepare_ip($id);
	}
Olav Kvittem's avatar
Olav Kvittem committed
786 787 788 789 790 791
	$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)"; 
792
	}
Olav Kvittem's avatar
Olav Kvittem committed
793 794 795 796 797 798 799
    }
    $tjoined{$f}=[gettimeofday];
    $filter .= " )";
#    $filter .= " and ip multicast";
#    $filter ="ip";
    my ($net, $netmask, $filter_t, $err, $dev);
#    printf $filter;
800
    $dev = Net::Pcap::lookupdev(\$err) || die "Could not lookupdev $dev : $err : $! .. Needs to run as sudo ..";
Olav Kvittem's avatar
Olav Kvittem committed
801 802
    my $r = Net::Pcap::lookupnet( $dev, \$net, \$netmask, \$err);
    die "Failed lookupnet for $dev : $r : $err : $!" if $r != 0;
803 804 805
    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
806 807 808 809 810 811 812 813 814 815 816
    $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
817 818
    printf "%s\n", $ttl_log if  $opt_ttl;

Olav Kvittem's avatar
Olav Kvittem committed
819 820
}

821 822 823 824 825 826 827
sub set_network_info{
	my ($f,$src_ip,$dst_ip,$src_port,$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;	
}
828
sub eat_pcap { # process pcap packets	
Olav Kvittem's avatar
Olav Kvittem committed
829 830
    my ($f, $pcap, $pkt)=@_;
    my $ip=null;
831
    my $ip_payload;# udp or tcp packet.
Olav Kvittem's avatar
Olav Kvittem committed
832
    my $rtp = new Net::RTP::Packet(); # spare rtp obj
833
    my $proto,$len=null;
Olav Kvittem's avatar
Olav Kvittem committed
834
    if (Net::Pcap::datalink($pcap) == 1){ # ethernet
835 836 837 838 839 840 841 842 843 844 845 846
    	if ($opt_ipv6){
	  		$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
847 848 849 850 851 852
    } 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);
    }
853 854 855 856 857 858 859 860 861
	
    if ($proto == IP_PROTO_TCP){
    	$opt_tcp = 1; 
		$ip_payload = NetPacket::TCP->decode($ip->{data});
    }
    else {
    	$ip_payload = NetPacket::UDP->decode($ip->{data});
    }
    my $dlen = $ip->{$len} - $hlen;
Olav Kvittem's avatar
Olav Kvittem committed
862 863 864
    my $flow_key;
    if ($opt_sum){
	$flow_key=$opt_sum;
865 866 867 868 869 870
    }
    elsif ($file_flag){
    	$flow_key = $f;
    }
    else {
		$flow_key=sprintf "%s:%d->%s:%d", 
871
        $ip->{src_ip}, $ip_payload->{src_port}, $ip->{dest_ip}, $ip_payload->{dest_port};
Olav Kvittem's avatar
Olav Kvittem committed
872
    }
873
    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
874
    $flow_pkts{$flow_key}++;
875
    #next if $ip->{proto} != IP_PROTO_UDP;
Olav Kvittem's avatar
Olav Kvittem committed
876 877 878 879
    if ($flow_pkts{$flow_key} <= 1) { # new flow
	push(@flows, $flow_key);  # 
	$flow_no++; 
	$file{$flow_key}=$f;
880
	$source{$flow_key} =sprintf "%s:%d", $ip->{src_ip}, $ip_payload->{src_port};
Olav Kvittem's avatar
Olav Kvittem committed
881
    }
Olav Kvittem's avatar
Olav Kvittem committed
882 883 884 885 886 887 888
    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}++;
889
	($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
890 891 892 893 894 895
	$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
896 897 898 899 900 901
    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);
902
		push (@myflows, $flow_key);
Olav Kvittem's avatar
Olav Kvittem committed
903
    }
904 905 906 907
    $tc= [$hdr{tv_sec}, $hdr{tv_usec}];
    if (! defined($t0)) {
	$t0=$tc;
    }
Olav Kvittem's avatar
Olav Kvittem committed
908 909
    $tjoined{$f}=$tc if !$tjoined{$f};
    $tjoined{$flow_key} = $tjoined{$f} if !exists($tjoined{$flow_key});
910
    $us = tv_interval ( $t0, $tc) * 10**6 ; # us relative packet time 	  
Olav Kvittem's avatar
Olav Kvittem committed
911 912
# fixate on first flow to appear
#    if ( $first_port) {
913
#	next if $ip_payload->{dest_port} != $first_port;
Olav Kvittem's avatar
Olav Kvittem committed
914
#    } else {
915
#	$first_port =  $ip_payload->{dest_port};
Olav Kvittem's avatar
Olav Kvittem committed
916
#    }
917 918 919 920
	if ($opt_tcp){
		&tcp_packet_stats($flow_key,$ip,$ip_payload);
	}
    &pkt_stats($flow_key, $ip_payload->{data}, $us, $dlen);
Olav Kvittem's avatar
Olav Kvittem committed
921 922
}
sub init_stats {
Olav Kvittem's avatar
Olav Kvittem committed
923 924 925
    my $f=shift;
    $pinterval=0;
    $npkt{$f}=$sumbyte{$f}=$ssbyte{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
926
    $first_sec=0;
Olav Kvittem's avatar
Olav Kvittem committed
927 928 929 930
    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};
931
    $n100ms{$f}=0; $sum100ms{$f}=0; $ss100bps{$f}=0; $akk100ms{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
932 933 934 935 936 937 938 939 940 941
    $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}; 
942
    
Olav Kvittem's avatar
Olav Kvittem committed
943 944

#MPEG-TS initalizing and resetting (new period!)
945
    
Olav Kvittem's avatar
Olav Kvittem committed
946 947
    $discontinuities = 0;
    %last_ccs = ();
948
    %mpeg_stats = ();
Olav Kvittem's avatar
Olav Kvittem committed
949 950 951 952
    @cc = ();
# r_stats {filename/handle}
    @pcr_stats{$f} = ();
#P-stats {filename/handle}
953
    @pts_stats{$f,$TS} = ();
Olav Kvittem's avatar
Olav Kvittem committed
954 955 956 957 958 959 960
    @mpeg_stats{$f} = ();
#P stuff
    $late_video_packets = 0;
#MPEG-TS END
    %dist1 = ();
    %dist2 = ();
    $num_cc = 0;
961 962 963 964
}
sub format_numbers{
	my $value =  shift;
	if ($value =~ /^[a-zA-z][\D]*$/ || $value  !~ /^\d+\.\d+$/) {
965
		return sprintf("%1.0f",$value);
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
	}
	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;
986 987 988 989 990 991 992 993
	#($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
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 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
	&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);
1049
    	$avg_bw->addText(&format_numbers( avg( $bw_stats{$f}{'sumbyte'}*8, $span )));
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
		$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);
}

1068
sub xml_new {
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 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
	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;
1134
	my ($network_info) = (\%network_info);
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
	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');
1147
	my $flow_nameXML = $doc->createElement('flow_name');
1148 1149
	my $dateXML = $doc->createElement('date');
    my $timeXML = $doc->createElement('time');
1150 1151 1152
    my $spanXML = $doc->createElement('span');
    my $pcr_jitter_parrent = $doc->createElement('pcr_jitter');
    my $network_info_parrent = $doc->createElement('network_info');
1153 1154
    $qstream->appendChild($flow);
    $flow->appendChild($flow_id);
1155
    $flow->appendChild($flow_nameXML);
1156 1157
    $flow->appendChild($dateXML);
    $flow->appendChild($timeXML);
1158 1159 1160
    $flow->appendChild($spanXML);
    $flow->appendChild($network_info_parrent);
    $flow->appendChild($pcr_jitter_parrent);
1161 1162 1163
    my $media="file";
    $media = "host:port" if $opt_net;
	my $id=$f;
1164 1165 1166
	$flow_name = $id;
	$id = $opt_id if $opt_id;
	$flow_name = $opt_name if $opt_name;
1167
	next if $npkt{$f} < $opt_flow_min;
1168 1169 1170 1171
	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
1172

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	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. 
1185 1186
	$flow_id->addText($id);
	$flow_nameXML->addText($flow_name);
1187 1188 1189 1190
	#datetime 
	$dateXML->addText($date);
	$timeXML->addText($time);
	$spanXML->addText($span);
1191 1192 1193 1194 1195 1196 1197
	for my $k1 ( sort keys %network_info ) {
    	for my $k2 ( keys %{$network_info->{ $k1 }} ) {
    		$child = $doc->createElement($k2);
    		$child->addText($network_info->{ $k1 }{ $k2 });
    		$network_info_parrent->appendChild($child);
    	}
   	}
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 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 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
    #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);
    	
1280 1281
    	$packets_dupXML->addText(&format_numbers($dup{$f}));
		$packets_lateXML->addText(&format_numbers($late{$f}));
1282 1283
		$packets_lostXML->addText(&format_numbers($nloss{$f}));
		my $n=$npkt{$f}; $n=1 if $npkt{$f} <1; # divide by zero protection
1284
		$packets_sizeXML->addText(&format_numbers($sumbyte{$f}/$n));
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
		
	    #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
	
1306
	$packets_numbXML->addText(&format_numbers($npkt{$f}));
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
	#$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');
1322 1323 1324 1325
	$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'}));
1326
	
1327 1328 1329 1330
		$pcr_jitter_parrent->appendChild($pcr_jitter_avg_XML);
		$pcr_jitter_parrent->appendChild($pcr_jitter_sdv_XML);
		$pcr_jitter_parrent->appendChild($pcr_jitter_min_XML);
		$pcr_jitter_parrent->appendChild($pcr_jitter_max_XML);
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	}
	#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
1373
		    $lost_packets_XML->addText(&format_numbers($mpeg_stats{$f}{$pid}{'lost_packets'}));
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
		    $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
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
}

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
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
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
1424

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

Olav Kvittem's avatar
Olav Kvittem committed