Files
otunes/apps/mobile/hooks/use-native-playlist-sync.ts
T
joshuafhiggins Verified df85978b8b Merge remote-tracking branch 'origin/main' into pr103
# Conflicts:
#	apps/mobile/contexts/playback.tsx
#	apps/web/app/api/song/[id]/play/route.ts
#	apps/web/components/album-view.tsx
#	apps/web/components/media-context-menu.tsx
#	apps/web/components/playlist-view.tsx
#	apps/web/lib/media-links.ts
#	apps/web/lib/server-hls.ts
#	packages/convex/convex/playback.ts
2026-11-16 22:00:41 -05:00

725 lines
22 KiB
TypeScript

import { atmosEnabled } from "@/lib/playback-capabilities"
import {
createAudioPlaylist,
setAudioModeAsync,
setIsAudioActiveAsync,
type AudioMetadata,
type AudioPlaylist,
type AudioPlaylistStatus,
type AudioSource,
} from "expo-audio"
import { useConvex } from "convex/react"
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
import { useCacheScope } from "@/contexts/persistent-cache"
import { useServerConfig } from "@/contexts/server-config"
import { useCachedQuery } from "@/hooks/use-cached-query"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { BrowseSong } from "@otunes/convex/types"
import { playbackStreamPlan } from "@otunes/convex/playback-stream"
import * as playbackQueue from "@otunes/convex/client/playback-queue"
import type { RepeatMode } from "@otunes/convex/client/playback-queue"
import type { PlaybackStore } from "@otunes/convex/client/playback-store"
import { getAuthCookieHeader } from "@/lib/auth-cookies"
import { loadPlaybackSongs } from "@/lib/playback-files"
import { showError } from "@/lib/toast"
import { albumCoverUrl, songAudioSource } from "@/lib/urls"
import {
activatePlaylistLockScreen,
releaseAudioPlaylist,
updatePlaylistLockScreenMetadata,
} from "@/lib/audio-playlist-lock-screen"
import {
nativePlaylistEdits,
nativePlaylistNeedsRebuild,
nativePlaylistSongIds,
repeatToPlaylistLoop,
} from "@/lib/native-playlist-window"
const PLAYBACK_READY_TIMEOUT_MS = 20_000
function lockScreenMetadata(
song: BrowseSong,
serverUrl: string
): AudioMetadata {
return {
title: song.title,
artist: song.artist.name,
albumTitle: song.album.title,
artworkUrl: albumCoverUrl(serverUrl, song.album.id, 512),
}
}
type PlaybackWaiter = { playing?: boolean; resolve: () => void }
/** Where the last sync left the native player, and the queue it reflects. */
type SyncedPosition = {
index: number
songId: Id<"songs"> | undefined
historyLength: number
}
/**
* Keeps the native `AudioPlaylist` in step with the playback queue, and
* feeds its status (position, playing, track changes) back into the queue.
*/
export function useNativePlaylistSync({
store,
queueSongIds,
queueHistorySongIds,
repeatEnabled,
currentSong,
queueStartVersion,
}: {
store: PlaybackStore
queueSongIds: Id<"songs">[]
queueHistorySongIds: Id<"songs">[]
repeatEnabled: RepeatMode
currentSong: BrowseSong | null
queueStartVersion: number
}) {
const convex = useConvex()
const { config } = useServerConfig()
const cacheScope = useCacheScope()
const user = useCachedQuery(api.auth.currentUser)
const storeRef = useRef(store)
storeRef.current = store
const playlistRef = useRef<AudioPlaylist | null>(null)
const statusSubscriptionRef = useRef<{ remove: () => void } | null>(null)
const trackChangedSubscriptionRef = useRef<{ remove: () => void } | null>(
null
)
const loadedSongIdsRef = useRef<Id<"songs">[]>([])
const loadedLoadKeyRef = useRef<string | null>(null)
const syncedPositionRef = useRef<SyncedPosition | null>(null)
const waitersRef = useRef(new Map<Id<"songs">, Set<PlaybackWaiter>>())
const autoPlayNextRef = useRef(false)
const isPlayingRef = useRef(false)
const currentTimeRef = useRef(0)
const pendingSeekRef = useRef<number | null>(null)
const seekInFlightRef = useRef(false)
// Set while `play()` waits for a pending seek to land.
const deferredPlayRef = useRef(false)
const loadRequestIdRef = useRef(0)
const [isPlaying, setIsPlayingState] = useState(false)
const [currentTime, setCurrentTime] = useState(0)
const [measuredDuration, setDuration] = useState(0)
const currentSongId = queueSongIds[0] ?? null
const duration =
measuredDuration > 0
? measuredDuration
: (currentSong?.durationSeconds ?? 0)
const queueSyncKey = useMemo(
() => JSON.stringify(queueSongIds),
[queueSongIds]
)
const historySyncKey = useMemo(
() => JSON.stringify(queueHistorySongIds),
[queueHistorySongIds]
)
const currentSongSyncKey = currentSong
? JSON.stringify([
currentSong.id,
currentSong.title,
currentSong.artist.name,
currentSong.album.id,
currentSong.album.title,
currentSong.album.cover ?? "",
])
: ""
const userPlaybackKey = user
? JSON.stringify([user._id, user.playbackQuality, atmosEnabled(user)])
: ""
const settleWaiters = useCallback(() => {
const player = playlistRef.current
if (waitersRef.current.size === 0 || !player) return
const songId = loadedSongIdsRef.current[player.currentIndex]
const waiters = songId && waitersRef.current.get(songId)
if (!waiters || !player.isLoaded) return
for (const waiter of waiters) {
if (waiter.playing === undefined || player.playing === waiter.playing) {
waiter.resolve()
}
}
}, [])
const markSynced = useCallback(
(playlist: AudioPlaylist, songId: Id<"songs">, historyLength: number) => {
syncedPositionRef.current = {
index: playlist.currentIndex,
songId,
historyLength,
}
settleWaiters()
},
[settleWaiters]
)
const destroyPlaylist = useCallback(() => {
statusSubscriptionRef.current?.remove()
statusSubscriptionRef.current = null
trackChangedSubscriptionRef.current?.remove()
trackChangedSubscriptionRef.current = null
const playlist = playlistRef.current
if (playlist) {
playlist.pause()
releaseAudioPlaylist(playlist)
}
playlistRef.current = null
loadedSongIdsRef.current = []
loadedLoadKeyRef.current = null
syncedPositionRef.current = null
}, [])
const resetPosition = useCallback(() => {
pendingSeekRef.current = null
deferredPlayRef.current = false
currentTimeRef.current = 0
setCurrentTime(0)
setDuration(0)
}, [])
// A new queue must not inherit the native player's position or a pending seek.
const resetForNewQueue = useCallback(() => {
loadRequestIdRef.current += 1
destroyPlaylist()
seekInFlightRef.current = false
resetPosition()
}, [destroyPlaylist, resetPosition])
/**
* Starts the player, unless a seek (a restored position) is still pending.
* Then the status handler starts it once the seek lands, so playback never
* starts at 0:00 and jumps.
*/
const startPlayer = useCallback((playlist: AudioPlaylist) => {
if (pendingSeekRef.current !== null && pendingSeekRef.current > 0) {
deferredPlayRef.current = true
return
}
playlist.play()
}, [])
const handlePlaylistStatusUpdate = useCallback(
(status: AudioPlaylistStatus) => {
settleWaiters()
// iOS may remove the finished item before delivering its final status.
if (!status.isLoaded && !status.didJustFinish) return
const state = storeRef.current.getState()
if (loadedSongIdsRef.current[status.currentIndex] !== state.queue[0]) {
return
}
if (status.duration > 0) {
setDuration(status.duration)
}
if (pendingSeekRef.current !== null && pendingSeekRef.current > 0) {
const seekSeconds =
status.duration > 0
? Math.min(pendingSeekRef.current, status.duration)
: pendingSeekRef.current
pendingSeekRef.current = null
const playlist = playlistRef.current
if (playlist) {
// Statuses report the old position until the seek lands.
seekInFlightRef.current = true
void playlist.seekTo(seekSeconds).finally(() => {
seekInFlightRef.current = false
if (deferredPlayRef.current && playlistRef.current === playlist) {
deferredPlayRef.current = false
playlist.play()
}
})
}
currentTimeRef.current = seekSeconds
setCurrentTime(seekSeconds)
} else if (!seekInFlightRef.current) {
currentTimeRef.current = status.currentTime
setCurrentTime(status.currentTime)
}
if (status.playing) {
autoPlayNextRef.current = false
if (!isPlayingRef.current) {
isPlayingRef.current = true
setIsPlayingState(true)
}
} else if (
isPlayingRef.current &&
!status.didJustFinish &&
!deferredPlayRef.current
) {
autoPlayNextRef.current = false
isPlayingRef.current = false
setIsPlayingState(false)
}
if (!status.didJustFinish) return
if (state.repeatEnabled === "once") return
if (status.currentIndex < status.trackCount - 1) return
const shouldContinue =
state.repeatEnabled !== "none" || state.queue.length > 1
if (!shouldContinue) {
autoPlayNextRef.current = false
isPlayingRef.current = false
setIsPlayingState(false)
storeRef.current.update(playbackQueue.skipNext)
const playlist = playlistRef.current
if (playlist) {
playlist.pause()
// Reload the final item, which iOS removes when playback ends. The
// index doesn't move, so handleTrackChanged treats it as ours.
playlist.skipTo(status.currentIndex)
}
currentTimeRef.current = 0
setCurrentTime(0)
return
}
autoPlayNextRef.current = true
isPlayingRef.current = true
setIsPlayingState(true)
storeRef.current.update(playbackQueue.skipNext)
if (storeRef.current.getState().queue[0] === state.queue[0]) {
const playlist = playlistRef.current
void playlist?.seekTo(0)
playlist?.play()
}
},
[settleWaiters]
)
const handleTrackChanged = useCallback(() => {
const playlist = playlistRef.current
const synced = syncedPositionRef.current
if (!playlist || !synced) return
// Queue edits run synchronously, so an event can describe an index from
// before them (Android's `previousIndex` also goes stale). The player's
// live index is the one that matters; if it is where the last sync left
// it, this change was ours.
const index = playlist.currentIndex
if (index === synced.index) return
const state = storeRef.current.getState()
// A queue change is still being applied and will move the player itself.
if (
state.queue[0] !== synced.songId ||
state.history.length !== synced.historyLength
) {
return
}
// The window is history then queue, so the current track sits at
// `history.length`.
const steps = index - state.history.length
if (steps === 0) return
autoPlayNextRef.current = true
isPlayingRef.current = true
setIsPlayingState(true)
storeRef.current.update((state) => {
if (steps === 1) return playbackQueue.skipNext(state)
if (steps > 1) return playbackQueue.skipToQueueIndex(state, steps)
let next = state
for (let i = 0; i < -steps; i++) {
next = playbackQueue.skipPrevious(next)
}
return next
})
const next = storeRef.current.getState()
syncedPositionRef.current = {
index,
songId: next.queue[0],
historyLength: next.history.length,
}
settleWaiters()
}, [settleWaiters])
const ensurePlaylist = useCallback(
(sources: AudioSource[]) => {
if (playlistRef.current) {
return playlistRef.current
}
const playlist = createAudioPlaylist({
sources,
updateInterval: 500,
loop: repeatToPlaylistLoop(storeRef.current.getState().repeatEnabled),
})
// System volume controls loudness; keep the player gain at full scale.
playlist.volume = 1
statusSubscriptionRef.current = playlist.addListener(
"playlistStatusUpdate",
handlePlaylistStatusUpdate
)
trackChangedSubscriptionRef.current = playlist.addListener(
"trackChanged",
handleTrackChanged
)
playlistRef.current = playlist
return playlist
},
[handlePlaylistStatusUpdate, handleTrackChanged]
)
useEffect(() => {
let cancelled = false
void setAudioModeAsync({
playsInSilentMode: true,
shouldPlayInBackground: true,
interruptionMode: "doNotMix",
}).catch(() => {
if (!cancelled) {
showError("Audio playback could not be initialized.")
}
})
return () => {
cancelled = true
destroyPlaylist()
}
}, [destroyPlaylist])
useEffect(() => {
const playlist = playlistRef.current
if (playlist) {
playlist.loop = repeatToPlaylistLoop(repeatEnabled)
}
}, [repeatEnabled])
const setIsPlaying = useCallback(
(playing: boolean) => {
isPlayingRef.current = playing
setIsPlayingState(playing)
const playlist = playlistRef.current
if (!playlist) return
if (playing) {
startPlayer(playlist)
} else {
deferredPlayRef.current = false
playlist.pause()
}
},
[startPlayer]
)
const seekTo = useCallback(
async (seconds: number) => {
const playlist = playlistRef.current
if (!playlist) return
const maxDuration = playlist.duration || duration || 0
const clamped = Math.max(0, Math.min(seconds, maxDuration))
await playlist.seekTo(clamped)
currentTimeRef.current = clamped
setCurrentTime(clamped)
},
[duration]
)
const handlePlaybackLoadFailure = useCallback(() => {
autoPlayNextRef.current = false
isPlayingRef.current = false
destroyPlaylist()
setIsPlayingState(false)
showError("Could not play this track. The queue has been cleared.")
storeRef.current.update(playbackQueue.clearQueue)
}, [destroyPlaylist])
useEffect(() => {
const loadRequestId = ++loadRequestIdRef.current
const isCurrentLoad = () => loadRequestIdRef.current === loadRequestId
async function syncPlaylist() {
if (!config || !currentSongId || !user) {
if (!currentSongId) {
destroyPlaylist()
}
return
}
const windowSongIds = nativePlaylistSongIds(
queueHistorySongIds,
queueSongIds
)
const currentIndex = queueHistorySongIds.length
const cookieHeader = await getAuthCookieHeader(
config.serverUrl,
config.convexUrl
)
if (!cookieHeader) {
showError("Your session has expired. Please sign in again.")
return
}
if (!isCurrentLoad()) return
const loadKey = `${user.playbackQuality}:${atmosEnabled(user)}:${cookieHeader}`
const needsRebuild =
loadedLoadKeyRef.current !== loadKey ||
nativePlaylistNeedsRebuild(
loadedSongIdsRef.current,
queueHistorySongIds,
queueSongIds
)
const metadata = currentSong
? lockScreenMetadata(currentSong, config.serverUrl)
: null
const shouldPlay = isPlayingRef.current || autoPlayNextRef.current
if (!needsRebuild) {
const playlist = playlistRef.current
if (!playlist) return
if (playlist.currentIndex !== currentIndex) {
playlist.skipTo(currentIndex)
}
playlist.loop = repeatToPlaylistLoop(repeatEnabled)
if (metadata) {
updatePlaylistLockScreenMetadata(playlist, metadata)
}
markSynced(playlist, currentSongId, currentIndex)
if (shouldPlay) {
isPlayingRef.current = true
setIsPlayingState(true)
startPlayer(playlist)
}
return
}
const buildSources = async (songIds: Id<"songs">[]) => {
const songs = await loadPlaybackSongs(convex, cacheScope, songIds)
const sources = new Map<Id<"songs">, AudioSource>()
for (const song of songs) {
if (!song) return null
const plan = playbackStreamPlan(
song.files,
user.playbackQuality,
atmosEnabled(user)
)
sources.set(
song.id,
songAudioSource(
config.serverUrl,
song.id,
plan.playbackQuality,
plan.preferAtmos,
plan.useHls,
{ cookieHeader, songTitle: song.title }
)
)
}
return sources
}
// Queue and history edits around the playing track are applied in place
// so playback isn't interrupted.
const loadedPlaylist = playlistRef.current
const edits =
loadedPlaylist && loadedLoadKeyRef.current === loadKey
? nativePlaylistEdits(
loadedSongIdsRef.current,
loadedPlaylist.currentIndex,
windowSongIds,
currentIndex
)
: null
if (loadedPlaylist && edits) {
const addedSongIds = edits.flatMap((edit) =>
edit.type === "remove" ? [] : [edit.songId]
)
const sources = await buildSources([...new Set(addedSongIds)])
if (!isCurrentLoad()) return
if (!sources) {
handlePlaybackLoadFailure()
return
}
for (const edit of edits) {
if (edit.type === "remove") {
loadedPlaylist.remove(edit.index)
} else if (edit.type === "insert") {
loadedPlaylist.insert(sources.get(edit.songId)!, edit.index)
} else {
loadedPlaylist.add(sources.get(edit.songId)!)
}
}
loadedSongIdsRef.current = windowSongIds
loadedPlaylist.loop = repeatToPlaylistLoop(repeatEnabled)
markSynced(loadedPlaylist, currentSongId, currentIndex)
if (shouldPlay && !loadedPlaylist.playing) {
isPlayingRef.current = true
setIsPlayingState(true)
startPlayer(loadedPlaylist)
}
return
}
const sourcesById = await buildSources(windowSongIds)
if (!isCurrentLoad()) return
if (!sourcesById) {
handlePlaybackLoadFailure()
return
}
const sources = windowSongIds.map((songId) => sourcesById.get(songId)!)
// Keep the position when only the stream settings changed.
const resumeSeconds =
loadedPlaylist &&
loadedSongIdsRef.current[loadedPlaylist.currentIndex] === currentSongId
? currentTimeRef.current
: null
destroyPlaylist()
if (resumeSeconds !== null && resumeSeconds > 0) {
pendingSeekRef.current = resumeSeconds
}
// Start at the current track and put history in front of it, so an
// earlier song is never loaded first.
const playlist = ensurePlaylist(sources.slice(currentIndex))
for (let index = 0; index < currentIndex; index++) {
playlist.insert(sources[index], index)
}
loadedSongIdsRef.current = windowSongIds
loadedLoadKeyRef.current = loadKey
playlist.loop = repeatToPlaylistLoop(repeatEnabled)
activatePlaylistLockScreen(
playlist,
metadata ?? { title: currentSong?.title }
)
markSynced(playlist, currentSongId, currentIndex)
if (shouldPlay) {
isPlayingRef.current = true
setIsPlayingState(true)
startPlayer(playlist)
} else {
playlist.pause()
// Enable audio for the restored queue without starting playback.
try {
await setIsAudioActiveAsync(true)
} catch {
if (isCurrentLoad()) {
showError("Audio playback could not be initialized.")
}
}
}
}
void syncPlaylist().catch(() => {
if (!isCurrentLoad()) return
handlePlaybackLoadFailure()
})
return () => {
if (loadRequestIdRef.current === loadRequestId) {
loadRequestIdRef.current += 1
}
}
}, [
cacheScope,
config,
convex,
currentSongId,
currentSongSyncKey,
destroyPlaylist,
ensurePlaylist,
handlePlaybackLoadFailure,
historySyncKey,
markSynced,
queueSyncKey,
queueStartVersion,
repeatEnabled,
startPlayer,
userPlaybackKey,
])
/**
* Resolves once the native player has `songId` loaded (and playing or
* paused, if given). Rejects if the queue moves to another song first.
*/
const waitForSong = useCallback(
(songId: Id<"songs">, playing?: boolean) =>
new Promise<void>((resolve, reject) => {
const store = storeRef.current
const waiters = waitersRef.current
let songWaiters = waiters.get(songId)
if (!songWaiters) {
songWaiters = new Set()
waiters.set(songId, songWaiters)
}
const finish = (error?: Error) => {
clearTimeout(timeout)
unsubscribe()
songWaiters.delete(waiter)
if (songWaiters.size === 0 && waiters.get(songId) === songWaiters) {
waiters.delete(songId)
}
if (error) reject(error)
else resolve()
}
const waiter: PlaybackWaiter = { playing, resolve: () => finish() }
const timeout = setTimeout(
() =>
finish(
new Error(
"The audio did not become ready. Check your connection and try again."
)
),
PLAYBACK_READY_TIMEOUT_MS
)
// Starting a queue clears it and fills it in one go; only the
// settled queue counts.
const unsubscribe = store.subscribe(() => {
queueMicrotask(() => {
if (store.getState().queue[0] !== songId) {
finish(
new Error(
"The playback queue changed before the request finished."
)
)
}
})
})
songWaiters.add(waiter)
settleWaiters()
}),
[settleWaiters]
)
/**
* Starts the current song at `seconds` once it loads, and shows that
* position right away. Playback waits for the seek (see `startPlayer`).
*/
const queueSeek = useCallback((seconds: number) => {
pendingSeekRef.current = seconds
currentTimeRef.current = seconds
setCurrentTime(seconds)
}, [])
const hasPlayer = useCallback(() => playlistRef.current !== null, [])
const requestAutoPlay = useCallback(() => {
autoPlayNextRef.current = true
}, [])
return {
isPlaying,
setIsPlaying,
currentTime,
currentTimeRef,
duration,
seekTo,
queueSeek,
hasPlayer,
requestAutoPlay,
resetPosition,
resetForNewQueue,
waitForSong,
}
}