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.
@@ -2159,6 +2159,9 @@ var StormcloudVideoPlayer = class {
2159
2159
  this.activeAdRequestToken = null;
2160
2160
  this.adRequestWatchdogToken = null;
2161
2161
  this.adFailsafeToken = null;
2162
+ this.fetchedAdDurations = /* @__PURE__ */ new Map();
2163
+ this.targetAdBreakDurationMs = null;
2164
+ this.isAdaptiveMode = false;
2162
2165
  initializePolyfills();
2163
2166
  const browserOverrides = getBrowserConfigOverrides();
2164
2167
  this.config = { ...config, ...browserOverrides };
@@ -2965,7 +2968,7 @@ var StormcloudVideoPlayer = class {
2965
2968
  }
2966
2969
  }
2967
2970
  async fetchAdConfiguration() {
2968
- var _a, _b, _c;
2971
+ var _a, _b, _c, _d, _e, _f, _g;
2969
2972
  const vastMode = this.config.vastMode || "default";
2970
2973
  if (this.config.debugAdTiming) {
2971
2974
  console.log("[StormcloudVideoPlayer] VAST mode:", vastMode);
@@ -3036,6 +3039,16 @@ var StormcloudVideoPlayer = class {
3036
3039
  );
3037
3040
  }
3038
3041
  }
3042
+ 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;
3043
+ if (numberAds != null && numberAds > 0) {
3044
+ this.apiNumberAds = numberAds;
3045
+ if (this.config.debugAdTiming) {
3046
+ console.log(
3047
+ "[StormcloudVideoPlayer] Number of ads per break from API:",
3048
+ this.apiNumberAds
3049
+ );
3050
+ }
3051
+ }
3039
3052
  }
