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/lib/index.cjs CHANGED
@@ -2226,6 +2226,9 @@ var StormcloudVideoPlayer = class {
2226
2226
  this.activeAdRequestToken = null;
2227
2227
  this.adRequestWatchdogToken = null;
2228
2228
  this.adFailsafeToken = null;
2229
+ this.fetchedAdDurations = /* @__PURE__ */ new Map();
2230
+ this.targetAdBreakDurationMs = null;
2231
+ this.isAdaptiveMode = false;
2229
2232
  initializePolyfills();
2230
2233
  const browserOverrides = getBrowserConfigOverrides();
2231
2234
  this.config = { ...config, ...browserOverrides };
@@ -3032,7 +3035,7 @@ var StormcloudVideoPlayer = class {
3032
3035
  }
3033
3036
  }
3034
3037
  async fetchAdConfiguration() {
3035
- var _a, _b, _c;
3038
+ var _a, _b, _c, _d, _e, _f, _g;
3036
3039
  const vastMode = this.config.vastMode || "default";
3037
3040
  if (this.config.debugAdTiming) {
3038
3041
  console.log("[StormcloudVideoPlayer] VAST mode:", vastMode);
@@ -3103,6 +3106,16 @@ var StormcloudVideoPlayer = class {
3103
3106
  );
3104
3107
  }
3105
3108
  }
3109
+ 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;
3110
+ if (numberAds != null && numberAds > 0) {
3111
+ this.apiNumberAds = numberAds;
3112
+ if (this.config.debugAdTiming) {
3113
+ console.log(
3114
+ "[StormcloudVideoPlayer] Number of ads per break from API:",
3115
+ this.apiNumberAds
3116
+ );
3117
+ }
3118
+ }
3106
3119
  }
