stormcloud-video-player 0.8.16 → 0.8.18

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.
Files changed (52) hide show
  1. package/dist/stormcloud-vp.min.js +2 -2
  2. package/lib/index.cjs +4449 -4221
  3. package/lib/index.cjs.map +1 -1
  4. package/lib/index.d.cts +18 -195
  5. package/lib/index.d.ts +18 -195
  6. package/lib/index.js +4455 -4219
  7. package/lib/index.js.map +1 -1
  8. package/lib/player/AdBreakOrchestrator.cjs +2275 -0
  9. package/lib/player/AdBreakOrchestrator.cjs.map +1 -0
  10. package/lib/player/AdBreakOrchestrator.d.cts +62 -0
  11. package/lib/player/AdPreloadPool.cjs +673 -0
  12. package/lib/player/AdPreloadPool.cjs.map +1 -0
  13. package/lib/player/AdPreloadPool.d.cts +37 -0
  14. package/lib/player/AdTimingService.cjs +841 -0
  15. package/lib/player/AdTimingService.cjs.map +1 -0
  16. package/lib/player/AdTimingService.d.cts +88 -0
  17. package/lib/player/HlsEngine.cjs +1502 -0
  18. package/lib/player/HlsEngine.cjs.map +1 -0
  19. package/lib/player/HlsEngine.d.cts +38 -0
  20. package/lib/player/PlaceholderLayer.cjs +191 -0
  21. package/lib/player/PlaceholderLayer.cjs.map +1 -0
  22. package/lib/player/PlaceholderLayer.d.cts +12 -0
  23. package/lib/player/PlayerControls.cjs +225 -0
  24. package/lib/player/PlayerControls.cjs.map +1 -0
  25. package/lib/player/PlayerControls.d.cts +13 -0
  26. package/lib/player/Scte35CueManager.cjs +413 -0
  27. package/lib/player/Scte35CueManager.cjs.map +1 -0
  28. package/lib/player/Scte35CueManager.d.cts +43 -0
  29. package/lib/player/Scte35Parser.cjs +945 -0
  30. package/lib/player/Scte35Parser.cjs.map +1 -0
  31. package/lib/player/Scte35Parser.d.cts +23 -0
  32. package/lib/player/StormcloudVideoPlayer.cjs +4411 -4168
  33. package/lib/player/StormcloudVideoPlayer.cjs.map +1 -1
  34. package/lib/player/StormcloudVideoPlayer.d.cts +19 -196
  35. package/lib/player/VmapManager.cjs +1706 -0
  36. package/lib/player/VmapManager.cjs.map +1 -0
  37. package/lib/player/VmapManager.d.cts +38 -0
  38. package/lib/player/playerTypes.cjs +49 -0
  39. package/lib/player/playerTypes.cjs.map +1 -0
  40. package/lib/player/playerTypes.d.cts +20 -0
  41. package/lib/players/HlsPlayer.cjs +4411 -4169
  42. package/lib/players/HlsPlayer.cjs.map +1 -1
  43. package/lib/players/HlsPlayer.d.cts +1 -1
  44. package/lib/players/index.cjs +4411 -4169
  45. package/lib/players/index.cjs.map +1 -1
  46. package/lib/sdk/hlsAdPlayer.d.cts +1 -1
  47. package/lib/{types-CWWADkvd.d.cts → types-DNiBmPKK.d.cts} +18 -1
  48. package/lib/ui/StormcloudVideoPlayer.cjs +4449 -4221
  49. package/lib/ui/StormcloudVideoPlayer.cjs.map +1 -1
  50. package/lib/ui/StormcloudVideoPlayer.d.cts +1 -1
  51. package/lib/utils/tracking.d.cts +1 -1
  52. package/package.json +1 -1
