Skip to content

Commit

Permalink
Skipoffset added to VMAP
Browse files Browse the repository at this point in the history
Added so that vmap-maker sends in skipoffset as a param into the VastBuilder. I also added a skipoffset variable into the defaultConfigs in vmap-maker.js.
  • Loading branch information
JinHedman committed Jun 15, 2024
1 parent 3e3d1e3 commit b5ee9c7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions api/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Session {
generalVastConfigs: {
sessionId: this.sessionId,
desiredDuration: params.dur || "0",
skipoffset: params.skip || null,
adserverHostname: this.host,
maxPodDuration: params.max || null,
minPodDuration: params.min || null,
Expand Down
7 changes: 6 additions & 1 deletion api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ const schemas = {
skip: {
type: "string",
description: "Skipoffset in seconds or percentage.",
example: "00:00:05 or 25%",
example: "5 or 25%",
},
uid: {
type: "string",
Expand Down Expand Up @@ -683,6 +683,11 @@ const schemas = {
description: "Desired duration for midroll ad break, in seconds.",
example: "60",
},
skip: {
type: "string",
description: "Skipoffset in seconds or percentage.",
example: "5 or 25%",
},
uid: {
type: "string",
description: "User ID.",
Expand Down
25 changes: 20 additions & 5 deletions utils/vast-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const DEFAULT_AD_LIST = [
*
*/
function VastBuilder(params) {
console.log("VastBuilder params: ", params);
let vastObject = {};
let adList = [];
let vast = null;
Expand Down Expand Up @@ -208,9 +209,8 @@ function AttachPodAds(vast, podAds, params) {
}
);
}
console.log("VAST params: ", params);
mediaNode = mediaNode
.attachLinear({skipoffset: isValidSkipOffset(params.skipoffset)}) // skipoffset does not seem to exist on VAST 2.0 and lower, you could also have skipoffset in percentage
.attachLinear({skipoffset: getSkipOffsetValue(params.skipoffset)}) // skipoffset does not seem to exist on VAST 2.0 and lower, you could also have skipoffset in percentage
.attachTrackingEvents()
.addTracking(`http://${params.adserverHostname}/api/v1/sessions/${params.sessionId}/tracking?${adId}=${podAds[i].id}_${i + 1}&progress=0`, { event: "start" })
.addTracking(`http://${params.adserverHostname}/api/v1/sessions/${params.sessionId}/tracking?${adId}=${podAds[i].id}_${i + 1}&progress=25`, { event: "firstQuartile" })
Expand Down Expand Up @@ -352,13 +352,28 @@ function indexOfSmallest(a) {
return lowest;
}

// Validate params.skipoffset is a valid VAST skipoffset value ("x%" or "hh:mm:ss").
function isValidSkipOffset(skipoffset) {
// Validate that params.skipoffset is a valid VAST skipoffset value ("x%" or "hh:mm:ss").
function getSkipOffsetValue(skipoffset) {
// "hh:mm:ss"
const timeFormatRegex = /^(\d{2}):([0-5][0-9]):([0-5][0-9])$/;
// "x%"
const percentageFormatRegex = /^(100|[1-9]?[0-9])%$/;
return timeFormatRegex.test(skipoffset) || percentageFormatRegex.test(skipoffset) ? skipoffset: null;
// "seconds"
const integerSecondsRegex = /^\d+$/;

if (timeFormatRegex.test(skipoffset) || percentageFormatRegex.test(skipoffset)){
return skipoffset;
}
// convert seconds to "hh:mm:ss" format
if (integerSecondsRegex.test(skipoffset)) {
const totalSeconds = parseInt(skipoffset, 10);
const hours = String(Math.floor(totalSeconds / 3600)).padStart(2, '0');
const minutes = String(Math.floor((totalSeconds % 3600) / 60)).padStart(2, '0');
const seconds = String(totalSeconds % 60).padStart(2, '0');

return `${hours}:${minutes}:${seconds}`;
}
return null;
}

function PopulatePod(_size, _min, _max, _ads, _chosenAds, _method, _targetDur) {
Expand Down
2 changes: 2 additions & 0 deletions utils/vmap-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function VmapBuilder(params) {
const defaultConfigs = {
sessionId: GVC.sessionId,
desiredDuration: "15",
skipoffset: GVC.skipoffset,
adserverHostname: GVC.adserverHostname,
maxPodDuration: null,
minPodDuration: null,
Expand All @@ -90,6 +91,7 @@ function VmapBuilder(params) {

const breakpoints = params.breakpoints ? params.breakpoints.split(",").filter((item) => !isNaN(Number(item))) : [];
if (params.preroll) {
//console.log("PARAMS: ",params.generalVastConfigs);
const preVast = VastBuilder(defaultConfigs);
vmap.attachAdBreak("preroll.ad", "linear", "start", preVast.xml, {
sessionId: GVC.sessionId,
Expand Down

0 comments on commit b5ee9c7

Please sign in to comment.