3107
3120
  getCurrentAdIndex() {
3108
3121
  return this.currentAdIndex;
@@ -3110,6 +3123,27 @@ var StormcloudVideoPlayer = class {
3110
3123
  getTotalAdsInBreak() {
3111
3124
  return this.totalAdsInBreak;
3112
3125
  }
3126
+ generateVastUrlsWithCorrelators(baseUrl, count) {
3127
+ const urls = [];
3128
+ for (let i = 0; i < count; i++) {
3129
+ try {
3130
+ const url = new URL(baseUrl);
3131
+ const timestamp = Date.now();
3132
+ const random = Math.floor(Math.random() * 1e6);
3133
+ const uniqueCorrelator = `${timestamp}${random}${i}`;
3134
+ url.searchParams.set("correlator", uniqueCorrelator);
3135
+ urls.push(url.toString());
3136
+ } catch (error) {
3137
+ console.warn(
3138
+ "[StormcloudVideoPlayer] Failed to parse VAST URL:",
3139
+ baseUrl,
3140
+ error
3141
+ );
3142
+ urls.push(`${baseUrl}${baseUrl.includes("?") ? "&" : "?"}correlator=${Date.now()}${i}`);
3143
+ }
3144
+ }
3145
+ return urls;
3146
+ }
3113
3147
  isAdPlaying() {
3114
3148
  return this.inAdBreak && this.ima.isAdPlaying();
3115
3149
  }
@@ -3148,7 +3182,52 @@ var StormcloudVideoPlayer = class {
3148
3182
  const tags = this.selectVastTagsForBreak(scheduled);
3149
3183
  let vastTagUrls = [];
3150
3184
  if (this.apiVastTagUrl) {
3151
- vastTagUrls = [this.apiVastTagUrl];
3185
+ let numberOfAds = 1;
3186
+ if (this.isLiveStream) {
3187
+ const adBreakDurationMs = _marker.durationSeconds != null ? _marker.durationSeconds * 1e3 : scheduled == null ? void 0 : scheduled.durationMs;
3188
+ if (adBreakDurationMs != null && adBreakDurationMs > 0) {
3189
+ this.isAdaptiveMode = true;
3190
+ this.targetAdBreakDurationMs = adBreakDurationMs;
3191
+ this.fetchedAdDurations.clear();
3192
+ numberOfAds = 2;
3193
+ if (this.config.debugAdTiming) {
3194
+ console.log(
3195
+ `[ADAPTIVE-POD] \u{1F4FA} LIVE MODE (ADAPTIVE): Target duration=${adBreakDurationMs}ms | Starting with ${numberOfAds} ads, will fetch actual durations and add more dynamically`
3196
+ );
3197
+ }
3198
+ } else {
3199
+ if (this.config.debugAdTiming) {
3200
+ console.warn(
3201
+ "[DEBUG-POD] \u26A0\uFE0F LIVE MODE: No duration available, defaulting to 1 ad"
3202
+ );
3203
+ }
3204
+ }
3205
+ } else {
3206
+ this.isAdaptiveMode = false;
3207
+ this.targetAdBreakDurationMs = null;
3208
+ this.fetchedAdDurations.clear();
3209
+ if (this.apiNumberAds && this.apiNumberAds > 1) {
3210
+ numberOfAds = this.apiNumberAds;
3211
+ if (this.config.debugAdTiming) {
3212
+ console.log(
3213
+ `[DEBUG-POD] \u{1F3AC} VOD MODE (FIXED): Using number_ads=${numberOfAds} from API`
3214
+ );
3215
+ }
3216
+ }
3217
+ }
3218
+ if (numberOfAds > 1) {
3219
+ vastTagUrls = this.generateVastUrlsWithCorrelators(
3220
+ this.apiVastTagUrl,
3221
+ numberOfAds
3222
+ );
3223
+ if (this.config.debugAdTiming) {
3224
+ console.log(
3225
+ `[DEBUG-POD] \u{1F504} Generated ${vastTagUrls.length} initial VAST URLs with unique correlators`
3226
+ );
3227
+ }
3228
+ } else {
3229
+ vastTagUrls = [this.apiVastTagUrl];
3230
+ }
3152
3231
  } else if (tags && tags.length > 0) {
3153
3232
  vastTagUrls = tags;
3154
3233
  } else {
@@ -3567,6 +3646,99 @@ var StormcloudVideoPlayer = class {
3567
3646
  }
3568
3647
  return mediaUrls;
3569
3648
  }
3649
+ async fetchVastDuration(vastTagUrl) {
3650
+ var _a;
3651
+ try {
3652
+ const response = await fetch(vastTagUrl, { mode: "cors" });
3653
+ if (!response.ok) {
3654
+ if (this.config.debugAdTiming) {
3655
+ console.warn(
3656
+ `[ADAPTIVE-POD] Failed to fetch VAST: ${response.status}`
3657
+ );
3658
+ }
3659
+ return null;
3660
+ }
3661
+ const xmlText = await response.text();
3662
+ const parser = new DOMParser();
3663
+ const xmlDoc = parser.parseFromString(xmlText, "text/xml");
3664
+ const durationText = (_a = xmlDoc.querySelector("Duration")) == null ? void 0 : _a.textContent;
3665
+ if (!durationText) {
3666
+ if (this.config.debugAdTiming) {
3667
+ console.warn("[ADAPTIVE-POD] No Duration element found in VAST");
3668
+ }
3669
+ return null;
3670
+ }
3671
+ const durationParts = durationText.split(":");
3672
+ const durationSeconds = parseInt(durationParts[0] || "0", 10) * 3600 + parseInt(durationParts[1] || "0", 10) * 60 + parseInt(durationParts[2] || "0", 10);
3673
+ return durationSeconds;
3674
+ } catch (error) {
3675
+ if (this.config.debugAdTiming) {
3676
+ console.warn(
3677
+ `[ADAPTIVE-POD] Error fetching VAST duration from ${vastTagUrl}:`,
3678
+ error
3679
+ );
3680
+ }
3681
+ return null;
3682
+ }
3683
+ }
3684
+ calculateAdditionalAdsNeeded() {
3685
+ if (!this.isAdaptiveMode || this.targetAdBreakDurationMs === null) {
3686
+ return 0;
3687
+ }
3688
+ let totalFetchedDurationMs = 0;
3689
+ for (const duration of this.fetchedAdDurations.values()) {
3690
+ totalFetchedDurationMs += duration * 1e3;
3691
+ }
3692
+ const remainingTimeMs = this.targetAdBreakDurationMs - totalFetchedDurationMs;
3693
+ if (remainingTimeMs <= 0) {
3694
+ if (this.config.debugAdTiming) {
3695
+ console.log(
3696
+ `[ADAPTIVE-POD] \u2705 Target duration reached: ${totalFetchedDurationMs}ms / ${this.targetAdBreakDurationMs}ms`
3697
+ );
3698
+ }
3699
+ return 0;
3700
+ }
3701
+ const fetchedCount = this.fetchedAdDurations.size;
3702
+ const averageDurationMs = fetchedCount > 0 ? totalFetchedDurationMs / fetchedCount : 30 * 1e3;
3703
+ const additionalAds = Math.ceil(remainingTimeMs / averageDurationMs);
3704
+ if (this.config.debugAdTiming) {
3705
+ console.log(
3706
+ `[ADAPTIVE-POD] \u{1F4CA} Need ${additionalAds} more ads | Fetched: ${totalFetchedDurationMs}ms / Target: ${this.targetAdBreakDurationMs}ms | Remaining: ${remainingTimeMs}ms | Avg duration: ${averageDurationMs}ms`
3707
+ );
3708
+ }
3709
+ return additionalAds;
3710
+ }
3711
+ async addAdaptiveAdsToQueue() {
3712
+ if (!this.isAdaptiveMode || !this.apiVastTagUrl) {
3713
+ return;
3714
+ }
3715
+ const additionalAds = this.calculateAdditionalAdsNeeded();
3716
+ if (additionalAds <= 0) {
3717
+ return;
3718
+ }
3719
+ const newUrls = this.generateVastUrlsWithCorrelators(
3720
+ this.apiVastTagUrl,
3721
+ additionalAds
3722
+ );
3723
+ if (this.config.debugAdTiming) {
3724
+ console.log(
3725
+ `[ADAPTIVE-POD] \u{1F504} Adding ${newUrls.length} additional VAST URLs to queue`
3726
+ );
3727
+ }
3728
+ this.adPodAllUrls.push(...newUrls);
3729
+ this.adPodQueue.push(...newUrls);
3730
+ this.totalAdsInBreak += newUrls.length;
3731
+ for (const url of newUrls) {
3732
+ this.preloadSingleAd(url).catch((error) => {
3733
+ if (this.config.debugAdTiming) {
3734
+ console.warn(
3735
+ `[ADAPTIVE-POD] Failed to preload adaptive ad:`,
3736
+ error
3737
+ );
3738
+ }
3739
+ });
3740
+ }
3741
+ }
3570
3742
  async preloadMediaFile(mediaUrl) {
3571
3743
  if (this.preloadedMediaUrls.has(mediaUrl)) {
3572
3744
  return;
@@ -3636,6 +3808,18 @@ var StormcloudVideoPlayer = class {
3636
3808
  async preloadSingleAd(vastTagUrl) {
3637
3809
  if (!vastTagUrl) return;
3638
3810
  try {
3811
+ if (this.isAdaptiveMode && !this.fetchedAdDurations.has(vastTagUrl)) {
3812
+ const duration = await this.fetchVastDuration(vastTagUrl);
3813
+ if (duration !== null) {
3814
+ this.fetchedAdDurations.set(vastTagUrl, duration);
3815
+ if (this.config.debugAdTiming) {
3816
+ console.log(
3817
+ `[ADAPTIVE-POD] \u2713 Fetched ad duration: ${duration}s (${this.fetchedAdDurations.size} ads fetched so far)`
3818
+ );
3819
+ }
3820
+ await this.addAdaptiveAdsToQueue();
3821
+ }
3822
+ }
3639
3823
  if (this.ima.preloadAds && !this.ima.hasPreloadedAd(vastTagUrl)) {
3640
3824
  if (!this.preloadingAdUrls.has(vastTagUrl)) {
3641
3825
  if (this.config.debugAdTiming) {