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