19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *
49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */
109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
11e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
12e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
14774b3d38a4a0f1a8ec08972a3c543cb5d607ce13henrike@webrtc.org#include "webrtc/base/constructormagic.h"
15e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "webrtc/typedefs.h"
179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc {
199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
200e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.orgclass SyncBuffer : public AudioMultiVector {
219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org public:
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  SyncBuffer(size_t channels, size_t length)
230e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.org      : AudioMultiVector(channels, length),
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        next_index_(length),
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        end_timestamp_(0),
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        dtmf_index_(0) {}
279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  virtual ~SyncBuffer() {}
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Returns the number of samples yet to play out form the buffer.
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t FutureLength() const;
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Adds the contents of |append_this| to the back of the SyncBuffer. Removes
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // the same number of samples from the beginning of the SyncBuffer, to
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // maintain a constant buffer size. The |next_index_| is updated to reflect
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // the move of the beginning of "future" data.
370e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.org  void PushBack(const AudioMultiVector& append_this);
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Adds |length| zeros to the beginning of each channel. Removes
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // the same number of samples from the end of the SyncBuffer, to
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // maintain a constant buffer size. The |next_index_| is updated to reflect
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // the move of the beginning of "future" data.
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Note that this operation may delete future samples that are waiting to
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // be played.
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void PushFrontZeros(size_t length);
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Inserts |length| zeros into each channel at index |position|. The size of
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // the SyncBuffer is kept constant, which means that the last |length|
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // elements in each channel will be purged.
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  virtual void InsertZerosAtIndex(size_t length, size_t position);
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Overwrites each channel in this SyncBuffer with values taken from
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // |insert_this|. The values are taken from the beginning of |insert_this| and
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // are inserted starting at |position|. |length| values are written into each
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // channel. The size of the SyncBuffer is kept constant. That is, if |length|
569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // and |position| are selected such that the new data would extend beyond the
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // end of the current SyncBuffer, the buffer is not extended.
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // The |next_index_| is not updated.
590e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.org  virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                              size_t length,
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                              size_t position);
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Same as the above method, but where all of |insert_this| is written (with
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // the same constraints as above, that the SyncBuffer is not extended).
650e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.org  virtual void ReplaceAtIndex(const AudioMultiVector& insert_this,
669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                              size_t position);
679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Reads |requested_len| samples from each channel and writes them interleaved
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // into |output|. The |next_index_| is updated to point to the sample to read
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // next time.
719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t GetNextAudioInterleaved(size_t requested_len, int16_t* output);
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Adds |increment| to |end_timestamp_|.
749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void IncreaseEndTimestamp(uint32_t increment);
759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Flushes the buffer. The buffer will contain only zeros after the flush, and
779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // |next_index_| will point to the end, like when the buffer was first
789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // created.
799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void Flush();
809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
81c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org  const AudioVector& Channel(size_t n) const { return *channels_[n]; }
82c1caa69f05663fc729af5a921eb95a73709f7dcdturaj@webrtc.org  AudioVector& Channel(size_t n) { return *channels_[n]; }
839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Accessors and mutators.
859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t next_index() const { return next_index_; }
869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void set_next_index(size_t value);
879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t end_timestamp() const { return end_timestamp_; }
889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void set_end_timestamp(uint32_t value) { end_timestamp_ = value; }
899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t dtmf_index() const { return dtmf_index_; }
909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void set_dtmf_index(size_t value);
919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org private:
939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t next_index_;
949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t end_timestamp_;  // The timestamp of the last sample in the buffer.
959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t dtmf_index_;  // Index to the first non-DTMF sample in the buffer.
969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DISALLOW_COPY_AND_ASSIGN(SyncBuffer);
989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org};
999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace webrtc
101e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_SYNC_BUFFER_H_
102