2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
18 #include <netinet/in.h>
21 #include "webrtc/modules/audio_coding/neteq/tools/packet.h"
22 #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
27 RtpFileSource* RtpFileSource::Create(const std::string& file_name) {
28 RtpFileSource* source = new RtpFileSource;
30 if (!source->OpenFile(file_name) || !source->SkipFileHeader()) {
38 RtpFileSource::~RtpFileSource() {
43 bool RtpFileSource::RegisterRtpHeaderExtension(RTPExtensionType type,
45 assert(parser_.get());
46 return parser_->RegisterRtpHeaderExtension(type, id);
49 Packet* RtpFileSource::NextPacket() {
50 while (!EndOfFile()) {
52 if (fread(&length, sizeof(length), 1, in_file_) == 0) {
56 length = ntohs(length);
59 if (fread(&plen, sizeof(plen), 1, in_file_) == 0) {
66 if (fread(&offset, sizeof(offset), 1, in_file_) == 0) {
70 offset = ntohl(offset);
72 // Use length here because a plen of 0 specifies RTCP.
73 assert(length >= kPacketHeaderSize);
74 size_t packet_size_bytes = length - kPacketHeaderSize;
75 if (packet_size_bytes == 0) {
76 // May be an RTCP packet.
80 scoped_ptr<uint8_t> packet_memory(new uint8_t[packet_size_bytes]);
81 if (fread(packet_memory.get(), 1, packet_size_bytes, in_file_) !=
86 scoped_ptr<Packet> packet(new Packet(packet_memory.release(),
91 if (!packet->valid_header()) {
95 if (filter_.test(packet->header().payloadType)) {
96 // This payload type should be filtered out. Continue to the next packet.
99 return packet.release();
104 bool RtpFileSource::EndOfFile() const {
106 return ftell(in_file_) >= file_end_;
109 RtpFileSource::RtpFileSource()
113 parser_(RtpHeaderParser::Create()) {}
115 bool RtpFileSource::OpenFile(const std::string& file_name) {
116 in_file_ = fopen(file_name.c_str(), "rb");
118 if (in_file_ == NULL) {
122 // Find out how long the file is.
123 fseek(in_file_, 0, SEEK_END);
124 file_end_ = ftell(in_file_);
129 bool RtpFileSource::SkipFileHeader() {
130 char firstline[kFirstLineLength];
132 if (fgets(firstline, kFirstLineLength, in_file_) == NULL) {
136 // Check that the first line is ok.
137 if ((strncmp(firstline, "#!rtpplay1.0", 12) != 0) &&
138 (strncmp(firstline, "#!RTPencode1.0", 14) != 0)) {
142 // Skip the file header.
143 if (fseek(in_file_, kRtpFileHeaderSize, SEEK_CUR) != 0) {
151 } // namespace webrtc