Skip to content

Commit

Permalink
Basics of ad signaling
Browse files Browse the repository at this point in the history
  • Loading branch information
matvp91 committed Dec 14, 2024
1 parent f8b5560 commit c3468bd
Showing 1 changed file with 46 additions and 33 deletions.
79 changes: 46 additions & 33 deletions packages/stitcher/src/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,23 @@ export async function formatMediaPlaylist(
export async function formatAssetList(session: Session, dateTime: DateTime) {
const assets = await getAssets(session, dateTime);

const assetsPromises = assets.map(async (asset) => {
if (asset.duration === undefined) {
asset.duration = await fetchDuration(asset.url);
}
return {
URI: asset.url,
DURATION: asset.duration,
"SPRS-KIND": asset.kind,
"X-AD-CREATIVE-SIGNALING": getAdCreativeSignaling(assets, asset),
};
});
await Promise.all(
assets.map(async (asset) => {
if (asset.duration === undefined) {
asset.duration = await fetchDuration(asset.url);
}
}),
);

return {
ASSETS: await Promise.all(assetsPromises),
ASSETS: assets.map((asset) => {
return {
URI: asset.url,
DURATION: asset.duration,
"SPRS-KIND": asset.kind,
"X-AD-CREATIVE-SIGNALING": getAdCreativeSignaling(assets, asset),
};
}),
};
}

Expand Down Expand Up @@ -243,42 +246,44 @@ export function mapAdBreaksToSessionInterstitials(
return interstitials;
}

interface SignalingEvent {
type: "clickthrough" | "quartile";
start?: number;
urls: string[];
}

export function getAdCreativeSignaling(
assets: InterstitialAsset[],
asset: InterstitialAsset,
) {
if (!asset.tracking) {
return;
}
const { duration, tracking } = asset;

assert(duration);

assert(asset.duration);
const { duration } = asset;
if (!tracking) {
return null;
}

let startTime = 0;
for (const item of assets) {
// TODO: item.duration MIGHT be not resolved yet, we got to do
// this afterwards
if (item === asset) {
for (const tempAsset of assets) {
if (tempAsset === asset) {
break;
}
startTime += item.duration ?? 0;
assert(tempAsset.duration);
startTime += tempAsset.duration;
}

const tracking: {
type: "impression" | "clickthrough" | "quartile";
start?: number;
urls: string[];
}[] = [];
const signalingEvents: SignalingEvent[] = [];

Object.entries(asset.tracking).forEach(([name, urls]) => {
if (name === "firstQuartile") {
tracking.push({
Object.entries(tracking).forEach(([name, urls]) => {
const offset = QUARTILE_EVENTS[name];
if (offset !== undefined) {
signalingEvents.push({
type: "quartile",
start: duration * 0.25,
start: startTime + duration * offset,
urls,
});
}
// TODO: The rest
});

return {
Expand All @@ -289,8 +294,16 @@ export function getAdCreativeSignaling(
type: "linear",
start: startTime,
duration: asset.duration,
tracking,
tracking: signalingEvents,
},
],
};
}

const QUARTILE_EVENTS: Record<string, number> = {
start: 0,
firstQuartile: 0.25,
midpoint: 0.5,
thirdQuartile: 0.75,
complete: 1,
};

0 comments on commit c3468bd

Please sign in to comment.