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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 14 | #include "modules/audio_coding/neteq/audio_multi_vector.h" |
| 15 | #include "modules/include/module_common_types.h" |
| 16 | #include "rtc_base/constructormagic.h" |
| 17 | #include "typedefs.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 21 | class SyncBuffer : public AudioMultiVector { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | public: |
| 23 | SyncBuffer(size_t channels, size_t length) |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 24 | : AudioMultiVector(channels, length), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 25 | next_index_(length), |
| 26 | end_timestamp_(0), |
| 27 | dtmf_index_(0) {} |
| 28 | |
henrik.lundin | 114c1b3 | 2017-04-26 07:47:32 -0700 | [diff] [blame] | 29 | // Returns the number of samples yet to play out from the buffer. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 30 | size_t FutureLength() const; |
| 31 | |
| 32 | // Adds the contents of |append_this| to the back of the SyncBuffer. Removes |
| 33 | // the same number of samples from the beginning of the SyncBuffer, to |
| 34 | // maintain a constant buffer size. The |next_index_| is updated to reflect |
| 35 | // the move of the beginning of "future" data. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 36 | void PushBack(const AudioMultiVector& append_this) override; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 37 | |
| 38 | // Adds |length| zeros to the beginning of each channel. Removes |
| 39 | // the same number of samples from the end of the SyncBuffer, to |
| 40 | // maintain a constant buffer size. The |next_index_| is updated to reflect |
| 41 | // the move of the beginning of "future" data. |
| 42 | // Note that this operation may delete future samples that are waiting to |
| 43 | // be played. |
| 44 | void PushFrontZeros(size_t length); |
| 45 | |
| 46 | // Inserts |length| zeros into each channel at index |position|. The size of |
| 47 | // the SyncBuffer is kept constant, which means that the last |length| |
| 48 | // elements in each channel will be purged. |
| 49 | virtual void InsertZerosAtIndex(size_t length, size_t position); |
| 50 | |
| 51 | // Overwrites each channel in this SyncBuffer with values taken from |
| 52 | // |insert_this|. The values are taken from the beginning of |insert_this| and |
| 53 | // are inserted starting at |position|. |length| values are written into each |
| 54 | // channel. The size of the SyncBuffer is kept constant. That is, if |length| |
| 55 | // and |position| are selected such that the new data would extend beyond the |
| 56 | // end of the current SyncBuffer, the buffer is not extended. |
| 57 | // The |next_index_| is not updated. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 58 | virtual void ReplaceAtIndex(const AudioMultiVector& insert_this, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 59 | size_t length, |
| 60 | size_t position); |
| 61 | |
| 62 | // Same as the above method, but where all of |insert_this| is written (with |
| 63 | // the same constraints as above, that the SyncBuffer is not extended). |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 64 | virtual void ReplaceAtIndex(const AudioMultiVector& insert_this, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 65 | size_t position); |
| 66 | |
| 67 | // Reads |requested_len| samples from each channel and writes them interleaved |
| 68 | // into |output|. The |next_index_| is updated to point to the sample to read |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 69 | // next time. The AudioFrame |output| is first reset, and the |data_|, |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 70 | // |num_channels_|, and |samples_per_channel_| fields are updated. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 71 | void GetNextAudioInterleaved(size_t requested_len, AudioFrame* output); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 72 | |
| 73 | // Adds |increment| to |end_timestamp_|. |
| 74 | void IncreaseEndTimestamp(uint32_t increment); |
| 75 | |
| 76 | // Flushes the buffer. The buffer will contain only zeros after the flush, and |
| 77 | // |next_index_| will point to the end, like when the buffer was first |
| 78 | // created. |
| 79 | void Flush(); |
| 80 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 81 | const AudioVector& Channel(size_t n) const { return *channels_[n]; } |
| 82 | AudioVector& Channel(size_t n) { return *channels_[n]; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 83 | |
| 84 | // Accessors and mutators. |
| 85 | size_t next_index() const { return next_index_; } |
| 86 | void set_next_index(size_t value); |
| 87 | uint32_t end_timestamp() const { return end_timestamp_; } |
| 88 | void set_end_timestamp(uint32_t value) { end_timestamp_ = value; } |
| 89 | size_t dtmf_index() const { return dtmf_index_; } |
| 90 | void set_dtmf_index(size_t value); |
| 91 | |
| 92 | private: |
| 93 | size_t next_index_; |
| 94 | uint32_t end_timestamp_; // The timestamp of the last sample in the buffer. |
| 95 | size_t dtmf_index_; // Index to the first non-DTMF sample in the buffer. |
| 96 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 97 | RTC_DISALLOW_COPY_AND_ASSIGN(SyncBuffer); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 101 | #endif // MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_ |