Compare commits
1
Commits
No files matched your search
@@ -3,11 +3,11 @@ import type { Id } from "@otunes/convex/_generated/dataModel"
|
||||
import { audioContentTypeForPath } from "@/lib/audio-content-type"
|
||||
import { seekableLosslessFile } from "@/lib/server-lossless"
|
||||
import { getConvexClient } from "@/lib/convex-server"
|
||||
import { audioInfo } from "@/lib/server-audio-info"
|
||||
import { ffmpegToReadableStream, streamFile } from "@/lib/server-audio-stream"
|
||||
import { spawnFfmpeg } from "@/lib/server-ffmpeg"
|
||||
import { NextRequest, NextResponse, userAgent } from "next/server"
|
||||
import { spawn } from "node:child_process"
|
||||
import fs from "node:fs/promises"
|
||||
import * as music from "music-metadata"
|
||||
|
||||
function parseByteRange(rangeHeader: string | null, fileSize: number) {
|
||||
if (!rangeHeader) {
|
||||
@@ -151,14 +151,15 @@ export async function GET(
|
||||
transcodeNeeded = false
|
||||
}
|
||||
|
||||
const metadata = await music.parseFile(song.files[originalCodec])
|
||||
|
||||
if (!transcodeNeeded && originalCodec === "lossless") {
|
||||
// Native iOS playback does not identify itself as Safari.
|
||||
const acceptsAlac =
|
||||
browser.name === "Safari" ||
|
||||
request.nextUrl.searchParams.get("losslessFormat") === "alac"
|
||||
if (acceptsAlac && metadata.format.codec === "ALAC") {
|
||||
if (
|
||||
acceptsAlac &&
|
||||
(await audioInfo(song.files[originalCodec])).codec === "ALAC"
|
||||
) {
|
||||
try {
|
||||
return await streamFileResponse(
|
||||
request,
|
||||
@@ -183,11 +184,6 @@ export async function GET(
|
||||
(originalCodec === "atmos" || originalCodec === "normal")
|
||||
) {
|
||||
// TODO: Dolby Atmos doesn't work on Google Chrome
|
||||
console.log(
|
||||
metadata.format.codec,
|
||||
metadata.format.container,
|
||||
metadata.format.codecProfile
|
||||
)
|
||||
try {
|
||||
return await streamFileResponse(
|
||||
request,
|
||||
@@ -200,7 +196,6 @@ export async function GET(
|
||||
}
|
||||
|
||||
const ffmpegArgs = [
|
||||
"-nostdin",
|
||||
"-i",
|
||||
song.files[originalCodec],
|
||||
"-c:a",
|
||||
@@ -214,8 +209,8 @@ export async function GET(
|
||||
|
||||
try {
|
||||
// TODO: Fix seeking for this
|
||||
const ffmpegProcess = spawn("ffmpeg", ffmpegArgs)
|
||||
const stream = ffmpegToReadableStream(ffmpegProcess)
|
||||
const ffmpeg = await spawnFfmpeg(ffmpegArgs, request.signal)
|
||||
const stream = ffmpegToReadableStream(ffmpeg)
|
||||
|
||||
return new NextResponse(stream, {
|
||||
status: 200,
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import fs from "node:fs/promises"
|
||||
import * as music from "music-metadata"
|
||||
|
||||
const MAX_ENTRIES = 1000
|
||||
|
||||
export type AudioInfo = {
|
||||
codec: string | undefined
|
||||
duration: number | undefined
|
||||
}
|
||||
|
||||
const cache = new Map<string, Promise<AudioInfo>>()
|
||||
|
||||
/** Codec info for a file, parsed once per file version. */
|
||||
export async function audioInfo(file: string): Promise<AudioInfo> {
|
||||
const stat = await fs.stat(file)
|
||||
const key = `${file}\0${stat.size}\0${stat.mtimeMs}`
|
||||
|
||||
const cached = cache.get(key)
|
||||
if (cached) {
|
||||
cache.delete(key)
|
||||
cache.set(key, cached)
|
||||
return cached
|
||||
}
|
||||
|
||||
const info = music.parseFile(file).then(({ format }) => ({
|
||||
codec: format.codec,
|
||||
duration: format.duration,
|
||||
}))
|
||||
cache.set(key, info)
|
||||
info.catch(() => {
|
||||
if (cache.get(key) === info) {
|
||||
cache.delete(key)
|
||||
}
|
||||
})
|
||||
|
||||
if (cache.size > MAX_ENTRIES) {
|
||||
cache.delete(cache.keys().next().value!)
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
@@ -1,96 +1,96 @@
|
||||
import { type ChildProcessWithoutNullStreams } from "node:child_process"
|
||||
import { createReadStream, ReadStream } from "node:fs"
|
||||
import { createReadStream } from "node:fs"
|
||||
import type { Readable } from "node:stream"
|
||||
import type { FfmpegProcess } from "@/lib/server-ffmpeg"
|
||||
|
||||
const HIGH_WATER_MARK_BYTES = 256 * 1024
|
||||
|
||||
export type ByteRange = {
|
||||
start: number
|
||||
end: number
|
||||
}
|
||||
|
||||
async function* nodeStreamToIterator(stream: ReadStream) {
|
||||
for await (const chunk of stream) {
|
||||
yield new Uint8Array(chunk)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Wraps a Node stream, pausing it while the consumer is behind. `cancel` runs
|
||||
* when the consumer goes away (seek, track change, disconnect). `finish` runs
|
||||
* when the source ends and may reject to error the stream.
|
||||
*/
|
||||
function toWebStream(
|
||||
source: Readable,
|
||||
{
|
||||
cancel,
|
||||
finish,
|
||||
}: { cancel: () => void; finish?: () => Promise<void> }
|
||||
): ReadableStream<Uint8Array> {
|
||||
let done = false
|
||||
|
||||
// @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()
|
||||
return new ReadableStream<Uint8Array>(
|
||||
{
|
||||
start(controller) {
|
||||
const fail = (error: unknown) => {
|
||||
if (done) {
|
||||
return
|
||||
}
|
||||
} catch {
|
||||
// Consumer may have already closed the stream (e.g. track change).
|
||||
done = true
|
||||
controller.error(error)
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
ffmpegProcess.stdout.on("data", (chunk: Buffer) => {
|
||||
if (closed) {
|
||||
source.on("data", (chunk: Buffer) => {
|
||||
if (done) {
|
||||
return
|
||||
}
|
||||
controller.enqueue(new Uint8Array(chunk))
|
||||
if ((controller.desiredSize ?? 0) <= 0) {
|
||||
source.pause()
|
||||
}
|
||||
})
|
||||
source.once("error", fail)
|
||||
source.once("end", () => {
|
||||
if (done) {
|
||||
return
|
||||
}
|
||||
void (finish?.() ?? Promise.resolve()).then(() => {
|
||||
if (!done) {
|
||||
done = true
|
||||
controller.close()
|
||||
}
|
||||
}, fail)
|
||||
})
|
||||
},
|
||||
pull() {
|
||||
source.resume()
|
||||
},
|
||||
cancel() {
|
||||
if (done) {
|
||||
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))
|
||||
done = true
|
||||
cancel()
|
||||
},
|
||||
},
|
||||
cancel() {
|
||||
if (closed) {
|
||||
return
|
||||
new ByteLengthQueuingStrategy({ highWaterMark: HIGH_WATER_MARK_BYTES })
|
||||
)
|
||||
}
|
||||
|
||||
export function streamFile(path: string, range?: ByteRange) {
|
||||
const file = createReadStream(path, range)
|
||||
return toWebStream(file, { cancel: () => file.destroy() })
|
||||
}
|
||||
|
||||
/** Streams ffmpeg stdout and kills ffmpeg when the consumer goes away. */
|
||||
export function ffmpegToReadableStream({
|
||||
child,
|
||||
exited,
|
||||
stderrTail,
|
||||
}: FfmpegProcess) {
|
||||
return toWebStream(child.stdout, {
|
||||
cancel: () => child.kill("SIGKILL"),
|
||||
finish: async () => {
|
||||
const code = await exited
|
||||
if (code !== 0) {
|
||||
console.error(`ffmpeg transcode exited with code ${code}`, stderrTail())
|
||||
throw new Error(`ffmpeg exited with code ${code}`)
|
||||
}
|
||||
closed = true
|
||||
ffmpegProcess.kill("SIGKILL")
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import fs from "node:fs/promises"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
|
||||
const DEFAULT_MAX_MB = 2048
|
||||
const ORPHAN_MAX_AGE_MS = 60 * 60 * 1000
|
||||
/** Prefix for in-progress entries. Pruning removes old unpinned ones as abandoned. */
|
||||
export const CACHE_ORPHAN_PREFIX = "convert-"
|
||||
|
||||
type CacheState = {
|
||||
pinned: Map<string, number>
|
||||
pruning: Promise<void> | null
|
||||
}
|
||||
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
__otunesCache?: CacheState
|
||||
}
|
||||
const state = (globalState.__otunesCache ??= {
|
||||
pinned: new Map(),
|
||||
pruning: null,
|
||||
})
|
||||
|
||||
/**
|
||||
* Transcode cache root. Set OTUNES_CACHE_DIR to a persistent volume to keep
|
||||
* transcodes across restarts.
|
||||
*/
|
||||
export function cacheRoot() {
|
||||
return process.env.OTUNES_CACHE_DIR || path.join(os.tmpdir(), "otunes-cache")
|
||||
}
|
||||
|
||||
export function cacheDir(kind: "hls" | "lossless") {
|
||||
return path.join(cacheRoot(), kind)
|
||||
}
|
||||
|
||||
function maxCacheBytes() {
|
||||
const configured = Number(process.env.OTUNES_CACHE_MAX_MB)
|
||||
const mb = Number.isFinite(configured) && configured > 0 ? configured : DEFAULT_MAX_MB
|
||||
return mb * 1024 * 1024
|
||||
}
|
||||
|
||||
/** Pinned entries are never pruned. Pins are reference counted. */
|
||||
export function pinCacheEntry(entry: string) {
|
||||
state.pinned.set(entry, (state.pinned.get(entry) ?? 0) + 1)
|
||||
}
|
||||
|
||||
export function unpinCacheEntry(entry: string) {
|
||||
const count = state.pinned.get(entry) ?? 0
|
||||
if (count <= 1) {
|
||||
state.pinned.delete(entry)
|
||||
} else {
|
||||
state.pinned.set(entry, count - 1)
|
||||
}
|
||||
}
|
||||
|
||||
/** Marks an entry as recently used so pruning removes it last. */
|
||||
export async function touchCacheEntry(entry: string) {
|
||||
const now = new Date()
|
||||
await fs.utimes(entry, now, now)
|
||||
}
|
||||
|
||||
async function entrySize(entry: string): Promise<number> {
|
||||
const stat = await fs.stat(entry).catch(() => null)
|
||||
if (!stat) {
|
||||
return 0
|
||||
}
|
||||
if (!stat.isDirectory()) {
|
||||
return stat.size
|
||||
}
|
||||
const names = await fs.readdir(entry).catch(() => [])
|
||||
const sizes = await Promise.all(
|
||||
names.map((name) => entrySize(path.join(entry, name)))
|
||||
)
|
||||
return sizes.reduce((total, size) => total + size, 0)
|
||||
}
|
||||
|
||||
async function prune() {
|
||||
const entries: {
|
||||
path: string
|
||||
size: number
|
||||
mtimeMs: number
|
||||
orphan: boolean
|
||||
}[] = []
|
||||
|
||||
for (const kind of ["hls", "lossless"] as const) {
|
||||
const dir = cacheDir(kind)
|
||||
const names = await fs.readdir(dir).catch(() => [])
|
||||
for (const name of names) {
|
||||
const entry = path.join(dir, name)
|
||||
const stat = await fs.stat(entry).catch(() => null)
|
||||
if (!stat) {
|
||||
continue
|
||||
}
|
||||
entries.push({
|
||||
path: entry,
|
||||
size: await entrySize(entry),
|
||||
mtimeMs: stat.mtimeMs,
|
||||
orphan: name.startsWith(CACHE_ORPHAN_PREFIX),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const maxBytes = maxCacheBytes()
|
||||
const now = Date.now()
|
||||
let total = entries.reduce((sum, entry) => sum + entry.size, 0)
|
||||
entries.sort((a, b) => a.mtimeMs - b.mtimeMs)
|
||||
|
||||
for (const entry of entries) {
|
||||
if (state.pinned.has(entry.path)) {
|
||||
continue
|
||||
}
|
||||
const remove = entry.orphan
|
||||
? now - entry.mtimeMs > ORPHAN_MAX_AGE_MS
|
||||
: total > maxBytes
|
||||
if (remove) {
|
||||
await fs.rm(entry.path, { recursive: true, force: true }).catch(() => {})
|
||||
total -= entry.size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes least recently used entries until the cache fits under
|
||||
* OTUNES_CACHE_MAX_MB, plus abandoned in-progress entries.
|
||||
*/
|
||||
export function pruneCache() {
|
||||
state.pruning ??= prune()
|
||||
.catch((error) => console.error("Transcode cache prune failed", error))
|
||||
.finally(() => {
|
||||
state.pruning = null
|
||||
})
|
||||
return state.pruning
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process"
|
||||
import os from "node:os"
|
||||
|
||||
const STDERR_TAIL_BYTES = 4096
|
||||
|
||||
export type FfmpegProcess = {
|
||||
child: ChildProcessWithoutNullStreams
|
||||
/** Resolves with the exit code, or null when killed by a signal. */
|
||||
exited: Promise<number | null>
|
||||
stderrTail: () => string
|
||||
}
|
||||
|
||||
type Limiter = {
|
||||
running: number
|
||||
waiting: (() => void)[]
|
||||
}
|
||||
|
||||
// Kept on globalThis so dev reloads share one limit with processes they spawned.
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
__otunesFfmpegLimiter?: Limiter
|
||||
}
|
||||
const limiter = (globalState.__otunesFfmpegLimiter ??= {
|
||||
running: 0,
|
||||
waiting: [],
|
||||
})
|
||||
|
||||
function maxConcurrentFfmpeg() {
|
||||
const configured = Number(process.env.OTUNES_MAX_FFMPEG)
|
||||
if (Number.isInteger(configured) && configured > 0) {
|
||||
return configured
|
||||
}
|
||||
return Math.max(2, os.availableParallelism())
|
||||
}
|
||||
|
||||
async function acquireSlot(signal?: AbortSignal) {
|
||||
signal?.throwIfAborted()
|
||||
if (limiter.running < maxConcurrentFfmpeg()) {
|
||||
limiter.running += 1
|
||||
return
|
||||
}
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const grant = () => {
|
||||
signal?.removeEventListener("abort", onAbort)
|
||||
resolve()
|
||||
}
|
||||
const onAbort = () => {
|
||||
const index = limiter.waiting.indexOf(grant)
|
||||
if (index !== -1) {
|
||||
limiter.waiting.splice(index, 1)
|
||||
}
|
||||
reject(signal?.reason)
|
||||
}
|
||||
limiter.waiting.push(grant)
|
||||
signal?.addEventListener("abort", onAbort, { once: true })
|
||||
})
|
||||
}
|
||||
|
||||
function releaseSlot() {
|
||||
const next = limiter.waiting.shift()
|
||||
if (next) {
|
||||
// The slot passes straight to the next waiter.
|
||||
next()
|
||||
} else {
|
||||
limiter.running -= 1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawns ffmpeg once a slot under the concurrency cap is free. The slot is
|
||||
* held until the process exits. Rejects if `signal` aborts while queued.
|
||||
*/
|
||||
export async function spawnFfmpeg(
|
||||
args: string[],
|
||||
signal?: AbortSignal
|
||||
): Promise<FfmpegProcess> {
|
||||
await acquireSlot(signal)
|
||||
|
||||
let child: ChildProcessWithoutNullStreams
|
||||
try {
|
||||
child = spawn("ffmpeg", [
|
||||
"-nostdin",
|
||||
"-hide_banner",
|
||||
"-loglevel",
|
||||
"error",
|
||||
...args,
|
||||
])
|
||||
} catch (error) {
|
||||
releaseSlot()
|
||||
throw error
|
||||
}
|
||||
|
||||
// stderr must be drained or ffmpeg blocks once the pipe buffer fills.
|
||||
let stderr = ""
|
||||
child.stderr.on("data", (data: Buffer) => {
|
||||
stderr = (stderr + data.toString()).slice(-STDERR_TAIL_BYTES)
|
||||
})
|
||||
|
||||
const exited = new Promise<number | null>((resolve) => {
|
||||
let released = false
|
||||
const finish = (code: number | null) => {
|
||||
if (!released) {
|
||||
released = true
|
||||
releaseSlot()
|
||||
}
|
||||
resolve(code)
|
||||
}
|
||||
child.once("error", () => finish(1))
|
||||
child.once("close", (code) => finish(code))
|
||||
})
|
||||
|
||||
return { child, exited, stderrTail: () => stderr.trim() }
|
||||
}
|
||||
+183
-93
@@ -2,8 +2,15 @@ import type { Doc } from "@otunes/convex/_generated/dataModel"
|
||||
import crypto from "node:crypto"
|
||||
import fs from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { spawn } from "node:child_process"
|
||||
import * as music from "music-metadata"
|
||||
import { audioInfo } from "@/lib/server-audio-info"
|
||||
import {
|
||||
cacheDir,
|
||||
pinCacheEntry,
|
||||
pruneCache,
|
||||
touchCacheEntry,
|
||||
unpinCacheEntry,
|
||||
} from "@/lib/server-cache"
|
||||
import { spawnFfmpeg, type FfmpegProcess } from "@/lib/server-ffmpeg"
|
||||
|
||||
export const HLS_SEGMENT_SECONDS = 6
|
||||
const HLS_MAX_IDLE_MS = 30 * 60 * 1000
|
||||
@@ -17,11 +24,28 @@ type HlsJob = {
|
||||
lastTouchedAt: number
|
||||
processExited: boolean
|
||||
processExitCode: number | null
|
||||
process: FfmpegProcess | null
|
||||
abort: AbortController
|
||||
evicted: boolean
|
||||
}
|
||||
|
||||
const jobs = new Map<string, HlsJob>()
|
||||
const jobsByLookup = new Map<string, HlsJob>()
|
||||
let lastCleanupAt = 0
|
||||
type HlsState = {
|
||||
/** In-flight and ready jobs by cache key, so overlapping requests share one ffmpeg. */
|
||||
pending: Map<string, Promise<HlsJob>>
|
||||
jobs: Map<string, HlsJob>
|
||||
jobsByLookup: Map<string, HlsJob>
|
||||
lastCleanupAt: number
|
||||
}
|
||||
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
__otunesHls?: HlsState
|
||||
}
|
||||
const state = (globalState.__otunesHls ??= {
|
||||
pending: new Map(),
|
||||
jobs: new Map(),
|
||||
jobsByLookup: new Map(),
|
||||
lastCleanupAt: 0,
|
||||
})
|
||||
|
||||
export function qualityKbps(quality: string | null) {
|
||||
if (quality === "low") {
|
||||
@@ -50,10 +74,6 @@ export function selectHlsSourceCodec(
|
||||
return sourceCodec
|
||||
}
|
||||
|
||||
function hlsCacheRoot() {
|
||||
return "/tmp/otunes"
|
||||
}
|
||||
|
||||
function hlsLookupKey(
|
||||
songId: string,
|
||||
quality: string | null,
|
||||
@@ -88,31 +108,38 @@ export function hlsSegmentPath(job: HlsJob, segmentIndex: number) {
|
||||
|
||||
async function touchHlsJob(job: HlsJob) {
|
||||
job.lastTouchedAt = Date.now()
|
||||
await fs.utimes(job.dir, new Date(job.lastTouchedAt), new Date(job.lastTouchedAt)).catch(
|
||||
() => {}
|
||||
)
|
||||
}
|
||||
|
||||
function forgetJob(job: HlsJob) {
|
||||
jobs.delete(job.key)
|
||||
if (jobsByLookup.get(job.lookupKey) === job) {
|
||||
jobsByLookup.delete(job.lookupKey)
|
||||
}
|
||||
await touchCacheEntry(job.dir).catch(() => {})
|
||||
}
|
||||
|
||||
async function evictJob(job: HlsJob) {
|
||||
forgetJob(job)
|
||||
await fs.rm(job.dir, { recursive: true, force: true }).catch(() => {})
|
||||
if (job.evicted) {
|
||||
return
|
||||
}
|
||||
job.evicted = true
|
||||
state.pending.delete(job.key)
|
||||
state.jobs.delete(job.key)
|
||||
if (state.jobsByLookup.get(job.lookupKey) === job) {
|
||||
state.jobsByLookup.delete(job.lookupKey)
|
||||
}
|
||||
|
||||
job.abort.abort()
|
||||
job.process?.child.kill("SIGKILL")
|
||||
const complete = job.processExited && job.processExitCode === 0
|
||||
if (!complete) {
|
||||
await job.process?.exited
|
||||
await fs.rm(job.dir, { recursive: true, force: true }).catch(() => {})
|
||||
}
|
||||
unpinCacheEntry(job.dir)
|
||||
}
|
||||
|
||||
async function cleanupIdleHlsJobs() {
|
||||
const now = Date.now()
|
||||
if (now - lastCleanupAt < 60_000) {
|
||||
if (now - state.lastCleanupAt < 60_000) {
|
||||
return
|
||||
}
|
||||
lastCleanupAt = now
|
||||
state.lastCleanupAt = now
|
||||
|
||||
for (const job of [...jobs.values()]) {
|
||||
for (const job of [...state.jobs.values()]) {
|
||||
if (now - job.lastTouchedAt > HLS_MAX_IDLE_MS) {
|
||||
await evictJob(job)
|
||||
}
|
||||
@@ -124,7 +151,7 @@ export function getExistingHlsJob(
|
||||
quality: string | null,
|
||||
preferAtmos: boolean
|
||||
) {
|
||||
return jobsByLookup.get(hlsLookupKey(songId, quality, preferAtmos)) ?? null
|
||||
return state.jobsByLookup.get(hlsLookupKey(songId, quality, preferAtmos)) ?? null
|
||||
}
|
||||
|
||||
export async function waitForHlsSegment(
|
||||
@@ -137,18 +164,22 @@ export async function waitForHlsSegment(
|
||||
const startedAt = Date.now()
|
||||
|
||||
while (Date.now() - startedAt < timeoutMs) {
|
||||
// Read before checking the file so a segment renamed just before exit is found.
|
||||
const exited = job.processExited
|
||||
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
|
||||
}
|
||||
}
|
||||
// ffmpeg writes to a .tmp file and renames it when complete.
|
||||
await fs.access(segmentPath)
|
||||
return segmentPath
|
||||
} catch {
|
||||
if (job.processExited && job.processExitCode !== 0) {
|
||||
throw new Error(`HLS transcode failed with code ${job.processExitCode}`)
|
||||
if (job.evicted) {
|
||||
throw new Error("HLS job was evicted")
|
||||
}
|
||||
if (exited) {
|
||||
throw new Error(
|
||||
job.processExitCode === 0
|
||||
? "HLS segment does not exist"
|
||||
: `HLS transcode failed with code ${job.processExitCode}`
|
||||
)
|
||||
}
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
||||
@@ -157,52 +188,15 @@ export async function waitForHlsSegment(
|
||||
throw new Error("Timed out waiting for HLS segment")
|
||||
}
|
||||
|
||||
export async function ensureHlsJob(
|
||||
song: Doc<"songs">,
|
||||
quality: string | null,
|
||||
preferAtmos: boolean
|
||||
) {
|
||||
await cleanupIdleHlsJobs()
|
||||
|
||||
const kbps = qualityKbps(quality)
|
||||
const sourceCodec = selectHlsSourceCodec(song, quality, preferAtmos)
|
||||
const sourcePath = song.files[sourceCodec]
|
||||
const key = await hlsJobKey(song, sourcePath, kbps)
|
||||
const lookupKey = hlsLookupKey(song._id, quality, preferAtmos)
|
||||
const existingJob = jobs.get(key)
|
||||
|
||||
if (existingJob) {
|
||||
try {
|
||||
await fs.access(existingJob.dir)
|
||||
await touchHlsJob(existingJob)
|
||||
return existingJob
|
||||
} catch {
|
||||
forgetJob(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 = {
|
||||
key,
|
||||
lookupKey,
|
||||
dir,
|
||||
duration,
|
||||
segmentCount,
|
||||
lastTouchedAt: Date.now(),
|
||||
processExited: false,
|
||||
processExitCode: null,
|
||||
}
|
||||
jobs.set(key, job)
|
||||
jobsByLookup.set(lookupKey, job)
|
||||
await touchHlsJob(job)
|
||||
async function hasCompletePlaylist(dir: string) {
|
||||
const playlist = await fs
|
||||
.readFile(path.join(dir, "index.m3u8"), "utf8")
|
||||
.catch(() => "")
|
||||
return playlist.includes("#EXT-X-ENDLIST")
|
||||
}
|
||||
|
||||
function startTranscode(job: HlsJob, sourcePath: string, kbps: number) {
|
||||
const ffmpegArgs = [
|
||||
"-nostdin",
|
||||
"-i",
|
||||
sourcePath,
|
||||
"-vn",
|
||||
@@ -222,25 +216,121 @@ export async function ensureHlsJob(
|
||||
"0",
|
||||
"-hls_segment_type",
|
||||
"mpegts",
|
||||
"-hls_flags",
|
||||
"temp_file",
|
||||
"-hls_segment_filename",
|
||||
path.join(dir, "segment_%05d.ts"),
|
||||
path.join(dir, "index.m3u8"),
|
||||
path.join(job.dir, "segment_%05d.ts"),
|
||||
path.join(job.dir, "index.m3u8"),
|
||||
]
|
||||
const ffmpegProcess = spawn("ffmpeg", ffmpegArgs)
|
||||
|
||||
ffmpegProcess.stderr.on("data", (data) => {
|
||||
console.error(`FFmpeg HLS log: ${data.toString()}`)
|
||||
})
|
||||
spawnFfmpeg(ffmpegArgs, job.abort.signal).then(
|
||||
async (ffmpeg) => {
|
||||
job.process = ffmpeg
|
||||
if (job.evicted) {
|
||||
ffmpeg.child.kill("SIGKILL")
|
||||
}
|
||||
const code = await ffmpeg.exited
|
||||
job.process = null
|
||||
job.processExited = true
|
||||
job.processExitCode = code
|
||||
if (job.evicted) {
|
||||
return
|
||||
}
|
||||
if (code === 0) {
|
||||
void pruneCache()
|
||||
} else {
|
||||
console.error(`ffmpeg HLS exited with code ${code}`, ffmpeg.stderrTail())
|
||||
}
|
||||
},
|
||||
() => {
|
||||
job.processExited = true
|
||||
job.processExitCode = 1
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
ffmpegProcess.on("close", (code) => {
|
||||
job.processExited = true
|
||||
job.processExitCode = code
|
||||
})
|
||||
async function createHlsJob(
|
||||
song: Doc<"songs">,
|
||||
sourcePath: string,
|
||||
kbps: number,
|
||||
key: string,
|
||||
lookupKey: string
|
||||
): Promise<HlsJob> {
|
||||
const info = await audioInfo(sourcePath)
|
||||
const duration = info.duration ?? song.length
|
||||
const segmentCount = Math.max(1, Math.ceil(duration / HLS_SEGMENT_SECONDS))
|
||||
const dir = path.join(cacheDir("hls"), key)
|
||||
pinCacheEntry(dir)
|
||||
|
||||
ffmpegProcess.on("error", () => {
|
||||
job.processExited = true
|
||||
job.processExitCode = 1
|
||||
})
|
||||
try {
|
||||
const complete = await hasCompletePlaylist(dir)
|
||||
if (!complete) {
|
||||
await fs.rm(dir, { recursive: true, force: true })
|
||||
await fs.mkdir(dir, { recursive: true })
|
||||
}
|
||||
|
||||
const job: HlsJob = {
|
||||
key,
|
||||
lookupKey,
|
||||
dir,
|
||||
duration,
|
||||
segmentCount,
|
||||
lastTouchedAt: Date.now(),
|
||||
processExited: complete,
|
||||
processExitCode: complete ? 0 : null,
|
||||
process: null,
|
||||
abort: new AbortController(),
|
||||
evicted: false,
|
||||
}
|
||||
state.jobs.set(key, job)
|
||||
await touchHlsJob(job)
|
||||
|
||||
if (!complete) {
|
||||
startTranscode(job, sourcePath, kbps)
|
||||
}
|
||||
return job
|
||||
} catch (error) {
|
||||
unpinCacheEntry(dir)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function ensureHlsJob(
|
||||
song: Doc<"songs">,
|
||||
quality: string | null,
|
||||
preferAtmos: boolean
|
||||
): Promise<HlsJob> {
|
||||
await cleanupIdleHlsJobs()
|
||||
|
||||
const kbps = qualityKbps(quality)
|
||||
const sourceCodec = selectHlsSourceCodec(song, quality, preferAtmos)
|
||||
const sourcePath = song.files[sourceCodec]
|
||||
const key = await hlsJobKey(song, sourcePath, kbps)
|
||||
const lookupKey = hlsLookupKey(song._id, quality, preferAtmos)
|
||||
|
||||
let pending = state.pending.get(key)
|
||||
if (!pending) {
|
||||
pending = createHlsJob(song, sourcePath, kbps, key, lookupKey)
|
||||
state.pending.set(key, pending)
|
||||
const created = pending
|
||||
created.catch(() => {
|
||||
if (state.pending.get(key) === created) {
|
||||
state.pending.delete(key)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const job = await pending
|
||||
const exists = await fs.access(job.dir).then(
|
||||
() => true,
|
||||
() => false
|
||||
)
|
||||
if (job.evicted || !exists) {
|
||||
await evictJob(job)
|
||||
return ensureHlsJob(song, quality, preferAtmos)
|
||||
}
|
||||
|
||||
state.jobsByLookup.set(lookupKey, job)
|
||||
await touchHlsJob(job)
|
||||
return job
|
||||
}
|
||||
@@ -1,8 +1,15 @@
|
||||
import { spawn } from "node:child_process"
|
||||
import { createHash } from "node:crypto"
|
||||
import fs from "node:fs/promises"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import {
|
||||
CACHE_ORPHAN_PREFIX,
|
||||
cacheDir,
|
||||
pinCacheEntry,
|
||||
pruneCache,
|
||||
touchCacheEntry,
|
||||
unpinCacheEntry,
|
||||
} from "@/lib/server-cache"
|
||||
import { spawnFfmpeg } from "@/lib/server-ffmpeg"
|
||||
|
||||
const conversions = new Map<string, Promise<string>>()
|
||||
|
||||
@@ -27,56 +34,38 @@ export async function seekableLosslessFile(source: string): Promise<string> {
|
||||
}
|
||||
|
||||
async function convert(source: string, key: string): Promise<string> {
|
||||
const dir = path.join(os.tmpdir(), "otunes-lossless")
|
||||
const dir = cacheDir("lossless")
|
||||
await fs.mkdir(dir, { recursive: true })
|
||||
const cutoff = Date.now() - 30 * 60 * 1000
|
||||
await Promise.all(
|
||||
(await fs.readdir(dir))
|
||||
.filter((name) => name.endsWith(".flac"))
|
||||
.map(async (name) => {
|
||||
const file = path.join(dir, name)
|
||||
const stat = await fs.stat(file).catch(() => null)
|
||||
if (stat && stat.mtimeMs < cutoff) await fs.rm(file, { force: true })
|
||||
})
|
||||
)
|
||||
const output = path.join(dir, `${key}.flac`)
|
||||
try {
|
||||
await fs.access(output)
|
||||
// Every range request touches the file so it stays cached while playing.
|
||||
await touchCacheEntry(output)
|
||||
return output
|
||||
} catch {
|
||||
// Publish only completed conversions so range requests always see a complete file.
|
||||
}
|
||||
const temporary = path.join(
|
||||
await fs.mkdtemp(path.join(dir, "convert-")),
|
||||
"audio.flac"
|
||||
)
|
||||
const temporaryDir = await fs.mkdtemp(path.join(dir, CACHE_ORPHAN_PREFIX))
|
||||
const temporary = path.join(temporaryDir, "audio.flac")
|
||||
pinCacheEntry(temporaryDir)
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const child = spawn(
|
||||
"ffmpeg",
|
||||
[
|
||||
"-nostdin",
|
||||
"-v",
|
||||
"error",
|
||||
"-i",
|
||||
source,
|
||||
"-map",
|
||||
"0:a:0",
|
||||
"-c:a",
|
||||
"flac",
|
||||
temporary,
|
||||
],
|
||||
{ stdio: "ignore" }
|
||||
)
|
||||
child.on("error", reject)
|
||||
child.on("close", (code) => {
|
||||
if (code === 0) resolve()
|
||||
else reject(new Error(`Lossless conversion failed: ${code}`))
|
||||
})
|
||||
})
|
||||
const ffmpeg = await spawnFfmpeg([
|
||||
"-i",
|
||||
source,
|
||||
"-map",
|
||||
"0:a:0",
|
||||
"-c:a",
|
||||
"flac",
|
||||
temporary,
|
||||
])
|
||||
const code = await ffmpeg.exited
|
||||
if (code !== 0) {
|
||||
throw new Error(`Lossless conversion failed: ${code}: ${ffmpeg.stderrTail()}`)
|
||||
}
|
||||
await fs.rename(temporary, output)
|
||||
return output
|
||||
} finally {
|
||||
await fs.rm(path.dirname(temporary), { recursive: true, force: true })
|
||||
unpinCacheEntry(temporaryDir)
|
||||
await fs.rm(temporaryDir, { recursive: true, force: true })
|
||||
}
|
||||
void pruneCache()
|
||||
return output
|
||||
}
|
||||
Reference in new issue
Block a user