mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
stream: add hls support for internal streams (#525)
This commit is contained in:
parent
4c8cd9dd30
commit
68f311c318
3 changed files with 63 additions and 8 deletions
56
src/modules/stream/internal-hls.js
Normal file
56
src/modules/stream/internal-hls.js
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ import { request } from 'undici';
|
||||||
import { Readable } from 'node:stream';
|
import { Readable } from 'node:stream';
|
||||||
import { assert } from 'console';
|
import { assert } from 'console';
|
||||||
import { getHeaders, pipe } from './shared.js';
|
import { getHeaders, pipe } from './shared.js';
|
||||||
|
import { handleHlsPlaylist, isHlsRequest } from './internal-hls.js';
|
||||||
|
|
||||||
const CHUNK_SIZE = BigInt(8e6); // 8 MB
|
const CHUNK_SIZE = BigInt(8e6); // 8 MB
|
||||||
const min = (a, b) => a < b ? a : b;
|
const min = (a, b) => a < b ? a : b;
|
||||||
|
@ -96,7 +97,11 @@ export async function internalStream(streamInfo, res) {
|
||||||
if (req.statusCode < 200 || req.statusCode > 299)
|
if (req.statusCode < 200 || req.statusCode > 299)
|
||||||
return res.end();
|
return res.end();
|
||||||
|
|
||||||
|
if (isHlsRequest(req)) {
|
||||||
|
await handleHlsPlaylist(streamInfo, req, res);
|
||||||
|
} else {
|
||||||
pipe(req.body, res, () => res.end());
|
pipe(req.body, res, () => res.end());
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
streamInfo.controller.abort();
|
streamInfo.controller.abort();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { randomBytes } from "crypto";
|
||||||
import { nanoid } from "nanoid";
|
import { nanoid } from "nanoid";
|
||||||
|
|
||||||
import { decryptStream, encryptStream, generateHmac } from "../sub/crypto.js";
|
import { decryptStream, encryptStream, generateHmac } from "../sub/crypto.js";
|
||||||
import { env, hlsExceptions } from "../config.js";
|
import { env } from "../config.js";
|
||||||
import { strict as assert } from "assert";
|
import { strict as assert } from "assert";
|
||||||
|
|
||||||
// optional dependency
|
// optional dependency
|
||||||
|
@ -106,12 +106,6 @@ export function destroyInternalStream(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapStream(streamInfo) {
|
function wrapStream(streamInfo) {
|
||||||
/* m3u8 links are currently not supported
|
|
||||||
* for internal streams, skip them */
|
|
||||||
if (hlsExceptions.includes(streamInfo.service)) {
|
|
||||||
return streamInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = streamInfo.urls;
|
const url = streamInfo.urls;
|
||||||
|
|
||||||
if (typeof url === 'string') {
|
if (typeof url === 'string') {
|
||||||
|
|
Loading…
Reference in a new issue