You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

688 lines
20 KiB

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