3040
3053
  getCurrentAdIndex() {
3041
3054
  return this.currentAdIndex;
@@ -3043,6 +3056,27 @@ var StormcloudVideoPlayer = class {
3043
3056
  getTotalAdsInBreak() {
3044
3057
  return this.totalAdsInBreak;
3045
3058
  }
3059
+ generateVastUrlsWithCorrelators(baseUrl, count) {
3060
+ const urls = [];
3061
+ for (let i = 0; i < count; i++) {
3062
+ try {
3063
+ const url = new URL(baseUrl);
3064
+ const timestamp = Date.now();
3065
+ const random = Math.floor(Math.random() * 1e6);
3066
+ const uniqueCorrelator = `${timestamp}${random}${i}`;
3067
+ url.searchParams.set("correlator", uniqueCorrelator);
3068
+ urls.push(url.toString());
3069
+ } catch (error) {
3070
+ console.warn(
3071
+ "[StormcloudVideoPlayer] Failed to parse VAST URL:",
3072
+ baseUrl,
3073
+ error
3074
+ );
3075
+ urls.push(`${baseUrl}${baseUrl.includes("?") ? "&" : "?"}correlator=${Date.now()}${i}`);
3076
+ }
3077
+ }
3078
+ return urls;
3079
+ }
3046
3080
  isAdPlaying() {
3047
3081
  return this.inAdBreak && this.ima.isAdPlaying();
3048
3082
  }
@@ -3081,7 +3115,52 @@ var StormcloudVideoPlayer = class {
3081
3115
  const tags = this.selectVastTagsForBreak(scheduled);
3082
3116
  let vastTagUrls = [];
3083
3117
  if (this.apiVastTagUrl) {
3084
- vastTagUrls = [this.apiVastTagUrl];
3118
+ let numberOfAds = 1;
3119
+ if (this.isLiveStream) {
3120
+ const adBreakDurationMs = _marker.durationSeconds != null ? _marker.durationSeconds * 1e3 : scheduled == null ? void 0 : scheduled.durationMs;
3121
+ if (adBreakDurationMs != null && adBreakDurationMs > 0) {
3122
+ this.isAdaptiveMode = true;
3123
+ this.targetAdBreakDurationMs = adBreakDurationMs;
3124
+ this.fetchedAdDurations.clear();
3125
+ numberOfAds = 2;
3126
+ if (this.config.debugAdTiming) {
3127
+ console.log(
3128
+ `[ADAPTIVE-POD] \u{1F4FA} LIVE MODE (ADAPTIVE): Target duration=${adBreakDurationMs}ms | Starting with ${numberOfAds} ads, will fetch actual durations and add more dynamically`
3129
+ );
3130
+ }
3131
+ } else {
3132
+ if (this.config.debugAdTiming) {
3133
+ console.warn(
3134
+ "[DEBUG-POD] \u26A0\uFE0F LIVE MODE: No duration available, defaulting to 1 ad"
3135
+ );
3136
+ }
3137
+ }
3138
+ } else {
3139
+ this.isAdaptiveMode = false;
3140
+ this.targetAdBreakDurationMs = null;
3141
+ this.fetchedAdDurations.clear();
3142
+ if (this.apiNumberAds && this.apiNumberAds > 1) {
3143
+ numberOfAds = this.apiNumberAds;
3144
+ if (this.config.debugAdTiming) {
3145
+ console.log(
3146
+ `[DEBUG-POD] \u{1F3AC} VOD MODE (FIXED): Using number_ads=${numberOfAds} from API`
3147
+ );
3148
+ }
3149
+ }
3150
+ }
3151
+ if (numberOfAds > 1) {
3152
+ vastTagUrls = this.generateVastUrlsWithCorrelators(
3153
+ this.apiVastTagUrl,
3154
+ numberOfAds
3155
+ );
3156
+ if (this.config.debugAdTiming) {
3157
+ console.log(
3158
+ `[DEBUG-POD] \u{1F504} Generated ${vastTagUrls.length} initial VAST URLs with unique correlators`
3159
+ );
3160
+ }
3161
+ } else {
3162
+ vastTagUrls = [this.apiVastTagUrl];
3163
+ }
3085
3164
  } else if (tags && tags.length > 0) {
3086
3165
  vastTagUrls = tags;
3087
3166
  } else {
@@ -3500,6 +3579,99 @@ var StormcloudVideoPlayer = class {
3500
3579
  }
3501
3580
  return mediaUrls;
3502
3581
  }
3582
+ async fetchVastDuration(vastTagUrl) {
3583
+ var _a;
3584
+ try {
3585
+ const response = await fetch(vastTagUrl, { mode: "cors" });
3586
+ if (!response.ok) {
3587
+ if (this.config.debugAdTiming) {
3588
+ console.warn(
3589
+ `[ADAPTIVE-POD] Failed to fetch VAST: ${response.status}`
3590
+ );
3591
+ }
3592
+ return null;
3593
+ }
3594
+ const xmlText = await response.text();
3595
+ const parser = new DOMParser();
3596
+ const xmlDoc = parser.parseFromString(xmlText, "text/xml");
3597
+ const durationText = (_a = xmlDoc.querySelector("Duration")) == null ? void 0 : _a.textContent;
3598
+ if (!durationText) {
3599
+ if (this.config.debugAdTiming) {
3600
+ console.warn("[ADAPTIVE-POD] No Duration element found in VAST");
3601
+ }
3602
+ return null;
3603
+ }
3604
+ const durationParts = durationText.split(":");
3605
+ const durationSeconds = parseInt(durationParts[0] || "0", 10) * 3600 + parseInt(durationParts[1] || "0", 10) * 60 + parseInt(durationParts[2] || "0", 10);
3606
+ return durationSeconds;
3607
+ } catch (error) {
3608
+ if (this.config.debugAdTiming) {
3609
+ console.warn(
3610
+ `[ADAPTIVE-POD] Error fetching VAST duration from ${vastTagUrl}:`,
3611
+ error
3612
+ );
3613
+ }
3614
+ return null;
3615
+ }
3616
+ }
3617
+ calculateAdditionalAdsNeeded() {
3618
+ if (!this.isAdaptiveMode || this.targetAdBreakDurationMs === null) {
3619
+ return 0;
3620
+ }
3621
+ let totalFetchedDurationMs = 0;
3622
+ for (const duration of this.fetchedAdDurations.values()) {
3623
+ totalFetchedDurationMs += duration * 1e3;
3624
+ }
3625
+ const remainingTimeMs = this.targetAdBreakDurationMs - totalFetchedDurationMs;
3626
+ if (remainingTimeMs <= 0) {
3627
+ if (this.config.debugAdTiming) {
3628
+ console.log(
3629
+ `[ADAPTIVE-POD] \u2705 Target duration reached: ${totalFetchedDurationMs}ms / ${this.targetAdBreakDurationMs}ms`
3630
+ );
3631
+ }
3632
+ return 0;
3633
+ }
3634
+ const fetchedCount = this.fetchedAdDurations.size;
3635
+ const averageDurationMs = fetchedCount > 0 ? totalFetchedDurationMs / fetchedCount : 30 * 1e3;
3636
+ const additionalAds = Math.ceil(remainingTimeMs / averageDurationMs);
3637
+ if (this.config.debugAdTiming) {
3638
+ console.log(
3639
+ `[ADAPTIVE-POD] \u{1F4CA} Need ${additionalAds} more ads | Fetched: ${totalFetchedDurationMs}ms / Target: ${this.targetAdBreakDurationMs}ms | Remaining: ${remainingTimeMs}ms | Avg duration: ${averageDurationMs}ms`
3640
+ );
3641
+ }
3642
+ return additionalAds;
3643
+ }
3644
+ async addAdaptiveAdsToQueue() {
3645
+ if (!this.isAdaptiveMode || !this.apiVastTagUrl) {
3646
+ return;
3647
+ }
3648
+ const additionalAds = this.calculateAdditionalAdsNeeded();
3649
+ if (additionalAds <= 0) {
3650
+ return;
3651
+ }
3652
+ const newUrls = this.generateVastUrlsWithCorrelators(
3653
+ this.apiVastTagUrl,
3654
+ additionalAds
3655
+ );
3656
+ if (this.config.debugAdTiming) {
3657
+ console.log(
3658
+ `[ADAPTIVE-POD] \u{1F504} Adding ${newUrls.length} additional VAST URLs to queue`
3659
+ );
3660
+ }
3661
+ this.adPodAllUrls.push(...newUrls);
3662
+ this.adPodQueue.push(...newUrls);
3663
+ this.totalAdsInBreak += newUrls.length;
3664
+ for (const url of newUrls) {
3665
+ this.preloadSingleAd(url).catch((error) => {
3666
+ if (this.config.debugAdTiming) {
3667
+ console.warn(
3668
+ `[ADAPTIVE-POD] Failed to preload adaptive ad:`,
3669
+ error
3670
+ );
3671
+ }
3672
+ });
3673
+ }
3674
+ }
3503
3675
  async preloadMediaFile(mediaUrl) {
3504
3676
  if (this.preloadedMediaUrls.has(mediaUrl)) {
3505
3677
  return;
@@ -3569,6 +3741,18 @@ var StormcloudVideoPlayer = class {
3569
3741
  async preloadSingleAd(vastTagUrl) {
3570
3742
  if (!vastTagUrl) return;
3571
3743
  try {
3744
+ if (this.isAdaptiveMode && !this.fetchedAdDurations.has(vastTagUrl)) {
3745
+ const duration = await this.fetchVastDuration(vastTagUrl);
3746
+ if (duration !== null) {
3747
+ this.fetchedAdDurations.set(vastTagUrl, duration);
3748
+ if (this.config.debugAdTiming) {
3749
+ console.log(
3750
+ `[ADAPTIVE-POD] \u2713 Fetched ad duration: ${duration}s (${this.fetchedAdDurations.size} ads fetched so far)`
3751
+ );
3752
+ }
3753
+ await this.addAdaptiveAdsToQueue();
3754
+ }
3755
+ }
3572
3756
  if (this.ima.preloadAds && !this.ima.hasPreloadedAd(vastTagUrl)) {
3573
3757
  if (!this.preloadingAdUrls.has(vastTagUrl)) {
3574
3758
  if (this.config.debugAdTiming) {