Files
otunes/apps/web/lib/server-hls.ts
T
joshuafhiggins 9da758b196 perf: optimize app rendering and data loading (#18)
- Virtualize mobile lists and defer queue loading
- Split playback state from actions to reduce rerenders
- Skip invalid or unnecessary Convex queries

Reviewed-on: #18
2026-09-05 16:35:37 -04:00

247 lines
5.6 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 { spawn } from "node:child_process"
import * as music from "music-metadata"
export const HLS_SEGMENT_SECONDS = 6
const HLS_MAX_IDLE_MS = 30 * 60 * 1000
type HlsJob = {
key: string
lookupKey: string
dir: string
duration: number
segmentCount: number
lastTouchedAt: number
processExited: boolean
processExitCode: number | null
}
const jobs = new Map<string, HlsJob>()
const jobsByLookup = new Map<string, HlsJob>()
let lastCleanupAt = 0
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 "/tmp/otunes"
}
function hlsLookupKey(
songId: string,
quality: string | null,
preferAtmos: boolean
) {
return `${songId}:${quality ?? ""}:${preferAtmos ? "1" : "0"}`
}
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) {
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)
}
}
async function evictJob(job: HlsJob) {
forgetJob(job)
await fs.rm(job.dir, { recursive: true, force: true }).catch(() => {})
}
async function cleanupIdleHlsJobs() {
const now = Date.now()
if (now - lastCleanupAt < 60_000) {
return
}
lastCleanupAt = now
for (const job of [...jobs.values()]) {
if (now - job.lastTouchedAt > HLS_MAX_IDLE_MS) {
await evictJob(job)
}
}
}
export function getExistingHlsJob(
songId: string,
quality: string | null,
preferAtmos: boolean
) {
return 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) {
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
) {
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)
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
}