ActivityPub-federated video streaming platform using P2P directly in your web browser
// One definition of the files a live directory contains
// Shared by the server and runners
export const LIVE_SEGMENT_EXTENSION = '.ts'
export const LIVE_PLAYLIST_EXTENSION = '.m3u8'
export function isLiveSegmentPath (path: string) {
return path.endsWith(LIVE_SEGMENT_EXTENSION)
}
export function isLivePlaylistPath (path: string) {
return path.endsWith(LIVE_PLAYLIST_EXTENSION)
}
// Our transcoding process generates the segments of a live as `<playlist id>-<counter>.ts`
// Returns undefined if the file was not generated by our transcoding process
export function getLivePlaylistIdFromSegmentPath (segmentPath: string) {
return parseLiveSegmentFilename(getFilename(segmentPath))?.playlistId
}
// Returns undefined if the file was not generated by our transcoding process
export function getLivePlaylistNameFromSegmentPath (segmentPath: string) {
const playlistId = getLivePlaylistIdFromSegmentPath(segmentPath)
if (playlistId === undefined) return undefined
return playlistId + LIVE_PLAYLIST_EXTENSION
}
// Order the files of a live directory the way the transcoding process generates them:
// * the playlists first, because the handlers of a segment need the playlist that references it
// * then the segments of a same playlist by their counter: it is zero padded (%v-%06d.ts) but it grows past 6 digits
// after 999999 segments, so a lexicographic sort would put the newest segments of a permanent live first
export function compareLiveFilenames (a: string, b: string) {
const aSegment = parseLiveSegmentFilename(a)
const bSegment = parseLiveSegmentFilename(b)
if (!aSegment || !bSegment) {
if (aSegment) return 1
if (bSegment) return -1
return compareStrings(a, b)
}
if (aSegment.playlistId !== bSegment.playlistId) return compareStrings(aSegment.playlistId, bSegment.playlistId)
return aSegment.counter - bSegment.counter
}
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
function parseLiveSegmentFilename (filename: string) {
if (!isLiveSegmentPath(filename)) return undefined
const matches = filename.match(/^(\d+)-(\d+)\.ts$/)
if (!matches) return undefined
return { playlistId: matches[1], counter: parseInt(matches[2], 10) }
}
function getFilename (path: string) {
return path.split(/[/\\]/).pop()
}
function compareStrings (a: string, b: string) {
if (a < b) return -1
if (a > b) return 1
return 0
}