stormcloud-video-player 0.3.58 → 0.3.60

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.
@@ -1797,6 +1797,16 @@ function createHlsAdPlayer(contentVideo, options) {
1797
1797
  });
1798
1798
  return bestMatch.file;
1799
1799
  }
1800
+ function isHlsMediaFile(mediaFile) {
1801
+ var type = mediaFile.type.toLowerCase();
1802
+ var url = mediaFile.url.toLowerCase();
1803
+ return type === "application/x-mpegurl" || type === "application/vnd.apple.mpegurl" || type.includes("mpegurl") || url.includes(".m3u8");
1804
+ }
1805
+ function isProgressiveMediaFile(mediaFile) {
1806
+ var type = mediaFile.type.toLowerCase();
1807
+ var url = mediaFile.url.toLowerCase();
1808
+ return type.startsWith("video/") || url.endsWith(".mp4") || url.includes(".mp4?");
1809
+ }
1800
1810
  function parseVastXml(xmlString) {
1801
1811
  try {
1802
1812
  var _xmlDoc_querySelector, _xmlDoc_querySelector1, _xmlDoc_querySelector_textContent, _xmlDoc_querySelector2;
@@ -1828,30 +1838,32 @@ function createHlsAdPlayer(contentVideo, options) {
1828
1838
  var width = mf.getAttribute("width") || "";
1829
1839
  var height = mf.getAttribute("height") || "";
1830
1840
  console.log("[HlsAdPlayer] MediaFile ".concat(index, ': type="').concat(type, '", url="').concat(url, '", width="').concat(width, '", height="').concat(height, '"'));
1831
- if (type === "application/x-mpegURL" || type.includes("m3u8")) {
1832
- if (!url) {
1833
- console.warn("[HlsAdPlayer] MediaFile ".concat(index, " has HLS type but empty URL"));
1834
- return;
1835
- }
1836
- var bitrateAttr = mf.getAttribute("bitrate");
1837
- var bitrateValue = bitrateAttr ? parseInt(bitrateAttr, 10) : void 0;
1838
- mediaFiles.push({
1839
- url: url,
1840
- type: type,
1841
- width: parseInt(width || "1920", 10),
1842
- height: parseInt(height || "1080", 10),
1843
- bitrate: bitrateValue && bitrateValue > 0 ? bitrateValue : void 0
1844
- });
1845
- console.log("[HlsAdPlayer] Added HLS MediaFile: ".concat(url));
1841
+ var mediaFile = {
1842
+ url: url,
1843
+ type: type,
1844
+ width: parseInt(width || "1920", 10),
1845
+ height: parseInt(height || "1080", 10),
1846
+ bitrate: void 0
1847
+ };
1848
+ if (!url) {
1849
+ console.warn("[HlsAdPlayer] MediaFile ".concat(index, " has empty URL"));
1850
+ return;
1851
+ }
1852
+ var bitrateAttr = mf.getAttribute("bitrate");
1853
+ var bitrateValue = bitrateAttr ? parseInt(bitrateAttr, 10) : void 0;
1854
+ mediaFile.bitrate = bitrateValue && bitrateValue > 0 ? bitrateValue : void 0;
1855
+ if (isHlsMediaFile(mediaFile) || isProgressiveMediaFile(mediaFile)) {
1856
+ mediaFiles.push(mediaFile);
1857
+ console.log("[HlsAdPlayer] Added ".concat(isHlsMediaFile(mediaFile) ? "HLS" : "progressive", " MediaFile: ").concat(url));
1846
1858
  } else {
1847
- console.log("[HlsAdPlayer] MediaFile ".concat(index, ' ignored (type="').concat(type, '" is not HLS)'));
1859
+ console.log("[HlsAdPlayer] MediaFile ".concat(index, ' ignored (type="').concat(type, '" is not supported)'));
1848
1860
  }
1849
1861
  });
1850
1862
  if (mediaFiles.length === 0) {
1851
1863
  if (isNoAdAvailable) {
1852
1864
  console.warn("[HlsAdPlayer] No ads available (VAST response indicates no ads)");
1853
1865
  } else {
1854
- console.warn("[HlsAdPlayer] No HLS media files found in VAST XML");
1866
+ console.warn("[HlsAdPlayer] No supported media files found in VAST XML");
1855
1867
  }
1856
1868
  return null;
1857
1869
  }
@@ -2148,7 +2160,7 @@ function createHlsAdPlayer(contentVideo, options) {
2148
2160
  },
2149
2161
  play: function play() {
2150
2162
  return _async_to_generator(function() {
2151
- var contentVolume, adVolume, mediaFile;
2163
+ var contentVolume, adVolume, mediaFile, isHlsAd;
2152
2164
  return _ts_generator(this, function(_state) {
2153
2165
  if (!currentAd) {
2154
2166
  console.warn("[HlsAdPlayer] Cannot play: No ad loaded (no ads available)");
@@ -2200,8 +2212,9 @@ function createHlsAdPlayer(contentVideo, options) {
2200
2212
  if (!mediaFile) {
2201
2213
  throw new Error("No media file available for ad");
2202
2214
  }
2203
- console.log("[HlsAdPlayer] Loading ad from: ".concat(mediaFile.url));
2204
- if (import_hls.default.isSupported()) {
2215
+ isHlsAd = isHlsMediaFile(mediaFile);
2216
+ console.log("[HlsAdPlayer] Loading ".concat(isHlsAd ? "HLS" : "progressive", " ad from: ").concat(mediaFile.url));
2217
+ if (isHlsAd && import_hls.default.isSupported()) {
2205
2218
  if (adHls) {
2206
2219
  adHls.destroy();
2207
2220
  }
@@ -2224,14 +2237,25 @@ function createHlsAdPlayer(contentVideo, options) {
2224
2237
  handleAdError();
2225
2238
  }
2226
2239
  });
2227
- } else if (adVideoElement.canPlayType("application/vnd.apple.mpegurl")) {
2240
+ } else if (isHlsAd && adVideoElement.canPlayType("application/vnd.apple.mpegurl")) {
2228
2241
  adVideoElement.src = mediaFile.url;
2229
2242
  adVideoElement.play().catch(function(error) {
2230
2243
  console.error("[HlsAdPlayer] Error starting ad playback:", error);
2231
2244
  handleAdError();
2232
2245
  });
2246
+ } else if (!isHlsAd && isProgressiveMediaFile(mediaFile)) {
2247
+ if (adHls) {
2248
+ adHls.destroy();
2249
+ adHls = void 0;
2250
+ }
2251
+ adVideoElement.src = mediaFile.url;
2252
+ adVideoElement.load();
2253
+ adVideoElement.play().catch(function(error) {
2254
+ console.error("[HlsAdPlayer] Error starting progressive ad playback:", error);
2255
+ handleAdError();
2256
+ });
2233
2257
  } else {
2234
- throw new Error("HLS not supported");
2258
+ throw new Error("Unsupported ad media file type: ".concat(mediaFile.type));
2235
2259
  }
2236
2260
  return [
2237
2261
  2,
@@ -3312,6 +3336,12 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3312
3336
  this.adRequestQueue = [];
3313
3337
  this.maxPlaceholderDurationMs = 5e3;
3314
3338
  this.isShowingPlaceholder = false;
3339
+ this.tsScte35Pids = /* @__PURE__ */ new Set();
3340
+ this.pmtPids = /* @__PURE__ */ new Set();
3341
+ this.processedTsScte35Sections = /* @__PURE__ */ new Set();
3342
+ this.pendingScte35Cues = /* @__PURE__ */ new Map();
3343
+ this.recentScte35CueKeys = /* @__PURE__ */ new Map();
3344
+ this.recentScte35CueLimit = 200;
3315
3345
  this.totalAdRequestsInBreak = 0;
3316
3346
  this.maxTotalAdRequestsPerBreak = 10;
3317
3347
  this.pendingAdBreak = null;
@@ -3581,7 +3611,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3581
3611
  if (_this.config.debugAdTiming) {
3582
3612
  console.log("[StormcloudVideoPlayer] \uD83C\uDFAF EARLY SCTE-35 DETECTION: Ad break marker found in fragment", i, "- starting pre-fetch (NOT playing yet)");
3583
3613
  }
3584
- _this.startAdPrefetch(marker, frag === null || frag === void 0 ? void 0 : frag.sn);
3614
+ _this.onScte35Cue(marker, _object_spread({}, _this.createScte35CueContext("manifest", "early", frag)));
3585
3615
  return;
3586
3616
  }
3587
3617
  }
@@ -3662,10 +3692,21 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3662
3692
  return _this.onId3Tag(tag);
3663
3693
  });
3664
3694
  });
3695
+ this.hls.on(import_hls2.default.Events.FRAG_LOADED, function(_evt, data) {
3696
+ var _data_frag;
3697
+ _this.processFragmentScte35Payload(data === null || data === void 0 ? void 0 : data.payload, typeof (data === null || data === void 0 ? void 0 : (_data_frag = data.frag) === null || _data_frag === void 0 ? void 0 : _data_frag.sn) === "number" ? data.frag.sn : void 0, _this.getFragmentStartSeconds(data === null || data === void 0 ? void 0 : data.frag));
3698
+ });
3699
+ this.hls.on(import_hls2.default.Events.FRAG_DECRYPTED, function(_evt, data) {
3700
+ var _data_frag;
3701
+ _this.processFragmentScte35Payload(data === null || data === void 0 ? void 0 : data.payload, typeof (data === null || data === void 0 ? void 0 : (_data_frag = data.frag) === null || _data_frag === void 0 ? void 0 : _data_frag.sn) === "number" ? data.frag.sn : void 0, _this.getFragmentStartSeconds(data === null || data === void 0 ? void 0 : data.frag));
3702
+ });
3665
3703
  this.hls.on(import_hls2.default.Events.FRAG_CHANGED, function(_evt, data) {
3666
3704
  var frag = data === null || data === void 0 ? void 0 : data.frag;
3667
3705
  var tagList = frag === null || frag === void 0 ? void 0 : frag.tagList;
3668
- if (!Array.isArray(tagList)) return;
3706
+ if (!Array.isArray(tagList)) {
3707
+ _this.activatePendingScte35CuesForFragment(frag);
3708
+ return;
3709
+ }
3669
3710
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3670
3711
  try {
3671
3712
  for(var _iterator = tagList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
@@ -3701,7 +3742,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3701
3742
  value: value
3702
3743
  }
3703
3744
  });
3704
- _this.onScte35Marker(marker);
3745
+ _this.onScte35Cue(marker, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3705
3746
  } else if (tag.includes("EXT-X-CUE-OUT")) {
3706
3747
  var durationSeconds = _this.parseCueOutDuration(value);
3707
3748
  var marker1 = _object_spread_props(_object_spread({
@@ -3714,15 +3755,15 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3714
3755
  value: value
3715
3756
  }
3716
3757
  });
3717
- _this.onScte35Marker(marker1);
3758
+ _this.onScte35Cue(marker1, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3718
3759
  } else if (tag.includes("EXT-X-CUE-IN")) {
3719
- _this.onScte35Marker({
3760
+ _this.onScte35Cue({
3720
3761
  type: "end",
3721
3762
  raw: {
3722
3763
  tag: tag,
3723
3764
  value: value
3724
3765
  }
3725
- });
3766
+ }, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3726
3767
  } else if (tag.includes("EXT-X-DATERANGE")) {
3727
3768
  var _attrs_CLASS;
3728
3769
  var attrs = _this.parseAttributeList(value);
@@ -3742,17 +3783,17 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3742
3783
  attrs: attrs
3743
3784
  }
3744
3785
  });
3745
- _this.onScte35Marker(marker2);
3786
+ _this.onScte35Cue(marker2, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3746
3787
  }
3747
3788
  if (hasScteIn) {
3748
- _this.onScte35Marker({
3789
+ _this.onScte35Cue({
3749
3790
  type: "end",
3750
3791
  raw: {
3751
3792
  tag: tag,
3752
3793
  value: value,
3753
3794
  attrs: attrs
3754
3795
  }
3755
- });
3796
+ }, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3756
3797
  }
3757
3798
  }
3758
3799
  }
@@ -3770,6 +3811,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3770
3811
  }
3771
3812
  }
3772
3813
  }
