blob: 38e7887794fcef506abdf69a3b6b4ada3f3c7254 [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000014#include "webrtc/base/constructormagic.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000015#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000020class SyncBuffer : public AudioMultiVector {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021 public:
22 SyncBuffer(size_t channels, size_t length)
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000023 : AudioMultiVector(channels, length),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024 next_index_(length),
25 end_timestamp_(0),
26 dtmf_index_(0) {}
27
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000028 // Returns the number of samples yet to play out form the buffer.
29 size_t FutureLength() const;
30
31 // Adds the contents of |append_this| to the back of the SyncBuffer. Removes
32 // the same number of samples from the beginning of the SyncBuffer, to
33 // maintain a constant buffer size. The |next_index_| is updated to reflect
34 // the move of the beginning of "future" data.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020035 void PushBack(const AudioMultiVector& append_this) override;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036
37 // Adds |length| zeros to the beginning of each channel. Removes
38 // the same number of samples from the end 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.
41 // Note that this operation may delete future samples that are waiting to
42 // be played.
43 void PushFrontZeros(size_t length);
44
45 // Inserts |length| zeros into each channel at index |position|. The size of
46 // the SyncBuffer is kept constant, which means that the last |length|
47 // elements in each channel will be purged.
48 virtual void InsertZerosAtIndex(size_t length, size_t position);
49
50 // Overwrites each channel in this SyncBuffer with values taken from
51 // |insert_this|. The values are taken from the beginning of |insert_this| and
52 // are inserted starting at |position|. |length| values are written into each
53 // channel. The size of the SyncBuffer is kept constant. That is, if |length|
54 // and |position| are selected such that the new data would extend beyond the
55 // end of the current SyncBuffer, the buffer is not extended.
56 // The |next_index_| is not updated.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000057 virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058 size_t length,
59 size_t position);
60
61 // Same as the above method, but where all of |insert_this| is written (with
62 // the same constraints as above, that the SyncBuffer is not extended).
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000063 virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064 size_t position);
65
66 // Reads |requested_len| samples from each channel and writes them interleaved
67 // into |output|. The |next_index_| is updated to point to the sample to read
68 // next time.
69 size_t GetNextAudioInterleaved(size_t requested_len, int16_t* output);
70
71 // Adds |increment| to |end_timestamp_|.
72 void IncreaseEndTimestamp(uint32_t increment);
73
74 // Flushes the buffer. The buffer will contain only zeros after the flush, and
75 // |next_index_| will point to the end, like when the buffer was first
76 // created.
77 void Flush();
78
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000079 const AudioVector& Channel(size_t n) const { return *channels_[n]; }
80 AudioVector& Channel(size_t n) { return *channels_[n]; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081
82 // Accessors and mutators.
83 size_t next_index() const { return next_index_; }
84 void set_next_index(size_t value);
85 uint32_t end_timestamp() const { return end_timestamp_; }
86 void set_end_timestamp(uint32_t value) { end_timestamp_ = value; }
87 size_t dtmf_index() const { return dtmf_index_; }
88 void set_dtmf_index(size_t value);
89
90 private:
91 size_t next_index_;
92 uint32_t end_timestamp_; // The timestamp of the last sample in the buffer.
93 size_t dtmf_index_; // Index to the first non-DTMF sample in the buffer.
94
henrikg3c089d72015-09-16 05:37:44 -070095 RTC_DISALLOW_COPY_AND_ASSIGN(SyncBuffer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000096};
97
98} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000099#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_