qstream 115 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

paalmm's avatar
paalmm committed
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');
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 81 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
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;

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

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


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

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

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

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

Olav Kvittem's avatar
Olav Kvittem committed
201
exit(0);
paalmm's avatar
paalmm committed
202

203 204 205 206 207 208 209 210 211
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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		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);
						}
229 230 231 232 233 234 235 236
					}
				}
			}
		}
	}
}
sub get_stream {
	my $uri = shift;
paalmm's avatar
paalmm committed
237
	my $data = get("$uri");
238
}
paalmm's avatar
paalmm committed
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 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
##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;
}

sub tcp_packet_stats {
	my ($pkt) = @_;
	my $ip_obj = NetPacket::IP->decode(eth_strip($pkt));
	my $tcp_obj = NetPacket::TCP->decode(ip_strip(eth_strip($pkt)));
	my $us = 0;
	my $f = $ip_obj->{src_ip}.":".$tcp_obj->{src_port};
	my $seq_num = $tcp_obj->{seqnum};
	my $options = $tcp_obj->{options}; #12 bytes
	if (!exists $tcp_packet_stats{$f}){
		$tcp_packet_stats{$f} = &share( {} );
		$seq_stats{$f} = &share( {} );
		$seq_stats_n{$f} = &share( {} );
		$tcp_packet_stats{$f}{'pkt_late'} = 0;
		$tcp_packet_stats{$f}{'pkt_retrans'} =0;
		$tcp_packet_stats{$f}{'pkt_dup'} = 0;
		
	}
	++$tcp_packet_stats{'num_packets'};
	++$tcp_packet_stats{$f}{'num_packets'};
	$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;
	&tcp_jitter_stats($f,$options);
	#Bandwidth messurements
	&pkt_stats($f, $tcp_obj->{data}, $elapsed * 10**6, $ip_obj->{'len'});
	$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'};
   	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
444 445


446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
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
466 467 468 469 470
#------------------------------------------------------------------------------

# unpack uri's to ip-address
sub prepare_ip {
    my $uri=shift;
471
    $uri =~ s/^(udp|rtp|http)://;
Olav Kvittem's avatar
Olav Kvittem committed
472 473
    $uri =~ s#//##;
    die "Unsupported streaming $1" if $uri =~/^(\w:)/;
Olav Kvittem's avatar
Olav Kvittem committed
474 475 476
    if ($multicast = ($uri =~ /@/)){
	($source, $uri)=split(/@/, $uri);
    }
Olav Kvittem's avatar
Olav Kvittem committed
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 531 532 533 534 535 536

    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());
537
		
Olav Kvittem's avatar
Olav Kvittem committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 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
		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
603
		$endstream = tv_interval($t0, $tc) >= $opt_last if $opt_last && !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	    } 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 : $!";
	}
622 623
	if ($opt_crude){
	    my $flow_key;
Olav Kvittem's avatar
Olav Kvittem committed
624
	    open CRUDE, "<$pcap_file" || die "Could not open $f : $err";
625
	    while(<CRUDE>){  # crude.sourceforge.net
Olav Kvittem's avatar
Olav Kvittem committed
626 627
		my $tx0;
		
628
		if (($fid, $seq, $src, $dst, $tx, $rx, $size)=
629
			 /ID=(\d+)\s+SEQ=(\d+)\s+SRC=([\d.:]+)\s+DST=([\d.:]+)\s+Tx=([\d.,]+)\s+Rx=([\d.,]+)\s+SIZE=(\d+)/){
630 631 632

		    $flow_key="$src->$dst";
		    $flow_pkts{$flow_key}++;
Olav Kvittem's avatar
Olav Kvittem committed
633
		    $tc=[int($rx), ($tx-int($rx))*10^6] ; # tval
634 635 636 637 638
		    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
639 640 641 642 643
			$rx0{$flow_key}=$rx;
			$tx0{$flow_key}=$tx;
			$tjoined{$flow_key}=$tc;
		    } 
		    
644 645 646 647 648 649 650 651 652
		    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
653 654 655 656 657 658 659
		    $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);
660
		}