3814
+ _this.activatePendingScte35CuesForFragment(frag);
3773
3815
  });
3774
3816
  this.hls.on(import_hls2.default.Events.ERROR, function(_evt, data) {
3775
3817
  if (data === null || data === void 0 ? void 0 : data.fatal) {
@@ -4075,7 +4117,10 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4075
4117
  }
4076
4118
  var marker = this.parseScte35FromId3(tag);
4077
4119
  if (marker) {
4078
- this.onScte35Marker(marker);
4120
+ this.onScte35Cue(marker, {
4121
+ source: "id3",
4122
+ readiness: "playback"
4123
+ });
4079
4124
  }
4080
4125
  }
4081
4126
  },
@@ -4219,9 +4264,251 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4219
4264
  }
4220
4265
  }
4221
4266
  },
4267
+ {
4268
+ key: "createScte35CueContext",
4269
+ value: function createScte35CueContext(source, readiness, frag) {
4270
+ var context = {
4271
+ source: source,
4272
+ readiness: readiness
4273
+ };
4274
+ if (typeof (frag === null || frag === void 0 ? void 0 : frag.sn) === "number") {
4275
+ context.fragmentSn = frag.sn;
4276
+ }
4277
+ var fragmentStartSeconds = this.getFragmentStartSeconds(frag);
4278
+ if (fragmentStartSeconds !== void 0) {
4279
+ context.fragmentStartSeconds = fragmentStartSeconds;
4280
+ }
4281
+ return context;
4282
+ }
4283
+ },
4284
+ {
4285
+ key: "getFragmentStartSeconds",
4286
+ value: function getFragmentStartSeconds(frag) {
4287
+ var candidates = [
4288
+ frag === null || frag === void 0 ? void 0 : frag.start,
4289
+ frag === null || frag === void 0 ? void 0 : frag.startPTS
4290
+ ];
4291
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
4292
+ try {
4293
+ for(var _iterator = candidates[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
4294
+ var candidate = _step.value;
4295
+ if (typeof candidate === "number" && Number.isFinite(candidate)) {
4296
+ return candidate;
4297
+ }
4298
+ }
4299
+ } catch (err) {
4300
+ _didIteratorError = true;
4301
+ _iteratorError = err;
4302
+ } finally{
4303
+ try {
4304
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
4305
+ _iterator.return();
4306
+ }
4307
+ } finally{
4308
+ if (_didIteratorError) {
4309
+ throw _iteratorError;
4310
+ }
4311
+ }
4312
+ }
4313
+ return void 0;
4314
+ }
4315
+ },
4316
+ {
4317
+ key: "onScte35Cue",
4318
+ value: function onScte35Cue(marker, context) {
4319
+ var cue = this.createScte35Cue(marker, context);
4320
+ var pendingKey = this.getPendingScte35CueKey(cue);
4321
+ if (context.readiness === "early") {
4322
+ this.pendingScte35Cues.set(pendingKey, cue);
4323
+ this.trimRecentScte35CueMaps();
4324
+ if (marker.type === "start") {
4325
+ this.startAdPrefetch(marker, context.fragmentSn, cue.key);
4326
+ }
4327
+ return;
4328
+ }
4329
+ if (marker.type === "end" && !this.shouldAcceptScte35EndCue(cue)) {
4330
+ if (this.config.debugAdTiming) {
4331
+ console.log("[StormcloudVideoPlayer] Ignoring SCTE-35 end cue", {
4332
+ key: cue.key,
4333
+ activeScte35BreakKey: this.activeScte35BreakKey,
4334
+ inAdBreak: this.inAdBreak,
4335
+ source: cue.source
4336
+ });
4337
+ }
4338
+ return;
4339
+ }
4340
+ if (marker.type === "start" && this.hasRecentlyHandledScte35Cue(cue)) {
4341
+ if (this.config.debugAdTiming) {
4342
+ console.log("[StormcloudVideoPlayer] Ignoring duplicate SCTE-35 start cue", {
4343
+ key: cue.key,
4344
+ source: cue.source
4345
+ });
4346
+ }
4347
+ return;
4348
+ }
4349
+ this.pendingScte35Cues.delete(pendingKey);
4350
+ this.markScte35CueHandled(cue);
4351
+ this.onScte35Marker(marker, cue.key);
4352
+ }
4353
+ },
4354
+ {
4355
+ key: "createScte35Cue",
4356
+ value: function createScte35Cue(marker, context) {
4357
+ var cue = {
4358
+ marker: marker,
4359
+ source: context.source,
4360
+ readiness: context.readiness,
4361
+ key: this.getScte35CueKey(marker, context)
4362
+ };
4363
+ if (context.fragmentSn !== void 0) {
4364
+ cue.fragmentSn = context.fragmentSn;
4365
+ }
4366
+ if (context.fragmentStartSeconds !== void 0) {
4367
+ cue.fragmentStartSeconds = context.fragmentStartSeconds;
4368
+ }
4369
+ return cue;
4370
+ }
4371
+ },
4372
+ {
4373
+ key: "getScte35CueKey",
4374
+ value: function getScte35CueKey(marker, context) {
4375
+ var _raw_attrs, _raw_attrs1, _raw_attrs2;
4376
+ var raw = this.getScte35MarkerRaw(marker);
4377
+ var spliceEventId = this.toIdentityString(raw === null || raw === void 0 ? void 0 : raw.splice_event_id);
4378
+ if (spliceEventId) {
4379
+ return "splice:".concat(spliceEventId);
4380
+ }
4381
+ var segmentationEventId = this.toIdentityString(raw === null || raw === void 0 ? void 0 : raw.segmentation_event_id);
4382
+ if (segmentationEventId) {
4383
+ return "segmentation:".concat(segmentationEventId);
4384
+ }
4385
+ var daterangeId = this.toIdentityString(raw === null || raw === void 0 ? void 0 : (_raw_attrs = raw.attrs) === null || _raw_attrs === void 0 ? void 0 : _raw_attrs.ID);
4386
+ if (daterangeId) {
4387
+ return "daterange:".concat(daterangeId);
4388
+ }
4389
+ var scteValue = this.toIdentityString(raw === null || raw === void 0 ? void 0 : (_raw_attrs1 = raw.attrs) === null || _raw_attrs1 === void 0 ? void 0 : _raw_attrs1["SCTE35-OUT"]) || this.toIdentityString(raw === null || raw === void 0 ? void 0 : (_raw_attrs2 = raw.attrs) === null || _raw_attrs2 === void 0 ? void 0 : _raw_attrs2["SCTE35-IN"]);
4390
+ if (scteValue) {
4391
+ return "daterange-scte:".concat(scteValue);
4392
+ }
4393
+ if (typeof marker.ptsSeconds === "number" && Number.isFinite(marker.ptsSeconds)) {
4394
+ return "pts:".concat(Math.round(marker.ptsSeconds * 2) / 2);
4395
+ }
4396
+ if (context.fragmentSn !== void 0) {
4397
+ return "fragment:".concat(context.fragmentSn);
4398
+ }
4399
+ return "".concat(context.source, ":wallclock:").concat(Math.floor(Date.now() / 1e3));
4400
+ }
4401
+ },
4402
+ {
4403
+ key: "getPendingScte35CueKey",
4404
+ value: function getPendingScte35CueKey(cue) {
4405
+ return "".concat(cue.marker.type, ":").concat(cue.key, ":").concat(cue.fragmentSn !== void 0 ? cue.fragmentSn : "unknown");
4406
+ }
4407
+ },
4408
+ {
4409
+ key: "getScte35MarkerRaw",
4410
+ value: function getScte35MarkerRaw(marker) {
4411
+ return _type_of(marker.raw) === "object" && marker.raw !== null ? marker.raw : {};
4412
+ }
4413
+ },
4414
+ {
4415
+ key: "toIdentityString",
4416
+ value: function toIdentityString(value) {
4417
+ if (typeof value === "number" && Number.isFinite(value)) {
4418
+ return String(value);
4419
+ }
4420
+ if (typeof value === "string" && value.trim().length > 0) {
4421
+ return value.trim();
4422
+ }
4423
+ return void 0;
4424
+ }
4425
+ },
4426
+ {
4427
+ key: "isStrongScte35CueKey",
4428
+ value: function isStrongScte35CueKey(key) {
4429
+ return !!(key && (key.startsWith("splice:") || key.startsWith("segmentation:") || key.startsWith("daterange:") || key.startsWith("daterange-scte:")));
4430
+ }
4431
+ },
4432
+ {
4433
+ key: "hasRecentlyHandledScte35Cue",
4434
+ value: function hasRecentlyHandledScte35Cue(cue) {
4435
+ if (this.activeScte35BreakKey === cue.key || this.scheduledScte35BreakKey === cue.key) {
4436
+ return true;
4437
+ }
4438
+ var recentKey = "".concat(cue.marker.type, ":").concat(cue.key);
4439
+ return this.recentScte35CueKeys.has(recentKey);
4440
+ }
4441
+ },
4442
+ {
4443
+ key: "markScte35CueHandled",
4444
+ value: function markScte35CueHandled(cue) {
4445
+ this.recentScte35CueKeys.set("".concat(cue.marker.type, ":").concat(cue.key), Date.now());
4446
+ this.trimRecentScte35CueMaps();
4447
+ }
4448
+ },
4449
+ {
4450
+ key: "trimRecentScte35CueMaps",
4451
+ value: function trimRecentScte35CueMaps() {
4452
+ while(this.recentScte35CueKeys.size > this.recentScte35CueLimit){
4453
+ var firstKey = this.recentScte35CueKeys.keys().next().value;
4454
+ if (!firstKey) {
4455
+ break;
4456
+ }
4457
+ this.recentScte35CueKeys.delete(firstKey);
4458
+ }
4459
+ while(this.pendingScte35Cues.size > this.recentScte35CueLimit){
4460
+ var firstKey1 = this.pendingScte35Cues.keys().next().value;
4461
+ if (!firstKey1) {
4462
+ break;
4463
+ }
4464
+ this.pendingScte35Cues.delete(firstKey1);
4465
+ }
4466
+ }
4467
+ },
4468
+ {
4469
+ key: "shouldAcceptScte35EndCue",
4470
+ value: function shouldAcceptScte35EndCue(cue) {
4471
+ if (!this.inAdBreak) {
4472
+ return false;
4473
+ }
4474
+ if (this.isStrongScte35CueKey(this.activeScte35BreakKey) && this.isStrongScte35CueKey(cue.key) && this.activeScte35BreakKey !== cue.key) {
4475
+ return false;
4476
+ }
4477
+ return true;
4478
+ }
4479
+ },
4480
+ {
4481
+ key: "activatePendingScte35CuesForFragment",
4482
+ value: function activatePendingScte35CuesForFragment(frag) {
4483
+ var _this = this;
4484
+ var fragmentSn = typeof (frag === null || frag === void 0 ? void 0 : frag.sn) === "number" ? frag.sn : void 0;
4485
+ if (fragmentSn === void 0) {
4486
+ return;
4487
+ }
4488
+ var matchingCues = Array.from(this.pendingScte35Cues.entries()).filter(function(param) {
4489
+ var _param = _sliced_to_array(param, 2), cue = _param[1];
4490
+ return cue.fragmentSn === fragmentSn;
4491
+ });
4492
+ matchingCues.forEach(function(param) {
4493
+ var _param = _sliced_to_array(param, 2), pendingKey = _param[0], cue = _param[1];
4494
+ var _cue_fragmentStartSeconds;
4495
+ _this.pendingScte35Cues.delete(pendingKey);
4496
+ var context = {
4497
+ source: cue.source,
4498
+ readiness: "playback"
4499
+ };
4500
+ context.fragmentSn = fragmentSn;
4501
+ var fragmentStartSeconds = (_cue_fragmentStartSeconds = cue.fragmentStartSeconds) !== null && _cue_fragmentStartSeconds !== void 0 ? _cue_fragmentStartSeconds : _this.getFragmentStartSeconds(frag);
4502
+ if (fragmentStartSeconds !== void 0) {
4503
+ context.fragmentStartSeconds = fragmentStartSeconds;
4504
+ }
4505
+ _this.onScte35Cue(cue.marker, context);
4506
+ });
4507
+ }
4508
+ },
4222
4509
  {
4223
4510
  key: "onScte35Marker",
4224
- value: function onScte35Marker(marker) {
4511
+ value: function onScte35Marker(marker, cueKey) {
4225
4512
  if (this.config.debugAdTiming) {
4226
4513
  console.log("[StormcloudVideoPlayer] SCTE-35 marker detected:", {
4227
4514
  type: marker.type,
@@ -4235,13 +4522,6 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4235
4522
  if (marker.type === "start") {
4236
4523
  var _this_config_immediateManifestAds;
4237
4524
  var _this_pendingAdBreak;
4238
- if (!this.video.muted) {
4239
- this.video.muted = true;
4240
- this.video.volume = 0;
4241
- if (this.config.debugAdTiming) {
4242
- console.log("[StormcloudVideoPlayer] Muted video on SCTE start marker");
4243
- }
4244
- }
4245
4525
  if (this.inAdBreak) {
4246
4526
  if (marker.durationSeconds != null) {
4247
4527
  var newDurationMs = marker.durationSeconds * 1e3;
@@ -4265,10 +4545,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4265
4545
  sendAdDetectTracking(this.config.licenseKey, detectPayload).catch(function() {});
4266
4546
  }
4267
4547
  var hasPrefetchedAds = this.pendingAdBreak && this.pendingAdBreak.vastUrls.length > 0;
4268
- this.inAdBreak = true;
4269
4548
  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;
4270
- this.expectedAdBreakDurationMs = durationMs;
4271
- this.currentAdBreakStartWallClockMs = Date.now();
4272
4549
  var isManifestMarker = this.isManifestBasedMarker(marker);
4273
4550
  var forceImmediate = (_this_config_immediateManifestAds = this.config.immediateManifestAds) !== null && _this_config_immediateManifestAds !== void 0 ? _this_config_immediateManifestAds : true;
4274
4551
  if (this.config.debugAdTiming) {
@@ -4284,7 +4561,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4284
4561
  console.log("[StormcloudVideoPlayer] Starting ad immediately (manifest-based)".concat(hasPrefetchedAds ? " with prefetched ads" : ""));
4285
4562
  }
4286
4563
  this.clearAdStartTimer();
4287
- this.handleAdStart(marker);
4564
+ this.startScte35AdBreak(marker, cueKey, durationMs);
4288
4565
  } else if (typeof marker.ptsSeconds === "number") {
4289
4566
  var _this_config_driftToleranceMs;
4290
4567
  var tol = (_this_config_driftToleranceMs = this.config.driftToleranceMs) !== null && _this_config_driftToleranceMs !== void 0 ? _this_config_driftToleranceMs : 1e3;
@@ -4304,23 +4581,22 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4304
4581
  if (this.config.debugAdTiming) {
4305
4582
  console.log("[StormcloudVideoPlayer] Scheduling ad start in ".concat(deltaMs, "ms"));
4306
4583
  }
4307
- this.scheduleAdStartIn(deltaMs);
4584
+ this.expectedAdBreakDurationMs = durationMs;
4585
+ this.scheduledScte35BreakKey = cueKey;
4586
+ this.scheduleAdStartIn(deltaMs, marker, cueKey);
4308
4587
  } else {
4309
4588
  if (this.config.debugAdTiming) {
4310
4589
  console.log("[StormcloudVideoPlayer] Starting ad immediately (within tolerance)");
4311
4590
  }
4312
4591
  this.clearAdStartTimer();
4313
- this.handleAdStart(marker);
4592
+ this.startScte35AdBreak(marker, cueKey, durationMs);
4314
4593
  }
4315
4594
  } else {
4316
4595
  if (this.config.debugAdTiming) {
4317
4596
  console.log("[StormcloudVideoPlayer] Starting ad immediately (fallback)");
4318
4597
  }
4319
4598
  this.clearAdStartTimer();
4320
- this.handleAdStart(marker);
4321
- }
4322
- if (this.expectedAdBreakDurationMs != null) {
4323
- this.scheduleAdStopCountdown(this.expectedAdBreakDurationMs);
4599
+ this.startScte35AdBreak(marker, cueKey, durationMs);
4324
4600
  }
4325
4601
  return;
4326
4602
  }
@@ -4376,6 +4652,27 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4376
4652
  }
4377
4653
  }
4378
4654
  },
4655
+ {
4656
+ key: "startScte35AdBreak",
4657
+ value: function startScte35AdBreak(marker, cueKey, durationMs) {
4658
+ if (!this.video.muted) {
4659
+ this.video.muted = true;
4660
+ this.video.volume = 0;
4661
+ if (this.config.debugAdTiming) {
4662
+ console.log("[StormcloudVideoPlayer] Muted video on SCTE start marker");
4663
+ }
4664
+ }
4665
+ this.inAdBreak = true;
4666
+ this.activeScte35BreakKey = cueKey !== null && cueKey !== void 0 ? cueKey : this.pendingScte35CueKey;
4667
+ this.scheduledScte35BreakKey = void 0;
4668
+ this.expectedAdBreakDurationMs = durationMs;
4669
+ this.currentAdBreakStartWallClockMs = Date.now();
4670
+ this.handleAdStart(marker);
4671
+ if (this.expectedAdBreakDurationMs != null) {
4672
+ this.scheduleAdStopCountdown(this.expectedAdBreakDurationMs);
4673
+ }
4674
+ }
4675
+ },
4379
4676
  {
4380
4677
  key: "parseCueOutDuration",
4381
4678
  value: function parseCueOutDuration(value) {
@@ -4459,6 +4756,336 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4459
4756
  return false;
4460
4757
  }
4461
4758
  },
4759
+ {
4760
+ key: "normalizeFragmentPayload",
4761
+ value: function normalizeFragmentPayload(payload) {
4762
+ if (_instanceof(payload, Uint8Array)) {
4763
+ return payload;
4764
+ }
4765
+ if (_instanceof(payload, ArrayBuffer)) {
4766
+ return new Uint8Array(payload);
4767
+ }
4768
+ if (ArrayBuffer.isView(payload)) {
4769
+ var view = payload;
4770
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
4771
+ }
4772
+ return void 0;
4773
+ }
4774
+ },
4775
+ {
4776
+ key: "processFragmentScte35Payload",
4777
+ value: function processFragmentScte35Payload(payloadData, fragmentSn, fragmentStartSeconds) {
4778
+ var _this = this;
4779
+ var payload = this.normalizeFragmentPayload(payloadData);
4780
+ if (!payload) {
4781
+ return;
4782
+ }
4783
+ var markers = this.parseScte35FromTsPackets(payload);
4784
+ markers.forEach(function(marker) {
4785
+ var context = {
4786
+ source: "ts",
4787
+ readiness: "early"
4788
+ };
4789
+ if (fragmentSn !== void 0) {
4790
+ context.fragmentSn = fragmentSn;
4791
+ }
4792
+ if (fragmentStartSeconds !== void 0) {
4793
+ context.fragmentStartSeconds = fragmentStartSeconds;
4794
+ }
4795
+ _this.onScte35Cue(marker, context);
4796
+ });
4797
+ }
4798
+ },
4799
+ {
4800
+ key: "parseScte35FromTsPackets",
4801
+ value: function parseScte35FromTsPackets(data) {
4802
+ var packetInfo = this.detectTsPacketFormat(data);
4803
+ if (!packetInfo) {
4804
+ return [];
4805
+ }
4806
+ var markers = [];
4807
+ var sectionAssemblers = /* @__PURE__ */ new Map();
4808
+ for(var packetStart = packetInfo.offset; packetStart + packetInfo.packetSize <= data.length; packetStart += packetInfo.packetSize){
4809
+ var tsStart = packetStart + packetInfo.tsPayloadOffset;
4810
+ if (data[tsStart] !== 71) {
4811
+ continue;
4812
+ }
4813
+ var secondByte = data[tsStart + 1];
4814
+ var thirdByte = data[tsStart + 2];
4815
+ var fourthByte = data[tsStart + 3];
4816
+ if (secondByte == null || thirdByte == null || fourthByte == null || (secondByte & 128) !== 0) {
4817
+ continue;
4818
+ }
4819
+ var payloadUnitStart = (secondByte & 64) !== 0;
4820
+ var pid = (secondByte & 31) << 8 | thirdByte;
4821
+ var adaptationFieldControl = fourthByte >> 4 & 3;
4822
+ var hasPayload = adaptationFieldControl === 1 || adaptationFieldControl === 3;
4823
+ if (!hasPayload) {
4824
+ continue;
4825
+ }
4826
+ var payloadStart = tsStart + 4;
4827
+ if (adaptationFieldControl === 3) {
4828
+ var adaptationLength = data[payloadStart];
4829
+ if (adaptationLength == null) {
4830
+ continue;
4831
+ }
4832
+ payloadStart += 1 + adaptationLength;
4833
+ }
4834
+ var payloadEnd = Math.min(tsStart + 188, data.length);
4835
+ if (payloadStart >= payloadEnd || payloadStart >= data.length) {
4836
+ continue;
4837
+ }
4838
+ var payload = data.subarray(payloadStart, payloadEnd);
4839
+ if (pid === 0) {
4840
+ this.parsePatSection(payload, payloadUnitStart);
4841
+ } else if (this.pmtPids.has(pid)) {
4842
+ this.parsePmtSection(payload, payloadUnitStart);
4843
+ } else if (this.tsScte35Pids.has(pid)) {
4844
+ this.collectScte35SectionsFromPayload(pid, payload, payloadUnitStart, sectionAssemblers, markers);
4845
+ }
4846
+ }
4847
+ return markers;
4848
+ }
4849
+ },
4850
+ {
4851
+ key: "detectTsPacketFormat",
4852
+ value: function detectTsPacketFormat(data) {
4853
+ var packetSizes = [
4854
+ 188,
4855
+ 192,
4856
+ 204
4857
+ ];
4858
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
4859
+ try {
4860
+ for(var _iterator = packetSizes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
4861
+ var packetSize = _step.value;
4862
+ var tsPayloadOffset = packetSize === 192 ? 4 : 0;
4863
+ for(var offset = 0; offset < packetSize; offset++){
4864
+ var matchedPackets = 0;
4865
+ for(var packetStart = offset; packetStart + tsPayloadOffset < data.length && matchedPackets < 5; packetStart += packetSize){
4866
+ if (data[packetStart + tsPayloadOffset] !== 71) {
4867
+ break;
4868
+ }
4869
+ matchedPackets++;
4870
+ }
4871
+ if (matchedPackets >= 2) {
4872
+ return {
4873
+ offset: offset,
4874
+ packetSize: packetSize,
4875
+ tsPayloadOffset: tsPayloadOffset
4876
+ };
4877
+ }
4878
+ }
4879
+ }
4880
+ } catch (err) {
4881
+ _didIteratorError = true;
4882
+ _iteratorError = err;
4883
+ } finally{
4884
+ try {
4885
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
4886
+ _iterator.return();
4887
+ }
4888
+ } finally{
4889
+ if (_didIteratorError) {
4890
+ throw _iteratorError;
4891
+ }
4892
+ }
4893
+ }
4894
+ return void 0;
4895
+ }
4896
+ },
4897
+ {
4898
+ key: "parsePatSection",
4899
+ value: function parsePatSection(payload, payloadUnitStart) {
4900
+ var sectionStart = this.getPsiSectionStart(payload, payloadUnitStart);
4901
+ if (sectionStart == null || sectionStart + 8 >= payload.length) {
4902
+ return;
4903
+ }
4904
+ if (payload[sectionStart] !== 0) {
4905
+ return;
4906
+ }
4907
+ var sectionLength = this.getPsiSectionLength(payload, sectionStart);
4908
+ var sectionEnd = Math.min(payload.length, sectionStart + 3 + sectionLength);
4909
+ for(var offset = sectionStart + 8; offset + 4 <= sectionEnd - 4; offset += 4){
4910
+ var _payload_offset, _payload_, _payload_1, _payload_2;
4911
+ var programNumber = ((_payload_offset = payload[offset]) !== null && _payload_offset !== void 0 ? _payload_offset : 0) << 8 | ((_payload_ = payload[offset + 1]) !== null && _payload_ !== void 0 ? _payload_ : 0);
4912
+ var pid = (((_payload_1 = payload[offset + 2]) !== null && _payload_1 !== void 0 ? _payload_1 : 0) & 31) << 8 | ((_payload_2 = payload[offset + 3]) !== null && _payload_2 !== void 0 ? _payload_2 : 0);
4913
+ if (programNumber !== 0) {
4914
+ this.pmtPids.add(pid);
4915
+ }
4916
+ }
4917
+ }
4918
+ },
4919
+ {
4920
+ key: "parsePmtSection",
4921
+ value: function parsePmtSection(payload, payloadUnitStart) {
4922
+ var _payload_, _payload_1;
4923
+ var sectionStart = this.getPsiSectionStart(payload, payloadUnitStart);
4924
+ if (sectionStart == null || sectionStart + 12 >= payload.length) {
4925
+ return;
4926
+ }
4927
+ if (payload[sectionStart] !== 2) {
4928
+ return;
4929
+ }
4930
+ var sectionLength = this.getPsiSectionLength(payload, sectionStart);
4931
+ var sectionEnd = Math.min(payload.length, sectionStart + 3 + sectionLength);
4932
+ var programInfoLength = (((_payload_ = payload[sectionStart + 10]) !== null && _payload_ !== void 0 ? _payload_ : 0) & 15) << 8 | ((_payload_1 = payload[sectionStart + 11]) !== null && _payload_1 !== void 0 ? _payload_1 : 0);
4933
+ var offset = sectionStart + 12 + programInfoLength;
4934
+ while(offset + 5 <= sectionEnd - 4){
4935
+ var _payload_offset, _payload_2, _payload_3, _payload_4, _payload_5;
4936
+ var streamType = (_payload_offset = payload[offset]) !== null && _payload_offset !== void 0 ? _payload_offset : 0;
4937
+ var elementaryPid = (((_payload_2 = payload[offset + 1]) !== null && _payload_2 !== void 0 ? _payload_2 : 0) & 31) << 8 | ((_payload_3 = payload[offset + 2]) !== null && _payload_3 !== void 0 ? _payload_3 : 0);
4938
+ var esInfoLength = (((_payload_4 = payload[offset + 3]) !== null && _payload_4 !== void 0 ? _payload_4 : 0) & 15) << 8 | ((_payload_5 = payload[offset + 4]) !== null && _payload_5 !== void 0 ? _payload_5 : 0);
4939
+ var descriptorStart = offset + 5;
4940
+ var descriptorEnd = Math.min(descriptorStart + esInfoLength, sectionEnd - 4);
4941
+ if (streamType === 134 || streamType === 6 && this.descriptorsIdentifyScte35(payload, descriptorStart, descriptorEnd)) {
4942
+ this.tsScte35Pids.add(elementaryPid);
4943
+ }
4944
+ offset = descriptorStart + esInfoLength;
4945
+ }
4946
+ }
4947
+ },
4948
+ {
4949
+ key: "descriptorsIdentifyScte35",
4950
+ value: function descriptorsIdentifyScte35(data, start, end) {
4951
+ var offset = start;
4952
+ while(offset + 2 <= end){
4953
+ var _data_offset, _data_, _data_descriptorStart, _data_1, _data_2, _data_3;
4954
+ var tag = (_data_offset = data[offset]) !== null && _data_offset !== void 0 ? _data_offset : 0;
4955
+ var length = (_data_ = data[offset + 1]) !== null && _data_ !== void 0 ? _data_ : 0;
4956
+ var descriptorStart = offset + 2;
4957
+ var descriptorEnd = descriptorStart + length;
4958
+ if (descriptorEnd > end) {
4959
+ return false;
4960
+ }
4961
+ var registration = length >= 4 ? String.fromCharCode((_data_descriptorStart = data[descriptorStart]) !== null && _data_descriptorStart !== void 0 ? _data_descriptorStart : 0, (_data_1 = data[descriptorStart + 1]) !== null && _data_1 !== void 0 ? _data_1 : 0, (_data_2 = data[descriptorStart + 2]) !== null && _data_2 !== void 0 ? _data_2 : 0, (_data_3 = data[descriptorStart + 3]) !== null && _data_3 !== void 0 ? _data_3 : 0) : "";
4962
+ if (tag === 138 || registration === "CUEI" || registration === "SCTE") {
4963
+ return true;
4964
+ }
4965
+ offset = descriptorEnd;
4966
+ }
4967
+ return false;
4968
+ }
4969
+ },
4970
+ {
4971
+ key: "collectScte35SectionsFromPayload",
4972
+ value: function collectScte35SectionsFromPayload(pid, payload, payloadUnitStart, assemblers, markers) {
4973
+ if (payload.length === 0) {
4974
+ return;
4975
+ }
4976
+ var offset = 0;
4977
+ if (payloadUnitStart) {
4978
+ var _payload_;
4979
+ var pointerField = (_payload_ = payload[0]) !== null && _payload_ !== void 0 ? _payload_ : 0;
4980
+ var previousAssembler = assemblers.get(pid);
4981
+ if (previousAssembler && pointerField > 0) {
4982
+ this.appendScte35Bytes(pid, payload.subarray(1, Math.min(1 + pointerField, payload.length)), assemblers, markers);
4983
+ }
4984
+ assemblers.delete(pid);
4985
+ offset = 1 + pointerField;
4986
+ } else {
4987
+ this.appendScte35Bytes(pid, payload, assemblers, markers);
4988
+ return;
4989
+ }
4990
+ while(offset + 3 <= payload.length){
4991
+ var tableId = payload[offset];
4992
+ if (tableId == null || tableId === 255) {
4993
+ return;
4994
+ }
4995
+ var sectionLength = this.getPsiSectionLength(payload, offset);
4996
+ var totalLength = 3 + sectionLength;
4997
+ if (sectionLength <= 0 || totalLength <= 3) {
4998
+ return;
4999
+ }
5000
+ if (offset + totalLength <= payload.length) {
5001
+ this.addScte35SectionMarker(payload.subarray(offset, offset + totalLength), markers);
5002
+ offset += totalLength;
5003
+ } else {
5004
+ assemblers.set(pid, {
5005
+ bytes: Array.from(payload.subarray(offset)),
5006
+ expectedLength: totalLength
5007
+ });
5008
+ return;
5009
+ }
5010
+ }
5011
+ }
5012
+ },
5013
+ {
5014
+ key: "appendScte35Bytes",
5015
+ value: function appendScte35Bytes(pid, bytes, assemblers, markers) {
5016
+ var assembler = assemblers.get(pid);
5017
+ if (!assembler) {
5018
+ return;
5019
+ }
5020
+ for(var i = 0; i < bytes.length && assembler.bytes.length < assembler.expectedLength; i++){
5021
+ var _bytes_i;
5022
+ assembler.bytes.push((_bytes_i = bytes[i]) !== null && _bytes_i !== void 0 ? _bytes_i : 0);
5023
+ }
5024
+ if (assembler.bytes.length >= assembler.expectedLength) {
5025
+ assemblers.delete(pid);
5026
+ this.addScte35SectionMarker(new Uint8Array(assembler.bytes), markers);
5027
+ }
5028
+ }
5029
+ },
5030
+ {
5031
+ key: "addScte35SectionMarker",
5032
+ value: function addScte35SectionMarker(section, markers) {
5033
+ if (section[0] !== 252 || this.hasProcessedTsScte35Section(section)) {
5034
+ return;
5035
+ }
5036
+ var marker = this.parseScte35Binary(section);
5037
+ if (!marker) {
5038
+ return;
5039
+ }
5040
+ marker.raw = _object_spread_props(_object_spread({}, _type_of(marker.raw) === "object" && marker.raw ? marker.raw : {}), {
5041
+ source: "ts-packet"
5042
+ });
5043
+ markers.push(marker);
5044
+ }
5045
+ },
5046
+ {
5047
+ key: "hasProcessedTsScte35Section",
5048
+ value: function hasProcessedTsScte35Section(section) {
5049
+ var checksum = 0;
5050
+ for(var i = 0; i < section.length; i++){
5051
+ var _section_i;
5052
+ checksum = checksum * 31 + ((_section_i = section[i]) !== null && _section_i !== void 0 ? _section_i : 0) >>> 0;
5053
+ }
5054
+ var key = "".concat(section.length, ":").concat(checksum);
5055
+ if (this.processedTsScte35Sections.has(key)) {
5056
+ return true;
5057
+ }
5058
+ this.processedTsScte35Sections.add(key);
5059
+ if (this.processedTsScte35Sections.size > 200) {
5060
+ var firstKey = this.processedTsScte35Sections.values().next().value;
5061
+ if (firstKey) {
5062
+ this.processedTsScte35Sections.delete(firstKey);
5063
+ }
5064
+ }
5065
+ return false;
5066
+ }
5067
+ },
5068
+ {
5069
+ key: "getPsiSectionStart",
5070
+ value: function getPsiSectionStart(payload, payloadUnitStart) {
5071
+ if (!payloadUnitStart) {
5072
+ return void 0;
5073
+ }
5074
+ var pointerField = payload[0];
5075
+ if (pointerField == null) {
5076
+ return void 0;
5077
+ }
5078
+ var sectionStart = 1 + pointerField;
5079
+ return sectionStart < payload.length ? sectionStart : void 0;
5080
+ }
5081
+ },
5082
+ {
5083
+ key: "getPsiSectionLength",
5084
+ value: function getPsiSectionLength(data, offset) {
5085
+ var _data_, _data_1;
5086
+ return (((_data_ = data[offset + 1]) !== null && _data_ !== void 0 ? _data_ : 0) & 15) << 8 | ((_data_1 = data[offset + 2]) !== null && _data_1 !== void 0 ? _data_1 : 0);
5087
+ }
5088
+ },
4462
5089
  {
4463
5090
  key: "parseScte35Binary",
4464
5091
  value: function parseScte35Binary(data) {
@@ -4498,10 +5125,28 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4498
5125
  value: function skipBits(n) {
4499
5126
  this.readBits(n);
4500
5127
  }
5128
+ },
5129
+ {
5130
+ key: "bytePosition",
5131
+ get: function get() {
5132
+ return this.bitPos === 0 ? this.bytePos : this.bytePos + 1;
5133
+ }
4501
5134
  }
4502
5135
  ]);
