|
| 1 | +#pragma once |
| 2 | +#include <chrono> |
| 3 | +#include <cstdint> |
| 4 | +#include <deque> |
| 5 | + |
| 6 | +// very simple non-RTP-based jitter buffer. does not handle out-of-order |
| 7 | +template<typename SampleFormat> |
| 8 | +class JitterBuffer { |
| 9 | +public: |
| 10 | + /* |
| 11 | + * desired_latency: how many milliseconds before audio can be drawn from buffer |
| 12 | + * maximum_latency: how many milliseconds before old audio starts to be discarded |
| 13 | + */ |
| 14 | + JitterBuffer(int desired_latency, int maximum_latency, int channels, int sample_rate) |
| 15 | + : m_desired_latency(desired_latency) |
| 16 | + , m_maximum_latency(maximum_latency) |
| 17 | + , m_channels(channels) |
| 18 | + , m_sample_rate(sample_rate) |
| 19 | + , m_last_push(std::chrono::steady_clock::now()) { |
| 20 | + } |
| 21 | + |
| 22 | + [[nodiscard]] size_t Available() const noexcept { |
| 23 | + return m_samples.size(); |
| 24 | + } |
| 25 | + |
| 26 | + bool PopSamples(SampleFormat *ptr, size_t amount) { |
| 27 | + CheckBuffering(); |
| 28 | + if (m_buffering || Available() < amount) return false; |
| 29 | + std::copy(m_samples.begin(), m_samples.begin() + amount, ptr); |
| 30 | + m_samples.erase(m_samples.begin(), m_samples.begin() + amount); |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + void PushSamples(SampleFormat *ptr, size_t amount) { |
| 35 | + m_samples.insert(m_samples.end(), ptr, ptr + amount); |
| 36 | + m_last_push = std::chrono::steady_clock::now(); |
| 37 | + const auto buffered = MillisBuffered(); |
| 38 | + if (buffered > m_maximum_latency) { |
| 39 | + const auto overflow_ms = MillisBuffered() - m_maximum_latency; |
| 40 | + const auto overflow_samples = overflow_ms * m_channels * m_sample_rate / 1000; |
| 41 | + m_samples.erase(m_samples.begin(), m_samples.begin() + overflow_samples); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | +private: |
| 46 | + [[nodiscard]] size_t MillisBuffered() const { |
| 47 | + return m_samples.size() * 1000 / m_channels / m_sample_rate; |
| 48 | + } |
| 49 | + |
| 50 | + void CheckBuffering() { |
| 51 | + // if we arent buffering but the buffer is empty then we should be |
| 52 | + if (m_samples.empty()) { |
| 53 | + if (!m_buffering) { |
| 54 | + m_buffering = true; |
| 55 | + } |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (!m_buffering) return; |
| 60 | + |
| 61 | + // if we reached desired latency, we are sufficiently buffered |
| 62 | + const auto millis_buffered = MillisBuffered(); |
| 63 | + if (millis_buffered >= m_desired_latency) { |
| 64 | + m_buffering = false; |
| 65 | + } |
| 66 | + // if we havent buffered to desired latency but max latency has elapsed, exit buffering so it doesnt get stuck |
| 67 | + const auto now = std::chrono::steady_clock::now(); |
| 68 | + const auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_push).count(); |
| 69 | + if (millis >= m_maximum_latency) { |
| 70 | + m_buffering = false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + int m_desired_latency; |
| 75 | + int m_maximum_latency; |
| 76 | + int m_channels; |
| 77 | + int m_sample_rate; |
| 78 | + bool m_buffering = true; |
| 79 | + std::chrono::time_point<std::chrono::steady_clock> m_last_push; |
| 80 | + |
| 81 | + std::deque<SampleFormat> m_samples; |
| 82 | +}; |
0 commit comments