From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- dom/media/AudioRingBuffer.h | 116 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 dom/media/AudioRingBuffer.h (limited to 'dom/media/AudioRingBuffer.h') diff --git a/dom/media/AudioRingBuffer.h b/dom/media/AudioRingBuffer.h new file mode 100644 index 0000000000..305e414bb8 --- /dev/null +++ b/dom/media/AudioRingBuffer.h @@ -0,0 +1,116 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef MOZILLA_AUDIO_RING_BUFFER_H_ +#define MOZILLA_AUDIO_RING_BUFFER_H_ + +#include "AudioSampleFormat.h" +#include "mozilla/Span.h" + +#include + +namespace mozilla { + +/** + * AudioRingBuffer works with audio sample format float or short. The + * implementation wrap around the RingBuffer thus it is not thread-safe. Reads + * and writes must happen in the same thread which may be different than the + * construction thread. The memory is pre-allocated in the constructor. The + * sample format has to be specified in order to be used. + */ +class AudioRingBuffer final { + public: + explicit AudioRingBuffer(uint32_t aSizeInBytes); + ~AudioRingBuffer(); + + /** + * Set the sample format to either short or float. The sample format must be + * set before the using any other method. + */ + void SetSampleFormat(AudioSampleFormat aFormat); + + /** + * Write `aBuffer.Length()` number of samples when the format is float. + */ + uint32_t Write(const Span& aBuffer); + + /** + * Write `aBuffer.Length()` number of samples when the format is short. + */ + uint32_t Write(const Span& aBuffer); + + /** + * Write `aSamples` number of samples from `aBuffer`. Note the `aBuffer` does + * not change. + */ + uint32_t Write(const AudioRingBuffer& aBuffer, uint32_t aSamples); + + /** + * Write `aSamples` number of zeros. + */ + uint32_t WriteSilence(uint32_t aSamples); + + /** + * Read `aBuffer.Length()` number of samples when the format is float. + */ + uint32_t Read(const Span& aBuffer); + + /** + * Read `aBuffer.Length()` number of samples when the format is short. + */ + uint32_t Read(const Span& aBuffer); + + /** + * Read the internal buffer without extra copies when sample format is float. + * Check also the RingBuffer::ReadNoCopy() for more details. + */ + uint32_t ReadNoCopy( + std::function&)>&& aCallable); + + /** + * Read the internal buffer without extra copies when sample format is short. + * Check also the RingBuffer::ReadNoCopy() for more details. + */ + uint32_t ReadNoCopy( + std::function&)>&& aCallable); + + /** + * Remove `aSamples` number of samples. + */ + uint32_t Discard(uint32_t aSamples); + + /** + * Remove all available samples. + */ + uint32_t Clear(); + + /** + * Return true if the buffer is full. + */ + bool IsFull() const; + + /** + * Return true if the buffer is empty. + */ + bool IsEmpty() const; + + /** + * Return the number of samples available for writing. + */ + uint32_t AvailableWrite() const; + + /** + * Return the number of samples available for reading. + */ + uint32_t AvailableRead() const; + + private: + class AudioRingBufferPrivate; + UniquePtr mPtr; +}; + +} // namespace mozilla + +#endif // MOZILLA_AUDIO_RING_BUFFER_H_ -- cgit v1.2.3