blob: 28d7649fdb353d7082ce946627671d4ee47b3080 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 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
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000011#include <algorithm> // Access to min.
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_coding/neteq/sync_buffer.h"
14#include "rtc_base/checks.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000015
16namespace webrtc {
17
18size_t SyncBuffer::FutureLength() const {
19 return Size() - next_index_;
20}
21
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000022void SyncBuffer::PushBack(const AudioMultiVector& append_this) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023 size_t samples_added = append_this.Size();
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000024 AudioMultiVector::PushBack(append_this);
25 AudioMultiVector::PopFront(samples_added);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026 if (samples_added <= next_index_) {
27 next_index_ -= samples_added;
28 } else {
29 // This means that we are pushing out future data that was never used.
30// assert(false);
31 // TODO(hlundin): This assert must be disabled to support 60 ms frames.
32 // This should not happen even for 60 ms frames, but it does. Investigate
33 // why.
34 next_index_ = 0;
35 }
36 dtmf_index_ -= std::min(dtmf_index_, samples_added);
37}
38
39void SyncBuffer::PushFrontZeros(size_t length) {
40 InsertZerosAtIndex(length, 0);
41}
42
43void SyncBuffer::InsertZerosAtIndex(size_t length, size_t position) {
44 position = std::min(position, Size());
45 length = std::min(length, Size() - position);
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000046 AudioMultiVector::PopBack(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047 for (size_t channel = 0; channel < Channels(); ++channel) {
48 channels_[channel]->InsertZerosAt(length, position);
49 }
50 if (next_index_ >= position) {
51 // We are moving the |next_index_| sample.
52 set_next_index(next_index_ + length); // Overflow handled by subfunction.
53 }
54 if (dtmf_index_ > 0 && dtmf_index_ >= position) {
55 // We are moving the |dtmf_index_| sample.
56 set_dtmf_index(dtmf_index_ + length); // Overflow handled by subfunction.
57 }
58}
59
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000060void SyncBuffer::ReplaceAtIndex(const AudioMultiVector& insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 size_t length,
62 size_t position) {
63 position = std::min(position, Size()); // Cap |position| in the valid range.
64 length = std::min(length, Size() - position);
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000065 AudioMultiVector::OverwriteAt(insert_this, length, position);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066}
67
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000068void SyncBuffer::ReplaceAtIndex(const AudioMultiVector& insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 size_t position) {
70 ReplaceAtIndex(insert_this, insert_this.Size(), position);
71}
72
henrik.lundin6d8e0112016-03-04 10:34:21 -080073void SyncBuffer::GetNextAudioInterleaved(size_t requested_len,
74 AudioFrame* output) {
75 RTC_DCHECK(output);
76 const size_t samples_to_read = std::min(FutureLength(), requested_len);
Jonathan Yu3ffa72d2017-07-07 00:05:10 -070077 output->ResetWithoutMuting();
henrik.lundin6d8e0112016-03-04 10:34:21 -080078 const size_t tot_samples_read =
yujo36b1a5f2017-06-12 12:45:32 -070079 ReadInterleavedFromIndex(next_index_, samples_to_read,
80 output->mutable_data());
henrik.lundin6d8e0112016-03-04 10:34:21 -080081 const size_t samples_read_per_channel = tot_samples_read / Channels();
82 next_index_ += samples_read_per_channel;
henrik.lundin6d8e0112016-03-04 10:34:21 -080083 output->num_channels_ = Channels();
84 output->samples_per_channel_ = samples_read_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000085}
86
87void SyncBuffer::IncreaseEndTimestamp(uint32_t increment) {
88 end_timestamp_ += increment;
89}
90
91void SyncBuffer::Flush() {
92 Zeros(Size());
93 next_index_ = Size();
94 end_timestamp_ = 0;
95 dtmf_index_ = 0;
96}
97
98void SyncBuffer::set_next_index(size_t value) {
99 // Cannot set |next_index_| larger than the size of the buffer.
100 next_index_ = std::min(value, Size());
101}
102
103void SyncBuffer::set_dtmf_index(size_t value) {
104 // Cannot set |dtmf_index_| larger than the size of the buffer.
105 dtmf_index_ = std::min(value, Size());
106}
107
108} // namespace webrtc