• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
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 
11 #include "test/rtp_file_reader.h"
12 
13 #include <stdio.h>
14 
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 #include "absl/strings/string_view.h"
20 #include "modules/rtp_rtcp/source/rtp_util.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/logging.h"
23 #include "rtc_base/system/arch.h"
24 
25 namespace webrtc {
26 namespace test {
27 
28 static const size_t kFirstLineLength = 80;
29 static uint16_t kPacketHeaderSize = 8;
30 
31 #define TRY(expr)                           \
32   do {                                      \
33     if (!(expr)) {                          \
34       RTC_LOG(LS_INFO) << "Failed to read"; \
35       return false;                         \
36     }                                       \
37   } while (0)
38 
ReadUint32(uint32_t * out,FILE * file)39 bool ReadUint32(uint32_t* out, FILE* file) {
40   *out = 0;
41   for (size_t i = 0; i < 4; ++i) {
42     *out <<= 8;
43     uint8_t tmp;
44     if (fread(&tmp, 1, sizeof(uint8_t), file) != sizeof(uint8_t))
45       return false;
46     *out |= tmp;
47   }
48   return true;
49 }
50 
ReadUint16(uint16_t * out,FILE * file)51 bool ReadUint16(uint16_t* out, FILE* file) {
52   *out = 0;
53   for (size_t i = 0; i < 2; ++i) {
54     *out <<= 8;
55     uint8_t tmp;
56     if (fread(&tmp, 1, sizeof(uint8_t), file) != sizeof(uint8_t))
57       return false;
58     *out |= tmp;
59   }
60   return true;
61 }
62 
63 class RtpFileReaderImpl : public RtpFileReader {
64  public:
65   virtual bool Init(FILE* file, const std::set<uint32_t>& ssrc_filter) = 0;
66 };
67 
68 class InterleavedRtpFileReader : public RtpFileReaderImpl {
69  public:
~InterleavedRtpFileReader()70   ~InterleavedRtpFileReader() override {
71     if (file_ != nullptr) {
72       fclose(file_);
73       file_ = nullptr;
74     }
75   }
76 
Init(FILE * file,const std::set<uint32_t> & ssrc_filter)77   bool Init(FILE* file, const std::set<uint32_t>& ssrc_filter) override {
78     file_ = file;
79     return true;
80   }
81 
NextPacket(RtpPacket * packet)82   bool NextPacket(RtpPacket* packet) override {
83     RTC_DCHECK(file_);
84     packet->length = RtpPacket::kMaxPacketBufferSize;
85     uint32_t len = 0;
86     TRY(ReadUint32(&len, file_));
87     if (packet->length < len) {
88       RTC_FATAL() << "Packet is too large to fit: " << len << " bytes vs "
89                   << packet->length
90                   << " bytes allocated. Consider increasing the buffer "
91                   << "size";
92     }
93     if (fread(packet->data, 1, len, file_) != len)
94       return false;
95 
96     packet->length = len;
97     packet->original_length = len;
98     packet->time_ms = time_ms_;
99     time_ms_ += 5;
100     return true;
101   }
102 
103  private:
104   FILE* file_ = nullptr;
105   int64_t time_ms_ = 0;
106 };
107 
108 // Read RTP packets from file in rtpdump format, as documented at:
109 // http://www.cs.columbia.edu/irt/software/rtptools/
110 class RtpDumpReader : public RtpFileReaderImpl {
111  public:
RtpDumpReader()112   RtpDumpReader() : file_(nullptr) {}
~RtpDumpReader()113   ~RtpDumpReader() override {
114     if (file_ != nullptr) {
115       fclose(file_);
116       file_ = nullptr;
117     }
118   }
119 
120   RtpDumpReader(const RtpDumpReader&) = delete;
121   RtpDumpReader& operator=(const RtpDumpReader&) = delete;
122 
Init(FILE * file,const std::set<uint32_t> & ssrc_filter)123   bool Init(FILE* file, const std::set<uint32_t>& ssrc_filter) override {
124     file_ = file;
125 
126     char firstline[kFirstLineLength + 1] = {0};
127     if (fgets(firstline, kFirstLineLength, file_) == nullptr) {
128       RTC_LOG(LS_INFO) << "Can't read from file";
129       return false;
130     }
131     if (strncmp(firstline, "#!rtpplay", 9) == 0) {
132       if (strncmp(firstline, "#!rtpplay1.0", 12) != 0) {
133         RTC_LOG(LS_INFO) << "Wrong rtpplay version, must be 1.0";
134         return false;
135       }
136     } else if (strncmp(firstline, "#!RTPencode", 11) == 0) {
137       if (strncmp(firstline, "#!RTPencode1.0", 14) != 0) {
138         RTC_LOG(LS_INFO) << "Wrong RTPencode version, must be 1.0";
139         return false;
140       }
141     } else {
142       RTC_LOG(LS_INFO) << "Wrong file format of input file";
143       return false;
144     }
145 
146     uint32_t start_sec;
147     uint32_t start_usec;
148     uint32_t source;
149     uint16_t port;
150     uint16_t padding;
151     TRY(ReadUint32(&start_sec, file_));
152     TRY(ReadUint32(&start_usec, file_));
153     TRY(ReadUint32(&source, file_));
154     TRY(ReadUint16(&port, file_));
155     TRY(ReadUint16(&padding, file_));
156 
157     return true;
158   }
159 
NextPacket(RtpPacket * packet)160   bool NextPacket(RtpPacket* packet) override {
161     uint8_t* rtp_data = packet->data;
162     packet->length = RtpPacket::kMaxPacketBufferSize;
163 
164     uint16_t len;
165     uint16_t plen;
166     uint32_t offset;
167     TRY(ReadUint16(&len, file_));
168     TRY(ReadUint16(&plen, file_));
169     TRY(ReadUint32(&offset, file_));
170 
171     // Use 'len' here because a 'plen' of 0 specifies rtcp.
172     len -= kPacketHeaderSize;
173     if (packet->length < len) {
174       RTC_LOG(LS_ERROR) << "Packet is too large to fit: " << len << " bytes vs "
175                         << packet->length
176                         << " bytes allocated. Consider increasing the buffer "
177                            "size";
178       return false;
179     }
180     if (fread(rtp_data, 1, len, file_) != len) {
181       return false;
182     }
183 
184     packet->length = len;
185     packet->original_length = plen;
186     packet->time_ms = offset;
187     return true;
188   }
189 
190  private:
191   FILE* file_;
192 };
193 
194 enum {
195   kResultFail = -1,
196   kResultSuccess = 0,
197   kResultSkip = 1,
198 
199   kPcapVersionMajor = 2,
200   kPcapVersionMinor = 4,
201   kLinktypeNull = 0,
202   kLinktypeEthernet = 1,
203   kBsdNullLoopback1 = 0x00000002,
204   kBsdNullLoopback2 = 0x02000000,
205   kEthernetIIHeaderMacSkip = 12,
206   kEthertypeIp = 0x0800,
207   kIpVersion4 = 4,
208   kMinIpHeaderLength = 20,
209   kFragmentOffsetClear = 0x0000,
210   kFragmentOffsetDoNotFragment = 0x4000,
211   kProtocolTcp = 0x06,
212   kProtocolUdp = 0x11,
213   kUdpHeaderLength = 8,
214   kMaxReadBufferSize = 4096
215 };
216 
217 const uint32_t kPcapBOMSwapOrder = 0xd4c3b2a1UL;
218 const uint32_t kPcapBOMNoSwapOrder = 0xa1b2c3d4UL;
219 
220 #define TRY_PCAP(expr)                                               \
221   do {                                                               \
222     int r = (expr);                                                  \
223     if (r == kResultFail) {                                          \
224       RTC_LOG(LS_INFO) << "FAIL at " << __FILE__ << ":" << __LINE__; \
225       return kResultFail;                                            \
226     } else if (r == kResultSkip) {                                   \
227       return kResultSkip;                                            \
228     }                                                                \
229   } while (0)
230 
231 // Read RTP packets from file in tcpdump/libpcap format, as documented at:
232 // http://wiki.wireshark.org/Development/LibpcapFileFormat
233 class PcapReader : public RtpFileReaderImpl {
234  public:
PcapReader()235   PcapReader()
236       : file_(nullptr),
237         swap_pcap_byte_order_(false),
238 #ifdef WEBRTC_ARCH_BIG_ENDIAN
239         swap_network_byte_order_(false),
240 #else
241         swap_network_byte_order_(true),
242 #endif
243         read_buffer_(),
244         packets_by_ssrc_(),
245         packets_(),
246         next_packet_it_() {
247   }
248 
~PcapReader()249   ~PcapReader() override {
250     if (file_ != nullptr) {
251       fclose(file_);
252       file_ = nullptr;
253     }
254   }
255 
256   PcapReader(const PcapReader&) = delete;
257   PcapReader& operator=(const PcapReader&) = delete;
258 
Init(FILE * file,const std::set<uint32_t> & ssrc_filter)259   bool Init(FILE* file, const std::set<uint32_t>& ssrc_filter) override {
260     return Initialize(file, ssrc_filter) == kResultSuccess;
261   }
262 
Initialize(FILE * file,const std::set<uint32_t> & ssrc_filter)263   int Initialize(FILE* file, const std::set<uint32_t>& ssrc_filter) {
264     file_ = file;
265 
266     if (ReadGlobalHeader() < 0) {
267       return kResultFail;
268     }
269 
270     int total_packet_count = 0;
271     uint32_t stream_start_ms = 0;
272     int32_t next_packet_pos = ftell(file_);
273     for (;;) {
274       TRY_PCAP(fseek(file_, next_packet_pos, SEEK_SET));
275       int result = ReadPacket(&next_packet_pos, stream_start_ms,
276                               ++total_packet_count, ssrc_filter);
277       if (result == kResultFail) {
278         break;
279       } else if (result == kResultSuccess && packets_.size() == 1) {
280         RTC_DCHECK_EQ(stream_start_ms, 0);
281         PacketIterator it = packets_.begin();
282         stream_start_ms = it->time_offset_ms;
283         it->time_offset_ms = 0;
284       }
285     }
286 
287     if (feof(file_) == 0) {
288       printf("Failed reading file!\n");
289       return kResultFail;
290     }
291 
292     printf("Total packets in file: %d\n", total_packet_count);
293     printf("Total RTP/RTCP packets: %zu\n", packets_.size());
294 
295     for (SsrcMapIterator mit = packets_by_ssrc_.begin();
296          mit != packets_by_ssrc_.end(); ++mit) {
297       uint32_t ssrc = mit->first;
298       const std::vector<uint32_t>& packet_indices = mit->second;
299       int pt = packets_[packet_indices[0]].payload_type;
300       printf("SSRC: %08x, %zu packets, pt=%d\n", ssrc, packet_indices.size(),
301              pt);
302     }
303 
304     // TODO(solenberg): Better validation of identified SSRC streams.
305     //
306     // Since we're dealing with raw network data here, we will wrongly identify
307     // some packets as RTP. When these packets are consumed by RtpPlayer, they
308     // are unlikely to cause issues as they will ultimately be filtered out by
309     // the RtpRtcp module. However, we should really do better filtering here,
310     // which we can accomplish in a number of ways, e.g.:
311     //
312     // - Verify that the time stamps and sequence numbers for RTP packets are
313     //   both increasing/decreasing. If they move in different directions, the
314     //   SSRC is likely bogus and can be dropped. (Normally they should be inc-
315     //   reasing but we must allow packet reordering).
316     // - If RTP sequence number is not changing, drop the stream.
317     // - Can also use srcip:port->dstip:port pairs, assuming few SSRC collisions
318     //   for up/down streams.
319 
320     next_packet_it_ = packets_.begin();
321     return kResultSuccess;
322   }
323 
NextPacket(RtpPacket * packet)324   bool NextPacket(RtpPacket* packet) override {
325     uint32_t length = RtpPacket::kMaxPacketBufferSize;
326     if (NextPcap(packet->data, &length, &packet->time_ms) != kResultSuccess)
327       return false;
328     packet->length = static_cast<size_t>(length);
329     packet->original_length = packet->length;
330     return true;
331   }
332 
NextPcap(uint8_t * data,uint32_t * length,uint32_t * time_ms)333   virtual int NextPcap(uint8_t* data, uint32_t* length, uint32_t* time_ms) {
334     RTC_DCHECK(data);
335     RTC_DCHECK(length);
336     RTC_DCHECK(time_ms);
337 
338     if (next_packet_it_ == packets_.end()) {
339       return -1;
340     }
341     if (*length < next_packet_it_->payload_length) {
342       return -1;
343     }
344     TRY_PCAP(fseek(file_, next_packet_it_->pos_in_file, SEEK_SET));
345     TRY_PCAP(Read(data, next_packet_it_->payload_length));
346     *length = next_packet_it_->payload_length;
347     *time_ms = next_packet_it_->time_offset_ms;
348     next_packet_it_++;
349 
350     return 0;
351   }
352 
353  private:
354   // A marker of an RTP packet within the file.
355   struct RtpPacketMarker {
356     uint32_t packet_number;  // One-based index (like in WireShark)
357     uint32_t time_offset_ms;
358     uint32_t source_ip;
359     uint32_t dest_ip;
360     uint16_t source_port;
361     uint16_t dest_port;
362     // Payload type of the RTP packet,
363     // or RTCP packet type of the first RTCP packet in a compound RTCP packet.
364     int payload_type;
365     int32_t pos_in_file;  // Byte offset of payload from start of file.
366     uint32_t payload_length;
367   };
368 
369   typedef std::vector<RtpPacketMarker>::iterator PacketIterator;
370   typedef std::map<uint32_t, std::vector<uint32_t> > SsrcMap;
371   typedef std::map<uint32_t, std::vector<uint32_t> >::iterator SsrcMapIterator;
372 
ReadGlobalHeader()373   int ReadGlobalHeader() {
374     uint32_t magic;
375     TRY_PCAP(Read(&magic, false));
376     if (magic == kPcapBOMSwapOrder) {
377       swap_pcap_byte_order_ = true;
378     } else if (magic == kPcapBOMNoSwapOrder) {
379       swap_pcap_byte_order_ = false;
380     } else {
381       return kResultFail;
382     }
383 
384     uint16_t version_major;
385     uint16_t version_minor;
386     TRY_PCAP(Read(&version_major, false));
387     TRY_PCAP(Read(&version_minor, false));
388     if (version_major != kPcapVersionMajor ||
389         version_minor != kPcapVersionMinor) {
390       return kResultFail;
391     }
392 
393     int32_t this_zone;  // GMT to local correction.
394     uint32_t sigfigs;   // Accuracy of timestamps.
395     uint32_t snaplen;   // Max length of captured packets, in octets.
396     uint32_t network;   // Data link type.
397     TRY_PCAP(Read(&this_zone, false));
398     TRY_PCAP(Read(&sigfigs, false));
399     TRY_PCAP(Read(&snaplen, false));
400     TRY_PCAP(Read(&network, false));
401 
402     // Accept only LINKTYPE_NULL and LINKTYPE_ETHERNET.
403     // See: http://www.tcpdump.org/linktypes.html
404     if (network != kLinktypeNull && network != kLinktypeEthernet) {
405       return kResultFail;
406     }
407 
408     return kResultSuccess;
409   }
410 
ReadPacket(int32_t * next_packet_pos,uint32_t stream_start_ms,uint32_t number,const std::set<uint32_t> & ssrc_filter)411   int ReadPacket(int32_t* next_packet_pos,
412                  uint32_t stream_start_ms,
413                  uint32_t number,
414                  const std::set<uint32_t>& ssrc_filter) {
415     RTC_DCHECK(next_packet_pos);
416 
417     uint32_t ts_sec;    // Timestamp seconds.
418     uint32_t ts_usec;   // Timestamp microseconds.
419     uint32_t incl_len;  // Number of octets of packet saved in file.
420     uint32_t orig_len;  // Actual length of packet.
421     TRY_PCAP(Read(&ts_sec, false));
422     TRY_PCAP(Read(&ts_usec, false));
423     TRY_PCAP(Read(&incl_len, false));
424     TRY_PCAP(Read(&orig_len, false));
425 
426     *next_packet_pos = ftell(file_) + incl_len;
427 
428     RtpPacketMarker marker = {0};
429     marker.packet_number = number;
430     marker.time_offset_ms = CalcTimeDelta(ts_sec, ts_usec, stream_start_ms);
431     TRY_PCAP(ReadPacketHeader(&marker));
432     marker.pos_in_file = ftell(file_);
433 
434     if (marker.payload_length > sizeof(read_buffer_)) {
435       printf("Packet too large!\n");
436       return kResultFail;
437     }
438     TRY_PCAP(Read(read_buffer_, marker.payload_length));
439 
440     rtc::ArrayView<const uint8_t> packet(read_buffer_, marker.payload_length);
441     if (IsRtcpPacket(packet)) {
442       marker.payload_type = packet[1];
443       packets_.push_back(marker);
444     } else if (IsRtpPacket(packet)) {
445       uint32_t ssrc = ParseRtpSsrc(packet);
446       marker.payload_type = ParseRtpPayloadType(packet);
447       if (ssrc_filter.empty() || ssrc_filter.find(ssrc) != ssrc_filter.end()) {
448         packets_by_ssrc_[ssrc].push_back(
449             static_cast<uint32_t>(packets_.size()));
450         packets_.push_back(marker);
451       } else {
452         return kResultSkip;
453       }
454     } else {
455       RTC_LOG(LS_INFO) << "Not recognized as RTP/RTCP";
456       return kResultSkip;
457     }
458 
459     return kResultSuccess;
460   }
461 
ReadPacketHeader(RtpPacketMarker * marker)462   int ReadPacketHeader(RtpPacketMarker* marker) {
463     int32_t file_pos = ftell(file_);
464 
465     // Check for BSD null/loopback frame header. The header is just 4 bytes in
466     // native byte order, so we check for both versions as we don't care about
467     // the header as such and will likely fail reading the IP header if this is
468     // something else than null/loopback.
469     uint32_t protocol;
470     TRY_PCAP(Read(&protocol, true));
471     if (protocol == kBsdNullLoopback1 || protocol == kBsdNullLoopback2) {
472       int result = ReadXxpIpHeader(marker);
473       RTC_LOG(LS_INFO) << "Recognized loopback frame";
474       if (result != kResultSkip) {
475         return result;
476       }
477     }
478 
479     TRY_PCAP(fseek(file_, file_pos, SEEK_SET));
480 
481     // Check for Ethernet II, IP frame header.
482     uint16_t type;
483     TRY_PCAP(Skip(kEthernetIIHeaderMacSkip));  // Source+destination MAC.
484     TRY_PCAP(Read(&type, true));
485     if (type == kEthertypeIp) {
486       int result = ReadXxpIpHeader(marker);
487       RTC_LOG(LS_INFO) << "Recognized ethernet 2 frame";
488       if (result != kResultSkip) {
489         return result;
490       }
491     }
492 
493     return kResultSkip;
494   }
495 
CalcTimeDelta(uint32_t ts_sec,uint32_t ts_usec,uint32_t start_ms)496   uint32_t CalcTimeDelta(uint32_t ts_sec, uint32_t ts_usec, uint32_t start_ms) {
497     // Round to nearest ms.
498     uint64_t t2_ms =
499         ((static_cast<uint64_t>(ts_sec) * 1000000) + ts_usec + 500) / 1000;
500     uint64_t t1_ms = static_cast<uint64_t>(start_ms);
501     if (t2_ms < t1_ms) {
502       return 0;
503     } else {
504       return t2_ms - t1_ms;
505     }
506   }
507 
ReadXxpIpHeader(RtpPacketMarker * marker)508   int ReadXxpIpHeader(RtpPacketMarker* marker) {
509     RTC_DCHECK(marker);
510 
511     uint16_t version;
512     uint16_t length;
513     uint16_t id;
514     uint16_t fragment;
515     uint16_t protocol;
516     uint16_t checksum;
517     TRY_PCAP(Read(&version, true));
518     TRY_PCAP(Read(&length, true));
519     TRY_PCAP(Read(&id, true));
520     TRY_PCAP(Read(&fragment, true));
521     TRY_PCAP(Read(&protocol, true));
522     TRY_PCAP(Read(&checksum, true));
523     TRY_PCAP(Read(&marker->source_ip, true));
524     TRY_PCAP(Read(&marker->dest_ip, true));
525 
526     if (((version >> 12) & 0x000f) != kIpVersion4) {
527       RTC_LOG(LS_INFO) << "IP header is not IPv4";
528       return kResultSkip;
529     }
530 
531     if (fragment != kFragmentOffsetClear &&
532         fragment != kFragmentOffsetDoNotFragment) {
533       RTC_LOG(LS_INFO) << "IP fragments cannot be handled";
534       return kResultSkip;
535     }
536 
537     // Skip remaining fields of IP header.
538     uint16_t header_length = (version & 0x0f00) >> (8 - 2);
539     RTC_DCHECK_GE(header_length, kMinIpHeaderLength);
540     TRY_PCAP(Skip(header_length - kMinIpHeaderLength));
541 
542     protocol = protocol & 0x00ff;
543     if (protocol == kProtocolTcp) {
544       RTC_LOG(LS_INFO) << "TCP packets are not handled";
545       return kResultSkip;
546     } else if (protocol == kProtocolUdp) {
547       uint16_t length;
548       uint16_t checksum;
549       TRY_PCAP(Read(&marker->source_port, true));
550       TRY_PCAP(Read(&marker->dest_port, true));
551       TRY_PCAP(Read(&length, true));
552       TRY_PCAP(Read(&checksum, true));
553       marker->payload_length = length - kUdpHeaderLength;
554     } else {
555       RTC_LOG(LS_INFO) << "Unknown transport (expected UDP or TCP)";
556       return kResultSkip;
557     }
558 
559     return kResultSuccess;
560   }
561 
Read(uint32_t * out,bool expect_network_order)562   int Read(uint32_t* out, bool expect_network_order) {
563     uint32_t tmp = 0;
564     if (fread(&tmp, 1, sizeof(uint32_t), file_) != sizeof(uint32_t)) {
565       return kResultFail;
566     }
567     if ((!expect_network_order && swap_pcap_byte_order_) ||
568         (expect_network_order && swap_network_byte_order_)) {
569       tmp = ((tmp >> 24) & 0x000000ff) | (tmp << 24) |
570             ((tmp >> 8) & 0x0000ff00) | ((tmp << 8) & 0x00ff0000);
571     }
572     *out = tmp;
573     return kResultSuccess;
574   }
575 
Read(uint16_t * out,bool expect_network_order)576   int Read(uint16_t* out, bool expect_network_order) {
577     uint16_t tmp = 0;
578     if (fread(&tmp, 1, sizeof(uint16_t), file_) != sizeof(uint16_t)) {
579       return kResultFail;
580     }
581     if ((!expect_network_order && swap_pcap_byte_order_) ||
582         (expect_network_order && swap_network_byte_order_)) {
583       tmp = ((tmp >> 8) & 0x00ff) | (tmp << 8);
584     }
585     *out = tmp;
586     return kResultSuccess;
587   }
588 
Read(uint8_t * out,uint32_t count)589   int Read(uint8_t* out, uint32_t count) {
590     if (fread(out, 1, count, file_) != count) {
591       return kResultFail;
592     }
593     return kResultSuccess;
594   }
595 
Read(int32_t * out,bool expect_network_order)596   int Read(int32_t* out, bool expect_network_order) {
597     int32_t tmp = 0;
598     if (fread(&tmp, 1, sizeof(uint32_t), file_) != sizeof(uint32_t)) {
599       return kResultFail;
600     }
601     if ((!expect_network_order && swap_pcap_byte_order_) ||
602         (expect_network_order && swap_network_byte_order_)) {
603       tmp = ((tmp >> 24) & 0x000000ff) | (tmp << 24) |
604             ((tmp >> 8) & 0x0000ff00) | ((tmp << 8) & 0x00ff0000);
605     }
606     *out = tmp;
607     return kResultSuccess;
608   }
609 
Skip(uint32_t length)610   int Skip(uint32_t length) {
611     if (fseek(file_, length, SEEK_CUR) != 0) {
612       return kResultFail;
613     }
614     return kResultSuccess;
615   }
616 
617   FILE* file_;
618   bool swap_pcap_byte_order_;
619   const bool swap_network_byte_order_;
620   uint8_t read_buffer_[kMaxReadBufferSize];
621 
622   SsrcMap packets_by_ssrc_;
623   std::vector<RtpPacketMarker> packets_;
624   PacketIterator next_packet_it_;
625 };
626 
CreateReaderForFormat(RtpFileReader::FileFormat format)627 RtpFileReaderImpl* CreateReaderForFormat(RtpFileReader::FileFormat format) {
628   RtpFileReaderImpl* reader = nullptr;
629   switch (format) {
630     case RtpFileReader::kPcap:
631       reader = new PcapReader();
632       break;
633     case RtpFileReader::kRtpDump:
634       reader = new RtpDumpReader();
635       break;
636     case RtpFileReader::kLengthPacketInterleaved:
637       reader = new InterleavedRtpFileReader();
638       break;
639   }
640   return reader;
641 }
642 
Create(FileFormat format,const uint8_t * data,size_t size,const std::set<uint32_t> & ssrc_filter)643 RtpFileReader* RtpFileReader::Create(FileFormat format,
644                                      const uint8_t* data,
645                                      size_t size,
646                                      const std::set<uint32_t>& ssrc_filter) {
647   std::unique_ptr<RtpFileReaderImpl> reader(CreateReaderForFormat(format));
648 
649   FILE* file = tmpfile();
650   if (file == nullptr) {
651     printf("ERROR: Can't open file from memory buffer\n");
652     return nullptr;
653   }
654 
655   if (fwrite(reinterpret_cast<const void*>(data), sizeof(uint8_t), size,
656              file) != size) {
657     return nullptr;
658   }
659   rewind(file);
660 
661   if (!reader->Init(file, ssrc_filter)) {
662     return nullptr;
663   }
664   return reader.release();
665 }
666 
Create(FileFormat format,absl::string_view filename,const std::set<uint32_t> & ssrc_filter)667 RtpFileReader* RtpFileReader::Create(FileFormat format,
668                                      absl::string_view filename,
669                                      const std::set<uint32_t>& ssrc_filter) {
670   RtpFileReaderImpl* reader = CreateReaderForFormat(format);
671   std::string filename_str = std::string(filename);
672   FILE* file = fopen(filename_str.c_str(), "rb");
673   if (file == nullptr) {
674     printf("ERROR: Can't open file: %s\n", filename_str.c_str());
675     return nullptr;
676   }
677 
678   if (!reader->Init(file, ssrc_filter)) {
679     delete reader;
680     return nullptr;
681   }
682   return reader;
683 }
684 
Create(FileFormat format,absl::string_view filename)685 RtpFileReader* RtpFileReader::Create(FileFormat format,
686                                      absl::string_view filename) {
687   return RtpFileReader::Create(format, filename, std::set<uint32_t>());
688 }
689 
690 }  // namespace test
691 }  // namespace webrtc
692