qstream 127 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
7
# XML, RTMP, TCP support, Pål Moen Møst, 2011
Olav Kvittem's avatar
Olav Kvittem committed
8
9
10
11
12
13
14
15
16

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

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

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

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

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
my $initial_wait= $opt_wait || 3 ; # frac seconds to wait initially
59
my $timeout = $initial_wait;
Olav Kvittem's avatar
Olav Kvittem committed
60
61
my $next_wait=0.1 ; # frac seconds to wait for receiving between packets

Olav Kvittem's avatar
Olav Kvittem committed
62
63
my @streams=@ARGV;

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

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

114
115
my %offset_bytes =();
my %network_info =();
116
117

my $start_time = time();
Olav Kvittem's avatar
Olav Kvittem committed
118
119
120
#MPEG-TS measurement globals (most are reset in init)
my $CHECK_NUM_TSP = 2;
#globals for mpeg ts 
121
#a tabel for transport streams pids. aleternativ to PAT impl.
122
my %pcr_pid = ();
123
my $TS;
Olav Kvittem's avatar
Olav Kvittem committed
124
125
126
127
128
129
130
131
132
133
134
135
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 = ();
136
my %jitter_stats = ();
137

Olav Kvittem's avatar
Olav Kvittem committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#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
152
153
$file_flag = 1 if $opt_filename;

154
#MPEG-TS END
Olav Kvittem's avatar
Olav Kvittem committed
155
156


157
158
$SIG{USR2} = sub {confess "Caught by SIGUSR2"; };

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

$SIG{TERM} = sub { $uninterrupted=0; # return if $nkill++ < 1;
167
		   &display_stats() if  !$endstream;  
168
169
170
171
172
		   &end_xml() if $opt_xml;
		   &handle_threads()  if ($opt_tcp and $opt_rtmp);

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

Olav Kvittem's avatar
Olav Kvittem committed
173
$SIG{ALRM} = sub { 
174
#   Writes stats to file if option XML is given. -PMM
Olav Kvittem's avatar
Olav Kvittem committed
175
#    die "No packets received" if $n_packets < 1;
176
177
178
179
    $uninterrupted=0;
    if  (! $endstream) {  # Changed logic 20120706:1007 CDT
		$endstream=1;
#ok	&display_stats;
180
    }
Olav Kvittem's avatar
Olav Kvittem committed
181
182
#	die "End after alarm\n"; 
#	return(0);
Olav Kvittem's avatar
Olav Kvittem committed
183
    print "endstream=1" if $opt_debug;
Olav Kvittem's avatar
Olav Kvittem committed
184
};
Olav Kvittem's avatar
Olav Kvittem committed
185
186
187
188

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

190
191
192
193
194
195
196
197
if ($opt_test){
    if($opt_mpeg || $opt_rtp || $opt_tcp_t){
	&unit_test;
    }
    else {
	print "You need to choose a test. (mpeg, tcp or rtp) \n";
    }
}
Olav Kvittem's avatar
Olav Kvittem committed
198

199
if ($opt_pcap || ($opt_tcp && !$opt_rtmp)){ # listen in parallell
Olav Kvittem's avatar
Olav Kvittem committed
200
201
202
    alarm($opt_last) if $opt_last; 
    $uninterrupted=1;
    $endstream=0;
203
    eat_pcap_streams(@streams);
204
    &display_stats() if !$opt_xml and !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
205
} else { # listen serially
206
    my $stream_count;
207
    #my $ln = @streams;
Olav Kvittem's avatar
Olav Kvittem committed
208
    foreach $id (@streams) {
209
210
	alarm($opt_last) if $opt_last;
	++$stream_count;
211
	#$last_stream = 1 if $stream_count == ($ln);
212
	#print "count $stream_count  lengde  $ln \n";
Olav Kvittem's avatar
Olav Kvittem committed
213
214
215
	$uninterrupted=1;
	$endstream=0;
	init_file($id);
216
217
	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;
218
	#&eat_tcp($id,$opt_port) if $opt_tcp && !$opt_rtmp;
paalmm's avatar
paalmm committed
219
	if (! ( $opt_list || $opt_sum || $opt_period)){
220
	    &display_stats();
Olav Kvittem's avatar
Olav Kvittem committed
221
222
223
	}
	last if ! $uninterrupted;
    }
224
    
Olav Kvittem's avatar
Olav Kvittem committed
225
226
}
&list_flows if $opt_list;
227
&display_stats() if $opt_sum and !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
228
&display_bins if $opt_bins;
229
&end_xml() if $opt_xml;
Olav Kvittem's avatar
Olav Kvittem committed
230

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

278
sub xml_parser {
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
    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 ($@){
			    };
307
			}
308
309
310
311
		    }
		}
		else {
		    $test_hash{'flow'}{$child->getNodeName()} =  $child->getFirstChild()->getData();
312
     		}	
