blob: 4949bb201f3bb15d03c6c7b7be9165f59fb371d3 [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/audio_coding/neteq/sync_buffer.h"
12
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013#include <algorithm> // Access to min.
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
17namespace webrtc {
18
19size_t SyncBuffer::FutureLength() const {
20 return Size() - next_index_;
21}
22
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000023void SyncBuffer::PushBack(const AudioMultiVector& append_this) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024 size_t samples_added = append_this.Size();
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000025 AudioMultiVector::PushBack(append_this);
26 AudioMultiVector::PopFront(samples_added);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027 if (samples_added <= next_index_) {
28 next_index_ -= samples_added;
29 } else {
30 // This means that we are pushing out future data that was never used.
Yves Gerey665174f2018-06-19 15:03:05 +020031 // assert(false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032 // TODO(hlundin): This assert must be disabled to support 60 ms frames.
33 // This should not happen even for 60 ms frames, but it does. Investigate
34 // why.
35 next_index_ = 0;
36 }
37 dtmf_index_ -= std::min(dtmf_index_, samples_added);
38}
39
Henrik Lundin00eb12a2018-09-05 18:14:52 +020040void SyncBuffer::PushBackInterleaved(const rtc::BufferT<int16_t>& append_this) {
41 const size_t size_before_adding = Size();
42 AudioMultiVector::PushBackInterleaved(append_this);
43 const size_t samples_added_per_channel = Size() - size_before_adding;
44 RTC_DCHECK_EQ(samples_added_per_channel * Channels(), append_this.size());
45 AudioMultiVector::PopFront(samples_added_per_channel);
46 next_index_ -= std::min(next_index_, samples_added_per_channel);
47 dtmf_index_ -= std::min(dtmf_index_, samples_added_per_channel);
48}
49
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050void SyncBuffer::PushFrontZeros(size_t length) {
51 InsertZerosAtIndex(length, 0);
52}
53
54void SyncBuffer::InsertZerosAtIndex(size_t length, size_t position) {
55 position = std::min(position, Size());
56 length = std::min(length, Size() - position);
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000057 AudioMultiVector::PopBack(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058 for (size_t channel = 0; channel < Channels(); ++channel) {
59 channels_[channel]->InsertZerosAt(length, position);
60 }
61 if (next_index_ >= position) {
62 // We are moving the |next_index_| sample.
63 set_next_index(next_index_ + length); // Overflow handled by subfunction.
64 }
65 if (dtmf_index_ > 0 && dtmf_index_ >= position) {
66 // We are moving the |dtmf_index_| sample.
67 set_dtmf_index(dtmf_index_ + length); // Overflow handled by subfunction.
68 }
69}
70
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000071void SyncBuffer::ReplaceAtIndex(const AudioMultiVector& insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072 size_t length,
73 size_t position) {
74 position = std::min(position, Size()); // Cap |position| in the valid range.
75 length = std::min(length, Size() - position);
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000076 AudioMultiVector::OverwriteAt(insert_this, length, position);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077}
78
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000079void SyncBuffer::ReplaceAtIndex(const AudioMultiVector& insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 size_t position) {
81 ReplaceAtIndex(insert_this, insert_this.Size(), position);
82}
83
henrik.lundin6d8e0112016-03-04 10:34:21 -080084void SyncBuffer::GetNextAudioInterleaved(size_t requested_len,
85 AudioFrame* output) {
86 RTC_DCHECK(output);
87 const size_t samples_to_read = std::min(FutureLength(), requested_len);
Jonathan Yu3ffa72d2017-07-07 00:05:10 -070088 output->ResetWithoutMuting();
Yves Gerey665174f2018-06-19 15:03:05 +020089 const size_t tot_samples_read = ReadInterleavedFromIndex(
90 next_index_, samples_to_read, output->mutable_data());
henrik.lundin6d8e0112016-03-04 10:34:21 -080091 const size_t samples_read_per_channel = tot_samples_read / Channels();
92 next_index_ += samples_read_per_channel;
henrik.lundin6d8e0112016-03-04 10:34:21 -080093 output->num_channels_ = Channels();
94 output->samples_per_channel_ = samples_read_per_channel;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095}
96
97void SyncBuffer::IncreaseEndTimestamp(uint32_t increment) {
98 end_timestamp_ += increment;
99}
100
101void SyncBuffer::Flush() {
102 Zeros(Size());
103 next_index_ = Size();
104 end_timestamp_ = 0;
105 dtmf_index_ = 0;
106}
107
108void SyncBuffer::set_next_index(size_t value) {
109 // Cannot set |next_index_| larger than the size of the buffer.
110 next_index_ = std::min(value, Size());
111}
112
113void SyncBuffer::set_dtmf_index(size_t value) {
114 // Cannot set |dtmf_index_| larger than the size of the buffer.
115 dtmf_index_ = std::min(value, Size());
116}
117
118} // namespace webrtc