OLD | NEW |
(Empty) | |
| 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 "webrtc/modules/audio_coding/neteq/tools/encode_neteq_input.h" |
| 12 |
| 13 #include <utility> |
| 14 |
| 15 #include "webrtc/base/checks.h" |
| 16 |
| 17 namespace webrtc { |
| 18 namespace test { |
| 19 |
| 20 EncodeNetEqInput::EncodeNetEqInput(std::unique_ptr<InputAudioFile> input, |
| 21 std::unique_ptr<AudioEncoder> encoder, |
| 22 int64_t input_duration_ms) |
| 23 : input_(std::move(input)), |
| 24 encoder_(std::move(encoder)), |
| 25 input_duration_ms_(input_duration_ms) { |
| 26 CreatePacket(); |
| 27 } |
| 28 |
| 29 rtc::Optional<int64_t> EncodeNetEqInput::NextPacketTime() const { |
| 30 RTC_DCHECK(packet_data_); |
| 31 return rtc::Optional<int64_t>(static_cast<int64_t>(packet_data_->time_ms)); |
| 32 } |
| 33 |
| 34 rtc::Optional<int64_t> EncodeNetEqInput::NextOutputEventTime() const { |
| 35 return rtc::Optional<int64_t>(next_output_event_ms_); |
| 36 } |
| 37 |
| 38 std::unique_ptr<NetEqInput::PacketData> EncodeNetEqInput::PopPacket() { |
| 39 RTC_DCHECK(packet_data_); |
| 40 // Grab the packet to return... |
| 41 std::unique_ptr<PacketData> packet_to_return = std::move(packet_data_); |
| 42 // ... and line up the next packet for future use. |
| 43 CreatePacket(); |
| 44 |
| 45 return packet_to_return; |
| 46 } |
| 47 |
| 48 void EncodeNetEqInput::AdvanceOutputEvent() { |
| 49 next_output_event_ms_ += kOutputPeriodMs; |
| 50 } |
| 51 |
| 52 rtc::Optional<RTPHeader> EncodeNetEqInput::NextHeader() const { |
| 53 RTC_DCHECK(packet_data_); |
| 54 return rtc::Optional<RTPHeader>(packet_data_->header.header); |
| 55 } |
| 56 |
| 57 void EncodeNetEqInput::CreatePacket() { |
| 58 // Create a new PacketData object. |
| 59 RTC_DCHECK(!packet_data_); |
| 60 packet_data_.reset(new NetEqInput::PacketData); |
| 61 RTC_DCHECK_EQ(packet_data_->payload.size(), 0u); |
| 62 |
| 63 // Loop until we get a packet. |
| 64 AudioEncoder::EncodedInfo info; |
| 65 RTC_DCHECK(!info.send_even_if_empty); |
| 66 int num_blocks = 0; |
| 67 while (packet_data_->payload.size() == 0 && !info.send_even_if_empty) { |
| 68 const size_t num_samples = rtc::CheckedDivExact( |
| 69 static_cast<int>(encoder_->SampleRateHz() * kOutputPeriodMs), 1000); |
| 70 std::unique_ptr<int16_t[]> audio(new int16_t[num_samples]); |
| 71 RTC_CHECK(input_->Read(num_samples, audio.get())); |
| 72 |
| 73 info = encoder_->Encode( |
| 74 rtp_timestamp_, rtc::ArrayView<const int16_t>(audio.get(), num_samples), |
| 75 &packet_data_->payload); |
| 76 |
| 77 rtp_timestamp_ += |
| 78 num_samples * encoder_->RtpTimestampRateHz() / encoder_->SampleRateHz(); |
| 79 ++num_blocks; |
| 80 } |
| 81 packet_data_->header.header.timestamp = info.encoded_timestamp; |
| 82 packet_data_->header.header.payloadType = info.payload_type; |
| 83 packet_data_->header.header.sequenceNumber = sequence_number_++; |
| 84 packet_data_->time_ms = next_packet_time_ms_; |
| 85 next_packet_time_ms_ += num_blocks * kOutputPeriodMs; |
| 86 } |
| 87 |
| 88 } // namespace test |
| 89 } // namespace webrtc |
OLD | NEW |