Olav Kvittem's avatar
Olav Kvittem committed
661
	    }
662 663 664 665 666 667
	    
	} 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
668
			#$file_flag =1;
669 670 671
		    eat_pcap($f, $pcap, $pkt);
		    $n_packets++;
		}
Olav Kvittem's avatar
Olav Kvittem committed
672 673
		printf "%s\n", $ttl_log if  $opt_ttl;

674 675 676 677
	    } 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
678 679 680 681 682 683 684 685 686 687 688 689
	    }
	}
	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
690 691
	die "Could not connect to port $port : $!";

Olav Kvittem's avatar
Olav Kvittem committed
692
    foreach $id (@streams){ # listen to all groups
Olav Kvittem's avatar
Olav Kvittem committed
693 694 695 696 697 698 699 700
	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
701 702 703 704 705 706 707
    }
    $tjoined{$f}=[gettimeofday];
    $filter .= " )";
#    $filter .= " and ip multicast";
#    $filter ="ip";
    my ($net, $netmask, $filter_t, $err, $dev);
#    printf $filter;
708
    $dev = Net::Pcap::lookupdev(\$err) || die "Could not lookupdev $dev : $err : $! .. Needs to run as sudo ..";
Olav Kvittem's avatar
Olav Kvittem committed
709 710
    my $r = Net::Pcap::lookupnet( $dev, \$net, \$netmask, \$err);
    die "Failed lookupnet for $dev : $r : $err : $!" if $r != 0;
711 712 713
    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
714 715 716 717 718 719 720 721 722 723 724
    $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
725 726
    printf "%s\n", $ttl_log if  $opt_ttl;

Olav Kvittem's avatar
Olav Kvittem committed
727 728 729
}


730
sub eat_pcap { # process pcap packets	
Olav Kvittem's avatar
Olav Kvittem committed
731 732 733 734 735
    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));
736
	
Olav Kvittem's avatar
Olav Kvittem committed
737 738 739 740 741 742
    } 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);
    }
743 744
    &tcp_packet_stats($pkt) if $ip->{proto} == IP_PROTO_TCP; 
    $opt_tcp = 1 if $ip->{proto} == IP_PROTO_TCP;
Olav Kvittem's avatar
Olav Kvittem committed
745 746 747 748 749 750 751
    next if $ip->{proto} != IP_PROTO_UDP;
    my $udp = NetPacket::UDP->decode($ip->{data});
    my $dlen = $ip->{len} - $ip->{hlen}*4;
    my $flow_key;

    if ($opt_sum){
	$flow_key=$opt_sum;
752 753 754 755 756 757
    }
    elsif ($file_flag){
    	$flow_key = $f;
    }
    else {
		$flow_key=sprintf "%s:%d->%s:%d", 
Olav Kvittem's avatar
Olav Kvittem committed
758 759 760 761 762 763 764 765 766
        $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
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
    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}++;
	($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
782 783 784 785 786 787
    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);
788
		push (@myflows, $flow_key);
Olav Kvittem's avatar
Olav Kvittem committed
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    }
    $tc= [$hdr{tv_sec}, $hdr{tv_usec}];
    if (! defined($t0)) {
	$t0=$tc;
    }
    $tjoined{$f}=$tc if !$tjoined{$f};
    $tjoined{$flow_key} = $tjoined{$f} if !exists($tjoined{$flow_key});

    $us = tv_interval ( $t0, $tc) * 10**6 ; # us relative packet time 	  

# 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
806
    &pkt_stats($flow_key, $udp->{data}, $us, $dlen);