313
314
315
316
	    }
	} 
    }
    return (\%test_hash)
317
}
paalmm's avatar
paalmm committed
318

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

356
357
##TCP impl. -PMM
sub handle_threads {
358
359
    $tcp_stop = 1;
    $thr->join();
360
361
}
sub end_xml{
362
363
364
365
366
    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";
367
    open (MYFILE, '>'.$filename) || die "Could not open $filename : $!";
368
369
370
371
372
373
374
375
    my $final_xml = $doc->toString();
    $twig->parse($final_xml);
    $twig->flush(\*MYFILE);
    #print "</qstream> \n"  if $opt_xml eq "-";
    $doc->dispose;
    close (MYFILE);
    $xml_printed = 1;
    &unit_test_2($filename) if $opt_test;
376
377
378
}

sub tcp_packet_stats {
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
    my $f = shift;
    my $ip = shift;
    my ($tcp_obj) = shift;
    my $len,$ip_hlen = 0;
    #ipv6.pm uses different variables. 
    if($opt_ipv6){
	$len = plen;
    }
    else {
	$len = len;
	$ip_hlen = 20; 
    }
    #my $ip = NetPacket::IP->decode(eth_strip($pkt));
    #my $tcp_obj = NetPacket::TCP->decode(ip_strip(eth_strip($pkt)));
    #my $f = $ip->{src_ip}.":".$tcp_obj->{src_port} if !$offline;
    my $seq_num = $tcp_obj->{seqnum};
    my $options = $tcp_obj->{options}; #12 bytes
    #print "$tcp_obj->{src_port} \n";
    if (!exists $tcp_packet_stats{$f}){
	&init_stats($f);
	$tcp_packet_stats{$f} = &share( {} );
	$seq_stats{$f} = &share( {} );
	$seq_stats_n{$f} = &share( {} );
	$tcp_packet_stats{$f}{'pkt_late'} = 0;
	$tcp_packet_stats{$f}{'pkt_retrans'} =0;
	$tcp_packet_stats{$f}{'pkt_dup'} = 0;
	
    }
    ++$tcp_packet_stats{$f}{'num_packets'};
    &tcp_jitter_stats($f,$options);
    #Bandwidth messurements
    $tcp_packet_stats{$f}{'num_bytes'} += $ip->{$len}; #counts the bytes
    #Check for duplicated,out-of-order, late and lost packets.
    $seq_stats{$f}{'seq_diff'} = $seq_num - $seq_stats{$f}{'p_seq_num'} - $seq_stats{$f}{'p_packet_len'};
    if ($seq_stats_n{$f}{'seq_num_p'} == $seq_num && $seq_stats{$f}{'p_seq_num'} != 0) {
	++$tcp_packet_stats{$f}{'pkt_dup'};
    }
    if ($seq_stats{$f}{'seq_diff'} != 0 && $seq_stats{$f}{'p_seq_num'} != 0){
	if ($seq_stats{$f}{'seq_diff'} < 0){
	    ++$tcp_packet_stats{$f}{'pkt_late'};
	    if ($seq_stats_n{$f}{'seq_num_n'} == $seq_num){
		++$tcp_packet_stats{$f}{'pkt_retrans'};
	    }
422
423
	}
	else {
424
	    ++$tcp_packet_stats{$f}{'pkt_lost'};
425
	}
426
427
428
429
430
431
432
433
    }
    if($seq_stats{$f}{'p_seq_num'} < $seq_num){
	$seq_stats{$f}{'n_seq_num'} = ($seq_num+$ip->{$len} -($ip_hlen)-($tcp_obj->{hlen}*4))
    } 
    $seq_stats{$f}{'p_seq_num'} = $seq_num;
    $seq_stats{$f}{'p_packet_len'} = $ip->{$len}-($ip_hlen)-($tcp_obj->{hlen}*4);
    $seq_stats_n{$f}{'seq_num_p'} = $seq_num;
    $seq_stats_n{$f}{'seq_num_n'} = $seq_stats{$f}{'n_seq_num'};
434
435
}
sub tcp_jitter_stats {
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
    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;
    }
