henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 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.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 11 | #include <algorithm> // Access to min. |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_coding/neteq/sync_buffer.h" |
| 14 | #include "rtc_base/checks.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | size_t SyncBuffer::FutureLength() const { |
| 19 | return Size() - next_index_; |
| 20 | } |
| 21 | |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 22 | void SyncBuffer::PushBack(const AudioMultiVector& append_this) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 23 | size_t samples_added = append_this.Size(); |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 24 | AudioMultiVector::PushBack(append_this); |
| 25 | AudioMultiVector::PopFront(samples_added); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 26 | 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. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | // assert(false); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
Henrik Lundin | 00eb12a | 2018-09-05 18:14:52 +0200 | [diff] [blame] | 39 | void SyncBuffer::PushBackInterleaved(const rtc::BufferT<int16_t>& append_this) { |
| 40 | const size_t size_before_adding = Size(); |
| 41 | AudioMultiVector::PushBackInterleaved(append_this); |
| 42 | const size_t samples_added_per_channel = Size() - size_before_adding; |
| 43 | RTC_DCHECK_EQ(samples_added_per_channel * Channels(), append_this.size()); |
| 44 | AudioMultiVector::PopFront(samples_added_per_channel); |
| 45 | next_index_ -= std::min(next_index_, samples_added_per_channel); |
| 46 | dtmf_index_ -= std::min(dtmf_index_, samples_added_per_channel); |
| 47 | } |
| 48 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 49 | void SyncBuffer::PushFrontZeros(size_t length) { |
| 50 | InsertZerosAtIndex(length, 0); |
| 51 | } |
| 52 | |
| 53 | void SyncBuffer::InsertZerosAtIndex(size_t length, size_t position) { |
| 54 | position = std::min(position, Size()); |
| 55 | length = std::min(length, Size() - position); |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 56 | AudioMultiVector::PopBack(length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 57 | for (size_t channel = 0; channel < Channels(); ++channel) { |
| 58 | channels_[channel]->InsertZerosAt(length, position); |
| 59 | } |
| 60 | if (next_index_ >= position) { |
| 61 | // We are moving the |next_index_| sample. |
| 62 | set_next_index(next_index_ + length); // Overflow handled by subfunction. |
| 63 | } |
| 64 | if (dtmf_index_ > 0 && dtmf_index_ >= position) { |
| 65 | // We are moving the |dtmf_index_| sample. |
| 66 | set_dtmf_index(dtmf_index_ + length); // Overflow handled by subfunction. |
| 67 | } |
| 68 | } |
| 69 | |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 70 | void SyncBuffer::ReplaceAtIndex(const AudioMultiVector& insert_this, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 71 | size_t length, |
| 72 | size_t position) { |
| 73 | position = std::min(position, Size()); // Cap |position| in the valid range. |
| 74 | length = std::min(length, Size() - position); |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 75 | AudioMultiVector::OverwriteAt(insert_this, length, position); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 76 | } |
| 77 | |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 78 | void SyncBuffer::ReplaceAtIndex(const AudioMultiVector& insert_this, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 79 | size_t position) { |
| 80 | ReplaceAtIndex(insert_this, insert_this.Size(), position); |
| 81 | } |
| 82 | |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 83 | void SyncBuffer::GetNextAudioInterleaved(size_t requested_len, |
| 84 | AudioFrame* output) { |
| 85 | RTC_DCHECK(output); |
| 86 | const size_t samples_to_read = std::min(FutureLength(), requested_len); |
Jonathan Yu | 3ffa72d | 2017-07-07 00:05:10 -0700 | [diff] [blame] | 87 | output->ResetWithoutMuting(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 88 | const size_t tot_samples_read = ReadInterleavedFromIndex( |
| 89 | next_index_, samples_to_read, output->mutable_data()); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 90 | const size_t samples_read_per_channel = tot_samples_read / Channels(); |
| 91 | next_index_ += samples_read_per_channel; |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 92 | output->num_channels_ = Channels(); |
| 93 | output->samples_per_channel_ = samples_read_per_channel; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void SyncBuffer::IncreaseEndTimestamp(uint32_t increment) { |
| 97 | end_timestamp_ += increment; |
| 98 | } |
| 99 | |
| 100 | void SyncBuffer::Flush() { |
| 101 | Zeros(Size()); |
| 102 | next_index_ = Size(); |
| 103 | end_timestamp_ = 0; |
| 104 | dtmf_index_ = 0; |
| 105 | } |
| 106 | |
| 107 | void SyncBuffer::set_next_index(size_t value) { |
| 108 | // Cannot set |next_index_| larger than the size of the buffer. |
| 109 | next_index_ = std::min(value, Size()); |
| 110 | } |
| 111 | |
| 112 | void SyncBuffer::set_dtmf_index(size_t value) { |
| 113 | // Cannot set |dtmf_index_| larger than the size of the buffer. |
| 114 | dtmf_index_ = std::min(value, Size()); |
| 115 | } |
| 116 | |
| 117 | } // namespace webrtc |