diff --git a/packages/stitcher/src/interstitials.ts b/packages/stitcher/src/interstitials.ts index 5c3ae144..260a5509 100644 --- a/packages/stitcher/src/interstitials.ts +++ b/packages/stitcher/src/interstitials.ts @@ -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[], ) { @@ -74,31 +79,22 @@ 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, @@ -106,17 +102,13 @@ function getAssetListUrl(interstitial: Interstitial, session?: Session) { } 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"; } diff --git a/packages/stitcher/src/playlist.ts b/packages/stitcher/src/playlist.ts index 423f7ecf..9c5a11ae 100644 --- a/packages/stitcher/src/playlist.ts +++ b/packages/stitcher/src/playlist.ts @@ -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"; @@ -202,7 +202,7 @@ async function initSessionOnMasterReq(session: Session) { session, vmap.adBreaks, ); - appendInterstitials(session.interstitials, interstitials); + mergeInterstitials(session.interstitials, interstitials); storeSession = true; } @@ -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 }, + }, + ], }); } diff --git a/packages/stitcher/src/session.ts b/packages/stitcher/src/session.ts index 2236278d..86610146 100644 --- a/packages/stitcher/src/session.ts +++ b/packages/stitcher/src/session.ts @@ -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 { @@ -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); @@ -83,21 +83,43 @@ function mapSessionInterstitials( startTime: DateTime, interstitials: SessionInterstitial[], ) { - return interstitials.map((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; } diff --git a/packages/stitcher/src/types.ts b/packages/stitcher/src/types.ts index 44490888..9fb50a68 100644 --- a/packages/stitcher/src/types.ts +++ b/packages/stitcher/src/types.ts @@ -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[]; }