476
477
}
sub check_period {
478
479
480
481
482
483
    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);
    }
484
485
}
sub eat_tcp {
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
    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;
503
		}
504
505
		printf "setup $f : %.3f\n",  $setuptime{$f} if $opt_debug;
	    }
506
	}
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
    }
    $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);
528
	}
529
    }
530
531
532
}
##RTMP impl. -PMM
sub rtmp_eat_stream {
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
    ($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;
548
		}
549
550
		printf "setup $f : %.3f\n",  $setuptime{$f} if $opt_debug;
	    }
551
	}
552
553
554
555
556
557
558
559
	$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;
	
    }
560
}
Olav Kvittem's avatar
Olav Kvittem committed
561
562


563
sub rtmp_prepare_url{
564
565
566
567
568
569
570
571
572
573
574
    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);
575
    }    
576
577
578
579
580
581
    for (my $i = 1; $i < $length;$i++){
	$app .= $string[$i];
	if ($i > 1 ){$app .="/";}
    }
    my $play = $string[$length];
    return ($address,$app,$play)
582
}
Olav Kvittem's avatar
Olav Kvittem committed
583
584
585
586
587
#------------------------------------------------------------------------------

# unpack uri's to ip-address
sub prepare_ip {
    my $uri=shift;
588
    $uri =~ s/^(udp|rtp|http)://;
Olav Kvittem's avatar
Olav Kvittem committed
589
590
    $uri =~ s#//##;
    die "Unsupported streaming $1" if $uri =~/^(\w:)/;
Olav Kvittem's avatar
Olav Kvittem committed
591
592
593
    if ($multicast = ($uri =~ /@/)){
	($source, $uri)=split(/@/, $uri);
    }
Olav Kvittem's avatar
Olav Kvittem committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629

    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())){
630
	    my $timeout=$next_wait; 
Olav Kvittem's avatar
Olav Kvittem committed
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
		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?
652
		&pkt_stats($cur_id, $packet->encode, $us, $packet->size());
653
		
Olav Kvittem's avatar
Olav Kvittem committed
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
		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
	}

paalmm's avatar
paalmm committed
689
690
	#while ($uninterrupted && !($opt_packets && $n_packets > $opt_packets)){
	while ($uninterrupted && ! $endstream && !($opt_packets && $n_packets > $opt_packets)){
691
 		if ($select->can_read($timeout) && $mc->recv($packet, 1560)) {
Olav Kvittem's avatar
Olav Kvittem committed
692
		my $timeout=$next_wait; 
Olav Kvittem's avatar
Olav Kvittem committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
		$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?
714
		&pkt_stats($cur_id, $packet, $us, length($packet));
Olav Kvittem's avatar
Olav Kvittem committed
715
716
717
		if ($opt_dump){
		    print DUMP $packet;
		}
718
		last if ! $uninterrupted || $endstream;
paalmm's avatar
paalmm committed
719
		$endstream = tv_interval($t0, $tc) >= $opt_last if $opt_last && !$endstream;
Olav Kvittem's avatar
Olav Kvittem committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
	    } 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 : $!";
	}
