-
-
Notifications
You must be signed in to change notification settings - Fork 739
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: add hls support for internal streams (#525)
- Loading branch information
Showing
3 changed files
with
63 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { createInternalStream } from './manage.js'; | ||
import HLS from 'hls-parser'; | ||
import path from "node:path"; | ||
|
||
function transformObject(streamInfo, hlsObject) { | ||
if (hlsObject === undefined) { | ||
return (object) => transformObject(streamInfo, object); | ||
} | ||
|
||
const fullUrl = hlsObject.uri.startsWith("/") | ||
? new URL(hlsObject.uri, streamInfo.url).toString() | ||
: new URL(path.join(streamInfo.url, "/../", hlsObject.uri)).toString(); | ||
hlsObject.uri = createInternalStream(fullUrl, streamInfo); | ||
|
||
return hlsObject; | ||
} | ||
|
||
function transformMasterPlaylist(streamInfo, hlsPlaylist) { | ||
const makeInternalStream = transformObject(streamInfo); | ||
|
||
const makeInternalVariants = (variant) => { | ||
variant = transformObject(streamInfo, variant); | ||
variant.video = variant.video.map(makeInternalStream); | ||
variant.audio = variant.audio.map(makeInternalStream); | ||
return variant; | ||
}; | ||
hlsPlaylist.variants = hlsPlaylist.variants.map(makeInternalVariants); | ||
|
||
return hlsPlaylist; | ||
} | ||
|
||
function transformMediaPlaylist(streamInfo, hlsPlaylist) { | ||
const makeInternalSegments = transformObject(streamInfo); | ||
hlsPlaylist.segments = hlsPlaylist.segments.map(makeInternalSegments); | ||
hlsPlaylist.prefetchSegments = hlsPlaylist.prefetchSegments.map(makeInternalSegments); | ||
return hlsPlaylist; | ||
} | ||
|
||
const HLS_MIME_TYPES = ["application/vnd.apple.mpegurl", "audio/mpegurl", "application/x-mpegURL"]; | ||
|
||
export function isHlsRequest (req) { | ||
return HLS_MIME_TYPES.includes(req.headers['content-type']); | ||
} | ||
|
||
export async function handleHlsPlaylist(streamInfo, req, res) { | ||
let hlsPlaylist = await req.body.text(); | ||
hlsPlaylist = HLS.parse(hlsPlaylist); | ||
|
||
hlsPlaylist = hlsPlaylist.isMasterPlaylist | ||
? transformMasterPlaylist(streamInfo, hlsPlaylist) | ||
: transformMediaPlaylist(streamInfo, hlsPlaylist); | ||
|
||
hlsPlaylist = HLS.stringify(hlsPlaylist); | ||
|
||
res.send(hlsPlaylist); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters