Files
otunes/apps/web/components/playback-provider.tsx
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

1499 lines
39 KiB
TypeScript

"use client"
import {
useState,
useEffect,
useCallback,
useRef,
RefObject,
createContext,
useContext,
ReactNode,
useSyncExternalStore,
useMemo,
} from "react"
import { useConvex, useMutation, useQuery } from "convex/react"
import type Hls from "hls.js"
import { toast } from "sonner"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { BrowseSong } from "@otunes/convex/types"
import {
playbackFormat,
type PlaybackFormat,
type PlaybackQuality,
playbackStreamPlan,
songHlsPlaylistPath,
songPlayPath,
} from "@otunes/convex/playback-stream"
import {
ANONYMOUS_CACHE_SCOPE,
useCacheScope,
} from "@/components/persistent-cache-provider"
import { useCachedQuery } from "@/hooks/use-cached-query"
import { useMediaSession } from "@/hooks/use-media-session"
import { fetchCachedQuery } from "@otunes/convex/client/cached-query"
import { supportsDolbyAtmosPlayback } from "@/lib/dolby-atmos-support"
import { flushPersistentCache } from "@/lib/persistent-cache"
import * as playbackQueue from "@otunes/convex/client/playback-queue"
import type { RepeatMode } from "@otunes/convex/client/playback-queue"
import {
getPlaybackStore,
getServerPlaybackSnapshot,
} from "@otunes/convex/client/playback-store"
import { rememberSongs, useSong } from "@/lib/song-cache"
const PREVIOUS_TRACK_RESTART_THRESHOLD_SECONDS = 5
const PROGRESS_SAVE_INTERVAL_MS = 5000
type PlaybackContextValue = {
queueSongIds: Id<"songs">[]
queueHistorySongIds: Id<"songs">[]
currentFormat: PlaybackFormat | null
currentSong: BrowseSong | null
shuffleEnabled: boolean
repeatEnabled: RepeatMode
queueOpen: boolean
isPlaying: boolean
volume: number
setQueueOpen: (open: boolean) => void
toggleQueueOpen: () => void
setIsPlaying: (playing: boolean) => void
setVolume: (volume: number) => void
playInOrder: (songIds: Id<"songs">[]) => Promise<void>
playShuffled: (songIds: Id<"songs">[]) => Promise<void>
addToQueueNext: (songIds: Id<"songs">[]) => Promise<void>
addToQueueLast: (songIds: Id<"songs">[]) => Promise<void>
playFromSong: (
songId: Id<"songs">,
songIds: Id<"songs">[],
origin?: string
) => Promise<void>
playFromLibrarySong: (
songId: Id<"songs">,
sort: {
sortKey: "title" | "artist" | "album"
sortDirection: "asc" | "desc"
}
) => Promise<void>
/** Pass `songIds` when the caller already has the album's tracks. */
playAlbum: (albumId: Id<"albums">, songIds?: Id<"songs">[]) => Promise<void>
playAlbumShuffled: (
albumId: Id<"albums">,
songIds?: Id<"songs">[]
) => Promise<void>
playArtist: (artistId: Id<"artists">) => Promise<void>
playArtistShuffled: (artistId: Id<"artists">) => Promise<void>
/** Pass `songIds` when the caller already has the playlist's songs. */
playPlaylist: (
playlistId: Id<"playlists">,
songIds?: Id<"songs">[]
) => Promise<void>
playPlaylistShuffled: (
playlistId: Id<"playlists">,
songIds?: Id<"songs">[]
) => Promise<void>
toggleShuffle: () => Promise<void>
cycleRepeat: () => Promise<void>
clearQueue: () => Promise<void>
reorderQueue: (songIds: Id<"songs">[]) => Promise<void>
skipToQueueIndex: (index: number) => Promise<void>
skipNext: () => Promise<void>
skipPrevious: () => Promise<void>
dolbyAtmosSupported: boolean | null
}
type PlaybackActions = Omit<
PlaybackContextValue,
| "queueSongIds"
| "queueHistorySongIds"
| "currentFormat"
| "currentSong"
| "shuffleEnabled"
| "repeatEnabled"
| "queueOpen"
| "isPlaying"
| "volume"
| "dolbyAtmosSupported"
>
const PlaybackContext = createContext<PlaybackContextValue | null>(null)
// Long lists read these narrow contexts so playback state changes do not
// re-render every row.
const PlaybackActionsContext = createContext<PlaybackActions | null>(null)
const CurrentSongIdContext = createContext<Id<"songs"> | null>(null)
const IsPlayingContext = createContext(false)
const PlaybackProgressContext = createContext<{
currentTime: number
duration: number
seekTo: (seconds: number) => void
} | null>(null)
const VOLUME_STORAGE_KEY = "otunes:volume"
const DEFAULT_VOLUME = 80
const volumeListeners = new Set<() => void>()
let dolbyAtmosSnapshot: boolean | undefined
function subscribePlaybackCapabilities() {
return () => {}
}
function getDolbyAtmosSnapshot() {
dolbyAtmosSnapshot ??= supportsDolbyAtmosPlayback(new Audio())
return dolbyAtmosSnapshot
}
function subscribeVolume(listener: () => void) {
volumeListeners.add(listener)
const onStorage = (event: StorageEvent) => {
if (event.key === null || event.key === VOLUME_STORAGE_KEY) {
listener()
}
}
window.addEventListener("storage", onStorage)
return () => {
volumeListeners.delete(listener)
window.removeEventListener("storage", onStorage)
}
}
function notifyVolumeChange() {
for (const listener of volumeListeners) {
listener()
}
}
function readStoredVolume(): number {
try {
const stored = localStorage.getItem(VOLUME_STORAGE_KEY)
if (stored === null) {
return DEFAULT_VOLUME
}
const parsed = Number(stored)
if (!Number.isFinite(parsed)) {
return DEFAULT_VOLUME
}
return Math.min(100, Math.max(0, Math.round(parsed)))
} catch {
return DEFAULT_VOLUME
}
}
type PlaybackLoadSettings = {
preferAtmos: boolean
playbackQuality: PlaybackQuality
useHls: boolean
}
function playbackLoadSettings(
user: {
preferAtmos?: boolean
playbackQuality?: PlaybackQuality
},
dolbyAtmosSupported: boolean | null,
files: Record<string, unknown>
): PlaybackLoadSettings {
const preferAtmos =
dolbyAtmosSupported === true && (user.preferAtmos ?? false)
const plan = playbackStreamPlan(files, user.playbackQuality, preferAtmos)
return {
preferAtmos: plan.preferAtmos,
playbackQuality: plan.playbackQuality,
useHls: plan.useHls,
}
}
function songLoadKey(songId: string, settings: PlaybackLoadSettings) {
return `${songId}:${settings.preferAtmos}:${settings.playbackQuality}:${settings.useHls ? "hls" : "file"}`
}
function destroyHlsInstance(hlsRef: RefObject<Hls | null>) {
hlsRef.current?.destroy()
hlsRef.current = null
}
function adoptPreloadedPlayback(
mainAudio: HTMLAudioElement,
mainHlsRef: RefObject<Hls | null>,
preloadAudio: HTMLAudioElement,
preloadHlsRef: RefObject<Hls | null>
) {
const preloadHls = preloadHlsRef.current
if (preloadHls) {
preloadHls.detachMedia()
preloadHlsRef.current = null
mainHlsRef.current = preloadHls
preloadHls.attachMedia(mainAudio)
} else {
mainAudio.src = preloadAudio.src
}
preloadAudio.pause()
preloadAudio.removeAttribute("src")
}
type AudioSourceCleanup = () => void
function loadAudioSource(
audio: HTMLAudioElement,
hlsRef: RefObject<Hls | null>,
songId: string,
settings: PlaybackLoadSettings,
onReady: () => void
): AudioSourceCleanup {
let disposed = false
let hlsInstance: Hls | null = null
const directUrl = songPlayPath(
songId,
settings.playbackQuality,
settings.preferAtmos
)
const hlsUrl = songHlsPlaylistPath(
songId,
settings.playbackQuality,
settings.preferAtmos
)
const onReadyToPlay = () => {
audio.removeEventListener("loadedmetadata", onReadyToPlay)
audio.removeEventListener("loadeddata", onReadyToPlay)
audio.removeEventListener("canplay", onReadyToPlay)
onReady()
}
audio.addEventListener("loadedmetadata", onReadyToPlay)
audio.addEventListener("loadeddata", onReadyToPlay)
audio.addEventListener("canplay", onReadyToPlay)
const loadDirectSource = () => {
if (disposed) {
return
}
audio.src = directUrl
audio.load()
}
if (settings.useHls && audio.canPlayType("application/vnd.apple.mpegurl")) {
audio.src = hlsUrl
audio.load()
} else if (settings.useHls) {
void import("hls.js")
.then(({ default: HlsConstructor }) => {
if (disposed) {
return
}
if (!HlsConstructor.isSupported()) {
loadDirectSource()
return
}
const hls = new HlsConstructor()
hlsInstance = hls
hlsRef.current = hls
hls.on(HlsConstructor.Events.MANIFEST_PARSED, onReady)
hls.loadSource(hlsUrl)
hls.attachMedia(audio)
})
.catch(loadDirectSource)
} else {
loadDirectSource()
}
return () => {
disposed = true
audio.removeEventListener("loadedmetadata", onReadyToPlay)
audio.removeEventListener("loadeddata", onReadyToPlay)
audio.removeEventListener("canplay", onReadyToPlay)
if (hlsInstance && hlsRef.current === hlsInstance) {
hlsInstance.destroy()
hlsRef.current = null
}
}
}
export function usePlayback() {
const context = useContext(PlaybackContext)
if (!context) {
throw new Error("usePlayback must be used within PlaybackProvider")
}
return context
}
export function usePlaybackActions() {
const context = useContext(PlaybackActionsContext)
if (!context) {
throw new Error("usePlaybackActions must be used within PlaybackProvider")
}
return context
}
export function useCurrentSongId() {
return useContext(CurrentSongIdContext)
}
export function useIsPlaying() {
return useContext(IsPlayingContext)
}
export function usePlaybackProgress() {
const context = useContext(PlaybackProgressContext)
if (!context) {
throw new Error(
"usePlaybackProgress must be used within PlaybackProvider"
)
}
return context
}
export function PlaybackProvider({ children }: { children: ReactNode }) {
const convex = useConvex()
const cacheScope = useCacheScope()
const isSignedIn = cacheScope !== ANONYMOUS_CACHE_SCOPE
const store = useMemo(
() => getPlaybackStore(isSignedIn ? cacheScope : null),
[cacheScope, isSignedIn]
)
const playback = useSyncExternalStore(
store.subscribe,
store.getSnapshot,
getServerPlaybackSnapshot
)
const user = useCachedQuery(api.auth.currentUser)
const serverPlayback = useQuery(
api.playback.getPlaybackState,
isSignedIn ? {} : "skip"
)
const syncPlaybackStateMutation = useMutation(
api.playback.syncPlaybackState
)
const recordPlayMutation = useMutation(api.playback.recordPlay)
const updateSongProgressMutation = useMutation(api.playback.updateSongProgress)
useEffect(() => {
if (!isSignedIn) {
return
}
store.setTransport({
sync: (state) =>
syncPlaybackStateMutation({
songQueue: state.queue,
queueHistory: state.history,
shuffleEnabled: state.shuffleEnabled,
repeatEnabled: state.repeatEnabled,
currentSongProgress: state.progressSeconds,
}),
loadProgress: () => convex.query(api.playback.getSongProgress, {}),
})
return () => store.setTransport(null)
}, [store, isSignedIn, syncPlaybackStateMutation, convex])
useEffect(() => {
if (!serverPlayback) {
return
}
if (serverPlayback.currentSong) {
rememberSongs([serverPlayback.currentSong])
}
store.receiveServerState(serverPlayback)
}, [store, serverPlayback])
const recordPlay = useCallback(
(args: { songId?: Id<"songs">; playlistId?: Id<"playlists"> }) => {
void recordPlayMutation(args).catch(() => {})
},
[recordPlayMutation]
)
const audioRef = useRef<HTMLAudioElement | null>(null)
const hlsRef = useRef<Hls | null>(null)
const preloadAudioRef = useRef<HTMLAudioElement | null>(null)
const preloadHlsRef = useRef<Hls | null>(null)
const loadedSongKeyRef = useRef<string | null>(null)
const preloadedSongKeyRef = useRef<string | null>(null)
const playbackErrorHandledRef = useRef(false)
const isPlayingRef = useRef(false)
const autoPlayNextRef = useRef(false)
const intentionalLoadRef = useRef(false)
const ignoreAudioSyncRef = useRef(false)
const progressRestoredForSongRef = useRef<Id<"songs"> | null>(null)
const lastSavedProgressRef = useRef(-1)
const prevIsPlayingRef = useRef(false)
const currentTimeRef = useRef(0)
const durationRef = useRef(0)
const [queueOpen, setQueueOpen] = useState(false)
const [isPlaying, setIsPlaying] = useState(false)
const [playbackStartVersion, setPlaybackStartVersion] = useState(0)
const [currentTime, setCurrentTime] = useState(0)
const [measuredDuration, setMeasuredDuration] = useState<{
songId: string
seconds: number
} | null>(null)
const [progressToRestore, setProgressToRestore] = useState<number | null>(
null
)
const dolbyAtmosSupported = useSyncExternalStore(
subscribePlaybackCapabilities,
getDolbyAtmosSnapshot,
() => null
)
const volume = useSyncExternalStore(
subscribeVolume,
readStoredVolume,
() => DEFAULT_VOLUME
)
const setVolume = useCallback((nextVolume: number) => {
const clamped = Math.min(100, Math.max(0, Math.round(nextVolume)))
try {
localStorage.setItem(VOLUME_STORAGE_KEY, String(clamped))
notifyVolumeChange()
} catch {
// Ignore storage failures (private browsing, quota, etc.)
}
}, [])
const queueSongIds = playback.queue
const currentSongId = queueSongIds[0] ?? null
const currentSong = useSong(currentSongId)
const playbackQuality = user?.playbackQuality
const preferAtmos = user?.preferAtmos
const duration =
currentSongId && measuredDuration?.songId === currentSongId
? measuredDuration.seconds
: (currentSong?.durationSeconds ?? 0)
useEffect(() => {
durationRef.current = duration
}, [duration])
const currentSongDoc = useCachedQuery(
api.library.getSongPlaybackInfo,
currentSongId ? { id: currentSongId } : "skip"
)
const isPlaybackActive = Boolean(currentSongId && isPlaying)
const repeatEnabled = playback.repeatEnabled
const queueLength = queueSongIds.length
const nextQueueSongId =
queueSongIds.length > 1 ? queueSongIds[1] : null
const nextQueueSongDoc = useCachedQuery(
api.library.getSongPlaybackInfo,
nextQueueSongId ? { id: nextQueueSongId } : "skip"
)
const seekTo = useCallback(
(seconds: number) => {
const audio = audioRef.current
if (!audio) {
return
}
const maxDuration =
Number.isFinite(audio.duration) && audio.duration > 0
? audio.duration
: durationRef.current
const clamped = Math.max(0, Math.min(seconds, maxDuration || 0))
audio.currentTime = clamped
currentTimeRef.current = clamped
setCurrentTime(clamped)
},
[]
)
const saveProgress = useCallback(
(seconds: number) => {
if (!currentSongId) {
return
}
const progressSeconds = Math.max(0, Math.floor(seconds))
if (progressSeconds === lastSavedProgressRef.current) {
return
}
lastSavedProgressRef.current = progressSeconds
store.setProgress(currentSongId, progressSeconds)
if (isSignedIn) {
void updateSongProgressMutation({
songId: currentSongId,
progressSeconds,
}).catch(() => {})
}
},
[currentSongId, store, isSignedIn, updateSongProgressMutation]
)
useEffect(() => {
if (!currentSongId) {
return
}
if (progressRestoredForSongRef.current === currentSongId) {
return
}
progressRestoredForSongRef.current = currentSongId
lastSavedProgressRef.current = -1
setProgressToRestore(store.getState().progressSeconds)
}, [currentSongId, store])
useEffect(() => {
if (progressToRestore === null || progressToRestore <= 0) {
return
}
const audio = audioRef.current
if (!audio || !currentSongId) {
return
}
const applyProgress = () => {
const maxDuration =
Number.isFinite(audio.duration) && audio.duration > 0
? audio.duration
: progressToRestore
const clamped = Math.min(progressToRestore, maxDuration)
audio.currentTime = clamped
currentTimeRef.current = clamped
setCurrentTime(clamped)
setProgressToRestore(null)
}
if (audio.readyState >= HTMLMediaElement.HAVE_METADATA) {
applyProgress()
return
}
audio.addEventListener("loadedmetadata", applyProgress, { once: true })
return () => {
audio.removeEventListener("loadedmetadata", applyProgress)
}
}, [progressToRestore, currentSongId])
useEffect(() => {
const audio = audioRef.current
if (!audio || !currentSongId) {
return
}
if (prevIsPlayingRef.current === isPlaying) {
return
}
prevIsPlayingRef.current = isPlaying
saveProgress(audio.currentTime)
}, [isPlaying, currentSongId, saveProgress])
useEffect(() => {
if (!isPlaying || !currentSongId) {
return
}
const interval = window.setInterval(() => {
const audio = audioRef.current
if (!audio) {
return
}
saveProgress(audio.currentTime)
}, PROGRESS_SAVE_INTERVAL_MS)
return () => {
window.clearInterval(interval)
}
}, [isPlaying, currentSongId, saveProgress])
useEffect(() => {
const persistNow = () => {
const audio = audioRef.current
const restorePending = progressToRestore !== null && progressToRestore > 0
if (audio && currentSongId && !restorePending) {
saveProgress(audio.currentTime)
}
flushPersistentCache()
}
const onVisibilityChange = () => {
if (document.visibilityState === "hidden") {
persistNow()
}
}
window.addEventListener("pagehide", persistNow)
document.addEventListener("visibilitychange", onVisibilityChange)
return () => {
window.removeEventListener("pagehide", persistNow)
document.removeEventListener("visibilitychange", onVisibilityChange)
}
}, [currentSongId, progressToRestore, saveProgress])
useEffect(() => {
isPlayingRef.current = isPlaying
}, [isPlaying])
const runWithoutAudioSync = useCallback((fn: () => void) => {
ignoreAudioSyncRef.current = true
try {
fn()
} finally {
ignoreAudioSyncRef.current = false
}
}, [])
const destroyHls = useCallback(() => {
destroyHlsInstance(hlsRef)
}, [])
const destroyPreloadHls = useCallback(() => {
destroyHlsInstance(preloadHlsRef)
}, [])
const clearPreload = useCallback(() => {
const preloadAudio = preloadAudioRef.current
destroyPreloadHls()
preloadedSongKeyRef.current = null
if (preloadAudio) {
preloadAudio.pause()
preloadAudio.removeAttribute("src")
}
}, [destroyPreloadHls])
useEffect(() => {
const audio = new Audio()
const preloadAudio = new Audio()
preloadAudio.preload = "auto"
audioRef.current = audio
preloadAudioRef.current = preloadAudio
const syncPlayingFromAudio = () => {
if (ignoreAudioSyncRef.current) {
return
}
if (autoPlayNextRef.current && audio.paused) {
return
}
setIsPlaying(!audio.paused)
}
audio.addEventListener("play", syncPlayingFromAudio)
audio.addEventListener("pause", syncPlayingFromAudio)
return () => {
audio.removeEventListener("play", syncPlayingFromAudio)
audio.removeEventListener("pause", syncPlayingFromAudio)
destroyHls()
destroyPreloadHls()
audio.pause()
audio.removeAttribute("src")
preloadAudio.pause()
preloadAudio.removeAttribute("src")
audioRef.current = null
preloadAudioRef.current = null
loadedSongKeyRef.current = null
preloadedSongKeyRef.current = null
}
}, [destroyHls, destroyPreloadHls])
useEffect(() => {
const audio = audioRef.current
if (!audio) {
return
}
audio.volume = volume / 100
}, [volume])
const attemptPlayback = useCallback(
(
audio: HTMLAudioElement,
expectedLoadKey: string,
playState: { playPending: boolean; playSucceeded: boolean }
) => {
if (
!(isPlayingRef.current || autoPlayNextRef.current) ||
playState.playPending ||
playState.playSucceeded
) {
return
}
playState.playPending = true
void audio
.play()
.then(() => {
playState.playPending = false
playState.playSucceeded = true
intentionalLoadRef.current = false
autoPlayNextRef.current = false
isPlayingRef.current = true
setIsPlaying(true)
})
.catch((error: unknown) => {
playState.playPending = false
if (loadedSongKeyRef.current !== expectedLoadKey) {
return
}
if (error instanceof DOMException && error.name === "AbortError") {
return
}
intentionalLoadRef.current = false
autoPlayNextRef.current = false
isPlayingRef.current = false
setIsPlaying(false)
})
},
[]
)
useEffect(() => {
const audio = audioRef.current
const preloadAudio = preloadAudioRef.current
if (!audio) {
return
}
if (!currentSongId) {
autoPlayNextRef.current = false
intentionalLoadRef.current = false
destroyHls()
clearPreload()
audio.pause()
audio.removeAttribute("src")
loadedSongKeyRef.current = null
return
}
if (!user || !currentSongDoc) {
return
}
const settings = playbackLoadSettings(
{ playbackQuality, preferAtmos },
dolbyAtmosSupported,
currentSongDoc.files
)
const loadKey = songLoadKey(currentSongId, settings)
if (loadedSongKeyRef.current === loadKey) {
return
}
playbackErrorHandledRef.current = false
intentionalLoadRef.current = true
runWithoutAudioSync(() => {
audio.pause()
})
const playState = { playPending: false, playSucceeded: false }
const requestPlay = () => {
if (!(isPlayingRef.current || autoPlayNextRef.current)) {
intentionalLoadRef.current = false
return
}
attemptPlayback(audio, loadKey, playState)
}
if (
preloadAudio &&
preloadedSongKeyRef.current === loadKey &&
(preloadHlsRef.current !== null || preloadAudio.src !== "")
) {
destroyHls()
adoptPreloadedPlayback(audio, hlsRef, preloadAudio, preloadHlsRef)
loadedSongKeyRef.current = loadKey
preloadedSongKeyRef.current = null
requestPlay()
return () => {
intentionalLoadRef.current = false
}
}
destroyHls()
loadedSongKeyRef.current = loadKey
const cleanupSource = loadAudioSource(
audio,
hlsRef,
currentSongId,
settings,
requestPlay
)
return () => {
intentionalLoadRef.current = false
cleanupSource()
}
}, [
currentSongId,
currentSongDoc,
user,
playbackQuality,
preferAtmos,
dolbyAtmosSupported,
runWithoutAudioSync,
destroyHls,
clearPreload,
attemptPlayback,
])
useEffect(() => {
const preloadAudio = preloadAudioRef.current
if (!preloadAudio || !nextQueueSongId || !currentSongId) {
clearPreload()
return
}
if (!user || !nextQueueSongDoc) {
return
}
const settings = playbackLoadSettings(
{ playbackQuality, preferAtmos },
dolbyAtmosSupported,
nextQueueSongDoc.files
)
const loadKey = songLoadKey(nextQueueSongId, settings)
if (preloadedSongKeyRef.current === loadKey) {
return
}
clearPreload()
const onPreloadReady = () => {
if (preloadedSongKeyRef.current !== null) {
return
}
preloadedSongKeyRef.current = loadKey
}
const cleanupSource = loadAudioSource(
preloadAudio,
preloadHlsRef,
nextQueueSongId,
settings,
onPreloadReady
)
return () => {
cleanupSource()
if (preloadedSongKeyRef.current === loadKey) {
preloadedSongKeyRef.current = null
}
destroyPreloadHls()
preloadAudio.pause()
preloadAudio.removeAttribute("src")
}
}, [
currentSongId,
nextQueueSongId,
nextQueueSongDoc,
user,
playbackQuality,
preferAtmos,
dolbyAtmosSupported,
clearPreload,
destroyPreloadHls,
])
useEffect(() => {
const audio = audioRef.current
if (!audio || !currentSongId) {
return
}
if (!isPlaying && !autoPlayNextRef.current) {
audio.pause()
return
}
const expectedLoadKey = loadedSongKeyRef.current
if (!expectedLoadKey) {
return
}
const playState = { playPending: false, playSucceeded: false }
if (audio.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA) {
attemptPlayback(audio, expectedLoadKey, playState)
return
}
const onCanPlay = () => {
audio.removeEventListener("canplay", onCanPlay)
attemptPlayback(audio, expectedLoadKey, playState)
}
audio.addEventListener("canplay", onCanPlay)
return () => {
audio.removeEventListener("canplay", onCanPlay)
}
}, [isPlaying, currentSongId, playbackStartVersion, attemptPlayback])
useEffect(() => {
const audio = audioRef.current
if (!audio) {
return
}
const handleEnded = () => {
const loopsSingleSong = repeatEnabled === "always" && queueLength === 1
if (repeatEnabled === "once" || loopsSingleSong) {
audio.currentTime = 0
void audio.play()
return
}
const shouldContinuePlaying = repeatEnabled !== "none" || queueLength > 1
if (shouldContinuePlaying) {
autoPlayNextRef.current = true
isPlayingRef.current = true
setIsPlaying(true)
}
const preloadAudio = preloadAudioRef.current
if (shouldContinuePlaying && preloadAudio && nextQueueSongId && user && nextQueueSongDoc) {
const settings = playbackLoadSettings(
user,
dolbyAtmosSupported,
nextQueueSongDoc.files
)
const nextLoadKey = songLoadKey(nextQueueSongId, settings)
if (
preloadedSongKeyRef.current === nextLoadKey &&
(preloadHlsRef.current !== null || preloadAudio.src !== "")
) {
intentionalLoadRef.current = true
runWithoutAudioSync(() => {
audio.pause()
})
destroyHls()
adoptPreloadedPlayback(audio, hlsRef, preloadAudio, preloadHlsRef)
loadedSongKeyRef.current = nextLoadKey
preloadedSongKeyRef.current = null
const playState = { playPending: false, playSucceeded: false }
if (audio.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA) {
attemptPlayback(audio, nextLoadKey, playState)
} else {
const onCanPlay = () => {
audio.removeEventListener("canplay", onCanPlay)
attemptPlayback(audio, nextLoadKey, playState)
}
audio.addEventListener("canplay", onCanPlay)
}
}
}
store.update(playbackQueue.skipNext)
if (!shouldContinuePlaying) {
autoPlayNextRef.current = false
isPlayingRef.current = false
audio.pause()
audio.currentTime = 0
currentTimeRef.current = 0
setCurrentTime(0)
setIsPlaying(false)
}
}
const handleError = () => {
if (intentionalLoadRef.current) {
return
}
if (!currentSongId || playbackErrorHandledRef.current) {
return
}
playbackErrorHandledRef.current = true
const { error } = audio
if (error) {
console.error("Audio playback error:", {
code: error.code,
message: error.message,
src: audio.src,
})
}
audio.pause()
autoPlayNextRef.current = false
destroyHls()
audio.removeAttribute("src")
loadedSongKeyRef.current = null
setIsPlaying(false)
toast.error("Playback error", {
description: "Could not play this track. The queue has been cleared.",
})
store.update(playbackQueue.clearQueue)
}
audio.addEventListener("ended", handleEnded)
audio.addEventListener("error", handleError)
return () => {
audio.removeEventListener("ended", handleEnded)
audio.removeEventListener("error", handleError)
}
}, [
repeatEnabled,
currentSongId,
queueLength,
nextQueueSongId,
nextQueueSongDoc,
user,
dolbyAtmosSupported,
store,
destroyHls,
runWithoutAudioSync,
attemptPlayback,
])
useEffect(() => {
const audio = audioRef.current
if (!audio) {
return
}
const syncDuration = () => {
if (!currentSongId) {
return
}
if (Number.isFinite(audio.duration) && audio.duration > 0) {
setMeasuredDuration({
songId: currentSongId,
seconds: audio.duration,
})
} else if (currentSong) {
setMeasuredDuration({
songId: currentSongId,
seconds: currentSong.durationSeconds,
})
}
}
const syncTime = () => {
currentTimeRef.current = audio.currentTime
setCurrentTime(audio.currentTime)
}
audio.addEventListener("timeupdate", syncTime)
audio.addEventListener("durationchange", syncDuration)
audio.addEventListener("loadedmetadata", syncDuration)
audio.addEventListener("seeked", syncTime)
return () => {
audio.removeEventListener("timeupdate", syncTime)
audio.removeEventListener("durationchange", syncDuration)
audio.removeEventListener("loadedmetadata", syncDuration)
audio.removeEventListener("seeked", syncTime)
}
}, [currentSongId, currentSong])
const queueOriginRef = useRef<string | undefined>(undefined)
const playRequestRef = useRef(0)
const startPlayback = useCallback(
(
recipe: (
state: playbackQueue.PlaybackQueueState
) => playbackQueue.PlaybackQueueState,
recorded: { playlistId?: Id<"playlists">; origin?: string } = {}
) => {
playRequestRef.current += 1
queueOriginRef.current =
recorded.origin ??
(recorded.playlistId ? `playlist:${recorded.playlistId}` : undefined)
const previousSongId = store.getState().queue[0]
store.update(playbackQueue.clearQueue)
store.update(recipe)
const firstSongId = store.getState().queue[0]
if (!firstSongId) {
return
}
if (firstSongId === previousSongId) {
seekTo(0)
}
// Repeated Play/Shuffle presses must request playback even when the
// first song and playing state have not changed.
isPlayingRef.current = true
setIsPlaying(true)
setPlaybackStartVersion((version) => version + 1)
recordPlay(
recorded.playlistId
? { playlistId: recorded.playlistId }
: { songId: firstSongId }
)
},
[store, seekTo, recordPlay]
)
const playCollection = useCallback(
async (
songIds: Id<"songs">[] | Promise<Id<"songs">[]>,
shuffled: boolean,
recorded: { playlistId?: Id<"playlists">; origin?: string } = {}
) => {
const request = ++playRequestRef.current
let resolvedSongIds: Id<"songs">[]
try {
resolvedSongIds = await songIds
} catch {
toast.error("Could not load songs to play")
return
}
if (request !== playRequestRef.current || resolvedSongIds.length === 0) {
return
}
startPlayback(
(state) => playbackQueue.startQueue(state, resolvedSongIds, shuffled),
recorded
)
},
[startPlayback]
)
const playInOrder = useCallback(
async (songIds: Id<"songs">[]) => {
if (songIds.length === 0) {
return
}
startPlayback((state) => playbackQueue.startQueue(state, songIds, false))
},
[startPlayback]
)
const playShuffled = useCallback(
async (songIds: Id<"songs">[]) => {
if (songIds.length === 0) {
return
}
startPlayback((state) => playbackQueue.startQueue(state, songIds, true))
},
[startPlayback]
)
const addToQueueNext = useCallback(
async (songIds: Id<"songs">[]) => {
if (songIds.length === 0) {
return
}
store.update((state) => playbackQueue.addToQueue(state, songIds, "next"))
setIsPlaying(true)
},
[store]
)
const addToQueueLast = useCallback(
async (songIds: Id<"songs">[]) => {
if (songIds.length === 0) {
return
}
store.update((state) => playbackQueue.addToQueue(state, songIds, "last"))
setIsPlaying(true)
},
[store]
)
const playFromSong = useCallback(
async (songId: Id<"songs">, songIds: Id<"songs">[], origin?: string) => {
if (
origin &&
queueOriginRef.current === origin &&
store.getState().queue[0] === songId
)
return
startPlayback(
(state) => playbackQueue.startQueueFromSong(state, songId, songIds),
{ origin }
)
},
[startPlayback, store]
)
const playFromLibrarySong = useCallback(
async (
songId: Id<"songs">,
sort: {
sortKey: "title" | "artist" | "album"
sortDirection: "asc" | "desc"
}
) => {
startPlayback((state) =>
playbackQueue.startQueueFromSong(state, songId, [songId])
)
const request = playRequestRef.current
let songIds: Id<"songs">[]
try {
songIds = await fetchCachedQuery(
convex,
cacheScope,
api.library.browseSortedSongIds,
sort
)
} catch {
return
}
if (request === playRequestRef.current) {
store.update((state) =>
playbackQueue.extendQueueFromSong(state, songId, songIds)
)
}
},
[convex, cacheScope, store, startPlayback]
)
const loadAlbumSongIds = useCallback(
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
songIds ??
fetchCachedQuery(convex, cacheScope, api.playlists.getAlbumSongIds, {
albumId,
}),
[convex, cacheScope]
)
const loadArtistSongIds = useCallback(
(artistId: Id<"artists">) =>
fetchCachedQuery(
convex,
cacheScope,
api.playlists.getOrderedArtistSongIds,
{ artistId }
),
[convex, cacheScope]
)
const loadPlaylistSongIds = useCallback(
(playlistId: Id<"playlists">, songIds?: Id<"songs">[]) =>
songIds ??
fetchCachedQuery(convex, cacheScope, api.playlists.getPlaylistSongIds, {
playlistId,
}),
[convex, cacheScope]
)
const playAlbum = useCallback(
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
playCollection(loadAlbumSongIds(albumId, songIds), false, {
origin: `album:${albumId}`,
}),
[playCollection, loadAlbumSongIds]
)
const playAlbumShuffled = useCallback(
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
playCollection(loadAlbumSongIds(albumId, songIds), true, {
origin: `album:${albumId}`,
}),
[playCollection, loadAlbumSongIds]
)
const playArtist = useCallback(
(artistId: Id<"artists">) =>
playCollection(loadArtistSongIds(artistId), false),
[playCollection, loadArtistSongIds]
)
const playArtistShuffled = useCallback(
(artistId: Id<"artists">) =>
playCollection(loadArtistSongIds(artistId), true),
[playCollection, loadArtistSongIds]
)
const playPlaylist = useCallback(
(playlistId: Id<"playlists">, songIds?: Id<"songs">[]) =>
playCollection(loadPlaylistSongIds(playlistId, songIds), false, {
playlistId,
}),
[playCollection, loadPlaylistSongIds]
)
const playPlaylistShuffled = useCallback(
(playlistId: Id<"playlists">, songIds?: Id<"songs">[]) =>
playCollection(loadPlaylistSongIds(playlistId, songIds), true, {
playlistId,
}),
[playCollection, loadPlaylistSongIds]
)
const toggleQueueOpen = useCallback(() => {
setQueueOpen((open) => !open)
}, [])
const toggleShuffle = useCallback(async () => {
store.update(playbackQueue.toggleShuffle)
}, [store])
const cycleRepeat = useCallback(async () => {
store.update(playbackQueue.cycleRepeat)
}, [store])
const clearQueue = useCallback(async () => {
playRequestRef.current += 1
store.update(playbackQueue.clearQueue)
setIsPlaying(false)
}, [store])
const reorderQueue = useCallback(
async (songIds: Id<"songs">[]) => {
store.update((state) => playbackQueue.reorderQueue(state, songIds))
},
[store]
)
const skipToQueueIndex = useCallback(
async (index: number) => {
store.update((state) => playbackQueue.skipToQueueIndex(state, index))
},
[store]
)
const skipNext = useCallback(async () => {
const before = store.getState()
if (before.repeatEnabled === "none" && before.queue.length <= 1) {
setIsPlaying(false)
}
store.update(playbackQueue.skipNext)
if (store.getState().queue[0] === before.queue[0]) {
seekTo(0)
}
}, [store, seekTo])
const skipPrevious = useCallback(async () => {
if (currentTimeRef.current > PREVIOUS_TRACK_RESTART_THRESHOLD_SECONDS) {
seekTo(0)
return
}
const previousSongId = store.getState().queue[0]
store.update(playbackQueue.skipPrevious)
if (store.getState().queue[0] === previousSongId) {
seekTo(0)
}
}, [store, seekTo])
useMediaSession({
song: currentSong,
isPlaying: isPlaybackActive,
duration,
currentTime,
seekTo,
setIsPlaying,
skipNext,
skipPrevious,
})
const currentFormat =
currentSongDoc && user
? playbackFormat(
currentSongDoc.files,
user.playbackQuality,
dolbyAtmosSupported === true && (user.preferAtmos ?? false)
)
: null
const actions = useMemo<PlaybackActions>(
() => ({
setQueueOpen,
toggleQueueOpen,
setIsPlaying,
setVolume,
playInOrder,
playShuffled,
addToQueueNext,
addToQueueLast,
playFromSong,
playFromLibrarySong,
playAlbum,
playAlbumShuffled,
playArtist,
playArtistShuffled,
playPlaylist,
playPlaylistShuffled,
toggleShuffle,
cycleRepeat,
clearQueue,
reorderQueue,
skipToQueueIndex,
skipNext,
skipPrevious,
}),
[
toggleQueueOpen,
setVolume,
playInOrder,
playShuffled,
addToQueueNext,
addToQueueLast,
playFromSong,
playFromLibrarySong,
playAlbum,
playAlbumShuffled,
playArtist,
playArtistShuffled,
playPlaylist,
playPlaylistShuffled,
toggleShuffle,
cycleRepeat,
clearQueue,
reorderQueue,
skipToQueueIndex,
skipNext,
skipPrevious,
]
)
const value = useMemo<PlaybackContextValue>(
() => ({
...actions,
queueSongIds: playback.queue,
queueHistorySongIds: playback.history,
currentSong,
currentFormat,
shuffleEnabled: playback.shuffleEnabled,
repeatEnabled: playback.repeatEnabled,
queueOpen,
isPlaying: isPlaybackActive,
volume,
dolbyAtmosSupported,
}),
[
actions,
playback,
currentSong,
currentFormat,
queueOpen,
isPlaybackActive,
volume,
dolbyAtmosSupported,
]
)
const progressValue = useMemo(
() => ({ currentTime, duration, seekTo }),
[currentTime, duration, seekTo]
)
return (
<PlaybackActionsContext.Provider value={actions}>
<CurrentSongIdContext.Provider value={currentSongId}>
<IsPlayingContext.Provider value={isPlaybackActive}>
<PlaybackContext.Provider value={value}>
<PlaybackProgressContext.Provider value={progressValue}>
{children}
</PlaybackProgressContext.Provider>
</PlaybackContext.Provider>
</IsPlayingContext.Provider>
</CurrentSongIdContext.Provider>
</PlaybackActionsContext.Provider>
)
}