stormcloud-video-player 0.3.1 → 0.3.3

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.d.cts CHANGED
@@ -181,6 +181,7 @@ declare class StormcloudVideoPlayer {
181
181
  private playSingleAd;
182
182
  private handleAdPodComplete;
183
183
  private handleAdFailure;
184
+ private tryNextAdWithRetry;
184
185
  private startAdRequestWatchdog;
185
186
  private clearAdRequestWatchdog;
186
187
  private startAdFailsafeTimer;
package/lib/index.d.ts CHANGED
@@ -181,6 +181,7 @@ declare class StormcloudVideoPlayer {
181
181
  private playSingleAd;
182
182
  private handleAdPodComplete;
183
183
  private handleAdFailure;
184
+ private tryNextAdWithRetry;
184
185
  private startAdRequestWatchdog;
185
186
  private clearAdRequestWatchdog;
186
187
  private startAdFailsafeTimer;
package/lib/index.js CHANGED
@@ -3182,19 +3182,34 @@ var StormcloudVideoPlayer = class {
3182
3182
  this.currentAdIndex = 0;
3183
3183
  this.totalAdsInBreak = vastTagUrls.length;
3184
3184
  this.adPodQueue = [...vastTagUrls];
3185
- this.preloadAllAdsInBackground().catch((error) => {
3185
+ if (this.isAdaptiveMode) {
3186
3186
  if (this.config.debugAdTiming) {
3187
- console.warn(
3188
- "[StormcloudVideoPlayer] Error in background preloading:",
3189
- error
3190
- );
3187
+ console.log("[ADAPTIVE-POD] Waiting for sequential preloading to complete...");
3188
+ }
3189
+ try {
3190
+ await this.preloadAllAdsInBackground();
3191
+ if (this.config.debugAdTiming) {
3192
+ console.log("[ADAPTIVE-POD] Preloading complete, starting playback");
3193
+ }
3194
+ } catch (error) {
3195
+ if (this.config.debugAdTiming) {
3196
+ console.warn(
3197
+ "[ADAPTIVE-POD] Error in preloading:",
3198
+ error
3199
+ );
3200
+ }
3191
3201
  }
3192
- });
3193
- try {
3194
3202
  await this.playAdPod();
3195
- } catch (error) {
3196
- console.error("[DEBUG-POD] \u274C Pod playback failed:", error);
3197
- this.handleAdFailure();
3203
+ } else {
3204
+ this.preloadAllAdsInBackground().catch((error) => {
3205
+ if (this.config.debugAdTiming) {
3206
+ console.warn(
3207
+ "[StormcloudVideoPlayer] Error in background preloading:",
3208
+ error
3209
+ );
3210
+ }
3211
+ });
3212
+ await this.playAdPod();
3198
3213
  }
3199
3214
  }
3200
3215
  if (this.expectedAdBreakDurationMs == null && (scheduled == null ? void 0 : scheduled.durationMs) != null) {
@@ -3208,21 +3223,38 @@ var StormcloudVideoPlayer = class {
3208
3223
  console.log("[DEBUG-POD] \u26A0\uFE0F No ads in pod");
3209
3224
  return;
3210
3225
  }
3211
- const waitTime = this.isAdaptiveMode ? 1500 : 500;
3226
+ const waitTime = this.isAdaptiveMode ? 100 : 500;
3212
3227
  await new Promise((resolve) => setTimeout(resolve, waitTime));
3228
+ if (this.config.debugAdTiming) {
3229
+ console.log(
3230
+ `[DEBUG-POD] \u{1F50D} Looking for preloaded ad in queue of ${this.adPodQueue.length} URLs`
3231
+ );
3232
+ }
3213
3233
  const firstPreloaded = this.findNextPreloadedAd();
3214
3234
  if (!firstPreloaded) {
3215
- console.log("[DEBUG-POD] \u26A0\uFE0F No preloaded ads after wait, trying first ad");
3235
+ console.log("[DEBUG-POD] \u26A0\uFE0F No preloaded ads found, trying first ad from queue");
3216
3236
  const firstAd = this.adPodQueue.shift();
3217
3237
  if (firstAd) {
3218
3238
  this.currentAdIndex++;
3219
- await this.playSingleAd(firstAd);
3239
+ console.log(`[DEBUG-POD] \u{1F3AC} Attempting to play first ad (not preloaded): ${firstAd.substring(0, 60)}...`);
3240
+ try {
3241
+ await this.playSingleAd(firstAd);
3242
+ } catch (error) {
3243
+ console.log("[DEBUG-POD] \u26A0\uFE0F First ad failed, error handler will retry");
3244
+ return;
3245
+ }
3246
+ } else {
3247
+ console.log("[DEBUG-POD] \u274C No ads available in queue");
3220
3248
  }
3221
3249
  return;
3222
3250
  }
3223
3251
  this.currentAdIndex++;
3224
3252
  console.log(`[DEBUG-POD] \u{1F680} Starting pod with ad ${this.currentAdIndex}/${this.totalAdsInBreak}`);
3225
- await this.playSingleAd(firstPreloaded);
3253
+ try {
3254
+ await this.playSingleAd(firstPreloaded);
3255
+ } catch (error) {
3256
+ console.log("[DEBUG-POD] \u26A0\uFE0F First ad failed, error handler will retry");
3257
+ }
3226
3258
  }
3227
3259
  findCurrentOrNextBreak(nowMs) {
3228
3260
  var _a;
@@ -3431,6 +3463,13 @@ var StormcloudVideoPlayer = class {
3431
3463
  console.log("[DEBUG-POD] \u274C handleAdFailure - skipping to next ad or ending break");
3432
3464
  const remaining = this.getRemainingAdMs();
3433
3465
  if (remaining > 500 && this.adPodQueue.length > 0) {
3466
+ if (this.isAdaptiveMode && this.currentAdIndex <= 1) {
3467
+ console.log("[ADAPTIVE-POD] \u23F3 First ad failed, waiting for sequential preload to catch up...");
3468
+ setTimeout(() => {
3469
+ this.tryNextAdWithRetry(0);
3470
+ }, 1500);
3471
+ return;
3472
+ }
3434
3473
  const nextPreloaded = this.findNextPreloadedAd();
3435
3474
  if (nextPreloaded) {
3436
3475
  this.currentAdIndex++;
@@ -3444,6 +3483,42 @@ var StormcloudVideoPlayer = class {
3444
3483
  console.log("[DEBUG-POD] \u23F9\uFE0F Ending ad break after failure");
3445
3484
  this.handleAdPodComplete();
3446
3485
  }
3486
+ tryNextAdWithRetry(retryCount) {
3487
+ const maxRetries = 3;
3488
+ const remaining = this.getRemainingAdMs();
3489
+ if (this.config.debugAdTiming) {
3490
+ console.log(
3491
+ `[ADAPTIVE-POD] \u{1F50D} Retry attempt ${retryCount}: remaining=${remaining}ms, queue=${this.adPodQueue.length}, totalAds=${this.totalAdsInBreak}`
3492
+ );
3493
+ }
3494
+ if (remaining <= 500 || this.adPodQueue.length === 0) {
3495
+ console.log("[ADAPTIVE-POD] \u23F9\uFE0F No more time or ads available");
3496
+ this.handleAdPodComplete();
3497
+ return;
3498
+ }
3499
+ const nextPreloaded = this.findNextPreloadedAd();
3500
+ if (nextPreloaded) {
3501
+ this.currentAdIndex++;
3502
+ console.log(
3503
+ `[ADAPTIVE-POD] \u2705 Found preloaded ad after retry ${retryCount}, playing (${this.currentAdIndex}/${this.totalAdsInBreak})`
3504
+ );
3505
+ this.playSingleAd(nextPreloaded).catch(() => {
3506
+ this.handleAdPodComplete();
3507
+ });
3508
+ } else if (retryCount < maxRetries) {
3509
+ console.log(
3510
+ `[ADAPTIVE-POD] \u23F3 No preloaded ads yet (queue has ${this.adPodQueue.length} URLs), retry ${retryCount + 1}/${maxRetries} in 1s...`
3511
+ );
3512
+ setTimeout(() => {
3513
+ this.tryNextAdWithRetry(retryCount + 1);
3514
+ }, 1e3);
3515
+ } else {
3516
+ console.log(
3517
+ `[ADAPTIVE-POD] \u274C Max retries reached, no preloaded ads available (queue=${this.adPodQueue.length} URLs)`
3518
+ );
3519
+ this.handleAdPodComplete();
3520
+ }
3521
+ }
3447
3522
  startAdRequestWatchdog(token) {
3448
3523
  var _a;
3449
3524
  this.clearAdRequestWatchdog();
@@ -3708,22 +3783,30 @@ var StormcloudVideoPlayer = class {
3708
3783
  if (this.isAdaptiveMode) {
3709
3784
  if (this.config.debugAdTiming) {
3710
3785
  console.log(
3711
- `[ADAPTIVE-POD] Starting sequential preload of ${this.adPodAllUrls.length} initial ads`
3786
+ `[ADAPTIVE-POD] \u{1F504} Starting sequential preload of ${this.adPodAllUrls.length} initial ads`
3712
3787
  );
3713
3788
  }
3714
3789
  const processedUrls = /* @__PURE__ */ new Set();
3715
3790
  while (true) {
3716
3791
  const nextUrl = this.adPodAllUrls.find((url) => !processedUrls.has(url));
3717
3792
  if (!nextUrl) {
3793
+ if (this.config.debugAdTiming) {
3794
+ console.log(`[ADAPTIVE-POD] \u2705 All queued ads processed (${processedUrls.size} total)`);
3795
+ }
3718
3796
  break;
3719
3797
  }
3720
3798
  processedUrls.add(nextUrl);
3799
+ if (this.config.debugAdTiming) {
3800
+ console.log(
3801
+ `[ADAPTIVE-POD] \u{1F4E5} Preloading ad ${processedUrls.size}/${this.adPodAllUrls.length}...`
3802
+ );
3803
+ }
3721
3804
  try {
3722
3805
  await this.preloadSingleAd(nextUrl);
3723
3806
  } catch (error) {
3724
3807
  if (this.config.debugAdTiming) {
3725
3808
  console.warn(
3726
- `[ADAPTIVE-POD] Preload failed for ${nextUrl}:`,
3809
+ `[ADAPTIVE-POD] \u26A0\uFE0F Preload failed for ad ${processedUrls.size}:`,
3727
3810
  error
3728
3811
  );
3729
3812
  }
@@ -3731,7 +3814,7 @@ var StormcloudVideoPlayer = class {
3731
3814
  if (this.calculateAdditionalAdsNeeded() === 0) {
3732
3815
  if (this.config.debugAdTiming) {
3733
3816
  console.log(
3734
- `[ADAPTIVE-POD] \u2705 Target duration reached, stopping preload`
3817
+ `[ADAPTIVE-POD] \u2705 Target duration reached (${processedUrls.size} ads preloaded), stopping`
3735
3818
  );
3736
3819
  }
3737
3820
  break;
@@ -3739,7 +3822,7 @@ var StormcloudVideoPlayer = class {
3739
3822
  }
3740
3823
  if (this.config.debugAdTiming) {
3741
3824
  console.log(
3742
- `[ADAPTIVE-POD] Sequential preloading completed (${processedUrls.size} ads preloaded)`
3825
+ `[ADAPTIVE-POD] \u2705 Sequential preloading completed (${processedUrls.size} ads ready)`
3743
3826
  );
3744
3827
  }
3745
3828
  } else {
@@ -3836,17 +3919,26 @@ var StormcloudVideoPlayer = class {
3836
3919
  }
3837
3920
  findNextPreloadedAd() {
3838
3921
  var _a, _b, _c;
3922
+ if (this.config.debugAdTiming) {
3923
+ console.log(
3924
+ `[DEBUG-POD] \u{1F50E} Searching for preloaded ad in queue (${this.adPodQueue.length} URLs, ${this.preloadedMediaUrls.size} media preloaded, ${this.vastToMediaUrlMap.size} VAST mappings)`
3925
+ );
3926
+ }
3839
3927
  for (let i = 0; i < this.adPodQueue.length; i++) {
3840
3928
  const vastTagUrl = this.adPodQueue[i];
3841
3929
  if (!vastTagUrl) continue;
3842
3930
  const hasImaPreload = (_c = (_b = (_a = this.ima).hasPreloadedAd) == null ? void 0 : _b.call(_a, vastTagUrl)) != null ? _c : false;
3843
3931
  const mediaUrls = this.vastToMediaUrlMap.get(vastTagUrl);
3844
3932
  const hasMediaPreload = mediaUrls && mediaUrls.length > 0 ? this.preloadedMediaUrls.has(mediaUrls[0]) : false;
3933
+ if (this.config.debugAdTiming) {
3934
+ console.log(
3935
+ `[DEBUG-POD] Ad ${i}: IMA=${hasImaPreload}, Media=${hasMediaPreload}, MediaURLs=${(mediaUrls == null ? void 0 : mediaUrls.length) || 0}, URL=${vastTagUrl.substring(0, 60)}...`
3936
+ );
3937
+ }
3845
3938
  if (hasImaPreload || hasMediaPreload) {
3846
3939
  if (this.config.debugAdTiming) {
3847
3940
  console.log(
3848
- `[StormcloudVideoPlayer] Found preloaded ad at index ${i}: ${vastTagUrl}`,
3849
- { hasImaPreload, hasMediaPreload }
3941
+ `[DEBUG-POD] \u2705 Found preloaded ad at index ${i}`
3850
3942
  );
3851
3943
  }
3852
3944
  this.adPodQueue.splice(0, i + 1);
@@ -3855,7 +3947,7 @@ var StormcloudVideoPlayer = class {
3855
3947
  }
3856
3948
  if (this.config.debugAdTiming) {
3857
3949
  console.log(
3858
- "[StormcloudVideoPlayer] No preloaded ads found in queue"
3950
+ `[DEBUG-POD] \u274C No preloaded ads found in queue`
3859
3951
  );
3860
3952
  }
3861
3953
  return void 0;