@@ -0,0 +1,841 @@
1
+ "use strict";
2
+ function _array_like_to_array(arr, len) {
3
+ if (len == null || len > arr.length) len = arr.length;
4
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
5
+ return arr2;
6
+ }
7
+ function _array_with_holes(arr) {
8
+ if (Array.isArray(arr)) return arr;
9
+ }
10
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
11
+ try {
12
+ var info = gen[key](arg);
13
+ var value = info.value;
14
+ } catch (error) {
15
+ reject(error);
16
+ return;
17
+ }
18
+ if (info.done) {
19
+ resolve(value);
20
+ } else {
21
+ Promise.resolve(value).then(_next, _throw);
22
+ }
23
+ }
24
+ function _async_to_generator(fn) {
25
+ return function() {
26
+ var self = this, args = arguments;
27
+ return new Promise(function(resolve, reject) {
28
+ var gen = fn.apply(self, args);
29
+ function _next(value) {
30
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
31
+ }
32
+ function _throw(err) {
33
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
34
+ }
35
+ _next(undefined);
36
+ });
37
+ };
38
+ }
39
+ function _class_call_check(instance, Constructor) {
40
+ if (!(instance instanceof Constructor)) {
41
+ throw new TypeError("Cannot call a class as a function");
42
+ }
43
+ }
44
+ function _defineProperties(target, props) {
45
+ for(var i = 0; i < props.length; i++){
46
+ var descriptor = props[i];
47
+ descriptor.enumerable = descriptor.enumerable || false;
48
+ descriptor.configurable = true;
49
+ if ("value" in descriptor) descriptor.writable = true;
50
+ Object.defineProperty(target, descriptor.key, descriptor);
51
+ }
52
+ }
53
+ function _create_class(Constructor, protoProps, staticProps) {
54
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
55
+ if (staticProps) _defineProperties(Constructor, staticProps);
56
+ return Constructor;
57
+ }
58
+ function _define_property(obj, key, value) {
59
+ if (key in obj) {
60
+ Object.defineProperty(obj, key, {
61
+ value: value,
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true
65
+ });
66
+ } else {
67
+ obj[key] = value;
68
+ }
69
+ return obj;
70
+ }
71
+ function _iterable_to_array_limit(arr, i) {
72
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
73
+ if (_i == null) return;
74
+ var _arr = [];
75
+ var _n = true;
76
+ var _d = false;
77
+ var _s, _e;
78
+ try {
79
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
80
+ _arr.push(_s.value);
81
+ if (i && _arr.length === i) break;
82
+ }
83
+ } catch (err) {
84
+ _d = true;
85
+ _e = err;
86
+ } finally{
87
+ try {
88
+ if (!_n && _i["return"] != null) _i["return"]();
89
+ } finally{
90
+ if (_d) throw _e;
91
+ }
92
+ }
93
+ return _arr;
94
+ }
95
+ function _non_iterable_rest() {
96
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
97
+ }
98
+ function _object_spread(target) {
99
+ for(var i = 1; i < arguments.length; i++){
100
+ var source = arguments[i] != null ? arguments[i] : {};
101
+ var ownKeys = Object.keys(source);
102
+ if (typeof Object.getOwnPropertySymbols === "function") {
103
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
104
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
105
+ }));
106
+ }
107
+ ownKeys.forEach(function(key) {
108
+ _define_property(target, key, source[key]);
109
+ });
110
+ }
111
+ return target;
112
+ }
113
+ function _sliced_to_array(arr, i) {
114
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
115
+ }
116
+ function _type_of(obj) {
117
+ "@swc/helpers - typeof";
118
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
119
+ }
120
+ function _unsupported_iterable_to_array(o, minLen) {
121
+ if (!o) return;
122
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
123
+ var n = Object.prototype.toString.call(o).slice(8, -1);
124
+ if (n === "Object" && o.constructor) n = o.constructor.name;
125
+ if (n === "Map" || n === "Set") return Array.from(n);
126
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
127
+ }
128
+ function _ts_generator(thisArg, body) {
129
+ var f, y, t, _ = {
130
+ label: 0,
131
+ sent: function() {
132
+ if (t[0] & 1) throw t[1];
133
+ return t[1];
134
+ },
135
+ trys: [],
136
+ ops: []
137
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
138
+ return d(g, "next", {
139
+ value: verb(0)
140
+ }), d(g, "throw", {
141
+ value: verb(1)
142
+ }), d(g, "return", {
143
+ value: verb(2)
144
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
145
+ value: function() {
146
+ return this;
147
+ }
148
+ }), g;
149
+ function verb(n) {
150
+ return function(v) {
151
+ return step([
152
+ n,
153
+ v
154
+ ]);
155
+ };
156
+ }
157
+ function step(op) {
158
+ if (f) throw new TypeError("Generator is already executing.");
159
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
160
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
161
+ if (y = 0, t) op = [
162
+ op[0] & 2,
163
+ t.value
164
+ ];
165
+ switch(op[0]){
166
+ case 0:
167
+ case 1:
168
+ t = op;
169
+ break;
170
+ case 4:
171
+ _.label++;
172
+ return {
173
+ value: op[1],
174
+ done: false
175
+ };
176
+ case 5:
177
+ _.label++;
178
+ y = op[1];
179
+ op = [
180
+ 0
181
+ ];
182
+ continue;
183
+ case 7:
184
+ op = _.ops.pop();
185
+ _.trys.pop();
186
+ continue;
187
+ default:
188
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
189
+ _ = 0;
190
+ continue;
191
+ }
192
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
193
+ _.label = op[1];
194
+ break;
195
+ }
196
+ if (op[0] === 6 && _.label < t[1]) {
197
+ _.label = t[1];
198
+ t = op;
199
+ break;
200
+ }
201
+ if (t && _.label < t[2]) {
202
+ _.label = t[2];
203
+ _.ops.push(op);
204
+ break;
205
+ }
206
+ if (t[2]) _.ops.pop();
207
+ _.trys.pop();
208
+ continue;
209
+ }
210
+ op = body.call(thisArg, _);
211
+ } catch (e) {
212
+ op = [
213
+ 6,
214
+ e
215
+ ];
216
+ y = 0;
217
+ } finally{
218
+ f = t = 0;
219
+ }
220
+ if (op[0] & 5) throw op[1];
221
+ return {
222
+ value: op[0] ? op[1] : void 0,
223
+ done: true
224
+ };
225
+ }
226
+ }
227
+ var __defProp = Object.defineProperty;
228
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
229
+ var __getOwnPropNames = Object.getOwnPropertyNames;
230
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
231
+ var __export = function __export(target, all) {
232
+ for(var name in all)__defProp(target, name, {
233
+ get: all[name],
234
+ enumerable: true
235
+ });
236
+ };
237
+ var __copyProps = function __copyProps(to, from, except, desc) {
238
+ if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") {
239
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
240
+ try {
241
+ var _loop = function() {
242
+ var key = _step.value;
243
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
244
+ get: function get() {
245
+ return from[key];
246
+ },
247
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
248
+ });
249
+ };
250
+ for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
251
+ } catch (err) {
252
+ _didIteratorError = true;
253
+ _iteratorError = err;
254
+ } finally{
255
+ try {
256
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
257
+ _iterator.return();
258
+ }
259
+ } finally{
260
+ if (_didIteratorError) {
261
+ throw _iteratorError;
262
+ }
263
+ }
264
+ }
265
+ }
266
+ return to;
267
+ };
268
+ var __toCommonJS = function __toCommonJS(mod) {
269
+ return __copyProps(__defProp({}, "__esModule", {
270
+ value: true
271
+ }), mod);
272
+ };
273
+ // src/player/AdTimingService.ts
274
+ var AdTimingService_exports = {};
275
+ __export(AdTimingService_exports, {
276
+ AdTimingService: function AdTimingService1() {
277
+ return AdTimingService;
278
+ }
279
+ });
280
+ module.exports = __toCommonJS(AdTimingService_exports);
281
+ var AdTimingService = /*#__PURE__*/ function() {
282
+ function AdTimingService(config, video, callbacks) {
283
+ _class_call_check(this, AdTimingService);
284
+ this.adRequestWatchdogToken = null;
285
+ this.adFailsafeToken = null;
286
+ this.ptsDriftEmaMs = 0;
287
+ this.adBreakPlayedDurationMs = 0;
288
+ this.adBreakEpoch = 0;
289
+ this.adRequestTokenCounter = 0;
290
+ this.activeAdRequestToken = null;
291
+ this.currentAdIndex = 0;
292
+ this.totalAdsInBreak = 0;
293
+ this.totalAdRequestsInBreak = 0;
294
+ this.maxTotalAdRequestsPerBreak = 10;
295
+ this.lastAdRequestTime = 0;
296
+ this.consecutiveFailures = 0;
297
+ this.maxConsecutiveFailures = 5;
298
+ this.minAdRequestIntervalMs = 3e3;
299
+ this.backoffBaseMs = 1e3;
300
+ this.maxBackoffMs = 15e3;
301
+ this.defaultMinRemainingMsToStartAd = 4e3;
302
+ this.failedVastUrls = /* @__PURE__ */ new Set();
303
+ this.temporaryFailureUrls = /* @__PURE__ */ new Map();
304
+ this.temporaryFailureCooldownMs = 3e4;
305
+ this.globalConsecutiveNoFills = 0;
306
+ this.globalNoFillThreshold = 3;
307
+ this.globalNoFillCooldownUntil = 0;
308
+ this.globalNoFillBackoffBaseMs = 5e3;
309
+ this.globalNoFillBackoffMaxMs = 6e4;
310
+ this.globalLastGamRequestTime = 0;
311
+ this.globalMinGamIntervalMs = 3e3;
312
+ this.config = config;
313
+ this.video = video;
314
+ this.callbacks = callbacks;
315
+ }
316
+ _create_class(AdTimingService, [
317
+ {
318
+ key: "debug",
319
+ get: function get() {
320
+ return !!this.config.debugAdTiming;
321
+ }
322
+ },
323
+ {
324
+ // --- Ad stop timer ---
325
+ key: "scheduleAdStopCountdown",
326
+ value: function scheduleAdStopCountdown(remainingMs) {
327
+ var _this = this;
328
+ this.clearAdStopTimer();
329
+ var ms = Math.max(0, Math.floor(remainingMs));
330
+ if (ms === 0) {
331
+ this.ensureAdStoppedByTimer();
332
+ return;
333
+ }
334
+ this.adStopTimerId = window.setTimeout(function() {
335
+ _this.ensureAdStoppedByTimer();
336
+ }, ms);
337
+ }
338
+ },
339
+ {
340
+ key: "clearAdStopTimer",
341
+ value: function clearAdStopTimer() {
342
+ if (this.adStopTimerId != null) {
343
+ clearTimeout(this.adStopTimerId);
344
+ this.adStopTimerId = void 0;
345
+ }
346
+ }
347
+ },
348
+ {
349
+ key: "ensureAdStoppedByTimer",
350
+ value: function ensureAdStoppedByTimer() {
351
+ this.adStopTimerId = void 0;
352
+ this.callbacks.onAdStopTimerFired();
353
+ }
354
+ },
355
+ {
356
+ // --- Ad start timer ---
357
+ key: "scheduleAdStartIn",
358
+ value: function scheduleAdStartIn(delayMs) {
359
+ var _this = this;
360
+ var marker = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {
361
+ type: "start"
362
+ }, cueKey = arguments.length > 2 ? arguments[2] : void 0;
363
+ this.clearAdStartTimer();
364
+ var ms = Math.max(0, Math.floor(delayMs));
365
+ if (ms === 0) {
366
+ this.callbacks.onAdStartTimerFired(marker, cueKey);
367
+ return;
368
+ }
369
+ this.adStartTimerId = window.setTimeout(function() {
370
+ _this.callbacks.onAdStartTimerFired(marker, cueKey);
371
+ }, ms);
372
+ }
373
+ },
374
+ {
375
+ key: "clearAdStartTimer",
376
+ value: function clearAdStartTimer() {
377
+ if (this.adStartTimerId != null) {
378
+ clearTimeout(this.adStartTimerId);
379
+ this.adStartTimerId = void 0;
380
+ }
381
+ }
382
+ },
383
+ {
384
+ // --- PTS drift ---
385
+ key: "updatePtsDrift",
386
+ value: function updatePtsDrift(ptsSecondsSample) {
387
+ var sampleMs = (this.video.currentTime - ptsSecondsSample) * 1e3;
388
+ if (!Number.isFinite(sampleMs) || Math.abs(sampleMs) > 6e4) return;
389
+ var alpha = 0.1;
390
+ this.ptsDriftEmaMs = this.ptsDriftEmaMs * (1 - alpha) + sampleMs * alpha;
391
+ }
392
+ },
393
+ {
394
+ key: "resolveScteBreakStartWallClockMs",
395
+ value: function resolveScteBreakStartWallClockMs(marker) {
396
+ var nowWallClockMs = Date.now();
397
+ if (typeof marker.ptsSeconds !== "number") {
398
+ return nowWallClockMs;
399
+ }
400
+ var nowMs = this.video.currentTime * 1e3;
401
+ var estimatedCurrentPtsMs = nowMs - this.ptsDriftEmaMs;
402
+ var deltaMs = marker.ptsSeconds * 1e3 - estimatedCurrentPtsMs;
403
+ if (!Number.isFinite(deltaMs)) {
404
+ return nowWallClockMs;
405
+ }
406
+ return nowWallClockMs + Math.floor(deltaMs);
407
+ }
408
+ },
409
+ {
410
+ // --- Duration boundary ---
411
+ key: "setAdBreakDurationBoundary",
412
+ value: function setAdBreakDurationBoundary(durationMs) {
413
+ this.expectedAdBreakDurationMs = durationMs;
414
+ if (durationMs != null && this.currentAdBreakStartWallClockMs != null) {
415
+ this.scteAdBreakEndWallClockMs = this.currentAdBreakStartWallClockMs + durationMs;
416
+ } else {
417
+ this.scteAdBreakEndWallClockMs = void 0;
418
+ }
419
+ }
420
+ },
421
+ {
422
+ key: "scheduleAdStopAtBreakBoundary",
423
+ value: function scheduleAdStopAtBreakBoundary() {
424
+ if (this.expectedAdBreakDurationMs == null) {
425
+ return;
426
+ }
427
+ this.scheduleAdStopCountdown(this.getWallClockRemainingAdMs());
428
+ }
429
+ },
430
+ {
431
+ // --- Duration budget ---
432
+ key: "getWallClockRemainingAdMs",
433
+ value: function getWallClockRemainingAdMs() {
434
+ var endWallClockMs = this.getAdBreakEndWallClockMs();
435
+ if (endWallClockMs != null) {
436
+ return Math.max(0, endWallClockMs - Date.now());
437
+ }
438
+ return 0;
439
+ }
440
+ },
441
+ {
442
+ key: "getAdBreakEndWallClockMs",
443
+ value: function getAdBreakEndWallClockMs() {
444
+ if (this.scteAdBreakEndWallClockMs != null) {
445
+ return this.scteAdBreakEndWallClockMs;
446
+ }
447
+ if (this.expectedAdBreakDurationMs == null || this.currentAdBreakStartWallClockMs == null) {
448
+ return void 0;
449
+ }
450
+ return this.currentAdBreakStartWallClockMs + this.expectedAdBreakDurationMs;
451
+ }
452
+ },
453
+ {
454
+ key: "getDurationBudgetRemainingMs",
455
+ value: function getDurationBudgetRemainingMs() {
456
+ if (this.expectedAdBreakDurationMs == null) {
457
+ return 0;
458
+ }
459
+ return Math.max(0, this.expectedAdBreakDurationMs - this.adBreakPlayedDurationMs);
460
+ }
461
+ },
462
+ {
463
+ key: "getLoadedAdDurationMs",
464
+ value: function getLoadedAdDurationMs(adController) {
465
+ var _adController_getLoadedAdDuration;
466
+ var durationSec = (_adController_getLoadedAdDuration = adController.getLoadedAdDuration) === null || _adController_getLoadedAdDuration === void 0 ? void 0 : _adController_getLoadedAdDuration.call(adController);
467
+ if (durationSec == null || durationSec <= 0) {
468
+ return 0;
469
+ }
470
+ return Math.round(durationSec * 1e3);
471
+ }
472
+ },
473
+ {
474
+ key: "adFitsRemainingBudget",
475
+ value: function adFitsRemainingBudget(durationMs) {
476
+ if (durationMs <= 0) {
477
+ return true;
478
+ }
479
+ return durationMs <= this.getDurationBudgetRemainingMs();
480
+ }
481
+ },
482
+ {
483
+ key: "logAdDurationDecision",
484
+ value: function logAdDurationDecision(accepted, durationMs, adController) {
485
+ var _ref;
486
+ var _adController_getLoadedAdId;
487
+ if (!this.debug) {
488
+ return;
489
+ }
490
+ var remainingBudget = this.getDurationBudgetRemainingMs();
491
+ var adId = (_ref = (_adController_getLoadedAdId = adController.getLoadedAdId) === null || _adController_getLoadedAdId === void 0 ? void 0 : _adController_getLoadedAdId.call(adController)) !== null && _ref !== void 0 ? _ref : "unknown";
492
+ if (accepted) {
493
+ console.log("[AD-DURATION] Accepted ad id=".concat(adId, ", duration=").concat(durationMs, "ms, budget remaining after play=").concat(Math.max(0, remainingBudget - durationMs), "ms"));
494
+ } else {
495
+ console.log("[AD-DURATION] Rejected ad id=".concat(adId, ", duration=").concat(durationMs, "ms exceeds remaining budget=").concat(remainingBudget, "ms — requesting another ad"));
496
+ }
497
+ }
498
+ },
499
+ {
500
+ key: "recordPlayedAdDuration",
501
+ value: function recordPlayedAdDuration(adController) {
502
+ var durationMs = this.getLoadedAdDurationMs(adController);
503
+ if (durationMs <= 0) {
504
+ return;
505
+ }
506
+ this.adBreakPlayedDurationMs += durationMs;
507
+ if (this.debug) {
508
+ var _this_expectedAdBreakDurationMs;
509
+ console.log("[AD-DURATION] Played ".concat(durationMs, "ms — total played=").concat(this.adBreakPlayedDurationMs, "ms / ").concat((_this_expectedAdBreakDurationMs = this.expectedAdBreakDurationMs) !== null && _this_expectedAdBreakDurationMs !== void 0 ? _this_expectedAdBreakDurationMs : "?", "ms"));
510
+ }
511
+ }
512
+ },
513
+ {
514
+ key: "ensureLoadedAdFitsBudget",
515
+ value: function ensureLoadedAdFitsBudget(adController) {
516
+ var durationMs = this.getLoadedAdDurationMs(adController);
517
+ var fits = this.adFitsRemainingBudget(durationMs);
518
+ this.logAdDurationDecision(fits, durationMs, adController);
519
+ return fits;
520
+ }
521
+ },
522
+ {
523
+ key: "rejectLoadedAdForDuration",
524
+ value: function rejectLoadedAdForDuration(adController) {
525
+ try {
526
+ adController.destroy();
527
+ } catch (unused) {}
528
+ }
529
+ },
530
+ {
531
+ key: "markAdStarted",
532
+ value: function markAdStarted() {
533
+ this.currentAdIndex++;
534
+ this.totalAdsInBreak = this.currentAdIndex;
535
+ this.totalAdRequestsInBreak++;
536
+ this.lastAdRequestTime = Date.now();
537
+ }
538
+ },
539
+ {
540
+ key: "getRemainingAdMs",
541
+ value: function getRemainingAdMs() {
542
+ var wallClockRemaining = this.getWallClockRemainingAdMs();
543
+ var budgetRemaining = this.getDurationBudgetRemainingMs();
544
+ if (this.expectedAdBreakDurationMs == null) {
545
+ return wallClockRemaining;
546
+ }
547
+ return Math.min(wallClockRemaining, budgetRemaining);
548
+ }
549
+ },
550
+ {
551
+ key: "getMinRemainingMsToStartAd",
552
+ value: function getMinRemainingMsToStartAd() {
553
+ var configured = this.config.minRemainingMsToStartAd;
554
+ if (typeof configured === "number" && configured >= 0) {
555
+ return configured;
556
+ }
557
+ return this.defaultMinRemainingMsToStartAd;
558
+ }
559
+ },
560
+ {
561
+ key: "hasTimeToStartAnotherAd",
562
+ value: function hasTimeToStartAnotherAd() {
563
+ var minRemaining = this.getMinRemainingMsToStartAd();
564
+ return this.getWallClockRemainingAdMs() > minRemaining && this.getDurationBudgetRemainingMs() > minRemaining;
565
+ }
566
+ },
567
+ {
568
+ // --- Circuit breaker ---
569
+ key: "isTemporaryAdError",
570
+ value: function isTemporaryAdError(error) {
571
+ var _error_message;
572
+ var message = (error === null || error === void 0 ? void 0 : (_error_message = error.message) === null || _error_message === void 0 ? void 0 : _error_message.toLowerCase()) || "";
573
+ if (message.includes("no ads available") || message.includes("no fill") || message.includes("nofill")) {
574
+ return true;
575
+ }
576
+ if (message.includes("timeout")) {
577
+ return true;
578
+ }
579
+ if (message.includes("rate limit") || message.includes("too many requests")) {
580
+ return true;
581
+ }
582
+ if (message.includes("ad playback error")) {
583
+ return true;
584
+ }
585
+ return false;
586
+ }
587
+ },
588
+ {
589
+ key: "isUrlInCooldown",
590
+ value: function isUrlInCooldown(vastTagUrl) {
591
+ var failureTime = this.temporaryFailureUrls.get(vastTagUrl);
592
+ if (failureTime == null) {
593
+ return false;
594
+ }
595
+ var elapsed = Date.now() - failureTime;
596
+ if (elapsed >= this.temporaryFailureCooldownMs) {
597
+ this.temporaryFailureUrls.delete(vastTagUrl);
598
+ return false;
599
+ }
600
+ return true;
601
+ }
602
+ },
603
+ {
604
+ key: "cleanupTemporaryFailures",
605
+ value: function cleanupTemporaryFailures() {
606
+ var now = Date.now();
607
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
608
+ try {
609
+ for(var _iterator = this.temporaryFailureUrls[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
610
+ var _step_value = _sliced_to_array(_step.value, 2), url = _step_value[0], timestamp = _step_value[1];
611
+ if (now - timestamp >= this.temporaryFailureCooldownMs) {
612
+ this.temporaryFailureUrls.delete(url);
613
+ }
614
+ }
615
+ } catch (err) {
616
+ _didIteratorError = true;
617
+ _iteratorError = err;
618
+ } finally{
619
+ try {
620
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
621
+ _iterator.return();
622
+ }
623
+ } finally{
624
+ if (_didIteratorError) {
625
+ throw _iteratorError;
626
+ }
627
+ }
628
+ }
629
+ }
630
+ },
631
+ {
632
+ key: "isGamInCooldown",
633
+ value: function isGamInCooldown() {
634
+ return Date.now() < this.globalNoFillCooldownUntil;
635
+ }
636
+ },
637
+ {
638
+ key: "recordGamNoFill",
639
+ value: function recordGamNoFill() {
640
+ this.globalConsecutiveNoFills++;
641
+ if (this.globalConsecutiveNoFills >= this.globalNoFillThreshold) {
642
+ var exponent = this.globalConsecutiveNoFills - this.globalNoFillThreshold;
643
+ var backoff = Math.min(this.globalNoFillBackoffBaseMs * Math.pow(2, exponent), this.globalNoFillBackoffMaxMs);
644
+ this.globalNoFillCooldownUntil = Date.now() + backoff;
645
+ if (this.debug) {
646
+ console.log("[CIRCUIT-BREAKER] GAM cooldown activated: ".concat(backoff, "ms (").concat(this.globalConsecutiveNoFills, " consecutive no-fills)"));
647
+ }
648
+ }
649
+ }
650
+ },
651
+ {
652
+ key: "resetGamNoFillCounter",
653
+ value: function resetGamNoFillCounter() {
654
+ if (this.globalConsecutiveNoFills > 0 && this.debug) {
655
+ console.log("[CIRCUIT-BREAKER] Resetting no-fill counter (was ".concat(this.globalConsecutiveNoFills, ")"));
656
+ }
657
+ this.globalConsecutiveNoFills = 0;
658
+ this.globalNoFillCooldownUntil = 0;
659
+ }
660
+ },
661
+ {
662
+ key: "enforceGlobalRateLimit",
663
+ value: function enforceGlobalRateLimit() {
664
+ return _async_to_generator(function() {
665
+ var now, elapsed, waitMs;
666
+ return _ts_generator(this, function(_state) {
667
+ switch(_state.label){
668
+ case 0:
669
+ now = Date.now();
670
+ elapsed = now - this.globalLastGamRequestTime;
671
+ if (!(elapsed < this.globalMinGamIntervalMs)) return [
672
+ 3,
673
+ 2
674
+ ];
675
+ waitMs = this.globalMinGamIntervalMs - elapsed;
676
+ if (this.debug) {
677
+ console.log("[RATE-LIMIT] Waiting ".concat(waitMs, "ms before next GAM request"));
678
+ }
679
+ return [
680
+ 4,
681
+ new Promise(function(resolve) {
682
+ return setTimeout(resolve, waitMs);
683
+ })
684
+ ];
685
+ case 1:
686
+ _state.sent();
687
+ _state.label = 2;
688
+ case 2:
689
+ this.globalLastGamRequestTime = Date.now();
690
+ return [
691
+ 2
692
+ ];
693
+ }
694
+ });
695
+ }).call(this);
696
+ }
697
+ },
698
+ {
699
+ // --- Watchdog ---
700
+ key: "startAdRequestWatchdog",
701
+ value: function startAdRequestWatchdog(token) {
702
+ var _this = this;
703
+ var _this_config_adFailsafeTimeoutMs;
704
+ this.clearAdRequestWatchdog();
705
+ var timeoutMs = (_this_config_adFailsafeTimeoutMs = this.config.adFailsafeTimeoutMs) !== null && _this_config_adFailsafeTimeoutMs !== void 0 ? _this_config_adFailsafeTimeoutMs : 1e4;
706
+ this.adRequestWatchdogToken = token;
707
+ this.adRequestWatchdogId = window.setTimeout(function() {
708
+ if (_this.adRequestWatchdogToken !== token) {
709
+ return;
710
+ }
711
+ _this.adRequestWatchdogId = void 0;
712
+ _this.adRequestWatchdogToken = null;
713
+ if (_this.activeAdRequestToken === token) {
714
+ _this.activeAdRequestToken = null;
715
+ }
716
+ _this.logAdState("ad_request_timeout", {
717
+ token: token,
718
+ timeoutMs: timeoutMs
719
+ });
720
+ _this.callbacks.onWatchdogTimeout();
721
+ }, timeoutMs);
722
+ this.logAdState("ad_request_watchdog_started", {
723
+ token: token,
724
+ timeoutMs: timeoutMs
725
+ });
726
+ }
727
+ },
728
+ {
729
+ key: "clearAdRequestWatchdog",
730
+ value: function clearAdRequestWatchdog() {
731
+ if (this.adRequestWatchdogId != null) {
732
+ clearTimeout(this.adRequestWatchdogId);
733
+ this.adRequestWatchdogId = void 0;
734
+ }
735
+ if (this.adRequestWatchdogToken != null) {
736
+ this.logAdState("ad_request_watchdog_cleared", {
737
+ token: this.adRequestWatchdogToken
738
+ });
739
+ this.adRequestWatchdogToken = null;
740
+ }
741
+ }
742
+ },
743
+ {
744
+ // --- Failsafe ---
745
+ key: "startAdFailsafeTimer",
746
+ value: function startAdFailsafeTimer(token) {
747
+ var _this = this;
748
+ var _this_config_adFailsafeTimeoutMs;
749
+ this.clearAdFailsafeTimer();
750
+ var failsafeMs = (_this_config_adFailsafeTimeoutMs = this.config.adFailsafeTimeoutMs) !== null && _this_config_adFailsafeTimeoutMs !== void 0 ? _this_config_adFailsafeTimeoutMs : 1e4;
751
+ this.adFailsafeToken = token;
752
+ this.adFailsafeTimerId = window.setTimeout(function() {
753
+ if (_this.adFailsafeToken !== token) {
754
+ return;
755
+ }
756
+ _this.adFailsafeTimerId = void 0;
757
+ _this.adFailsafeToken = null;
758
+ if (_this.activeAdRequestToken === token) {
759
+ _this.activeAdRequestToken = null;
760
+ }
761
+ _this.logAdState("ad_failsafe_triggered", {
762
+ token: token,
763
+ failsafeMs: failsafeMs,
764
+ videoPaused: _this.video.paused
765
+ });
766
+ _this.callbacks.onFailsafeTimeout();
767
+ }, failsafeMs);
768
+ this.logAdState("ad_failsafe_started", {
769
+ token: token,
770
+ failsafeMs: failsafeMs
771
+ });
772
+ }
773
+ },
774
+ {
775
+ key: "clearAdFailsafeTimer",
776
+ value: function clearAdFailsafeTimer() {
777
+ if (this.adFailsafeTimerId != null) {
778
+ clearTimeout(this.adFailsafeTimerId);
779
+ this.logAdState("ad_failsafe_cleared", {
780
+ token: this.adFailsafeToken
781
+ });
782
+ this.adFailsafeTimerId = void 0;
783
+ }
784
+ this.adFailsafeToken = null;
785
+ }
786
+ },
787
+ {
788
+ key: "logAdState",
789
+ value: function logAdState(event) {
790
+ var extra = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, adPlayer = arguments.length > 2 ? arguments[2] : void 0, inAdBreak = arguments.length > 3 ? arguments[3] : void 0, showAds = arguments.length > 4 ? arguments[4] : void 0;
791
+ var _ref;
792
+ if (!this.debug) {
793
+ return;
794
+ }
795
+ console.log("[StormcloudVideoPlayer][AdState]", _object_spread({
796
+ event: event,
797
+ timestamp: /* @__PURE__ */ new Date().toISOString(),
798
+ showAds: showAds !== null && showAds !== void 0 ? showAds : false,
799
+ adPlaying: (_ref = adPlayer === null || adPlayer === void 0 ? void 0 : adPlayer.isAdPlaying()) !== null && _ref !== void 0 ? _ref : false,
800
+ inAdBreak: inAdBreak !== null && inAdBreak !== void 0 ? inAdBreak : false,
801
+ activeAdRequestToken: this.activeAdRequestToken
802
+ }, extra));
803
+ }
804
+ },
805
+ {
806
+ // --- Reset for ad break end ---
807
+ key: "resetForBreakEnd",
808
+ value: function resetForBreakEnd() {
809
+ this.clearAdStopTimer();
810
+ this.clearAdStartTimer();
811
+ this.clearAdRequestWatchdog();
812
+ this.clearAdFailsafeTimer();
813
+ this.activeAdRequestToken = null;
814
+ this.expectedAdBreakDurationMs = void 0;
815
+ this.currentAdBreakStartWallClockMs = void 0;
816
+ this.scteAdBreakEndWallClockMs = void 0;
817
+ this.adBreakPlayedDurationMs = 0;
818
+ this.currentAdIndex = 0;
819
+ this.totalAdsInBreak = 0;
820
+ this.consecutiveFailures = 0;
821
+ this.globalConsecutiveNoFills = 0;
822
+ this.adBreakEpoch++;
823
+ }
824
+ },
825
+ {
826
+ key: "destroy",
827
+ value: function destroy() {
828
+ this.clearAdStopTimer();
829
+ this.clearAdStartTimer();
830
+ this.clearAdFailsafeTimer();
831
+ this.clearAdRequestWatchdog();
832
+ }
833
+ }
834
+ ]);
835
+ return AdTimingService;
836
+ }();
837
+ // Annotate the CommonJS export names for ESM import in node:
838
+ 0 && (module.exports = {
839
+ AdTimingService: AdTimingService
840
+ });
841
+ //# sourceMappingURL=AdTimingService.cjs.map