sync_buffer.h (4839B)
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 #ifndef MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <vector> 18 19 #include "api/audio/audio_view.h" 20 #include "modules/audio_coding/neteq/audio_multi_vector.h" 21 #include "modules/audio_coding/neteq/audio_vector.h" 22 #include "rtc_base/buffer.h" 23 24 namespace webrtc { 25 26 class SyncBuffer final : public AudioMultiVector { 27 public: 28 SyncBuffer(size_t channels, size_t length) 29 : AudioMultiVector(channels, length), 30 next_index_(length), 31 end_timestamp_(0), 32 dtmf_index_(0) {} 33 34 SyncBuffer(const SyncBuffer&) = delete; 35 SyncBuffer& operator=(const SyncBuffer&) = delete; 36 37 ~SyncBuffer() override = default; 38 39 // Returns the number of samples yet to play out from the buffer. 40 size_t FutureLength() const; 41 42 // Adds the contents of `append_this` to the back of the SyncBuffer. Removes 43 // the same number of samples from the beginning of the SyncBuffer, to 44 // maintain a constant buffer size. The `next_index_` is updated to reflect 45 // the move of the beginning of "future" data. 46 void PushBack(const AudioMultiVector& append_this) override; 47 48 // Like PushBack, but reads the samples channel-interleaved from the input. 49 void PushBackInterleaved(const BufferT<int16_t>& append_this); 50 51 // Adds `length` zeros to the beginning of each channel. Removes 52 // the same number of samples from the end of the SyncBuffer, to 53 // maintain a constant buffer size. The `next_index_` is updated to reflect 54 // the move of the beginning of "future" data. 55 // Note that this operation may delete future samples that are waiting to 56 // be played. 57 void PushFrontZeros(size_t length); 58 59 // Inserts `length` zeros into each channel at index `position`. The size of 60 // the SyncBuffer is kept constant, which means that the last `length` 61 // elements in each channel will be purged. 62 void InsertZerosAtIndex(size_t length, size_t position); 63 64 // Overwrites each channel in this SyncBuffer with values taken from 65 // `insert_this`. The values are taken from the beginning of `insert_this` and 66 // are inserted starting at `position`. `length` values are written into each 67 // channel. The size of the SyncBuffer is kept constant. That is, if `length` 68 // and `position` are selected such that the new data would extend beyond the 69 // end of the current SyncBuffer, the buffer is not extended. 70 // The `next_index_` is not updated. 71 void ReplaceAtIndex(const AudioMultiVector& insert_this, 72 size_t length, 73 size_t position); 74 75 // Same as the above method, but where all of `insert_this` is written (with 76 // the same constraints as above, that the SyncBuffer is not extended). 77 void ReplaceAtIndex(const AudioMultiVector& insert_this, size_t position); 78 79 // If enough data is available, reads `audio.samples_per_channel()` samples 80 // from each channel into `audio` and return true. 81 // If not enough data is available, the function returns false and no data 82 // will have been read. 83 // 84 // When successful, the internal `next_index_` position is updated to point 85 // to the samples to read next time around. 86 // 87 // Note: `audio` must be configured to have the same number of channels as 88 // `Channels()` and expected to meet the size limitations of AudioFrame. 89 bool GetNextAudioInterleaved(InterleavedView<int16_t> audio); 90 91 // Adds `increment` to `end_timestamp_`. 92 void IncreaseEndTimestamp(uint32_t increment); 93 94 // Flushes the buffer. The buffer will contain only zeros after the flush, and 95 // `next_index_` will point to the end, like when the buffer was first 96 // created. 97 void Flush(); 98 99 const AudioVector& Channel(size_t n) const { return *channels_[n]; } 100 AudioVector& Channel(size_t n) { return *channels_[n]; } 101 102 // Accessors and mutators. 103 size_t next_index() const { return next_index_; } 104 void set_next_index(size_t value); 105 uint32_t end_timestamp() const { return end_timestamp_; } 106 void set_end_timestamp(uint32_t value) { end_timestamp_ = value; } 107 size_t dtmf_index() const { return dtmf_index_; } 108 void set_dtmf_index(size_t value); 109 110 private: 111 size_t next_index_; 112 uint32_t end_timestamp_; // The timestamp of the last sample in the buffer. 113 size_t dtmf_index_; // Index to the first non-DTMF sample in the buffer. 114 }; 115 116 } // namespace webrtc 117 #endif // MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_