stormcloud-video-player 0.3.0 → 0.3.2

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.
@@ -80,6 +80,7 @@ declare class StormcloudVideoPlayer {
80
80
  private playSingleAd;
81
81
  private handleAdPodComplete;
82
82
  private handleAdFailure;
83
+ private tryNextAdWithRetry;
83
84
  private startAdRequestWatchdog;
84
85
  private clearAdRequestWatchdog;
85
86
  private startAdFailsafeTimer;
@@ -3195,12 +3195,7 @@ var StormcloudVideoPlayer = class {
3195
3195
  );
3196
3196
  }
3197
3197
  });
3198
- try {
3199
- await this.playAdPod();
3200
- } catch (error) {
3201
- console.error("[DEBUG-POD] \u274C Pod playback failed:", error);
3202
- this.handleAdFailure();
3203
- }
3198
+ await this.playAdPod();
3204
3199
  }
3205
3200
  if (this.expectedAdBreakDurationMs == null && (scheduled == null ? void 0 : scheduled.durationMs) != null) {
3206
3201
  this.expectedAdBreakDurationMs = scheduled.durationMs;
@@ -3213,20 +3208,30 @@ var StormcloudVideoPlayer = class {
3213
3208
  console.log("[DEBUG-POD] \u26A0\uFE0F No ads in pod");
3214
3209
  return;
3215
3210
  }
3216
- await new Promise((resolve) => setTimeout(resolve, 500));
3211
+ const waitTime = this.isAdaptiveMode ? 1500 : 500;
3212
+ await new Promise((resolve) => setTimeout(resolve, waitTime));
3217
3213
  const firstPreloaded = this.findNextPreloadedAd();
3218
3214
  if (!firstPreloaded) {
3219
3215
  console.log("[DEBUG-POD] \u26A0\uFE0F No preloaded ads after wait, trying first ad");
3220
3216
  const firstAd = this.adPodQueue.shift();
3221
3217
  if (firstAd) {
3222
3218
  this.currentAdIndex++;
3223
- await this.playSingleAd(firstAd);
3219
+ try {
3220
+ await this.playSingleAd(firstAd);
3221
+ } catch (error) {
3222
+ console.log("[DEBUG-POD] \u26A0\uFE0F First ad failed, error handler will retry");
3223
+ return;
3224
+ }
3224
3225
  }
3225
3226
  return;
3226
3227
  }
3227
3228
  this.currentAdIndex++;
3228
3229
  console.log(`[DEBUG-POD] \u{1F680} Starting pod with ad ${this.currentAdIndex}/${this.totalAdsInBreak}`);
3229
- await this.playSingleAd(firstPreloaded);
3230
+ try {
3231
+ await this.playSingleAd(firstPreloaded);
3232
+ } catch (error) {
3233
+ console.log("[DEBUG-POD] \u26A0\uFE0F First ad failed, error handler will retry");
3234
+ }
3230
3235
  }
