From b5f2b7171f7c0a19d4c4a2974cd8977fd4955464 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Sun, 28 May 2023 16:53:07 -0400 Subject: make RTP timestamp strictly linear this should fix some artificial delay --- src/audio/manager.cpp | 13 +++++++++++++ src/audio/manager.hpp | 4 ++++ src/discord/voiceclient.cpp | 11 ++++++----- src/discord/voiceclient.hpp | 1 - 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/audio/manager.cpp b/src/audio/manager.cpp index 6e85ed8..ce20940 100644 --- a/src/audio/manager.cpp +++ b/src/audio/manager.cpp @@ -50,6 +50,15 @@ void capture_data_callback(ma_device *pDevice, void *pOutput, const void *pInput if (mgr == nullptr) return; mgr->OnCapturedPCM(static_cast(pInput), frameCount); + + /* + * You can simply increment it by 480 in UDPSocket::SendEncrypted but this is wrong + * The timestamp is supposed to be strictly linear eg. if there's discontinuous + * transmission for 1 second then the timestamp should be 48000 greater than the + * last packet. So it's incremented here because this is fired 100x per second + * and is always called in sync with UDPSocket::SendEncrypted + */ + mgr->m_rtp_timestamp += 480; } AudioManager::AudioManager() { @@ -473,6 +482,10 @@ AudioDevices &AudioManager::GetDevices() { return m_devices; } +uint32_t AudioManager::GetRTPTimestamp() const noexcept { + return m_rtp_timestamp; +} + AudioManager::type_signal_opus_packet AudioManager::signal_opus_packet() { return m_signal_opus_packet; } diff --git a/src/audio/manager.hpp b/src/audio/manager.hpp index 9cd7f42..ed40f35 100644 --- a/src/audio/manager.hpp +++ b/src/audio/manager.hpp @@ -63,6 +63,8 @@ public: [[nodiscard]] AudioDevices &GetDevices(); + [[nodiscard]] uint32_t GetRTPTimestamp() const noexcept; + private: void OnCapturedPCM(const int16_t *pcm, ma_uint32 frames); @@ -113,6 +115,8 @@ private: AudioDevices m_devices; + std::atomic m_rtp_timestamp = 0; + public: using type_signal_opus_packet = sigc::signal; type_signal_opus_packet signal_opus_packet(); diff --git a/src/discord/voiceclient.cpp b/src/discord/voiceclient.cpp index c37ba7b..741b07f 100644 --- a/src/discord/voiceclient.cpp +++ b/src/discord/voiceclient.cpp @@ -49,17 +49,18 @@ void UDPSocket::SetSSRC(uint32_t ssrc) { void UDPSocket::SendEncrypted(const uint8_t *data, size_t len) { m_sequence++; - m_timestamp += 480; // this is important + + const uint32_t timestamp = Abaddon::Get().GetAudio().GetRTPTimestamp(); std::vector rtp(12 + len + crypto_secretbox_MACBYTES, 0); rtp[0] = 0x80; // ver 2 rtp[1] = 0x78; // payload type 0x78 rtp[2] = (m_sequence >> 8) & 0xFF; rtp[3] = (m_sequence >> 0) & 0xFF; - rtp[4] = (m_timestamp >> 24) & 0xFF; - rtp[5] = (m_timestamp >> 16) & 0xFF; - rtp[6] = (m_timestamp >> 8) & 0xFF; - rtp[7] = (m_timestamp >> 0) & 0xFF; + rtp[4] = (timestamp >> 24) & 0xFF; + rtp[5] = (timestamp >> 16) & 0xFF; + rtp[6] = (timestamp >> 8) & 0xFF; + rtp[7] = (timestamp >> 0) & 0xFF; rtp[8] = (m_ssrc >> 24) & 0xFF; rtp[9] = (m_ssrc >> 16) & 0xFF; rtp[10] = (m_ssrc >> 8) & 0xFF; diff --git a/src/discord/voiceclient.hpp b/src/discord/voiceclient.hpp index 7bf4295..0112749 100644 --- a/src/discord/voiceclient.hpp +++ b/src/discord/voiceclient.hpp @@ -171,7 +171,6 @@ private: uint32_t m_ssrc; uint16_t m_sequence = 0; - uint32_t m_timestamp = 0; public: using type_signal_data = sigc::signal>; -- cgit v1.2.3