Files
otunes/apps/web/lib/server-hls.ts
T

340 lines
8.0 KiB
TypeScript

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 { 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
/** The server-only slice of a song needed to transcode it. */
export type SongSource = Pick<Doc<"songs">, "_id" | "files" | "length">
type HlsJob = {
key: string
lookupKey: string
dir: string
duration: number
segmentCount: number
lastTouchedAt: number
processExited: boolean
processExitCode: number | null
process: FfmpegProcess | null
abort: AbortController
evicted: boolean
}
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") {
return 64
}
if (quality === "high") {
return 256
}
return 128
}
export function selectHlsSourceCodec(
song: SongSource,
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 hlsLookupKey(
songId: string,
quality: string | null,
preferAtmos: boolean
) {
return `${songId}:${quality ?? ""}:${preferAtmos ? "1" : "0"}`
}
async function hlsJobKey(
song: SongSource,
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) {
job.lastTouchedAt = Date.now()
await touchCacheEntry(job.dir).catch(() => {})
}
async function evictJob(job: HlsJob) {
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 - state.lastCleanupAt < 60_000) {
return
}
state.lastCleanupAt = now
for (const job of [...state.jobs.values()]) {
if (now - job.lastTouchedAt > HLS_MAX_IDLE_MS) {
await evictJob(job)
}
}
}
export function getExistingHlsJob(
songId: string,
quality: string | null,
preferAtmos: boolean
) {
return state.jobsByLookup.get(hlsLookupKey(songId, quality, preferAtmos)) ?? null
}
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) {
// Read before checking the file so a segment renamed just before exit is found.
const exited = job.processExited
try {
// ffmpeg writes to a .tmp file and renames it when complete.
await fs.access(segmentPath)
return segmentPath
} catch {
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))
}
throw new Error("Timed out waiting for HLS segment")
}
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 = [
"-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_flags",
"temp_file",
"-hls_segment_filename",
path.join(job.dir, "segment_%05d.ts"),
path.join(job.dir, "index.m3u8"),
]
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
}
)
}
async function createHlsJob(
song: SongSource,
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)
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: SongSource,
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
}