stormcloud-video-player 0.7.10 → 0.7.12

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.js CHANGED
@@ -1413,7 +1413,6 @@ function createAdStormPlayer(contentVideo, options) {
1413
1413
  contentVideo.muted = originalMutedState;
1414
1414
  contentVideo.volume = originalVolume;
1415
1415
  currentAd = void 0;
1416
- tornDown = false;
1417
1416
  return [
1418
1417
  2,
1419
1418
  Promise.resolve()
@@ -1528,6 +1527,15 @@ function createAdStormPlayer(contentVideo, options) {
1528
1527
  if (!token) return [
1529
1528
  2
1530
1529
  ];
1530
+ if (currentAd) {
1531
+ preloadSlots.set(token, {
1532
+ ad: currentAd
1533
+ });
1534
+ currentAd = void 0;
1535
+ return [
1536
+ 2
1537
+ ];
1538
+ }
1531
1539
  requestContext = typeof arg1 === "string" ? arg2 : arg1;
1532
1540
  return [
1533
1541
  4,
@@ -2021,56 +2029,6 @@ function sendInitialTracking(licenseKey) {
2021
2029
  });
2022
2030
  })();
2023
2031
  }
2024
- function sendAdDetectTracking(licenseKey, adDetectInfo) {
2025
- return _async_to_generator(function() {
2026
- var clientInfo, browserId, trackingData, error;
2027
- return _ts_generator(this, function(_state) {
2028
- switch(_state.label){
2029
- case 0:
2030
- _state.trys.push([
2031
- 0,
2032
- 3,
2033
- ,
2034
- 4
2035
- ]);
2036
- clientInfo = getClientInfo();
2037
- return [
2038
- 4,
2039
- getBrowserID(clientInfo)
2040
- ];
2041
- case 1:
2042
- browserId = _state.sent();
2043
- trackingData = _object_spread({
2044
- browserId: browserId
2045
- }, clientInfo);
2046
- return [
2047
- 4,
2048
- sendTrackRequest(licenseKey, _object_spread_props(_object_spread({}, trackingData), {
2049
- licenseKey: licenseKey,
2050
- adDetectInfo: adDetectInfo
2051
- }))
2052
- ];
2053
- case 2:
2054
- _state.sent();
2055
- return [
2056
- 3,
2057
- 4
2058
- ];
2059
- case 3:
2060
- error = _state.sent();
2061
- console.error("[StormcloudVideoPlayer] Error sending ad detect tracking:", error);
2062
- return [
2063
- 3,
2064
- 4
2065
- ];
2066
- case 4:
2067
- return [
2068
- 2
2069
- ];
2070
- }
2071
- });
2072
- })();
2073
- }
2074
2032
  function sendAdLoadedTracking(licenseKey, adLoadedInfo) {
2075
2033
  return _async_to_generator(function() {
2076
2034
  var clientInfo, browserId, trackingData, error;
@@ -2783,7 +2741,6 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
2783
2741
  this.continuousFetchLoopPromise = null;
2784
2742
  this.attached = false;
2785
2743
  this.inAdBreak = false;
2786
- this.ptsDriftEmaMs = 0;
2787
2744
  this.adPodQueue = [];
2788
2745
  this.lastHeartbeatTime = 0;
2789
2746
  this.currentAdIndex = 0;
@@ -2802,10 +2759,12 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
2802
2759
  this.isInAdTransition = false;
2803
2760
  this.maxPlaceholderDurationMs = 5e3;
2804
2761
  this.isShowingPlaceholder = false;
2762
+ this.lastAdInsertionPoint = null;
2763
+ this.processedAdInsertionUpdatedAt = null;
2805
2764
  this.totalAdRequestsInBreak = 0;
2806
2765
  this.maxTotalAdRequestsPerBreak = 20;
2807
2766
  this.pendingAdBreak = null;
2808
- this.savedMutedStateBeforeScte = null;
2767
+ this.savedMutedStateBeforeAd = null;
2809
2768
  this.consecutiveFailures = 0;
2810
2769
  this.maxConsecutiveFailures = 5;
2811
2770
  this.lastAdRequestTime = 0;
@@ -2818,7 +2777,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
2818
2777
  this.adRequestRetryBackoffMs = 1500;
2819
2778
  this.preloadedTokens = [];
2820
2779
  this.debugLogEntries = [];
2821
- this.scteMarkerHistory = [];
2780
+ this.adInsertionDebugHistory = [];
2822
2781
  initializePolyfills();
2823
2782
  var browserOverrides = getBrowserConfigOverrides();
2824
2783
  this.config = _object_spread({}, browserOverrides, config);
@@ -3003,6 +2962,9 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3003
2962
  _state.sent();
3004
2963
  _state.label = 2;
3005
2964
  case 2:
2965
+ if (!this.config.disableAds && this.config.projectId) {
2966
+ this.startAdInsertionPolling();
2967
+ }
3006
2968
  return [
3007
2969
  2
3008
2970
  ];
@@ -3010,78 +2972,11 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3010
2972
  });
3011
2973
  }).call(_this);
3012
2974
  });
3013
- this.hls.on(Hls.Events.LEVEL_LOADED, function(_evt, data) {
2975
+ this.hls.on(Hls.Events.LEVEL_LOADED, function() {
3014
2976
  if (_this.inAdBreak || _this.pendingAdBreak) {
3015
2977
  return;
3016
2978
  }
3017
- var details = data === null || data === void 0 ? void 0 : data.details;
3018
- if (!details || !details.fragments || details.fragments.length === 0) {
3019
- return;
3020
- }
3021
- var fragmentsToScan = Math.min(5, details.fragments.length);
3022
- for(var i = 0; i < fragmentsToScan; i++){
3023
- var frag = details.fragments[i];
3024
- var tagList = frag === null || frag === void 0 ? void 0 : frag.tagList;
3025
- if (!Array.isArray(tagList)) continue;
3026
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3027
- try {
3028
- for(var _iterator = tagList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3029
- var entry = _step.value;
3030
- var tag = "";
3031
- var value = "";
3032
- if (Array.isArray(entry)) {
3033
- var _entry_, _entry_1;
3034
- tag = String((_entry_ = entry[0]) !== null && _entry_ !== void 0 ? _entry_ : "");
3035
- value = String((_entry_1 = entry[1]) !== null && _entry_1 !== void 0 ? _entry_1 : "");
3036
- } else if (typeof entry === "string") {
3037
- var idx = entry.indexOf(":");
3038
- if (idx >= 0) {
3039
- tag = entry.substring(0, idx);
3040
- value = entry.substring(idx + 1);
3041
- } else {
3042
- tag = entry;
3043
- }
3044
- }
3045
- if (!tag) continue;
3046
- if (tag.includes("EXT-X-CUE-OUT") || tag.includes("EXT-X-DATERANGE")) {
3047
- var attrs = tag.includes("EXT-X-DATERANGE") ? _this.parseAttributeList(value) : {};
3048
- var hasScteOut = tag.includes("EXT-X-CUE-OUT") || "SCTE35-OUT" in attrs || attrs["SCTE35-OUT"] !== void 0;
3049
- if (hasScteOut) {
3050
- var durationSeconds = _this.parseCueOutDuration(value);
3051
- var marker = _object_spread_props(_object_spread({
3052
- type: "start"
3053
- }, durationSeconds !== void 0 ? {
3054
- durationSeconds: durationSeconds
3055
- } : {}), {
3056
- raw: {
3057
- tag: tag,
3058
- value: value,
3059
- earlyDetection: true
3060
- }
3061
- });
3062
- if (_this.config.debugAdTiming) {
3063
- console.log("[StormcloudVideoPlayer] \uD83C\uDFAF EARLY SCTE-35 DETECTION: Ad break marker found in fragment", i, "- starting pre-fetch (NOT playing yet)");
3064
- }
3065
- _this.startAdPrefetch(marker, frag === null || frag === void 0 ? void 0 : frag.sn);
3066
- return;
3067
- }
3068
- }
3069
- }
3070
- } catch (err) {
3071
- _didIteratorError = true;
3072
- _iteratorError = err;
3073
- } finally{
3074
- try {
3075
- if (!_iteratorNormalCompletion && _iterator.return != null) {
3076
- _iterator.return();
3077
- }
3078
- } finally{
3079
- if (_didIteratorError) {
3080
- throw _iteratorError;
3081
- }
3082
- }
3083
- }
3084
- }
2979
+ _this.checkAdInsertionInManifest();
3085
2980
  });
3086
2981
  this.hls.on(Hls.Events.FRAG_BUFFERED, function(_evt, data) {
3087
2982
  return _async_to_generator(function() {
@@ -3143,111 +3038,55 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3143
3038
  });
3144
3039
  }).call(_this);
3145
3040
  });
