stormcloud-video-player 0.2.36 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -0
- package/dist/stormcloud-vp.min.js +2 -2
- package/lib/index.cjs +186 -2
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +8 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +186 -2
- package/lib/index.js.map +1 -1
- package/lib/player/StormcloudVideoPlayer.cjs +186 -2
- package/lib/player/StormcloudVideoPlayer.cjs.map +1 -1
- package/lib/player/StormcloudVideoPlayer.d.cts +8 -0
- package/lib/players/HlsPlayer.cjs +186 -2
- package/lib/players/HlsPlayer.cjs.map +1 -1
- package/lib/players/index.cjs +186 -2
- package/lib/players/index.cjs.map +1 -1
- package/lib/ui/StormcloudVideoPlayer.cjs +186 -2
- package/lib/ui/StormcloudVideoPlayer.cjs.map +1 -1
- package/package.json +1 -1
|
@@ -15,6 +15,7 @@ declare class StormcloudVideoPlayer {
|
|
|
15
15
|
private ptsDriftEmaMs;
|
|
16
16
|
private adPodQueue;
|
|
17
17
|
private apiVastTagUrl;
|
|
18
|
+
private apiNumberAds;
|
|
18
19
|
private lastHeartbeatTime;
|
|
19
20
|
private heartbeatInterval;
|
|
20
21
|
private currentAdIndex;
|
|
@@ -36,6 +37,9 @@ declare class StormcloudVideoPlayer {
|
|
|
36
37
|
private adRequestWatchdogId;
|
|
37
38
|
private adRequestWatchdogToken;
|
|
38
39
|
private adFailsafeToken;
|
|
40
|
+
private fetchedAdDurations;
|
|
41
|
+
private targetAdBreakDurationMs;
|
|
42
|
+
private isAdaptiveMode;
|
|
39
43
|
constructor(config: StormcloudVideoPlayerConfig);
|
|
40
44
|
private createAdPlayer;
|
|
41
45
|
load(): Promise<void>;
|
|
@@ -56,6 +60,7 @@ declare class StormcloudVideoPlayer {
|
|
|
56
60
|
private fetchAdConfiguration;
|
|
57
61
|
getCurrentAdIndex(): number;
|
|
58
62
|
getTotalAdsInBreak(): number;
|
|
63
|
+
private generateVastUrlsWithCorrelators;
|
|
59
64
|
isAdPlaying(): boolean;
|
|
60
65
|
isShowingAds(): boolean;
|
|
61
66
|
getStreamType(): "hls" | "other";
|
|
@@ -83,6 +88,9 @@ declare class StormcloudVideoPlayer {
|
|
|
83
88
|
private logAdState;
|
|
84
89
|
private fetchAndParseVastXml;
|
|
85
90
|
private extractMediaUrlsFromVast;
|
|
91
|
+
private fetchVastDuration;
|
|
92
|
+
private calculateAdditionalAdsNeeded;
|
|
93
|
+
private addAdaptiveAdsToQueue;
|
|
86
94
|
private preloadMediaFile;
|
|
87
95
|
private preloadAllAdsInBackground;
|
|
88
96
|
private preloadSingleAd;
|
|
@@ -2162,6 +2162,9 @@ var StormcloudVideoPlayer = class {
|
|
|
2162
2162
|
this.activeAdRequestToken = null;
|
|
2163
2163
|
this.adRequestWatchdogToken = null;
|
|
2164
2164
|
this.adFailsafeToken = null;
|
|
2165
|
+
this.fetchedAdDurations = /* @__PURE__ */ new Map();
|
|
2166
|
+
this.targetAdBreakDurationMs = null;
|
|
2167
|
+
this.isAdaptiveMode = false;
|
|
2165
2168
|
initializePolyfills();
|
|
2166
2169
|
const browserOverrides = getBrowserConfigOverrides();
|
|
2167
2170
|
this.config = { ...config, ...browserOverrides };
|
|
@@ -2968,7 +2971,7 @@ var StormcloudVideoPlayer = class {
|
|
|
2968
2971
|
}
|
|
2969
2972
|
}
|
|
2970
2973
|
async fetchAdConfiguration() {
|
|
2971
|
-
var _a, _b, _c;
|
|
2974
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
2972
2975
|
const vastMode = this.config.vastMode || "default";
|
|
2973
2976
|
if (this.config.debugAdTiming) {
|
|
2974
2977
|
console.log("[StormcloudVideoPlayer] VAST mode:", vastMode);
|
|
@@ -3039,6 +3042,16 @@ var StormcloudVideoPlayer = class {
|
|
|
3039
3042
|
);
|
|
3040
3043
|
}
|
|
3041
3044
|
}
|
|
3045
|
+
const numberAds = (_g = (_f = (_e = (_d = data.response) == null ? void 0 : _d.options) == null ? void 0 : _e.vast) == null ? void 0 : _f.cue_tones) == null ? void 0 : _g.number_ads;
|
|
3046
|
+
if (numberAds != null && numberAds > 0) {
|
|
3047
|
+
this.apiNumberAds = numberAds;
|
|
3048
|
+
if (this.config.debugAdTiming) {
|
|
3049
|
+
console.log(
|
|
3050
|
+
"[StormcloudVideoPlayer] Number of ads per break from API:",
|
|
3051
|
+
this.apiNumberAds
|
|
3052
|
+
);
|
|
3053
|
+
}
|
|
3054
|
+
}
|
|
3042
3055
|
}
|
|
3043
3056
|
getCurrentAdIndex() {
|
|
3044
3057
|
return this.currentAdIndex;
|
|
@@ -3046,6 +3059,27 @@ var StormcloudVideoPlayer = class {
|
|
|
3046
3059
|
getTotalAdsInBreak() {
|
|
3047
3060
|
return this.totalAdsInBreak;
|
|
3048
3061
|
}
|
|
3062
|
+
generateVastUrlsWithCorrelators(baseUrl, count) {
|
|
3063
|
+
const urls = [];
|
|
3064
|
+
for (let i = 0; i < count; i++) {
|
|
3065
|
+
try {
|
|
3066
|
+
const url = new URL(baseUrl);
|
|
3067
|
+
const timestamp = Date.now();
|
|
3068
|
+
const random = Math.floor(Math.random() * 1e6);
|
|
3069
|
+
const uniqueCorrelator = `${timestamp}${random}${i}`;
|
|
3070
|
+
url.searchParams.set("correlator", uniqueCorrelator);
|
|
3071
|
+
urls.push(url.toString());
|
|
3072
|
+
} catch (error) {
|
|
3073
|
+
console.warn(
|
|
3074
|
+
"[StormcloudVideoPlayer] Failed to parse VAST URL:",
|
|
3075
|
+
baseUrl,
|
|
3076
|
+
error
|
|
3077
|
+
);
|
|
3078
|
+
urls.push(`${baseUrl}${baseUrl.includes("?") ? "&" : "?"}correlator=${Date.now()}${i}`);
|
|
3079
|
+
}
|
|
3080
|
+
}
|
|
3081
|
+
return urls;
|
|
3082
|
+
}
|
|
3049
3083
|
isAdPlaying() {
|
|
3050
3084
|
return this.inAdBreak && this.ima.isAdPlaying();
|
|
3051
3085
|
}
|
|
@@ -3084,7 +3118,52 @@ var StormcloudVideoPlayer = class {
|
|
|
3084
3118
|
const tags = this.selectVastTagsForBreak(scheduled);
|
|
3085
3119
|
let vastTagUrls = [];
|
|
3086
3120
|
if (this.apiVastTagUrl) {
|
|
3087
|
-
|
|
3121
|
+
let numberOfAds = 1;
|
|
3122
|
+
if (this.isLiveStream) {
|
|
3123
|
+
const adBreakDurationMs = _marker.durationSeconds != null ? _marker.durationSeconds * 1e3 : scheduled == null ? void 0 : scheduled.durationMs;
|
|
3124
|
+
if (adBreakDurationMs != null && adBreakDurationMs > 0) {
|
|
3125
|
+
this.isAdaptiveMode = true;
|
|
3126
|
+
this.targetAdBreakDurationMs = adBreakDurationMs;
|
|
3127
|
+
this.fetchedAdDurations.clear();
|
|
3128
|
+
numberOfAds = 2;
|
|
3129
|
+
if (this.config.debugAdTiming) {
|
|
3130
|
+
console.log(
|
|
3131
|
+
`[ADAPTIVE-POD] \u{1F4FA} LIVE MODE (ADAPTIVE): Target duration=${adBreakDurationMs}ms | Starting with ${numberOfAds} ads, will fetch actual durations and add more dynamically`
|
|
3132
|
+
);
|
|
3133
|
+
}
|
|
3134
|
+
} else {
|
|
3135
|
+
if (this.config.debugAdTiming) {
|
|
3136
|
+
console.warn(
|
|
3137
|
+
"[DEBUG-POD] \u26A0\uFE0F LIVE MODE: No duration available, defaulting to 1 ad"
|
|
3138
|
+
);
|
|
3139
|
+
}
|
|
3140
|
+
}
|
|
3141
|
+
} else {
|
|
3142
|
+
this.isAdaptiveMode = false;
|
|
3143
|
+
this.targetAdBreakDurationMs = null;
|
|
3144
|
+
this.fetchedAdDurations.clear();
|
|
3145
|
+
if (this.apiNumberAds && this.apiNumberAds > 1) {
|
|
3146
|
+
numberOfAds = this.apiNumberAds;
|
|
3147
|
+
if (this.config.debugAdTiming) {
|
|
3148
|
+
console.log(
|
|
3149
|
+
`[DEBUG-POD] \u{1F3AC} VOD MODE (FIXED): Using number_ads=${numberOfAds} from API`
|
|
3150
|
+
);
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
if (numberOfAds > 1) {
|
|
3155
|
+
vastTagUrls = this.generateVastUrlsWithCorrelators(
|
|
3156
|
+
this.apiVastTagUrl,
|
|
3157
|
+
numberOfAds
|
|
3158
|
+
);
|
|
3159
|
+
if (this.config.debugAdTiming) {
|
|
3160
|
+
console.log(
|
|
3161
|
+
`[DEBUG-POD] \u{1F504} Generated ${vastTagUrls.length} initial VAST URLs with unique correlators`
|
|
3162
|
+
);
|
|
3163
|
+
}
|
|
3164
|
+
} else {
|
|
3165
|
+
vastTagUrls = [this.apiVastTagUrl];
|
|
3166
|
+
}
|
|
3088
3167
|
} else if (tags && tags.length > 0) {
|
|
3089
3168
|
vastTagUrls = tags;
|
|
3090
3169
|
} else {
|
|
@@ -3503,6 +3582,99 @@ var StormcloudVideoPlayer = class {
|
|
|
3503
3582
|
}
|
|
3504
3583
|
return mediaUrls;
|
|
3505
3584
|
}
|
|
3585
|
+
async fetchVastDuration(vastTagUrl) {
|
|
3586
|
+
var _a;
|
|
3587
|
+
try {
|
|
3588
|
+
const response = await fetch(vastTagUrl, { mode: "cors" });
|
|
3589
|
+
if (!response.ok) {
|
|
3590
|
+
if (this.config.debugAdTiming) {
|
|
3591
|
+
console.warn(
|
|
3592
|
+
`[ADAPTIVE-POD] Failed to fetch VAST: ${response.status}`
|
|
3593
|
+
);
|
|
3594
|
+
}
|
|
3595
|
+
return null;
|
|
3596
|
+
}
|
|
3597
|
+
const xmlText = await response.text();
|
|
3598
|
+
const parser = new DOMParser();
|
|
3599
|
+
const xmlDoc = parser.parseFromString(xmlText, "text/xml");
|
|
3600
|
+
const durationText = (_a = xmlDoc.querySelector("Duration")) == null ? void 0 : _a.textContent;
|
|
3601
|
+
if (!durationText) {
|
|
3602
|
+
if (this.config.debugAdTiming) {
|
|
3603
|
+
console.warn("[ADAPTIVE-POD] No Duration element found in VAST");
|
|
3604
|
+
}
|
|
3605
|
+
return null;
|
|
3606
|
+
}
|
|
3607
|
+
const durationParts = durationText.split(":");
|
|
3608
|
+
const durationSeconds = parseInt(durationParts[0] || "0", 10) * 3600 + parseInt(durationParts[1] || "0", 10) * 60 + parseInt(durationParts[2] || "0", 10);
|
|
3609
|
+
return durationSeconds;
|
|
3610
|
+
} catch (error) {
|
|
3611
|
+
if (this.config.debugAdTiming) {
|
|
3612
|
+
console.warn(
|
|
3613
|
+
`[ADAPTIVE-POD] Error fetching VAST duration from ${vastTagUrl}:`,
|
|
3614
|
+
error
|
|
3615
|
+
);
|
|
3616
|
+
}
|
|
3617
|
+
return null;
|
|
3618
|
+
}
|
|
3619
|
+
}
|
|
3620
|
+
calculateAdditionalAdsNeeded() {
|
|
3621
|
+
if (!this.isAdaptiveMode || this.targetAdBreakDurationMs === null) {
|
|
3622
|
+
return 0;
|
|
3623
|
+
}
|
|
3624
|
+
let totalFetchedDurationMs = 0;
|
|
3625
|
+
for (const duration of this.fetchedAdDurations.values()) {
|
|
3626
|
+
totalFetchedDurationMs += duration * 1e3;
|
|
3627
|
+
}
|
|
3628
|
+
const remainingTimeMs = this.targetAdBreakDurationMs - totalFetchedDurationMs;
|
|
3629
|
+
if (remainingTimeMs <= 0) {
|
|
3630
|
+
if (this.config.debugAdTiming) {
|
|
3631
|
+
console.log(
|
|
3632
|
+
`[ADAPTIVE-POD] \u2705 Target duration reached: ${totalFetchedDurationMs}ms / ${this.targetAdBreakDurationMs}ms`
|
|
3633
|
+
);
|
|
3634
|
+
}
|
|
3635
|
+
return 0;
|
|
3636
|
+
}
|
|
3637
|
+
const fetchedCount = this.fetchedAdDurations.size;
|
|
3638
|
+
const averageDurationMs = fetchedCount > 0 ? totalFetchedDurationMs / fetchedCount : 30 * 1e3;
|
|
3639
|
+
const additionalAds = Math.ceil(remainingTimeMs / averageDurationMs);
|
|
3640
|
+
if (this.config.debugAdTiming) {
|
|
3641
|
+
console.log(
|
|
3642
|
+
`[ADAPTIVE-POD] \u{1F4CA} Need ${additionalAds} more ads | Fetched: ${totalFetchedDurationMs}ms / Target: ${this.targetAdBreakDurationMs}ms | Remaining: ${remainingTimeMs}ms | Avg duration: ${averageDurationMs}ms`
|
|
3643
|
+
);
|
|
3644
|
+
}
|
|
3645
|
+
return additionalAds;
|
|
3646
|
+
}
|
|
3647
|
+
async addAdaptiveAdsToQueue() {
|
|
3648
|
+
if (!this.isAdaptiveMode || !this.apiVastTagUrl) {
|
|
3649
|
+
return;
|
|
3650
|
+
}
|
|
3651
|
+
const additionalAds = this.calculateAdditionalAdsNeeded();
|
|
3652
|
+
if (additionalAds <= 0) {
|
|
3653
|
+
return;
|
|
3654
|
+
}
|
|
3655
|
+
const newUrls = this.generateVastUrlsWithCorrelators(
|
|
3656
|
+
this.apiVastTagUrl,
|
|
3657
|
+
additionalAds
|
|
3658
|
+
);
|
|
3659
|
+
if (this.config.debugAdTiming) {
|
|
3660
|
+
console.log(
|
|
3661
|
+
`[ADAPTIVE-POD] \u{1F504} Adding ${newUrls.length} additional VAST URLs to queue`
|
|
3662
|
+
);
|
|
3663
|
+
}
|
|
3664
|
+
this.adPodAllUrls.push(...newUrls);
|
|
3665
|
+
this.adPodQueue.push(...newUrls);
|
|
3666
|
+
this.totalAdsInBreak += newUrls.length;
|
|
3667
|
+
for (const url of newUrls) {
|
|
3668
|
+
this.preloadSingleAd(url).catch((error) => {
|
|
3669
|
+
if (this.config.debugAdTiming) {
|
|
3670
|
+
console.warn(
|
|
3671
|
+
`[ADAPTIVE-POD] Failed to preload adaptive ad:`,
|
|
3672
|
+
error
|
|
3673
|
+
);
|
|
3674
|
+
}
|
|
3675
|
+
});
|
|
3676
|
+
}
|
|
3677
|
+
}
|
|
3506
3678
|
async preloadMediaFile(mediaUrl) {
|
|
3507
3679
|
if (this.preloadedMediaUrls.has(mediaUrl)) {
|
|
3508
3680
|
return;
|
|
@@ -3572,6 +3744,18 @@ var StormcloudVideoPlayer = class {
|
|
|
3572
3744
|
async preloadSingleAd(vastTagUrl) {
|
|
3573
3745
|
if (!vastTagUrl) return;
|
|
3574
3746
|
try {
|
|
3747
|
+
if (this.isAdaptiveMode && !this.fetchedAdDurations.has(vastTagUrl)) {
|
|
3748
|
+
const duration = await this.fetchVastDuration(vastTagUrl);
|
|
3749
|
+
if (duration !== null) {
|
|
3750
|
+
this.fetchedAdDurations.set(vastTagUrl, duration);
|
|
3751
|
+
if (this.config.debugAdTiming) {
|
|
3752
|
+
console.log(
|
|
3753
|
+
`[ADAPTIVE-POD] \u2713 Fetched ad duration: ${duration}s (${this.fetchedAdDurations.size} ads fetched so far)`
|
|
3754
|
+
);
|
|
3755
|
+
}
|
|
3756
|
+
await this.addAdaptiveAdsToQueue();
|
|
3757
|
+
}
|
|
3758
|
+
}
|
|
3575
3759
|
if (this.ima.preloadAds && !this.ima.hasPreloadedAd(vastTagUrl)) {
|
|
3576
3760
|
if (!this.preloadingAdUrls.has(vastTagUrl)) {
|
|
3577
3761
|
if (this.config.debugAdTiming) {
|