-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Discriminated unions for interstitials type (#139)
* Added discriminated unions for interstitials type * Added assetList type * Added timeline style * Added tests * New interstitials API * Fix snapshots
- Loading branch information
Showing
12 changed files
with
483 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,118 @@ | ||
import { createUrl } from "./lib/url"; | ||
import { fetchDuration } from "./playlist"; | ||
import { getAdMediasFromVast } from "./vast"; | ||
import { getAssetsFromVast } from "./vast"; | ||
import type { DateRange } from "./parser"; | ||
import type { Session } from "./session"; | ||
import type { Interstitial, InterstitialAssetType } from "./types"; | ||
import type { Interstitial, InterstitialAsset } from "./types"; | ||
import type { DateTime } from "luxon"; | ||
|
||
export function getStaticDateRanges(session: Session, isLive: boolean) { | ||
const group: { | ||
dateTime: DateTime; | ||
types: InterstitialAssetType[]; | ||
}[] = []; | ||
|
||
for (const interstitial of session.interstitials) { | ||
let item = group.find((item) => | ||
item.dateTime.equals(interstitial.dateTime), | ||
); | ||
|
||
if (!item) { | ||
item = { | ||
dateTime: interstitial.dateTime, | ||
types: [], | ||
}; | ||
group.push(item); | ||
} | ||
|
||
const type = getInterstitialType(interstitial); | ||
if (type && !item.types.includes(type)) { | ||
item.types.push(type); | ||
} | ||
} | ||
|
||
return group.map((item) => { | ||
const assetListUrl = createAssetListUrl({ | ||
dateTime: item.dateTime, | ||
session, | ||
}); | ||
return session.interstitials.map<DateRange>((interstitial) => { | ||
const startDate = interstitial.dateTime; | ||
const assetListUrl = getAssetListUrl(interstitial, session); | ||
|
||
const clientAttributes: Record<string, number | string> = { | ||
RESTRICT: "SKIP,JUMP", | ||
"ASSET-LIST": assetListUrl, | ||
CUE: "ONCE", | ||
"CONTENT-MAY-VARY": "YES", | ||
"TIMELINE-OCCUPIES": "POINT", | ||
"TIMELINE-STYLE": getTimelineStyle(interstitial), | ||
}; | ||
|
||
if (!isLive) { | ||
clientAttributes["RESUME-OFFSET"] = 0; | ||
} | ||
|
||
const isPreroll = item.dateTime.equals(session.startTime); | ||
if (isPreroll) { | ||
clientAttributes["CUE"] += ",PRE"; | ||
const cue: string[] = ["ONCE"]; | ||
if (startDate.equals(session.startTime)) { | ||
cue.push("PRE"); | ||
} | ||
|
||
if (item.types.length) { | ||
clientAttributes["SPRS-TYPES"] = item.types.join(","); | ||
if (cue.length) { | ||
clientAttributes["CUE"] = cue.join(","); | ||
} | ||
|
||
return { | ||
classId: "com.apple.hls.interstitial", | ||
id: `${item.dateTime.toUnixInteger()}`, | ||
startDate: item.dateTime, | ||
id: `${startDate.toMillis()}`, | ||
startDate, | ||
clientAttributes, | ||
}; | ||
}); | ||
} | ||
|
||
export async function getAssets(session: Session, dateTime: DateTime) { | ||
const assets: { | ||
URI: string; | ||
DURATION: number; | ||
"SPRS-TYPE"?: InterstitialAssetType; | ||
}[] = []; | ||
const assets: InterstitialAsset[] = []; | ||
|
||
const interstitials = session.interstitials.filter((interstitial) => | ||
const interstitial = session.interstitials.find((interstitial) => | ||
interstitial.dateTime.equals(dateTime), | ||
); | ||
|
||
if (interstitial?.vast) { | ||
const nextAssets = await getAssetsFromVast(interstitial.vast); | ||
assets.push(...nextAssets); | ||
} | ||
|
||
if (interstitial?.assets) { | ||
assets.push(...interstitial.assets); | ||
} | ||
|
||
return assets; | ||
} | ||
|
||
export function appendInterstitials( | ||
source: Interstitial[], | ||
interstitials: Interstitial[], | ||
) { | ||
for (const interstitial of interstitials) { | ||
const adMedias = await getAdMediasFromVast(interstitial); | ||
for (const adMedia of adMedias) { | ||
assets.push({ | ||
URI: adMedia.masterUrl, | ||
DURATION: adMedia.duration, | ||
"SPRS-TYPE": "ad", | ||
}); | ||
const target = source.find((item) => | ||
item.dateTime.equals(interstitial.dateTime), | ||
); | ||
|
||
if (!target) { | ||
source.push(interstitial); | ||
continue; | ||
} | ||
|
||
if (interstitial.asset) { | ||
assets.push({ | ||
URI: interstitial.asset.url, | ||
DURATION: await fetchDuration(interstitial.asset.url), | ||
"SPRS-TYPE": interstitial.asset.type, | ||
}); | ||
if (interstitial.assets) { | ||
if (!target.assets) { | ||
target.assets = interstitial.assets; | ||
} else { | ||
target.assets.push(...interstitial.assets); | ||
} | ||
} | ||
} | ||
|
||
return assets; | ||
if (interstitial.vast) { | ||
target.vast = interstitial.vast; | ||
} | ||
|
||
if (interstitial.assetList) { | ||
target.assetList = interstitial.assetList; | ||
} | ||
} | ||
} | ||
|
||
function createAssetListUrl(params: { dateTime: DateTime; session?: Session }) { | ||
function getAssetListUrl(interstitial: Interstitial, session?: Session) { | ||
if (interstitial.assetList) { | ||
return interstitial.assetList.url; | ||
} | ||
return createUrl("out/asset-list.json", { | ||
dt: params.dateTime.toISO(), | ||
sid: params.session?.id, | ||
dt: interstitial.dateTime.toISO(), | ||
sid: session?.id, | ||
}); | ||
} | ||
|
||
function getInterstitialType( | ||
interstitial: Interstitial, | ||
): InterstitialAssetType | undefined { | ||
if (interstitial.vastData || interstitial.vastUrl) { | ||
return "ad"; | ||
function getTimelineStyle(interstitial: Interstitial) { | ||
if (interstitial.assets) { | ||
for (const asset of interstitial.assets) { | ||
if (asset.kind === "ad") { | ||
return "HIGHLIGHT"; | ||
} | ||
} | ||
} | ||
return interstitial.asset?.type; | ||
|
||
if (interstitial.vast) { | ||
return "HIGHLIGHT"; | ||
} | ||
|
||
return "PRIMARY"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.