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