|
| 1 | +import { addPluginListener, invoke } from '@tauri-apps/api/core'; |
| 2 | + |
| 3 | +import { writeCacheFile } from './files.js'; |
| 4 | +import { isNativeMobile } from './os.js'; |
| 5 | + |
| 6 | +const FOLDER = 'native-audio'; |
| 7 | + |
| 8 | +interface PluginState { |
| 9 | + status: 'idle' | 'loading' | 'playing' | 'ended' | 'error'; |
| 10 | + currentTime: number; |
| 11 | + duration: number; |
| 12 | + isPlaying: boolean; |
| 13 | + error?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export interface NativeTrack { |
| 17 | + /** The blob URL the timeline already holds; it also tells tracks apart. */ |
| 18 | + url: string; |
| 19 | + name: string; |
| 20 | +} |
| 21 | + |
| 22 | +export interface NativeTrackState { |
| 23 | + playing: boolean; |
| 24 | + ended: boolean; |
| 25 | + currentTime: number; |
| 26 | + duration: number | null; |
| 27 | +} |
| 28 | + |
| 29 | +let activeUrl = $state<string | null>(null); |
| 30 | +let plugin = $state<PluginState | null>(null); |
| 31 | +let ready: Promise<void> | null = null; |
| 32 | +let activePath: string | null = null; |
| 33 | +let loads = 0; |
| 34 | +let queue: Promise<unknown> = Promise.resolve(); |
| 35 | + |
| 36 | +/** |
| 37 | + * The webviews mute or freeze `<audio>` once the app leaves the screen, so the phone apps play |
| 38 | + * through the system player, which also brings lock screen and notification controls. |
| 39 | + */ |
| 40 | +export function playsNatively(): boolean { |
| 41 | + return isNativeMobile(); |
| 42 | +} |
| 43 | + |
| 44 | +function setUp(): Promise<void> { |
| 45 | + ready ??= (async () => { |
| 46 | + await addPluginListener<PluginState>('native-audio', 'native_audio_state', (next) => { |
| 47 | + plugin = next; |
| 48 | + }); |
| 49 | + plugin = await invoke<PluginState>('plugin:native-audio|initialize'); |
| 50 | + })().catch((error: unknown) => { |
| 51 | + ready = null; |
| 52 | + throw error; |
| 53 | + }); |
| 54 | + return ready; |
| 55 | +} |
| 56 | + |
| 57 | +function serial<T>(task: () => Promise<T>): Promise<T> { |
| 58 | + const run = queue.then(task, task); |
| 59 | + queue = run.catch(() => undefined); |
| 60 | + return run; |
| 61 | +} |
| 62 | + |
| 63 | +async function load(track: NativeTrack): Promise<void> { |
| 64 | + await setUp(); |
| 65 | + if (activeUrl === track.url) return; |
| 66 | + const bytes = new Uint8Array(await (await fetch(track.url)).arrayBuffer()); |
| 67 | + loads += 1; |
| 68 | + const path = await writeCacheFile(FOLDER, `${String(loads)}-${track.name}`, bytes); |
| 69 | + plugin = await invoke<PluginState>('plugin:native-audio|set_source', { |
| 70 | + src: path, |
| 71 | + title: track.name, |
| 72 | + }); |
| 73 | + const previous = activePath; |
| 74 | + activeUrl = track.url; |
| 75 | + activePath = path; |
| 76 | + if (previous !== null) { |
| 77 | + const { remove } = await import('@tauri-apps/plugin-fs'); |
| 78 | + await remove(previous).catch((error: unknown) => { |
| 79 | + console.debug('[sable audio] the previous track was not removed', previous, error); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export const nativeAudio = { |
| 85 | + /** Null while another track, or none, holds the player. */ |
| 86 | + of(url: string): NativeTrackState | null { |
| 87 | + if (activeUrl !== url || plugin === null) return null; |
| 88 | + return { |
| 89 | + playing: plugin.isPlaying, |
| 90 | + ended: plugin.status === 'ended', |
| 91 | + currentTime: plugin.currentTime, |
| 92 | + duration: plugin.duration > 0 ? plugin.duration : null, |
| 93 | + }; |
| 94 | + }, |
| 95 | + toggle(track: NativeTrack, rate = 1): Promise<void> { |
| 96 | + return serial(async () => { |
| 97 | + if (activeUrl === track.url && plugin?.isPlaying) { |
| 98 | + plugin = await invoke<PluginState>('plugin:native-audio|pause'); |
| 99 | + return; |
| 100 | + } |
| 101 | + await load(track); |
| 102 | + await invoke('plugin:native-audio|set_rate', { rate }); |
| 103 | + plugin = await invoke<PluginState>('plugin:native-audio|play'); |
| 104 | + }); |
| 105 | + }, |
| 106 | + seek(track: NativeTrack, position: number): Promise<void> { |
| 107 | + return serial(async () => { |
| 108 | + await load(track); |
| 109 | + plugin = await invoke<PluginState>('plugin:native-audio|seek_to', { position }); |
| 110 | + }); |
| 111 | + }, |
| 112 | + setRate(url: string, rate: number): Promise<void> { |
| 113 | + return serial(async () => { |
| 114 | + if (activeUrl !== url) return; |
| 115 | + plugin = await invoke<PluginState>('plugin:native-audio|set_rate', { rate }); |
| 116 | + }); |
| 117 | + }, |
| 118 | +}; |
0 commit comments