Skip to content

Commit

Permalink
fix: Merge interstitials when VMAP returns multiple AdBreak elements …
Browse files Browse the repository at this point in the history
…for the same timeOffset
  • Loading branch information
matvp91 committed Dec 20, 2024
1 parent 59cb212 commit 4ed7a45
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 63 deletions.
66 changes: 29 additions & 37 deletions packages/stitcher/src/interstitials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,30 @@ export function getStaticDateRanges(session: Session, isLive: boolean) {
}

export async function getAssets(session: Session, dateTime: DateTime) {
const assets: InterstitialAsset[] = [];

const interstitial = session.interstitials.find((interstitial) =>
interstitial.dateTime.equals(dateTime),
);

if (interstitial?.vast) {
const nextAssets = await getAssetsFromVast(interstitial.vast);
assets.push(...nextAssets);
if (!interstitial) {
return [];
}

if (interstitial?.assets) {
assets.push(...interstitial.assets);
const assets: InterstitialAsset[] = [];

for (const chunk of interstitial.chunks) {
if (chunk.type === "vast") {
const nextAssets = await getAssetsFromVast(chunk.data);
assets.push(...nextAssets);
}
if (chunk.type === "asset") {
assets.push(chunk.data);
}
}

return assets;
}

export function appendInterstitials(
export function mergeInterstitials(
source: Interstitial[],
interstitials: Interstitial[],
) {
Expand All @@ -74,49 +79,36 @@ export function appendInterstitials(

if (!target) {
source.push(interstitial);
continue;
}

if (interstitial.assets) {
if (!target.assets) {
target.assets = interstitial.assets;
} else {
target.assets.push(...interstitial.assets);
}
}

if (interstitial.vast) {
target.vast = interstitial.vast;
}

if (interstitial.assetList) {
target.assetList = interstitial.assetList;
} else {
// If we found a source for the particular dateTime, we push the
// other chunks at the end.
target.chunks.push(...interstitial.chunks);
}
}
}

function getAssetListUrl(interstitial: Interstitial, session?: Session) {
if (interstitial.assetList) {
return interstitial.assetList.url;
const assetListChunks = interstitial.chunks.filter(
(chunk) => chunk.type === "assetList",
);
if (assetListChunks.length === 1 && assetListChunks[0]) {
return assetListChunks[0].data.url;
}

return createUrl("out/asset-list.json", {
dt: interstitial.dateTime.toISO(),
sid: session?.id,
});
}

function getTimelineStyle(interstitial: Interstitial) {
if (interstitial.assets) {
for (const asset of interstitial.assets) {
if (asset.kind === "ad") {
return "HIGHLIGHT";
}
for (const chunk of interstitial.chunks) {
if (chunk.type === "asset" && chunk.data.kind === "ad") {
return "HIGHLIGHT";
}
if (chunk.type === "vast") {
return "HIGHLIGHT";
}
}

if (interstitial.vast) {
return "HIGHLIGHT";
}

return "PRIMARY";
}
16 changes: 10 additions & 6 deletions packages/stitcher/src/playlist.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { assert } from "shared/assert";
import { filterMasterPlaylist, formatFilterToQueryParam } from "./filters";
import {
appendInterstitials,
getAssets,
getStaticDateRanges,
mergeInterstitials,
} from "./interstitials";
import { encrypt } from "./lib/crypto";
import { createUrl, joinUrl, resolveUri } from "./lib/url";
Expand Down Expand Up @@ -202,7 +202,7 @@ async function initSessionOnMasterReq(session: Session) {
session,
vmap.adBreaks,
);
appendInterstitials(session.interstitials, interstitials);
mergeInterstitials(session.interstitials, interstitials);

storeSession = true;
}
Expand All @@ -229,10 +229,14 @@ export function mapAdBreaksToSessionInterstitials(

interstitials.push({
dateTime,
vast: {
url: adBreak.vastUrl,
data: adBreak.vastData,
},
// We're going to push a single chunk here and have them merged before
// we return the full list of interstitials.
chunks: [
{
type: "vast",
data: { url: adBreak.vastUrl, data: adBreak.vastData },
},
],
});
}

Expand Down
56 changes: 39 additions & 17 deletions packages/stitcher/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { randomUUID } from "crypto";
import { DateTime } from "luxon";
import { kv } from "./adapters/kv";
import { appendInterstitials } from "./interstitials";
import { mergeInterstitials } from "./interstitials";
import { JSON } from "./lib/json";
import { resolveUri } from "./lib/url";
import type { Interstitial } from "./types";
import type { Interstitial, InterstitialChunk } from "./types";
import type { VmapParams } from "./vmap";

export interface Session {
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function createSession(params: {
startTime,
params.interstitials,
);
appendInterstitials(session.interstitials, interstitials);
mergeInterstitials(session.interstitials, interstitials);
}

const value = JSON.stringify(session);
Expand All @@ -83,21 +83,43 @@ function mapSessionInterstitials(
startTime: DateTime,
interstitials: SessionInterstitial[],
) {
return interstitials.map<Interstitial>((item) => {
const { time, duration, assets, ...rest } = item;
const result: Interstitial[] = [];

for (const interstitial of interstitials) {
const dateTime =
typeof time === "string"
? DateTime.fromISO(time)
: startTime.plus({ seconds: time });
typeof interstitial.time === "string"
? DateTime.fromISO(interstitial.time)
: startTime.plus({ seconds: interstitial.time });

return {
dateTime,
duration,
assets: assets?.map((asset) => {
const chunks: InterstitialChunk[] = [];

if (interstitial.assets) {
for (const asset of interstitial.assets) {
const { uri, ...rest } = asset;
return { url: resolveUri(uri), ...rest };
}),
...rest,
};
});
chunks.push({
type: "asset",
data: {
url: resolveUri(uri),
...rest,
},
});
}
}

if (interstitial.vast) {
chunks.push({ type: "vast", data: interstitial.vast });
}

if (interstitial.assetList) {
chunks.push({ type: "assetList", data: interstitial.assetList });
}

result.push({
dateTime,
duration: interstitial.duration,
chunks,
});
}

return result;
}
9 changes: 6 additions & 3 deletions packages/stitcher/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ export interface InterstitialAssetList {
url: string;
}

export type InterstitialChunk =
| { type: "asset"; data: InterstitialAsset }
| { type: "vast"; data: InterstitialVast }
| { type: "assetList"; data: InterstitialAssetList };

export interface Interstitial {
dateTime: DateTime;
duration?: number;
assets?: InterstitialAsset[];
vast?: InterstitialVast;
assetList?: InterstitialAssetList;
chunks: InterstitialChunk[];
}

0 comments on commit 4ed7a45

Please sign in to comment.