[client] audio: harden low-latency streaming

Rework audio provider and backend lifecycles so playback and capture
callbacks quiesce without blocking real-time threads. Move activation,
teardown, controls, retries, and diagnostics onto bounded workers.

Harden USB audio cadence, feedback, and capture recovery.
Preserve source clocks through recording and pace packets from the
device clock. Bound queues, waits, conversion buffers, and packet sizes.

Make PipeWire and PulseAudio stream control thread-safe and recoverable.
Correct latency clock domains, coalesce rate updates, preserve recent
capture under overload, and keep logging outside real-time callbacks.
This commit is contained in:
Geoffrey McRae
2026-08-10 16:36:12 +10:00
parent 4434985aa3
commit 87aa61510c
13 changed files with 4795 additions and 1217 deletions

View File

@@ -106,7 +106,8 @@ typedef struct LG_AudioEventOps
{
/* Format and clock pointers are borrowed for the duration of each call.
* A NULL source clock indicates that the provider has no usable clock.
* Providers must serialize event delivery for an attachment. Stream
* Providers must serialize event delivery within each stream direction.
* Playback and recording events may be delivered concurrently. Stream
* generations are nonzero and uniquely identify each stream instance. */
void (*playbackStart)(void * opaque, uint32_t generation,
const LG_AudioFormat * format, const LG_AudioClock * sourceClock);
@@ -163,7 +164,9 @@ typedef struct LG_AudioOps
* audio callback with the measured playback device clock. Its position uses
* the device's independent output-frame timeline and its time includes the
* backend's estimated presentation latency. targetRate is the source frame
* rate requested by the client's buffer controller. */
* rate requested by the client's buffer controller. Return false while
* active rate feedback is unavailable; sustained failure makes the client
* restart playback with local rate control. */
bool (*clockFeedback)(void * opaque, uint32_t generation,
const LG_AudioClock * playbackClock, double targetRate);
}

View File

@@ -28,7 +28,9 @@
#include "interface/audio.h"
typedef int (*LG_AudioPullFn)(uint8_t * dst, int frames);
typedef void (*LG_AudioPushFn)(uint8_t * src, int frames);
typedef bool (*LG_AudioPushFn)(uint8_t * src, int frames,
const LG_AudioClock * sourceClock);
typedef void (*LG_AudioFailureFn)(uint32_t cookie);
struct LG_AudioDevOps
{
@@ -55,10 +57,13 @@ struct LG_AudioDevOps
bool requestResampler, bool * resamplerEnabled,
int * maxPeriodFrames, int * startFrames, LG_AudioPullFn pullFn);
/* called when there is data available to start playback */
void (*start)(void);
/* Called when there is data available to start playback. failureFn may
* report an asynchronous failure using the supplied cookie. Returning
* false means the failure callback is already quiescent. */
bool (*start)(LG_AudioFailureFn failureFn, uint32_t cookie);
/* called when the source reports the audio stream has stopped */
/* Called when the source reports the audio stream has stopped. This must
* synchronously quiesce the failure callback before returning. */
void (*stop)(void);
/* [optional] called to set the volume of the channels */
@@ -80,10 +85,17 @@ struct LG_AudioDevOps
struct
{
/* start the record stream using the requested interleaved format */
void (*start)(const LG_AudioFormat * format, LG_AudioPushFn pushFn);
/* Start the record stream using the requested interleaved format.
* sourceClock is borrowed for the duration of pushFn and describes the
* first frame. It is NULL when the backend has no source clock. pushFn
* returns false when the active provider rejects the frames.
* failureFn may report an asynchronous failure using the supplied cookie.
* Returning false means both callbacks are already quiescent. */
bool (*start)(const LG_AudioFormat * format, LG_AudioPushFn pushFn,
LG_AudioFailureFn failureFn, uint32_t cookie);
/* called when the source reports the audio stream has stopped */
/* Called when the source reports the audio stream has stopped. This must
* synchronously quiesce both callbacks before returning. */
void (*stop)(void);
/* [optional] called to set the volume of the channels */