xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/tools/neteq_packet_source_input.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2016 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/neteq_packet_source_input.h"
12 
13 #include <algorithm>
14 #include <limits>
15 
16 #include "absl/strings/string_view.h"
17 #include "modules/audio_coding/neteq/tools/rtp_file_source.h"
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 namespace test {
22 
NetEqPacketSourceInput()23 NetEqPacketSourceInput::NetEqPacketSourceInput() : next_output_event_ms_(0) {}
24 
NextPacketTime() const25 absl::optional<int64_t> NetEqPacketSourceInput::NextPacketTime() const {
26   return packet_
27              ? absl::optional<int64_t>(static_cast<int64_t>(packet_->time_ms()))
28              : absl::nullopt;
29 }
30 
NextHeader() const31 absl::optional<RTPHeader> NetEqPacketSourceInput::NextHeader() const {
32   return packet_ ? absl::optional<RTPHeader>(packet_->header()) : absl::nullopt;
33 }
34 
LoadNextPacket()35 void NetEqPacketSourceInput::LoadNextPacket() {
36   packet_ = source()->NextPacket();
37 }
38 
PopPacket()39 std::unique_ptr<NetEqInput::PacketData> NetEqPacketSourceInput::PopPacket() {
40   if (!packet_) {
41     return std::unique_ptr<PacketData>();
42   }
43   std::unique_ptr<PacketData> packet_data(new PacketData);
44   packet_data->header = packet_->header();
45   if (packet_->payload_length_bytes() == 0 &&
46       packet_->virtual_payload_length_bytes() > 0) {
47     // This is a header-only "dummy" packet. Set the payload to all zeros, with
48     // length according to the virtual length.
49     packet_data->payload.SetSize(packet_->virtual_payload_length_bytes());
50     std::fill_n(packet_data->payload.data(), packet_data->payload.size(), 0);
51   } else {
52     packet_data->payload.SetData(packet_->payload(),
53                                  packet_->payload_length_bytes());
54   }
55   packet_data->time_ms = packet_->time_ms();
56 
57   LoadNextPacket();
58 
59   return packet_data;
60 }
61 
NetEqRtpDumpInput(absl::string_view file_name,const RtpHeaderExtensionMap & hdr_ext_map,absl::optional<uint32_t> ssrc_filter)62 NetEqRtpDumpInput::NetEqRtpDumpInput(absl::string_view file_name,
63                                      const RtpHeaderExtensionMap& hdr_ext_map,
64                                      absl::optional<uint32_t> ssrc_filter)
65     : source_(RtpFileSource::Create(file_name, ssrc_filter)) {
66   for (const auto& ext_pair : hdr_ext_map) {
67     source_->RegisterRtpHeaderExtension(ext_pair.second, ext_pair.first);
68   }
69   LoadNextPacket();
70 }
71 
NextOutputEventTime() const72 absl::optional<int64_t> NetEqRtpDumpInput::NextOutputEventTime() const {
73   return next_output_event_ms_;
74 }
75 
AdvanceOutputEvent()76 void NetEqRtpDumpInput::AdvanceOutputEvent() {
77   if (next_output_event_ms_) {
78     *next_output_event_ms_ += kOutputPeriodMs;
79   }
80   if (!NextPacketTime()) {
81     next_output_event_ms_ = absl::nullopt;
82   }
83 }
84 
source()85 PacketSource* NetEqRtpDumpInput::source() {
86   return source_.get();
87 }
88 
89 }  // namespace test
90 }  // namespace webrtc
91