738
739
	if ($opt_crude){
	    my $flow_key;
Olav Kvittem's avatar
Olav Kvittem committed
740
	    open CRUDE, "<$pcap_file" || die "Could not open $f : $err";
741
	    while(<CRUDE>){  # crude.sourceforge.net
Olav Kvittem's avatar
Olav Kvittem committed
742
743
		my $tx0;
		
744
		if (($fid, $seq, $src, $dst, $tx, $rx, $size)=
745
		    /ID=(\d+)\s+SEQ=(\d+)\s+SRC=([\d.:]+)\s+DST=([\d.:]+)\s+Tx=([\d.,]+)\s+Rx=([\d.,]+)\s+SIZE=(\d+)/){
746
747
748

		    $flow_key="$src->$dst";
		    $flow_pkts{$flow_key}++;
Olav Kvittem's avatar
Olav Kvittem committed
749
		    $tc=[int($rx), ($tx-int($rx))*10^6] ; # tval
750
751
752
753
754
		    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
755
756
757
758
759
			$rx0{$flow_key}=$rx;
			$tx0{$flow_key}=$tx;
			$tjoined{$flow_key}=$tc;
		    } 
		    
760
761
762
763
764
765
766
767
768
		    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
769
770
771
772
773
774
775
		    $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);
776
		}
Olav Kvittem's avatar
Olav Kvittem committed
777
	    }
778
779
780
781
782
783
	    
	} 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) ) ) {
784
785
		    #$file_flag =1;
		    $offline =1;
786
787
788
		    eat_pcap($f, $pcap, $pkt);
		    $n_packets++;
		}
Olav Kvittem's avatar
Olav Kvittem committed
789
790
		printf "%s\n", $ttl_log if  $opt_ttl;

791
792
793
794
	    } 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
795
796
797
798
799
800
801
802
803
804
805
	    }
	}
	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="( ";
806
    my ($address, $port, $multicast);
Olav Kvittem's avatar
Olav Kvittem committed
807
    my $mc = IO::Socket::Multicast->new(LocalPort=>$DEFAULT_PORT, ReuseAddr=>1) ||
Olav Kvittem's avatar
Olav Kvittem committed
808
	die "Could not connect to port $port : $!";
Olav Kvittem's avatar
Olav Kvittem committed
809
    foreach $id (@streams){ # listen to all groups
810
	if ($opt_ipv6){
811
812
	    $address = Net::IPv6Addr::ipv6_parse($id);
	    $port =  $opt_p;
813
814
	}
	else {
815
	    ($address, $port, $multicast) = prepare_ip($id);
816
	}
Olav Kvittem's avatar
Olav Kvittem committed
817
818
819
820
821
822
	$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)"; 
823
	}
Olav Kvittem's avatar
Olav Kvittem committed
824
825
826
827
828
829
830
    }
    $tjoined{$f}=[gettimeofday];
    $filter .= " )";
#    $filter .= " and ip multicast";
#    $filter ="ip";
    my ($net, $netmask, $filter_t, $err, $dev);
#    printf $filter;
831
    $dev = Net::Pcap::lookupdev(\$err) || die "Could not lookupdev $dev : $err : $! .. Needs to run as sudo ..";
Olav Kvittem's avatar
Olav Kvittem committed
832
833
    my $r = Net::Pcap::lookupnet( $dev, \$net, \$netmask, \$err);
    die "Failed lookupnet for $dev : $r : $err : $!" if $r != 0;
834
835
836
    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
837
838
839
840
841
842
843
844
845
846
847
    $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
848
849
    printf "%s\n", $ttl_log if  $opt_ttl;

Olav Kvittem's avatar
Olav Kvittem committed
850
851
}

852
sub set_network_info{
853
854
855
856
857
    my ($f,$src_ip,$src_port,$dst_ip,$dst_port) = @_;
    $network_info{$f}{'src_ip'} = $src_ip;
    $network_info{$f}{'dst_ip'} = $dst_ip;
    $network_info{$f}{'src_port'} = $src_port;
    $network_info{$f}{'dst_port'} = $dst_port;	
858
}
859
sub eat_pcap { # process pcap packets	
Olav Kvittem's avatar
Olav Kvittem committed
860
861
    my ($f, $pcap, $pkt)=@_;
    my $ip=null;
862
    my $ip_payload;# udp or tcp packet.
Olav Kvittem's avatar
Olav Kvittem committed
863
    my $rtp = new Net::RTP::Packet(); # spare rtp obj
864
    my $proto,$len=null;
Olav Kvittem's avatar
Olav Kvittem committed
865
    if (Net::Pcap::datalink($pcap) == 1){ # ethernet
866
    	if ($opt_ipv6){
867
868
869
870
871
872
873
874
875
876
877
	    $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
878
879
880
881
882
883
    } 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);
    }
