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 WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
13
14#include "webrtc/base/constructormagic.h"
15#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
16#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
20class SyncBuffer : public AudioMultiVector {
21 public:
22  SyncBuffer(size_t channels, size_t length)
23      : AudioMultiVector(channels, length),
24        next_index_(length),
25        end_timestamp_(0),
26        dtmf_index_(0) {}
27
28  virtual ~SyncBuffer() {}
29
30  // Returns the number of samples yet to play out form the buffer.
31  size_t FutureLength() const;
32
33  // Adds the contents of |append_this| to the back of the SyncBuffer. Removes
34  // the same number of samples from the beginning of the SyncBuffer, to
35  // maintain a constant buffer size. The |next_index_| is updated to reflect
36  // the move of the beginning of "future" data.
37  void PushBack(const AudioMultiVector& append_this);
38
39  // Adds |length| zeros to the beginning of each channel. Removes
40  // the same number of samples from the end of the SyncBuffer, to
41  // maintain a constant buffer size. The |next_index_| is updated to reflect
42  // the move of the beginning of "future" data.
43  // Note that this operation may delete future samples that are waiting to
44  // be played.
45  void PushFrontZeros(size_t length);
46
47  // Inserts |length| zeros into each channel at index |position|. The size of
48  // the SyncBuffer is kept constant, which means that the last |length|
49  // elements in each channel will be purged.
50  virtual void InsertZerosAtIndex(size_t length, size_t position);
51
52  // Overwrites each channel in this SyncBuffer with values taken from
53  // |insert_this|. The values are taken from the beginning of |insert_this| and
54  // are inserted starting at |position|. |length| values are written into each
55  // channel. The size of the SyncBuffer is kept constant. That is, if |length|
56  // and |position| are selected such that the new data would extend beyond the
57  // end of the current SyncBuffer, the buffer is not extended.
58  // The |next_index_| is not updated.
59  virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
60                              size_t length,
61                              size_t position);
62
63  // Same as the above method, but where all of |insert_this| is written (with
64  // the same constraints as above, that the SyncBuffer is not extended).
65  virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
66                              size_t position);
67
68  // Reads |requested_len| samples from each channel and writes them interleaved
69  // into |output|. The |next_index_| is updated to point to the sample to read
70  // next time.
71  size_t GetNextAudioInterleaved(size_t requested_len, int16_t* output);
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
81  const AudioVector& Channel(size_t n) const { return *channels_[n]; }
82  AudioVector& Channel(size_t n) { return *channels_[n]; }
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
97  DISALLOW_COPY_AND_ASSIGN(SyncBuffer);
98};
99
100}  // namespace webrtc
101#endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
102