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