4503
5136
  return BitReader;
4504
5137
  }();
5138
+ var readSpliceTime = function readSpliceTime(r2, ptsAdjustmentTicks2) {
5139
+ var timeSpecifiedFlag = r2.readBits(1) === 1;
5140
+ if (!timeSpecifiedFlag) {
5141
+ r2.readBits(7);
5142
+ return void 0;
5143
+ }
5144
+ r2.readBits(6);
5145
+ var high = r2.readBits(1);
5146
+ var low = r2.readBits(32);
5147
+ var ptsTicks = (high * 4294967296 + low + ptsAdjustmentTicks2) % 8589934592;
5148
+ return ptsTicks / 9e4;
5149
+ };
4505
5150
  var r = new BitReader(data);
4506
5151
  var tableId = r.readBits(8);
4507
5152
  if (tableId !== 252) return void 0;
@@ -4509,77 +5154,206 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4509
5154
  r.readBits(1);
4510
5155
  r.readBits(2);
4511
5156
  var sectionLength = r.readBits(12);
5157
+ if (sectionLength <= 0 || data.length < 3 + sectionLength) {
5158
+ return void 0;
5159
+ }
4512
5160
  r.readBits(8);
4513
5161
  r.readBits(1);
4514
5162
  r.readBits(6);
4515
5163
  var ptsAdjHigh = r.readBits(1);
