Skip to content

Commit

Permalink
fix: Simplified filtering for Stitcher
Browse files Browse the repository at this point in the history
  • Loading branch information
matvp91 committed Nov 16, 2024
1 parent 68c5368 commit cbc0ba1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 51 deletions.
15 changes: 9 additions & 6 deletions packages/stitcher/src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function filterMasterPlaylist(master: MasterPlaylist, filter: Filter) {
}
}

export function parseFilterQuery(query: Record<string, string>) {
export function getFilterFromQuery(query: Record<string, string>) {
const filter: Filter = {};
if ("filter.resolution" in query) {
filter.resolution = query["filter.resolution"];
Expand All @@ -89,9 +89,12 @@ export function parseFilterQuery(query: Record<string, string>) {
return filter;
}

export function filterQuery(filter?: Filter) {
return {
"filter.resolution": filter?.resolution,
"filter.audioLanguage": filter?.audioLanguage,
};
export function getQueryParamsFromFilter(filter: Filter) {
const queryParams: Record<string, string> = {};

Object.entries(filter).forEach(([key, value]) => {
queryParams[`filter.${key}`] = value;
});

return queryParams;
}
11 changes: 4 additions & 7 deletions packages/stitcher/src/interstitials.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "shared/assert";
import { Group } from "./lib/group";
import { buildProxyUrl, resolveUri } from "./lib/url";
import { makeUrl, resolveUri } from "./lib/url";
import { fetchDuration } from "./playlist";
import { getAdMediasFromAdBreak } from "./vast";
import { parseVmap } from "./vmap";
Expand Down Expand Up @@ -44,12 +44,9 @@ export function getStaticDateRanges(startTime: DateTime, session: Session) {
group.forEach((timeOffset, types) => {
const startDate = startTime.plus({ seconds: timeOffset });

const assetListUrl = buildProxyUrl(
`session/${session.id}/asset-list.json`,
{
startDate: startDate.toISO(),
},
);
const assetListUrl = makeUrl(`session/${session.id}/asset-list.json`, {
startDate: startDate.toISO(),
});

const clientAttributes: Record<string, number | string> = {
RESTRICT: "SKIP,JUMP",
Expand Down
30 changes: 1 addition & 29 deletions packages/stitcher/src/lib/url.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import * as path from "path";
import { encrypt } from "./crypto";
import { env } from "../env";
import { filterQuery } from "../filters";
import type { Filter } from "../filters";
import type { Session } from "../session";

const uuidRegex = /^[a-z,0-9,-]{36,36}$/;

Expand Down Expand Up @@ -63,33 +59,9 @@ export function joinUrl(urlFile: string, filePath: string) {
return `${url.protocol}//${url.host}${path.join(url.pathname, filePath)}`;
}

export function buildProxyUrl(
export function makeUrl(
path: string,
params: Record<string, string | undefined | null> = {},
) {
return buildUrl(`${env.PUBLIC_STITCHER_ENDPOINT}/${path}`, params);
}

export function buildProxyMasterUrl(params: {
url: string;
session?: Session;
filter?: Filter;
}) {
return buildProxyUrl("out/master.m3u8", {
eurl: encrypt(params.url),
sid: params.session?.id,
...filterQuery(params.filter),
});
}

export function buildProxyMediaUrl(params: {
type: string;
url: string;
session?: Session;
}) {
return buildProxyUrl("out/playlist.m3u8", {
type: params.type,
eurl: encrypt(params.url),
sid: params.session?.id,
});
}
33 changes: 29 additions & 4 deletions packages/stitcher/src/playlist.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DateTime } from "luxon";
import { assert } from "shared/assert";
import { filterMasterPlaylist } from "./filters";
import { filterMasterPlaylist, getQueryParamsFromFilter } from "./filters";
import { getAssets, getStaticDateRanges } from "./interstitials";
import { buildProxyMediaUrl, joinUrl, resolveUri } from "./lib/url";
import { encrypt } from "./lib/crypto";
import { joinUrl, makeUrl, resolveUri } from "./lib/url";
import {
groupRenditions,
parseMasterPlaylist,
Expand All @@ -26,7 +27,7 @@ export async function formatMasterPlaylist(

for (const variant of master.variants) {
const url = joinUrl(masterUrl, variant.uri);
variant.uri = buildProxyMediaUrl({
variant.uri = makeMediaUrl({
url,
session: options.session,
type: "VIDEO",
Expand All @@ -36,7 +37,7 @@ export async function formatMasterPlaylist(
const renditions = groupRenditions(master.variants);
renditions.forEach((rendition) => {
const url = joinUrl(masterUrl, rendition.uri);
rendition.uri = buildProxyMediaUrl({
rendition.uri = makeMediaUrl({
url,
session: options.session,
type: rendition.type,
Expand Down Expand Up @@ -110,3 +111,27 @@ export async function fetchDuration(uri: string) {
return acc;
}, 0);
}

export function makeMasterUrl(params: {
url: string;
filter: Filter;
session?: Session;
}) {
return makeUrl("out/master.m3u8", {
eurl: encrypt(params.url),
sid: params.session?.id,
...getQueryParamsFromFilter(params.filter),
});
}

function makeMediaUrl(params: {
type: string;
url: string;
session?: Session;
}) {
return makeUrl("out/playlist.m3u8", {
type: params.type,
eurl: encrypt(params.url),
sid: params.session?.id,
});
}
11 changes: 6 additions & 5 deletions packages/stitcher/src/routes/session.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Elysia, t } from "elysia";
import { parseFilterQuery } from "../filters";
import { getFilterFromQuery } from "../filters";
import { decrypt } from "../lib/crypto";
import { buildProxyMasterUrl } from "../lib/url";
import {
formatAssetList,
formatMasterPlaylist,
formatMediaPlaylist,
makeMasterUrl,
} from "../playlist";
import {
createSession,
Expand All @@ -19,10 +19,11 @@ export const session = new Elysia()
async ({ body }) => {
const session = await createSession(body);

const url = buildProxyMasterUrl({
const filter = body.filter ?? {};
const url = makeMasterUrl({
url: session.url,
session,
filter: body.filter,
filter,
});

return { url };
Expand Down Expand Up @@ -93,7 +94,7 @@ export const session = new Elysia()

await processSessionOnMasterReq(session);

const filter = parseFilterQuery(query);
const filter = getFilterFromQuery(query);

const url = decrypt(query.eurl);

Expand Down

0 comments on commit cbc0ba1

Please sign in to comment.