From 541e396833f27725bd82f701086f0b74c7ecc67f Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 11 Nov 2026 15:22:31 -0600 Subject: [PATCH] Redesign the RTP stats APIs to be race-free We now accumulate the stats internally via atomic operations and copy them into the caller's buffer when requested. While we could expose the atomic fields in the public ABI, we want to avoid this due to MSVC's C11 atomic implementation having no ABI guarantees with the /experimental flag. --- src/AudioStream.c | 4 ++-- src/Limelight.h | 10 ++++------ src/RtpAudioQueue.c | 34 ++++++++++++++++++++++++---------- src/RtpAudioQueue.h | 15 ++++++++++++++- src/RtpVideoQueue.c | 18 ++++++++++++++++-- src/RtpVideoQueue.h | 15 ++++++++++++++- src/VideoStream.c | 4 ++-- 7 files changed, 76 insertions(+), 24 deletions(-) diff --git a/src/AudioStream.c b/src/AudioStream.c index 075c985..698ee9f 100644 --- a/src/AudioStream.c +++ b/src/AudioStream.c @@ -475,6 +475,6 @@ int LiGetPendingAudioDuration(void) { return LiGetPendingAudioFrames() * AudioPacketDuration; } -const RTP_AUDIO_STATS* LiGetRTPAudioStats(void) { - return &rtpAudioQueue.stats; +void LiGetRTPAudioStats(PRTP_AUDIO_STATS stats) { + RtpaGetStats(&rtpAudioQueue, stats); } diff --git a/src/Limelight.h b/src/Limelight.h index f0593c1..4645178 100644 --- a/src/Limelight.h +++ b/src/Limelight.h @@ -879,8 +879,7 @@ int LiGetPendingAudioFrames(void); // negotiated audio frame duration. int LiGetPendingAudioDuration(void); -// Returns a pointer to a struct containing various statistics about the RTP audio stream. -// The data should be considered read-only and must not be modified. +// Populates a caller-supplied struct containing various statistics about the RTP audio stream. typedef struct _RTP_AUDIO_STATS { uint32_t packetCountAudio; // total audio packets uint32_t packetCountFec; // total packets of type FEC @@ -891,10 +890,9 @@ typedef struct _RTP_AUDIO_STATS { uint32_t packetCountFecInvalid; // invalid FEC packet } RTP_AUDIO_STATS, *PRTP_AUDIO_STATS; -const RTP_AUDIO_STATS* LiGetRTPAudioStats(void); +void LiGetRTPAudioStats(PRTP_AUDIO_STATS stats); -// Returns a pointer to a struct containing various statistics about the RTP video stream. -// The data should be considered read-only and must not be modified. +// Populates a caller-supplied struct containing various statistics about the RTP video stream. // Right now this is mainly used to track total video and FEC packets, as there are // many video stats already implemented at a higher level in moonlight-qt. typedef struct _RTP_VIDEO_STATS { @@ -907,7 +905,7 @@ typedef struct _RTP_VIDEO_STATS { uint32_t packetCountFecInvalid; // invalid FEC packet } RTP_VIDEO_STATS, *PRTP_VIDEO_STATS; -const RTP_VIDEO_STATS* LiGetRTPVideoStats(void); +void LiGetRTPVideoStats(PRTP_VIDEO_STATS stats); // Port index flags for use with LiGetPortFromPortFlagIndex() and LiGetProtocolFromPortFlagIndex() #define ML_PORT_INDEX_TCP_47984 0 diff --git a/src/RtpAudioQueue.c b/src/RtpAudioQueue.c index d9ef7f1..8fbbe19 100644 --- a/src/RtpAudioQueue.c +++ b/src/RtpAudioQueue.c @@ -205,19 +205,19 @@ static PRTPA_FEC_BLOCK getFecBlockForRtpPacket(PRTP_AUDIO_QUEUE queue, PRTP_PACK if (packet->packetType == RTP_PAYLOAD_TYPE_AUDIO) { if (length < sizeof(RTP_PACKET)) { - queue->stats.packetCountInvalid++; + atomic_fetch_add_explicit(&queue->stats.packetCountInvalid, 1, memory_order_relaxed); Limelog("RTP audio data packet too small: %u\n", length); LC_ASSERT_VT(false); return NULL; } - queue->stats.packetCountAudio++; + atomic_fetch_add_explicit(&queue->stats.packetCountAudio, 1, memory_order_relaxed); // Remember if we've received out-of-sequence packets lately. We can use // this knowledge to more quickly give up on FEC blocks. if (!queue->synchronizing && isBefore16(packet->sequenceNumber, queue->oldestRtpBaseSequenceNumber)) { queue->lastOosSequenceNumber = packet->sequenceNumber; - queue->stats.packetCountOOS++; + atomic_fetch_add_explicit(&queue->stats.packetCountOOS, 1, memory_order_relaxed); if (!queue->receivedOosData) { Limelog("Leaving fast audio recovery mode after OOS audio data (%u < %u)\n", packet->sequenceNumber, queue->oldestRtpBaseSequenceNumber); @@ -243,13 +243,13 @@ static PRTPA_FEC_BLOCK getFecBlockForRtpPacket(PRTP_AUDIO_QUEUE queue, PRTP_PACK PAUDIO_FEC_HEADER fecHeader = (PAUDIO_FEC_HEADER)(packet + 1); if (length < sizeof(RTP_PACKET) + sizeof(AUDIO_FEC_HEADER)) { - queue->stats.packetCountFecInvalid++; + atomic_fetch_add_explicit(&queue->stats.packetCountFecInvalid, 1, memory_order_relaxed); Limelog("RTP audio FEC packet too small: %u\n", length); LC_ASSERT_VT(false); return NULL; } - queue->stats.packetCountFec++; + atomic_fetch_add_explicit(&queue->stats.packetCountFec, 1, memory_order_relaxed); // This is an FEC packet, so we can just copy (and byteswap) the FEC header fecBlockPayloadType = fecHeader->payloadType; @@ -260,7 +260,7 @@ static PRTPA_FEC_BLOCK getFecBlockForRtpPacket(PRTP_AUDIO_QUEUE queue, PRTP_PACK // Ensure the FEC shard index is valid to prevent OOB access // later during recovery. if (fecHeader->fecShardIndex >= RTPA_FEC_SHARDS) { - queue->stats.packetCountFecInvalid++; + atomic_fetch_add_explicit(&queue->stats.packetCountFecInvalid, 1, memory_order_relaxed); Limelog("Too many audio FEC shards: %u\n", fecHeader->fecShardIndex); LC_ASSERT_VT(false); return NULL; @@ -270,7 +270,7 @@ static PRTPA_FEC_BLOCK getFecBlockForRtpPacket(PRTP_AUDIO_QUEUE queue, PRTP_PACK // The FEC blocks must start on a RTPA_DATA_SHARDS boundary for our queuing logic to work. This isn't // the case for older versions of GeForce Experience (at least 3.13). Disable the FEC logic if this // invariant is validated. - queue->stats.packetCountFecInvalid++; + atomic_fetch_add_explicit(&queue->stats.packetCountFecInvalid, 1, memory_order_relaxed); Limelog("Invalid FEC block base sequence number (got %u, expected %u)\n", fecBlockBaseSeqNum, (fecBlockBaseSeqNum / RTPA_DATA_SHARDS) * RTPA_DATA_SHARDS); Limelog("Audio FEC has been disabled due to an incompatibility with your host's old software!\n"); @@ -314,7 +314,7 @@ static PRTPA_FEC_BLOCK getFecBlockForRtpPacket(PRTP_AUDIO_QUEUE queue, PRTP_PACK if (existingBlock->blockSize != blockSize) { // This can happen with older versions of GeForce Experience (3.13) and Sunshine that don't use a // constant size for audio packets. - queue->stats.packetCountFecInvalid++; + atomic_fetch_add_explicit(&queue->stats.packetCountFecInvalid, 1, memory_order_relaxed); Limelog("Audio block size mismatch (got %u, expected %u)\n", blockSize, existingBlock->blockSize); Limelog("Audio FEC has been disabled due to an incompatibility with your host's old software!\n"); LC_ASSERT_VT(existingBlock->blockSize == blockSize); @@ -467,7 +467,7 @@ static bool completeFecBlock(PRTP_AUDIO_QUEUE queue, PRTPA_FEC_BLOCK block) { if (block->dataShardsReceived != RTPA_DATA_SHARDS) { - queue->stats.packetCountFecRecovered += RTPA_DATA_SHARDS - block->dataShardsReceived; + atomic_fetch_add_explicit(&queue->stats.packetCountFecRecovered, RTPA_DATA_SHARDS - block->dataShardsReceived, memory_order_relaxed); #ifdef FEC_VERBOSE Limelog("Recovered %d audio data shards from block %d\n", RTPA_DATA_SHARDS - block->dataShardsReceived, @@ -547,7 +547,7 @@ static void handleMissingPackets(PRTP_AUDIO_QUEUE queue) { if (!queue->receivedOosData || PltGetMicroseconds() - queue->blockHead->queueTimeUs > (uint64_t)(AudioPacketDuration * RTPA_DATA_SHARDS) + (RTPQ_OOS_WAIT_TIME_MS * 1000)) { LC_ASSERT(!isBefore16(queue->nextRtpSequenceNumber, queue->blockHead->fecHeader.baseSequenceNumber)); - queue->stats.packetCountFecFailed++; + atomic_fetch_add_explicit(&queue->stats.packetCountFecFailed, 1, memory_order_relaxed); Limelog("Unable to recover audio data block %u to %u (%u+%u=%u received < %u needed)\n", queue->blockHead->fecHeader.baseSequenceNumber, queue->blockHead->fecHeader.baseSequenceNumber + RTPA_DATA_SHARDS - 1, @@ -729,3 +729,17 @@ PRTP_PACKET RtpaGetQueuedPacket(PRTP_AUDIO_QUEUE queue, uint16_t customHeaderLen return NULL; } + +void RtpaGetStats(PRTP_AUDIO_QUEUE queue, PRTP_AUDIO_STATS stats) { + if (stats == NULL) { + return; + } + + stats->packetCountAudio = atomic_load_explicit(&queue->stats.packetCountAudio, memory_order_relaxed); + stats->packetCountFec = atomic_load_explicit(&queue->stats.packetCountFec, memory_order_relaxed); + stats->packetCountFecRecovered = atomic_load_explicit(&queue->stats.packetCountFecRecovered, memory_order_relaxed); + stats->packetCountFecFailed = atomic_load_explicit(&queue->stats.packetCountFecFailed, memory_order_relaxed); + stats->packetCountOOS = atomic_load_explicit(&queue->stats.packetCountOOS, memory_order_relaxed); + stats->packetCountInvalid = atomic_load_explicit(&queue->stats.packetCountInvalid, memory_order_relaxed); + stats->packetCountFecInvalid = atomic_load_explicit(&queue->stats.packetCountFecInvalid, memory_order_relaxed); +} diff --git a/src/RtpAudioQueue.h b/src/RtpAudioQueue.h index 2a73ab2..8950b2a 100644 --- a/src/RtpAudioQueue.h +++ b/src/RtpAudioQueue.h @@ -2,6 +2,8 @@ #include "Video.h" +#include + typedef struct _reed_solomon reed_solomon; // Maximum time to wait for an OOS data/FEC shard @@ -47,6 +49,16 @@ typedef struct _RTPA_FEC_BLOCK { // Data for shards comes here } RTPA_FEC_BLOCK, *PRTPA_FEC_BLOCK; +typedef struct _RTP_AUDIO_QUEUE_STATS { + atomic_uint packetCountAudio; + atomic_uint packetCountFec; + atomic_uint packetCountFecRecovered; + atomic_uint packetCountFecFailed; + atomic_uint packetCountOOS; + atomic_uint packetCountInvalid; + atomic_uint packetCountFecInvalid; +} RTP_AUDIO_QUEUE_STATS, *PRTP_AUDIO_QUEUE_STATS; + typedef struct _RTP_AUDIO_QUEUE { PRTPA_FEC_BLOCK blockHead; PRTPA_FEC_BLOCK blockTail; @@ -64,7 +76,7 @@ typedef struct _RTP_AUDIO_QUEUE { bool synchronizing; bool incompatibleServer; - RTP_AUDIO_STATS stats; + RTP_AUDIO_QUEUE_STATS stats; } RTP_AUDIO_QUEUE, *PRTP_AUDIO_QUEUE; #define RTPQ_RET_PACKET_CONSUMED 0x1 @@ -79,3 +91,4 @@ void RtpaInitializeQueue(PRTP_AUDIO_QUEUE queue); void RtpaCleanupQueue(PRTP_AUDIO_QUEUE queue); int RtpaAddPacket(PRTP_AUDIO_QUEUE queue, PRTP_PACKET packet, uint16_t length); PRTP_PACKET RtpaGetQueuedPacket(PRTP_AUDIO_QUEUE queue, uint16_t customHeaderLength, uint16_t* length); +void RtpaGetStats(PRTP_AUDIO_QUEUE queue, PRTP_AUDIO_STATS stats); diff --git a/src/RtpVideoQueue.c b/src/RtpVideoQueue.c index 59fd9fb..5133a65 100644 --- a/src/RtpVideoQueue.c +++ b/src/RtpVideoQueue.c @@ -714,8 +714,8 @@ int RtpvAddPacket(PRTP_VIDEO_QUEUE queue, PRTP_PACKET packet, int length, PRTPV_ queue->multiFecCurrentBlockNumber = fecCurrentBlockNumber; queue->multiFecLastBlockNumber = (nvPacket->multiFecBlocks >> 6) & 0x3; - queue->stats.packetCountVideo += queue->bufferDataPackets; - queue->stats.packetCountFec += queue->bufferParityPackets; + atomic_fetch_add_explicit(&queue->stats.packetCountVideo, queue->bufferDataPackets, memory_order_relaxed); + atomic_fetch_add_explicit(&queue->stats.packetCountFec, queue->bufferParityPackets, memory_order_relaxed); } // Reject packets above our FEC queue valid sequence number range @@ -809,3 +809,17 @@ int RtpvAddPacket(PRTP_VIDEO_QUEUE queue, PRTP_PACKET packet, int length, PRTPV_ } } +void RtpvGetStats(PRTP_VIDEO_QUEUE queue, PRTP_VIDEO_STATS stats) { + if (stats == NULL) { + return; + } + + stats->packetCountVideo = atomic_load_explicit(&queue->stats.packetCountVideo, memory_order_relaxed); + stats->packetCountFec = atomic_load_explicit(&queue->stats.packetCountFec, memory_order_relaxed); + stats->packetCountFecRecovered = atomic_load_explicit(&queue->stats.packetCountFecRecovered, memory_order_relaxed); + stats->packetCountFecFailed = atomic_load_explicit(&queue->stats.packetCountFecFailed, memory_order_relaxed); + stats->packetCountOOS = atomic_load_explicit(&queue->stats.packetCountOOS, memory_order_relaxed); + stats->packetCountInvalid = atomic_load_explicit(&queue->stats.packetCountInvalid, memory_order_relaxed); + stats->packetCountFecInvalid = atomic_load_explicit(&queue->stats.packetCountFecInvalid, memory_order_relaxed); +} + diff --git a/src/RtpVideoQueue.h b/src/RtpVideoQueue.h index c9bce6f..6e12b3c 100644 --- a/src/RtpVideoQueue.h +++ b/src/RtpVideoQueue.h @@ -2,6 +2,8 @@ #include "Video.h" +#include + typedef struct _RTPV_QUEUE_ENTRY { struct _RTPV_QUEUE_ENTRY* next; struct _RTPV_QUEUE_ENTRY* prev; @@ -19,6 +21,16 @@ typedef struct _RTPV_QUEUE_LIST { uint32_t count; } RTPV_QUEUE_LIST, *PRTPV_QUEUE_LIST; +typedef struct _RTP_VIDEO_QUEUE_STATS { + atomic_uint packetCountVideo; + atomic_uint packetCountFec; + atomic_uint packetCountFecRecovered; + atomic_uint packetCountFecFailed; + atomic_uint packetCountOOS; + atomic_uint packetCountInvalid; + atomic_uint packetCountFecInvalid; +} RTP_VIDEO_QUEUE_STATS, *PRTP_VIDEO_QUEUE_STATS; + typedef struct _RTP_VIDEO_QUEUE { RTPV_QUEUE_LIST pendingFecBlockList; RTPV_QUEUE_LIST completedFecBlockList; @@ -47,7 +59,7 @@ typedef struct _RTP_VIDEO_QUEUE { uint64_t lastOosFramePresentationTimestamp; bool receivedOosData; - RTP_VIDEO_STATS stats; // the above values are short-lived, this tracks stats for the life of the queue + RTP_VIDEO_QUEUE_STATS stats; // the above values are short-lived, this tracks stats for the life of the queue } RTP_VIDEO_QUEUE, *PRTP_VIDEO_QUEUE; #define RTPF_RET_QUEUED 0 @@ -58,3 +70,4 @@ void RtpvCleanupQueue(PRTP_VIDEO_QUEUE queue); int RtpvAddPacket(PRTP_VIDEO_QUEUE queue, PRTP_PACKET packet, int length, PRTPV_QUEUE_ENTRY packetEntry); uint32_t RtpvGetCurrentFrameNumber(PRTP_VIDEO_QUEUE queue); void RtpvSubmitQueuedPackets(PRTP_VIDEO_QUEUE queue); +void RtpvGetStats(PRTP_VIDEO_QUEUE queue, PRTP_VIDEO_STATS stats); diff --git a/src/VideoStream.c b/src/VideoStream.c index 8ea7451..a751005 100644 --- a/src/VideoStream.c +++ b/src/VideoStream.c @@ -416,6 +416,6 @@ int startVideoStream(void* rendererContext, int drFlags) { return 0; } -const RTP_VIDEO_STATS* LiGetRTPVideoStats(void) { - return &rtpQueue.stats; +void LiGetRTPVideoStats(PRTP_VIDEO_STATS stats) { + RtpvGetStats(&rtpQueue, stats); }