// A recording's HLS playlists (RFC 8216): the media playlist over its fragmented-MP4 segments (§4.3: EXT-X-MAP names // each segment's init, EXT-X-DISCONTINUITY where the init changes, EXT-X-ENDLIST once the recording ended) and a // multivariant playlist naming that one rendition (§4.3.4.2: its bandwidth, codecs, resolution and frame rate). Pure. import type { LiveAudio, LiveVideo } from './types.ts'; /** A segment as the recording keeps it, with the metadata of the init it plays under. */ export type HlsSegment = { seq: number; init: number; duration: number; size: number; video?: LiveVideo; audio?: LiveAudio }; const DEFAULT_WINDOW = 6; /** The media playlist: the last `window` segments while the recording is published, every segment once it ended (or * with `window: 'all'`, an event playlist). */ export function mediaPlaylist( recording: { state: 'publishing' | 'ended'; window?: number }, segments: ReadonlyArray, options: { window?: number | 'all'; uri?: (file: string) => string } = {}, ): string { const uri = options.uri ?? ((file: string) => file); const ended = recording.state === 'ended'; const window = options.window ?? recording.window ?? DEFAULT_WINDOW; const listed = ended || window === 'all' ? [...segments] : segments.slice(Math.max(0, segments.length - Math.max(1, window))); // every EXTINF, rounded, is at most the target (§4.3.3.1) const target = Math.max(1, ...segments.map((s) => Math.round(s.duration))); const first = listed[0]; const lines = ['#EXTM3U', '#EXT-X-VERSION:7', `#EXT-X-TARGETDURATION:${target}`, `#EXT-X-MEDIA-SEQUENCE:${first?.seq ?? 0}`]; // each init after the recording's first is one discontinuity: those before the window are its sequence (§4.3.3.3) if (first && first.init > 0) lines.push(`#EXT-X-DISCONTINUITY-SEQUENCE:${first.init}`); if (ended) lines.push('#EXT-X-PLAYLIST-TYPE:VOD'); else if (window === 'all') lines.push('#EXT-X-PLAYLIST-TYPE:EVENT'); lines.push('#EXT-X-INDEPENDENT-SEGMENTS'); let init: number | undefined; for (const s of listed) { if (s.init !== init) { if (init !== undefined) lines.push('#EXT-X-DISCONTINUITY'); lines.push(`#EXT-X-MAP:URI="${uri(`init-${s.init}.mp4`)}"`); init = s.init; } lines.push(`#EXTINF:${s.duration.toFixed(3)},`, uri(`seg-${s.seq}.m4s`)); } if (ended) lines.push('#EXT-X-ENDLIST'); return `${lines.join('\n')}\n`; } /** The multivariant playlist naming the recording's one rendition, its peak bandwidth measured from its segments. */ export function masterPlaylist( recording: { video?: LiveVideo; audio?: LiveAudio }, segments: ReadonlyArray, media: string, ): string { return renditionPlaylist({ codecs: [recording.video?.codec, recording.audio?.codec].filter((codec): codec is string => !!codec), ...(recording.video ? { width: recording.video.width, height: recording.video.height, frameRate: recording.video.frameRate } : {}), }, segments, media); } /** A broadcast view's master describes its own retained segments, independently of the source's current encoder. */ export function rangedMasterPlaylist(segments: ReadonlyArray, media: string): string { const first = segments[0]?.video; const codecs = segments.every((s) => s.video || s.audio) ? [...new Set(segments.flatMap((s) => [s.video?.codec, s.audio?.codec].filter((codec): codec is string => !!codec)))] : []; const sameSize = first && segments.every((s) => s.video?.width === first.width && s.video?.height === first.height); const sameRate = first?.frameRate && segments.every((s) => s.video?.frameRate === first.frameRate); return renditionPlaylist({ codecs, ...(sameSize ? { width: first.width, height: first.height } : {}), ...(sameRate ? { frameRate: first.frameRate } : {}), }, segments, media); } function renditionPlaylist( metadata: { codecs: ReadonlyArray; width?: number; height?: number; frameRate?: number }, segments: ReadonlyArray, media: string, ): string { const peak = Math.max(1, ...segments.filter((s) => s.duration > 0).map((s) => Math.ceil((s.size * 8) / s.duration))); const codecs = metadata.codecs.join(','); const attributes = [ `BANDWIDTH=${peak}`, ...(codecs ? [`CODECS="${codecs}"`] : []), ...(metadata.width && metadata.height ? [`RESOLUTION=${metadata.width}x${metadata.height}`] : []), ...(metadata.frameRate ? [`FRAME-RATE=${metadata.frameRate.toFixed(3)}`] : []), ]; return `#EXTM3U\n#EXT-X-VERSION:7\n#EXT-X-INDEPENDENT-SEGMENTS\n#EXT-X-STREAM-INF:${attributes.join(',')}\n${media}\n`; }