Skip to content

Commit

Permalink
Merge pull request #85 from Eyevinn/skippable-ads
Browse files Browse the repository at this point in the history
skipoffset added to AttachPodAds
  • Loading branch information
Nfrederiksen authored Jul 3, 2024
2 parents 53957b5 + fc7cb85 commit 84603fa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/ClientRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ClientRequest {
// Private fields
#consent;
#requestedDuration;
#skipoffset;
#userId;
#operatingSystem;
#deviceType;
Expand All @@ -18,6 +19,7 @@ class ClientRequest {
constructor(params) {
this.#consent = params.c || null;
this.#requestedDuration = params.dur || null;
this.#skipoffset = params.skip || null;
this.#userId = params.uid || null;
this.#operatingSystem = params.os || null;
this.#deviceType = params.dt || null;
Expand All @@ -41,6 +43,7 @@ class ClientRequest {
const properties = {
Consent: this.#consent,
RequestedDuration: this.#requestedDuration,
Skipoffset: this.#skipoffset,
UserId: this.#userId,
OperatingSystem: this.#operatingSystem,
DeviceType: this.#deviceType,
Expand Down
2 changes: 2 additions & 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 All @@ -58,6 +59,7 @@ class Session {
const vastObj = VastBuilder({
sessionId: this.sessionId,
desiredDuration: params.dur || "0",
skipoffset: params.skip || null,
adserverHostname: this.host,
maxPodDuration: params.max || null,
minPodDuration: params.min || null,
Expand Down
15 changes: 15 additions & 0 deletions api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ const vastSchema = () => ({
Linear: {
type: "object",
properties: {
skipoffset: {
type: "string",
example: "00:00:05",
xml: { attribute: true },
},
Duration: {
type: "string",
example: "00:00:30",
Expand Down Expand Up @@ -593,6 +598,11 @@ const schemas = {
description: "Desired duration 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 Expand Up @@ -691,6 +701,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
23 changes: 22 additions & 1 deletion utils/vast-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function AttachPodAds(vast, podAds, params) {
);
}
mediaNode = mediaNode
.attachLinear()
.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 @@ -351,6 +351,27 @@ function indexOfSmallest(a) {
return lowest;
}

function getSkipOffsetValue(skipoffset) {
// "x%"
const percentageFormatRegex = /^(100|[1-9]?[0-9])%$/;
// "seconds"
const integerSecondsRegex = /^\d+$/;

if (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) {
// Base Case #1: Regardless of current Pod size, return if Pod duration is greater than _max!
if (_chosenAds.length > 0) {
Expand Down
1 change: 1 addition & 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 Down

0 comments on commit 84603fa

Please sign in to comment.