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