Olav Kvittem's avatar
Olav Kvittem committed
807 808 809
    
}
sub init_stats {
Olav Kvittem's avatar
Olav Kvittem committed
810 811 812
    my $f=shift;
    $pinterval=0;
    $npkt{$f}=$sumbyte{$f}=$ssbyte{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
813
    $first_sec=0;
Olav Kvittem's avatar
Olav Kvittem committed
814 815 816 817
    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};
818
    $n100ms{$f}=0; $sum100ms{$f}=0; $ss100bps{$f}=0; $akk100ms{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
819 820 821 822 823 824 825 826 827 828
    $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}; 
829
    
Olav Kvittem's avatar
Olav Kvittem committed
830 831

#MPEG-TS initalizing and resetting (new period!)
832
    
Olav Kvittem's avatar
Olav Kvittem committed
833 834
    $discontinuities = 0;
    %last_ccs = ();
835
    %mpeg_stats = ();
Olav Kvittem's avatar
Olav Kvittem committed
836 837 838 839
    @cc = ();
# r_stats {filename/handle}
    @pcr_stats{$f} = ();
#P-stats {filename/handle}
840
    @pts_stats{$f,$TS} = ();
Olav Kvittem's avatar
Olav Kvittem committed
841 842 843 844 845 846 847
    @mpeg_stats{$f} = ();
#P stuff
    $late_video_packets = 0;
#MPEG-TS END
    %dist1 = ();
    %dist2 = ();
    $num_cc = 0;
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 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 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
}
sub format_numbers{
	my $value =  shift;
	if ($value =~ /^[a-zA-z][\D]*$/ || $value  !~ /^\d+\.\d+$/) {
		return $value;
	}
	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;
	($address, $port) = prepare_ip($f) if $opt_tcp && !$opt_rtmp;
	$port  = 1935 unless $opt_port;
	my $id=$address.":".$port;
	my ($date,$time)  = &get_date_time();
	my $span=&tv_interval($tstart{$id}, $tend{$id}); # second
	&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);
    	$avg_bw->addText(&format_numbers( avg( $bw_stats{$f}{'sumbyte'}*8, $opt_period )));
		$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;
	next if $npkt{$f} <= $opt_flow_min;
	my $source=$f; 
	$source=$source{$f} if  ! $opt_v && $source{$f} ne "";
	$source=$opt_id if $opt_id;
	my $jitter_stat= $opt_rtp || $opt_crude;
Olav Kvittem's avatar
Olav Kvittem committed
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 1119 1120 1121 1122 1123 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 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	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);
    	
    	$packets_dupXML->addText($dup{$f});
		$packets_lateXML->addText($late{$f});
		$packets_lostXML->addText(&format_numbers($nloss{$f}));
		my $n=$npkt{$f}; $n=1 if $npkt{$f} <1; # divide by zero protection
		$packets_sizeXML->addText($sumbyte{$f}/$n);
		
	    #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
	
	$packets_numbXML->addText($npkt{$f});
	#$packets_sizeXML->addText();

	#bandwidth	
	$avg_bw->addText(&format_numbers( avg( $sumbyte{$f}*8, $span )));
	$max_bw->addText(&format_numbers($maxbps{$f}));
	$max_bw_100ms->addText(&format_numbers($max100ms{$f}));
	$sdv_bw->addText(&format_numbers(sdv($n100ms{$f},
			$akk100ms{$f}, 
			$ss100bps{$f})));	
	if ($opt_mpeg) {			
	#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
1244
		    $lost_packets_XML->addText(&format_numbers($mpeg_stats{$f}{$pid}{'lost_packets'}));
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
		    $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
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
}

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
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
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
1295

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

Olav Kvittem's avatar
Olav Kvittem committed
1299 1300 1301 1302
	if ($seq_diff == 0) { # Duplicated
	    $dup{$f} ++;
	} elsif ($seq_diff < 0) { # Out Of Order
	    $late{$f}++;
Olav Kvittem's avatar
Olav Kvittem committed
1303
#		$lost{$f}--;
Olav Kvittem's avatar
Olav Kvittem committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	} 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
1314
#  compute jitter relative to previous packet
Olav Kvittem's avatar
Olav Kvittem committed
1315 1316
	$mgap= ($mtime - $pmtime{$f} ); # media time gap in samples 8khz=us
	$jitter=abs($gap - $mgap);
