stormcloud-video-player 0.2.36 → 0.3.1

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 {
@@ -3131,7 +3210,8 @@ var StormcloudVideoPlayer = class {
3131
3210
  console.log("[DEBUG-POD] \u26A0\uFE0F No ads in pod");
3132
3211
  return;
3133
3212
  }
3134
- await new Promise((resolve) => setTimeout(resolve, 500));
3213
+ const waitTime = this.isAdaptiveMode ? 1500 : 500;
3214
+ await new Promise((resolve) => setTimeout(resolve, waitTime));
3135
3215
  const firstPreloaded = this.findNextPreloadedAd();
3136
3216
  if (!firstPreloaded) {
3137
3217
  console.log("[DEBUG-POD] \u26A0\uFE0F No preloaded ads after wait, trying first ad");
@@ -3500,6 +3580,89 @@ var StormcloudVideoPlayer = class {
3500
3580
  }
3501
3581
  return mediaUrls;
3502
3582
  }
3583
+ async fetchVastDuration(vastTagUrl) {
3584
+ var _a;
3585
+ try {
3586
+ const response = await fetch(vastTagUrl, { mode: "cors" });
3587
+ if (!response.ok) {
3588
+ if (this.config.debugAdTiming) {
3589
+ console.warn(
3590
+ `[ADAPTIVE-POD] Failed to fetch VAST: ${response.status}`
3591
+ );
3592
+ }
3593
+ return null;
3594
+ }
3595
+ const xmlText = await response.text();
3596
+ const parser = new DOMParser();
3597
+ const xmlDoc = parser.parseFromString(xmlText, "text/xml");
3598
+ const durationText = (_a = xmlDoc.querySelector("Duration")) == null ? void 0 : _a.textContent;
3599
+ if (!durationText) {
3600
+ if (this.config.debugAdTiming) {
3601
+ console.warn("[ADAPTIVE-POD] No Duration element found in VAST");
3602
+ }
3603
+ return null;
3604
+ }
3605
+ const durationParts = durationText.split(":");
3606
+ const durationSeconds = parseInt(durationParts[0] || "0", 10) * 3600 + parseInt(durationParts[1] || "0", 10) * 60 + parseInt(durationParts[2] || "0", 10);
3607
+ return durationSeconds;
3608
+ } catch (error) {
3609
+ if (this.config.debugAdTiming) {
3610
+ console.warn(
3611
+ `[ADAPTIVE-POD] Error fetching VAST duration from ${vastTagUrl}:`,
3612
+ error
3613
+ );
3614
+ }
3615
+ return null;
3616
+ }
3617
+ }
3618
+ calculateAdditionalAdsNeeded() {
3619
+ if (!this.isAdaptiveMode || this.targetAdBreakDurationMs === null) {
3620
+ return 0;
3621
+ }
3622
+ let totalFetchedDurationMs = 0;
3623
+ for (const duration of this.fetchedAdDurations.values()) {
3624
+ totalFetchedDurationMs += duration * 1e3;
3625
+ }
3626
+ const remainingTimeMs = this.targetAdBreakDurationMs - totalFetchedDurationMs;
3627
+ if (remainingTimeMs <= 0) {
3628
+ if (this.config.debugAdTiming) {
3629
+ console.log(
3630
+ `[ADAPTIVE-POD] \u2705 Target duration reached: ${totalFetchedDurationMs}ms / ${this.targetAdBreakDurationMs}ms`
3631
+ );
3632
+ }
3633
+ return 0;
3634
+ }
3635
+ const fetchedCount = this.fetchedAdDurations.size;
3636
+ const averageDurationMs = fetchedCount > 0 ? totalFetchedDurationMs / fetchedCount : 30 * 1e3;
3637
+ const additionalAds = Math.ceil(remainingTimeMs / averageDurationMs);
3638
+ if (this.config.debugAdTiming) {
3639
+ console.log(
3640
+ `[ADAPTIVE-POD] \u{1F4CA} Need ${additionalAds} more ads | Fetched: ${totalFetchedDurationMs}ms / Target: ${this.targetAdBreakDurationMs}ms | Remaining: ${remainingTimeMs}ms | Avg duration: ${averageDurationMs}ms`
3641
+ );
3642
+ }
3643
+ return additionalAds;
3644
+ }
3645
+ async addAdaptiveAdsToQueue() {
3646
+ if (!this.isAdaptiveMode || !this.apiVastTagUrl) {
3647
+ return;
3648
+ }
3649
+ const additionalAds = this.calculateAdditionalAdsNeeded();
3650
+ if (additionalAds <= 0) {
3651
+ return;
3652
+ }
3653
+ const newUrls = this.generateVastUrlsWithCorrelators(
3654
+ this.apiVastTagUrl,
3655
+ additionalAds
3656
+ );
3657
+ if (this.config.debugAdTiming) {
3658
+ console.log(
3659
+ `[ADAPTIVE-POD] \u{1F504} Adding ${newUrls.length} additional VAST URLs to queue (will be preloaded sequentially)`
3660
+ );
3661
+ }
3662
+ this.adPodAllUrls.push(...newUrls);
3663
+ this.adPodQueue.push(...newUrls);
3664
+ this.totalAdsInBreak += newUrls.length;
3665
+ }
3503
3666
  async preloadMediaFile(mediaUrl) {
3504
3667
  if (this.preloadedMediaUrls.has(mediaUrl)) {
3505
3668
  return;
@@ -3544,31 +3707,82 @@ var StormcloudVideoPlayer = class {
3544
3707
  if (this.adPodAllUrls.length === 0) {
3545
3708
  return;
3546
3709
  }
3547
- if (this.config.debugAdTiming) {
3548
- console.log(
3549
- `[StormcloudVideoPlayer] Starting parallel preload of ${this.adPodAllUrls.length} ads`
3550
- );
3551
- }
3552
- const preloadPromises = this.adPodAllUrls.map(
3553
- (vastTagUrl) => this.preloadSingleAd(vastTagUrl).catch((error) => {
3554
- if (this.config.debugAdTiming) {
3555
- console.warn(
3556
- `[StormcloudVideoPlayer] Preload failed for ${vastTagUrl}:`,
3557
- error
3558
- );
3710
+ if (this.isAdaptiveMode) {
3711
+ if (this.config.debugAdTiming) {
3712
+ console.log(
3713
+ `[ADAPTIVE-POD] Starting sequential preload of ${this.adPodAllUrls.length} initial ads`
3714
+ );
3715
+ }
3716
+ const processedUrls = /* @__PURE__ */ new Set();
3717
+ while (true) {
3718
+ const nextUrl = this.adPodAllUrls.find((url) => !processedUrls.has(url));
3719
+ if (!nextUrl) {
3720
+ break;
3559
3721
  }
3560
- })
3561
- );
3562
- await Promise.all(preloadPromises);
3563
- if (this.config.debugAdTiming) {
3564
- console.log(
3565
- `[StormcloudVideoPlayer] Background preloading completed for all ads`
3722
+ processedUrls.add(nextUrl);
3723
+ try {
3724
+ await this.preloadSingleAd(nextUrl);
3725
+ } catch (error) {
3726
+ if (this.config.debugAdTiming) {
3727
+ console.warn(
3728
+ `[ADAPTIVE-POD] Preload failed for ${nextUrl}:`,
3729
+ error
3730
+ );
3731
+ }
3732
+ }
3733
+ if (this.calculateAdditionalAdsNeeded() === 0) {
3734
+ if (this.config.debugAdTiming) {
3735
+ console.log(
3736
+ `[ADAPTIVE-POD] \u2705 Target duration reached, stopping preload`
3737
+ );
3738
+ }
3739
+ break;
3740
+ }
3741
+ }
3742
+ if (this.config.debugAdTiming) {
3743
+ console.log(
3744
+ `[ADAPTIVE-POD] Sequential preloading completed (${processedUrls.size} ads preloaded)`
3745
+ );
3746
+ }
3747
+ } else {
3748
+ if (this.config.debugAdTiming) {
3749
+ console.log(
3750
+ `[StormcloudVideoPlayer] Starting parallel preload of ${this.adPodAllUrls.length} ads`
3751
+ );
3752
+ }
3753
+ const preloadPromises = this.adPodAllUrls.map(
3754
+ (vastTagUrl) => this.preloadSingleAd(vastTagUrl).catch((error) => {
3755
+ if (this.config.debugAdTiming) {
3756
+ console.warn(
3757
+ `[StormcloudVideoPlayer] Preload failed for ${vastTagUrl}:`,
3758
+ error
3759
+ );
3760
+ }
3761
+ })
3566
3762
  );
3763
+ await Promise.all(preloadPromises);
3764
+ if (this.config.debugAdTiming) {
3765
+ console.log(
3766
+ `[StormcloudVideoPlayer] Background preloading completed for all ads`
3767
+ );
3768
+ }
3567
3769
  }
3568
3770
  }
3569
3771
  async preloadSingleAd(vastTagUrl) {
3570
3772
  if (!vastTagUrl) return;
3571
3773
  try {
3774
+ if (this.isAdaptiveMode && !this.fetchedAdDurations.has(vastTagUrl)) {
3775
+ const duration = await this.fetchVastDuration(vastTagUrl);
3776
+ if (duration !== null) {
3777
+ this.fetchedAdDurations.set(vastTagUrl, duration);
3778
+ if (this.config.debugAdTiming) {
3779
+ console.log(
3780
+ `[ADAPTIVE-POD] \u2713 Fetched ad duration: ${duration}s (${this.fetchedAdDurations.size} ads fetched so far)`
3781
+ );
3782
+ }
3783
+ await this.addAdaptiveAdsToQueue();
3784
+ }
3785
+ }
3572
3786
  if (this.ima.preloadAds && !this.ima.hasPreloadedAd(vastTagUrl)) {
3573
3787
  if (!this.preloadingAdUrls.has(vastTagUrl)) {
3574
3788
  if (this.config.debugAdTiming) {