• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019 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/initial_packet_inserter_neteq_input.h"
12 
13 #include <limits>
14 #include <memory>
15 #include <utility>
16 
17 #include "rtc_base/checks.h"
18 
19 namespace webrtc {
20 namespace test {
21 
InitialPacketInserterNetEqInput(std::unique_ptr<NetEqInput> source,int number_of_initial_packets,int sample_rate_hz)22 InitialPacketInserterNetEqInput::InitialPacketInserterNetEqInput(
23     std::unique_ptr<NetEqInput> source,
24     int number_of_initial_packets,
25     int sample_rate_hz)
26     : source_(std::move(source)),
27       packets_to_insert_(number_of_initial_packets),
28       sample_rate_hz_(sample_rate_hz) {}
29 
NextPacketTime() const30 absl::optional<int64_t> InitialPacketInserterNetEqInput::NextPacketTime()
31     const {
32   return source_->NextPacketTime();
33 }
34 
NextOutputEventTime() const35 absl::optional<int64_t> InitialPacketInserterNetEqInput::NextOutputEventTime()
36     const {
37   return source_->NextOutputEventTime();
38 }
39 
40 std::unique_ptr<InitialPacketInserterNetEqInput::PacketData>
PopPacket()41 InitialPacketInserterNetEqInput::PopPacket() {
42   if (!first_packet_) {
43     first_packet_ = source_->PopPacket();
44     if (!first_packet_) {
45       // The source has no packets, so we should not insert any dummy packets.
46       packets_to_insert_ = 0;
47     }
48   }
49   if (packets_to_insert_ > 0) {
50     RTC_CHECK(first_packet_);
51     auto dummy_packet = std::unique_ptr<PacketData>(new PacketData());
52     dummy_packet->header = first_packet_->header;
53     dummy_packet->payload = rtc::Buffer(first_packet_->payload.data(),
54                                         first_packet_->payload.size());
55     dummy_packet->time_ms = first_packet_->time_ms;
56     dummy_packet->header.sequenceNumber -= packets_to_insert_;
57     // This assumes 20ms per packet.
58     dummy_packet->header.timestamp -=
59         20 * sample_rate_hz_ * packets_to_insert_ / 1000;
60     packets_to_insert_--;
61     return dummy_packet;
62   }
63   return source_->PopPacket();
64 }
65 
AdvanceOutputEvent()66 void InitialPacketInserterNetEqInput::AdvanceOutputEvent() {
67   source_->AdvanceOutputEvent();
68 }
69 
ended() const70 bool InitialPacketInserterNetEqInput::ended() const {
71   return source_->ended();
72 }
73 
NextHeader() const74 absl::optional<RTPHeader> InitialPacketInserterNetEqInput::NextHeader() const {
75   return source_->NextHeader();
76 }
77 
78 }  // namespace test
79 }  // namespace webrtc
80