[idd] audio: bridge broker packets over LGMP streams

Add optional playback and capture streams to the LGMP transport.
Negotiate ownership over the existing control path and keep audio
availability isolated from display transport readiness.

Route broker packets through public LGMP stream APIs without changing
the LGMP queue table or shared-memory mapping.
This commit is contained in:
Geoffrey McRae
2026-09-02 01:47:11 +10:00
parent 947f03ede6
commit 348f92d8b6
11 changed files with 2079 additions and 17 deletions

View File

@@ -79,6 +79,7 @@
<ClCompile Include="transport\TransportFactory.cpp" />
<ClCompile Include="transport\lgmp\CIVSHMEM.cpp" />
<ClCompile Include="transport\lgmp\CRecovery.cpp" />
<ClCompile Include="transport\lgmp\CLGMPAudioTransport.cpp" />
<ClCompile Include="transport\lgmp\CLGMPClipboardTransport.cpp" />
<ClCompile Include="transport\lgmp\CLGMPClipboardFiles.cpp" />
<ClCompile Include="transport\lgmp\CLGMPControl.cpp" />
@@ -158,6 +159,7 @@
<ClInclude Include="transport\TransportFactory.h" />
<ClInclude Include="transport\lgmp\CIVSHMEM.h" />
<ClInclude Include="transport\lgmp\CRecovery.h" />
<ClInclude Include="transport\lgmp\CLGMPAudioTransport.h" />
<ClInclude Include="transport\lgmp\CLGMPClipboardTransport.h" />
<ClInclude Include="transport\lgmp\CLGMPClipboardFiles.h" />
<ClInclude Include="transport\lgmp\CLGMPControl.h" />

View File

@@ -256,6 +256,9 @@
<ClInclude Include="transport\lgmp\CRecovery.h">
<Filter>Transport\LGMP</Filter>
</ClInclude>
<ClInclude Include="transport\lgmp\CLGMPAudioTransport.h">
<Filter>Transport\LGMP</Filter>
</ClInclude>
<ClInclude Include="transport\lgmp\CLGMPClipboardTransport.h">
<Filter>Transport\LGMP</Filter>
</ClInclude>
@@ -429,6 +432,9 @@
<ClCompile Include="transport\lgmp\CRecovery.cpp">
<Filter>Transport\LGMP</Filter>
</ClCompile>
<ClCompile Include="transport\lgmp\CLGMPAudioTransport.cpp">
<Filter>Transport\LGMP</Filter>
</ClCompile>
<ClCompile Include="transport\lgmp\CLGMPClipboardTransport.cpp">
<Filter>Transport\LGMP</Filter>
</ClCompile>

View File

