blob: 26151bba1ff6547fea7c3a1d6625c82ad3c3ef09 [file] [log] [blame]
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +00001/*
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +00002 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +000011#include "webrtc/test/rtp_file_reader.h"
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdio.h>
14
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +000015#include <map>
16#include <string>
17#include <vector>
18
henrik.lundin@webrtc.org38c121c2014-09-30 11:08:44 +000019#include "webrtc/base/checks.h"
pkasting@chromium.orgd3245462015-02-23 21:28:22 +000020#include "webrtc/base/format_macros.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000021#include "webrtc/base/scoped_ptr.h"
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +000022#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +000023
24namespace webrtc {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +000025namespace test {
26
27static const size_t kFirstLineLength = 40;
28static uint16_t kPacketHeaderSize = 8;
29
30#if 1
31# define DEBUG_LOG(text)
32# define DEBUG_LOG1(text, arg)
33#else
34# define DEBUG_LOG(text) (printf(text "\n"))
35# define DEBUG_LOG1(text, arg) (printf(text "\n", arg))
36#endif
37
38#define TRY(expr) \
39 do { \
40 if (!(expr)) { \
41 DEBUG_LOG1("FAIL at " __FILE__ ":%d", __LINE__); \
42 return false; \
43 } \
44 } while (0)
45
stefan@webrtc.org48ac2262015-03-02 16:18:56 +000046bool ReadUint32(uint32_t* out, FILE* file) {
47 *out = 0;
48 for (size_t i = 0; i < 4; ++i) {
49 *out <<= 8;
50 uint8_t tmp;
51 if (fread(&tmp, 1, sizeof(uint8_t), file) != sizeof(uint8_t))
52 return false;
53 *out |= tmp;
54 }
55 return true;
56}
57
58bool ReadUint16(uint16_t* out, FILE* file) {
59 *out = 0;
60 for (size_t i = 0; i < 2; ++i) {
61 *out <<= 8;
62 uint8_t tmp;
63 if (fread(&tmp, 1, sizeof(uint8_t), file) != sizeof(uint8_t))
64 return false;
65 *out |= tmp;
66 }
67 return true;
68}
69
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +000070class RtpFileReaderImpl : public RtpFileReader {
71 public:
72 virtual bool Init(const std::string& filename) = 0;
73};
74
stefan@webrtc.org48ac2262015-03-02 16:18:56 +000075class InterleavedRtpFileReader : public RtpFileReaderImpl {
76 public:
77 virtual ~InterleavedRtpFileReader() {
78 if (file_ != NULL) {
79 fclose(file_);
80 file_ = NULL;
81 }
82 }
83
84 virtual bool Init(const std::string& filename) {
85 file_ = fopen(filename.c_str(), "rb");
86 if (file_ == NULL) {
87 printf("ERROR: Can't open file: %s\n", filename.c_str());
88 return false;
89 }
90 return true;
91 }
92 virtual bool NextPacket(RtpPacket* packet) {
93 assert(file_ != NULL);
94 packet->length = RtpPacket::kMaxPacketBufferSize;
95 uint32_t len = 0;
96 TRY(ReadUint32(&len, file_));
97 if (packet->length < len) {
98 FATAL() << "Packet is too large to fit: " << len << " bytes vs "
99 << packet->length
100 << " bytes allocated. Consider increasing the buffer "
101 "size";
102 }
103 if (fread(packet->data, 1, len, file_) != len)
104 return false;
105
106 packet->length = len;
107 packet->original_length = len;
108 packet->time_ms = time_ms_;
109 time_ms_ += 5;
110 return true;
111 }
112
113 private:
114 FILE* file_ = NULL;
115 int64_t time_ms_ = 0;
116};
117
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000118// Read RTP packets from file in rtpdump format, as documented at:
119// http://www.cs.columbia.edu/irt/software/rtptools/
120class RtpDumpReader : public RtpFileReaderImpl {
121 public:
122 RtpDumpReader() : file_(NULL) {}
123 virtual ~RtpDumpReader() {
124 if (file_ != NULL) {
125 fclose(file_);
126 file_ = NULL;
127 }
128 }
129
130 bool Init(const std::string& filename) {
131 file_ = fopen(filename.c_str(), "rb");
132 if (file_ == NULL) {
133 printf("ERROR: Can't open file: %s\n", filename.c_str());
134 return false;
135 }
136
137 char firstline[kFirstLineLength + 1] = {0};
138 if (fgets(firstline, kFirstLineLength, file_) == NULL) {
139 DEBUG_LOG("ERROR: Can't read from file\n");
140 return false;
141 }
142 if (strncmp(firstline, "#!rtpplay", 9) == 0) {
143 if (strncmp(firstline, "#!rtpplay1.0", 12) != 0) {
144 DEBUG_LOG("ERROR: wrong rtpplay version, must be 1.0\n");
145 return false;
146 }
147 } else if (strncmp(firstline, "#!RTPencode", 11) == 0) {
148 if (strncmp(firstline, "#!RTPencode1.0", 14) != 0) {
149 DEBUG_LOG("ERROR: wrong RTPencode version, must be 1.0\n");
150 return false;
151 }
152 } else {
153 DEBUG_LOG("ERROR: wrong file format of input file\n");
154 return false;
155 }
156
157 uint32_t start_sec;
158 uint32_t start_usec;
159 uint32_t source;
160 uint16_t port;
161 uint16_t padding;
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000162 TRY(ReadUint32(&start_sec, file_));
163 TRY(ReadUint32(&start_usec, file_));
164 TRY(ReadUint32(&source, file_));
165 TRY(ReadUint16(&port, file_));
166 TRY(ReadUint16(&padding, file_));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000167
168 return true;
169 }
170
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000171 bool NextPacket(RtpPacket* packet) override {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000172 uint8_t* rtp_data = packet->data;
henrik.lundin@webrtc.org91d928e2014-11-26 15:50:30 +0000173 packet->length = RtpPacket::kMaxPacketBufferSize;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000174
175 uint16_t len;
176 uint16_t plen;
177 uint32_t offset;
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000178 TRY(ReadUint16(&len, file_));
179 TRY(ReadUint16(&plen, file_));
180 TRY(ReadUint32(&offset, file_));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000181
182 // Use 'len' here because a 'plen' of 0 specifies rtcp.
183 len -= kPacketHeaderSize;
184 if (packet->length < len) {
henrik.lundin@webrtc.org38c121c2014-09-30 11:08:44 +0000185 FATAL() << "Packet is too large to fit: " << len << " bytes vs "
186 << packet->length
187 << " bytes allocated. Consider increasing the buffer "
188 "size";
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000189 }
190 if (fread(rtp_data, 1, len, file_) != len) {
191 return false;
192 }
193
194 packet->length = len;
henrik.lundin@webrtc.org38c121c2014-09-30 11:08:44 +0000195 packet->original_length = plen;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000196 packet->time_ms = offset;
197 return true;
198 }
199
200 private:
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000201 FILE* file_;
202
203 DISALLOW_COPY_AND_ASSIGN(RtpDumpReader);
204};
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000205
206enum {
207 kResultFail = -1,
208 kResultSuccess = 0,
209 kResultSkip = 1,
210
211 kPcapVersionMajor = 2,
212 kPcapVersionMinor = 4,
213 kLinktypeNull = 0,
214 kLinktypeEthernet = 1,
215 kBsdNullLoopback1 = 0x00000002,
216 kBsdNullLoopback2 = 0x02000000,
217 kEthernetIIHeaderMacSkip = 12,
218 kEthertypeIp = 0x0800,
219 kIpVersion4 = 4,
220 kMinIpHeaderLength = 20,
221 kFragmentOffsetClear = 0x0000,
222 kFragmentOffsetDoNotFragment = 0x4000,
223 kProtocolTcp = 0x06,
224 kProtocolUdp = 0x11,
225 kUdpHeaderLength = 8,
226 kMaxReadBufferSize = 4096
227};
228
229const uint32_t kPcapBOMSwapOrder = 0xd4c3b2a1UL;
230const uint32_t kPcapBOMNoSwapOrder = 0xa1b2c3d4UL;
231
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000232#define TRY_PCAP(expr) \
233 do { \
234 int r = (expr); \
235 if (r == kResultFail) { \
236 DEBUG_LOG1("FAIL at " __FILE__ ":%d", __LINE__); \
237 return kResultFail; \
238 } else if (r == kResultSkip) { \
239 return kResultSkip; \
240 } \
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000241 } while (0)
242
243// Read RTP packets from file in tcpdump/libpcap format, as documented at:
244// http://wiki.wireshark.org/Development/LibpcapFileFormat
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000245class PcapReader : public RtpFileReaderImpl {
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000246 public:
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000247 PcapReader()
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000248 : file_(NULL),
249 swap_pcap_byte_order_(false),
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000250#ifdef WEBRTC_ARCH_BIG_ENDIAN
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000251 swap_network_byte_order_(false),
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000252#else
253 swap_network_byte_order_(true),
254#endif
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000255 read_buffer_(),
256 packets_by_ssrc_(),
257 packets_(),
258 next_packet_it_() {
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000259 }
260
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000261 virtual ~PcapReader() {
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000262 if (file_ != NULL) {
263 fclose(file_);
264 file_ = NULL;
265 }
266 }
267
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000268 bool Init(const std::string& filename) override {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000269 return Initialize(filename) == kResultSuccess;
270 }
271
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000272 int Initialize(const std::string& filename) {
273 file_ = fopen(filename.c_str(), "rb");
274 if (file_ == NULL) {
275 printf("ERROR: Can't open file: %s\n", filename.c_str());
276 return kResultFail;
277 }
278
279 if (ReadGlobalHeader() < 0) {
280 return kResultFail;
281 }
282
283 int total_packet_count = 0;
284 uint32_t stream_start_ms = 0;
285 int32_t next_packet_pos = ftell(file_);
286 for (;;) {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000287 TRY_PCAP(fseek(file_, next_packet_pos, SEEK_SET));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000288 int result = ReadPacket(&next_packet_pos, stream_start_ms,
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000289 ++total_packet_count);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000290 if (result == kResultFail) {
291 break;
292 } else if (result == kResultSuccess && packets_.size() == 1) {
293 assert(stream_start_ms == 0);
294 PacketIterator it = packets_.begin();
295 stream_start_ms = it->time_offset_ms;
296 it->time_offset_ms = 0;
297 }
298 }
299
300 if (feof(file_) == 0) {
301 printf("Failed reading file!\n");
302 return kResultFail;
303 }
304
305 printf("Total packets in file: %d\n", total_packet_count);
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000306 printf("Total RTP/RTCP packets: %" PRIuS "\n", packets_.size());
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000307
308 for (SsrcMapIterator mit = packets_by_ssrc_.begin();
309 mit != packets_by_ssrc_.end(); ++mit) {
310 uint32_t ssrc = mit->first;
311 const std::vector<uint32_t>& packet_numbers = mit->second;
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000312 uint8_t pt = packets_[packet_numbers[0]].rtp_header.payloadType;
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000313 printf("SSRC: %08x, %" PRIuS " packets, pt=%d\n", ssrc,
314 packet_numbers.size(), pt);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000315 }
316
317 // TODO(solenberg): Better validation of identified SSRC streams.
318 //
319 // Since we're dealing with raw network data here, we will wrongly identify
320 // some packets as RTP. When these packets are consumed by RtpPlayer, they
321 // are unlikely to cause issues as they will ultimately be filtered out by
322 // the RtpRtcp module. However, we should really do better filtering here,
323 // which we can accomplish in a number of ways, e.g.:
324 //
325 // - Verify that the time stamps and sequence numbers for RTP packets are
326 // both increasing/decreasing. If they move in different directions, the
327 // SSRC is likely bogus and can be dropped. (Normally they should be inc-
328 // reasing but we must allow packet reordering).
329 // - If RTP sequence number is not changing, drop the stream.
330 // - Can also use srcip:port->dstip:port pairs, assuming few SSRC collisions
331 // for up/down streams.
332
333 next_packet_it_ = packets_.begin();
334 return kResultSuccess;
335 }
336
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000337 bool NextPacket(RtpPacket* packet) override {
henrik.lundin@webrtc.org91d928e2014-11-26 15:50:30 +0000338 uint32_t length = RtpPacket::kMaxPacketBufferSize;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000339 if (NextPcap(packet->data, &length, &packet->time_ms) != kResultSuccess)
340 return false;
341 packet->length = static_cast<size_t>(length);
henrik.lundin@webrtc.org38c121c2014-09-30 11:08:44 +0000342 packet->original_length = packet->length;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000343 return true;
344 }
345
346 virtual int NextPcap(uint8_t* data, uint32_t* length, uint32_t* time_ms) {
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000347 assert(data);
348 assert(length);
349 assert(time_ms);
350
351 if (next_packet_it_ == packets_.end()) {
352 return -1;
353 }
354 if (*length < next_packet_it_->payload_length) {
355 return -1;
356 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000357 TRY_PCAP(fseek(file_, next_packet_it_->pos_in_file, SEEK_SET));
358 TRY_PCAP(Read(data, next_packet_it_->payload_length));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000359 *length = next_packet_it_->payload_length;
360 *time_ms = next_packet_it_->time_offset_ms;
361 next_packet_it_++;
362
363 return 0;
364 }
365
366 private:
367 // A marker of an RTP packet within the file.
368 struct RtpPacketMarker {
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000369 uint32_t packet_number; // One-based index (like in WireShark)
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000370 uint32_t time_offset_ms;
371 uint32_t source_ip;
372 uint32_t dest_ip;
373 uint16_t source_port;
374 uint16_t dest_port;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000375 RTPHeader rtp_header;
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000376 int32_t pos_in_file; // Byte offset of payload from start of file.
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000377 uint32_t payload_length;
378 };
379
380 typedef std::vector<RtpPacketMarker>::iterator PacketIterator;
381 typedef std::map<uint32_t, std::vector<uint32_t> > SsrcMap;
382 typedef std::map<uint32_t, std::vector<uint32_t> >::iterator SsrcMapIterator;
383
384 int ReadGlobalHeader() {
385 uint32_t magic;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000386 TRY_PCAP(Read(&magic, false));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000387 if (magic == kPcapBOMSwapOrder) {
388 swap_pcap_byte_order_ = true;
389 } else if (magic == kPcapBOMNoSwapOrder) {
390 swap_pcap_byte_order_ = false;
391 } else {
392 return kResultFail;
393 }
394
395 uint16_t version_major;
396 uint16_t version_minor;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000397 TRY_PCAP(Read(&version_major, false));
398 TRY_PCAP(Read(&version_minor, false));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000399 if (version_major != kPcapVersionMajor ||
400 version_minor != kPcapVersionMinor) {
401 return kResultFail;
402 }
403
404 int32_t this_zone; // GMT to local correction.
405 uint32_t sigfigs; // Accuracy of timestamps.
406 uint32_t snaplen; // Max length of captured packets, in octets.
407 uint32_t network; // Data link type.
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000408 TRY_PCAP(Read(&this_zone, false));
409 TRY_PCAP(Read(&sigfigs, false));
410 TRY_PCAP(Read(&snaplen, false));
411 TRY_PCAP(Read(&network, false));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000412
413 // Accept only LINKTYPE_NULL and LINKTYPE_ETHERNET.
414 // See: http://www.tcpdump.org/linktypes.html
415 if (network != kLinktypeNull && network != kLinktypeEthernet) {
416 return kResultFail;
417 }
418
419 return kResultSuccess;
420 }
421
422 int ReadPacket(int32_t* next_packet_pos, uint32_t stream_start_ms,
423 uint32_t number) {
424 assert(next_packet_pos);
425
426 uint32_t ts_sec; // Timestamp seconds.
427 uint32_t ts_usec; // Timestamp microseconds.
428 uint32_t incl_len; // Number of octets of packet saved in file.
429 uint32_t orig_len; // Actual length of packet.
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000430 TRY_PCAP(Read(&ts_sec, false));
431 TRY_PCAP(Read(&ts_usec, false));
432 TRY_PCAP(Read(&incl_len, false));
433 TRY_PCAP(Read(&orig_len, false));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000434
435 *next_packet_pos = ftell(file_) + incl_len;
436
437 RtpPacketMarker marker = {0};
438 marker.packet_number = number;
439 marker.time_offset_ms = CalcTimeDelta(ts_sec, ts_usec, stream_start_ms);
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000440 TRY_PCAP(ReadPacketHeader(&marker));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000441 marker.pos_in_file = ftell(file_);
442
443 if (marker.payload_length > sizeof(read_buffer_)) {
444 printf("Packet too large!\n");
445 return kResultFail;
446 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000447 TRY_PCAP(Read(read_buffer_, marker.payload_length));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000448
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000449 RtpUtility::RtpHeaderParser rtp_parser(read_buffer_, marker.payload_length);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000450 if (rtp_parser.RTCP()) {
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000451 rtp_parser.ParseRtcp(&marker.rtp_header);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000452 packets_.push_back(marker);
453 } else {
454 if (!rtp_parser.Parse(marker.rtp_header, NULL)) {
455 DEBUG_LOG("Not recognized as RTP/RTCP");
456 return kResultSkip;
457 }
458
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000459 uint32_t ssrc = marker.rtp_header.ssrc;
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000460 packets_by_ssrc_[ssrc].push_back(marker.packet_number);
461 packets_.push_back(marker);
462 }
463
464 return kResultSuccess;
465 }
466
467 int ReadPacketHeader(RtpPacketMarker* marker) {
468 int32_t file_pos = ftell(file_);
469
470 // Check for BSD null/loopback frame header. The header is just 4 bytes in
471 // native byte order, so we check for both versions as we don't care about
472 // the header as such and will likely fail reading the IP header if this is
473 // something else than null/loopback.
474 uint32_t protocol;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000475 TRY_PCAP(Read(&protocol, true));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000476 if (protocol == kBsdNullLoopback1 || protocol == kBsdNullLoopback2) {
477 int result = ReadXxpIpHeader(marker);
478 DEBUG_LOG("Recognized loopback frame");
479 if (result != kResultSkip) {
480 return result;
481 }
482 }
483
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000484 TRY_PCAP(fseek(file_, file_pos, SEEK_SET));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000485
486 // Check for Ethernet II, IP frame header.
487 uint16_t type;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000488 TRY_PCAP(Skip(kEthernetIIHeaderMacSkip)); // Source+destination MAC.
489 TRY_PCAP(Read(&type, true));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000490 if (type == kEthertypeIp) {
491 int result = ReadXxpIpHeader(marker);
492 DEBUG_LOG("Recognized ethernet 2 frame");
493 if (result != kResultSkip) {
494 return result;
495 }
496 }
497
498 return kResultSkip;
499 }
500
501 uint32_t CalcTimeDelta(uint32_t ts_sec, uint32_t ts_usec, uint32_t start_ms) {
502 // Round to nearest ms.
503 uint64_t t2_ms = ((static_cast<uint64_t>(ts_sec) * 1000000) + ts_usec +
504 500) / 1000;
505 uint64_t t1_ms = static_cast<uint64_t>(start_ms);
506 if (t2_ms < t1_ms) {
507 return 0;
508 } else {
509 return t2_ms - t1_ms;
510 }
511 }
512
513 int ReadXxpIpHeader(RtpPacketMarker* marker) {
514 assert(marker);
515
516 uint16_t version;
517 uint16_t length;
518 uint16_t id;
519 uint16_t fragment;
520 uint16_t protocol;
521 uint16_t checksum;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000522 TRY_PCAP(Read(&version, true));
523 TRY_PCAP(Read(&length, true));
524 TRY_PCAP(Read(&id, true));
525 TRY_PCAP(Read(&fragment, true));
526 TRY_PCAP(Read(&protocol, true));
527 TRY_PCAP(Read(&checksum, true));
528 TRY_PCAP(Read(&marker->source_ip, true));
529 TRY_PCAP(Read(&marker->dest_ip, true));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000530
531 if (((version >> 12) & 0x000f) != kIpVersion4) {
532 DEBUG_LOG("IP header is not IPv4");
533 return kResultSkip;
534 }
535
536 if (fragment != kFragmentOffsetClear &&
537 fragment != kFragmentOffsetDoNotFragment) {
538 DEBUG_LOG("IP fragments cannot be handled");
539 return kResultSkip;
540 }
541
542 // Skip remaining fields of IP header.
543 uint16_t header_length = (version & 0x0f00) >> (8 - 2);
544 assert(header_length >= kMinIpHeaderLength);
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000545 TRY_PCAP(Skip(header_length - kMinIpHeaderLength));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000546
547 protocol = protocol & 0x00ff;
548 if (protocol == kProtocolTcp) {
549 DEBUG_LOG("TCP packets are not handled");
550 return kResultSkip;
551 } else if (protocol == kProtocolUdp) {
552 uint16_t length;
553 uint16_t checksum;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000554 TRY_PCAP(Read(&marker->source_port, true));
555 TRY_PCAP(Read(&marker->dest_port, true));
556 TRY_PCAP(Read(&length, true));
557 TRY_PCAP(Read(&checksum, true));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000558 marker->payload_length = length - kUdpHeaderLength;
559 } else {
560 DEBUG_LOG("Unknown transport (expected UDP or TCP)");
561 return kResultSkip;
562 }
563
564 return kResultSuccess;
565 }
566
567 int Read(uint32_t* out, bool expect_network_order) {
568 uint32_t tmp = 0;
569 if (fread(&tmp, 1, sizeof(uint32_t), file_) != sizeof(uint32_t)) {
570 return kResultFail;
571 }
572 if ((!expect_network_order && swap_pcap_byte_order_) ||
573 (expect_network_order && swap_network_byte_order_)) {
574 tmp = ((tmp >> 24) & 0x000000ff) | (tmp << 24) |
575 ((tmp >> 8) & 0x0000ff00) | ((tmp << 8) & 0x00ff0000);
576 }
577 *out = tmp;
578 return kResultSuccess;
579 }
580
581 int Read(uint16_t* out, bool expect_network_order) {
582 uint16_t tmp = 0;
583 if (fread(&tmp, 1, sizeof(uint16_t), file_) != sizeof(uint16_t)) {
584 return kResultFail;
585 }
586 if ((!expect_network_order && swap_pcap_byte_order_) ||
587 (expect_network_order && swap_network_byte_order_)) {
588 tmp = ((tmp >> 8) & 0x00ff) | (tmp << 8);
589 }
590 *out = tmp;
591 return kResultSuccess;
592 }
593
594 int Read(uint8_t* out, uint32_t count) {
595 if (fread(out, 1, count, file_) != count) {
596 return kResultFail;
597 }
598 return kResultSuccess;
599 }
600
601 int Read(int32_t* out, bool expect_network_order) {
602 int32_t tmp = 0;
603 if (fread(&tmp, 1, sizeof(uint32_t), file_) != sizeof(uint32_t)) {
604 return kResultFail;
605 }
606 if ((!expect_network_order && swap_pcap_byte_order_) ||
607 (expect_network_order && swap_network_byte_order_)) {
608 tmp = ((tmp >> 24) & 0x000000ff) | (tmp << 24) |
609 ((tmp >> 8) & 0x0000ff00) | ((tmp << 8) & 0x00ff0000);
610 }
611 *out = tmp;
612 return kResultSuccess;
613 }
614
615 int Skip(uint32_t length) {
616 if (fseek(file_, length, SEEK_CUR) != 0) {
617 return kResultFail;
618 }
619 return kResultSuccess;
620 }
621
622 FILE* file_;
623 bool swap_pcap_byte_order_;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000624 const bool swap_network_byte_order_;
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000625 uint8_t read_buffer_[kMaxReadBufferSize];
626
627 SsrcMap packets_by_ssrc_;
628 std::vector<RtpPacketMarker> packets_;
629 PacketIterator next_packet_it_;
630
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000631 DISALLOW_COPY_AND_ASSIGN(PcapReader);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000632};
633
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000634RtpFileReader* RtpFileReader::Create(FileFormat format,
635 const std::string& filename) {
636 RtpFileReaderImpl* reader = NULL;
637 switch (format) {
638 case kPcap:
639 reader = new PcapReader();
640 break;
641 case kRtpDump:
642 reader = new RtpDumpReader();
643 break;
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000644 case kLengthPacketInterleaved:
645 reader = new InterleavedRtpFileReader();
646 break;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000647 }
648 if (!reader->Init(filename)) {
649 delete reader;
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000650 return NULL;
651 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000652 return reader;
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000653}
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000654
655} // namespace test
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000656} // namespace webrtc