blob: c5d245b1150cab9c41df968c07d82e3202b55874 [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
46class RtpFileReaderImpl : public RtpFileReader {
47 public:
48 virtual bool Init(const std::string& filename) = 0;
49};
50
51// Read RTP packets from file in rtpdump format, as documented at:
52// http://www.cs.columbia.edu/irt/software/rtptools/
53class RtpDumpReader : public RtpFileReaderImpl {
54 public:
55 RtpDumpReader() : file_(NULL) {}
56 virtual ~RtpDumpReader() {
57 if (file_ != NULL) {
58 fclose(file_);
59 file_ = NULL;
60 }
61 }
62
63 bool Init(const std::string& filename) {
64 file_ = fopen(filename.c_str(), "rb");
65 if (file_ == NULL) {
66 printf("ERROR: Can't open file: %s\n", filename.c_str());
67 return false;
68 }
69
70 char firstline[kFirstLineLength + 1] = {0};
71 if (fgets(firstline, kFirstLineLength, file_) == NULL) {
72 DEBUG_LOG("ERROR: Can't read from file\n");
73 return false;
74 }
75 if (strncmp(firstline, "#!rtpplay", 9) == 0) {
76 if (strncmp(firstline, "#!rtpplay1.0", 12) != 0) {
77 DEBUG_LOG("ERROR: wrong rtpplay version, must be 1.0\n");
78 return false;
79 }
80 } else if (strncmp(firstline, "#!RTPencode", 11) == 0) {
81 if (strncmp(firstline, "#!RTPencode1.0", 14) != 0) {
82 DEBUG_LOG("ERROR: wrong RTPencode version, must be 1.0\n");
83 return false;
84 }
85 } else {
86 DEBUG_LOG("ERROR: wrong file format of input file\n");
87 return false;
88 }
89
90 uint32_t start_sec;
91 uint32_t start_usec;
92 uint32_t source;
93 uint16_t port;
94 uint16_t padding;
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +000095 TRY(ReadUint32(&start_sec));
96 TRY(ReadUint32(&start_usec));
97 TRY(ReadUint32(&source));
98 TRY(ReadUint16(&port));
99 TRY(ReadUint16(&padding));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000100
101 return true;
102 }
103
henrik.lundin@webrtc.org91d928e2014-11-26 15:50:30 +0000104 virtual bool NextPacket(RtpPacket* packet) OVERRIDE {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000105 uint8_t* rtp_data = packet->data;
henrik.lundin@webrtc.org91d928e2014-11-26 15:50:30 +0000106 packet->length = RtpPacket::kMaxPacketBufferSize;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000107
108 uint16_t len;
109 uint16_t plen;
110 uint32_t offset;
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +0000111 TRY(ReadUint16(&len));
112 TRY(ReadUint16(&plen));
113 TRY(ReadUint32(&offset));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000114
115 // Use 'len' here because a 'plen' of 0 specifies rtcp.
116 len -= kPacketHeaderSize;
117 if (packet->length < len) {
henrik.lundin@webrtc.org38c121c2014-09-30 11:08:44 +0000118 FATAL() << "Packet is too large to fit: " << len << " bytes vs "
119 << packet->length
120 << " bytes allocated. Consider increasing the buffer "
121 "size";
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000122 }
123 if (fread(rtp_data, 1, len, file_) != len) {
124 return false;
125 }
126
127 packet->length = len;
henrik.lundin@webrtc.org38c121c2014-09-30 11:08:44 +0000128 packet->original_length = plen;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000129 packet->time_ms = offset;
130 return true;
131 }
132
133 private:
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +0000134 bool ReadUint32(uint32_t* out) {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000135 *out = 0;
136 for (size_t i = 0; i < 4; ++i) {
137 *out <<= 8;
138 uint8_t tmp;
139 if (fread(&tmp, 1, sizeof(uint8_t), file_) != sizeof(uint8_t))
140 return false;
141 *out |= tmp;
142 }
143 return true;
144 }
145
henrik.lundin@webrtc.org83317142014-12-01 11:25:04 +0000146 bool ReadUint16(uint16_t* out) {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000147 *out = 0;
148 for (size_t i = 0; i < 2; ++i) {
149 *out <<= 8;
150 uint8_t tmp;
151 if (fread(&tmp, 1, sizeof(uint8_t), file_) != sizeof(uint8_t))
152 return false;
153 *out |= tmp;
154 }
155 return true;
156 }
157
158 FILE* file_;
159
160 DISALLOW_COPY_AND_ASSIGN(RtpDumpReader);
161};
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000162
163enum {
164 kResultFail = -1,
165 kResultSuccess = 0,
166 kResultSkip = 1,
167
168 kPcapVersionMajor = 2,
169 kPcapVersionMinor = 4,
170 kLinktypeNull = 0,
171 kLinktypeEthernet = 1,
172 kBsdNullLoopback1 = 0x00000002,
173 kBsdNullLoopback2 = 0x02000000,
174 kEthernetIIHeaderMacSkip = 12,
175 kEthertypeIp = 0x0800,
176 kIpVersion4 = 4,
177 kMinIpHeaderLength = 20,
178 kFragmentOffsetClear = 0x0000,
179 kFragmentOffsetDoNotFragment = 0x4000,
180 kProtocolTcp = 0x06,
181 kProtocolUdp = 0x11,
182 kUdpHeaderLength = 8,
183 kMaxReadBufferSize = 4096
184};
185
186const uint32_t kPcapBOMSwapOrder = 0xd4c3b2a1UL;
187const uint32_t kPcapBOMNoSwapOrder = 0xa1b2c3d4UL;
188
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000189#define TRY_PCAP(expr) \
190 do { \
191 int r = (expr); \
192 if (r == kResultFail) { \
193 DEBUG_LOG1("FAIL at " __FILE__ ":%d", __LINE__); \
194 return kResultFail; \
195 } else if (r == kResultSkip) { \
196 return kResultSkip; \
197 } \
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000198 } while (0)
199
200// Read RTP packets from file in tcpdump/libpcap format, as documented at:
201// http://wiki.wireshark.org/Development/LibpcapFileFormat
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000202class PcapReader : public RtpFileReaderImpl {
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000203 public:
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000204 PcapReader()
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000205 : file_(NULL),
206 swap_pcap_byte_order_(false),
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000207#ifdef WEBRTC_ARCH_BIG_ENDIAN
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000208 swap_network_byte_order_(false),
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000209#else
210 swap_network_byte_order_(true),
211#endif
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000212 read_buffer_(),
213 packets_by_ssrc_(),
214 packets_(),
215 next_packet_it_() {
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000216 }
217
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000218 virtual ~PcapReader() {
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000219 if (file_ != NULL) {
220 fclose(file_);
221 file_ = NULL;
222 }
223 }
224
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000225 bool Init(const std::string& filename) OVERRIDE {
226 return Initialize(filename) == kResultSuccess;
227 }
228
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000229 int Initialize(const std::string& filename) {
230 file_ = fopen(filename.c_str(), "rb");
231 if (file_ == NULL) {
232 printf("ERROR: Can't open file: %s\n", filename.c_str());
233 return kResultFail;
234 }
235
236 if (ReadGlobalHeader() < 0) {
237 return kResultFail;
238 }
239
240 int total_packet_count = 0;
241 uint32_t stream_start_ms = 0;
242 int32_t next_packet_pos = ftell(file_);
243 for (;;) {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000244 TRY_PCAP(fseek(file_, next_packet_pos, SEEK_SET));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000245 int result = ReadPacket(&next_packet_pos, stream_start_ms,
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000246 ++total_packet_count);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000247 if (result == kResultFail) {
248 break;
249 } else if (result == kResultSuccess && packets_.size() == 1) {
250 assert(stream_start_ms == 0);
251 PacketIterator it = packets_.begin();
252 stream_start_ms = it->time_offset_ms;
253 it->time_offset_ms = 0;
254 }
255 }
256
257 if (feof(file_) == 0) {
258 printf("Failed reading file!\n");
259 return kResultFail;
260 }
261
262 printf("Total packets in file: %d\n", total_packet_count);
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000263 printf("Total RTP/RTCP packets: %" PRIuS "\n", packets_.size());
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000264
265 for (SsrcMapIterator mit = packets_by_ssrc_.begin();
266 mit != packets_by_ssrc_.end(); ++mit) {
267 uint32_t ssrc = mit->first;
268 const std::vector<uint32_t>& packet_numbers = mit->second;
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000269 uint8_t pt = packets_[packet_numbers[0]].rtp_header.payloadType;
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000270 printf("SSRC: %08x, %" PRIuS " packets, pt=%d\n", ssrc,
271 packet_numbers.size(), pt);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000272 }
273
274 // TODO(solenberg): Better validation of identified SSRC streams.
275 //
276 // Since we're dealing with raw network data here, we will wrongly identify
277 // some packets as RTP. When these packets are consumed by RtpPlayer, they
278 // are unlikely to cause issues as they will ultimately be filtered out by
279 // the RtpRtcp module. However, we should really do better filtering here,
280 // which we can accomplish in a number of ways, e.g.:
281 //
282 // - Verify that the time stamps and sequence numbers for RTP packets are
283 // both increasing/decreasing. If they move in different directions, the
284 // SSRC is likely bogus and can be dropped. (Normally they should be inc-
285 // reasing but we must allow packet reordering).
286 // - If RTP sequence number is not changing, drop the stream.
287 // - Can also use srcip:port->dstip:port pairs, assuming few SSRC collisions
288 // for up/down streams.
289
290 next_packet_it_ = packets_.begin();
291 return kResultSuccess;
292 }
293
henrik.lundin@webrtc.org91d928e2014-11-26 15:50:30 +0000294 virtual bool NextPacket(RtpPacket* packet) OVERRIDE {
295 uint32_t length = RtpPacket::kMaxPacketBufferSize;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000296 if (NextPcap(packet->data, &length, &packet->time_ms) != kResultSuccess)
297 return false;
298 packet->length = static_cast<size_t>(length);
henrik.lundin@webrtc.org38c121c2014-09-30 11:08:44 +0000299 packet->original_length = packet->length;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000300 return true;
301 }
302
303 virtual int NextPcap(uint8_t* data, uint32_t* length, uint32_t* time_ms) {
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000304 assert(data);
305 assert(length);
306 assert(time_ms);
307
308 if (next_packet_it_ == packets_.end()) {
309 return -1;
310 }
311 if (*length < next_packet_it_->payload_length) {
312 return -1;
313 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000314 TRY_PCAP(fseek(file_, next_packet_it_->pos_in_file, SEEK_SET));
315 TRY_PCAP(Read(data, next_packet_it_->payload_length));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000316 *length = next_packet_it_->payload_length;
317 *time_ms = next_packet_it_->time_offset_ms;
318 next_packet_it_++;
319
320 return 0;
321 }
322
323 private:
324 // A marker of an RTP packet within the file.
325 struct RtpPacketMarker {
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000326 uint32_t packet_number; // One-based index (like in WireShark)
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000327 uint32_t time_offset_ms;
328 uint32_t source_ip;
329 uint32_t dest_ip;
330 uint16_t source_port;
331 uint16_t dest_port;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000332 RTPHeader rtp_header;
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000333 int32_t pos_in_file; // Byte offset of payload from start of file.
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000334 uint32_t payload_length;
335 };
336
337 typedef std::vector<RtpPacketMarker>::iterator PacketIterator;
338 typedef std::map<uint32_t, std::vector<uint32_t> > SsrcMap;
339 typedef std::map<uint32_t, std::vector<uint32_t> >::iterator SsrcMapIterator;
340
341 int ReadGlobalHeader() {
342 uint32_t magic;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000343 TRY_PCAP(Read(&magic, false));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000344 if (magic == kPcapBOMSwapOrder) {
345 swap_pcap_byte_order_ = true;
346 } else if (magic == kPcapBOMNoSwapOrder) {
347 swap_pcap_byte_order_ = false;
348 } else {
349 return kResultFail;
350 }
351
352 uint16_t version_major;
353 uint16_t version_minor;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000354 TRY_PCAP(Read(&version_major, false));
355 TRY_PCAP(Read(&version_minor, false));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000356 if (version_major != kPcapVersionMajor ||
357 version_minor != kPcapVersionMinor) {
358 return kResultFail;
359 }
360
361 int32_t this_zone; // GMT to local correction.
362 uint32_t sigfigs; // Accuracy of timestamps.
363 uint32_t snaplen; // Max length of captured packets, in octets.
364 uint32_t network; // Data link type.
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000365 TRY_PCAP(Read(&this_zone, false));
366 TRY_PCAP(Read(&sigfigs, false));
367 TRY_PCAP(Read(&snaplen, false));
368 TRY_PCAP(Read(&network, false));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000369
370 // Accept only LINKTYPE_NULL and LINKTYPE_ETHERNET.
371 // See: http://www.tcpdump.org/linktypes.html
372 if (network != kLinktypeNull && network != kLinktypeEthernet) {
373 return kResultFail;
374 }
375
376 return kResultSuccess;
377 }
378
379 int ReadPacket(int32_t* next_packet_pos, uint32_t stream_start_ms,
380 uint32_t number) {
381 assert(next_packet_pos);
382
383 uint32_t ts_sec; // Timestamp seconds.
384 uint32_t ts_usec; // Timestamp microseconds.
385 uint32_t incl_len; // Number of octets of packet saved in file.
386 uint32_t orig_len; // Actual length of packet.
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000387 TRY_PCAP(Read(&ts_sec, false));
388 TRY_PCAP(Read(&ts_usec, false));
389 TRY_PCAP(Read(&incl_len, false));
390 TRY_PCAP(Read(&orig_len, false));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000391
392 *next_packet_pos = ftell(file_) + incl_len;
393
394 RtpPacketMarker marker = {0};
395 marker.packet_number = number;
396 marker.time_offset_ms = CalcTimeDelta(ts_sec, ts_usec, stream_start_ms);
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000397 TRY_PCAP(ReadPacketHeader(&marker));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000398 marker.pos_in_file = ftell(file_);
399
400 if (marker.payload_length > sizeof(read_buffer_)) {
401 printf("Packet too large!\n");
402 return kResultFail;
403 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000404 TRY_PCAP(Read(read_buffer_, marker.payload_length));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000405
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000406 RtpUtility::RtpHeaderParser rtp_parser(read_buffer_, marker.payload_length);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000407 if (rtp_parser.RTCP()) {
solenberg@webrtc.orga5fd2f12013-06-26 08:36:07 +0000408 rtp_parser.ParseRtcp(&marker.rtp_header);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000409 packets_.push_back(marker);
410 } else {
411 if (!rtp_parser.Parse(marker.rtp_header, NULL)) {
412 DEBUG_LOG("Not recognized as RTP/RTCP");
413 return kResultSkip;
414 }
415
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000416 uint32_t ssrc = marker.rtp_header.ssrc;
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000417 packets_by_ssrc_[ssrc].push_back(marker.packet_number);
418 packets_.push_back(marker);
419 }
420
421 return kResultSuccess;
422 }
423
424 int ReadPacketHeader(RtpPacketMarker* marker) {
425 int32_t file_pos = ftell(file_);
426
427 // Check for BSD null/loopback frame header. The header is just 4 bytes in
428 // native byte order, so we check for both versions as we don't care about
429 // the header as such and will likely fail reading the IP header if this is
430 // something else than null/loopback.
431 uint32_t protocol;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000432 TRY_PCAP(Read(&protocol, true));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000433 if (protocol == kBsdNullLoopback1 || protocol == kBsdNullLoopback2) {
434 int result = ReadXxpIpHeader(marker);
435 DEBUG_LOG("Recognized loopback frame");
436 if (result != kResultSkip) {
437 return result;
438 }
439 }
440
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000441 TRY_PCAP(fseek(file_, file_pos, SEEK_SET));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000442
443 // Check for Ethernet II, IP frame header.
444 uint16_t type;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000445 TRY_PCAP(Skip(kEthernetIIHeaderMacSkip)); // Source+destination MAC.
446 TRY_PCAP(Read(&type, true));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000447 if (type == kEthertypeIp) {
448 int result = ReadXxpIpHeader(marker);
449 DEBUG_LOG("Recognized ethernet 2 frame");
450 if (result != kResultSkip) {
451 return result;
452 }
453 }
454
455 return kResultSkip;
456 }
457
458 uint32_t CalcTimeDelta(uint32_t ts_sec, uint32_t ts_usec, uint32_t start_ms) {
459 // Round to nearest ms.
460 uint64_t t2_ms = ((static_cast<uint64_t>(ts_sec) * 1000000) + ts_usec +
461 500) / 1000;
462 uint64_t t1_ms = static_cast<uint64_t>(start_ms);
463 if (t2_ms < t1_ms) {
464 return 0;
465 } else {
466 return t2_ms - t1_ms;
467 }
468 }
469
470 int ReadXxpIpHeader(RtpPacketMarker* marker) {
471 assert(marker);
472
473 uint16_t version;
474 uint16_t length;
475 uint16_t id;
476 uint16_t fragment;
477 uint16_t protocol;
478 uint16_t checksum;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000479 TRY_PCAP(Read(&version, true));
480 TRY_PCAP(Read(&length, true));
481 TRY_PCAP(Read(&id, true));
482 TRY_PCAP(Read(&fragment, true));
483 TRY_PCAP(Read(&protocol, true));
484 TRY_PCAP(Read(&checksum, true));
485 TRY_PCAP(Read(&marker->source_ip, true));
486 TRY_PCAP(Read(&marker->dest_ip, true));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000487
488 if (((version >> 12) & 0x000f) != kIpVersion4) {
489 DEBUG_LOG("IP header is not IPv4");
490 return kResultSkip;
491 }
492
493 if (fragment != kFragmentOffsetClear &&
494 fragment != kFragmentOffsetDoNotFragment) {
495 DEBUG_LOG("IP fragments cannot be handled");
496 return kResultSkip;
497 }
498
499 // Skip remaining fields of IP header.
500 uint16_t header_length = (version & 0x0f00) >> (8 - 2);
501 assert(header_length >= kMinIpHeaderLength);
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000502 TRY_PCAP(Skip(header_length - kMinIpHeaderLength));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000503
504 protocol = protocol & 0x00ff;
505 if (protocol == kProtocolTcp) {
506 DEBUG_LOG("TCP packets are not handled");
507 return kResultSkip;
508 } else if (protocol == kProtocolUdp) {
509 uint16_t length;
510 uint16_t checksum;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000511 TRY_PCAP(Read(&marker->source_port, true));
512 TRY_PCAP(Read(&marker->dest_port, true));
513 TRY_PCAP(Read(&length, true));
514 TRY_PCAP(Read(&checksum, true));
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000515 marker->payload_length = length - kUdpHeaderLength;
516 } else {
517 DEBUG_LOG("Unknown transport (expected UDP or TCP)");
518 return kResultSkip;
519 }
520
521 return kResultSuccess;
522 }
523
524 int Read(uint32_t* out, bool expect_network_order) {
525 uint32_t tmp = 0;
526 if (fread(&tmp, 1, sizeof(uint32_t), file_) != sizeof(uint32_t)) {
527 return kResultFail;
528 }
529 if ((!expect_network_order && swap_pcap_byte_order_) ||
530 (expect_network_order && swap_network_byte_order_)) {
531 tmp = ((tmp >> 24) & 0x000000ff) | (tmp << 24) |
532 ((tmp >> 8) & 0x0000ff00) | ((tmp << 8) & 0x00ff0000);
533 }
534 *out = tmp;
535 return kResultSuccess;
536 }
537
538 int Read(uint16_t* out, bool expect_network_order) {
539 uint16_t tmp = 0;
540 if (fread(&tmp, 1, sizeof(uint16_t), file_) != sizeof(uint16_t)) {
541 return kResultFail;
542 }
543 if ((!expect_network_order && swap_pcap_byte_order_) ||
544 (expect_network_order && swap_network_byte_order_)) {
545 tmp = ((tmp >> 8) & 0x00ff) | (tmp << 8);
546 }
547 *out = tmp;
548 return kResultSuccess;
549 }
550
551 int Read(uint8_t* out, uint32_t count) {
552 if (fread(out, 1, count, file_) != count) {
553 return kResultFail;
554 }
555 return kResultSuccess;
556 }
557
558 int Read(int32_t* out, bool expect_network_order) {
559 int32_t tmp = 0;
560 if (fread(&tmp, 1, sizeof(uint32_t), file_) != sizeof(uint32_t)) {
561 return kResultFail;
562 }
563 if ((!expect_network_order && swap_pcap_byte_order_) ||
564 (expect_network_order && swap_network_byte_order_)) {
565 tmp = ((tmp >> 24) & 0x000000ff) | (tmp << 24) |
566 ((tmp >> 8) & 0x0000ff00) | ((tmp << 8) & 0x00ff0000);
567 }
568 *out = tmp;
569 return kResultSuccess;
570 }
571
572 int Skip(uint32_t length) {
573 if (fseek(file_, length, SEEK_CUR) != 0) {
574 return kResultFail;
575 }
576 return kResultSuccess;
577 }
578
579 FILE* file_;
580 bool swap_pcap_byte_order_;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000581 const bool swap_network_byte_order_;
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000582 uint8_t read_buffer_[kMaxReadBufferSize];
583
584 SsrcMap packets_by_ssrc_;
585 std::vector<RtpPacketMarker> packets_;
586 PacketIterator next_packet_it_;
587
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000588 DISALLOW_COPY_AND_ASSIGN(PcapReader);
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000589};
590
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000591RtpFileReader* RtpFileReader::Create(FileFormat format,
592 const std::string& filename) {
593 RtpFileReaderImpl* reader = NULL;
594 switch (format) {
595 case kPcap:
596 reader = new PcapReader();
597 break;
598 case kRtpDump:
599 reader = new RtpDumpReader();
600 break;
601 }
602 if (!reader->Init(filename)) {
603 delete reader;
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000604 return NULL;
605 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000606 return reader;
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000607}
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000608
609} // namespace test
solenberg@webrtc.org56b5f772013-04-16 10:31:56 +0000610} // namespace webrtc