fix #14740: serve playback only to the main window

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-10 13:52:26 +01:00
co-authored by Claude Opus 5.5
parent a74cfa5864
commit 8c47388eee
14 changed files with 624 additions and 124 deletions
+41 -13
View File
@@ -1,7 +1,6 @@
<script lang="ts">
import { convertFileSrc } from '@tauri-apps/api/core'
import { onDestroy } from 'svelte'
import { errorText, preparePlayback, type CallId } from './api'
import { errorText, playbackUrl, preparePlayback, releasePlayback, type CallId } from './api'
import { formatOffset } from './format'
let {
@@ -31,6 +30,21 @@
let error = $state('')
/** The mix held in memory where the webview cannot stream it (Linux). */
let blobUrl: string | null = null
/** This player's hold on the mix on disk (see `releasePlayback`). */
let lease: number | null = null
/** Set once the player is gone: a load still under way stops at the next
* step and keeps nothing (no blob, no mix on disk). */
let destroyed = false
const abort = new AbortController()
const LOAD_FAILED = 'Audio konnte nicht geladen werden.'
/** Lets go of the mix on disk (once). */
function release() {
if (lease === null) return
releasePlayback(callId, lease).catch(() => {})
lease = null
}
/** Mixes the call (first use only) and loads the mix into the player. */
function load(): Promise<boolean> {
@@ -38,29 +52,38 @@
preparing = true
error = ''
try {
const info = await preparePlayback(callId)
duration = info.duration_secs
const url = convertFileSrc(info.path)
const prepared = await preparePlayback(callId)
lease = prepared.lease
if (destroyed) return false
duration = prepared.duration_secs
const url = playbackUrl(callId)
try {
await loadSource(url)
} catch (err) {
if (destroyed) return false
if (!(err instanceof MediaError) || err.code !== MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED) {
throw err
}
// WebKitGTK (Linux) plays no media from a custom scheme such as
// asset:// (WebView2 gets http://asset.localhost instead), but
// fetches it fine: there the mix is played from memory.
const response = await fetch(url)
if (!response.ok) throw new Error(`Audio konnte nicht geladen werden (${response.status}).`)
blobUrl = URL.createObjectURL(await response.blob())
// playback:// (WebView2 gets http://playback.localhost instead),
// but fetches it fine: there the mix is played from memory, and
// the file is not needed any more.
const response = await fetch(url, { signal: abort.signal })
if (!response.ok) throw new Error(LOAD_FAILED)
const blob = await response.blob()
if (destroyed) return false
blobUrl = URL.createObjectURL(blob)
release()
await loadSource(blobUrl)
}
return true
return !destroyed
} catch (err) {
error = err instanceof MediaError ? 'Audio konnte nicht geladen werden.' : errorText(err)
if (destroyed) return false
error = err instanceof MediaError ? LOAD_FAILED : errorText(err)
loading = null // a later click tries again
return false
} finally {
if (destroyed) release()
preparing = false
}
})()
@@ -127,11 +150,16 @@
}
onDestroy(() => {
// Lets go of the file, so the call can be deleted right away.
destroyed = true
abort.abort()
// Lets go of the file and the memory, so the call can be deleted right
// away and no customer audio stays behind.
audio?.pause()
audio?.removeAttribute('src')
audio?.load()
if (blobUrl) URL.revokeObjectURL(blobUrl)
blobUrl = null
release()
onplaying?.(false)
})
</script>