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