Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 1 | function 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 Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 18 | [SeqNo,TimeStamp,ArrTime,Size,PT,M,SSRC] = importfile(input_file); |
| 19 | |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 20 | %% Find streams. |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 21 | [uSSRC, ~, uix] = unique(SSRC); |
| 22 | |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 23 | % 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 Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 26 | if 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 Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 40 | ix = find(uix == sel); |
| 41 | % This is where the data vectors are trimmed. |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 42 | 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); |
| 49 | end |
| 50 | |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 51 | %% Unwrap SeqNo and TimeStamp. |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 52 | SeqNoUW = maxUnwrap(SeqNo, 65535); |
| 53 | TimeStampUW = maxUnwrap(TimeStamp, 4294967295); |
| 54 | |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 55 | %% Generate some stats for the stream. |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 56 | fprintf('Statistics:\n'); |
| 57 | fprintf('SSRC: %s\n', SSRC{1}); |
| 58 | uPT = unique(PT); |
| 59 | if length(uPT) > 1 |
| 60 | warning('This tool cannot yet handle changes in codec sample rate'); |
| 61 | end |
| 62 | fprintf('Payload type(s): %i', uPT(1)); |
| 63 | if length(uPT) > 1 |
| 64 | fprintf(', %i', uPT(2:end)); |
| 65 | end |
| 66 | fprintf('\n'); |
| 67 | fprintf('Packets: %i\n', length(SeqNo)); |
| 68 | fprintf('Missing sequence numbers: %i\n', ... |
| 69 | length(find(diff(sort(SeqNoUW)) > 1))); |
| 70 | fprintf('Reordered packets: %i\n', length(find(diff(sort(SeqNoUW)) < 1))); |
| 71 | tsdiff = diff(TimeStampUW); |
| 72 | tsdiff = tsdiff(diff(SeqNoUW) == 1); |
| 73 | [utsdiff, ~, ixtsdiff] = unique(tsdiff); |
| 74 | fprintf('Common packet sizes:\n'); |
| 75 | for i = 1:length(utsdiff) |
| 76 | fprintf(' %i samples (%i%%)\n', ... |
| 77 | utsdiff(i), ... |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 78 | round(100 * length(find(ixtsdiff == i))/length(ixtsdiff))); |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 79 | end |
| 80 | |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 81 | %% Trying to figure out sample rate. |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 82 | fs_est = (TimeStampUW(end) - TimeStampUW(1)) / (ArrTime(end) - ArrTime(1)); |
| 83 | fs_vec = [8, 16, 32, 48]; |
| 84 | fs = 0; |
| 85 | for f = fs_vec |
| 86 | if abs((fs_est-f)/f) < 0.05 % 5% margin |
| 87 | fs = f; |
| 88 | break; |
| 89 | end |
| 90 | end |
| 91 | if 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): '); |
| 95 | else |
| 96 | fprintf('Sample rate estimated to %i kHz\n', fs); |
| 97 | end |
| 98 | |
| 99 | SendTimeMs = (TimeStampUW - TimeStampUW(1)) / fs; |
| 100 | |
| 101 | fprintf('Stream duration at sender: %.1f seconds\n', ... |
| 102 | (SendTimeMs(end) - SendTimeMs(1)) / 1000); |
| 103 | |
| 104 | fprintf('Stream duration at receiver: %.1f seconds\n', ... |
| 105 | (ArrTime(end) - ArrTime(1)) / 1000); |
| 106 | |
| 107 | fprintf('Clock drift: %.2f%%\n', ... |
| 108 | 100 * ((ArrTime(end) - ArrTime(1)) / ... |
| 109 | (SendTimeMs(end) - SendTimeMs(1)) - 1)); |
| 110 | |
| 111 | fprintf('Sent average bitrate: %i kbps\n', ... |
| 112 | round(sum(Size) * 8 / (SendTimeMs(end)-SendTimeMs(1)))); |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 113 | |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 114 | fprintf('Received average bitrate: %i kbps\n', ... |
| 115 | round(sum(Size) * 8 / (ArrTime(end)-ArrTime(1)))); |
| 116 | |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 117 | %% Plots. |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 118 | delay = ArrTime - SendTimeMs; |
| 119 | delay = delay - min(delay); |
| 120 | figure |
| 121 | plot(SendTimeMs / 1000, delay); |
| 122 | xlabel('Send time [s]'); |
| 123 | ylabel('Relative transport delay [ms]'); |
| 124 | title(sprintf('SSRC: %s', SSRC{1})); |
| 125 | |
| 126 | SendBitrateKbps = 8 * Size(1:end-1) ./ diff(SendTimeMs); |
| 127 | figure |
| 128 | plot(SendTimeMs(1:end-1)/1000, SendBitrateKbps); |
| 129 | xlabel('Send time [s]'); |
| 130 | ylabel('Send bitrate [kbps]'); |
| 131 | end |
| 132 | |
Henrik Lundin | 60508f8 | 2015-06-03 09:38:23 +0200 | [diff] [blame^] | 133 | %% Auto-generated subfunction. |
Henrik Lundin | 8051832 | 2015-05-28 12:37:46 +0200 | [diff] [blame] | 134 | function [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. |
| 153 | if nargin<=2 |
| 154 | startRow = 2; |
| 155 | endRow = inf; |
| 156 | end |
| 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. |
| 167 | formatSpec = '%5f%11f%11f%6f%6f%3f%s%[^\n\r]'; |
| 168 | |
| 169 | %% Open the text file. |
| 170 | fileID = 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. |
| 176 | dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, ... |
| 177 | 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow(1)-1, ... |
| 178 | 'ReturnOnError', false); |
| 179 | for 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 |
| 187 | end |
| 188 | |
| 189 | %% Close the text file. |
| 190 | fclose(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 |
| 199 | SeqNo = dataArray{:, 1}; |
| 200 | TimeStamp = dataArray{:, 2}; |
| 201 | SendTime = dataArray{:, 3}; |
| 202 | Size = dataArray{:, 4}; |
| 203 | PT = dataArray{:, 5}; |
| 204 | M = dataArray{:, 6}; |
| 205 | SSRC = dataArray{:, 7}; |
| 206 | end |
| 207 | |