4516
5164
  var ptsAdjLow = r.readBits(32);
4517
- void ptsAdjHigh;
4518
- void ptsAdjLow;
5165
+ var ptsAdjustmentTicks = ptsAdjHigh * 4294967296 + ptsAdjLow;
4519
5166
  r.readBits(8);
4520
5167
  r.readBits(12);
4521
5168
  var spliceCommandLength = r.readBits(12);
4522
5169
  var spliceCommandType = r.readBits(8);
4523
- if (spliceCommandType !== 5) {
5170
+ var markerType;
5171
+ var durationSeconds = void 0;
5172
+ var ptsSeconds = void 0;
5173
+ var spliceEventId = void 0;
5174
+ var commandBodyStart = r.bytePosition;
5175
+ if (spliceCommandType === 5) {
5176
+ spliceEventId = r.readBits(32);
5177
+ var cancel = r.readBits(1) === 1;
5178
+ r.readBits(7);
5179
+ if (cancel) return void 0;
5180
+ var outOfNetwork = r.readBits(1) === 1;
5181
+ var programSpliceFlag = r.readBits(1) === 1;
5182
+ var durationFlag = r.readBits(1) === 1;
5183
+ var spliceImmediateFlag = r.readBits(1) === 1;
5184
+ r.readBits(4);
5185
+ markerType = outOfNetwork ? "start" : "end";
5186
+ if (programSpliceFlag && !spliceImmediateFlag) {
5187
+ ptsSeconds = readSpliceTime(r, ptsAdjustmentTicks);
5188
+ } else if (!programSpliceFlag) {
5189
+ var componentCount = r.readBits(8);
5190
+ for(var i = 0; i < componentCount; i++){
5191
+ r.readBits(8);
5192
+ if (!spliceImmediateFlag) {
5193
+ var componentPtsSeconds = readSpliceTime(r, ptsAdjustmentTicks);
5194
+ if (ptsSeconds === void 0) {
5195
+ ptsSeconds = componentPtsSeconds;
5196
+ }
5197
+ }
5198
+ }
5199
+ }
5200
+ if (durationFlag) {
5201
+ r.readBits(6);
5202
+ r.readBits(1);
5203
+ var high = r.readBits(1);
5204
+ var low = r.readBits(32);
5205
+ var durationTicks = high * 4294967296 + low;
5206
+ durationSeconds = durationTicks / 9e4;
5207
+ }
5208
+ r.readBits(16);
5209
+ r.readBits(8);
5210
+ r.readBits(8);
5211
+ } else if (spliceCommandType === 6) {
5212
+ ptsSeconds = readSpliceTime(r, ptsAdjustmentTicks);
5213
+ } else {
4524
5214
  return void 0;
4525
5215
  }
4526
- r.readBits(32);
4527
- var cancel = r.readBits(1) === 1;
4528
- r.readBits(7);
4529
- if (cancel) return void 0;
4530
- var outOfNetwork = r.readBits(1) === 1;
4531
- var programSpliceFlag = r.readBits(1) === 1;
4532
- var durationFlag = r.readBits(1) === 1;
4533
- var spliceImmediateFlag = r.readBits(1) === 1;
4534
- r.readBits(4);
4535
- if (programSpliceFlag && !spliceImmediateFlag) {
4536
- var timeSpecifiedFlag = r.readBits(1) === 1;
4537
- if (timeSpecifiedFlag) {
4538
- r.readBits(6);
4539
- r.readBits(33);
4540
- } else {
4541
- r.readBits(7);
4542
- }
4543
- } else if (!programSpliceFlag) {
4544
- var componentCount = r.readBits(8);
4545
- for(var i = 0; i < componentCount; i++){
4546
- r.readBits(8);
4547
- if (!spliceImmediateFlag) {
4548
- var timeSpecifiedFlag1 = r.readBits(1) === 1;
4549
- if (timeSpecifiedFlag1) {
4550
- r.readBits(6);
4551
- r.readBits(33);
4552
- } else {
4553
- r.readBits(7);
4554
- }
5216
+ var expectedDescriptorOffset = spliceCommandLength === 4095 ? r.bytePosition : commandBodyStart + spliceCommandLength;
5217
+ var descriptorMarker = this.parseScte35SegmentationDescriptors(data, expectedDescriptorOffset);
5218
+ if (descriptorMarker) {
5219
+ markerType = descriptorMarker.type;
5220
+ if (durationSeconds === void 0) {
5221
+ durationSeconds = descriptorMarker.durationSeconds;
5222
+ }
5223
+ }
5224
+ if (!markerType) {
5225
+ return void 0;
5226
+ }
5227
+ var marker = _object_spread_props(_object_spread({
5228
+ type: markerType
5229
+ }, ptsSeconds !== void 0 ? {
5230
+ ptsSeconds: ptsSeconds
5231
+ } : {}, durationSeconds !== void 0 ? {
5232
+ durationSeconds: durationSeconds
5233
+ } : {}), {
5234
+ raw: _object_spread({
5235
+ splice_command_type: spliceCommandType
5236
+ }, spliceEventId !== void 0 ? {
5237
+ splice_event_id: spliceEventId
5238
+ } : {}, (descriptorMarker === null || descriptorMarker === void 0 ? void 0 : descriptorMarker.segmentationEventId) !== void 0 ? {
5239
+ segmentation_event_id: descriptorMarker.segmentationEventId
5240
+ } : {})
5241
+ });
5242
+ return marker;
5243
+ }
5244
+ },
5245
+ {
5246
+ key: "parseScte35SegmentationDescriptors",
5247
+ value: function parseScte35SegmentationDescriptors(data, offset) {
5248
+ var _data_offset, _data_;
5249
+ if (offset + 2 > data.length) {
5250
+ return void 0;
5251
+ }
5252
+ var descriptorLoopLength = ((_data_offset = data[offset]) !== null && _data_offset !== void 0 ? _data_offset : 0) << 8 | ((_data_ = data[offset + 1]) !== null && _data_ !== void 0 ? _data_ : 0);
5253
+ var descriptorOffset = offset + 2;
5254
+ var descriptorEnd = Math.min(data.length, descriptorOffset + descriptorLoopLength);
5255
+ while(descriptorOffset + 2 <= descriptorEnd){
5256
+ var _data_descriptorOffset, _data_1;
5257
+ var descriptorTag = (_data_descriptorOffset = data[descriptorOffset]) !== null && _data_descriptorOffset !== void 0 ? _data_descriptorOffset : 0;
5258
+ var descriptorLength = (_data_1 = data[descriptorOffset + 1]) !== null && _data_1 !== void 0 ? _data_1 : 0;
5259
+ var descriptorDataStart = descriptorOffset + 2;
5260
+ var descriptorDataEnd = descriptorDataStart + descriptorLength;
5261
+ if (descriptorDataEnd > descriptorEnd) {
5262
+ return void 0;
5263
+ }
5264
+ if (descriptorTag === 2) {
5265
+ var descriptorMarker = this.parseScte35SegmentationDescriptor(data.subarray(descriptorDataStart, descriptorDataEnd));
5266
+ if (descriptorMarker) {
5267
+ return descriptorMarker;
4555
5268
  }
4556
5269
  }
5270
+ descriptorOffset = descriptorDataEnd;
5271
+ }
5272
+ return void 0;
5273
+ }
5274
+ },
5275
+ {
5276
+ key: "parseScte35SegmentationDescriptor",
5277
+ value: function parseScte35SegmentationDescriptor(descriptor) {
5278
+ var _descriptor_, _descriptor_1, _descriptor_2, _descriptor_3, _descriptor_4, _descriptor_5, _descriptor_6, _descriptor_7, _descriptor_8, _descriptor_9, _descriptor_offset, _descriptor_offset1;
5279
+ if (descriptor.length < 11) {
5280
+ return void 0;
5281
+ }
5282
+ var identifier = String.fromCharCode((_descriptor_ = descriptor[0]) !== null && _descriptor_ !== void 0 ? _descriptor_ : 0, (_descriptor_1 = descriptor[1]) !== null && _descriptor_1 !== void 0 ? _descriptor_1 : 0, (_descriptor_2 = descriptor[2]) !== null && _descriptor_2 !== void 0 ? _descriptor_2 : 0, (_descriptor_3 = descriptor[3]) !== null && _descriptor_3 !== void 0 ? _descriptor_3 : 0);
5283
+ if (identifier !== "CUEI") {
5284
+ return void 0;
5285
+ }
5286
+ var segmentationEventId = ((_descriptor_4 = descriptor[4]) !== null && _descriptor_4 !== void 0 ? _descriptor_4 : 0) * 16777216 + (((_descriptor_5 = descriptor[5]) !== null && _descriptor_5 !== void 0 ? _descriptor_5 : 0) << 16) + (((_descriptor_6 = descriptor[6]) !== null && _descriptor_6 !== void 0 ? _descriptor_6 : 0) << 8) + ((_descriptor_7 = descriptor[7]) !== null && _descriptor_7 !== void 0 ? _descriptor_7 : 0);
5287
+ var cancelIndicator = (((_descriptor_8 = descriptor[8]) !== null && _descriptor_8 !== void 0 ? _descriptor_8 : 0) & 128) !== 0;
5288
+ if (cancelIndicator) {
5289
+ return void 0;
5290
+ }
5291
+ var flags = (_descriptor_9 = descriptor[9]) !== null && _descriptor_9 !== void 0 ? _descriptor_9 : 0;
5292
+ var programSegmentationFlag = (flags & 128) !== 0;
5293
+ var segmentationDurationFlag = (flags & 64) !== 0;
5294
+ var offset = 10;
5295
+ if (!programSegmentationFlag) {
5296
+ var _descriptor_offset2;
5297
+ var componentCount = (_descriptor_offset2 = descriptor[offset]) !== null && _descriptor_offset2 !== void 0 ? _descriptor_offset2 : 0;
5298
+ offset += 1 + componentCount * 6;
5299
+ if (offset > descriptor.length) {
5300
+ return void 0;
5301
+ }
4557
5302
  }
4558
5303
  var durationSeconds = void 0;
4559
- if (durationFlag) {
4560
- r.readBits(6);
4561
- r.readBits(1);
4562
- var high = r.readBits(1);
4563
- var low = r.readBits(32);
4564
- var durationTicks = high * 4294967296 + low;
5304
+ if (segmentationDurationFlag) {
5305
+ var _descriptor_offset3, _descriptor_10, _descriptor_11, _descriptor_12, _descriptor_13;
5306
+ if (offset + 5 > descriptor.length) {
5307
+ return void 0;
5308
+ }
5309
+ var durationTicks = ((_descriptor_offset3 = descriptor[offset]) !== null && _descriptor_offset3 !== void 0 ? _descriptor_offset3 : 0) * 4294967296 + ((_descriptor_10 = descriptor[offset + 1]) !== null && _descriptor_10 !== void 0 ? _descriptor_10 : 0) * 16777216 + (((_descriptor_11 = descriptor[offset + 2]) !== null && _descriptor_11 !== void 0 ? _descriptor_11 : 0) << 16) + (((_descriptor_12 = descriptor[offset + 3]) !== null && _descriptor_12 !== void 0 ? _descriptor_12 : 0) << 8) + ((_descriptor_13 = descriptor[offset + 4]) !== null && _descriptor_13 !== void 0 ? _descriptor_13 : 0);
4565
5310
  durationSeconds = durationTicks / 9e4;
5311
+ offset += 5;
4566
5312
  }
4567
- r.readBits(16);
4568
- r.readBits(8);
4569
- r.readBits(8);
4570
- if (outOfNetwork) {
4571
- var marker = _object_spread_props(_object_spread({
4572
- type: "start"
4573
- }, durationSeconds !== void 0 ? {
4574
- durationSeconds: durationSeconds
4575
- } : {}), {
4576
- raw: {
4577
- splice_command_type: 5
4578
- }
4579
- });
4580
- return marker;
5313
+ if (offset + 2 > descriptor.length) {
5314
+ return void 0;
5315
+ }
5316
+ offset += 1;
5317
+ var segmentationUpidLength = (_descriptor_offset = descriptor[offset]) !== null && _descriptor_offset !== void 0 ? _descriptor_offset : 0;
5318
+ offset += 1 + segmentationUpidLength;
5319
+ if (offset >= descriptor.length) {
5320
+ return void 0;
5321
+ }
5322
+ var segmentationTypeId = (_descriptor_offset1 = descriptor[offset]) !== null && _descriptor_offset1 !== void 0 ? _descriptor_offset1 : 0;
5323
+ var markerType = this.markerTypeFromSegmentationTypeId(segmentationTypeId);
5324
+ if (!markerType) {
5325
+ return void 0;
5326
+ }
5327
+ return _object_spread_props(_object_spread({
5328
+ type: markerType
5329
+ }, durationSeconds !== void 0 ? {
5330
+ durationSeconds: durationSeconds
5331
+ } : {}), {
5332
+ segmentationEventId: segmentationEventId
5333
+ });
5334
+ }
5335
+ },
5336
+ {
5337
+ key: "markerTypeFromSegmentationTypeId",
5338
+ value: function markerTypeFromSegmentationTypeId(segmentationTypeId) {
5339
+ switch(segmentationTypeId){
5340
+ case 16:
5341
+ case 34:
5342
+ case 48:
5343
+ case 50:
5344
+ case 52:
5345
+ case 54:
5346
+ return "start";
5347
+ case 17:
5348
+ case 35:
5349
+ case 49:
5350
+ case 51:
5351
+ case 53:
5352
+ case 55:
5353
+ return "end";
5354
+ default:
5355
+ return void 0;
4581
5356
  }
4582
- return void 0;
4583
5357
  }
4584
5358
  },
4585
5359
  {
@@ -4976,7 +5750,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4976
5750
  },
4977
5751
  {
4978
5752
  key: "startAdPrefetch",
4979
- value: function startAdPrefetch(marker, fragmentSn) {
5753
+ value: function startAdPrefetch(marker, fragmentSn, cueKey) {
4980
5754
  if (this.pendingAdBreak || this.inAdBreak) {
4981
5755
  return;
4982
5756
  }
@@ -5004,6 +5778,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5004
5778
  isFetching: false,
5005
5779
  fetchStartTime: Date.now()
5006
5780
  });
5781
+ this.pendingScte35CueKey = cueKey;
5007
5782
  this.adDetectSentForCurrentBreak = true;
5008
5783
  var detectPayload = {};
5009
5784
  if (marker.durationSeconds != null) detectPayload.durationSeconds = marker.durationSeconds;
@@ -5025,6 +5800,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5025
5800
  this.prefetchTimerId = void 0;
5026
5801
  }
5027
5802
  this.pendingAdBreak = null;
5803
+ this.pendingScte35CueKey = void 0;
5028
5804
  }
5029
5805
  },
5030
5806
  {
@@ -6508,18 +7284,17 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
6508
7284
  key: "scheduleAdStartIn",
6509
7285
  value: function scheduleAdStartIn(delayMs) {
6510
7286
  var _this = this;
7287
+ var marker = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {
7288
+ type: "start"
7289
+ }, cueKey = arguments.length > 2 ? arguments[2] : void 0;
6511
7290
  this.clearAdStartTimer();
6512
7291
  var ms = Math.max(0, Math.floor(delayMs));
6513
7292
  if (ms === 0) {
6514
- this.handleAdStart({
6515
- type: "start"
6516
- }).catch(function() {});
7293
+ this.startScte35AdBreak(marker, cueKey, marker.durationSeconds != null ? marker.durationSeconds * 1e3 : void 0);
6517
7294
  return;
6518
7295
  }
6519
7296
  this.adStartTimerId = window.setTimeout(function() {
6520
- _this.handleAdStart({
6521
- type: "start"
6522
- }).catch(function() {});
7297
+ _this.startScte35AdBreak(marker, cueKey, marker.durationSeconds != null ? marker.durationSeconds * 1e3 : _this.expectedAdBreakDurationMs);
6523
7298
  }, ms);
6524
7299
  }
6525
7300
  },
@@ -6950,6 +7725,8 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
6950
7725
  this.adRequestQueue = [];
6951
7726
  this.inAdBreak = false;
6952
7727
  this.adDetectSentForCurrentBreak = false;
7728
+ this.activeScte35BreakKey = void 0;
7729
+ this.scheduledScte35BreakKey = void 0;
6953
7730
  this.expectedAdBreakDurationMs = void 0;
6954
7731
  this.currentAdBreakStartWallClockMs = void 0;
6955
7732
  this.clearAdStartTimer();