@@ -1348,7 +1348,8 @@ public:
AudioResult SetClockFeedback(const AudioClockFeedback& feedback)
{
const uint32_t allowedFlags =
AUDIO_CLOCK_VALID | AUDIO_CLOCK_STABLE;
AUDIO_CLOCK_VALID | AUDIO_CLOCK_STABLE |
AUDIO_CLOCK_DISCONTINUITY;
if ((feedback.flags & ~allowedFlags) ||
!(feedback.flags & AUDIO_CLOCK_VALID))
return AudioResult::REJECTED;

View File

@@ -49,8 +49,9 @@ enum AudioPacketFlag : uint32_t
enum AudioClockFlag : uint32_t
{
AUDIO_CLOCK_VALID = 1U << 0,
AUDIO_CLOCK_STABLE = 1U << 1,
AUDIO_CLOCK_VALID = 1U << 0,
AUDIO_CLOCK_STABLE = 1U << 1,
AUDIO_CLOCK_DISCONTINUITY = 1U << 2,
};
enum class AudioStreamState : uint32_t
@@ -118,6 +119,7 @@ struct AudioClockFeedback
uint64_t playbackGeneration = 0;
uint64_t position = 0;
int64_t timeNs = 0;
uint64_t rateQ32 = 0;
uint64_t targetRateQ32 = 0;
uint32_t flags = 0;
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,230 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
#include "CSRWLock.h"
#include "common/KVMFRAudio.h"
#include "transport/IAudioSource.h"
#include <LGProtocol/LGMPConfig.h>
#include <Windows.h>
#include <stddef.h>
#include <stdint.h>
extern "C" {
#include "lgmp/host.h"
#include "lgmp/stream.h"
}
class CLGMPControl;
class CLGMPHost;
class CLGMPTransport;
class CLGMPAudioTransport final : public IAudioSource
{
private:
static constexpr unsigned RESPONSE_COUNT = LGMP_Q_POINTER_LEN;
static constexpr unsigned CLAIM_HISTORY_COUNT = 1024;
static constexpr unsigned INBOUND_DRAIN_MAX =
KVMFR_AUDIO_STREAM_SLOT_COUNT * 2U;
enum class OwnerState
{
NONE,
BOUND,
RELEASING,
};
struct PendingResponse
{
bool valid = false;
uint32_t clientID = 0;
KVMFRAudioStreamResponse response = {};
};
struct ResponseMemory
{
PLGMPMemory memory = nullptr;
bool valid = false;
uint32_t clientID = 0;
uint32_t requestGeneration = 0;
uint64_t claimToken = 0;
KVMFRAudioResponseResult result = 0;
};
struct ClaimRecord
{
bool valid = false;
bool closed = false;
uint32_t clientID = 0;
uint32_t requestGeneration = 0;
uint64_t claimToken = 0;
KVMFRAudioStreamResponse response = {};
};
struct PendingCapture
{
bool valid = false;
LGMPStreamBuffer buffer = {};
AudioPacket packet = {};
const uint8_t * data = nullptr;
size_t size = 0;
uint64_t readySerial = 0;
};
CLGMPHost& m_host;
CLGMPControl& m_control;
PLGMPHostStream m_hostToClientStream = nullptr;
PLGMPHostStream m_clientToHostStream = nullptr;
KVMFRStreamDescriptor m_hostToClientDescriptor = {};
KVMFRStreamDescriptor m_clientToHostDescriptor = {};
ResponseMemory m_responseMemory[RESPONSE_COUNT];
PendingResponse m_pendingResponse[LGMP_MAX_CLIENTS];
ClaimRecord m_claimRecord[CLAIM_HISTORY_COUNT];
CSRWLock m_lifecycleLock;
CSRWLock m_callbackLock;
CSRWLock m_lock;
IAudioTarget * m_target = nullptr;
bool m_initialized = false;
bool m_started = false;
bool m_failed = false;
bool m_failureNotified = false;
OwnerState m_ownerState = OwnerState::NONE;
uint32_t m_ownerClientID = 0;
uint32_t m_ownerGeneration = 0;
uint32_t m_clientCapabilities = 0;
uint64_t m_ownerToken = 0;
uint64_t m_ownerSerial = 0;
uint64_t m_ownerDeadline = 0;
uint64_t m_releaseGeneration = 0;
bool m_responseAdmitted = false;
bool m_clientReady = false;
bool m_activationIssued = false;
bool m_deactivationIssued = false;
KVMFRAudioStreamResponse m_ownerResponse = {};
AudioEndpointStatus m_state;
bool m_stateKnown = false;
bool m_statusDirty = false;
uint64_t m_announcedPlayback = 0;
uint64_t m_announcedCapture = 0;
uint64_t m_hostSequence = 0;
uint64_t m_nextClientSequence = 0;
bool m_clientSequenceKnown = false;
bool m_captureDiscontinuity = false;
bool m_playbackBlocked = false;
bool m_playbackReadyPending = false;
bool m_playbackCreditValid = false;
LGMPStreamBuffer m_playbackCredit = {};
AudioPacket m_blockedPlayback = {};
size_t m_blockedPlaybackSize = 0;
uint64_t m_captureReadySerial = 0;
PendingCapture m_pendingCapture;
bool InitializeStreams();
void FreeStreams();
bool BindStreams(uint32_t clientID);
bool TryUnbindStreams();
void ForceUnbindStreams();
static bool SameFormat(const AudioFormat& left,
const AudioFormat& right);
static bool SamePacket(const AudioPacket& left,
const AudioPacket& right);
static bool ValidFormat(const AudioFormat& format);
static KVMFRAudioFormat WireFormat(const AudioFormat& format);
static KVMFRAudioStreamState WireState(AudioStreamState state);
static KVMFRStreamDescriptor WireDescriptor(
const LGMPStreamDescriptor& descriptor);
bool ValidateClaim(const KVMFRAudioClaim& claim) const;
bool ResponsePending(uint32_t clientID,
const KVMFRAudioStreamResponse& response) const;
bool QueueResponse(uint32_t clientID,
const KVMFRAudioStreamResponse& response);
void CleanupResponseMemory();
void PumpResponses();
bool OwnerSubscribed() const;
void RenewLease();
void BeginRelease(const char * reason);
void ReleasePendingCapture();
void CancelPlaybackCredit();
void ClearBlockedPlayback(bool notify);
bool RememberBlockedPlayback(const AudioPacket& packet, size_t size);
void MaintainOwner(uint64_t now);
void ResetOwner();
KVMFRAudioStatus WireStatus() const;
bool CommitMessage(LGMPStreamBuffer& buffer,
KVMFRAudioMessageType type, uint64_t streamGeneration,
KVMFRAudioMessageFlags flags, uint64_t position, int64_t timeNs,
uint64_t rateQ32, uint32_t frames, const void * payload,
uint32_t length);
bool WriteMessage(KVMFRAudioMessageType type,
uint64_t streamGeneration, KVMFRAudioMessageFlags flags,
uint64_t position, int64_t timeNs, uint64_t rateQ32,
uint32_t frames, const void * payload, uint32_t length);
void PumpState();
void CheckPlaybackCredit();
bool ParseInbound(const LGMPStreamBuffer& buffer,
KVMFRAudioMessage& message) const;
bool PendingCaptureCurrent() const;
bool PrepareCapture(const KVMFRAudioMessage& message,
const LGMPStreamBuffer& buffer);
void DrainInbound();
void ProcessActivation();
void NotifyPlaybackReady();
void NotifyFailure();
void Fail(const char * operation, LGMP_STATUS status);
void InitializeOwner(uint32_t clientID,
const KVMFRAudioClaim& claim);
void HandleClaim(uint32_t clientID,
const KVMFRAudioClaim& claim);
bool Initialize();
void Process();
void TransportStop();
friend class CLGMPTransport;
public:
CLGMPAudioTransport(CLGMPHost& host, CLGMPControl& control) :
m_host(host), m_control(control) {}
~CLGMPAudioTransport() override;
CLGMPAudioTransport(const CLGMPAudioTransport&) = delete;
CLGMPAudioTransport& operator=(
const CLGMPAudioTransport&) = delete;
bool Start(IAudioTarget& target) override;
void Stop() override;
void AudioState(const AudioEndpointStatus& state) override;
AudioResult SendPlayback(const AudioPacket& packet,
const uint8_t * data, size_t size) override;
void CaptureReceiveReady() override;
};

View File

@@ -128,6 +128,31 @@ LGMP_STATUS CLGMPControl::AckData()
return lgmpHostAckData(m_pointerQueue);
}
LGMP_STATUS CLGMPControl::PostForClient(uint64_t udata,
PLGMPMemory memory, uint32_t clientID, unsigned& recipients)
{
recipients = 0;
if (!m_pointerQueue || !memory || !clientID)
return LGMP_ERR_INVALID_ARGUMENT;
return lgmpHostQueuePostForClients(m_pointerQueue, udata, memory,
&clientID, 1, &recipients);
}
bool CLGMPControl::PayloadPending(PLGMPMemory memory) const
{
return m_pointerQueue && memory &&
lgmpHostQueuePayloadPending(m_pointerQueue, memory);
}
LGMP_STATUS CLGMPControl::GetClientIDs(
uint32_t clientIDs[LGMP_MAX_CLIENTS], unsigned& count) const
{
count = 0;
if (!m_pointerQueue || !clientIDs)
return LGMP_ERR_INVALID_ARGUMENT;
return lgmpHostGetClientIDs(m_pointerQueue, clientIDs, &count);
}
bool CLGMPControl::HasNewSubscribers()
{
return lgmpHostQueueNewSubs(m_pointerQueue) != 0;

View File

@@ -34,11 +34,13 @@
#include <memory>
class CLGMPTransport;
class CLGMPAudioTransport;
class CLGMPControl final : public IControlSink
{
private:
friend class CLGMPTransport;
friend class CLGMPAudioTransport;
static constexpr int POINTER_SHAPE_BUFFERS = 3;
static constexpr int COLOR_TRANSFORM_BUFFERS = 3;
@@ -66,6 +68,11 @@ private:
LGMP_STATUS ReadDataWithSource(void * data, size_t * size,
uint32_t * sourceClientID);
LGMP_STATUS AckData();
LGMP_STATUS PostForClient(uint64_t udata, PLGMPMemory memory,
uint32_t clientID, unsigned& recipients);
bool PayloadPending(PLGMPMemory memory) const;
LGMP_STATUS GetClientIDs(
uint32_t clientIDs[LGMP_MAX_CLIENTS], unsigned& count) const;
bool HasNewSubscribers();
void RequestReplay();

View File

@@ -53,7 +53,8 @@ bool CLGMPHost::Initialize(CIVSHMEM& ivshmem)
KVMFR_FEATURE_WINDOWSIZE |
KVMFR_FEATURE_FRAME_SCHEDULE |
KVMFR_FEATURE_INPUT |
KVMFR_FEATURE_CLIPBOARD;
KVMFR_FEATURE_CLIPBOARD |
KVMFR_FEATURE_AUDIO;
strncpy_s(kvmfr.hostver, LG_VERSION_STR, sizeof(kvmfr.hostver) - 1);
ss.write(reinterpret_cast<const char *>(&kvmfr), sizeof(kvmfr));
}

View File

@@ -25,6 +25,8 @@
#include <LGProtocol/KVMFR.h>
#include <LGProtocol/KVMFRRecovery.h>
#include <string.h>
static bool TranslateFrameScheduleFlags(
uint32_t source, uint32_t& destination)
{
@@ -66,6 +68,7 @@ static const FrameProfile FRAME_PROFILES[] =
CLGMPTransport::CLGMPTransport(const TransportInstance& config) :
m_config(config),
m_control(m_host),
m_audio(m_host, m_control),
m_frames(m_host, m_ivshmem),
m_input(m_host),
m_clipboard(m_host)
@@ -91,12 +94,16 @@ bool CLGMPTransport::Initialize()
if (!m_host.Initialize(m_ivshmem))
return false;
// Preserve the existing shared-memory layout by appending input and then
// clipboard after the frame/pointer queues and retained pointer state.
// Preserve the existing shared-memory layout by appending input,
// clipboard, and optional audio after the frame/pointer queues and
// retained pointer state.
if (!m_frames.Initialize() || !m_control.Initialize() ||
!m_input.Initialize() || !m_clipboard.Initialize())
return false;
if (!m_audio.Initialize())
DEBUG_WARN("LGMP audio transport is unavailable");
m_frames.SealMemoryLayout();
return true;
}
@@ -122,12 +129,6 @@ ITransport::ProcessResult CLGMPTransport::Process(ITransportEvents& events)
recovery.active, admission.state, admission.error);
}
// Before the swap chain establishes the frame-buffer alignment, service
// only the protocol-independent recovery channel. This preserves the old
// transport startup boundary while keeping recovery available immediately.
if (!Atomic::Load(m_ready, std::memory_order_acquire))
return ProcessResult::OK;
const LGMP_STATUS processStatus = m_host.Process();
if (processStatus != LGMP_OK)
{
@@ -143,14 +144,16 @@ ITransport::ProcessResult CLGMPTransport::Process(ITransportEvents& events)
return ProcessResult::FAILURE;
}
m_input.FlushStatus();
const bool ready = Atomic::Load(m_ready, std::memory_order_acquire);
const uint64_t now = CFrameScheduler::Nanotime();
// Take the frame subscriber snapshot before processing scheduling messages,
// then publish both updates together just as the original timer did.
const CLGMPFrameTransport::SubscriberSnapshot subscribers =
m_frames.SnapshotSubscribers();
CLGMPFrameTransport::SubscriberSnapshot subscribers;
if (ready)
subscribers = m_frames.SnapshotSubscribers();
m_audio.Process();
uint8_t data[LGMP_MSGS_SIZE];
size_t size;
@@ -225,12 +228,35 @@ ITransport::ProcessResult CLGMPTransport::Process(ITransportEvents& events)
DEBUG_WARN("Ignoring invalid KVMFR frame schedule");
break;
}
case KVMFR_MESSAGE_AUDIO_CLAIM:
{
KVMFRAudioClaim claim = {};
const bool valid = size == sizeof(claim);
if (valid)
memcpy(&claim, data, sizeof(claim));
else
DEBUG_WARN("Ignoring invalid KVMFR audio claim");
// Do not let stream binding or a full response queue hold the
// shared pointer/control receive head.
m_control.AckData();
if (valid)
m_audio.HandleClaim(sourceClientID, claim);
continue;
}
}
m_control.AckData();
}
m_frames.FinalizeSubscribers(subscribers, now);
m_audio.Process();
if (ready)
{
m_input.FlushStatus();
m_frames.FinalizeSubscribers(subscribers, now);
}
if (m_control.HasNewSubscribers())
m_control.RequestReplay();
@@ -245,6 +271,7 @@ void CLGMPTransport::Stop()
m_hasActive = false;
m_clipboard.Stop();
m_input.Stop();
m_audio.TransportStop();
}
void CLGMPTransport::SyncRecovery()

View File

@@ -23,6 +23,7 @@
#include "Atomic.h"
#include "transport/ITransport.h"
#include "transport/lgmp/CIVSHMEM.h"
#include "transport/lgmp/CLGMPAudioTransport.h"
#include "transport/lgmp/CLGMPClipboardTransport.h"
#include "transport/lgmp/CLGMPControl.h"
#include "transport/lgmp/CLGMPFrameTransport.h"
@@ -39,6 +40,7 @@ private:
CIVSHMEM m_ivshmem;
CLGMPHost m_host;
CLGMPControl m_control;
CLGMPAudioTransport m_audio;
CLGMPFrameTransport m_frames;
CLGMPInputTransport m_input;
CLGMPClipboardTransport m_clipboard;
@@ -76,6 +78,7 @@ public:
DirectFrameBufferMemory GetDirectMemory() const override;
IFrameSink * FrameSink() override { return &m_frames; }
IAudioSource * Audio() override { return &m_audio; }
IControlSink * Control() override { return &m_control; }
IInputSource * Input() override { return &m_input; }
IClipboardSource * Clipboard() override { return &m_clipboard; }