Bug 1890689 Change AudioRingBuffer::SetLengthBytes() to EnsureLengthBytes() r=pehrsons
[gecko.git] / dom / media / AudioRingBuffer.h
bloba08211ba15c074c2538d61f02a4a141842adab04
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_AUDIO_RING_BUFFER_H_
7 #define MOZILLA_AUDIO_RING_BUFFER_H_
9 #include "AudioSampleFormat.h"
10 #include "mozilla/Span.h"
12 #include <functional>
14 namespace mozilla {
16 /**
17 * AudioRingBuffer works with audio sample format float or short. The
18 * implementation wrap around the RingBuffer thus it is not thread-safe. Reads
19 * and writes must happen in the same thread which may be different than the
20 * construction thread. The memory is pre-allocated in the constructor, but may
21 * also be re-allocated on the fly should a larger length be needed. The sample
22 * format has to be specified in order to be used.
24 class AudioRingBuffer final {
25 public:
26 explicit AudioRingBuffer(uint32_t aSizeInBytes);
27 ~AudioRingBuffer();
29 /**
30 * Set the sample format to either short or float. The sample format must be
31 * set before the using any other method.
33 void SetSampleFormat(AudioSampleFormat aFormat);
35 /**
36 * Write `aBuffer.Length()` number of samples when the format is float.
38 uint32_t Write(const Span<const float>& aBuffer);
40 /**
41 * Write `aBuffer.Length()` number of samples when the format is short.
43 uint32_t Write(const Span<const int16_t>& aBuffer);
45 /**
46 * Write `aSamples` number of samples from `aBuffer`. Note the `aBuffer` does
47 * not change.
49 uint32_t Write(const AudioRingBuffer& aBuffer, uint32_t aSamples);
51 /**
52 * Write `aSamples` number of zeros before the beginning of the existing data.
54 uint32_t PrependSilence(uint32_t aSamples);
56 /**
57 * Write `aSamples` number of zeros.
59 uint32_t WriteSilence(uint32_t aSamples);
61 /**
62 * Read `aBuffer.Length()` number of samples when the format is float.
64 uint32_t Read(const Span<float>& aBuffer);
66 /**
67 * Read `aBuffer.Length()` number of samples when the format is short.
69 uint32_t Read(const Span<int16_t>& aBuffer);
71 /**
72 * Read the internal buffer without extra copies when sample format is float.
73 * Check also the RingBuffer::ReadNoCopy() for more details.
75 uint32_t ReadNoCopy(
76 std::function<uint32_t(const Span<const float>&)>&& aCallable);
78 /**
79 * Read the internal buffer without extra copies when sample format is short.
80 * Check also the RingBuffer::ReadNoCopy() for more details.
82 uint32_t ReadNoCopy(
83 std::function<uint32_t(const Span<const int16_t>&)>&& aCallable);
85 /**
86 * Remove `aSamples` number of samples.
88 uint32_t Discard(uint32_t aSamples);
90 /**
91 * Remove all available samples.
93 uint32_t Clear();
95 /**
96 * Increase the ring buffer size if necessary to at least the specified length
97 * in bytes. Must be divisible by the sample size.
98 * Will not deallocate memory if the underlying buffer is large enough.
99 * Returns false if memory allocation is required and fails.
101 bool EnsureLengthBytes(uint32_t aLengthBytes);
104 * Return the number of samples this buffer can hold.
106 uint32_t Capacity() const;
109 * Return true if the buffer is full.
111 bool IsFull() const;
114 * Return true if the buffer is empty.
116 bool IsEmpty() const;
119 * Return the number of samples available for writing.
121 uint32_t AvailableWrite() const;
124 * Return the number of samples available for reading.
126 uint32_t AvailableRead() const;
128 private:
129 class AudioRingBufferPrivate;
130 UniquePtr<AudioRingBufferPrivate> mPtr;
133 } // namespace mozilla
135 #endif // MOZILLA_AUDIO_RING_BUFFER_H_