feat: actual, honest-to-god playback
Docker Image CI / build-and-push (push) Has been cancelled

This commit is contained in:
joshuafhiggins committed 2026-06-04 22:47:22 -04:00 Verified
1 parent 11d2b41303
commit 76bead7449
10 files changed
+471 -49

No files matched your search

+83 -34
View File
@@ -1,16 +1,77 @@
import { api } from "@/convex/_generated/api"
import type { Id } from "@/convex/_generated/dataModel"
import { audioContentTypeForPath } from "@/lib/audio-content-type"
import { getConvexClient } from "@/lib/convex-server"
import { serveCoverFile } from "@/lib/serve-cover"
import { NextRequest, NextResponse } from "next/server"
import { spawn } from "node:child_process"
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process"
import fs from "node:fs/promises"
import * as music from "music-metadata"
const DATA_DIR = process.env.NEXT_PUBLIC_CONVEX_URL?.includes("127.0.0.1")
? "/Users/joshuafhiggins/Downloads/test"
: process.env.DATA || "/data"
function ffmpegToReadableStream(ffmpegProcess: ChildProcessWithoutNullStreams) {
let closed = false
return new ReadableStream({
start(controller) {
const finalize = (err?: Error) => {
if (closed) {
return
}
closed = true
try {
if (err) {
controller.error(err)
} else {
controller.close()
}
} catch {
// Consumer may have already closed the stream (e.g. track change).
}
}
ffmpegProcess.stdout.on("data", (chunk: Buffer) => {
if (closed) {
return
}
try {
controller.enqueue(new Uint8Array(chunk))
} catch {
closed = true
ffmpegProcess.kill("SIGKILL")
}
})
ffmpegProcess.stderr.on("data", (data) => {
//console.error(`FFmpeg log: ${data.toString()}`)
})
ffmpegProcess.stdout.on("end", () => finalize())
ffmpegProcess.stdout.on("error", (err) => finalize(err))
ffmpegProcess.on("close", (code) => {
if (code !== 0 && !closed) {
finalize(new Error(`ffmpeg exited with code ${code}`))
} else {
finalize()
}
})
ffmpegProcess.on("error", (err) => finalize(err))
},
cancel() {
if (closed) {
return
}
closed = true
ffmpegProcess.kill("SIGKILL")
},
})
}
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
@@ -34,9 +95,8 @@ export async function GET(
| "lossless"
| null
const preferAtmos = request.nextUrl.searchParams.get("preferAtmos") as
| boolean
| null
const preferAtmos =
request.nextUrl.searchParams.get("preferAtmos") === "true"
let kbps = 0
if (quality === "low") {
@@ -56,54 +116,43 @@ export async function GET(
} else if (kbps === 0 && "lossless" in song.files) {
originalCodec = "lossless"
transcodeNeeded = false
} else if (kbps === 0) {
transcodeNeeded = false
}
const file = `${DATA_DIR}/${originalCodec}/${artist.name}/${album.name}/${song.files[originalCodec]}`
if (!transcodeNeeded) {
const data = await fs.readFile(file)
const metadata = await music.parseFile(file, { skipCovers: true })
return new NextResponse(data, {
status: 200,
headers: {
"Content-Type": `audio/${metadata.format.container || "aac"}`,
"Transfer-Encoding": "chunked",
"Content-Type": audioContentTypeForPath(file),
"Cache-Control": "no-cache",
},
})
}
let ffmpegArgs = ["-i", file, "-c:a", "aac", "-b:a", `${kbps}k`, "-f", "mpegts", "pipe:1"]
const ffmpegArgs = [
"-nostdin",
"-i",
file,
"-c:a",
`${quality === "low" ? "libfdk_aac" : "aac"}`,
"-b:a",
`${kbps}k`,
"-f",
"adts",
"pipe:1",
]
try {
const ffmpegProcess = spawn("ffmpeg", ffmpegArgs)
const stream = new ReadableStream({
start(controller) {
ffmpegProcess.stdout.on("data", (chunk: Buffer) => {
controller.enqueue(new Uint8Array(chunk))
})
ffmpegProcess.stdout.on("end", () => {
controller.close()
})
ffmpegProcess.stdout.on("error", (err) => {
controller.error(err)
})
ffmpegProcess.stderr.on("data", (data) => {
//console.error(`FFmpeg log: ${data.toString()}`)
})
},
cancel() {
ffmpegProcess.kill("SIGKILL")
},
})
const stream = ffmpegToReadableStream(ffmpegProcess)
return new NextResponse(stream, {
status: 200,
headers: {
"Content-Type": "audio/mpegts",
"Content-Type": "audio/aac",
"Transfer-Encoding": "chunked",
"Cache-Control": "no-cache",
},
+1 -1
View File
@@ -196,7 +196,7 @@ function ProfileSection({ user }: { user: Doc<"users"> }) {
async function handleClearImage() {
try {
await updateImage({})
await updateImage({ image: null })
toast.success("Profile photo removed")
} catch (error) {
toast.error(
+289 -8
View File
@@ -2,10 +2,12 @@
import * as React from "react"
import { useConvex, useMutation, useQuery } from "convex/react"
import { toast } from "sonner"
import { api } from "@/convex/_generated/api"
import type { Id } from "@/convex/_generated/dataModel"
import type { BrowseSong } from "@/convex/library"
import { songPlayUrl } from "@/lib/song-play-url"
type RepeatMode = "none" | "once" | "always"
@@ -17,6 +19,9 @@ type PlaybackContextValue = {
queueOpen: boolean
isPlaying: boolean
volume: number
currentTime: number
duration: number
seekTo: (seconds: number) => void
setQueueOpen: (open: boolean) => void
toggleQueueOpen: () => void
setIsPlaying: (playing: boolean) => void
@@ -43,6 +48,49 @@ type PlaybackContextValue = {
const PlaybackContext = React.createContext<PlaybackContextValue | null>(null)
const VOLUME_STORAGE_KEY = "otunes:volume"
const DEFAULT_VOLUME = 80
const volumeListeners = new Set<() => void>()
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
}
}
export function usePlayback() {
const context = React.useContext(PlaybackContext)
if (!context) {
@@ -53,7 +101,12 @@ export function usePlayback() {
export function PlaybackProvider({ children }: { children: React.ReactNode }) {
const convex = useConvex()
const user = useQuery(api.auth.currentUser)
const playback = useQuery(api.playback.getPlaybackState)
const audioRef = React.useRef<HTMLAudioElement | null>(null)
const loadedSongIdRef = React.useRef<string | null>(null)
const playbackErrorHandledRef = React.useRef(false)
const isPlayingRef = React.useRef(false)
const playInOrderMutation = useMutation(api.playback.playInOrder)
const playShuffledMutation = useMutation(api.playback.playShuffled)
const addToQueueNextMutation = useMutation(api.playback.addToQueueNext)
@@ -69,15 +122,236 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
const [queueOpen, setQueueOpen] = React.useState(false)
const [isPlaying, setIsPlaying] = React.useState(false)
const [volume, setVolume] = React.useState(80)
const [currentTime, setCurrentTime] = React.useState(0)
const [duration, setDuration] = React.useState(0)
const volume = React.useSyncExternalStore(
subscribeVolume,
readStoredVolume,
() => DEFAULT_VOLUME
)
const setVolume = React.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 currentSong = playback?.currentSong ?? null
const isPlaybackActive = Boolean(currentSong && isPlaying)
const repeatEnabled = playback?.repeatEnabled ?? "none"
React.useEffect(() => {
if (playback?.currentSong) {
setIsPlaying(true)
} else {
setIsPlaying(false)
setCurrentTime(0)
setDuration(currentSong?.durationSeconds ?? 0)
}, [currentSong?.id, currentSong?.durationSeconds])
const seekTo = React.useCallback((seconds: number) => {
const audio = audioRef.current
if (!audio) {
return
}
}, [playback?.currentSong?.id])
const maxDuration =
Number.isFinite(audio.duration) && audio.duration > 0
? audio.duration
: duration
const clamped = Math.max(0, Math.min(seconds, maxDuration || 0))
audio.currentTime = clamped
setCurrentTime(clamped)
}, [duration])
React.useEffect(() => {
isPlayingRef.current = isPlaying
}, [isPlaying])
React.useEffect(() => {
const audio = new Audio()
audioRef.current = audio
return () => {
audio.pause()
audio.removeAttribute("src")
audioRef.current = null
loadedSongIdRef.current = null
}
}, [])
React.useEffect(() => {
const audio = audioRef.current
if (!audio) {
return
}
audio.volume = volume / 100
}, [volume])
React.useEffect(() => {
const audio = audioRef.current
if (!audio) {
return
}
if (!currentSong) {
audio.pause()
audio.removeAttribute("src")
loadedSongIdRef.current = null
return
}
if (user === undefined || !user) {
return
}
if (loadedSongIdRef.current === currentSong.id) {
return
}
const url = songPlayUrl(
currentSong.id,
user.playbackQuality ?? "medium",
user.preferAtmos ?? false
)
playbackErrorHandledRef.current = false
audio.pause()
const onCanPlay = () => {
audio.removeEventListener("canplay", onCanPlay)
if (isPlayingRef.current) {
void audio.play().catch(() => {
setIsPlaying(false)
})
}
}
audio.addEventListener("canplay", onCanPlay)
audio.src = url
audio.load()
loadedSongIdRef.current = currentSong.id
return () => {
audio.removeEventListener("canplay", onCanPlay)
}
}, [currentSong?.id, user === undefined])
React.useEffect(() => {
const audio = audioRef.current
if (!audio || !currentSong) {
return
}
if (!isPlaying) {
audio.pause()
return
}
if (audio.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA) {
void audio.play().catch(() => {
setIsPlaying(false)
})
return
}
const onCanPlay = () => {
audio.removeEventListener("canplay", onCanPlay)
if (isPlayingRef.current) {
void audio.play().catch(() => {
setIsPlaying(false)
})
}
}
audio.addEventListener("canplay", onCanPlay)
return () => {
audio.removeEventListener("canplay", onCanPlay)
}
}, [isPlaying, currentSong])
React.useEffect(() => {
const audio = audioRef.current
if (!audio) {
return
}
const handleEnded = () => {
if (repeatEnabled === "once") {
audio.currentTime = 0
void audio.play()
return
}
void skipNextMutation()
}
const handleError = () => {
if (!currentSong || 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()
audio.removeAttribute("src")
loadedSongIdRef.current = null
setIsPlaying(false)
toast.error("Playback error", {
description: "Could not play this track. The queue has been cleared.",
})
void clearQueueMutation()
}
audio.addEventListener("ended", handleEnded)
audio.addEventListener("error", handleError)
return () => {
audio.removeEventListener("ended", handleEnded)
audio.removeEventListener("error", handleError)
}
}, [repeatEnabled, currentSong, skipNextMutation, clearQueueMutation])
React.useEffect(() => {
const audio = audioRef.current
if (!audio) {
return
}
const syncDuration = () => {
if (Number.isFinite(audio.duration) && audio.duration > 0) {
setDuration(audio.duration)
} else if (currentSong) {
setDuration(currentSong.durationSeconds)
}
}
const syncTime = () => {
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)
}
}, [currentSong])
const playInOrder = React.useCallback(
async (songIds: Id<"songs">[]) => {
@@ -202,8 +476,11 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
shuffleEnabled: playback?.shuffleEnabled ?? false,
repeatEnabled: playback?.repeatEnabled ?? "none",
queueOpen,
isPlaying,
isPlaying: isPlaybackActive,
volume,
currentTime,
duration,
seekTo,
setQueueOpen,
toggleQueueOpen,
setIsPlaying,
@@ -245,9 +522,13 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
[
playback,
queueOpen,
isPlaying,
isPlaybackActive,
volume,
currentTime,
duration,
seekTo,
toggleQueueOpen,
setVolume,
playInOrder,
playShuffled,
addToQueueNext,
+52 -1
View File
@@ -1,5 +1,6 @@
"use client"
import * as React from "react"
import Image from "next/image"
import {
ListMusic,
@@ -22,6 +23,13 @@ import { useSidebar } from "@/components/ui/sidebar"
import { albumCoverUrl } from "@/lib/cover-urls"
import { cn } from "@/lib/utils"
function formatDuration(seconds: number): string {
const total = Math.max(0, Math.floor(seconds))
const minutes = Math.floor(total / 60)
const remainingSeconds = total % 60
return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`
}
export function PlayerBar() {
const {
currentSong,
@@ -30,9 +38,12 @@ export function PlayerBar() {
queueOpen,
isPlaying,
volume,
currentTime,
duration,
queue,
setIsPlaying,
setVolume,
seekTo,
toggleQueueOpen,
toggleShuffle,
cycleRepeat,
@@ -40,9 +51,13 @@ export function PlayerBar() {
skipPrevious,
} = usePlayback()
const { state, isMobile } = useSidebar()
const [isSeeking, setIsSeeking] = React.useState(false)
const [seekValue, setSeekValue] = React.useState(0)
const hasQueue = queue.length > 0
const sidebarExpanded = !isMobile && state === "expanded"
const displayTime = isSeeking ? seekValue : currentTime
const maxDuration = Math.max(duration, 1)
return (
<div
@@ -52,7 +67,8 @@ export function PlayerBar() {
queueOpen && !isMobile && "right-72"
)}
>
<div className="pointer-events-auto grid w-full max-w-3xl grid-cols-[1fr_auto_1fr] items-center gap-2 rounded-xl border bg-card/80 px-3 py-2 shadow-lg backdrop-blur">
<div className="pointer-events-auto flex w-full max-w-3xl flex-col gap-2 rounded-xl border bg-card/80 px-3 py-2 shadow-lg backdrop-blur">
<div className="grid grid-cols-[1fr_auto_1fr] items-center gap-2">
<div className="flex items-center justify-start gap-1">
<Button
variant="ghost"
@@ -166,6 +182,41 @@ export function PlayerBar() {
<Volume2 />
</Button>
</div>
</div>
{hasQueue ? (
<div className="flex items-center gap-2 px-1">
<span
className="w-9 shrink-0 text-right text-xs tabular-nums text-muted-foreground"
aria-hidden
>
{formatDuration(displayTime)}
</span>
<Slider
className="flex-1"
min={0}
max={maxDuration}
step={0.1}
value={[displayTime]}
disabled={!currentSong}
onValueChange={([value]) => {
setIsSeeking(true)
setSeekValue(value)
}}
onValueCommit={([value]) => {
seekTo(value)
setIsSeeking(false)
}}
aria-label="Seek"
/>
<span
className="w-9 shrink-0 text-xs tabular-nums text-muted-foreground"
aria-hidden
>
{formatDuration(duration)}
</span>
</div>
) : null}
</div>
</div>
)
+1 -1
View File
@@ -87,7 +87,7 @@ function Calendar({
: "flex items-center gap-1 rounded-(--cell-radius) text-sm [&>svg]:size-3.5 [&>svg]:text-muted-foreground",
defaultClassNames.caption_label
),
table: "w-full border-collapse",
month_grid: cn("w-full border-collapse", defaultClassNames.month_grid),
weekdays: cn("flex", defaultClassNames.weekdays),
weekday: cn(
"flex-1 rounded-(--cell-radius) text-[0.8rem] font-normal text-muted-foreground select-none",
+2
View File
@@ -43,6 +43,8 @@ export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
} else {
await ctx.db.patch(userId, { isAdmin: false })
}
await ctx.db.patch(userId, { playbackQuality: "high" })
return
}
},
+3 -3
View File
@@ -322,7 +322,7 @@ async function unfavoriteSongHandler(ctx: MutationCtx, songId: Id<"songs">) {
return
}
let favoriteSongs = (user.favoriteSongs ?? []).filter((id) => id !== songId)
const favoriteSongs = (user.favoriteSongs ?? []).filter((id) => id !== songId)
let favoriteAlbums = user.favoriteAlbums ?? []
let favoriteArtists = user.favoriteArtists ?? []
@@ -392,8 +392,8 @@ export const unfavoriteAlbum = mutation({
const albumSongIds = await getSongIdsForAlbum(ctx, albumId)
const removeSongs = new Set(albumSongIds)
let favoriteAlbums = (user.favoriteAlbums ?? []).filter((id) => id !== albumId)
let favoriteSongs = (user.favoriteSongs ?? []).filter((id) => !removeSongs.has(id))
const favoriteAlbums = (user.favoriteAlbums ?? []).filter((id) => id !== albumId)
const favoriteSongs = (user.favoriteSongs ?? []).filter((id) => !removeSongs.has(id))
let favoriteArtists = user.favoriteArtists ?? []
if (favoriteArtists.includes(album.artist)) {
+1 -1
View File
@@ -6,7 +6,7 @@ import convexPlugin from "@convex-dev/eslint-plugin"
const eslintConfig = defineConfig([
...nextVitals,
...nextTs,
...convexPlugin,
...convexPlugin.configs.recommended,
// Override default ignores of eslint-config-next.
globalIgnores([
+22
View File
@@ -0,0 +1,22 @@
export function audioContentTypeForPath(filePath: string): string {
const lower = filePath.toLowerCase()
if (lower.endsWith(".mp3")) {
return "audio/mpeg"
}
if (lower.endsWith(".m4a") || lower.endsWith(".mp4")) {
return "audio/mp4"
}
if (lower.endsWith(".flac")) {
return "audio/flac"
}
if (lower.endsWith(".ogg") || lower.endsWith(".opus")) {
return "audio/ogg"
}
if (lower.endsWith(".wav")) {
return "audio/wav"
}
if (lower.endsWith(".aac")) {
return "audio/aac"
}
return "application/octet-stream"
}
+17
View File
@@ -0,0 +1,17 @@
export type PlaybackQuality = "low" | "medium" | "high" | "lossless"
export function songPlayUrl(
songId: string,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined
) {
const params = new URLSearchParams()
if (quality) {
params.set("quality", quality)
}
if (preferAtmos) {
params.set("preferAtmos", "true")
}
const query = params.toString()
return `/api/song/${songId}/play${query ? `?${query}` : ""}`
}