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