From 74a61b9f27d653da206cad1b268cff566c447840 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 11 Nov 2026 14:44:50 -0600 Subject: [PATCH] Synchronize HDR metadata updates --- src/ControlStream.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/ControlStream.c b/src/ControlStream.c index 845a67a..9c4b66d 100644 --- a/src/ControlStream.c +++ b/src/ControlStream.c @@ -114,8 +114,9 @@ static uint32_t lastSeenFrame; static bool stopping; static bool disconnectPending; static bool encryptedControlStream; -static bool hdrEnabled; +static atomic_bool hdrEnabled; static SS_HDR_METADATA hdrMetadata; +static PLT_MUTEX hdrMutex; static int intervalGoodFrameCount; static int intervalTotalFrameCount; @@ -320,6 +321,7 @@ int initializeControlStream(void) { LbqInitializeLinkedBlockingQueue(&frameFecStatusQueue, 8); // Limits number of frame status reports per periodic ping interval LbqInitializeLinkedBlockingQueue(&asyncCallbackQueue, 30); PltCreateMutex(&enetMutex); + PltCreateMutex(&hdrMutex); encryptedControlStream = APP_VERSION_AT_LEAST(7, 1, 431); @@ -369,7 +371,7 @@ int initializeControlStream(void) { usePeriodicPing = APP_VERSION_AT_LEAST(7, 1, 415); encryptionCtx = PltCreateCryptoContext(0); decryptionCtx = PltCreateCryptoContext(CTRL_CHANNEL_COUNT); - hdrEnabled = false; + atomic_store_explicit(&hdrEnabled, false, memory_order_relaxed); memset(&hdrMetadata, 0, sizeof(hdrMetadata)); return 0; @@ -395,6 +397,7 @@ void destroyControlStream(void) { freeBasicLbqList(LbqDestroyLinkedBlockingQueue(&frameFecStatusQueue)); freeBasicLbqList(LbqDestroyLinkedBlockingQueue(&asyncCallbackQueue)); + PltDeleteMutex(&hdrMutex); PltDeleteMutex(&enetMutex); } @@ -1173,7 +1176,7 @@ static void asyncCallbackThreadFunc(void* context) { queuedCb = nextCb; } - ListenerCallbacks.setHdrMode(hdrEnabled); + ListenerCallbacks.setHdrMode(LiGetCurrentHostDisplayHdrMode()); break; case IDX_SET_MOTION_EVENT: @@ -1475,6 +1478,8 @@ static void controlReceiveThreadFunc(void* context) { BbInitializeWrappedBuffer(&bb, (char*)ctlHdr, sizeof(*ctlHdr), packetLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE); + PltLockMutex(&hdrMutex); + BbGet8(&bb, &enableByte); if (IS_SUNSHINE()) { // Zero the metadata buffer to properly handle older servers if we have to add new fields @@ -1494,7 +1499,9 @@ static void controlReceiveThreadFunc(void* context) { BbGet16(&bb, &hdrMetadata.maxFullFrameLuminance); } - hdrEnabled = (enableByte != 0); + atomic_store_explicit(&hdrEnabled, enableByte != 0, memory_order_release); + + PltUnlockMutex(&hdrMutex); } // Process client callbacks in a separate thread @@ -2278,14 +2285,24 @@ int startControlStream(void) { } bool LiGetCurrentHostDisplayHdrMode(void) { - return hdrEnabled; + return atomic_load_explicit(&hdrEnabled, memory_order_acquire); } bool LiGetHdrMetadata(PSS_HDR_METADATA metadata) { - if (!IS_SUNSHINE() || !hdrEnabled) { + if (!IS_SUNSHINE() || !atomic_load_explicit(&hdrEnabled, memory_order_acquire)) { + return false; + } + + PltLockMutex(&hdrMutex); + + // Check again inside the lock to make sure HDR is still enabled + if (!atomic_load_explicit(&hdrEnabled, memory_order_relaxed)) { + PltUnlockMutex(&hdrMutex); return false; } *metadata = hdrMetadata; + PltUnlockMutex(&hdrMutex); + return true; }