blob: 7ad45631b933cd2cc2bbb1dbee42324dccb58242 [file] [log] [blame]
Henrik Lundin80518322015-05-28 12:37:46 +02001function rtpAnalyze( input_file )
2%RTP_ANALYZE Analyze RTP stream(s) from a txt file
3% The function takes the output from the command line tool rtp_analyze
4% and analyzes the stream(s) therein. First, process your rtpdump file
5% through rtp_analyze (from command line):
6% $ out/Debug/rtp_analyze my_file.rtp my_file.txt
7% Then load it with this function (in Matlab):
8% >> rtpAnalyze('my_file.txt')
9
10% Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
11%
12% Use of this source code is governed by a BSD-style license
13% that can be found in the LICENSE file in the root of the source
14% tree. An additional intellectual property rights grant can be found
15% in the file PATENTS. All contributing project authors may
16% be found in the AUTHORS file in the root of the source tree.
17
Henrik Lundin80518322015-05-28 12:37:46 +020018[SeqNo,TimeStamp,ArrTime,Size,PT,M,SSRC] = importfile(input_file);
19
Henrik Lundin60508f82015-06-03 09:38:23 +020020%% Find streams.
Henrik Lundin80518322015-05-28 12:37:46 +020021[uSSRC, ~, uix] = unique(SSRC);
22
Henrik Lundin60508f82015-06-03 09:38:23 +020023% If there are multiple streams, select one and purge the other
24% streams from the data vectors. If there is only one stream, the
25% vectors are good to use as they are.
Henrik Lundin80518322015-05-28 12:37:46 +020026if length(uSSRC) > 1
27 for i=1:length(uSSRC)
28 uPT = unique(PT(uix == i));
29 fprintf('%i: %s (%d packets, pt: %i', i, uSSRC{i}, ...
30 length(find(uix==i)), uPT(1));
31 if length(uPT) > 1
32 fprintf(', %i', uPT(2:end));
33 end
34 fprintf(')\n');
35 end
36 sel = input('Select stream number: ');
37 if sel < 1 || sel > length(uSSRC)
38 error('Out of range');
39 end
Henrik Lundin60508f82015-06-03 09:38:23 +020040 ix = find(uix == sel);
41 % This is where the data vectors are trimmed.
Henrik Lundin80518322015-05-28 12:37:46 +020042 SeqNo = SeqNo(ix);
43 TimeStamp = TimeStamp(ix);
44 ArrTime = ArrTime(ix);
45 Size = Size(ix);
46 PT = PT(ix);
47 M = M(ix);
48 SSRC = SSRC(ix);
49end
50
Henrik Lundin60508f82015-06-03 09:38:23 +020051%% Unwrap SeqNo and TimeStamp.
Henrik Lundin80518322015-05-28 12:37:46 +020052SeqNoUW = maxUnwrap(SeqNo, 65535);
53TimeStampUW = maxUnwrap(TimeStamp, 4294967295);
54
Henrik Lundin60508f82015-06-03 09:38:23 +020055%% Generate some stats for the stream.
Henrik Lundin80518322015-05-28 12:37:46 +020056fprintf('Statistics:\n');
57fprintf('SSRC: %s\n', SSRC{1});
58uPT = unique(PT);
59if length(uPT) > 1
60 warning('This tool cannot yet handle changes in codec sample rate');
61end
62fprintf('Payload type(s): %i', uPT(1));
63if length(uPT) > 1
64 fprintf(', %i', uPT(2:end));
65end
66fprintf('\n');
67fprintf('Packets: %i\n', length(SeqNo));
68fprintf('Missing sequence numbers: %i\n', ...
69 length(find(diff(sort(SeqNoUW)) > 1)));
70fprintf('Reordered packets: %i\n', length(find(diff(sort(SeqNoUW)) < 1)));
71tsdiff = diff(TimeStampUW);
72tsdiff = tsdiff(diff(SeqNoUW) == 1);
73[utsdiff, ~, ixtsdiff] = unique(tsdiff);
74fprintf('Common packet sizes:\n');
75for i = 1:length(utsdiff)
76 fprintf(' %i samples (%i%%)\n', ...
77 utsdiff(i), ...
Henrik Lundin60508f82015-06-03 09:38:23 +020078 round(100 * length(find(ixtsdiff == i))/length(ixtsdiff)));
Henrik Lundin80518322015-05-28 12:37:46 +020079end
80
Henrik Lundin60508f82015-06-03 09:38:23 +020081%% Trying to figure out sample rate.
Henrik Lundin80518322015-05-28 12:37:46 +020082fs_est = (TimeStampUW(end) - TimeStampUW(1)) / (ArrTime(end) - ArrTime(1));
83fs_vec = [8, 16, 32, 48];
84fs = 0;
85for f = fs_vec
86 if abs((fs_est-f)/f) < 0.05 % 5% margin
87 fs = f;
88 break;
89 end
90end
91if fs == 0
92 fprintf('Cannot determine sample rate. I get it to %.2f kHz\n', ...
93 fs_est);
94 fs = input('Please, input a sample rate (in kHz): ');
95else
96 fprintf('Sample rate estimated to %i kHz\n', fs);
97end
98
99SendTimeMs = (TimeStampUW - TimeStampUW(1)) / fs;
100
101fprintf('Stream duration at sender: %.1f seconds\n', ...
102 (SendTimeMs(end) - SendTimeMs(1)) / 1000);
103
104fprintf('Stream duration at receiver: %.1f seconds\n', ...
105 (ArrTime(end) - ArrTime(1)) / 1000);
106
107fprintf('Clock drift: %.2f%%\n', ...
108 100 * ((ArrTime(end) - ArrTime(1)) / ...
109 (SendTimeMs(end) - SendTimeMs(1)) - 1));
110
111fprintf('Sent average bitrate: %i kbps\n', ...
112 round(sum(Size) * 8 / (SendTimeMs(end)-SendTimeMs(1))));
Henrik Lundin60508f82015-06-03 09:38:23 +0200113
Henrik Lundin80518322015-05-28 12:37:46 +0200114fprintf('Received average bitrate: %i kbps\n', ...
115 round(sum(Size) * 8 / (ArrTime(end)-ArrTime(1))));
116
Henrik Lundin60508f82015-06-03 09:38:23 +0200117%% Plots.
Henrik Lundin80518322015-05-28 12:37:46 +0200118delay = ArrTime - SendTimeMs;
119delay = delay - min(delay);
120figure
121plot(SendTimeMs / 1000, delay);
122xlabel('Send time [s]');
123ylabel('Relative transport delay [ms]');
124title(sprintf('SSRC: %s', SSRC{1}));
125
126SendBitrateKbps = 8 * Size(1:end-1) ./ diff(SendTimeMs);
127figure
128plot(SendTimeMs(1:end-1)/1000, SendBitrateKbps);
129xlabel('Send time [s]');
130ylabel('Send bitrate [kbps]');
131end
132
Henrik Lundin60508f82015-06-03 09:38:23 +0200133%% Auto-generated subfunction.
Henrik Lundin80518322015-05-28 12:37:46 +0200134function [SeqNo,TimeStamp,SendTime,Size,PT,M,SSRC] = ...
135 importfile(filename, startRow, endRow)
136%IMPORTFILE Import numeric data from a text file as column vectors.
137% [SEQNO,TIMESTAMP,SENDTIME,SIZE,PT,M,SSRC] = IMPORTFILE(FILENAME) Reads
138% data from text file FILENAME for the default selection.
139%
140% [SEQNO,TIMESTAMP,SENDTIME,SIZE,PT,M,SSRC] = IMPORTFILE(FILENAME,
141% STARTROW, ENDROW) Reads data from rows STARTROW through ENDROW of text
142% file FILENAME.
143%
144% Example:
145% [SeqNo,TimeStamp,SendTime,Size,PT,M,SSRC] =
146% importfile('rtpdump_recv.txt',2, 123);
147%
148% See also TEXTSCAN.
149
150% Auto-generated by MATLAB on 2015/05/28 09:55:50
151
152%% Initialize variables.
153if nargin<=2
154 startRow = 2;
155 endRow = inf;
156end
157
158%% Format string for each line of text:
159% column1: double (%f)
160% column2: double (%f)
161% column3: double (%f)
162% column4: double (%f)
163% column5: double (%f)
164% column6: double (%f)
165% column7: text (%s)
166% For more information, see the TEXTSCAN documentation.
167formatSpec = '%5f%11f%11f%6f%6f%3f%s%[^\n\r]';
168
169%% Open the text file.
170fileID = fopen(filename,'r');
171
172%% Read columns of data according to format string.
173% This call is based on the structure of the file used to generate this
174% code. If an error occurs for a different file, try regenerating the code
175% from the Import Tool.
176dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, ...
177 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow(1)-1, ...
178 'ReturnOnError', false);
179for block=2:length(startRow)
180 frewind(fileID);
181 dataArrayBlock = textscan(fileID, formatSpec, ...
182 endRow(block)-startRow(block)+1, 'Delimiter', '', 'WhiteSpace', ...
183 '', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
184 for col=1:length(dataArray)
185 dataArray{col} = [dataArray{col};dataArrayBlock{col}];
186 end
187end
188
189%% Close the text file.
190fclose(fileID);
191
192%% Post processing for unimportable data.
193% No unimportable data rules were applied during the import, so no post
194% processing code is included. To generate code which works for
195% unimportable data, select unimportable cells in a file and regenerate the
196% script.
197
198%% Allocate imported array to column variable names
199SeqNo = dataArray{:, 1};
200TimeStamp = dataArray{:, 2};
201SendTime = dataArray{:, 3};
202Size = dataArray{:, 4};
203PT = dataArray{:, 5};
204M = dataArray{:, 6};
205SSRC = dataArray{:, 7};
206end
207