Skip to content

spice-gtk: let audio play out before stopping playback - #8194

Open
UTMapp-bot wants to merge 1 commit into
utmapp:mainfrom
UTMapp-bot:fix/issue-7788-spice-audio-tail-v2
Open

UTMapp-bot wants to merge 1 commit into
utmapp:mainfrom
UTMapp-bot:fix/issue-7788-spice-audio-tail-v2

Conversation

@UTMapp-bot

Copy link
Copy Markdown
Collaborator

With SPICE audio (the default), short sounds from the guest are cut off or never play. QEMU's SPICE audio backend sends playback-stop right after the last data of a stream. spice-gstaudio then switched the playback pipeline to READY at once, which threw away the audio still queued in the pipeline and in the CoreAudio sink's ring buffer (200 ms by default). Guests that start and stop the stream for every sound lost the end of each sound, and sounds shorter than the sink's latency never played at all.

Windows XP on AC97 does this. In the reporter's debug log, every sound is its own stream, and the cards dealt in Spider Solitaire start a new stream about every 125 ms. Only 8 of the 62 streams in that log ever reached the point where the sink reports a latency.

The fix is a new patch in patches/spice-gtk-0.42.patch. On playback-stop, the latency updates stop at once, but the pipeline keeps playing for two more seconds (or the sink's last reported latency plus half a second, if that is longer). If the guest starts playing again in the meantime, the stop is cancelled and the running pipeline is reused, so a series of short sounds plays through one open stream. A format change and dispose still stop at once. When the guest stops or the connection closes, the audio device is now released up to two seconds later. One case is not covered: if the guest switches to a different sample rate within those two seconds, the previous sound's tail is still cut, because the old pipeline is replaced at once.

Resolves #7788

Testing

Tested by an AI (Claude) on an Apple silicon MacBook Pro running macOS 27.2.

Setup

  • A Debug build of main with its arm64 sysroot.
  • "Before" and "after" are the same app. Only spice-client-glib-2.0.8.framework differs: "after" is spice-gtk 0.42 built with this branch's patch series.
  • Both copies were re-signed without the hardened runtime, so that a measuring library could be inserted (the same for both).
  • Guest: Debian 13 x86_64 (emulated), machine pc, CPU qemu64, AC97, SPICE audio.
  • In the guest, the pattern from the reporter's log: 48 kHz stereo, one stream per sound, each stream open for well under 200 ms (aplay with a 40 ms buffer). The sequence was 10 × 100 ms tones 1 s apart, then 10 × 50 ms tones back to back, then one 1.5 s tone.

Measurement

An inserted library wraps the render callback that GStreamer's osxaudio sink installs on its AudioUnit. It counts the audible frames that actually reach CoreAudio between each AudioOutputUnitStart and AudioOutputUnitStop. GStreamer and SPICE debug logging were off, because at a high level they starve the pipeline.

Results (audible milliseconds that reached CoreAudio, three runs each; phases split by the guest's timestamps)

10 × 100 ms tones (1000 ms) 10 × 50 ms tones (500 ms)
Before 234, 200 and 390 ms; 13–17 of the 18–22 output starts were silent 229, 384 and 333 ms; 3–6 of 7–10 output starts were silent
After 975, 1058 and 984 ms 477, 478 and 482 ms

Before the fix, the output unit was started and stopped for every sound, sometimes more than once. After it, it runs once per group of sounds.

The 1.5 s tone was cut short in every run, before or after, where aplay in the guest reported a write error. That is an ALSA error inside the guest, unrelated to this change. It played whole in runs without one.

UTM's log shows no new warnings. Powering the VM off with the pipeline open caused no crash.

QEMU's SPICE audio backend sends playback-stop right after the last data
of a stream, and spice-gstaudio switched the pipeline to READY at once,
discarding what was still queued in the pipeline and the CoreAudio ring
buffer. Guests that start and stop the stream for every sound (Windows
XP on AC97: each card dealt in Spider Solitaire is its own stream of
about 100 ms) lost the end of every sound, and sounds shorter than the
sink's latency never played at all.

Keep the pipeline playing for two seconds after playback-stop, and
cancel the stop when playback starts again.

Fixes utmapp#7788

Assisted-by: Claude:claude-opus-5-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spider Solitaire choppy sounds on Windows Me and XP, but other sounds are okay

2 participants