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