feat: fixed seeking bugs, hls for transcoded aac
This commit is contained in:
12 files changed
+675
-118
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import { getConvexClient } from "@/lib/convex-server"
|
||||
import { ensureHlsJob, HLS_SEGMENT_SECONDS } from "@/lib/server-hls"
|
||||
import { NextRequest } from "next/server"
|
||||
|
||||
function hlsSegmentUrl(request: NextRequest, index: number) {
|
||||
const url = new URL(request.url)
|
||||
url.pathname = `${url.pathname.replace(/\/$/, "")}/segment/${index}.ts`
|
||||
return `${url.pathname}${url.search}`
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id } = await params
|
||||
|
||||
try {
|
||||
const song = (await getConvexClient().query(api.library.getSong, {
|
||||
id: id as Id<"songs">,
|
||||
}))!
|
||||
|
||||
const quality = request.nextUrl.searchParams.get("quality")
|
||||
const preferAtmos =
|
||||
request.nextUrl.searchParams.get("preferAtmos") === "true"
|
||||
const job = await ensureHlsJob(song, quality, preferAtmos)
|
||||
const { duration, segmentCount } = job
|
||||
const targetDuration = Math.ceil(HLS_SEGMENT_SECONDS)
|
||||
|
||||
const lines = [
|
||||
"#EXTM3U",
|
||||
"#EXT-X-VERSION:3",
|
||||
`#EXT-X-TARGETDURATION:${targetDuration}`,
|
||||
"#EXT-X-MEDIA-SEQUENCE:0",
|
||||
"#EXT-X-PLAYLIST-TYPE:VOD",
|
||||
]
|
||||
|
||||
for (let index = 0; index < segmentCount; index += 1) {
|
||||
const start = index * HLS_SEGMENT_SECONDS
|
||||
const segmentDuration = Math.min(HLS_SEGMENT_SECONDS, duration - start)
|
||||
lines.push(`#EXTINF:${segmentDuration.toFixed(3)},`)
|
||||
lines.push(hlsSegmentUrl(request, index))
|
||||
}
|
||||
|
||||
lines.push("#EXT-X-ENDLIST")
|
||||
|
||||
return new Response(`${lines.join("\n")}\n`, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Cache-Control": "no-cache",
|
||||
"Content-Type": "application/vnd.apple.mpegurl",
|
||||
},
|
||||
})
|
||||
} catch {
|
||||
return new Response("Not found", { status: 404 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import { getConvexClient } from "@/lib/convex-server"
|
||||
import { ensureHlsJob, waitForHlsSegment } from "@/lib/server-hls"
|
||||
import { streamFile } from "@/lib/server-audio-stream"
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import fs from "node:fs/promises"
|
||||
|
||||
function parseSegmentIndex(segment: string) {
|
||||
const indexText = segment.endsWith(".ts") ? segment.slice(0, -3) : segment
|
||||
const index = Number(indexText)
|
||||
|
||||
if (!Number.isInteger(index) || index < 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string; segment: string }> }
|
||||
) {
|
||||
const { id, segment } = await params
|
||||
const segmentIndex = parseSegmentIndex(segment)
|
||||
|
||||
if (segmentIndex === null) {
|
||||
return new Response("Invalid segment", { status: 400 })
|
||||
}
|
||||
|
||||
try {
|
||||
const song = (await getConvexClient().query(api.library.getSong, {
|
||||
id: id as Id<"songs">,
|
||||
}))!
|
||||
|
||||
const quality = request.nextUrl.searchParams.get("quality")
|
||||
const preferAtmos =
|
||||
request.nextUrl.searchParams.get("preferAtmos") === "true"
|
||||
const job = await ensureHlsJob(song, quality, preferAtmos)
|
||||
if (segmentIndex >= job.segmentCount) {
|
||||
return new Response("Segment not found", { status: 404 })
|
||||
}
|
||||
|
||||
const segmentPath = await waitForHlsSegment(job, segmentIndex)
|
||||
const { size } = await fs.stat(segmentPath)
|
||||
|
||||
return new NextResponse(streamFile(segmentPath), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Cache-Control": "no-cache",
|
||||
"Content-Length": String(size),
|
||||
"Content-Type": "video/mp2t",
|
||||
},
|
||||
})
|
||||
} catch {
|
||||
return new Response("Not found", { status: 404 })
|
||||
}
|
||||
}
|
||||
+103
-102
@@ -2,96 +2,110 @@ 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 { ffmpegToReadableStream, streamFile } from "@/lib/server-audio-stream"
|
||||
import { NextRequest, NextResponse, userAgent } from "next/server"
|
||||
import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process"
|
||||
import { spawn } from "node:child_process"
|
||||
import fs from "node:fs/promises"
|
||||
import { createReadStream, ReadStream } from "node:fs"
|
||||
import * as music from "music-metadata"
|
||||
|
||||
async function* nodeStreamToIterator(stream: ReadStream) {
|
||||
for await (const chunk of stream) {
|
||||
yield new Uint8Array(chunk)
|
||||
function parseByteRange(rangeHeader: string | null, fileSize: number) {
|
||||
if (!rangeHeader) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (!rangeHeader.startsWith("bytes=")) {
|
||||
return "invalid" as const
|
||||
}
|
||||
|
||||
const rangeSpec = rangeHeader.slice("bytes=".length)
|
||||
if (rangeSpec.includes(",")) {
|
||||
return "invalid" as const
|
||||
}
|
||||
|
||||
const [startText, endText] = rangeSpec.split("-")
|
||||
if (startText === undefined || endText === undefined) {
|
||||
return "invalid" as const
|
||||
}
|
||||
|
||||
if (startText === "" && endText === "") {
|
||||
return "invalid" as const
|
||||
}
|
||||
|
||||
let start: number
|
||||
let end: number
|
||||
|
||||
if (startText === "") {
|
||||
const suffixLength = Number(endText)
|
||||
if (!Number.isInteger(suffixLength) || suffixLength <= 0) {
|
||||
return "invalid" as const
|
||||
}
|
||||
|
||||
start = Math.max(fileSize - suffixLength, 0)
|
||||
end = fileSize - 1
|
||||
} else {
|
||||
start = Number(startText)
|
||||
end = endText === "" ? fileSize - 1 : Number(endText)
|
||||
|
||||
if (
|
||||
!Number.isInteger(start) ||
|
||||
!Number.isInteger(end) ||
|
||||
start < 0 ||
|
||||
end < start
|
||||
) {
|
||||
return "invalid" as const
|
||||
}
|
||||
}
|
||||
|
||||
if (start >= fileSize) {
|
||||
return "invalid" as const
|
||||
}
|
||||
|
||||
return {
|
||||
start,
|
||||
end: Math.min(end, fileSize - 1),
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-expect-error trust me bro
|
||||
function iteratorToStream(iterator) {
|
||||
return new ReadableStream({
|
||||
async pull(controller) {
|
||||
const { value, done } = await iterator.next()
|
||||
if (done) {
|
||||
controller.close()
|
||||
} else {
|
||||
controller.enqueue(value)
|
||||
}
|
||||
},
|
||||
async function streamFileResponse(
|
||||
request: NextRequest,
|
||||
path: string,
|
||||
contentType: string
|
||||
) {
|
||||
const { size } = await fs.stat(path)
|
||||
const range = parseByteRange(request.headers.get("range"), size)
|
||||
|
||||
if (range === "invalid") {
|
||||
return new Response(null, {
|
||||
status: 416,
|
||||
headers: {
|
||||
"Accept-Ranges": "bytes",
|
||||
"Cache-Control": "no-cache",
|
||||
"Content-Range": `bytes */${size}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const headers = new Headers({
|
||||
"Accept-Ranges": "bytes",
|
||||
"Cache-Control": "no-cache",
|
||||
"Content-Type": contentType,
|
||||
})
|
||||
}
|
||||
|
||||
export function streamFile(path: string): ReadableStream {
|
||||
const nodeStream = createReadStream(path)
|
||||
return iteratorToStream(nodeStreamToIterator(nodeStream))
|
||||
}
|
||||
if (range) {
|
||||
headers.set("Content-Length", String(range.end - range.start + 1))
|
||||
headers.set("Content-Range", `bytes ${range.start}-${range.end}/${size}`)
|
||||
|
||||
function ffmpegToReadableStream(ffmpegProcess: ChildProcessWithoutNullStreams) {
|
||||
let closed = false
|
||||
return new NextResponse(streamFile(path, range), {
|
||||
status: 206,
|
||||
headers,
|
||||
})
|
||||
}
|
||||
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
const finalize = (err?: Error) => {
|
||||
if (closed) {
|
||||
return
|
||||
}
|
||||
closed = true
|
||||
headers.set("Content-Length", String(size))
|
||||
|
||||
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")
|
||||
},
|
||||
return new NextResponse(streamFile(path), {
|
||||
status: 200,
|
||||
headers,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -145,14 +159,11 @@ export async function GET(
|
||||
if (!transcodeNeeded && originalCodec === "lossless") {
|
||||
if (browser.name === "Safari" && metadata.format.codec === "ALAC") {
|
||||
try {
|
||||
return new NextResponse(streamFile(song.files[originalCodec]), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": `${audioContentTypeForPath(song.files[originalCodec])}`,
|
||||
"Transfer-Encoding": "chunked",
|
||||
"Cache-Control": "no-cache",
|
||||
},
|
||||
})
|
||||
return await streamFileResponse(
|
||||
request,
|
||||
song.files[originalCodec],
|
||||
audioContentTypeForPath(song.files[originalCodec])
|
||||
)
|
||||
} catch {
|
||||
return new Response("Server Error", { status: 500 })
|
||||
}
|
||||
@@ -168,20 +179,14 @@ export async function GET(
|
||||
]
|
||||
|
||||
try {
|
||||
// TODO: Fix seeking for this
|
||||
const ffmpegProcess = spawn("ffmpeg", ffmpegArgs)
|
||||
const stream = ffmpegToReadableStream(ffmpegProcess)
|
||||
const bytes = (metadata.format.duration! * metadata.format.bitrate!) / 8
|
||||
|
||||
return new NextResponse(stream, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "audio/mpeg",
|
||||
"Transfer-Encoding": "chunked",
|
||||
"Content-Type": "audio/flac",
|
||||
"Cache-Control": "no-cache",
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": `${bytes}`,
|
||||
"Content-Range": `bytes 0-${bytes - 1}/${bytes}`,
|
||||
},
|
||||
})
|
||||
} catch {
|
||||
@@ -200,14 +205,11 @@ export async function GET(
|
||||
metadata.format.codecProfile
|
||||
)
|
||||
try {
|
||||
return new NextResponse(streamFile(song.files[originalCodec]), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": `${audioContentTypeForPath(song.files[originalCodec])}`,
|
||||
"Transfer-Encoding": "chunked",
|
||||
"Cache-Control": "no-cache",
|
||||
},
|
||||
})
|
||||
return await streamFileResponse(
|
||||
request,
|
||||
song.files[originalCodec],
|
||||
audioContentTypeForPath(song.files[originalCodec])
|
||||
)
|
||||
} catch {
|
||||
return new Response("Server Error", { status: 500 })
|
||||
}
|
||||
@@ -235,7 +237,6 @@ export async function GET(
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "audio/aac",
|
||||
"Transfer-Encoding": "chunked",
|
||||
"Cache-Control": "no-cache",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -719,7 +719,9 @@ function UserRowActions({
|
||||
|
||||
function LibrarySection() {
|
||||
const updateLibrary = useAction(api.actions.updateLibrary)
|
||||
const cleanupHlsStreams = useAction(api.actions.adminCleanupHlsStreams)
|
||||
const [updating, setUpdating] = useState(false)
|
||||
const [cleaningHls, setCleaningHls] = useState(false)
|
||||
|
||||
async function handleUpdateLibrary() {
|
||||
setUpdating(true)
|
||||
@@ -735,6 +737,31 @@ function LibrarySection() {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCleanupHlsStreams() {
|
||||
setCleaningHls(true)
|
||||
try {
|
||||
const result = await cleanupHlsStreams({})
|
||||
toast.success(
|
||||
result.removed === 1
|
||||
? "Removed 1 idle HLS stream"
|
||||
: `Removed ${result.removed} idle HLS streams`
|
||||
)
|
||||
if (result.failed > 0) {
|
||||
toast.warning(
|
||||
result.failed === 1
|
||||
? "1 HLS stream could not be removed"
|
||||
: `${result.failed} HLS streams could not be removed`
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error(
|
||||
error instanceof Error ? error.message : "Failed to clean HLS streams"
|
||||
)
|
||||
} finally {
|
||||
setCleaningHls(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
@@ -744,14 +771,25 @@ function LibrarySection() {
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
type="button"
|
||||
disabled={updating}
|
||||
onClick={() => void handleUpdateLibrary()}
|
||||
>
|
||||
{updating && <Spinner />}
|
||||
Update library
|
||||
</Button>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
disabled={updating}
|
||||
onClick={() => void handleUpdateLibrary()}
|
||||
>
|
||||
{updating && <Spinner />}
|
||||
Update library
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={cleaningHls}
|
||||
onClick={() => void handleCleanupHlsStreams()}
|
||||
>
|
||||
{cleaningHls && <Spinner />}
|
||||
Clean HLS streams
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
|
||||
import * as React from "react"
|
||||
import { useConvex, useMutation, useQuery } from "convex/react"
|
||||
import Hls from "hls.js"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import type { BrowseSong } from "@/convex/library"
|
||||
import { supportsDolbyAtmosPlayback } from "@/lib/dolby-atmos-support"
|
||||
import { songPlayUrl } from "@/lib/song-play-url"
|
||||
import { songHlsPlaylistUrl, songPlayUrl } from "@/lib/song-play-url"
|
||||
|
||||
type RepeatMode = "none" | "once" | "always"
|
||||
|
||||
@@ -110,6 +111,7 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
const user = useQuery(api.auth.currentUser)
|
||||
const playback = useQuery(api.playback.getPlaybackState)
|
||||
const audioRef = React.useRef<HTMLAudioElement | null>(null)
|
||||
const hlsRef = React.useRef<Hls | null>(null)
|
||||
const loadedSongKeyRef = React.useRef<string | null>(null)
|
||||
const playbackErrorHandledRef = React.useRef(false)
|
||||
const isPlayingRef = React.useRef(false)
|
||||
@@ -189,6 +191,11 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
}, [])
|
||||
|
||||
const destroyHls = React.useCallback(() => {
|
||||
hlsRef.current?.destroy()
|
||||
hlsRef.current = null
|
||||
}, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
const audio = new Audio()
|
||||
audioRef.current = audio
|
||||
@@ -207,12 +214,13 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
return () => {
|
||||
audio.removeEventListener("play", syncPlayingFromAudio)
|
||||
audio.removeEventListener("pause", syncPlayingFromAudio)
|
||||
destroyHls()
|
||||
audio.pause()
|
||||
audio.removeAttribute("src")
|
||||
audioRef.current = null
|
||||
loadedSongKeyRef.current = null
|
||||
}
|
||||
}, [])
|
||||
}, [destroyHls])
|
||||
|
||||
React.useEffect(() => {
|
||||
const audio = audioRef.current
|
||||
@@ -229,6 +237,7 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
if (!currentSong) {
|
||||
destroyHls()
|
||||
audio.pause()
|
||||
audio.removeAttribute("src")
|
||||
loadedSongKeyRef.current = null
|
||||
@@ -241,22 +250,26 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const preferAtmos =
|
||||
dolbyAtmosSupported === true && (user.preferAtmos ?? false)
|
||||
const loadKey = `${currentSong.id}:${preferAtmos}:${user.playbackQuality ?? "medium"}`
|
||||
const playbackQuality = user.playbackQuality ?? "medium"
|
||||
const useHls = playbackQuality !== "lossless" && !preferAtmos
|
||||
const loadKey = `${currentSong.id}:${preferAtmos}:${playbackQuality}:${useHls ? "hls" : "file"}`
|
||||
|
||||
if (loadedSongKeyRef.current === loadKey) {
|
||||
return
|
||||
}
|
||||
|
||||
const url = songPlayUrl(
|
||||
const directUrl = songPlayUrl(
|
||||
currentSong.id,
|
||||
user.playbackQuality ?? "medium",
|
||||
playbackQuality,
|
||||
preferAtmos
|
||||
)
|
||||
const hlsUrl = songHlsPlaylistUrl(currentSong.id, playbackQuality, preferAtmos)
|
||||
|
||||
playbackErrorHandledRef.current = false
|
||||
runWithoutAudioSync(() => {
|
||||
audio.pause()
|
||||
})
|
||||
destroyHls()
|
||||
|
||||
const onCanPlay = () => {
|
||||
audio.removeEventListener("canplay", onCanPlay)
|
||||
@@ -268,8 +281,18 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
audio.addEventListener("canplay", onCanPlay)
|
||||
audio.src = url
|
||||
audio.load()
|
||||
if (useHls && audio.canPlayType("application/vnd.apple.mpegurl")) {
|
||||
audio.src = hlsUrl
|
||||
audio.load()
|
||||
} else if (useHls && Hls.isSupported()) {
|
||||
const hls = new Hls()
|
||||
hlsRef.current = hls
|
||||
hls.loadSource(hlsUrl)
|
||||
hls.attachMedia(audio)
|
||||
} else {
|
||||
audio.src = directUrl
|
||||
audio.load()
|
||||
}
|
||||
loadedSongKeyRef.current = loadKey
|
||||
|
||||
return () => {
|
||||
@@ -282,6 +305,7 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
user?.preferAtmos,
|
||||
dolbyAtmosSupported,
|
||||
runWithoutAudioSync,
|
||||
destroyHls,
|
||||
])
|
||||
|
||||
React.useEffect(() => {
|
||||
@@ -349,6 +373,7 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
audio.pause()
|
||||
destroyHls()
|
||||
audio.removeAttribute("src")
|
||||
loadedSongKeyRef.current = null
|
||||
setIsPlaying(false)
|
||||
@@ -366,7 +391,7 @@ export function PlaybackProvider({ children }: { children: React.ReactNode }) {
|
||||
audio.removeEventListener("ended", handleEnded)
|
||||
audio.removeEventListener("error", handleError)
|
||||
}
|
||||
}, [repeatEnabled, currentSong, skipNextMutation, clearQueueMutation])
|
||||
}, [repeatEnabled, currentSong, skipNextMutation, clearQueueMutation, destroyHls])
|
||||
|
||||
React.useEffect(() => {
|
||||
const audio = audioRef.current
|
||||
|
||||
@@ -5,6 +5,9 @@ import { api } from "@/convex/_generated/api"
|
||||
import * as fs from "node:fs/promises"
|
||||
import * as fsSync from "node:fs"
|
||||
import * as music from "music-metadata"
|
||||
import * as os from "node:os"
|
||||
import * as path from "node:path"
|
||||
import { ConvexError } from "convex/values"
|
||||
|
||||
import type { Id } from "./_generated/dataModel"
|
||||
import { action, type ActionCtx } from "./_generated/server"
|
||||
@@ -12,6 +15,48 @@ import { action, type ActionCtx } from "./_generated/server"
|
||||
const DATA_DIR = process.env.CONVEX_SITE_URL?.includes("127.0.0.1")
|
||||
? "/Volumes/FastStorage/Apple Music"
|
||||
: process.env.DATA || "/data"
|
||||
const HLS_CACHE_DIR = path.join(os.tmpdir(), "../otunes-hls")
|
||||
const HLS_MAX_IDLE_MS = 30 * 60 * 1000
|
||||
|
||||
async function cleanHlsStreamsOlderThan(maxIdleMs: number) {
|
||||
const now = Date.now()
|
||||
let checked = 0
|
||||
let removed = 0
|
||||
let failed = 0
|
||||
console.log(os.tmpdir())
|
||||
console.log(HLS_CACHE_DIR)
|
||||
|
||||
let entries: fsSync.Dirent[] = []
|
||||
try {
|
||||
entries = await fs.readdir(HLS_CACHE_DIR, { withFileTypes: true })
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
if (!entry.isDirectory()) {
|
||||
continue
|
||||
}
|
||||
|
||||
checked += 1
|
||||
const entryPath = path.join(HLS_CACHE_DIR, entry.name)
|
||||
try {
|
||||
const stat = await fs.stat(entryPath)
|
||||
if (now - stat.mtimeMs <= maxIdleMs) {
|
||||
continue
|
||||
}
|
||||
|
||||
await fs.rm(entryPath, { recursive: true, force: true })
|
||||
removed += 1
|
||||
} catch {
|
||||
failed += 1
|
||||
}
|
||||
}
|
||||
|
||||
return { checked, removed, failed }
|
||||
}
|
||||
|
||||
async function coverPathIfExists(path: string): Promise<string | null> {
|
||||
return fs
|
||||
@@ -492,3 +537,22 @@ export const updateLibrary = action({
|
||||
return "success"
|
||||
},
|
||||
})
|
||||
|
||||
export const cleanupHlsStreams = action({
|
||||
args: {},
|
||||
handler: async () => {
|
||||
return await cleanHlsStreamsOlderThan(HLS_MAX_IDLE_MS)
|
||||
},
|
||||
})
|
||||
|
||||
export const adminCleanupHlsStreams = action({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
const user = await ctx.runQuery(api.auth.currentUser)
|
||||
if (!user?.isAdmin) {
|
||||
throw new ConvexError("Not authorized")
|
||||
}
|
||||
|
||||
return await cleanHlsStreamsOlderThan(HLS_MAX_IDLE_MS)
|
||||
},
|
||||
})
|
||||
@@ -9,4 +9,10 @@ crons.interval(
|
||||
api.actions.updateLibrary
|
||||
)
|
||||
|
||||
crons.interval(
|
||||
"cleanup idle hls streams",
|
||||
{ minutes: 15 },
|
||||
api.actions.cleanupHlsStreams
|
||||
)
|
||||
|
||||
export default crons
|
||||
@@ -0,0 +1,96 @@
|
||||
import { type ChildProcessWithoutNullStreams } from "node:child_process"
|
||||
import { createReadStream, ReadStream } from "node:fs"
|
||||
|
||||
export type ByteRange = {
|
||||
start: number
|
||||
end: number
|
||||
}
|
||||
|
||||
async function* nodeStreamToIterator(stream: ReadStream) {
|
||||
for await (const chunk of stream) {
|
||||
yield new Uint8Array(chunk)
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-expect-error trust me bro
|
||||
function iteratorToStream(iterator) {
|
||||
return new ReadableStream({
|
||||
async pull(controller) {
|
||||
const { value, done } = await iterator.next()
|
||||
if (done) {
|
||||
controller.close()
|
||||
} else {
|
||||
controller.enqueue(value)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function streamFile(path: string, range?: ByteRange): ReadableStream {
|
||||
const nodeStream = createReadStream(path, range)
|
||||
return iteratorToStream(nodeStreamToIterator(nodeStream))
|
||||
}
|
||||
|
||||
export 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")
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
import type { Doc } from "@/convex/_generated/dataModel"
|
||||
import crypto from "node:crypto"
|
||||
import fs from "node:fs/promises"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { spawn } from "node:child_process"
|
||||
import * as music from "music-metadata"
|
||||
|
||||
export const HLS_SEGMENT_SECONDS = 6
|
||||
|
||||
type HlsJob = {
|
||||
dir: string
|
||||
duration: number
|
||||
segmentCount: number
|
||||
startedAt: number
|
||||
processExited: boolean
|
||||
processExitCode: number | null
|
||||
}
|
||||
|
||||
const jobs = new Map<string, HlsJob>()
|
||||
|
||||
export function qualityKbps(quality: string | null) {
|
||||
if (quality === "low") {
|
||||
return 64
|
||||
}
|
||||
|
||||
if (quality === "high") {
|
||||
return 256
|
||||
}
|
||||
|
||||
return 128
|
||||
}
|
||||
|
||||
export function selectHlsSourceCodec(
|
||||
song: Doc<"songs">,
|
||||
quality: string | null,
|
||||
preferAtmos: boolean
|
||||
) {
|
||||
let sourceCodec = "lossless" in song.files ? "lossless" : "normal"
|
||||
if (preferAtmos && "atmos" in song.files) {
|
||||
sourceCodec = "atmos"
|
||||
} else if (quality === "high" && "normal" in song.files) {
|
||||
sourceCodec = "normal"
|
||||
}
|
||||
|
||||
return sourceCodec
|
||||
}
|
||||
|
||||
function hlsCacheRoot() {
|
||||
return path.join(os.tmpdir(), "otunes-hls")
|
||||
}
|
||||
|
||||
async function hlsJobKey(
|
||||
song: Doc<"songs">,
|
||||
sourcePath: string,
|
||||
kbps: number
|
||||
) {
|
||||
const stat = await fs.stat(sourcePath)
|
||||
return crypto
|
||||
.createHash("sha256")
|
||||
.update(song._id)
|
||||
.update("\0")
|
||||
.update(sourcePath)
|
||||
.update("\0")
|
||||
.update(String(stat.size))
|
||||
.update("\0")
|
||||
.update(String(stat.mtimeMs))
|
||||
.update("\0")
|
||||
.update(String(kbps))
|
||||
.digest("hex")
|
||||
}
|
||||
|
||||
export function hlsSegmentPath(job: HlsJob, segmentIndex: number) {
|
||||
return path.join(job.dir, `segment_${segmentIndex.toString().padStart(5, "0")}.ts`)
|
||||
}
|
||||
|
||||
async function touchHlsJob(job: HlsJob) {
|
||||
const now = new Date()
|
||||
await fs.utimes(job.dir, now, now).catch(() => {})
|
||||
}
|
||||
|
||||
export async function waitForHlsSegment(
|
||||
job: HlsJob,
|
||||
segmentIndex: number,
|
||||
timeoutMs = 30000
|
||||
) {
|
||||
await touchHlsJob(job)
|
||||
const segmentPath = hlsSegmentPath(job, segmentIndex)
|
||||
const startedAt = Date.now()
|
||||
|
||||
while (Date.now() - startedAt < timeoutMs) {
|
||||
try {
|
||||
const firstStat = await fs.stat(segmentPath)
|
||||
if (firstStat.size > 0) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
||||
const secondStat = await fs.stat(segmentPath)
|
||||
if (firstStat.size === secondStat.size) {
|
||||
return segmentPath
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
if (job.processExited && job.processExitCode !== 0) {
|
||||
throw new Error(`HLS transcode failed with code ${job.processExitCode}`)
|
||||
}
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
||||
}
|
||||
|
||||
throw new Error("Timed out waiting for HLS segment")
|
||||
}
|
||||
|
||||
export async function ensureHlsJob(
|
||||
song: Doc<"songs">,
|
||||
quality: string | null,
|
||||
preferAtmos: boolean
|
||||
) {
|
||||
const kbps = qualityKbps(quality)
|
||||
const sourceCodec = selectHlsSourceCodec(song, quality, preferAtmos)
|
||||
const sourcePath = song.files[sourceCodec]
|
||||
const key = await hlsJobKey(song, sourcePath, kbps)
|
||||
const existingJob = jobs.get(key)
|
||||
|
||||
if (existingJob) {
|
||||
await touchHlsJob(existingJob)
|
||||
return existingJob
|
||||
}
|
||||
|
||||
const metadata = await music.parseFile(sourcePath)
|
||||
const duration = metadata.format.duration ?? song.length
|
||||
const segmentCount = Math.max(1, Math.ceil(duration / HLS_SEGMENT_SECONDS))
|
||||
const dir = path.join(hlsCacheRoot(), key)
|
||||
await fs.mkdir(dir, { recursive: true })
|
||||
|
||||
const job: HlsJob = {
|
||||
dir,
|
||||
duration,
|
||||
segmentCount,
|
||||
startedAt: Date.now(),
|
||||
processExited: false,
|
||||
processExitCode: null,
|
||||
}
|
||||
jobs.set(key, job)
|
||||
await touchHlsJob(job)
|
||||
|
||||
const ffmpegArgs = [
|
||||
"-nostdin",
|
||||
"-i",
|
||||
sourcePath,
|
||||
"-vn",
|
||||
"-c:a",
|
||||
"aac",
|
||||
"-b:a",
|
||||
`${kbps}k`,
|
||||
"-ac",
|
||||
"2",
|
||||
"-f",
|
||||
"hls",
|
||||
"-hls_time",
|
||||
String(HLS_SEGMENT_SECONDS),
|
||||
"-hls_playlist_type",
|
||||
"vod",
|
||||
"-hls_list_size",
|
||||
"0",
|
||||
"-hls_segment_type",
|
||||
"mpegts",
|
||||
"-hls_segment_filename",
|
||||
path.join(dir, "segment_%05d.ts"),
|
||||
path.join(dir, "index.m3u8"),
|
||||
]
|
||||
const ffmpegProcess = spawn("ffmpeg", ffmpegArgs)
|
||||
|
||||
ffmpegProcess.stderr.on("data", (data) => {
|
||||
console.error(`FFmpeg HLS log: ${data.toString()}`)
|
||||
})
|
||||
|
||||
ffmpegProcess.on("close", (code) => {
|
||||
job.processExited = true
|
||||
job.processExitCode = code
|
||||
})
|
||||
|
||||
ffmpegProcess.on("error", () => {
|
||||
job.processExited = true
|
||||
job.processExitCode = 1
|
||||
})
|
||||
|
||||
return job
|
||||
}
|
||||
@@ -15,3 +15,19 @@ export function songPlayUrl(
|
||||
const query = params.toString()
|
||||
return `/api/song/${songId}/play${query ? `?${query}` : ""}`
|
||||
}
|
||||
|
||||
export function songHlsPlaylistUrl(
|
||||
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/hls${query ? `?${query}` : ""}`
|
||||
}
|
||||
Generated
+7
@@ -17,6 +17,7 @@
|
||||
"convex": "^1.40.0",
|
||||
"date-fns": "^4.4.0",
|
||||
"embla-carousel-react": "^8.6.0",
|
||||
"hls.js": "^1.6.16",
|
||||
"input-otp": "^1.4.2",
|
||||
"lucide-react": "^1.17.0",
|
||||
"music-metadata": "11.12.3",
|
||||
@@ -8394,6 +8395,12 @@
|
||||
"hermes-estree": "0.25.1"
|
||||
}
|
||||
},
|
||||
"node_modules/hls.js": {
|
||||
"version": "1.6.16",
|
||||
"resolved": "https://registry.npmjs.org/hls.js/-/hls.js-1.6.16.tgz",
|
||||
"integrity": "sha512-VSIRpLfRwlAAdGL4wiTucx2ScRipo0ed1FBatWkyt832jC4CReKstga6yIhYVwGu9LOBjuX9wzmRMeQdBJtzEA==",
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/hono": {
|
||||
"version": "4.12.23",
|
||||
"resolved": "https://registry.npmjs.org/hono/-/hono-4.12.23.tgz",
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
"convex": "^1.40.0",
|
||||
"date-fns": "^4.4.0",
|
||||
"embla-carousel-react": "^8.6.0",
|
||||
"hls.js": "1.6.16",
|
||||
"input-otp": "^1.4.2",
|
||||
"lucide-react": "^1.17.0",
|
||||
"music-metadata": "11.12.3",
|
||||
|
||||
Reference in new issue
Block a user