henrik.lundin@webrtc.org | 9a40081 | 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 | |
| 11 | #include <assert.h> |
| 12 | |
| 13 | #include <algorithm> // Access to min. |
| 14 | |
| 15 | #include "webrtc/modules/audio_coding/neteq4/sync_buffer.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | size_t SyncBuffer::FutureLength() const { |
| 20 | return Size() - next_index_; |
| 21 | } |
| 22 | |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 23 | void SyncBuffer::PushBack(const AudioMultiVector& append_this) { |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 24 | size_t samples_added = append_this.Size(); |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 25 | AudioMultiVector::PushBack(append_this); |
| 26 | AudioMultiVector::PopFront(samples_added); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 27 | 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. |
| 31 | // assert(false); |
| 32 | // 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 | |
| 40 | void SyncBuffer::PushFrontZeros(size_t length) { |
| 41 | InsertZerosAtIndex(length, 0); |
| 42 | } |
| 43 | |
| 44 | void SyncBuffer::InsertZerosAtIndex(size_t length, size_t position) { |
| 45 | position = std::min(position, Size()); |
| 46 | length = std::min(length, Size() - position); |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 47 | AudioMultiVector::PopBack(length); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | for (size_t channel = 0; channel < Channels(); ++channel) { |
| 49 | channels_[channel]->InsertZerosAt(length, position); |
| 50 | } |
| 51 | if (next_index_ >= position) { |
| 52 | // We are moving the |next_index_| sample. |
| 53 | set_next_index(next_index_ + length); // Overflow handled by subfunction. |
| 54 | } |
| 55 | if (dtmf_index_ > 0 && dtmf_index_ >= position) { |
| 56 | // We are moving the |dtmf_index_| sample. |
| 57 | set_dtmf_index(dtmf_index_ + length); // Overflow handled by subfunction. |
| 58 | } |
| 59 | } |
| 60 | |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 61 | void SyncBuffer::ReplaceAtIndex(const AudioMultiVector& insert_this, |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 62 | size_t length, |
| 63 | size_t position) { |
| 64 | position = std::min(position, Size()); // Cap |position| in the valid range. |
| 65 | length = std::min(length, Size() - position); |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 66 | AudioMultiVector::OverwriteAt(insert_this, length, position); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 67 | } |
| 68 | |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 69 | void SyncBuffer::ReplaceAtIndex(const AudioMultiVector& insert_this, |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 70 | size_t position) { |
| 71 | ReplaceAtIndex(insert_this, insert_this.Size(), position); |
| 72 | } |
| 73 | |
| 74 | size_t SyncBuffer::GetNextAudioInterleaved(size_t requested_len, |
| 75 | int16_t* output) { |
| 76 | if (!output) { |
| 77 | assert(false); |
| 78 | return 0; |
| 79 | } |
| 80 | size_t samples_to_read = std::min(FutureLength(), requested_len); |
| 81 | ReadInterleavedFromIndex(next_index_, samples_to_read, output); |
| 82 | next_index_ += samples_to_read; |
| 83 | return samples_to_read; |
| 84 | } |
| 85 | |
| 86 | void SyncBuffer::IncreaseEndTimestamp(uint32_t increment) { |
| 87 | end_timestamp_ += increment; |
| 88 | } |
| 89 | |
| 90 | void SyncBuffer::Flush() { |
| 91 | Zeros(Size()); |
| 92 | next_index_ = Size(); |
| 93 | end_timestamp_ = 0; |
| 94 | dtmf_index_ = 0; |
| 95 | } |
| 96 | |
| 97 | void SyncBuffer::set_next_index(size_t value) { |
| 98 | // Cannot set |next_index_| larger than the size of the buffer. |
| 99 | next_index_ = std::min(value, Size()); |
| 100 | } |
| 101 | |
| 102 | void SyncBuffer::set_dtmf_index(size_t value) { |
| 103 | // Cannot set |dtmf_index_| larger than the size of the buffer. |
| 104 | dtmf_index_ = std::min(value, Size()); |
| 105 | } |
| 106 | |
| 107 | } // namespace webrtc |