rtp_file_source.cc (3742B)
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 <algorithm> 14 #include <cstdint> 15 #include <cstring> 16 #include <memory> 17 #include <optional> 18 19 #include "absl/strings/string_view.h" 20 #include "api/units/timestamp.h" 21 #include "modules/audio_coding/neteq/tools/packet_source.h" 22 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 23 #include "modules/rtp_rtcp/source/rtp_packet_received.h" 24 #include "rtc_base/checks.h" 25 #include "test/rtp_file_reader.h" 26 27 namespace webrtc { 28 namespace test { 29 30 RtpFileSource* RtpFileSource::Create(absl::string_view file_name, 31 std::optional<uint32_t> ssrc_filter) { 32 RtpFileSource* source = new RtpFileSource(ssrc_filter); 33 RTC_CHECK(source->OpenFile(file_name)); 34 return source; 35 } 36 37 bool RtpFileSource::ValidRtpDump(absl::string_view file_name) { 38 std::unique_ptr<RtpFileReader> temp_file( 39 RtpFileReader::Create(RtpFileReader::kRtpDump, file_name)); 40 return !!temp_file; 41 } 42 43 bool RtpFileSource::ValidPcap(absl::string_view file_name) { 44 std::unique_ptr<RtpFileReader> temp_file( 45 RtpFileReader::Create(RtpFileReader::kPcap, file_name)); 46 return !!temp_file; 47 } 48 49 RtpFileSource::~RtpFileSource() {} 50 51 bool RtpFileSource::RegisterRtpHeaderExtension(RTPExtensionType type, 52 uint8_t id) { 53 return rtp_header_extension_map_.RegisterByType(id, type); 54 } 55 56 std::unique_ptr<RtpPacketReceived> RtpFileSource::NextPacket() { 57 while (true) { 58 RtpPacket temp_packet; 59 if (!rtp_reader_->NextPacket(&temp_packet)) { 60 return nullptr; 61 } 62 if (temp_packet.original_length == 0) { 63 // May be an RTCP packet. 64 // Read the next one. 65 continue; 66 } 67 auto rtp_packet = 68 std::make_unique<RtpPacketReceived>(&rtp_header_extension_map_); 69 if (!rtp_packet->Parse(temp_packet.data, temp_packet.length)) { 70 continue; 71 } 72 if (filter_.test(rtp_packet->PayloadType()) || 73 (ssrc_filter_ && rtp_packet->Ssrc() != *ssrc_filter_)) { 74 // This payload type should be filtered out. Continue to the next packet. 75 continue; 76 } 77 rtp_packet->set_arrival_time(Timestamp::Millis(temp_packet.time_ms)); 78 79 // Simulate payload if only the RTP header was written in the file. 80 if (temp_packet.original_length > rtp_packet->size()) { 81 size_t payload_size = 82 temp_packet.original_length - rtp_packet->headers_size(); 83 if (rtp_packet->has_padding()) { 84 // If padding bit is set in the RTP header, assume it was a pure padding 85 // packet. 86 rtp_packet->SetPadding(payload_size); 87 } else { 88 std::fill_n(rtp_packet->AllocatePayload(payload_size), payload_size, 0); 89 } 90 } 91 return rtp_packet; 92 } 93 } 94 95 RtpFileSource::RtpFileSource(std::optional<uint32_t> ssrc_filter) 96 : PacketSource(), ssrc_filter_(ssrc_filter) {} 97 98 bool RtpFileSource::OpenFile(absl::string_view file_name) { 99 rtp_reader_.reset(RtpFileReader::Create(RtpFileReader::kRtpDump, file_name)); 100 if (rtp_reader_) 101 return true; 102 rtp_reader_.reset(RtpFileReader::Create(RtpFileReader::kPcap, file_name)); 103 if (!rtp_reader_) { 104 RTC_FATAL() 105 << "Couldn't open input file as either a rtpdump or .pcap. Note " 106 << "that .pcapng is not supported."; 107 } 108 return true; 109 } 110 111 } // namespace test 112 } // namespace webrtc