henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h" |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <limits> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "modules/audio_coding/neteq/tools/rtp_file_source.h" |
| 17 | #include "rtc_base/checks.h" |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | |
| 22 | NetEqPacketSourceInput::NetEqPacketSourceInput() : next_output_event_ms_(0) {} |
| 23 | |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 24 | absl::optional<int64_t> NetEqPacketSourceInput::NextPacketTime() const { |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 25 | return packet_ |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 26 | ? absl::optional<int64_t>(static_cast<int64_t>(packet_->time_ms())) |
| 27 | : absl::nullopt; |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 28 | } |
| 29 | |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 30 | absl::optional<RTPHeader> NetEqPacketSourceInput::NextHeader() const { |
| 31 | return packet_ ? absl::optional<RTPHeader>(packet_->header()) : absl::nullopt; |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void NetEqPacketSourceInput::LoadNextPacket() { |
| 35 | packet_ = source()->NextPacket(); |
| 36 | } |
| 37 | |
| 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); |
henrik.lundin | 246ef3e | 2017-04-24 16:14:32 | [diff] [blame] | 43 | packet_data->header = packet_->header(); |
henrik.lundin | d1a10a0 | 2016-08-24 17:58:54 | [diff] [blame] | 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 | } |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 54 | packet_data->time_ms = packet_->time_ms(); |
| 55 | |
| 56 | LoadNextPacket(); |
| 57 | |
| 58 | return packet_data; |
| 59 | } |
| 60 | |
henrik.lundin | 8a6a600 | 2016-08-25 07:46:36 | [diff] [blame] | 61 | NetEqRtpDumpInput::NetEqRtpDumpInput(const std::string& file_name, |
Bjorn Terelius | 5350d1c | 2018-10-11 14:51:23 | [diff] [blame] | 62 | const RtpHeaderExtensionMap& hdr_ext_map, |
| 63 | absl::optional<uint32_t> ssrc_filter) |
| 64 | : source_(RtpFileSource::Create(file_name, ssrc_filter)) { |
henrik.lundin | 8a6a600 | 2016-08-25 07:46:36 | [diff] [blame] | 65 | for (const auto& ext_pair : hdr_ext_map) { |
| 66 | source_->RegisterRtpHeaderExtension(ext_pair.second, ext_pair.first); |
| 67 | } |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 68 | LoadNextPacket(); |
| 69 | } |
| 70 | |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 71 | absl::optional<int64_t> NetEqRtpDumpInput::NextOutputEventTime() const { |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 72 | return next_output_event_ms_; |
| 73 | } |
| 74 | |
| 75 | void NetEqRtpDumpInput::AdvanceOutputEvent() { |
| 76 | if (next_output_event_ms_) { |
| 77 | *next_output_event_ms_ += kOutputPeriodMs; |
| 78 | } |
| 79 | if (!NextPacketTime()) { |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 80 | next_output_event_ms_ = absl::nullopt; |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | PacketSource* NetEqRtpDumpInput::source() { |
| 85 | return source_.get(); |
| 86 | } |
| 87 | |
henrik.lundin | e8a77e3 | 2016-06-22 13:34:03 | [diff] [blame] | 88 | } // namespace test |
| 89 | } // namespace webrtc |