884
    
885
886
    if ($proto == IP_PROTO_TCP){
    	$opt_tcp = 1; 
887
	$ip_payload = NetPacket::TCP->decode($ip->{data});
888
889
890
891
892
    }
    else {
    	$ip_payload = NetPacket::UDP->decode($ip->{data});
    }
    my $dlen = $ip->{$len} - $hlen;
Olav Kvittem's avatar
Olav Kvittem committed
893
894
895
    my $flow_key;
    if ($opt_sum){
	$flow_key=$opt_sum;
896
897
898
899
900
    }
    elsif ($file_flag){
    	$flow_key = $f;
    }
    else {
901
	$flow_key=sprintf "%s:%d->%s:%d", 
902
        $ip->{src_ip}, $ip_payload->{src_port}, $ip->{dest_ip}, $ip_payload->{dest_port};
Olav Kvittem's avatar
Olav Kvittem committed
903
    }
904
    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
905
    $flow_pkts{$flow_key}++;
906
    #next if $ip->{proto} != IP_PROTO_UDP;
Olav Kvittem's avatar
Olav Kvittem committed
907
908
909
910
    if ($flow_pkts{$flow_key} <= 1) { # new flow
	push(@flows, $flow_key);  # 
	$flow_no++; 
	$file{$flow_key}=$f;
911
	$source{$flow_key} =sprintf "%s:%d", $ip->{src_ip}, $ip_payload->{src_port};
Olav Kvittem's avatar
Olav Kvittem committed
912
    }
Olav Kvittem's avatar
Olav Kvittem committed
913
914
915
916
917
918
919
    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}++;
920
	($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
921
922
923
924
925
926
	$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
927
928
929
930
931
932
    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);
933
	push (@myflows, $flow_key);
Olav Kvittem's avatar
Olav Kvittem committed
934
    }
935
936
937
938
    $tc= [$hdr{tv_sec}, $hdr{tv_usec}];
    if (! defined($t0)) {
	$t0=$tc;
    }
Olav Kvittem's avatar
Olav Kvittem committed
939
940
    $tjoined{$f}=$tc if !$tjoined{$f};
    $tjoined{$flow_key} = $tjoined{$f} if !exists($tjoined{$flow_key});
941
    $us = tv_interval ( $t0, $tc) * 10**6 ; # us relative packet time 	  
Olav Kvittem's avatar
Olav Kvittem committed
942
943
# fixate on first flow to appear
#    if ( $first_port) {
944
#	next if $ip_payload->{dest_port} != $first_port;
Olav Kvittem's avatar
Olav Kvittem committed
945
#    } else {
946
#	$first_port =  $ip_payload->{dest_port};
Olav Kvittem's avatar
Olav Kvittem committed
947
#    }
948
949
950
    if ($opt_tcp){
	&tcp_packet_stats($flow_key,$ip,$ip_payload);
    }
951
    &pkt_stats($flow_key, $ip_payload->{data}, $us, $dlen);
Olav Kvittem's avatar
Olav Kvittem committed
952
}
953
954

#--------------------------------------------------------------------------------
Olav Kvittem's avatar
Olav Kvittem committed
955
sub init_stats {
Olav Kvittem's avatar
Olav Kvittem committed
956
957
958
    my $f=shift;
    $pinterval=0;
    $npkt{$f}=$sumbyte{$f}=$ssbyte{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
959
    $first_sec=0;
Olav Kvittem's avatar
Olav Kvittem committed
960
    delete $pmtime{$f}; delete $pus{$f} ; delete $first_us{$f};  delete $last_us{$f}; 
961
#pmm delete $prev_1s{$f}; delete $prev_100ms{$f}; 
Olav Kvittem's avatar
Olav Kvittem committed
962
    delete $per1s{$f}; delete $per100ms{$f};
963
#pmm delete $sum1s{$f}; delete $sum100ms{$f};
964
    $n100ms{$f}=0; $sum100ms{$f}=0; $ss100bps{$f}=0; $akk100ms{$f}=0;
Olav Kvittem's avatar
Olav Kvittem committed
965
966
967
968
969
970
971
972
973
974
    $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}; 
975
    
Olav Kvittem's avatar
Olav Kvittem committed
976
977

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

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
    #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));
		}   
	    }     
