• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
12 #define MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <vector>
18 
19 #include "api/audio/audio_frame.h"
20 #include "modules/audio_coding/neteq/audio_multi_vector.h"
21 #include "modules/audio_coding/neteq/audio_vector.h"
22 #include "rtc_base/buffer.h"
23 #include "rtc_base/constructor_magic.h"
24 
25 namespace webrtc {
26 
27 class SyncBuffer : public AudioMultiVector {
28  public:
SyncBuffer(size_t channels,size_t length)29   SyncBuffer(size_t channels, size_t length)
30       : AudioMultiVector(channels, length),
31         next_index_(length),
32         end_timestamp_(0),
33         dtmf_index_(0) {}
34 
35   // Returns the number of samples yet to play out from the buffer.
36   size_t FutureLength() const;
37 
38   // Adds the contents of |append_this| to the back of the SyncBuffer. Removes
39   // the same number of samples from the beginning 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   void PushBack(const AudioMultiVector& append_this) override;
43 
44   // Like PushBack, but reads the samples channel-interleaved from the input.
45   void PushBackInterleaved(const rtc::BufferT<int16_t>& append_this);
46 
47   // Adds |length| zeros to the beginning of each channel. Removes
48   // the same number of samples from the end of the SyncBuffer, to
49   // maintain a constant buffer size. The |next_index_| is updated to reflect
50   // the move of the beginning of "future" data.
51   // Note that this operation may delete future samples that are waiting to
52   // be played.
53   void PushFrontZeros(size_t length);
54 
55   // Inserts |length| zeros into each channel at index |position|. The size of
56   // the SyncBuffer is kept constant, which means that the last |length|
57   // elements in each channel will be purged.
58   virtual void InsertZerosAtIndex(size_t length, size_t position);
59 
60   // Overwrites each channel in this SyncBuffer with values taken from
61   // |insert_this|. The values are taken from the beginning of |insert_this| and
62   // are inserted starting at |position|. |length| values are written into each
63   // channel. The size of the SyncBuffer is kept constant. That is, if |length|
64   // and |position| are selected such that the new data would extend beyond the
65   // end of the current SyncBuffer, the buffer is not extended.
66   // The |next_index_| is not updated.
67   virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
68                               size_t length,
69                               size_t position);
70 
71   // Same as the above method, but where all of |insert_this| is written (with
72   // the same constraints as above, that the SyncBuffer is not extended).
73   virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
74                               size_t position);
75 
76   // Reads |requested_len| samples from each channel and writes them interleaved
77   // into |output|. The |next_index_| is updated to point to the sample to read
78   // next time. The AudioFrame |output| is first reset, and the |data_|,
79   // |num_channels_|, and |samples_per_channel_| fields are updated.
80   void GetNextAudioInterleaved(size_t requested_len, AudioFrame* output);
81 
82   // Adds |increment| to |end_timestamp_|.
83   void IncreaseEndTimestamp(uint32_t increment);
84 
85   // Flushes the buffer. The buffer will contain only zeros after the flush, and
86   // |next_index_| will point to the end, like when the buffer was first
87   // created.
88   void Flush();
89 
Channel(size_t n)90   const AudioVector& Channel(size_t n) const { return *channels_[n]; }
Channel(size_t n)91   AudioVector& Channel(size_t n) { return *channels_[n]; }
92 
93   // Accessors and mutators.
next_index()94   size_t next_index() const { return next_index_; }
95   void set_next_index(size_t value);
end_timestamp()96   uint32_t end_timestamp() const { return end_timestamp_; }
set_end_timestamp(uint32_t value)97   void set_end_timestamp(uint32_t value) { end_timestamp_ = value; }
dtmf_index()98   size_t dtmf_index() const { return dtmf_index_; }
99   void set_dtmf_index(size_t value);
100 
101  private:
102   size_t next_index_;
103   uint32_t end_timestamp_;  // The timestamp of the last sample in the buffer.
104   size_t dtmf_index_;       // Index to the first non-DTMF sample in the buffer.
105 
106   RTC_DISALLOW_COPY_AND_ASSIGN(SyncBuffer);
107 };
108 
109 }  // namespace webrtc
110 #endif  // MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
111