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 "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
12
13 #include <assert.h>
14 #include <string.h>
15 #ifdef WIN32
16 #include <winsock2.h>
17 #else
18 #include <netinet/in.h>
19 #endif
20
21 #include "webrtc/base/checks.h"
22 #include "webrtc/modules/audio_coding/neteq/tools/packet.h"
23 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
24 #include "webrtc/test/rtp_file_reader.h"
25
26 namespace webrtc {
27 namespace test {
28
Create(const std::string & file_name)29 RtpFileSource* RtpFileSource::Create(const std::string& file_name) {
30 RtpFileSource* source = new RtpFileSource();
31 RTC_CHECK(source->OpenFile(file_name));
32 return source;
33 }
34
ValidRtpDump(const std::string & file_name)35 bool RtpFileSource::ValidRtpDump(const std::string& file_name) {
36 rtc::scoped_ptr<RtpFileReader> temp_file(
37 RtpFileReader::Create(RtpFileReader::kRtpDump, file_name));
38 return !!temp_file;
39 }
40
ValidPcap(const std::string & file_name)41 bool RtpFileSource::ValidPcap(const std::string& file_name) {
42 rtc::scoped_ptr<RtpFileReader> temp_file(
43 RtpFileReader::Create(RtpFileReader::kPcap, file_name));
44 return !!temp_file;
45 }
46
~RtpFileSource()47 RtpFileSource::~RtpFileSource() {
48 }
49
RegisterRtpHeaderExtension(RTPExtensionType type,uint8_t id)50 bool RtpFileSource::RegisterRtpHeaderExtension(RTPExtensionType type,
51 uint8_t id) {
52 assert(parser_.get());
53 return parser_->RegisterRtpHeaderExtension(type, id);
54 }
55
NextPacket()56 Packet* RtpFileSource::NextPacket() {
57 while (true) {
58 RtpPacket temp_packet;
59 if (!rtp_reader_->NextPacket(&temp_packet)) {
60 return NULL;
61 }
62 if (temp_packet.original_length == 0) {
63 // May be an RTCP packet.
64 // Read the next one.
65 continue;
66 }
67 rtc::scoped_ptr<uint8_t[]> packet_memory(new uint8_t[temp_packet.length]);
68 memcpy(packet_memory.get(), temp_packet.data, temp_packet.length);
69 rtc::scoped_ptr<Packet> packet(new Packet(
70 packet_memory.release(), temp_packet.length,
71 temp_packet.original_length, temp_packet.time_ms, *parser_.get()));
72 if (!packet->valid_header()) {
73 assert(false);
74 return NULL;
75 }
76 if (filter_.test(packet->header().payloadType) ||
77 (use_ssrc_filter_ && packet->header().ssrc != ssrc_)) {
78 // This payload type should be filtered out. Continue to the next packet.
79 continue;
80 }
81 return packet.release();
82 }
83 }
84
RtpFileSource()85 RtpFileSource::RtpFileSource()
86 : PacketSource(),
87 parser_(RtpHeaderParser::Create()) {}
88
OpenFile(const std::string & file_name)89 bool RtpFileSource::OpenFile(const std::string& file_name) {
90 rtp_reader_.reset(RtpFileReader::Create(RtpFileReader::kRtpDump, file_name));
91 if (rtp_reader_)
92 return true;
93 rtp_reader_.reset(RtpFileReader::Create(RtpFileReader::kPcap, file_name));
94 if (!rtp_reader_) {
95 FATAL() << "Couldn't open input file as either a rtpdump or .pcap. Note "
96 "that .pcapng is not supported.";
97 }
98 return true;
99 }
100
101 } // namespace test
102 } // namespace webrtc
103