1067
	}
1068
    }
1069
1070
1071
1072
    #bandwidth	
    if ($opt_tcp){
    	$f = $id;
    	my $bw_stats = $doc->createElement('bandwidth_stats');
1073
1074
1075
	my $avg_bw = $doc->createElement('bandwidth_avg');
    	my $max_bw = $doc->createElement('bandwidth_max_1s');
	my $max_bw_100ms = $doc->createElement('bandwidth_max_100ms');
1076
1077
1078
1079
1080
1081
    	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);
1082
    	$avg_bw->addText(&format_numbers( avg( $bw_stats{$f}{'sumbyte'}*8, $span )));
1083
1084
1085
1086
1087
	$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'})));
1088
1089
    	my $child = $doc->createElement('tcp_packet_stats');
    	$flow->appendChild($child);
1090
1091
1092
1093
1094
1095
1096
	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);
	}
    } 
1097
1098
1099
1100
    #my $final_xml = $flow->toString();
    &init_stats($id);
}

1101
sub xml_new {
1102
1103
    my ($pcr_stats,$pts_stats,$mpeg_stats) = (\%pcr_stats, \%pts_stats,\%mpeg_stats) if $opt_mpeg;
    my $doc = XML::DOM::Document->new;
1104
1105
1106
1107
1108
1109
1110
1111
1112
    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 " ";
1113
    my $mpeg_stats_parrent = $doc->createElement('mpeg_stats');
1114
1115
1116
1117
    my $pcr_stats_parrent = $doc->createElement('pcr_stats');
    my $transport_streams = $doc->createElement('transport_streams');
    my $pts_stats_parrent = $doc->createElement('pts_stats');

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


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

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

1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
    #network_stats
    $setuptimeXML->addText(&format_numbers($setuptime{$f}));
    $gap_avgXML->addText(&format_numbers(avg( $sumgap{$f}, $ngap{$f}) / 1000 ));
    $gap_sdvXML->addText(&format_numbers(sdv($ngap{$f}, $sumgap{$f},$ssgap{$f})/1000));
    $gap_minXML->addText(&format_numbers($mingap{$f}/1000));
    $gap_maxXML->addText(&format_numbers($mingap{$f}/1000));
    
    #Jitter / $opt_crue or $opt_rtp
    if ($jitter_stat){
	#create elements
	my $MOS_XML = $doc->createElement('MOS');
	my $rtp_timestamp = $doc->createElement('rtp_timestamp');
	my $rtp_dup = $doc->createElement('rtp_duplicated');
	my $rtp_late = $doc->createElement('rtp_pkt_late');
	my $rtp_lost = $doc->createElement('rtp_pkt_lost');
	my $rtp_gaps = $doc->createElement('rtp_pkt_gaps');
	my $rtp_avg = $doc->createElement('rtp_pkt_lost_avg');
	my $rtp_sdv = $doc->createElement('rtp_pkt_lost_sdv');
	my $rtp_jitter_numb = $doc->createElement('rtp_jitter_numb');
	my $rtp_jitter_avgXML = $doc->createElement('rtp_jitter_avg');
	my $rtp_jitter_sdvXML = $doc->createElement('rtp_jitter_sdv');
	my $rtp_jitter_minXML = $doc->createElement('rtp_jitter_min');
	my $rtp_jitter_maxXML = $doc->createElement('rtp_jitter_max');
	my $rtp_jitter_rfc = $doc->createElement('rtp_jitter_rfc');
	my $packets_dupXML = $doc->createElement('packet_duplicated');
1288
1289
    	my $packets_lateXML = $doc->createElement('packet_late');
    	my $packets_lostXML = $doc->createElement('packet_lost');
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
	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);
1309
1310
1311
1312
    	$packet_stats->appendChild($packets_lateXML);
    	$packet_stats->appendChild($packets_lostXML);
    	$packet_stats->appendChild($packets_sizeXML);
    	
1313
    	$packets_dupXML->addText(&format_numbers($dup{$f}));
1314