fix(ios): rebuild audio only when needed and stop VoiceOver list churn
Two iOS bugs with the same shape: unconditional rebuilds where a conditional check belongs. The audio graph was torn down on every unintentional disconnect and every foreground transition. A lost connection ran the same teardown as an explicit disconnect, deactivating the AVAudioSession and so dropping the Bluetooth HFP link for a transport blip, and foregrounding always called Reconfigure even though the `audio` background mode keeps the graph live. Both cost seconds of dead audio on a headset. Split "session ended" from "transport blipped". Detach unbinds the client but keeps the session, graph, and route, so a reconnect rebinds to a live HFP link; the route is parked on stream id 0 so capture cannot feed the next connection a stream it never announced. StartListening reuses a running graph, StartMicrophone reuses a running tap of the same width, and Reconfigure gained a non-forcing mode that no-ops when tap presence, channel width, and voice processing all still match. Foregrounding now ensures the graph is running and only reconfigures if it actually stopped. Route changes, media-services resets, and the stall watchdog still force a full rebuild. Every list also reloaded on a model event raised 20 times a second by the microphone level timer. ReloadData recreates the accessibility element tree, so VoiceOver explore mode re-announced the row under a dragging finger and a double tap landed on an element that no longer existed. No controller ever unsubscribed, so popped controllers kept reloading too. Move the level to its own LevelChanged event, and reload lists through ListRefresher, which subscribes only while on screen and only reloads when the rendered content signature changed. The voice bar publishes its accessibility value on 5% steps, MoveUserController reloads just its two checkmark rows, and the chat transcripts skip reassigning identical text. The changed logic sits on UIKit and AVFoundation types the net10.0 test project cannot reference, so this carries no tests; the Bluetooth reconnect and foreground paths need device verification.
This commit is contained in:
@@ -39,6 +39,9 @@ internal sealed class IosAudioEngine
|
||||
private AVAudioConverterInputHandler? inputProvider;
|
||||
private bool inputProvided;
|
||||
private bool tapInstalled;
|
||||
// The voice-processing state the live graph was actually built with, so Reconfigure can tell
|
||||
// a settings change apart from a re-check of an unchanged graph.
|
||||
private bool voiceProcessing;
|
||||
private long captureCallbacks, capturedFrames, convertedFrames, rejectedFeeds, converterFailures, stereoFrames, stereoDifferentFrames;
|
||||
private long renderCallbacks, lastRenderTimestamp;
|
||||
internal bool IsConnected { get; private set; }
|
||||
@@ -50,26 +53,60 @@ internal sealed class IosAudioEngine
|
||||
|
||||
internal void StartListening(VoiceCatClient owner)
|
||||
{
|
||||
Stop(); client = owner; IsConnected = true; owner.Audio.MixedPcm += ReceiveMixedPcm; Rebuild();
|
||||
if (client is { } previous) previous.Audio.MixedPcm -= ReceiveMixedPcm;
|
||||
client = owner; IsConnected = true; owner.Audio.MixedPcm += ReceiveMixedPcm;
|
||||
// A reconnect after Detach finds the session active and the graph already running on the
|
||||
// right hardware. Rebuilding it there would release an HFP headset and pay a Bluetooth
|
||||
// profile renegotiation for a transport blip that changed no audio configuration.
|
||||
if (engine?.Running == true) { playbackRing.Resynchronize(); return; }
|
||||
Volatile.Write(ref microphone, null); Rebuild();
|
||||
}
|
||||
|
||||
// Unbinds the client without touching the session or the graph, for a connection that was
|
||||
// lost rather than ended. Capture keeps feeding a ring nobody drains and Render emits silence
|
||||
// until StartListening rebinds, which keeps the route and its Bluetooth profile alive.
|
||||
internal void Detach()
|
||||
{
|
||||
if (client is { } owner) owner.Audio.MixedPcm -= ReceiveMixedPcm;
|
||||
client = null; playbackRing.Resynchronize();
|
||||
// The route's stream id belongs to the connection that just died. Keep the tap and its
|
||||
// hardware, but park the route on the unbound id so a rebind cannot feed the next
|
||||
// connection a stream it never announced.
|
||||
if (Volatile.Read(ref microphone) is { } stale && stale.StreamId != 0)
|
||||
Volatile.Write(ref microphone, CreateMicrophoneRoute(0, stale.Channels));
|
||||
}
|
||||
|
||||
internal void StartMicrophone(uint streamId, int channels)
|
||||
{
|
||||
MicrophoneRoute? current = Volatile.Read(ref microphone);
|
||||
// Restoring voice after a reconnect only changes which stream id the existing capture
|
||||
// feeds. Reuse a running tap of the same width instead of rebuilding the graph around it.
|
||||
if (current is { StreamId: 0 } && current.Channels == Math.Clamp(channels, 1, 2) && tapInstalled && engine?.Running == true)
|
||||
{
|
||||
current.Ring.Resynchronize();
|
||||
Volatile.Write(ref microphone, CreateMicrophoneRoute(streamId, channels)); return;
|
||||
}
|
||||
Volatile.Write(ref microphone, CreateMicrophoneRoute(streamId, channels)); Rebuild();
|
||||
}
|
||||
|
||||
internal void StopMicrophone() { Volatile.Write(ref microphone, null); Rebuild(); }
|
||||
internal void Reconfigure()
|
||||
|
||||
// `force` rebuilds unconditionally, which is what a route change, a media-services reset and
|
||||
// the stall watchdog all need. Callers that are only re-checking a graph they expect to be
|
||||
// healthy — foregrounding, above all — pass false and get a no-op when nothing has changed.
|
||||
internal void Reconfigure(bool force = true)
|
||||
{
|
||||
if (!IsConnected) return;
|
||||
MicrophoneRoute? current = Volatile.Read(ref microphone);
|
||||
int channels = IosAudioRouter.Shared.CaptureChannels;
|
||||
if (!force && engine?.Running == true && tapInstalled == (current is not null) &&
|
||||
(current?.Channels ?? channels) == channels && voiceProcessing == IosAudioRouter.Shared.UsesVoiceProcessing) return;
|
||||
if (current is not null)
|
||||
{
|
||||
// Stop the old tap before publishing a route with a different sample width. Otherwise
|
||||
// an in-flight callback could interpret its old converter buffer using the new width.
|
||||
DestroyGraph();
|
||||
if (current.Channels != channels) client!.Audio.SetCaptureChannels(current.StreamId, channels);
|
||||
if (current.Channels != channels && current.StreamId != 0) client?.Audio.SetCaptureChannels(current.StreamId, channels);
|
||||
Volatile.Write(ref microphone, CreateMicrophoneRoute(current.StreamId, channels));
|
||||
}
|
||||
Rebuild();
|
||||
@@ -88,6 +125,7 @@ internal sealed class IosAudioEngine
|
||||
private void Rebuild()
|
||||
{
|
||||
DestroyGraph(); MicrophoneRoute? route = Volatile.Read(ref microphone); IosAudioRouter.Shared.Apply(route is not null);
|
||||
voiceProcessing = IosAudioRouter.Shared.UsesVoiceProcessing;
|
||||
var next = new AVAudioEngine();
|
||||
AVAudioInputNode? input = null;
|
||||
if (route is not null)
|
||||
@@ -189,7 +227,8 @@ internal sealed class IosAudioEngine
|
||||
private void PumpMicrophoneChunk(VoiceCatClient owner)
|
||||
{
|
||||
MicrophoneRoute? route = Volatile.Read(ref microphone);
|
||||
if (route is null) return;
|
||||
// Stream id 0 is a route parked by Detach: still capturing, not yet bound to a connection.
|
||||
if (route is null || route.StreamId == 0) return;
|
||||
int required = 960 * route.Channels;
|
||||
if (route.Ring.Read(microphoneFrame.AsSpan(0, required)) == required &&
|
||||
ReferenceEquals(route, Volatile.Read(ref microphone)) &&
|
||||
|
||||
Reference in New Issue
Block a user