make RTP timestamp strictly linear

this should fix some artificial delay
This commit is contained in:
ouwou
2023-05-28 16:53:07 -04:00
parent 7f021a7852
commit b5f2b7171f
4 changed files with 23 additions and 6 deletions

View File

@@ -50,6 +50,15 @@ void capture_data_callback(ma_device *pDevice, void *pOutput, const void *pInput
if (mgr == nullptr) return;
mgr->OnCapturedPCM(static_cast<const int16_t *>(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;
}

View File

@@ -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<uint32_t> m_rtp_timestamp = 0;
public:
using type_signal_opus_packet = sigc::signal<void(int payload_size)>;
type_signal_opus_packet signal_opus_packet();

View File

@@ -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<uint8_t> 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;

View File

@@ -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<void, std::vector<uint8_t>>;