blob: 7f6c111c716cdad57e95f8178d9ea9ed732d9a6b [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
12#define MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
16#include <vector>
17
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020018#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/neteq/audio_multi_vector.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "modules/audio_coding/neteq/audio_vector.h"
Henrik Lundin00eb12a2018-09-05 18:14:52 +020021#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023
24namespace webrtc {
25
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000026class SyncBuffer : public AudioMultiVector {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027 public:
28 SyncBuffer(size_t channels, size_t length)
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000029 : AudioMultiVector(channels, length),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030 next_index_(length),
31 end_timestamp_(0),
32 dtmf_index_(0) {}
33
henrik.lundin114c1b32017-04-26 07:47:32 -070034 // Returns the number of samples yet to play out from the buffer.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035 size_t FutureLength() const;
36
37 // Adds the contents of |append_this| to the back of the SyncBuffer. Removes
38 // the same number of samples from the beginning of the SyncBuffer, to
39 // maintain a constant buffer size. The |next_index_| is updated to reflect
40 // the move of the beginning of "future" data.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020041 void PushBack(const AudioMultiVector& append_this) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042
Henrik Lundin00eb12a2018-09-05 18:14:52 +020043 // Like PushBack, but reads the samples channel-interleaved from the input.
44 void PushBackInterleaved(const rtc::BufferT<int16_t>& append_this);
45
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046 // Adds |length| zeros to the beginning of each channel. Removes
47 // the same number of samples from the end of the SyncBuffer, to
48 // maintain a constant buffer size. The |next_index_| is updated to reflect
49 // the move of the beginning of "future" data.
50 // Note that this operation may delete future samples that are waiting to
51 // be played.
52 void PushFrontZeros(size_t length);
53
54 // Inserts |length| zeros into each channel at index |position|. The size of
55 // the SyncBuffer is kept constant, which means that the last |length|
56 // elements in each channel will be purged.
57 virtual void InsertZerosAtIndex(size_t length, size_t position);
58
59 // Overwrites each channel in this SyncBuffer with values taken from
60 // |insert_this|. The values are taken from the beginning of |insert_this| and
61 // are inserted starting at |position|. |length| values are written into each
62 // channel. The size of the SyncBuffer is kept constant. That is, if |length|
63 // and |position| are selected such that the new data would extend beyond the
64 // end of the current SyncBuffer, the buffer is not extended.
65 // The |next_index_| is not updated.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000066 virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000067 size_t length,
68 size_t position);
69
70 // Same as the above method, but where all of |insert_this| is written (with
71 // the same constraints as above, that the SyncBuffer is not extended).
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000072 virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073 size_t position);
74
75 // Reads |requested_len| samples from each channel and writes them interleaved
76 // into |output|. The |next_index_| is updated to point to the sample to read
henrik.lundin6d8e0112016-03-04 10:34:21 -080077 // next time. The AudioFrame |output| is first reset, and the |data_|,
henrik.lundin7dc68892016-04-06 01:03:02 -070078 // |num_channels_|, and |samples_per_channel_| fields are updated.
henrik.lundin6d8e0112016-03-04 10:34:21 -080079 void GetNextAudioInterleaved(size_t requested_len, AudioFrame* output);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080
81 // Adds |increment| to |end_timestamp_|.
82 void IncreaseEndTimestamp(uint32_t increment);
83
84 // Flushes the buffer. The buffer will contain only zeros after the flush, and
85 // |next_index_| will point to the end, like when the buffer was first
86 // created.
87 void Flush();
88
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000089 const AudioVector& Channel(size_t n) const { return *channels_[n]; }
90 AudioVector& Channel(size_t n) { return *channels_[n]; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091
92 // Accessors and mutators.
93 size_t next_index() const { return next_index_; }
94 void set_next_index(size_t value);
95 uint32_t end_timestamp() const { return end_timestamp_; }
96 void set_end_timestamp(uint32_t value) { end_timestamp_ = value; }
97 size_t dtmf_index() const { return dtmf_index_; }
98 void set_dtmf_index(size_t value);
99
100 private:
101 size_t next_index_;
102 uint32_t end_timestamp_; // The timestamp of the last sample in the buffer.
Yves Gerey665174f2018-06-19 15:03:05 +0200103 size_t dtmf_index_; // Index to the first non-DTMF sample in the buffer.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104
henrikg3c089d72015-09-16 05:37:44 -0700105 RTC_DISALLOW_COPY_AND_ASSIGN(SyncBuffer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106};
107
108} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200109#endif // MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_