blob: ee02288c86d66e5f3dd06b1495cd8e62456662fb [file] [log] [blame]
henrik.lundine8a77e32016-06-22 13:34:031/*
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 Bonadei92ea95e2017-09-15 04:47:3111#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
henrik.lundine8a77e32016-06-22 13:34:0312
13#include <algorithm>
14#include <limits>
15
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
17#include "rtc_base/checks.h"
henrik.lundine8a77e32016-06-22 13:34:0318
19namespace webrtc {
20namespace test {
21
22NetEqPacketSourceInput::NetEqPacketSourceInput() : next_output_event_ms_(0) {}
23
Danil Chapovalovb6021232018-06-19 11:26:3624absl::optional<int64_t> NetEqPacketSourceInput::NextPacketTime() const {
henrik.lundine8a77e32016-06-22 13:34:0325 return packet_
Danil Chapovalovb6021232018-06-19 11:26:3626 ? absl::optional<int64_t>(static_cast<int64_t>(packet_->time_ms()))
27 : absl::nullopt;
henrik.lundine8a77e32016-06-22 13:34:0328}
29
Danil Chapovalovb6021232018-06-19 11:26:3630absl::optional<RTPHeader> NetEqPacketSourceInput::NextHeader() const {
31 return packet_ ? absl::optional<RTPHeader>(packet_->header()) : absl::nullopt;
henrik.lundine8a77e32016-06-22 13:34:0332}
33
34void NetEqPacketSourceInput::LoadNextPacket() {
35 packet_ = source()->NextPacket();
36}
37
38std::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.lundin246ef3e2017-04-24 16:14:3243 packet_data->header = packet_->header();
henrik.lundind1a10a02016-08-24 17:58:5444 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.lundine8a77e32016-06-22 13:34:0354 packet_data->time_ms = packet_->time_ms();
55
56 LoadNextPacket();
57
58 return packet_data;
59}
60
henrik.lundin8a6a6002016-08-25 07:46:3661NetEqRtpDumpInput::NetEqRtpDumpInput(const std::string& file_name,
Bjorn Terelius5350d1c2018-10-11 14:51:2362 const RtpHeaderExtensionMap& hdr_ext_map,
63 absl::optional<uint32_t> ssrc_filter)
64 : source_(RtpFileSource::Create(file_name, ssrc_filter)) {
henrik.lundin8a6a6002016-08-25 07:46:3665 for (const auto& ext_pair : hdr_ext_map) {
66 source_->RegisterRtpHeaderExtension(ext_pair.second, ext_pair.first);
67 }
henrik.lundine8a77e32016-06-22 13:34:0368 LoadNextPacket();
69}
70
Danil Chapovalovb6021232018-06-19 11:26:3671absl::optional<int64_t> NetEqRtpDumpInput::NextOutputEventTime() const {
henrik.lundine8a77e32016-06-22 13:34:0372 return next_output_event_ms_;
73}
74
75void NetEqRtpDumpInput::AdvanceOutputEvent() {
76 if (next_output_event_ms_) {
77 *next_output_event_ms_ += kOutputPeriodMs;
78 }
79 if (!NextPacketTime()) {
Danil Chapovalovb6021232018-06-19 11:26:3680 next_output_event_ms_ = absl::nullopt;
henrik.lundine8a77e32016-06-22 13:34:0381 }
82}
83
84PacketSource* NetEqRtpDumpInput::source() {
85 return source_.get();
86}
87
henrik.lundine8a77e32016-06-22 13:34:0388} // namespace test
89} // namespace webrtc