sync_buffer.h 4.5 KB

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