133 lines
3.3 KiB
TypeScript
133 lines
3.3 KiB
TypeScript
export type PlaybackQuality = "low" | "medium" | "high" | "lossless"
|
|
|
|
const PLAYBACK_QUALITIES: readonly string[] = [
|
|
"low",
|
|
"medium",
|
|
"high",
|
|
"lossless",
|
|
] satisfies PlaybackQuality[]
|
|
|
|
/** Reads a `quality` request parameter; anything unknown means the default. */
|
|
export function parsePlaybackQuality(
|
|
value: string | null | undefined
|
|
): PlaybackQuality | undefined {
|
|
return value && PLAYBACK_QUALITIES.includes(value)
|
|
? (value as PlaybackQuality)
|
|
: undefined
|
|
}
|
|
|
|
export function qualityKbps(quality: PlaybackQuality | null | undefined) {
|
|
if (quality === "low") {
|
|
return 64
|
|
}
|
|
|
|
if (quality === "high") {
|
|
return 256
|
|
}
|
|
|
|
if (quality === "medium") {
|
|
return 128
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
export type PlaybackSource = "atmos" | "lossless" | "normal"
|
|
|
|
export type PlaybackStreamPlan = {
|
|
playbackQuality: PlaybackQuality
|
|
preferAtmos: boolean
|
|
/** The file that is streamed as-is, or transcoded when `transcodeNeeded`. */
|
|
source: PlaybackSource
|
|
/** AAC bitrate to transcode to; 0 for lossless quality. */
|
|
kbps: number
|
|
transcodeNeeded: boolean
|
|
useHls: boolean
|
|
}
|
|
|
|
export function playbackStreamPlan(
|
|
files: Record<string, unknown>,
|
|
quality: PlaybackQuality | undefined,
|
|
preferAtmos: boolean
|
|
): PlaybackStreamPlan {
|
|
const playbackQuality = quality ?? "medium"
|
|
const kbps = qualityKbps(playbackQuality)
|
|
const effectivePreferAtmos = preferAtmos && "atmos" in files
|
|
|
|
let source: PlaybackSource = "lossless" in files ? "lossless" : "normal"
|
|
let transcodeNeeded = true
|
|
|
|
if (effectivePreferAtmos) {
|
|
source = "atmos"
|
|
transcodeNeeded = false
|
|
} else if (kbps === 0 && "lossless" in files) {
|
|
source = "lossless"
|
|
transcodeNeeded = false
|
|
} else if (kbps === 0 || (kbps === 256 && "normal" in files)) {
|
|
source = "normal"
|
|
transcodeNeeded = false
|
|
}
|
|
|
|
return {
|
|
playbackQuality,
|
|
preferAtmos: effectivePreferAtmos,
|
|
source,
|
|
kbps,
|
|
transcodeNeeded,
|
|
useHls: transcodeNeeded,
|
|
}
|
|
}
|
|
|
|
function songStreamQuery(
|
|
params: URLSearchParams,
|
|
quality: PlaybackQuality | undefined,
|
|
preferAtmos: boolean | undefined
|
|
) {
|
|
if (quality) {
|
|
params.set("quality", quality)
|
|
}
|
|
if (preferAtmos) {
|
|
params.set("preferAtmos", "true")
|
|
}
|
|
const query = params.toString()
|
|
return query ? `?${query}` : ""
|
|
}
|
|
|
|
export function songPlayPath(
|
|
songId: string,
|
|
quality: PlaybackQuality | undefined,
|
|
preferAtmos: boolean | undefined,
|
|
options?: { losslessFormat?: "alac" }
|
|
) {
|
|
const params = new URLSearchParams()
|
|
if (options?.losslessFormat) {
|
|
params.set("losslessFormat", options.losslessFormat)
|
|
}
|
|
return `/api/song/${songId}/play${songStreamQuery(params, quality, preferAtmos)}`
|
|
}
|
|
|
|
export function songHlsPlaylistPath(
|
|
songId: string,
|
|
quality: PlaybackQuality | undefined,
|
|
preferAtmos: boolean | undefined
|
|
) {
|
|
const query = songStreamQuery(new URLSearchParams(), quality, preferAtmos)
|
|
return `/api/song/${songId}/play/hls${query}`
|
|
}
|
|
|
|
export type PlaybackFormat = "atmos" | "lossless"
|
|
|
|
/** The labeled format a song streams in under the plan, if any. */
|
|
export function playbackFormat(
|
|
files: Record<string, unknown>,
|
|
quality: PlaybackQuality | undefined,
|
|
preferAtmos: boolean
|
|
): PlaybackFormat | null {
|
|
const plan = playbackStreamPlan(files, quality, preferAtmos)
|
|
if (plan.preferAtmos) return "atmos"
|
|
if (plan.playbackQuality === "lossless" && "lossless" in files) {
|
|
return "lossless"
|
|
}
|
|
return null
|
|
}
|