-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcfull_analysis.py
executable file
·180 lines (140 loc) · 5.81 KB
/
cfull_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/python
import getopt
import pyshark
import numpy
import math
import sys
from decimal import *
PKT_SEQUENCE_BIT_DEPTH = pow(2,16) # The RTP packet sequence number is defined as a 16 bit number.
RTP_TIMESTAMP_BIT_DEPTH = pow(2,32) # The RTP timestamp value is defined as a 32 bit number.
RTP_CLOCK = 90000 # RTP clock Frequency is defined at 90kHz
B = 1.1 # Drain factor as defined in SMPTE2110-21
def frame_len(capture):
# To calculate Npackets, you need to count the amount of packets between two rtp.marker == 1 flags.
# This is as easy as looking to 2 rtp.marker == 1 packets and substract the rtp.sequence number.
# The exception that will occurs is that the packet sequence number rotates. Modulo is your friend!
first_frame = None
for pkt in capture:
if pkt.rtp.marker == '1':
if not first_frame:
first_frame = int(pkt.rtp.seq)
else:
return (int(pkt.rtp.seq) - first_frame) % PKT_SEQUENCE_BIT_DEPTH
return None
def frame_rate(capture):
# To calculate the framerate of a given capture, you need to look at three consequent rtp time stamps [(t2-t1) +
# (t3-t2)] / 2 will result in the average timestamp difference. Note: the frame periods (difference between 90
# kHz timestamps) might not appear constant For example 60/1.001 Hz frame periods effectively alternate between
# increments of 1501 and 1502 ticks of the 90 kHz clock.
rtp_timestamp = []
for pkt in capture:
if pkt.rtp.marker == '1':
if len(rtp_timestamp) < 3:
rtp_timestamp.append(int(pkt.rtp.timestamp))
else:
frame_rate_c = Decimal(RTP_CLOCK /
(( (rtp_timestamp[2] - rtp_timestamp[1]) % RTP_TIMESTAMP_BIT_DEPTH +
(rtp_timestamp[1] - rtp_timestamp[0]) % RTP_TIMESTAMP_BIT_DEPTH) / 2))
# frame_rate_c = Decimal(RTP_CLOCK /
# (((rtp_timestamp[2] - rtp_timestamp[1]) + (
# rtp_timestamp[1] - rtp_timestamp[0])) / 2))
return frame_rate_c
return None
def cfull_analysis(capture, tframe, npackets, B):
# Time different between packets
global idx
timediffs = []
# SMPTE2110-21: leaky bucket model
cfull = []
# Amount of packets cleared out of the buffer
cleared = []
# Start of the experiment
initialtime = time = oldTime = 0.000000000
# Formula defined in SMPTE2110-21
tdrain = tframe / npackets / Decimal(B)
print("Tdrain = {}".format(tdrain))
try:
for idx, pkt in enumerate(capture):
time = Decimal(pkt.time)
if initialtime == 0:
# Record initial timing of the PCAP file
initialtime = time
# Initiate the Cfull bucket with 1 packet
cfull.append(1)
cfull[-1] = 1
cleared.append(0)
if oldTime != 0:
timediff = time - oldTime
timediffs.append(timediff)
clearnbr = math.floor((Decimal(time) - Decimal(initialtime)) / Decimal(tdrain))
cleared.append(clearnbr)
buffer = cfull[-1] + 1 - (cleared[-1] - cleared[-2])
if buffer >= 0:
cfull.append(buffer)
if buffer < 0:
cfull.append(0)
oldTime = time
except KeyboardInterrupt:
print("\nInterrupted")
print("Intial Time = ", initialtime)
print("#packets = ", idx + 1)
print("#cleared = ", numpy.max(cleared))
print("Cfull Max = ", max(cfull))
print("Cfull Min = ", numpy.min(cfull))
print("Cfull Avg = ", numpy.mean(cfull))
print("average = ", numpy.mean(timediffs))
print("maximum = ", numpy.max(timediffs))
print("minimim = ", numpy.min(timediffs))
print("Reference Time :", Decimal(time) - Decimal(initialtime))
return cfull
def write_array(filename, array):
text_file = open(filename, "w")
idx = 0
while idx < len(array):
text_file.write(str(array[idx]) + "\n")
idx += 1
text_file.close()
return 0
def usage():
print("cfull_analysis.py -c|--cap <capture_file> -g|--group <multicast_group> -p|--port <udp_port>")
def getarguments(argv):
global opts
short_opts = 'hc:g:p:'
long_opts = ["help", "cap=", "group=", "port="]
try:
opts, args = getopt.getopt(argv, short_opts, long_opts)
if not opts:
print("No options supplied")
usage()
sys.exit(2)
except getopt.GetoptError:
print("Error in options {}".format(opts))
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-c", "--cap"):
capfile = arg
elif opt in ("-g", "--group"):
group = arg
elif opt in ("-p", "--port"):
port = arg
else:
print("unknown option " + opt)
usage()
sys.exit()
return (capfile, group, port)
if __name__ == '__main__':
capfile, group, port = getarguments(sys.argv[1:])
capture = pyshark.FileCapture(capfile, keep_packets=False, decode_as={"udp.port=" + port: 'rtp'}, display_filter='ip.dst==' + group + ' && rtp.marker == 1')
frame_ln = frame_len(capture)
print("Npackets : ", frame_ln)
framerate = frame_rate(capture)
print("Frame Frequency: ", round(framerate, 2), " Hz")
tframe = 1 / framerate
capture = pyshark.FileCapture(capfile, keep_packets=False, decode_as={"udp.port=" + port: 'rtp'},
display_filter='ip.dst==' + group, only_summaries=True)
cfull_array = cfull_analysis(capture, tframe, frame_ln, B)
write_array(capfile + "_cfull_" + ".txt", cfull_array)