Olav Kvittem's avatar
Olav Kvittem committed
1317 1318 1319
#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
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	$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})){
	$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;
Olav Kvittem's avatar
Olav Kvittem committed
1351
	    }
Olav Kvittem's avatar
Olav Kvittem committed
1352 1353 1354 1355 1356 1357 1358 1359 1360
	    printf "setup $f : %.3f\n",  $setuptime{$f} if $opt_debug;
	}
    }
    $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;
1361 1362 1363 1364
    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
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
    $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
1418 1419 1420 1421 1422 1423
	}
	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
1424
	    }
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
1425
	    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
1426 1427 1428 1429
	    
	} elsif ($opt_crude){
	    &seq_stat($f, $seq_num{$f}, $mtime{$f});

1430 1431 1432 1433
	} 
	#&burst($f);
	&burst($f) if !$opt_tcp;
	&burst_2($f) if $opt_tcp;
Olav Kvittem's avatar
Olav Kvittem committed
1434
    }
Olav Kvittem's avatar
Olav Kvittem committed
1435 1436 1437 1438
    if ($opt_period){
	$interval=int($us/10**6 / $opt_period);
	if ($interval > $pinterval){
	    &mpeg_debug() if $opt_debug;
1439 1440
	    &display_stats() if !$opt_rtmp;
	    &init_stats($f);
Olav Kvittem's avatar
Olav Kvittem committed
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	    $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
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
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 ) {
	    	for my $pid ( keys %{$pts_stats->{ $k1 }} ) {
				if ( grep { $_ eq $pts_stats->{ $f }{ $pid }{ 'stream_id_type'} || $_ eq 'all'} @filter) {
					$pts_stats{$k1}{$pid}{'dwell_avg'} = (10**3) * avg($pts_stats{$k1}{$pid}{'dwell_sum'}, $pts_stats{$k1}{$pid}{'num'});
					$pts_stats{$k1}{$pid}{'dwell_sdv'} = (10**3) * sdv($pts_stats{$k1}{$pid}{'num'}, $pts_stats{$k1}{$pid}{'dwell_sum'},  $pts_stats{$k1}{$pid}{'dwell_squared'});
					$mpeg_stats{$k1}{$pid}{'media_bandwidth'} = ((($mpeg_stats{$k1}{$pid}{'num'} * 188)*8)/$span);
					$mpeg_stats{$k1}{$pid}{'avg_packets_1s'} = avg($mpeg_stats{$k1}{$pid}{'num'},$span);
					$mpeg_stats{$k1}{$pid}{'media_loss_rate'} = ($mpeg_stats{$k1}{$pid}{'lost_packets'}/$span);
					$mpeg_stats{$k1}{$pid}{'media_bandwidth'} = 1 if $mpeg_stats{$k1}{$pid}{'media_bandwidth'}==0; #Zero division protection.
					$mpeg_stats{$k1}{$pid}{'delay_factor'} = ((($mpeg_stats{$k1}{$pid}{'avg_packets_1s'}* $pts_stats{$k1}{$pid}{'dwell_avg'}*(10**-3))*188)/$mpeg_stats{$k1}{$pid}{'media_bandwidth'})*(10**2) if($opt_new || $opt_xml);
				}
			}
		}
	}
	if($opt_tcp){
			$tcp_packet_stats{$f}{'span'} =  $span;
			$tcp_packet_stats{$f}{'avg_bandwidth'} = (($tcp_packet_stats{$f}{'num_bytes'}*8)/$span);
			$tcp_packet_stats{$f}{'jitter_avg'} = avg($tcp_packet_stats{$f}{'jitter_sum'},$tcp_packet_stats{$f}{'num_packets'}); 
			$tcp_packet_stats{$f}{'jitter_sdv'} = sdv($tcp_packet_stats{$f}{'num_packets'}, $tcp_packet_stats{$f}{'jitter_sum'}, $tcp_packet_stats{$f}{'jitter_square'});
			$tcp_packet_stats{$f}{'gap_avg'} = (10**3)*avg($tcp_packet_stats{$f}{'gap_sum'},$tcp_packet_stats{$f}{'num_packets'});
			$tcp_packet_stats{$f}{'gap_sdv'} = (10**3)*sdv($tcp_packet_stats{$f}{'num_packets'}, $tcp_packet_stats{$f}{'gap_sum'}, $tcp_packet_stats{$f}{'gap_square'});
	}
}
Olav Kvittem's avatar
Olav Kvittem committed
1483 1484 1485 1486 1487