3231
3236
  findCurrentOrNextBreak(nowMs) {
3232
3237
  var _a;
@@ -3435,6 +3440,13 @@ var StormcloudVideoPlayer = class {
3435
3440
  console.log("[DEBUG-POD] \u274C handleAdFailure - skipping to next ad or ending break");
3436
3441
  const remaining = this.getRemainingAdMs();
3437
3442
  if (remaining > 500 && this.adPodQueue.length > 0) {
3443
+ if (this.isAdaptiveMode && this.currentAdIndex <= 1) {
3444
+ console.log("[ADAPTIVE-POD] \u23F3 First ad failed, waiting for sequential preload to catch up...");
3445
+ setTimeout(() => {
3446
+ this.tryNextAdWithRetry(0);
3447
+ }, 1500);
3448
+ return;
3449
+ }
3438
3450
  const nextPreloaded = this.findNextPreloadedAd();
3439
3451
  if (nextPreloaded) {
3440
3452
  this.currentAdIndex++;
@@ -3448,6 +3460,42 @@ var StormcloudVideoPlayer = class {
3448
3460
  console.log("[DEBUG-POD] \u23F9\uFE0F Ending ad break after failure");
3449
3461
  this.handleAdPodComplete();
3450
3462
  }
3463
+ tryNextAdWithRetry(retryCount) {
3464
+ const maxRetries = 3;
3465
+ const remaining = this.getRemainingAdMs();
3466
+ if (this.config.debugAdTiming) {
3467
+ console.log(
3468
+ `[ADAPTIVE-POD] \u{1F50D} Retry attempt ${retryCount}: remaining=${remaining}ms, queue=${this.adPodQueue.length}, totalAds=${this.totalAdsInBreak}`
3469
+ );
3470
+ }
3471
+ if (remaining <= 500 || this.adPodQueue.length === 0) {
3472
+ console.log("[ADAPTIVE-POD] \u23F9\uFE0F No more time or ads available");
3473
+ this.handleAdPodComplete();
3474
+ return;
3475
+ }
3476
+ const nextPreloaded = this.findNextPreloadedAd();
3477
+ if (nextPreloaded) {
3478
+ this.currentAdIndex++;
3479
+ console.log(
3480
+ `[ADAPTIVE-POD] \u2705 Found preloaded ad after retry ${retryCount}, playing (${this.currentAdIndex}/${this.totalAdsInBreak})`
3481
+ );
3482
+ this.playSingleAd(nextPreloaded).catch(() => {
3483
+ this.handleAdPodComplete();
3484
+ });
3485
+ } else if (retryCount < maxRetries) {
3486
+ console.log(
3487
+ `[ADAPTIVE-POD] \u23F3 No preloaded ads yet (queue has ${this.adPodQueue.length} URLs), retry ${retryCount + 1}/${maxRetries} in 1s...`
3488
+ );
3489
+ setTimeout(() => {
3490
+ this.tryNextAdWithRetry(retryCount + 1);
3491
+ }, 1e3);
3492
+ } else {
3493
+ console.log(
3494
+ `[ADAPTIVE-POD] \u274C Max retries reached, no preloaded ads available (queue=${this.adPodQueue.length} URLs)`
3495
+ );
3496
+ this.handleAdPodComplete();
3497
+ }
3498
+ }
3451
3499
  startAdRequestWatchdog(token) {
3452
3500
  var _a;
3453
3501
  this.clearAdRequestWatchdog();
@@ -3658,22 +3706,12 @@ var StormcloudVideoPlayer = class {
3658
3706
  );
3659
3707
  if (this.config.debugAdTiming) {
3660
3708
  console.log(
3661
- `[ADAPTIVE-POD] \u{1F504} Adding ${newUrls.length} additional VAST URLs to queue`
3709
+ `[ADAPTIVE-POD] \u{1F504} Adding ${newUrls.length} additional VAST URLs to queue (will be preloaded sequentially)`
3662
3710
  );
3663
3711
  }
3664
3712
  this.adPodAllUrls.push(...newUrls);
3665
3713
  this.adPodQueue.push(...newUrls);
3666
3714
  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
3715
  }
3678
3716
  async preloadMediaFile(mediaUrl) {
3679
3717
  if (this.preloadedMediaUrls.has(mediaUrl)) {
@@ -3719,26 +3757,73 @@ var StormcloudVideoPlayer = class {
3719
3757
  if (this.adPodAllUrls.length === 0) {
3720
3758
  return;
3721
3759
  }
3722
- if (this.config.debugAdTiming) {
3723
- console.log(
3724
- `[StormcloudVideoPlayer] Starting parallel preload of ${this.adPodAllUrls.length} ads`
3725
- );
3726
- }
3727
- const preloadPromises = this.adPodAllUrls.map(
3728
- (vastTagUrl) => this.preloadSingleAd(vastTagUrl).catch((error) => {
3760
+ if (this.isAdaptiveMode) {
3761
+ if (this.config.debugAdTiming) {
3762
+ console.log(
3763
+ `[ADAPTIVE-POD] \u{1F504} Starting sequential preload of ${this.adPodAllUrls.length} initial ads`
3764
+ );
3765
+ }
3766
+ const processedUrls = /* @__PURE__ */ new Set();
3767
+ while (true) {
3768
+ const nextUrl = this.adPodAllUrls.find((url) => !processedUrls.has(url));
3769
+ if (!nextUrl) {
3770
+ if (this.config.debugAdTiming) {
3771
+ console.log(`[ADAPTIVE-POD] \u2705 All queued ads processed (${processedUrls.size} total)`);
3772
+ }
3773
+ break;
3774
+ }
3775
+ processedUrls.add(nextUrl);
3729
3776
  if (this.config.debugAdTiming) {
3730
- console.warn(
3731
- `[StormcloudVideoPlayer] Preload failed for ${vastTagUrl}:`,
3732
- error
3777
+ console.log(
3778
+ `[ADAPTIVE-POD] \u{1F4E5} Preloading ad ${processedUrls.size}/${this.adPodAllUrls.length}...`
3733
3779
  );
3734
3780
  }
3735
- })
3736
- );
3737
- await Promise.all(preloadPromises);
3738
- if (this.config.debugAdTiming) {
3739
- console.log(
3740
- `[StormcloudVideoPlayer] Background preloading completed for all ads`
3781
+ try {
3782
+ await this.preloadSingleAd(nextUrl);
3783
+ } catch (error) {
3784
+ if (this.config.debugAdTiming) {
3785
+ console.warn(
3786
+ `[ADAPTIVE-POD] \u26A0\uFE0F Preload failed for ad ${processedUrls.size}:`,
3787
+ error
3788
+ );
3789
+ }
3790
+ }
3791
+ if (this.calculateAdditionalAdsNeeded() === 0) {
3792
+ if (this.config.debugAdTiming) {
3793
+ console.log(
3794
+ `[ADAPTIVE-POD] \u2705 Target duration reached (${processedUrls.size} ads preloaded), stopping`
3795
+ );
3796
+ }
3797
+ break;
3798
+ }
3799
+ }
3800
+ if (this.config.debugAdTiming) {
3801
+ console.log(
3802
+ `[ADAPTIVE-POD] \u2705 Sequential preloading completed (${processedUrls.size} ads ready)`
3803
+ );
3804
+ }
3805
+ } else {
3806
+ if (this.config.debugAdTiming) {
3807
+ console.log(
3808
+ `[StormcloudVideoPlayer] Starting parallel preload of ${this.adPodAllUrls.length} ads`
3809
+ );
3810
+ }
3811
+ const preloadPromises = this.adPodAllUrls.map(
3812
+ (vastTagUrl) => this.preloadSingleAd(vastTagUrl).catch((error) => {
3813
+ if (this.config.debugAdTiming) {
3814
+ console.warn(
3815
+ `[StormcloudVideoPlayer] Preload failed for ${vastTagUrl}:`,
3816
+ error
3817
+ );
3818
+ }
3819
+ })
3741
3820
  );
3821
+ await Promise.all(preloadPromises);
3822
+ if (this.config.debugAdTiming) {
3823
+ console.log(
3824
+ `[StormcloudVideoPlayer] Background preloading completed for all ads`
3825
+ );
3826
+ }
3742
3827
  }
3743
3828
  }
3744
3829
  async preloadSingleAd(vastTagUrl) {