3146
- this.hls.on(Hls.Events.FRAG_PARSING_METADATA, function(_evt, data) {
3147
- var id3Tags = ((data === null || data === void 0 ? void 0 : data.samples) || []).map(function(s) {
3148
- return {
3149
- key: "ID3",
3150
- value: s === null || s === void 0 ? void 0 : s.data,
3151
- ptsSeconds: s === null || s === void 0 ? void 0 : s.pts
3152
- };
3153
- });
3154
- id3Tags.forEach(function(tag) {
3155
- return _this.onId3Tag(tag);
3156
- });
3157
- });
3158
3041
  this.hls.on(Hls.Events.FRAG_CHANGED, function(_evt, data) {
3159
3042
  var frag = data === null || data === void 0 ? void 0 : data.frag;
3160
- var tagList = frag === null || frag === void 0 ? void 0 : frag.tagList;
3161
- if (!Array.isArray(tagList)) return;
3162
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3163
- try {
3164
- for(var _iterator = tagList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3165
- var entry = _step.value;
3166
- var tag = "";
3167
- var value = "";
3168
- if (Array.isArray(entry)) {
3169
- var _entry_, _entry_1;
3170
- tag = String((_entry_ = entry[0]) !== null && _entry_ !== void 0 ? _entry_ : "");
3171
- value = String((_entry_1 = entry[1]) !== null && _entry_1 !== void 0 ? _entry_1 : "");
3172
- } else if (typeof entry === "string") {
3173
- var idx = entry.indexOf(":");
3174
- if (idx >= 0) {
3175
- tag = entry.substring(0, idx);
3176
- value = entry.substring(idx + 1);
3177
- } else {
3178
- tag = entry;
3179
- value = "";
3180
- }
3043
+ if (!frag) return;
3044
+ if (_this.lastAdInsertionPoint && !_this.inAdBreak && _this.lastAdInsertionPoint.updated_at !== _this.processedAdInsertionUpdatedAt) {
3045
+ var segmentName = _this.lastAdInsertionPoint.segment_ts_name;
3046
+ if (_this.fragmentMatchesSegment(frag, segmentName)) {
3047
+ var _ref;
3048
+ _this.processedAdInsertionUpdatedAt = _this.lastAdInsertionPoint.updated_at;
3049
+ var offsetMs = (_this.lastAdInsertionPoint.offset_seconds || 0) * 1e3;
3050
+ _this.pushAdInsertionDebug("segment_playing", segmentName, {
3051
+ offsetSeconds: _this.lastAdInsertionPoint.offset_seconds,
3052
+ detail: "sn=".concat((_ref = frag === null || frag === void 0 ? void 0 : frag.sn) !== null && _ref !== void 0 ? _ref : "?")
3053
+ });
3054
+ if (_this.config.debugAdTiming) {
3055
+ console.log('[StormcloudVideoPlayer] Ad insertion segment "'.concat(segmentName, '" now playing — scheduling ad start in ').concat(offsetMs, "ms"));
3181
3056
  }
3182
- if (!tag) continue;
3183
- if (tag.includes("EXT-X-CUE-OUT-CONT")) {
3184
- var prog = _this.parseCueOutCont(value);
3185
- var marker = _object_spread_props(_object_spread({
3186
- type: "progress"
3187
- }, (prog === null || prog === void 0 ? void 0 : prog.duration) !== void 0 ? {
3188
- durationSeconds: prog.duration
3189
- } : {}, (prog === null || prog === void 0 ? void 0 : prog.elapsed) !== void 0 ? {
3190
- ptsSeconds: prog.elapsed
3191
- } : {}), {
3192
- raw: {
3193
- tag: tag,
3194
- value: value
3195
- }
3196
- });
3197
- _this.onScte35Marker(marker);
3198
- } else if (tag.includes("EXT-X-CUE-OUT")) {
3199
- var durationSeconds = _this.parseCueOutDuration(value);
3200
- var marker1 = _object_spread_props(_object_spread({
3201
- type: "start"
3202
- }, durationSeconds !== void 0 ? {
3203
- durationSeconds: durationSeconds
3204
- } : {}), {
3205
- raw: {
3206
- tag: tag,
3207
- value: value
3208
- }
3209
- });
3210
- _this.onScte35Marker(marker1);
3211
- } else if (tag.includes("EXT-X-CUE-IN")) {
3212
- _this.onScte35Marker({
3213
- type: "end",
3214
- raw: {
3215
- tag: tag,
3216
- value: value
3217
- }
3057
+ _this.pushAdInsertionDebug("ad_scheduled", segmentName, {
3058
+ offsetSeconds: _this.lastAdInsertionPoint.offset_seconds,
3059
+ detail: "in ".concat(offsetMs, "ms, dur=60s")
3060
+ });
3061
+ _this.clearAdInsertionOffsetTimer();
3062
+ _this.adInsertionOffsetTimerId = window.setTimeout(function() {
3063
+ _this.adInsertionOffsetTimerId = void 0;
3064
+ if (_this.inAdBreak) return;
3065
+ _this.pushAdInsertionDebug("ad_triggered", segmentName, {
3066
+ detail: "ad break started (60s)"
3218
3067
  });
3219
- } else if (tag.includes("EXT-X-DATERANGE")) {
3220
- var _attrs_CLASS;
3221
- var attrs = _this.parseAttributeList(value);
3222
- var hasScteOut = "SCTE35-OUT" in attrs || attrs["SCTE35-OUT"] !== void 0;
3223
- var hasScteIn = "SCTE35-IN" in attrs || attrs["SCTE35-IN"] !== void 0;
3224
- var klass = String((_attrs_CLASS = attrs["CLASS"]) !== null && _attrs_CLASS !== void 0 ? _attrs_CLASS : "");
3225
- var duration = _this.toNumber(attrs["DURATION"]);
3226
- if (hasScteOut || /com\.apple\.hls\.cue/i.test(klass)) {
3227
- var marker2 = _object_spread_props(_object_spread({
3228
- type: "start"
3229
- }, duration !== void 0 ? {
3230
- durationSeconds: duration
3231
- } : {}), {
3232
- raw: {
3233
- tag: tag,
3234
- value: value,
3235
- attrs: attrs
3236
- }
3237
- });
3238
- _this.onScte35Marker(marker2);
3239
- }
3240
- if (hasScteIn) {
3241
- _this.onScte35Marker({
3242
- type: "end",
3243
- raw: {
3244
- tag: tag,
3245
- value: value,
3246
- attrs: attrs
3247
- }
3248
- });
3249
- }
3250
- }
3068
+ void _this.handleAdStart(60);
3069
+ }, offsetMs);
3070
+ }
3071
+ }
3072
+ });
3073
+ this.hls.on(Hls.Events.FRAG_PARSING_USERDATA, function(_evt, data) {
3074
+ var _ref;
3075
+ var samples = (_ref = data === null || data === void 0 ? void 0 : data.samples) !== null && _ref !== void 0 ? _ref : [];
3076
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3077
+ try {
3078
+ for(var _iterator = samples[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3079
+ var sample = _step.value;
3080
+ var _ref1, _ref2;
3081
+ var _data_frag, _data_frag1;
3082
+ var bytes = sample === null || sample === void 0 ? void 0 : sample.data;
3083
+ if (!bytes || bytes.length < 5) continue;
3084
+ var isSCTE35 = bytes[0] === 252;
3085
+ if (!isSCTE35) continue;
3086
+ var segName = (_ref1 = (_ref2 = data === null || data === void 0 ? void 0 : (_data_frag = data.frag) === null || _data_frag === void 0 ? void 0 : _data_frag.relurl) !== null && _ref2 !== void 0 ? _ref2 : data === null || data === void 0 ? void 0 : (_data_frag1 = data.frag) === null || _data_frag1 === void 0 ? void 0 : _data_frag1.url) !== null && _ref1 !== void 0 ? _ref1 : "";
3087
+ _this.pushAdInsertionDebug("scte35_inserted", segName, {
3088
+ detail: "len=".concat(bytes.length, "B")
3089
+ });
3251
3090
  }
3252
3091
  } catch (err) {
3253
3092
  _didIteratorError = true;
@@ -3338,8 +3177,8 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3338
3177
  _this.activeAdRequestToken = null;
3339
3178
  _this.showAds = true;
3340
3179
  if (_this.config.disableFiller) {
3341
- if (_this.savedMutedStateBeforeScte == null) {
3342
- _this.savedMutedStateBeforeScte = {
3180
+ if (_this.savedMutedStateBeforeAd == null) {
3181
+ _this.savedMutedStateBeforeAd = {
3343
3182
  muted: _this.video.muted,
3344
3183
  volume: _this.video.volume
3345
3184
  };
@@ -3367,10 +3206,10 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3367
3206
  });
3368
3207
  this.adLayer.on("content_resume", function() {
3369
3208
  var _ref, _ref1;
3370
- var _this_savedMutedStateBeforeScte, _this_savedMutedStateBeforeScte1;
3209
+ var _this_savedMutedStateBeforeAd, _this_savedMutedStateBeforeAd1;
3371
3210
  var remaining = _this.getRemainingAdMs();
3372
- var breakMuted = (_ref = (_this_savedMutedStateBeforeScte = _this.savedMutedStateBeforeScte) === null || _this_savedMutedStateBeforeScte === void 0 ? void 0 : _this_savedMutedStateBeforeScte.muted) !== null && _ref !== void 0 ? _ref : _this.adLayer.getOriginalMutedState();
3373
- var breakVolume = (_ref1 = (_this_savedMutedStateBeforeScte1 = _this.savedMutedStateBeforeScte) === null || _this_savedMutedStateBeforeScte1 === void 0 ? void 0 : _this_savedMutedStateBeforeScte1.volume) !== null && _ref1 !== void 0 ? _ref1 : _this.adLayer.getOriginalVolume();
3211
+ var breakMuted = (_ref = (_this_savedMutedStateBeforeAd = _this.savedMutedStateBeforeAd) === null || _this_savedMutedStateBeforeAd === void 0 ? void 0 : _this_savedMutedStateBeforeAd.muted) !== null && _ref !== void 0 ? _ref : _this.adLayer.getOriginalMutedState();
3212
+ var breakVolume = (_ref1 = (_this_savedMutedStateBeforeAd1 = _this.savedMutedStateBeforeAd) === null || _this_savedMutedStateBeforeAd1 === void 0 ? void 0 : _this_savedMutedStateBeforeAd1.volume) !== null && _ref1 !== void 0 ? _ref1 : _this.adLayer.getOriginalVolume();
3374
3213
  if (_this.config.debugAdTiming) {
3375
3214
  console.log("[StormcloudVideoPlayer] content_resume received, inAdBreak=%s, remaining=%s, preloadedTokens=%d, pendingNext=%s", _this.inAdBreak, remaining, _this.preloadedTokens.length, !!_this.pendingNextAdBids);
3376
3215
  }
@@ -3447,7 +3286,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3447
3286
  var remainingFinal = _this.getRemainingAdMs();
3448
3287
  if (_this.inAdBreak && remainingFinal > _this.MIN_AD_REMAINING_MS) {
3449
3288
  if (_this.config.debugAdTiming) {
3450
- console.log("[StormcloudVideoPlayer] content_resume: no more ads, showing filler for", remainingFinal, "ms");
3289
+ console.log("[StormcloudVideoPlayer] content_resume: ad ended/failed with time remaining, showing filler and continuing fetch for", remainingFinal, "ms");
3451
3290
  }
3452
3291
  if (!_this.config.disableFiller) {
3453
3292
  _this.showPlaceholderLayer();
@@ -3465,7 +3304,8 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3465
3304
  (_this_video_play = _this.video.play()) === null || _this_video_play === void 0 ? void 0 : _this_video_play.catch(function() {});
3466
3305
  }
3467
3306
  }
3468
- _this.stopContinuousFetching();
3307
+ _this.continuousFetchingActive = true;
3308
+ _this.startContinuousFetchLoop();
3469
3309
  return;
3470
3310
  }
3471
3311
  if (_this.config.debugAdTiming) {
@@ -3612,547 +3452,6 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3612
3452
  return !!(this.config.allowNativeHls && canNative);
3613
3453
  }
3614
3454
  },
3615
- {
3616
- key: "onId3Tag",
3617
- value: function onId3Tag(tag) {
3618
- if (typeof tag.ptsSeconds === "number") {
3619
- this.updatePtsDrift(tag.ptsSeconds);
3620
- }
3621
- var marker = this.parseScte35FromId3(tag);
3622
- if (marker) {
3623
- this.onScte35Marker(marker);
3624
- }
3625
- }
3626
- },
3627
- {
3628
- key: "parseScte35FromId3",
3629
- value: function parseScte35FromId3(tag) {
3630
- var text = this.decodeId3ValueToText(tag.value);
3631
- if (!text) return void 0;
3632
- var cueOutMatch = text.match(/EXT-X-CUE-OUT(?::([^\r\n]*))?/i) || text.match(/CUE-OUT(?::([^\r\n]*))?/i);
3633
- if (cueOutMatch) {
3634
- var _cueOutMatch_;
3635
- var arg = ((_cueOutMatch_ = cueOutMatch[1]) !== null && _cueOutMatch_ !== void 0 ? _cueOutMatch_ : "").trim();
3636
- var dur = this.parseCueOutDuration(arg);
3637
- var marker = _object_spread_props(_object_spread({
3638
- type: "start"
3639
- }, tag.ptsSeconds !== void 0 ? {
3640
- ptsSeconds: tag.ptsSeconds
3641
- } : {}, dur !== void 0 ? {
3642
- durationSeconds: dur
3643
- } : {}), {
3644
- raw: {
3645
- id3: text
3646
- }
3647
- });
3648
- return marker;
3649
- }
3650
- var cueOutContMatch = text.match(/EXT-X-CUE-OUT-CONT:([^\r\n]*)/i);
3651
- if (cueOutContMatch) {
3652
- var _cueOutContMatch_;
3653
- var arg1 = ((_cueOutContMatch_ = cueOutContMatch[1]) !== null && _cueOutContMatch_ !== void 0 ? _cueOutContMatch_ : "").trim();
3654
- var cont = this.parseCueOutCont(arg1);
3655
- var marker1 = _object_spread_props(_object_spread({
3656
- type: "progress"
3657
- }, tag.ptsSeconds !== void 0 ? {
3658
- ptsSeconds: tag.ptsSeconds
3659
- } : {}, (cont === null || cont === void 0 ? void 0 : cont.duration) !== void 0 ? {
3660
- durationSeconds: cont.duration
3661
- } : {}), {
3662
- raw: {
3663
- id3: text
3664
- }
3665
- });
3666
- return marker1;
3667
- }
3668
- var cueInMatch = text.match(/EXT-X-CUE-IN\b/i) || text.match(/CUE-IN\b/i);
3669
- if (cueInMatch) {
3670
- var marker2 = _object_spread_props(_object_spread({
3671
- type: "end"
3672
- }, tag.ptsSeconds !== void 0 ? {
3673
- ptsSeconds: tag.ptsSeconds
3674
- } : {}), {
3675
- raw: {
3676
- id3: text
3677
- }
3678
- });
3679
- return marker2;
3680
- }
3681
- var daterangeMatch = text.match(/EXT-X-DATERANGE:([^\r\n]*)/i);
3682
- if (daterangeMatch) {
3683
- var _daterangeMatch_, _attrs_CLASS;
3684
- var attrs = this.parseAttributeList((_daterangeMatch_ = daterangeMatch[1]) !== null && _daterangeMatch_ !== void 0 ? _daterangeMatch_ : "");
3685
- var hasScteOut = "SCTE35-OUT" in attrs || attrs["SCTE35-OUT"] !== void 0;
3686
- var hasScteIn = "SCTE35-IN" in attrs || attrs["SCTE35-IN"] !== void 0;
3687
- var klass = String((_attrs_CLASS = attrs["CLASS"]) !== null && _attrs_CLASS !== void 0 ? _attrs_CLASS : "");
3688
- var duration = this.toNumber(attrs["DURATION"]);
3689
- if (hasScteOut || /com\.apple\.hls\.cue/i.test(klass)) {
3690
- var marker3 = _object_spread_props(_object_spread({
3691
- type: "start"
3692
- }, tag.ptsSeconds !== void 0 ? {
3693
- ptsSeconds: tag.ptsSeconds
3694
- } : {}, duration !== void 0 ? {
3695
- durationSeconds: duration
3696
- } : {}), {
3697
- raw: {
3698
- id3: text,
3699
- attrs: attrs
3700
- }
3701
- });
3702
- return marker3;
3703
- }
3704
- if (hasScteIn) {
3705
- var marker4 = _object_spread_props(_object_spread({
3706
- type: "end"
3707
- }, tag.ptsSeconds !== void 0 ? {
3708
- ptsSeconds: tag.ptsSeconds
3709
- } : {}), {
3710
- raw: {
3711
- id3: text,
3712
- attrs: attrs
3713
- }
3714
- });
3715
- return marker4;
3716
- }
3717
- }
3718
- if (/SCTE35-OUT/i.test(text)) {
3719
- var marker5 = _object_spread_props(_object_spread({
3720
- type: "start"
3721
- }, tag.ptsSeconds !== void 0 ? {
3722
- ptsSeconds: tag.ptsSeconds
3723
- } : {}), {
3724
- raw: {
3725
- id3: text
3726
- }
3727
- });
3728
- return marker5;
3729
- }
3730
- if (/SCTE35-IN/i.test(text)) {
3731
- var marker6 = _object_spread_props(_object_spread({
3732
- type: "end"
3733
- }, tag.ptsSeconds !== void 0 ? {
3734
- ptsSeconds: tag.ptsSeconds
3735
- } : {}), {
3736
- raw: {
3737
- id3: text
3738
- }
3739
- });
3740
- return marker6;
3741
- }
3742
- if (_instanceof(tag.value, Uint8Array)) {
3743
- var bin = this.parseScte35Binary(tag.value);
3744
- if (bin) return bin;
3745
- }
3746
- return void 0;
3747
- }
3748
- },
3749
- {
3750
- key: "decodeId3ValueToText",
3751
- value: function decodeId3ValueToText(value) {
3752
- try {
3753
- if (typeof value === "string") return value;
3754
- var decoder = new TextDecoder("utf-8", {
3755
- fatal: false
3756
- });
3757
- var text = decoder.decode(value);
3758
- if (text && /[\x20-\x7E]/.test(text)) return text;
3759
- var out = "";
3760
- for(var i = 0; i < value.length; i++)out += String.fromCharCode(value[i]);
3761
- return out;
3762
- } catch (unused) {
3763
- return void 0;
3764
- }
3765
- }
3766
- },
3767
- {
3768
- key: "onScte35Marker",
3769
- value: function onScte35Marker(marker) {
3770
- var _this = this;
3771
- if (this.config.disableAds) return;
3772
- this.pushScteMarker(marker);
3773
- this.pushDebugLog("info", "scte35", "SCTE-35 marker detected", {
3774
- type: marker.type,
3775
- ptsSeconds: marker.ptsSeconds,
3776
- durationSeconds: marker.durationSeconds,
3777
- currentTime: this.video.currentTime
3778
- });
3779
- if (this.config.debugAdTiming) {
3780
- console.log("[StormcloudVideoPlayer] SCTE-35 marker detected:", {
3781
- type: marker.type,
3782
- ptsSeconds: marker.ptsSeconds,
3783
- durationSeconds: marker.durationSeconds,
3784
- currentTime: this.video.currentTime,
3785
- raw: marker.raw,
3786
- hasPendingAdBreak: !!this.pendingAdBreak
3787
- });
3788
- }
3789
- if (marker.type === "start") {
3790
- var _this_config_immediateManifestAds;
3791
- var _this_pendingAdBreak;
3792
- if (this.savedMutedStateBeforeScte == null) {
3793
- this.savedMutedStateBeforeScte = {
3794
- muted: this.video.muted,
3795
- volume: this.video.volume
3796
- };
3797
- this.adLayer.updateOriginalMutedState(this.video.muted, this.video.volume);
3798
- }
3799
- if (!this.config.disableFiller && !this.video.muted && !this.adLayer.isAdPlaying()) {
3800
- this.video.muted = true;
3801
- this.video.volume = 0;
3802
- if (this.config.debugAdTiming) {
3803
- console.log("[StormcloudVideoPlayer] Muted video on SCTE start marker");
3804
- }
3805
- }
3806
- if (this.inAdBreak) {
3807
- if (marker.durationSeconds != null) {
3808
- var newDurationMs = marker.durationSeconds * 1e3;
3809
- if (this.expectedAdBreakDurationMs == null || newDurationMs > this.expectedAdBreakDurationMs) {
3810
- this.expectedAdBreakDurationMs = newDurationMs;
3811
- var elapsedMs = this.currentAdBreakStartWallClockMs != null ? Date.now() - this.currentAdBreakStartWallClockMs : 0;
3812
- var remainingMs = Math.max(0, newDurationMs - elapsedMs);
3813
- this.scheduleAdStopCountdown(remainingMs);
3814
- if (this.config.debugAdTiming) {
3815
- console.log("[StormcloudVideoPlayer] Updated ad break duration from subsequent marker: ".concat(newDurationMs, "ms, remaining: ").concat(remainingMs, "ms"));
3816
- }
3817
- }
3818
- }
3819
- return;
3820
- }
3821
- this.inAdBreak = true;
3822
- var durationMs = marker.durationSeconds != null ? marker.durationSeconds * 1e3 : ((_this_pendingAdBreak = this.pendingAdBreak) === null || _this_pendingAdBreak === void 0 ? void 0 : _this_pendingAdBreak.marker.durationSeconds) != null ? this.pendingAdBreak.marker.durationSeconds * 1e3 : void 0;
3823
- this.expectedAdBreakDurationMs = durationMs;
3824
- this.currentAdBreakStartWallClockMs = Date.now();
3825
- if (this.config.licenseKey) {
3826
- var _this_pendingAdBreak1;
3827
- var adDetectInfo = _object_spread({
3828
- source: "scte35",
3829
- timestamp: /* @__PURE__ */ new Date().toISOString()
3830
- }, marker.durationSeconds != null && {
3831
- durationSeconds: marker.durationSeconds
3832
- }, marker.ptsSeconds != null && {
3833
- ptsSeconds: marker.ptsSeconds
3834
- }, ((_this_pendingAdBreak1 = this.pendingAdBreak) === null || _this_pendingAdBreak1 === void 0 ? void 0 : _this_pendingAdBreak1.detectedAtFragmentSn) != null && {
3835
- detectedAtFragmentSn: this.pendingAdBreak.detectedAtFragmentSn
3836
- });
3837
- sendAdDetectTracking(this.config.licenseKey, adDetectInfo);
3838
- }
3839
- var isManifestMarker = this.isManifestBasedMarker(marker);
3840
- var forceImmediate = (_this_config_immediateManifestAds = this.config.immediateManifestAds) !== null && _this_config_immediateManifestAds !== void 0 ? _this_config_immediateManifestAds : true;
3841
- if (this.config.debugAdTiming) {
3842
- console.log("[StormcloudVideoPlayer] Ad start decision:", {
3843
- isManifestMarker: isManifestMarker,
3844
- forceImmediate: forceImmediate,
3845
- hasPts: typeof marker.ptsSeconds === "number"
3846
- });
3847
- }
3848
- if (isManifestMarker && forceImmediate) {
3849
- if (this.config.debugAdTiming) {
3850
- console.log("[StormcloudVideoPlayer] Starting ad immediately (manifest-based)");
3851
- }
3852
- this.clearAdStartTimer();
3853
- this.handleAdStart(marker);
3854
- } else if (typeof marker.ptsSeconds === "number") {
3855
- var _this_config_driftToleranceMs;
3856
- var tol = (_this_config_driftToleranceMs = this.config.driftToleranceMs) !== null && _this_config_driftToleranceMs !== void 0 ? _this_config_driftToleranceMs : 1e3;
3857
- var nowMs = this.video.currentTime * 1e3;
3858
- var estCurrentPtsMs = nowMs - this.ptsDriftEmaMs;
3859
- var deltaMs = Math.floor(marker.ptsSeconds * 1e3 - estCurrentPtsMs);
3860
- if (this.config.debugAdTiming) {
3861
- console.log("[StormcloudVideoPlayer] PTS-based timing calculation:", {
3862
- nowMs: nowMs,
3863
- estCurrentPtsMs: estCurrentPtsMs,
3864
- markerPtsMs: marker.ptsSeconds * 1e3,
3865
- deltaMs: deltaMs,
3866
- tolerance: tol
3867
- });
3868
- }
3869
- if (deltaMs > tol) {
3870
- if (this.config.debugAdTiming) {
3871
- console.log("[StormcloudVideoPlayer] Scheduling ad start in ".concat(deltaMs, "ms"));
3872
- }
3873
- this.scheduleAdStartIn(deltaMs);
3874
- } else {
3875
- if (this.config.debugAdTiming) {
3876
- console.log("[StormcloudVideoPlayer] Starting ad immediately (within tolerance)");
3877
- }
3878
- this.clearAdStartTimer();
3879
- this.handleAdStart(marker);
3880
- }
3881
- } else {
3882
- if (this.config.debugAdTiming) {
3883
- console.log("[StormcloudVideoPlayer] Starting ad immediately (fallback)");
3884
- }
3885
- this.clearAdStartTimer();
3886
- this.handleAdStart(marker);
3887
- }
3888
- if (this.expectedAdBreakDurationMs != null) {
3889
- this.scheduleAdStopCountdown(this.expectedAdBreakDurationMs);
3890
- }
3891
- return;
3892
- }
3893
- if (marker.type === "progress" && this.inAdBreak) {
3894
- if (marker.durationSeconds != null) {
3895
- this.expectedAdBreakDurationMs = marker.durationSeconds * 1e3;
3896
- }
3897
- if (this.expectedAdBreakDurationMs != null && this.currentAdBreakStartWallClockMs != null) {
3898
- var elapsedMs1 = Date.now() - this.currentAdBreakStartWallClockMs;
3899
- var remainingMs1 = Math.max(0, this.expectedAdBreakDurationMs - elapsedMs1);
3900
- this.scheduleAdStopCountdown(remainingMs1);
3901
- }
3902
- if (!this.adLayer.isAdPlaying() && this.pendingNextAdBids != null && this.pendingNextAdBids.length > 0) {
3903
- var bids = this.pendingNextAdBids;
3904
- this.pendingNextAdBids = null;
3905
- this.currentAdIndex++;
3906
- this.adLayer.playAd(bids).catch(function() {
3907
- return _this.handleAdFailure();
3908
- });
3909
- }
3910
- return;
3911
- }
3912
- if (marker.type === "end") {
3913
- var remaining = this.getRemainingAdMs();
3914
- var adPlaying = this.adLayer.isAdPlaying();
3915
- var hasQueuedAds = this.pendingNextAdBids != null && this.pendingNextAdBids.length > 0;
3916
- if (this.config.debugAdTiming) {
3917
- console.log("[StormcloudVideoPlayer] SCTE-35 end marker received:", {
3918
- inAdBreak: this.inAdBreak,
3919
- remaining: remaining,
3920
- adPlaying: adPlaying,
3921
- hasQueuedAds: hasQueuedAds,
3922
- activeAdRequest: this.activeAdRequestToken !== null
3923
- });
3924
- }
3925
- if (!this.inAdBreak) {
3926
- if (this.config.debugAdTiming) {
3927
- console.log("[StormcloudVideoPlayer] Ignoring SCTE-35 end marker - not in ad break");
3928
- }
3929
- return;
3930
- }
3931
- if (adPlaying || remaining > 500) {
3932
- if (this.config.debugAdTiming) {
3933
- console.log("[StormcloudVideoPlayer] Ignoring premature SCTE-35 end marker - ads still active or time remaining");
3934
- }
3935
- return;
3936
- }
3937
- this.inAdBreak = false;
3938
- this.expectedAdBreakDurationMs = void 0;
3939
- this.currentAdBreakStartWallClockMs = void 0;
3940
- this.clearAdStartTimer();
3941
- this.clearAdStopTimer();
3942
- if (adPlaying) {
3943
- this.adLayer.stop().catch(function() {});
3944
- }
3945
- this.handleAdPodComplete();
3946
- return;
3947
- }
3948
- }
3949
- },
3950
- {
3951
- key: "parseCueOutDuration",
3952
- value: function parseCueOutDuration(value) {
3953
- var num = parseFloat(value.trim());
3954
- if (!Number.isNaN(num)) return num;
3955
- var match = value.match(/(?:^|[,\s])DURATION\s*=\s*([0-9.]+)/i) || value.match(/Duration\s*=\s*([0-9.]+)/i);
3956
- if (match && match[1] != null) {
3957
- var dStr = match[1];
3958
- var d = parseFloat(dStr);
3959
- return Number.isNaN(d) ? void 0 : d;
3960
- }
3961
- return void 0;
3962
- }
3963
- },
3964
- {
3965
- key: "parseCueOutCont",
3966
- value: function parseCueOutCont(value) {
3967
- var res = {};
3968
- var elapsedMatch = value.match(/Elapsed\s*=\s*([0-9.]+)/i);
3969
- var durationMatch = value.match(/Duration\s*=\s*([0-9.]+)/i);
3970
- if (elapsedMatch && elapsedMatch[1] != null) {
3971
- var e = parseFloat(elapsedMatch[1]);
3972
- if (!Number.isNaN(e)) res.elapsed = e;
3973
- }
3974
- if (durationMatch && durationMatch[1] != null) {
3975
- var d = parseFloat(durationMatch[1]);
3976
- if (!Number.isNaN(d)) res.duration = d;
3977
- }
3978
- if (!("elapsed" in res) || !("duration" in res)) {
3979
- var slashMatch = value.match(/([0-9.]+)\s*\/\s*([0-9.]+)/);
3980
- if (slashMatch && slashMatch[1] && slashMatch[2]) {
3981
- var elapsed = parseFloat(slashMatch[1]);
3982
- var duration = parseFloat(slashMatch[2]);
3983
- if (!Number.isNaN(elapsed) && !("elapsed" in res)) res.elapsed = elapsed;
3984
- if (!Number.isNaN(duration) && !("duration" in res)) res.duration = duration;
3985
- }
3986
- }
3987
- if ("elapsed" in res || "duration" in res) return res;
3988
- return void 0;
3989
- }
3990
- },
3991
- {
3992
- key: "parseAttributeList",
3993
- value: function parseAttributeList(value) {
3994
- var attrs = {};
3995
- var regex = /([A-Z0-9-]+)=(("[^"]*")|([^",]*))(?:,|$)/gi;
3996
- var match;
3997
- while((match = regex.exec(value)) !== null){
3998
- var _match_, _ref, _match_1;
3999
- var key = (_match_ = match[1]) !== null && _match_ !== void 0 ? _match_ : "";
4000
- var rawVal = (_ref = (_match_1 = match[3]) !== null && _match_1 !== void 0 ? _match_1 : match[4]) !== null && _ref !== void 0 ? _ref : "";
4001
- if (rawVal.startsWith('"') && rawVal.endsWith('"')) {
4002
- rawVal = rawVal.slice(1, -1);
4003
- }
4004
- if (key) {
4005
- attrs[key] = rawVal;
4006
- }
4007
- }
4008
- return attrs;
4009
- }
4010
- },
4011
- {
4012
- key: "toNumber",
4013
- value: function toNumber(val) {
4014
- if (val == null) return void 0;
4015
- var n = typeof val === "string" ? parseFloat(val) : Number(val);
4016
- return Number.isNaN(n) ? void 0 : n;
4017
- }
4018
- },
4019
- {
4020
- key: "isManifestBasedMarker",
4021
- value: function isManifestBasedMarker(marker) {
4022
- var raw = marker.raw;
4023
- if (!raw) return false;
4024
- if (raw.tag) {
4025
- var tag = String(raw.tag);
4026
- return tag.includes("EXT-X-CUE-OUT") || tag.includes("EXT-X-CUE-IN") || tag.includes("EXT-X-DATERANGE");
4027
- }
4028
- if (raw.id3) return false;
4029
- if (raw.splice_command_type) return false;
4030
- return false;
4031
- }
4032
- },
4033
- {
4034
- key: "parseScte35Binary",
4035
- value: function parseScte35Binary(data) {
4036
- var BitReader = /*#__PURE__*/ function() {
4037
- function BitReader(buf) {
4038
- _class_call_check(this, BitReader);
4039
- this.buf = buf;
4040
- this.bytePos = 0;
4041
- this.bitPos = 0;
4042
- }
4043
- _create_class(BitReader, [
4044
- {
4045
- key: "readBits",
4046
- value: function readBits(numBits) {
4047
- var result = 0;
4048
- while(numBits > 0){
4049
- if (this.bytePos >= this.buf.length) return result;
4050
- var remainingInByte = 8 - this.bitPos;
4051
- var toRead = Math.min(numBits, remainingInByte);
4052
- var currentByte = this.buf[this.bytePos];
4053
- var shift = remainingInByte - toRead;
4054
- var mask = (1 << toRead) - 1 & 255;
4055
- var bits = currentByte >> shift & mask;
4056
- result = result << toRead | bits;
4057
- this.bitPos += toRead;
4058
- if (this.bitPos >= 8) {
4059
- this.bitPos = 0;
4060
- this.bytePos += 1;
4061
- }
4062
- numBits -= toRead;
4063
- }
4064
- return result >>> 0;
4065
- }
4066
- },
4067
- {
4068
- key: "skipBits",
4069
- value: function skipBits(n) {
4070
- this.readBits(n);
4071
- }
4072
- }
4073
- ]);
4074
- return BitReader;
4075
- }();
4076
- var r = new BitReader(data);
4077
- var tableId = r.readBits(8);
4078
- if (tableId !== 252) return void 0;
4079
- r.readBits(1);
4080
- r.readBits(1);
4081
- r.readBits(2);
4082
- var sectionLength = r.readBits(12);
4083
- r.readBits(8);
4084
- r.readBits(1);
4085
- r.readBits(6);
4086
- var ptsAdjHigh = r.readBits(1);
4087
- var ptsAdjLow = r.readBits(32);
4088
- void ptsAdjHigh;
4089
- void ptsAdjLow;
4090
- r.readBits(8);
4091
- r.readBits(12);
4092
- var spliceCommandLength = r.readBits(12);
4093
- var spliceCommandType = r.readBits(8);
4094
- if (spliceCommandType !== 5) {
4095
- return void 0;
4096
- }
4097
- r.readBits(32);
4098
- var cancel = r.readBits(1) === 1;
4099
- r.readBits(7);
4100
- if (cancel) return void 0;
4101
- var outOfNetwork = r.readBits(1) === 1;
4102
- var programSpliceFlag = r.readBits(1) === 1;
4103
- var durationFlag = r.readBits(1) === 1;
4104
- var spliceImmediateFlag = r.readBits(1) === 1;
4105
- r.readBits(4);
4106
- if (programSpliceFlag && !spliceImmediateFlag) {
4107
- var timeSpecifiedFlag = r.readBits(1) === 1;
4108
- if (timeSpecifiedFlag) {
4109
- r.readBits(6);
4110
- r.readBits(33);
4111
- } else {
4112
- r.readBits(7);
4113
- }
4114
- } else if (!programSpliceFlag) {
4115
- var componentCount = r.readBits(8);
4116
- for(var i = 0; i < componentCount; i++){
4117
- r.readBits(8);
4118
- if (!spliceImmediateFlag) {
4119
- var timeSpecifiedFlag1 = r.readBits(1) === 1;
4120
- if (timeSpecifiedFlag1) {
4121
- r.readBits(6);
4122
- r.readBits(33);
4123
- } else {
4124
- r.readBits(7);
4125
- }
4126
- }
4127
- }
4128
- }
4129
- var durationSeconds = void 0;
4130
- if (durationFlag) {
4131
- r.readBits(6);
4132
- r.readBits(1);
4133
- var high = r.readBits(1);
4134
- var low = r.readBits(32);
4135
- var durationTicks = high * 4294967296 + low;
4136
- durationSeconds = durationTicks / 9e4;
4137
- }
4138
- r.readBits(16);
4139
- r.readBits(8);
4140
- r.readBits(8);
4141
- if (outOfNetwork) {
4142
- var marker = _object_spread_props(_object_spread({
4143
- type: "start"
4144
- }, durationSeconds !== void 0 ? {
4145
- durationSeconds: durationSeconds
4146
- } : {}), {
4147
- raw: {
4148
- splice_command_type: 5
4149
- }
4150
- });
4151
- return marker;
4152
- }
4153
- return void 0;
4154
- }
4155
- },
4156
3455
  {
4157
3456
  key: "initializeTracking",
4158
3457
  value: function initializeTracking() {
@@ -4274,35 +3573,35 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4274
3573
  },
4275
3574
  {
4276
3575
  key: "startAdPrefetch",
4277
- value: function startAdPrefetch(marker, fragmentSn) {
3576
+ value: function startAdPrefetch(durationSeconds, fragmentSn) {
4278
3577
  if (this.config.disableAds) return;
4279
3578
  if (this.pendingAdBreak || this.inAdBreak) {
4280
3579
  return;
4281
3580
  }
4282
- this.pendingAdBreak = _object_spread_props(_object_spread({
4283
- marker: marker
4284
- }, fragmentSn !== void 0 ? {
3581
+ this.pendingAdBreak = _object_spread_props(_object_spread({}, durationSeconds !== void 0 ? {
3582
+ durationSeconds: durationSeconds
3583
+ } : {}, fragmentSn !== void 0 ? {
4285
3584
  detectedAtFragmentSn: fragmentSn
4286
3585
  } : {}), {
4287
3586
  isFetching: false,
4288
3587
  fetchStartTime: Date.now()
4289
3588
  });
4290
- void this.runAdPrefetch(marker);
3589
+ void this.runAdPrefetch(durationSeconds);
4291
3590
  if (this.config.debugAdTiming) {
4292
- console.log("[PREFETCH] Ad break marker registered, multi-ad prefetch started");
3591
+ console.log("[PREFETCH] Ad break registered, multi-ad prefetch started");
4293
3592
  }
4294
3593
  }
4295
3594
  },
4296
3595
  {
4297
3596
  key: "runAdPrefetch",
4298
- value: function runAdPrefetch(marker) {
3597
+ value: function runAdPrefetch(durationSeconds) {
4299
3598
  return _async_to_generator(function() {
4300
- var _this, _marker_durationSeconds, _ref, _firstBids_, durSec, context, firstBids, unused, adDurationSec, estimatedCount, firstToken, remaining, results, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, result, token;
3599
+ var _this, _ref, _firstBids_, durSec, context, firstBids, unused, adDurationSec, estimatedCount, firstToken, unused1, remaining, results, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, result, token, unused2, err;
4301
3600
  return _ts_generator(this, function(_state) {
4302
3601
  switch(_state.label){
4303
3602
  case 0:
4304
3603
  _this = this;
4305
- durSec = (_marker_durationSeconds = marker.durationSeconds) !== null && _marker_durationSeconds !== void 0 ? _marker_durationSeconds : 60;
3604
+ durSec = durationSeconds !== null && durationSeconds !== void 0 ? durationSeconds : 60;
4306
3605
  context = {
4307
3606
  breakDurationSec: durSec,
4308
3607
  remainingBreakSec: durSec
@@ -4352,14 +3651,43 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4352
3651
  console.log("[PREFETCH] Ad duration=".concat(adDurationSec, "s, break=").concat(durSec, "s → ").concat(estimatedCount, " ad(s) needed"));
4353
3652
  }
4354
3653
  firstToken = "preload_".concat(Date.now(), "_").concat(Math.random().toString(36).slice(2, 7));
4355
- this.preloadedTokens.push(firstToken);
4356
- void this.adLayer.preloadAd(firstBids, firstToken);
3654
+ _state.label = 5;
3655
+ case 5:
3656
+ _state.trys.push([
3657
+ 5,
3658
+ 7,
3659
+ ,
3660
+ 8
3661
+ ]);
3662
+ return [
3663
+ 4,
3664
+ this.adLayer.preloadAd(firstBids, firstToken)
3665
+ ];
3666
+ case 6:
3667
+ _state.sent();
3668
+ if (!this.inAdBreak) {
3669
+ this.preloadedTokens.push(firstToken);
3670
+ if (this.config.debugAdTiming) {
3671
+ console.log("[PREFETCH] First ad preloaded and queued, token=".concat(firstToken));
3672
+ }
3673
+ }
3674
+ return [
3675
+ 3,
3676
+ 8
3677
+ ];
3678
+ case 7:
3679
+ unused1 = _state.sent();
4357
3680
  if (this.config.debugAdTiming) {
4358
- console.log("[PREFETCH] First ad preloading, token=".concat(firstToken));
3681
+ console.warn("[PREFETCH] First ad preload failed, token=".concat(firstToken));
4359
3682
  }
3683
+ return [
3684
+ 3,
3685
+ 8
3686
+ ];
3687
+ case 8:
4360
3688
  if (!(estimatedCount > 1)) return [
4361
3689
  3,
4362
- 6
3690
+ 19
4363
3691
  ];
4364
3692
  remaining = Array.from({
4365
3693
  length: estimatedCount - 1
@@ -4381,38 +3709,100 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4381
3709
  4,
4382
3710
  Promise.all(remaining)
4383
3711
  ];
4384
- case 5:
3712
+ case 9:
4385
3713
  results = _state.sent();
4386
3714
  _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3715
+ _state.label = 10;
3716
+ case 10:
3717
+ _state.trys.push([
3718
+ 10,
3719
+ 17,
3720
+ 18,
3721
+ 19
3722
+ ]);
3723
+ _iterator = results[Symbol.iterator]();
3724
+ _state.label = 11;
3725
+ case 11:
3726
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
3727
+ 3,
3728
+ 16
3729
+ ];
3730
+ result = _step.value;
3731
+ if (this.inAdBreak) return [
3732
+ 3,
3733
+ 16
3734
+ ];
3735
+ if (!(result.ok && result.value.length > 0)) return [
3736
+ 3,
3737
+ 15
3738
+ ];
3739
+ token = "preload_".concat(Date.now(), "_").concat(Math.random().toString(36).slice(2, 7));
3740
+ _state.label = 12;
3741
+ case 12:
3742
+ _state.trys.push([
3743
+ 12,
3744
+ 14,
3745
+ ,
3746
+ 15
3747
+ ]);
3748
+ return [
3749
+ 4,
3750
+ this.adLayer.preloadAd(result.value, token)
3751
+ ];
3752
+ case 13:
3753
+ _state.sent();
3754
+ if (!this.inAdBreak) {
3755
+ this.preloadedTokens.push(token);
3756
+ if (this.config.debugAdTiming) {
3757
+ console.log("[PREFETCH] Additional ad preloaded and queued, token=".concat(token));
3758
+ }
3759
+ }
3760
+ return [
3761
+ 3,
3762
+ 15
3763
+ ];
3764
+ case 14:
3765
+ unused2 = _state.sent();
3766
+ if (this.config.debugAdTiming) {
3767
+ console.warn("[PREFETCH] Additional ad preload failed, token=".concat(token));
3768
+ }
3769
+ return [
3770
+ 3,
3771
+ 15
3772
+ ];
3773
+ case 15:
3774
+ _iteratorNormalCompletion = true;
3775
+ return [
3776
+ 3,
3777
+ 11
3778
+ ];
3779
+ case 16:
3780
+ return [
3781
+ 3,
3782
+ 19
3783
+ ];
3784
+ case 17:
3785
+ err = _state.sent();
3786
+ _didIteratorError = true;
3787
+ _iteratorError = err;
3788
+ return [
3789
+ 3,
3790
+ 19
3791
+ ];
3792
+ case 18:
4387
3793
  try {
4388
- for(_iterator = results[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
4389
- result = _step.value;
4390
- if (this.inAdBreak) break;
4391
- if (result.ok && result.value.length > 0) {
4392
- token = "preload_".concat(Date.now(), "_").concat(Math.random().toString(36).slice(2, 7));
4393
- this.preloadedTokens.push(token);
4394
- void this.adLayer.preloadAd(result.value, token);
4395
- if (this.config.debugAdTiming) {
4396
- console.log("[PREFETCH] Additional ad preloading, token=".concat(token));
4397
- }
4398
- }
3794
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
3795
+ _iterator.return();
4399
3796
  }
4400
- } catch (err) {
4401
- _didIteratorError = true;
4402
- _iteratorError = err;
4403
3797
  } finally{
4404
- try {
4405
- if (!_iteratorNormalCompletion && _iterator.return != null) {
4406
- _iterator.return();
4407
- }
4408
- } finally{
4409
- if (_didIteratorError) {
4410
- throw _iteratorError;
4411
- }
3798
+ if (_didIteratorError) {
3799
+ throw _iteratorError;
4412
3800
  }
4413
3801
  }
4414
- _state.label = 6;
4415
- case 6:
3802
+ return [
3803
+ 7
3804
+ ];
3805
+ case 19:
4416
3806
  if (this.config.debugAdTiming) {
4417
3807
  console.log("[PREFETCH] Pre-fetch complete: ".concat(this.preloadedTokens.length, " ad(s) queued"));
4418
3808
  }
@@ -4460,6 +3850,177 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4460
3850
  this.preloadedTokens = [];
4461
3851
  }
4462
3852
  },
3853
+ {
3854
+ key: "startAdInsertionPolling",
3855
+ value: function startAdInsertionPolling() {
3856
+ var _this = this;
3857
+ if (this.adInsertionPollingId != null) return;
3858
+ this.fetchAdInsertionPoint();
3859
+ this.adInsertionPollingId = window.setInterval(function() {
3860
+ _this.fetchAdInsertionPoint();
3861
+ }, 1e3);
3862
+ }
3863
+ },
3864
+ {
3865
+ key: "stopAdInsertionPolling",
3866
+ value: function stopAdInsertionPolling() {
3867
+ if (this.adInsertionPollingId != null) {
3868
+ clearInterval(this.adInsertionPollingId);
3869
+ this.adInsertionPollingId = void 0;
3870
+ }
3871
+ }
3872
+ },
3873
+ {
3874
+ key: "fetchAdInsertionPoint",
3875
+ value: function fetchAdInsertionPoint() {
3876
+ return _async_to_generator(function() {
3877
+ var _this_lastAdInsertionPoint, resp, data, isNew, unused;
3878
+ return _ts_generator(this, function(_state) {
3879
+ switch(_state.label){
3880
+ case 0:
3881
+ if (!this.config.projectId) return [
3882
+ 2
3883
+ ];
3884
+ _state.label = 1;
3885
+ case 1:
3886
+ _state.trys.push([
3887
+ 1,
3888
+ 4,
3889
+ ,
3890
+ 5
3891
+ ]);
3892
+ return [
3893
+ 4,
3894
+ fetch("https://adstorm.co/api-adstorm-dev/adstorm/swirl/projects/".concat(encodeURIComponent(this.config.projectId), "/ad-insertion-point"))
3895
+ ];
3896
+ case 2:
3897
+ resp = _state.sent();
3898
+ if (!resp.ok) return [
3899
+ 2
3900
+ ];
3901
+ return [
3902
+ 4,
3903
+ resp.json()
3904
+ ];
3905
+ case 3:
3906
+ data = _state.sent();
3907
+ isNew = data.updated_at !== ((_this_lastAdInsertionPoint = this.lastAdInsertionPoint) === null || _this_lastAdInsertionPoint === void 0 ? void 0 : _this_lastAdInsertionPoint.updated_at);
3908
+ this.lastAdInsertionPoint = data;
3909
+ if (isNew) {
3910
+ this.pushAdInsertionDebug("api_response", data.segment_ts_name, {
3911
+ offsetSeconds: data.offset_seconds,
3912
+ updatedAt: data.updated_at,
3913
+ detail: "project=".concat(data.project_id)
3914
+ });
3915
+ }
3916
+ if (this.config.debugAdTiming) {
3917
+ console.log("[StormcloudVideoPlayer] Ad insertion point API response:", data);
3918
+ }
3919
+ return [
3920
+ 3,
3921
+ 5
3922
+ ];
3923
+ case 4:
3924
+ unused = _state.sent();
3925
+ if (this.config.debugAdTiming) {
3926
+ console.warn("[StormcloudVideoPlayer] Ad insertion point API fetch failed");
3927
+ }
3928
+ return [
3929
+ 3,
3930
+ 5
3931
+ ];
3932
+ case 5:
3933
+ return [
3934
+ 2
3935
+ ];
3936
+ }
3937
+ });
3938
+ }).call(this);
3939
+ }
3940
+ },
3941
+ {
3942
+ key: "checkAdInsertionInManifest",
3943
+ value: function checkAdInsertionInManifest() {
3944
+ var _this_hls;
3945
+ if (!this.lastAdInsertionPoint) return;
3946
+ if (this.inAdBreak || this.pendingAdBreak) return;
3947
+ if (this.lastAdInsertionPoint.updated_at === this.processedAdInsertionUpdatedAt) return;
3948
+ var segmentName = this.lastAdInsertionPoint.segment_ts_name;
3949
+ var levels = (_this_hls = this.hls) === null || _this_hls === void 0 ? void 0 : _this_hls.levels;
3950
+ if (!levels) return;
3951
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3952
+ try {
3953
+ for(var _iterator = levels[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3954
+ var level = _step.value;
3955
+ var _level_details;
3956
+ var fragments = (_level_details = level.details) === null || _level_details === void 0 ? void 0 : _level_details.fragments;
3957
+ if (!Array.isArray(fragments)) continue;
3958
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
3959
+ try {
3960
+ for(var _iterator1 = fragments[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
3961
+ var frag = _step1.value;
3962
+ if (this.fragmentMatchesSegment(frag, segmentName)) {
3963
+ var _ref;
3964
+ this.pushAdInsertionDebug("segment_found", segmentName, {
3965
+ detail: "sn=".concat((_ref = frag === null || frag === void 0 ? void 0 : frag.sn) !== null && _ref !== void 0 ? _ref : "?")
3966
+ });
3967
+ if (this.config.debugAdTiming) {
3968
+ console.log('[StormcloudVideoPlayer] Ad insertion segment "'.concat(segmentName, '" found in manifest — starting pre-fetch'));
3969
+ }
3970
+ this.startAdPrefetch(60, frag === null || frag === void 0 ? void 0 : frag.sn);
3971
+ return;
3972
+ }
3973
+ }
3974
+ } catch (err) {
3975
+ _didIteratorError1 = true;
3976
+ _iteratorError1 = err;
3977
+ } finally{
3978
+ try {
3979
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
3980
+ _iterator1.return();
3981
+ }
3982
+ } finally{
3983
+ if (_didIteratorError1) {
3984
+ throw _iteratorError1;
3985
+ }
3986
+ }
3987
+ }
3988
+ }
3989
+ } catch (err) {
3990
+ _didIteratorError = true;
3991
+ _iteratorError = err;
3992
+ } finally{
3993
+ try {
3994
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
3995
+ _iterator.return();
3996
+ }
3997
+ } finally{
3998
+ if (_didIteratorError) {
3999
+ throw _iteratorError;
4000
+ }
4001
+ }
4002
+ }
4003
+ }
4004
+ },
4005
+ {
4006
+ key: "fragmentMatchesSegment",
4007
+ value: function fragmentMatchesSegment(frag, segmentName) {
4008
+ var _rawUrl_split_;
4009
+ var rawUrl = (frag === null || frag === void 0 ? void 0 : frag.url) || (frag === null || frag === void 0 ? void 0 : frag.relurl) || "";
4010
+ var url = (_rawUrl_split_ = rawUrl.split("?")[0]) !== null && _rawUrl_split_ !== void 0 ? _rawUrl_split_ : "";
4011
+ var name = segmentName.startsWith("/") ? segmentName : "/" + segmentName;
4012
+ return url.endsWith(name) || url.includes(name);
4013
+ }
4014
+ },
4015
+ {
4016
+ key: "clearAdInsertionOffsetTimer",
4017
+ value: function clearAdInsertionOffsetTimer() {
4018
+ if (this.adInsertionOffsetTimerId != null) {
4019
+ clearTimeout(this.adInsertionOffsetTimerId);
4020
+ this.adInsertionOffsetTimerId = void 0;
4021
+ }
4022
+ }
4023
+ },
4463
4024
  {
4464
4025
  key: "startContinuousFetchLoop",
4465
4026
  value: function startContinuousFetchLoop() {
@@ -4785,13 +4346,13 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4785
4346
  },
4786
4347
  {
4787
4348
  key: "handleAdStart",
4788
- value: function handleAdStart(_marker) {
4349
+ value: function handleAdStart(durationSeconds) {
4789
4350
  return _async_to_generator(function() {
4790
- var _this_savedMutedStateBeforeScte, adBreakDurationMs, mode, state, adBreakToken, adVolume, token, remaining, err;
4351
+ var _this_savedMutedStateBeforeAd, adBreakDurationMs, mode, state, adBreakToken, adVolume, token, remaining, err;
4791
4352
  return _ts_generator(this, function(_state) {
4792
4353
  switch(_state.label){
4793
4354
  case 0:
4794
- adBreakDurationMs = _marker.durationSeconds != null ? _marker.durationSeconds * 1e3 : void 0;
4355
+ adBreakDurationMs = durationSeconds != null ? durationSeconds * 1e3 : void 0;
4795
4356
  if (this.config.debugAdTiming) {
4796
4357
  mode = this.isLiveStream ? "LIVE" : "VOD";
4797
4358
  console.log("[CONTINUOUS-FETCH] \uD83D\uDCFA ".concat(mode, " MODE: Target duration=").concat(adBreakDurationMs, "ms"));
@@ -4802,13 +4363,13 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4802
4363
  this.pendingNextAdBids = null;
4803
4364
  this.isShowingPlaceholder = false;
4804
4365
  this.totalAdRequestsInBreak = 0;
4805
- if (this.savedMutedStateBeforeScte == null && !this.video.muted) {
4806
- this.savedMutedStateBeforeScte = {
4366
+ if (this.savedMutedStateBeforeAd == null && !this.video.muted) {
4367
+ this.savedMutedStateBeforeAd = {
4807
4368
  muted: false,
4808
4369
  volume: this.video.volume
4809
4370
  };
4810
4371
  }
4811
- state = (_this_savedMutedStateBeforeScte = this.savedMutedStateBeforeScte) !== null && _this_savedMutedStateBeforeScte !== void 0 ? _this_savedMutedStateBeforeScte : {
4372
+ state = (_this_savedMutedStateBeforeAd = this.savedMutedStateBeforeAd) !== null && _this_savedMutedStateBeforeAd !== void 0 ? _this_savedMutedStateBeforeAd : {
4812
4373
  muted: this.video.muted,
4813
4374
  volume: this.video.volume
4814
4375
  };
@@ -4835,6 +4396,9 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4835
4396
  if (this.expectedAdBreakDurationMs == null && adBreakDurationMs != null) {
4836
4397
  this.expectedAdBreakDurationMs = adBreakDurationMs;
4837
4398
  }
4399
+ if (this.expectedAdBreakDurationMs != null) {
4400
+ this.scheduleAdStopCountdown(this.expectedAdBreakDurationMs);
4401
+ }
4838
4402
  this.clearPendingAdBreak();
4839
4403
  adBreakToken = Date.now();
4840
4404
  this.activeAdRequestToken = adBreakToken;
@@ -5333,54 +4897,18 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5333
4897
  this.handleAdPodComplete();
5334
4898
  }
5335
4899
  },
5336
- {
5337
- key: "scheduleAdStartIn",
5338
- value: function scheduleAdStartIn(delayMs) {
5339
- var _this = this;
5340
- this.clearAdStartTimer();
5341
- var ms = Math.max(0, Math.floor(delayMs));
5342
- if (ms === 0) {
5343
- this.handleAdStart({
5344
- type: "start"
5345
- }).catch(function() {});
5346
- return;
5347
- }
5348
- this.adStartTimerId = window.setTimeout(function() {
5349
- _this.handleAdStart({
5350
- type: "start"
5351
- }).catch(function() {});
5352
- }, ms);
5353
- }
5354
- },
5355
- {
5356
- key: "clearAdStartTimer",
5357
- value: function clearAdStartTimer() {
5358
- if (this.adStartTimerId != null) {
5359
- clearTimeout(this.adStartTimerId);
5360
- this.adStartTimerId = void 0;
5361
- }
5362
- }
5363
- },
5364
- {
5365
- key: "updatePtsDrift",
5366
- value: function updatePtsDrift(ptsSecondsSample) {
5367
- var sampleMs = (this.video.currentTime - ptsSecondsSample) * 1e3;
5368
- if (!Number.isFinite(sampleMs) || Math.abs(sampleMs) > 6e4) return;
5369
- var alpha = 0.1;
5370
- this.ptsDriftEmaMs = this.ptsDriftEmaMs * (1 - alpha) + sampleMs * alpha;
5371
- }
5372
- },
5373
4900
  {
5374
4901
  key: "handleAdPodComplete",
5375
4902
  value: function handleAdPodComplete() {
5376
4903
  var _this = this;
5377
4904
  var _ref, _ref1;
5378
- var _this_savedMutedStateBeforeScte, _this_savedMutedStateBeforeScte1;
4905
+ var _this_savedMutedStateBeforeAd, _this_savedMutedStateBeforeAd1;
5379
4906
  if (this.config.debugAdTiming) {
5380
4907
  console.log("[StormcloudVideoPlayer] \uD83C\uDFC1 Ad pod complete - cleaning up");
5381
4908
  }
5382
4909
  this.clearAdRequestWatchdog();
5383
4910
  this.clearAdFailsafeTimer();
4911
+ this.clearAdInsertionOffsetTimer();
5384
4912
  this.activeAdRequestToken = null;
5385
4913
  this.isInAdTransition = false;
5386
4914
  this.stopContinuousFetching();
@@ -5396,7 +4924,6 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5396
4924
  this.inAdBreak = false;
5397
4925
  this.expectedAdBreakDurationMs = void 0;
5398
4926
  this.currentAdBreakStartWallClockMs = void 0;
5399
- this.clearAdStartTimer();
5400
4927
  this.clearAdStopTimer();
5401
4928
  this.adPodQueue = [];
5402
4929
  this.showAds = false;
@@ -5404,8 +4931,8 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5404
4931
  this.totalAdsInBreak = 0;
5405
4932
  this.totalAdRequestsInBreak = 0;
5406
4933
  this.consecutiveFailures = 0;
5407
- var restoredMuted = (_ref = (_this_savedMutedStateBeforeScte = this.savedMutedStateBeforeScte) === null || _this_savedMutedStateBeforeScte === void 0 ? void 0 : _this_savedMutedStateBeforeScte.muted) !== null && _ref !== void 0 ? _ref : this.adLayer.getOriginalMutedState();
5408
- var restoredVolume = (_ref1 = (_this_savedMutedStateBeforeScte1 = this.savedMutedStateBeforeScte) === null || _this_savedMutedStateBeforeScte1 === void 0 ? void 0 : _this_savedMutedStateBeforeScte1.volume) !== null && _ref1 !== void 0 ? _ref1 : this.adLayer.getOriginalVolume();
4934
+ var restoredMuted = (_ref = (_this_savedMutedStateBeforeAd = this.savedMutedStateBeforeAd) === null || _this_savedMutedStateBeforeAd === void 0 ? void 0 : _this_savedMutedStateBeforeAd.muted) !== null && _ref !== void 0 ? _ref : this.adLayer.getOriginalMutedState();
4935
+ var restoredVolume = (_ref1 = (_this_savedMutedStateBeforeAd1 = this.savedMutedStateBeforeAd) === null || _this_savedMutedStateBeforeAd1 === void 0 ? void 0 : _this_savedMutedStateBeforeAd1.volume) !== null && _ref1 !== void 0 ? _ref1 : this.adLayer.getOriginalVolume();
5409
4936
  this.adLayer.updateOriginalMutedState(restoredMuted, restoredVolume);
5410
4937
  this.adLayer.stop().catch(function() {});
5411
4938
  if (this.video.muted !== restoredMuted) {
@@ -5514,7 +5041,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5514
5041
  }, delay);
5515
5042
  });
5516
5043
  }
5517
- this.savedMutedStateBeforeScte = null;
5044
+ this.savedMutedStateBeforeAd = null;
5518
5045
  }
5519
5046
  },
5520
5047
  {
@@ -5664,19 +5191,6 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5664
5191
  return Math.max(0, this.expectedAdBreakDurationMs - elapsed);
5665
5192
  }
5666
5193
  },
5667
- {
5668
- key: "pushScteMarker",
5669
- value: function pushScteMarker(marker) {
5670
- if (!this.config.debugAdTiming) return;
5671
- this.scteMarkerHistory.push({
5672
- timestampMs: Date.now(),
5673
- marker: marker
5674
- });
5675
- if (this.scteMarkerHistory.length > DEBUG_HISTORY_LIMIT) {
5676
- this.scteMarkerHistory = this.scteMarkerHistory.slice(-DEBUG_HISTORY_LIMIT);
5677
- }
5678
- }
5679
- },
5680
5194
  {
5681
5195
  key: "pushDebugLog",
5682
5196
  value: function pushDebugLog(level, category, message, details) {
@@ -5695,20 +5209,29 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5695
5209
  }
5696
5210
  },
5697
5211
  {
5698
- key: "getRecentScteMarkers",
5699
- value: function getRecentScteMarkers() {
5700
- return this.scteMarkerHistory.map(function(entry) {
5701
- return _object_spread({
5702
- timestampMs: entry.timestampMs,
5703
- type: entry.marker.type
5704
- }, entry.marker.ptsSeconds !== void 0 ? {
5705
- ptsSeconds: entry.marker.ptsSeconds
5706
- } : {}, entry.marker.durationSeconds !== void 0 ? {
5707
- durationSeconds: entry.marker.durationSeconds
5708
- } : {}, entry.marker.raw !== void 0 ? {
5709
- raw: entry.marker.raw
5710
- } : {});
5711
- });
5212
+ key: "pushAdInsertionDebug",
5213
+ value: function pushAdInsertionDebug(event, segmentName, opts) {
5214
+ if (!this.config.debugAdTiming) return;
5215
+ this.adInsertionDebugHistory.push(_object_spread({
5216
+ timestampMs: Date.now(),
5217
+ event: event,
5218
+ segmentName: segmentName
5219
+ }, (opts === null || opts === void 0 ? void 0 : opts.offsetSeconds) !== void 0 ? {
5220
+ offsetSeconds: opts.offsetSeconds
5221
+ } : {}, (opts === null || opts === void 0 ? void 0 : opts.updatedAt) ? {
5222
+ updatedAt: opts.updatedAt
5223
+ } : {}, (opts === null || opts === void 0 ? void 0 : opts.detail) ? {
5224
+ detail: opts.detail
5225
+ } : {}));
5226
+ if (this.adInsertionDebugHistory.length > DEBUG_HISTORY_LIMIT) {
5227
+ this.adInsertionDebugHistory = this.adInsertionDebugHistory.slice(-DEBUG_HISTORY_LIMIT);
5228
+ }
5229
+ }
5230
+ },
5231
+ {
5232
+ key: "getAdInsertionDebugLog",
5233
+ value: function getAdInsertionDebugLog() {
5234
+ return this.adInsertionDebugHistory.slice();
5712
5235
  }
5713
5236
  },
5714
5237
  {
@@ -5977,9 +5500,10 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5977
5500
  key: "destroy",
5978
5501
  value: function destroy() {
5979
5502
  var _this_hls, _this_adLayer;
5503
+ this.stopAdInsertionPolling();
5504
+ this.clearAdInsertionOffsetTimer();
5980
5505
  this.stopContinuousFetching();
5981
5506
  this.stopFillerBreakTimer();
5982
- this.clearAdStartTimer();
5983
5507
  this.clearAdStopTimer();
5984
5508
  this.clearAdFailsafeTimer();
5985
5509
  this.clearAdRequestWatchdog();
@@ -6007,14 +5531,14 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
6007
5531
  (_this_adLayer = this.adLayer) === null || _this_adLayer === void 0 ? void 0 : _this_adLayer.destroy();
6008
5532
  this.consecutiveFailures = 0;
6009
5533
  this.debugLogEntries = [];
6010
- this.scteMarkerHistory = [];
5534
+ this.adInsertionDebugHistory = [];
6011
5535
  }
6012
5536
  }
6013
5537
  ]);
6014
5538
  return StormcloudVideoPlayer;
6015
5539
  }();
6016
5540
  // src/ui/StormcloudVideoPlayer.tsx
6017
- import { FaPlay, FaPause, FaVolumeUp, FaVolumeMute, FaVolumeDown, FaExpand, FaCompress, FaCog, FaTimes } from "react-icons/fa";
5541
+ import { FaPlay, FaPause, FaVolumeUp, FaVolumeMute, FaVolumeDown, FaExpand, FaCompress, FaCog, FaTimes, FaCopy } from "react-icons/fa";
6018
5542
  // src/ui/OverlayRenderer.tsx
6019
5543
  import React, { useEffect, useRef, useState, useCallback, useMemo } from "react";
6020
5544
  // src/utils/overlays.ts
@@ -7384,7 +6908,7 @@ var AI_CONTEXT_MIN_POLL_MS = 800;
7384
6908
  var PANEL_BASE_RIGHT_OFFSET = 10;
7385
6909
  var StormcloudVideoPlayerComponent = React2.memo(function(props) {
7386
6910
  var _ref;
7387
- var _aiLiveContext_context, _aiLiveContext_context_keywords, _aiLiveContext_context1;
6911
+ var _aiLiveContext_context;
7388
6912
  var src = props.src, autoplay = props.autoplay, muted = props.muted, lowLatencyMode = props.lowLatencyMode, allowNativeHls = props.allowNativeHls, driftToleranceMs = props.driftToleranceMs, immediateManifestAds = props.immediateManifestAds, debugAdTiming = props.debugAdTiming, showCustomControls = props.showCustomControls, hideLoadingIndicator = props.hideLoadingIndicator, onVolumeToggle = props.onVolumeToggle, onFullscreenToggle = props.onFullscreenToggle, onControlClick = props.onControlClick, onReady = props.onReady, wrapperClassName = props.wrapperClassName, wrapperStyle = props.wrapperStyle, className = props.className, style = props.style, controls = props.controls, playsInline = props.playsInline, preload = props.preload, poster = props.poster, children = props.children, licenseKey = props.licenseKey, minSegmentsBeforePlay = props.minSegmentsBeforePlay, disableAds = props.disableAds, disableFiller = props.disableFiller, swirlProjectId = props.swirlProjectId, restVideoAttrs = _object_without_properties(props, [
7389
6913
  "src",
7390
6914
  "autoplay",
@@ -7476,13 +7000,44 @@ var StormcloudVideoPlayerComponent = React2.memo(function(props) {
7476
7000
  return "--:--:--";
7477
7001
  }
7478
7002
  };
7479
- var formatDebugRaw = function formatDebugRaw(raw) {
7480
- if (!raw || (typeof raw === "undefined" ? "undefined" : _type_of(raw)) !== "object") return "";
7481
- var obj = raw;
7482
- if (typeof obj.tag === "string") return obj.tag;
7483
- if (typeof obj.id3 === "string") return "ID3";
7484
- if (typeof obj.splice_command_type === "number") return "binary splice";
7485
- return "marker";
7003
+ var formatAdInsertionEvent = function formatAdInsertionEvent(event) {
7004
+ switch(event){
7005
+ case "api_response":
7006
+ return {
7007
+ label: "API",
7008
+ color: "#60a5fa"
7009
+ };
7010
+ case "scte35_inserted":
7011
+ return {
7012
+ label: "SCTE-35",
7013
+ color: "#f43f5e"
7014
+ };
7015
+ case "segment_found":
7016
+ return {
7017
+ label: "FOUND",
7018
+ color: "#a78bfa"
7019
+ };
7020
+ case "segment_playing":
7021
+ return {
7022
+ label: "PLAYING",
7023
+ color: "#fbbf24"
7024
+ };
7025
+ case "ad_scheduled":
7026
+ return {
7027
+ label: "SCHED",
7028
+ color: "#fb923c"
7029
+ };
7030
+ case "ad_triggered":
7031
+ return {
7032
+ label: "TRIGGER",
7033
+ color: "#34d399"
7034
+ };
7035
+ default:
7036
+ return {
7037
+ label: event,
7038
+ color: "rgba(255,255,255,0.68)"
7039
+ };
7040
+ }
7486
7041
  };
7487
7042
  var formatAiRelativeTime = function formatAiRelativeTime(timestamp) {
7488
7043
  var epochMs = Date.parse(timestamp);
@@ -7627,6 +7182,7 @@ var StormcloudVideoPlayerComponent = React2.memo(function(props) {
7627
7182
  if (minSegmentsBeforePlay !== void 0) cfg.minSegmentsBeforePlay = minSegmentsBeforePlay;
7628
7183
  if (disableAds !== void 0) cfg.disableAds = disableAds;
7629
7184
  cfg.disableFiller = disableFiller !== null && disableFiller !== void 0 ? disableFiller : true;
7185
+ if (swirlProjectId !== void 0) cfg.projectId = String(swirlProjectId);
7630
7186
  var player = new StormcloudVideoPlayer(cfg);
7631
7187
  playerRef.current = player;
7632
7188
  player.load().then(function() {
@@ -7895,7 +7451,7 @@ var StormcloudVideoPlayerComponent = React2.memo(function(props) {
7895
7451
  var updateDebugData = function updateDebugData() {
7896
7452
  var player = playerRef.current;
7897
7453
  if (!player) return;
7898
- setDebugMarkers(player.getRecentScteMarkers().slice(-DEBUG_PANEL_MARKER_LIMIT).reverse());
7454
+ setDebugMarkers(player.getAdInsertionDebugLog().slice(-DEBUG_PANEL_MARKER_LIMIT).reverse());
7899
7455
  };
7900
7456
  updateDebugData();
7901
7457
  var interval = window.setInterval(updateDebugData, 500);
@@ -8430,34 +7986,7 @@ var StormcloudVideoPlayerComponent = React2.memo(function(props) {
8430
7986
  whiteSpace: "pre-wrap"
8431
7987
  },
8432
7988
  children: (_ref = (_aiLiveContext_context = aiLiveContext.context) === null || _aiLiveContext_context === void 0 ? void 0 : _aiLiveContext_context.context) !== null && _ref !== void 0 ? _ref : aiLiveContext.isLoading ? "Analyzing live stream..." : "Waiting for AI context response."
8433
- }),
8434
- ((_aiLiveContext_context1 = aiLiveContext.context) === null || _aiLiveContext_context1 === void 0 ? void 0 : (_aiLiveContext_context_keywords = _aiLiveContext_context1.keywords) === null || _aiLiveContext_context_keywords === void 0 ? void 0 : _aiLiveContext_context_keywords.length) ? /* @__PURE__ */ jsx2("div", {
8435
- style: {
8436
- marginTop: "10px",
8437
- display: "flex",
8438
- flexWrap: "wrap",
8439
- gap: "6px"
8440
- },
8441
- children: aiLiveContext.context.keywords.slice(0, 12).map(function(kw) {
8442
- return /* @__PURE__ */ jsx2("span", {
8443
- style: {
8444
- fontSize: "10px",
8445
- fontWeight: 600,
8446
- padding: "4px 7px",
8447
- borderRadius: "999px",
8448
- background: "rgba(236, 72, 153, 0.2)",
8449
- border: "1px solid rgba(244, 114, 182, 0.42)",
8450
- color: "#fce7f3",
8451
- maxWidth: "100%",
8452
- overflow: "hidden",
8453
- textOverflow: "ellipsis",
8454
- whiteSpace: "nowrap"
8455
- },
8456
- title: kw,
8457
- children: kw
8458
- }, kw);
8459
- })
8460
- }) : null
7989
+ })
8461
7990
  ]
8462
7991
  })
8463
7992
  ]
@@ -8499,7 +8028,7 @@ var StormcloudVideoPlayerComponent = React2.memo(function(props) {
8499
8028
  fontWeight: 700,
8500
8029
  letterSpacing: "0.02em"
8501
8030
  },
8502
- children: "Debug Ad Timing"
8031
+ children: "Ad Insertion Debug"
8503
8032
  }),
8504
8033
  /* @__PURE__ */ jsx2("button", {
8505
8034
  className: "sc-ctrl-btn",
@@ -8527,9 +8056,13 @@ var StormcloudVideoPlayerComponent = React2.memo(function(props) {
8527
8056
  overflowX: "hidden",
8528
8057
  height: "calc(100% - 46px)",
8529
8058
  display: "grid",
8530
- gap: "12px"
8059
+ gap: "12px",
8060
+ minWidth: 0
8531
8061
  },
8532
8062
  children: /* @__PURE__ */ jsxs2("div", {
8063
+ style: {
8064
+ minWidth: 0
8065
+ },
8533
8066
  children: [
8534
8067
  /* @__PURE__ */ jsx2("div", {
8535
8068
  style: {
@@ -8540,69 +8073,145 @@ var StormcloudVideoPlayerComponent = React2.memo(function(props) {
8540
8073
  color: "rgba(255,255,255,0.68)",
8541
8074
  marginBottom: "8px"
8542
8075
  },
8543
- children: "SCTE-35 markers"
8076
+ children: "Ad Insertion Points"
8544
8077
  }),
8545
8078
  /* @__PURE__ */ jsx2("div", {
8546
8079
  style: {
8547
8080
  display: "grid",
8548
- gap: "6px"
8081
+ gap: "6px",
8082
+ minWidth: 0
8549
8083
  },
8550
8084
  children: debugMarkers.length === 0 ? /* @__PURE__ */ jsx2("div", {
8551
8085
  style: {
8552
8086
  fontSize: "12px",
8553
8087
  color: "rgba(255,255,255,0.55)"
8554
8088
  },
8555
- children: "No markers detected yet."
8089
+ children: "No ad insertion events yet."
8556
8090
  }) : debugMarkers.map(function(entry, idx) {
8091
+ var evt = formatAdInsertionEvent(entry.event);
8092
+ var fileName = entry.segmentName ? entry.segmentName.split("/").pop() || entry.segmentName : "";
8557
8093
  return /* @__PURE__ */ jsxs2("div", {
8558
8094
  style: {
8559
- display: "grid",
8560
- gridTemplateColumns: "56px 52px 1fr",
8561
- gap: "8px",
8562
- alignItems: "center",
8095
+ minWidth: 0,
8563
8096
  fontFamily: "'SF Mono', 'Cascadia Code', monospace",
8564
8097
  fontSize: "11px",
8565
8098
  background: "rgba(255,255,255,0.05)",
8566
8099
  border: "1px solid rgba(255,255,255,0.08)",
8567
8100
  borderRadius: "8px",
8568
- padding: "6px 8px"
8101
+ padding: "6px 8px",
8102
+ display: "flex",
8103
+ flexDirection: "column",
8104
+ gap: "4px"
8569
8105
  },
8570
8106
  children: [
8571
- /* @__PURE__ */ jsx2("span", {
8572
- style: {
8573
- color: "rgba(255,255,255,0.68)"
8574
- },
8575
- children: formatDebugClock(entry.timestampMs)
8576
- }),
8577
- /* @__PURE__ */ jsx2("span", {
8107
+ /* @__PURE__ */ jsxs2("div", {
8578
8108
  style: {
8579
- textTransform: "uppercase",
8580
- fontWeight: 700,
8581
- color: entry.type === "start" ? "#34d399" : entry.type === "end" ? "#f87171" : "#fbbf24"
8109
+ display: "grid",
8110
+ gridTemplateColumns: "56px 54px minmax(0, 1fr) 20px",
8111
+ gap: "6px",
8112
+ alignItems: "center",
8113
+ minWidth: 0
8582
8114
  },
8583
- children: entry.type
8115
+ children: [
8116
+ /* @__PURE__ */ jsx2("span", {
8117
+ style: {
8118
+ color: "rgba(255,255,255,0.68)"
8119
+ },
8120
+ children: formatDebugClock(entry.timestampMs)
8121
+ }),
8122
+ /* @__PURE__ */ jsx2("span", {
8123
+ style: {
8124
+ fontWeight: 700,
8125
+ color: evt.color,
8126
+ fontSize: "10px"
8127
+ },
8128
+ children: evt.label
8129
+ }),
8130
+ /* @__PURE__ */ jsx2("span", {
8131
+ style: {
8132
+ color: "rgba(255,255,255,0.88)",
8133
+ overflow: "hidden",
8134
+ textOverflow: "ellipsis",
8135
+ whiteSpace: "nowrap",
8136
+ minWidth: 0
8137
+ },
8138
+ title: entry.segmentName,
8139
+ children: fileName
8140
+ }),
8141
+ /* @__PURE__ */ jsx2("button", {
8142
+ onClick: function onClick() {
8143
+ return navigator.clipboard.writeText(entry.segmentName);
8144
+ },
8145
+ title: entry.segmentName,
8146
+ style: {
8147
+ background: "none",
8148
+ border: "none",
8149
+ cursor: "pointer",
8150
+ padding: "2px",
8151
+ color: "rgba(255,255,255,0.45)",
8152
+ display: "flex",
8153
+ alignItems: "center",
8154
+ justifyContent: "center",
8155
+ borderRadius: "4px",
8156
+ flexShrink: 0
8157
+ },
8158
+ onMouseEnter: function onMouseEnter(e) {
8159
+ return e.currentTarget.style.color = "rgba(255,255,255,0.9)";
8160
+ },
8161
+ onMouseLeave: function onMouseLeave(e) {
8162
+ return e.currentTarget.style.color = "rgba(255,255,255,0.45)";
8163
+ },
8164
+ children: /* @__PURE__ */ jsx2(FaCopy, {
8165
+ size: 10
8166
+ })
8167
+ })
8168
+ ]
8584
8169
  }),
8585
- /* @__PURE__ */ jsx2("span", {
8170
+ (entry.offsetSeconds != null || entry.detail || entry.updatedAt) && /* @__PURE__ */ jsxs2("div", {
8586
8171
  style: {
8587
- color: "rgba(255,255,255,0.88)"
8172
+ display: "flex",
8173
+ flexWrap: "wrap",
8174
+ gap: "6px",
8175
+ paddingLeft: "0px",
8176
+ fontSize: "10px"
8588
8177
  },
8589
- children: /* @__PURE__ */ jsxs2("span", {
8590
- style: {
8591
- display: "inline-block",
8592
- maxWidth: "100%",
8593
- verticalAlign: "bottom",
8594
- overflow: "hidden",
8595
- textOverflow: "ellipsis",
8596
- whiteSpace: "nowrap"
8597
- },
8598
- children: [
8599
- entry.durationSeconds != null ? "dur:".concat(entry.durationSeconds.toFixed(2), "s") : "dur:-",
8600
- " ",
8601
- entry.ptsSeconds != null ? "pts:".concat(entry.ptsSeconds.toFixed(2)) : "pts:-",
8602
- " ",
8603
- formatDebugRaw(entry.raw)
8604
- ]
8605
- })
8178
+ children: [
8179
+ entry.offsetSeconds != null && /* @__PURE__ */ jsxs2("span", {
8180
+ style: {
8181
+ color: "rgba(255,255,255,0.55)",
8182
+ background: "rgba(255,255,255,0.06)",
8183
+ borderRadius: "4px",
8184
+ padding: "1px 5px"
8185
+ },
8186
+ children: [
8187
+ "+",
8188
+ entry.offsetSeconds.toFixed(2),
8189
+ "s"
8190
+ ]
8191
+ }),
8192
+ entry.detail && /* @__PURE__ */ jsx2("span", {
8193
+ style: {
8194
+ color: "rgba(255,255,255,0.55)",
8195
+ background: "rgba(255,255,255,0.06)",
8196
+ borderRadius: "4px",
8197
+ padding: "1px 5px",
8198
+ overflow: "hidden",
8199
+ textOverflow: "ellipsis",
8200
+ whiteSpace: "nowrap",
8201
+ maxWidth: "100%"
8202
+ },
8203
+ children: entry.detail
8204
+ }),
8205
+ entry.updatedAt && /* @__PURE__ */ jsx2("span", {
8206
+ style: {
8207
+ color: "rgba(255,255,255,0.38)",
8208
+ background: "rgba(255,255,255,0.04)",
8209
+ borderRadius: "4px",
8210
+ padding: "1px 5px"
8211
+ },
8212
+ children: formatAiRelativeTime(entry.updatedAt)
8213
+ })
8214
+ ]
8606
8215
  })
8607
8216
  ]
8608
8217
  }, "".concat(entry.timestampMs, "-").concat(idx));