sub burst {
    my $f=shift;
    my $span=$last_us{$f}-$first_us{$f};
    my $per1s=int($span/10**6);
Olav Kvittem's avatar
Olav Kvittem committed
1488
    #printf "per1s: %f\n", $per1s;
Olav Kvittem's avatar
Olav Kvittem committed
1489
    my $per100ms=int($span/10**5);
Olav Kvittem's avatar
Olav Kvittem committed
1490 1491
    #printf "per1s_f%f\n", $per1s{$f};
    
Olav Kvittem's avatar
Olav Kvittem committed
1492 1493
    if ( $per1s > $per1s{$f} ){ # period expired
	my $per=($last_us{$f}-$prev_1s{$f})/10**6;
Olav Kvittem's avatar
Olav Kvittem committed
1494
	#		printf "per: %f\n", $per;
Olav Kvittem's avatar
Olav Kvittem committed
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	if ($per>0){
	    my $bps = $sum1s{$f} * 8 / $per;
	    $maxbps{$f} =  $bps if $bps > $maxbps{$f};
	    $per1s{$f} =  $per1s;
	    $prev_1s{$f} = $last_us{$f};
	    $sum1s{$f}=0;
	}
    }
    if ( $per100ms > $per100ms{$f} ){ # period expired
	my $per=($last_us{$f}-$prev_100ms{$f})/10**6;
	if ($per>0){
	    my $bps = $sum100ms{$f} * 8 / $per;
	    $max100ms{$f} =  $bps if $bps > $max100ms{$f};
	    $n100ms{$f}++;
	    $ss100bps{$f}+= $bps**2;
	    $akk100ms{$f}+=$bps;
	    $per100ms{$f} =  $per100ms;
	    $prev_100ms{$f} = $last_us{$f};
	    $sum100ms{$f}=0;
	}
    }
}

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
sub burst_2 {
    my $f=shift;
    #my %bw_stats = ();
    my $span=$last_us{$f}-$first_us{$f};
    my $per1s=int($span/10**6);
    #printf "per1s: %f\n", $per1s;
    my $per100ms=int($span/10**5);
    #printf "per1s_f%f\n", $per1s{$f};
  	#$bw_stats{$f}{'maxbps'} = "destat";  
    if ( $per1s > $per1s{$f} ){ # period expired
	my $per=($last_us{$f}-$prev_1s{$f})/10**6;
	#		printf "per: %f\n", $per;
	if ($per>0){
	    my $bps = $sum1s{$f} * 8 / $per;
	    $maxbps{$f} =  $bps if $bps > $maxbps{$f};
	     $bw_stats{$f}{'maxbps'} =  $bps if $bps > $bw_stats{$f}{'maxbps'};
	    $per1s{$f} =  $per1s;
	     $bw_stats{$f}{'per1s'}= $per1s;
	    $prev_1s{$f} = $last_us{$f};
	     $bw_stats{$f}{'per1s'}= $last_us{$f};
	    $sum1s{$f}=0;
	}
    }
    if ( $per100ms > $per100ms{$f} ){ # period expired
	my $per=($last_us{$f}-$prev_100ms{$f})/10**6;
	if ($per>0){
	    my $bps = $sum100ms{$f} * 8 / $per;
	    $max100ms{$f} =  $bps if $bps > $max100ms{$f};
	     $bw_stats{$f}{'max100ms'} = $bps if $bps > $bw_stats{$f}{'max100ms'} ;
	     #print "$bw_stats{$f}{$f}{'max100ms'} \n";
	    $n100ms{$f}++;
	     $bw_stats{$f}{'n100ms'}++;
	    $ss100bps+= $bps**2;
	     $bw_stats{$f}{'ss100bps'} +=$bps**2;
	    $akk100ms{$f}+=$bps;
	     $bw_stats{$f}{'akk100ms'} +=$bps;
	    $per100ms{$f} =  $per100ms;
	     $bw_stats{$f}{'per100ms'} = $per100ms;
	    $prev_100ms{$f} = $last_us{$f};
	    $sum100ms{$f}=0;
	}
    }
}
Olav Kvittem's avatar
Olav Kvittem committed
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
sub jitter_stats {
	my $pts_stats = shift;
	my $span = shift;
	Dumper($pts_stats);
	my $counter = 0;
	my $max_colums = (length(@filter)+1);
	for my $k1 ( sort keys %$pts_stats ) {
    	for my $k2 ( keys %{$pts_stats->{ $k1 }} ) {
      		if ( grep { $_ eq $pts_stats->{ $k1 }{ $k2 }{ 'stream_id_type'} || $_ eq 'all'} @filter) {
      			$counter++;
        		$jitter_stats{$k2}{'dwell_avg'} = &lesbar( (10**3) * avg($pts_stats{$k1}{$k2}{'dwell_sum'}, $pts_stats{$k1}{$k2}{'num'}));
        		$jitter_stats{$k2}{'dwell_min'} = &lesbar( (10**3) * $pts_stats{$k1}{$k2}{'min_dwell'});
        		$jitter_stats{$k2}{'dwell_max'} = &lesbar( (10**3) * $pts_stats{$k1}{$k2}{'max_dwell'});
        		$jitter_stats{$k2}{'dwell_sdv'} = &lesbar((10**3) * sdv($pts_stats{$k1}{$k2}{'num'}, $pts_stats{$k1}{$k2}{'dwell_sum'},  $pts_stats{$k1}{$k2}{'dwell_squared'}));
        		$jitter_stats{$k2}{'type'} = $pts_stats->{ $k1 }{ $k2 }{ 'stream_id_type'};
        		$jitter_stats{$k2}{'media_loss_rate'} =  ($mpeg_stats{$k1}{$k2}{'lost_packets'}/$span);
        		$jitter_stats{$k2}{'DF'} =  $mpeg_stats{$k1}{$k2}{'delay_factor'};
        		last if $counter >= $max_colums;
      		}
      	}
  	}		    
	return \%jitter_stats;
}

sub get_date_time{
	my $f = shift;
Olav Kvittem's avatar
Olav Kvittem committed
1588 1589 1590
	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"){
1591 1592 1593 1594
			    $time.=sprintf ".%06d", $tstart{$f}->[1];
			} else {
			    my $time=sprintf "%02d:%02d:%02d", $hour, $min, $sec;
			}
Olav Kvittem's avatar
Olav Kvittem committed
1595
	my $date=sprintf "%4d-%02d-%02d", $year+1900, $mon+1, $mday;
1596
	return ($date,$time);
Olav Kvittem's avatar
Olav Kvittem committed
1597

1598
}
Olav Kvittem's avatar
Olav Kvittem committed
1599

1600 1601 1602 1603 1604 1605 1606 1607 1608
sub display_stats{
	@myflows=@streams if ($#myflows < 0); # no response
	foreach $f (sort @myflows) {
	my ($ip,$port) = prepare_ip($f) if $opt_tcp;