stormcloud-video-player 0.3.58 → 0.3.59

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
@@ -3278,6 +3278,12 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3278
3278
  this.adRequestQueue = [];
3279
3279
  this.maxPlaceholderDurationMs = 5e3;
3280
3280
  this.isShowingPlaceholder = false;
3281
+ this.tsScte35Pids = /* @__PURE__ */ new Set();
3282
+ this.pmtPids = /* @__PURE__ */ new Set();
3283
+ this.processedTsScte35Sections = /* @__PURE__ */ new Set();
3284
+ this.pendingScte35Cues = /* @__PURE__ */ new Map();
3285
+ this.recentScte35CueKeys = /* @__PURE__ */ new Map();
3286
+ this.recentScte35CueLimit = 200;
3281
3287
  this.totalAdRequestsInBreak = 0;
3282
3288
  this.maxTotalAdRequestsPerBreak = 10;
3283
3289
  this.pendingAdBreak = null;
@@ -3547,7 +3553,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3547
3553
  if (_this.config.debugAdTiming) {
3548
3554
  console.log("[StormcloudVideoPlayer] \uD83C\uDFAF EARLY SCTE-35 DETECTION: Ad break marker found in fragment", i, "- starting pre-fetch (NOT playing yet)");
3549
3555
  }
3550
- _this.startAdPrefetch(marker, frag === null || frag === void 0 ? void 0 : frag.sn);
3556
+ _this.onScte35Cue(marker, _object_spread({}, _this.createScte35CueContext("manifest", "early", frag)));
3551
3557
  return;
3552
3558
  }
3553
3559
  }
@@ -3628,10 +3634,21 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3628
3634
  return _this.onId3Tag(tag);
3629
3635
  });
3630
3636
  });
3637
+ this.hls.on(Hls2.Events.FRAG_LOADED, function(_evt, data) {
3638
+ var _data_frag;
3639
+ _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));
3640
+ });
3641
+ this.hls.on(Hls2.Events.FRAG_DECRYPTED, function(_evt, data) {
3642
+ var _data_frag;
3643
+ _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));
3644
+ });
3631
3645
  this.hls.on(Hls2.Events.FRAG_CHANGED, function(_evt, data) {
3632
3646
  var frag = data === null || data === void 0 ? void 0 : data.frag;
3633
3647
  var tagList = frag === null || frag === void 0 ? void 0 : frag.tagList;
3634
- if (!Array.isArray(tagList)) return;
3648
+ if (!Array.isArray(tagList)) {
3649
+ _this.activatePendingScte35CuesForFragment(frag);
3650
+ return;
3651
+ }
3635
3652
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3636
3653
  try {
3637
3654
  for(var _iterator = tagList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
@@ -3667,7 +3684,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3667
3684
  value: value
3668
3685
  }
3669
3686
  });
3670
- _this.onScte35Marker(marker);
3687
+ _this.onScte35Cue(marker, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3671
3688
  } else if (tag.includes("EXT-X-CUE-OUT")) {
3672
3689
  var durationSeconds = _this.parseCueOutDuration(value);
3673
3690
  var marker1 = _object_spread_props(_object_spread({
@@ -3680,15 +3697,15 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3680
3697
  value: value
3681
3698
  }
3682
3699
  });
3683
- _this.onScte35Marker(marker1);
3700
+ _this.onScte35Cue(marker1, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3684
3701
  } else if (tag.includes("EXT-X-CUE-IN")) {
3685
- _this.onScte35Marker({
3702
+ _this.onScte35Cue({
3686
3703
  type: "end",
3687
3704
  raw: {
3688
3705
  tag: tag,
3689
3706
  value: value
3690
3707
  }
3691
- });
3708
+ }, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3692
3709
  } else if (tag.includes("EXT-X-DATERANGE")) {
3693
3710
  var _attrs_CLASS;
3694
3711
  var attrs = _this.parseAttributeList(value);
@@ -3708,17 +3725,17 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3708
3725
  attrs: attrs
3709
3726
  }
3710
3727
  });
3711
- _this.onScte35Marker(marker2);
3728
+ _this.onScte35Cue(marker2, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3712
3729
  }
3713
3730
  if (hasScteIn) {
3714
- _this.onScte35Marker({
3731
+ _this.onScte35Cue({
3715
3732
  type: "end",
3716
3733
  raw: {
3717
3734
  tag: tag,
3718
3735
  value: value,
3719
3736
  attrs: attrs
3720
3737
  }
3721
- });
3738
+ }, _object_spread({}, _this.createScte35CueContext("manifest", "playback", frag)));
3722
3739
  }
3723
3740
  }
3724
3741
  }
@@ -3736,6 +3753,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3736
3753
  }
3737
3754
  }
3738
3755
  }
3756
+ _this.activatePendingScte35CuesForFragment(frag);
3739
3757
  });
3740
3758
  this.hls.on(Hls2.Events.ERROR, function(_evt, data) {
3741
3759
  if (data === null || data === void 0 ? void 0 : data.fatal) {
@@ -4041,7 +4059,10 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4041
4059
  }
4042
4060
  var marker = this.parseScte35FromId3(tag);
4043
4061
  if (marker) {
4044
- this.onScte35Marker(marker);
4062
+ this.onScte35Cue(marker, {
4063
+ source: "id3",
4064
+ readiness: "playback"
4065
+ });
4045
4066
  }
4046
4067
  }
4047
4068
  },
@@ -4185,9 +4206,251 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4185
4206
  }
4186
4207
  }
4187
4208
  },
4209
+ {
4210
+ key: "createScte35CueContext",
4211
+ value: function createScte35CueContext(source, readiness, frag) {
4212
+ var context = {
4213
+ source: source,
4214
+ readiness: readiness
4215
+ };
4216
+ if (typeof (frag === null || frag === void 0 ? void 0 : frag.sn) === "number") {
4217
+ context.fragmentSn = frag.sn;
4218
+ }
4219
+ var fragmentStartSeconds = this.getFragmentStartSeconds(frag);
4220
+ if (fragmentStartSeconds !== void 0) {
4221
+ context.fragmentStartSeconds = fragmentStartSeconds;
4222
+ }
4223
+ return context;
4224
+ }
4225
+ },
4226
+ {
4227
+ key: "getFragmentStartSeconds",
4228
+ value: function getFragmentStartSeconds(frag) {
4229
+ var candidates = [
4230
+ frag === null || frag === void 0 ? void 0 : frag.start,
4231
+ frag === null || frag === void 0 ? void 0 : frag.startPTS
4232
+ ];
4233
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
4234
+ try {
4235
+ for(var _iterator = candidates[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
4236
+ var candidate = _step.value;
4237
+ if (typeof candidate === "number" && Number.isFinite(candidate)) {
4238
+ return candidate;
4239
+ }
4240
+ }
4241
+ } catch (err) {
4242
+ _didIteratorError = true;
4243
+ _iteratorError = err;
4244
+ } finally{
4245
+ try {
4246
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
4247
+ _iterator.return();
4248
+ }
4249
+ } finally{
4250
+ if (_didIteratorError) {
4251
+ throw _iteratorError;
4252
+ }
4253
+ }
4254
+ }
4255
+ return void 0;
4256
+ }
4257
+ },
4258
+ {
4259
+ key: "onScte35Cue",
4260
+ value: function onScte35Cue(marker, context) {
4261
+ var cue = this.createScte35Cue(marker, context);
4262
+ var pendingKey = this.getPendingScte35CueKey(cue);
4263
+ if (context.readiness === "early") {
4264
+ this.pendingScte35Cues.set(pendingKey, cue);
4265
+ this.trimRecentScte35CueMaps();
4266
+ if (marker.type === "start") {
4267
+ this.startAdPrefetch(marker, context.fragmentSn, cue.key);
4268
+ }
4269
+ return;
4270
+ }
4271
+ if (marker.type === "end" && !this.shouldAcceptScte35EndCue(cue)) {
4272
+ if (this.config.debugAdTiming) {
4273
+ console.log("[StormcloudVideoPlayer] Ignoring SCTE-35 end cue", {
4274
+ key: cue.key,
4275
+ activeScte35BreakKey: this.activeScte35BreakKey,
4276
+ inAdBreak: this.inAdBreak,
4277
+ source: cue.source
4278
+ });
4279
+ }
4280
+ return;
4281
+ }
4282
+ if (marker.type === "start" && this.hasRecentlyHandledScte35Cue(cue)) {
4283
+ if (this.config.debugAdTiming) {
4284
+ console.log("[StormcloudVideoPlayer] Ignoring duplicate SCTE-35 start cue", {
4285
+ key: cue.key,
4286
+ source: cue.source
4287
+ });
4288
+ }
4289
+ return;
4290
+ }
4291
+ this.pendingScte35Cues.delete(pendingKey);
4292
+ this.markScte35CueHandled(cue);
4293
+ this.onScte35Marker(marker, cue.key);
4294
+ }
4295
+ },
4296
+ {
4297
+ key: "createScte35Cue",
4298
+ value: function createScte35Cue(marker, context) {
4299
+ var cue = {
4300
+ marker: marker,
4301
+ source: context.source,
4302
+ readiness: context.readiness,
4303
+ key: this.getScte35CueKey(marker, context)
4304
+ };
4305
+ if (context.fragmentSn !== void 0) {
4306
+ cue.fragmentSn = context.fragmentSn;
4307
+ }
4308
+ if (context.fragmentStartSeconds !== void 0) {
4309
+ cue.fragmentStartSeconds = context.fragmentStartSeconds;
4310
+ }
4311
+ return cue;
4312
+ }
4313
+ },
4314
+ {
4315
+ key: "getScte35CueKey",
4316
+ value: function getScte35CueKey(marker, context) {
4317
+ var _raw_attrs, _raw_attrs1, _raw_attrs2;
4318
+ var raw = this.getScte35MarkerRaw(marker);
4319
+ var spliceEventId = this.toIdentityString(raw === null || raw === void 0 ? void 0 : raw.splice_event_id);
4320
+ if (spliceEventId) {
4321
+ return "splice:".concat(spliceEventId);
4322
+ }
4323
+ var segmentationEventId = this.toIdentityString(raw === null || raw === void 0 ? void 0 : raw.segmentation_event_id);
4324
+ if (segmentationEventId) {
4325
+ return "segmentation:".concat(segmentationEventId);
4326
+ }
4327
+ 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);
4328
+ if (daterangeId) {
4329
+ return "daterange:".concat(daterangeId);
4330
+ }
4331
+ 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"]);
4332
+ if (scteValue) {
4333
+ return "daterange-scte:".concat(scteValue);
4334
+ }
4335
+ if (typeof marker.ptsSeconds === "number" && Number.isFinite(marker.ptsSeconds)) {
4336
+ return "pts:".concat(Math.round(marker.ptsSeconds * 2) / 2);
4337
+ }
4338
+ if (context.fragmentSn !== void 0) {
4339
+ return "fragment:".concat(context.fragmentSn);
4340
+ }
4341
+ return "".concat(context.source, ":wallclock:").concat(Math.floor(Date.now() / 1e3));
4342
+ }
4343
+ },
4344
+ {
4345
+ key: "getPendingScte35CueKey",
4346
+ value: function getPendingScte35CueKey(cue) {
4347
+ return "".concat(cue.marker.type, ":").concat(cue.key, ":").concat(cue.fragmentSn !== void 0 ? cue.fragmentSn : "unknown");
4348
+ }
4349
+ },
4350
+ {
4351
+ key: "getScte35MarkerRaw",
4352
+ value: function getScte35MarkerRaw(marker) {
4353
+ return _type_of(marker.raw) === "object" && marker.raw !== null ? marker.raw : {};
4354
+ }
4355
+ },
4356
+ {
4357
+ key: "toIdentityString",
4358
+ value: function toIdentityString(value) {
4359
+ if (typeof value === "number" && Number.isFinite(value)) {
4360
+ return String(value);
4361
+ }
4362
+ if (typeof value === "string" && value.trim().length > 0) {
4363
+ return value.trim();
4364
+ }
4365
+ return void 0;
4366
+ }
4367
+ },
4368
+ {
4369
+ key: "isStrongScte35CueKey",
4370
+ value: function isStrongScte35CueKey(key) {
4371
+ return !!(key && (key.startsWith("splice:") || key.startsWith("segmentation:") || key.startsWith("daterange:") || key.startsWith("daterange-scte:")));
4372
+ }
4373
+ },
4374
+ {
4375
+ key: "hasRecentlyHandledScte35Cue",
4376
+ value: function hasRecentlyHandledScte35Cue(cue) {
4377
+ if (this.activeScte35BreakKey === cue.key || this.scheduledScte35BreakKey === cue.key) {
4378
+ return true;
4379
+ }
4380
+ var recentKey = "".concat(cue.marker.type, ":").concat(cue.key);
4381
+ return this.recentScte35CueKeys.has(recentKey);
4382
+ }
4383
+ },
4384
+ {
4385
+ key: "markScte35CueHandled",
4386
+ value: function markScte35CueHandled(cue) {
4387
+ this.recentScte35CueKeys.set("".concat(cue.marker.type, ":").concat(cue.key), Date.now());
4388
+ this.trimRecentScte35CueMaps();
4389
+ }
4390
+ },
4391
+ {
4392
+ key: "trimRecentScte35CueMaps",
4393
+ value: function trimRecentScte35CueMaps() {
4394
+ while(this.recentScte35CueKeys.size > this.recentScte35CueLimit){
4395
+ var firstKey = this.recentScte35CueKeys.keys().next().value;
4396
+ if (!firstKey) {
4397
+ break;
4398
+ }
4399
+ this.recentScte35CueKeys.delete(firstKey);
4400
+ }
4401
+ while(this.pendingScte35Cues.size > this.recentScte35CueLimit){
4402
+ var firstKey1 = this.pendingScte35Cues.keys().next().value;
4403
+ if (!firstKey1) {
4404
+ break;
4405
+ }
4406
+ this.pendingScte35Cues.delete(firstKey1);
4407
+ }
4408
+ }
4409
+ },
4410
+ {
4411
+ key: "shouldAcceptScte35EndCue",
4412
+ value: function shouldAcceptScte35EndCue(cue) {
4413
+ if (!this.inAdBreak) {
4414
+ return false;
4415
+ }
4416
+ if (this.isStrongScte35CueKey(this.activeScte35BreakKey) && this.isStrongScte35CueKey(cue.key) && this.activeScte35BreakKey !== cue.key) {
4417
+ return false;
4418
+ }
4419
+ return true;
4420
+ }
4421
+ },
4422
+ {
4423
+ key: "activatePendingScte35CuesForFragment",
4424
+ value: function activatePendingScte35CuesForFragment(frag) {
4425
+ var _this = this;
4426
+ var fragmentSn = typeof (frag === null || frag === void 0 ? void 0 : frag.sn) === "number" ? frag.sn : void 0;
4427
+ if (fragmentSn === void 0) {
4428
+ return;
4429
+ }
4430
+ var matchingCues = Array.from(this.pendingScte35Cues.entries()).filter(function(param) {
4431
+ var _param = _sliced_to_array(param, 2), cue = _param[1];
4432
+ return cue.fragmentSn === fragmentSn;
4433
+ });
4434
+ matchingCues.forEach(function(param) {
4435
+ var _param = _sliced_to_array(param, 2), pendingKey = _param[0], cue = _param[1];
4436
+ var _cue_fragmentStartSeconds;
4437
+ _this.pendingScte35Cues.delete(pendingKey);
4438
+ var context = {
4439
+ source: cue.source,
4440
+ readiness: "playback"
4441
+ };
4442
+ context.fragmentSn = fragmentSn;
4443
+ var fragmentStartSeconds = (_cue_fragmentStartSeconds = cue.fragmentStartSeconds) !== null && _cue_fragmentStartSeconds !== void 0 ? _cue_fragmentStartSeconds : _this.getFragmentStartSeconds(frag);
4444
+ if (fragmentStartSeconds !== void 0) {
4445
+ context.fragmentStartSeconds = fragmentStartSeconds;
4446
+ }
4447
+ _this.onScte35Cue(cue.marker, context);
4448
+ });
4449
+ }
4450
+ },
4188
4451
  {
4189
4452
  key: "onScte35Marker",
4190
- value: function onScte35Marker(marker) {
4453
+ value: function onScte35Marker(marker, cueKey) {
4191
4454
  if (this.config.debugAdTiming) {
4192
4455
  console.log("[StormcloudVideoPlayer] SCTE-35 marker detected:", {
4193
4456
  type: marker.type,
@@ -4201,13 +4464,6 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4201
4464
  if (marker.type === "start") {
4202
4465
  var _this_config_immediateManifestAds;
4203
4466
  var _this_pendingAdBreak;
4204
- if (!this.video.muted) {
4205
- this.video.muted = true;
4206
- this.video.volume = 0;
4207
- if (this.config.debugAdTiming) {
4208
- console.log("[StormcloudVideoPlayer] Muted video on SCTE start marker");
4209
- }
4210
- }
4211
4467
  if (this.inAdBreak) {
4212
4468
  if (marker.durationSeconds != null) {
4213
4469
  var newDurationMs = marker.durationSeconds * 1e3;
@@ -4231,10 +4487,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4231
4487
  sendAdDetectTracking(this.config.licenseKey, detectPayload).catch(function() {});
4232
4488
  }
4233
4489
  var hasPrefetchedAds = this.pendingAdBreak && this.pendingAdBreak.vastUrls.length > 0;
4234
- this.inAdBreak = true;
4235
4490
  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;
4236
- this.expectedAdBreakDurationMs = durationMs;
4237
- this.currentAdBreakStartWallClockMs = Date.now();
4238
4491
  var isManifestMarker = this.isManifestBasedMarker(marker);
4239
4492
  var forceImmediate = (_this_config_immediateManifestAds = this.config.immediateManifestAds) !== null && _this_config_immediateManifestAds !== void 0 ? _this_config_immediateManifestAds : true;
4240
4493
  if (this.config.debugAdTiming) {
@@ -4250,7 +4503,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4250
4503
  console.log("[StormcloudVideoPlayer] Starting ad immediately (manifest-based)".concat(hasPrefetchedAds ? " with prefetched ads" : ""));
4251
4504
  }
4252
4505
  this.clearAdStartTimer();
4253
- this.handleAdStart(marker);
4506
+ this.startScte35AdBreak(marker, cueKey, durationMs);
4254
4507
  } else if (typeof marker.ptsSeconds === "number") {
4255
4508
  var _this_config_driftToleranceMs;
4256
4509
  var tol = (_this_config_driftToleranceMs = this.config.driftToleranceMs) !== null && _this_config_driftToleranceMs !== void 0 ? _this_config_driftToleranceMs : 1e3;
@@ -4270,23 +4523,22 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4270
4523
  if (this.config.debugAdTiming) {
4271
4524
  console.log("[StormcloudVideoPlayer] Scheduling ad start in ".concat(deltaMs, "ms"));
4272
4525
  }
4273
- this.scheduleAdStartIn(deltaMs);
4526
+ this.expectedAdBreakDurationMs = durationMs;
4527
+ this.scheduledScte35BreakKey = cueKey;
4528
+ this.scheduleAdStartIn(deltaMs, marker, cueKey);
4274
4529
  } else {
4275
4530
  if (this.config.debugAdTiming) {
4276
4531
  console.log("[StormcloudVideoPlayer] Starting ad immediately (within tolerance)");
4277
4532
  }
4278
4533
  this.clearAdStartTimer();
4279
- this.handleAdStart(marker);
4534
+ this.startScte35AdBreak(marker, cueKey, durationMs);
4280
4535
  }
4281
4536
  } else {
4282
4537
  if (this.config.debugAdTiming) {
4283
4538
  console.log("[StormcloudVideoPlayer] Starting ad immediately (fallback)");
4284
4539
  }
4285
4540
  this.clearAdStartTimer();
4286
- this.handleAdStart(marker);
4287
- }
4288
- if (this.expectedAdBreakDurationMs != null) {
4289
- this.scheduleAdStopCountdown(this.expectedAdBreakDurationMs);
4541
+ this.startScte35AdBreak(marker, cueKey, durationMs);
4290
4542
  }
4291
4543
  return;
4292
4544
  }
@@ -4342,6 +4594,27 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4342
4594
  }
4343
4595
  }
4344
4596
  },
4597
+ {
4598
+ key: "startScte35AdBreak",
4599
+ value: function startScte35AdBreak(marker, cueKey, durationMs) {
4600
+ if (!this.video.muted) {
4601
+ this.video.muted = true;
4602
+ this.video.volume = 0;
4603
+ if (this.config.debugAdTiming) {
4604
+ console.log("[StormcloudVideoPlayer] Muted video on SCTE start marker");
4605
+ }
4606
+ }
4607
+ this.inAdBreak = true;
4608
+ this.activeScte35BreakKey = cueKey !== null && cueKey !== void 0 ? cueKey : this.pendingScte35CueKey;
4609
+ this.scheduledScte35BreakKey = void 0;
4610
+ this.expectedAdBreakDurationMs = durationMs;
4611
+ this.currentAdBreakStartWallClockMs = Date.now();
4612
+ this.handleAdStart(marker);
4613
+ if (this.expectedAdBreakDurationMs != null) {
4614
+ this.scheduleAdStopCountdown(this.expectedAdBreakDurationMs);
4615
+ }
4616
+ }
4617
+ },
4345
4618
  {
4346
4619
  key: "parseCueOutDuration",
4347
4620
  value: function parseCueOutDuration(value) {
@@ -4425,6 +4698,336 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4425
4698
  return false;
4426
4699
  }
4427
4700
  },
4701
+ {
4702
+ key: "normalizeFragmentPayload",
4703
+ value: function normalizeFragmentPayload(payload) {
4704
+ if (_instanceof(payload, Uint8Array)) {
4705
+ return payload;
4706
+ }
4707
+ if (_instanceof(payload, ArrayBuffer)) {
4708
+ return new Uint8Array(payload);
4709
+ }
4710
+ if (ArrayBuffer.isView(payload)) {
4711
+ var view = payload;
4712
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
4713
+ }
4714
+ return void 0;
4715
+ }
4716
+ },
4717
+ {
4718
+ key: "processFragmentScte35Payload",
4719
+ value: function processFragmentScte35Payload(payloadData, fragmentSn, fragmentStartSeconds) {
4720
+ var _this = this;
4721
+ var payload = this.normalizeFragmentPayload(payloadData);
4722
+ if (!payload) {
4723
+ return;
4724
+ }
4725
+ var markers = this.parseScte35FromTsPackets(payload);
4726
+ markers.forEach(function(marker) {
4727
+ var context = {
4728
+ source: "ts",
4729
+ readiness: "early"
4730
+ };
4731
+ if (fragmentSn !== void 0) {
4732
+ context.fragmentSn = fragmentSn;
4733
+ }
4734
+ if (fragmentStartSeconds !== void 0) {
4735
+ context.fragmentStartSeconds = fragmentStartSeconds;
4736
+ }
4737
+ _this.onScte35Cue(marker, context);
4738
+ });
4739
+ }
4740
+ },
4741
+ {
4742
+ key: "parseScte35FromTsPackets",
4743
+ value: function parseScte35FromTsPackets(data) {
4744
+ var packetInfo = this.detectTsPacketFormat(data);
4745
+ if (!packetInfo) {
4746
+ return [];
4747
+ }
4748
+ var markers = [];
4749
+ var sectionAssemblers = /* @__PURE__ */ new Map();
4750
+ for(var packetStart = packetInfo.offset; packetStart + packetInfo.packetSize <= data.length; packetStart += packetInfo.packetSize){
4751
+ var tsStart = packetStart + packetInfo.tsPayloadOffset;
4752
+ if (data[tsStart] !== 71) {
4753
+ continue;
4754
+ }
4755
+ var secondByte = data[tsStart + 1];
4756
+ var thirdByte = data[tsStart + 2];
4757
+ var fourthByte = data[tsStart + 3];
4758
+ if (secondByte == null || thirdByte == null || fourthByte == null || (secondByte & 128) !== 0) {
4759
+ continue;
4760
+ }
4761
+ var payloadUnitStart = (secondByte & 64) !== 0;
4762
+ var pid = (secondByte & 31) << 8 | thirdByte;
4763
+ var adaptationFieldControl = fourthByte >> 4 & 3;
4764
+ var hasPayload = adaptationFieldControl === 1 || adaptationFieldControl === 3;
4765
+ if (!hasPayload) {
4766
+ continue;
4767
+ }
4768
+ var payloadStart = tsStart + 4;
4769
+ if (adaptationFieldControl === 3) {
4770
+ var adaptationLength = data[payloadStart];
4771
+ if (adaptationLength == null) {
4772
+ continue;
4773
+ }
4774
+ payloadStart += 1 + adaptationLength;
4775
+ }
4776
+ var payloadEnd = Math.min(tsStart + 188, data.length);
4777
+ if (payloadStart >= payloadEnd || payloadStart >= data.length) {
4778
+ continue;
4779
+ }
4780
+ var payload = data.subarray(payloadStart, payloadEnd);
4781
+ if (pid === 0) {
4782
+ this.parsePatSection(payload, payloadUnitStart);
4783
+ } else if (this.pmtPids.has(pid)) {
4784
+ this.parsePmtSection(payload, payloadUnitStart);
4785
+ } else if (this.tsScte35Pids.has(pid)) {
4786
+ this.collectScte35SectionsFromPayload(pid, payload, payloadUnitStart, sectionAssemblers, markers);
4787
+ }
4788
+ }
4789
+ return markers;
4790
+ }
4791
+ },
4792
+ {
4793
+ key: "detectTsPacketFormat",
4794
+ value: function detectTsPacketFormat(data) {
4795
+ var packetSizes = [
4796
+ 188,
4797
+ 192,
4798
+ 204
4799
+ ];
4800
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
4801
+ try {
4802
+ for(var _iterator = packetSizes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
4803
+ var packetSize = _step.value;
4804
+ var tsPayloadOffset = packetSize === 192 ? 4 : 0;
4805
+ for(var offset = 0; offset < packetSize; offset++){
4806
+ var matchedPackets = 0;
4807
+ for(var packetStart = offset; packetStart + tsPayloadOffset < data.length && matchedPackets < 5; packetStart += packetSize){
4808
+ if (data[packetStart + tsPayloadOffset] !== 71) {
4809
+ break;
4810
+ }
4811
+ matchedPackets++;
4812
+ }
4813
+ if (matchedPackets >= 2) {
4814
+ return {
4815
+ offset: offset,
4816
+ packetSize: packetSize,
4817
+ tsPayloadOffset: tsPayloadOffset
4818
+ };
4819
+ }
4820
+ }
4821
+ }
4822
+ } catch (err) {
4823
+ _didIteratorError = true;
4824
+ _iteratorError = err;
4825
+ } finally{
4826
+ try {
4827
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
4828
+ _iterator.return();
4829
+ }
4830
+ } finally{
4831
+ if (_didIteratorError) {
4832
+ throw _iteratorError;
4833
+ }
4834
+ }
4835
+ }
4836
+ return void 0;
4837
+ }
4838
+ },
4839
+ {
4840
+ key: "parsePatSection",
4841
+ value: function parsePatSection(payload, payloadUnitStart) {
4842
+ var sectionStart = this.getPsiSectionStart(payload, payloadUnitStart);
4843
+ if (sectionStart == null || sectionStart + 8 >= payload.length) {
4844
+ return;
4845
+ }
4846
+ if (payload[sectionStart] !== 0) {
4847
+ return;
4848
+ }
4849
+ var sectionLength = this.getPsiSectionLength(payload, sectionStart);
4850
+ var sectionEnd = Math.min(payload.length, sectionStart + 3 + sectionLength);
4851
+ for(var offset = sectionStart + 8; offset + 4 <= sectionEnd - 4; offset += 4){
4852
+ var _payload_offset, _payload_, _payload_1, _payload_2;
4853
+ 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);
4854
+ 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);
4855
+ if (programNumber !== 0) {
4856
+ this.pmtPids.add(pid);
4857
+ }
4858
+ }
4859
+ }
4860
+ },
4861
+ {
4862
+ key: "parsePmtSection",
4863
+ value: function parsePmtSection(payload, payloadUnitStart) {
4864
+ var _payload_, _payload_1;
4865
+ var sectionStart = this.getPsiSectionStart(payload, payloadUnitStart);
4866
+ if (sectionStart == null || sectionStart + 12 >= payload.length) {
4867
+ return;
4868
+ }
4869
+ if (payload[sectionStart] !== 2) {
4870
+ return;
4871
+ }
4872
+ var sectionLength = this.getPsiSectionLength(payload, sectionStart);
4873
+ var sectionEnd = Math.min(payload.length, sectionStart + 3 + sectionLength);
4874
+ 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);
4875
+ var offset = sectionStart + 12 + programInfoLength;
4876
+ while(offset + 5 <= sectionEnd - 4){
4877
+ var _payload_offset, _payload_2, _payload_3, _payload_4, _payload_5;
4878
+ var streamType = (_payload_offset = payload[offset]) !== null && _payload_offset !== void 0 ? _payload_offset : 0;
4879
+ 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);
4880
+ 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);
4881
+ var descriptorStart = offset + 5;
4882
+ var descriptorEnd = Math.min(descriptorStart + esInfoLength, sectionEnd - 4);
4883
+ if (streamType === 134 || streamType === 6 && this.descriptorsIdentifyScte35(payload, descriptorStart, descriptorEnd)) {
4884
+ this.tsScte35Pids.add(elementaryPid);
4885
+ }
4886
+ offset = descriptorStart + esInfoLength;
4887
+ }
4888
+ }
4889
+ },
4890
+ {
4891
+ key: "descriptorsIdentifyScte35",
4892
+ value: function descriptorsIdentifyScte35(data, start, end) {
4893
+ var offset = start;
4894
+ while(offset + 2 <= end){
4895
+ var _data_offset, _data_, _data_descriptorStart, _data_1, _data_2, _data_3;
4896
+ var tag = (_data_offset = data[offset]) !== null && _data_offset !== void 0 ? _data_offset : 0;
4897
+ var length = (_data_ = data[offset + 1]) !== null && _data_ !== void 0 ? _data_ : 0;
4898
+ var descriptorStart = offset + 2;
4899
+ var descriptorEnd = descriptorStart + length;
4900
+ if (descriptorEnd > end) {
4901
+ return false;
4902
+ }
4903
+ 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) : "";
4904
+ if (tag === 138 || registration === "CUEI" || registration === "SCTE") {
4905
+ return true;
4906
+ }
4907
+ offset = descriptorEnd;
4908
+ }
4909
+ return false;
4910
+ }
4911
+ },
4912
+ {
4913
+ key: "collectScte35SectionsFromPayload",
4914
+ value: function collectScte35SectionsFromPayload(pid, payload, payloadUnitStart, assemblers, markers) {
4915
+ if (payload.length === 0) {
4916
+ return;
4917
+ }
4918
+ var offset = 0;
4919
+ if (payloadUnitStart) {
4920
+ var _payload_;
4921
+ var pointerField = (_payload_ = payload[0]) !== null && _payload_ !== void 0 ? _payload_ : 0;
4922
+ var previousAssembler = assemblers.get(pid);
4923
+ if (previousAssembler && pointerField > 0) {
4924
+ this.appendScte35Bytes(pid, payload.subarray(1, Math.min(1 + pointerField, payload.length)), assemblers, markers);
4925
+ }
4926
+ assemblers.delete(pid);
4927
+ offset = 1 + pointerField;
4928
+ } else {
4929
+ this.appendScte35Bytes(pid, payload, assemblers, markers);
4930
+ return;
4931
+ }
4932
+ while(offset + 3 <= payload.length){
4933
+ var tableId = payload[offset];
4934
+ if (tableId == null || tableId === 255) {
4935
+ return;
4936
+ }
4937
+ var sectionLength = this.getPsiSectionLength(payload, offset);
4938
+ var totalLength = 3 + sectionLength;
4939
+ if (sectionLength <= 0 || totalLength <= 3) {
4940
+ return;
4941
+ }
4942
+ if (offset + totalLength <= payload.length) {
4943
+ this.addScte35SectionMarker(payload.subarray(offset, offset + totalLength), markers);
4944
+ offset += totalLength;
4945
+ } else {
4946
+ assemblers.set(pid, {
4947
+ bytes: Array.from(payload.subarray(offset)),
4948
+ expectedLength: totalLength
4949
+ });
4950
+ return;
4951
+ }
4952
+ }
4953
+ }
4954
+ },
4955
+ {
4956
+ key: "appendScte35Bytes",
4957
+ value: function appendScte35Bytes(pid, bytes, assemblers, markers) {
4958
+ var assembler = assemblers.get(pid);
4959
+ if (!assembler) {
4960
+ return;
4961
+ }
4962
+ for(var i = 0; i < bytes.length && assembler.bytes.length < assembler.expectedLength; i++){
4963
+ var _bytes_i;
4964
+ assembler.bytes.push((_bytes_i = bytes[i]) !== null && _bytes_i !== void 0 ? _bytes_i : 0);
4965
+ }
4966
+ if (assembler.bytes.length >= assembler.expectedLength) {
4967
+ assemblers.delete(pid);
4968
+ this.addScte35SectionMarker(new Uint8Array(assembler.bytes), markers);
4969
+ }
4970
+ }
4971
+ },
4972
+ {
4973
+ key: "addScte35SectionMarker",
4974
+ value: function addScte35SectionMarker(section, markers) {
4975
+ if (section[0] !== 252 || this.hasProcessedTsScte35Section(section)) {
4976
+ return;
4977
+ }
4978
+ var marker = this.parseScte35Binary(section);
4979
+ if (!marker) {
4980
+ return;
4981
+ }
4982
+ marker.raw = _object_spread_props(_object_spread({}, _type_of(marker.raw) === "object" && marker.raw ? marker.raw : {}), {
4983
+ source: "ts-packet"
4984
+ });
4985
+ markers.push(marker);
4986
+ }
4987
+ },
4988
+ {
4989
+ key: "hasProcessedTsScte35Section",
4990
+ value: function hasProcessedTsScte35Section(section) {
4991
+ var checksum = 0;
4992
+ for(var i = 0; i < section.length; i++){
4993
+ var _section_i;
4994
+ checksum = checksum * 31 + ((_section_i = section[i]) !== null && _section_i !== void 0 ? _section_i : 0) >>> 0;
4995
+ }
4996
+ var key = "".concat(section.length, ":").concat(checksum);
4997
+ if (this.processedTsScte35Sections.has(key)) {
4998
+ return true;
4999
+ }
5000
+ this.processedTsScte35Sections.add(key);
5001
+ if (this.processedTsScte35Sections.size > 200) {
5002
+ var firstKey = this.processedTsScte35Sections.values().next().value;
5003
+ if (firstKey) {
5004
+ this.processedTsScte35Sections.delete(firstKey);
5005
+ }
5006
+ }
5007
+ return false;
5008
+ }
5009
+ },
5010
+ {
5011
+ key: "getPsiSectionStart",
5012
+ value: function getPsiSectionStart(payload, payloadUnitStart) {
5013
+ if (!payloadUnitStart) {
5014
+ return void 0;
5015
+ }
5016
+ var pointerField = payload[0];
5017
+ if (pointerField == null) {
5018
+ return void 0;
5019
+ }
5020
+ var sectionStart = 1 + pointerField;
5021
+ return sectionStart < payload.length ? sectionStart : void 0;
5022
+ }
5023
+ },
5024
+ {
5025
+ key: "getPsiSectionLength",
5026
+ value: function getPsiSectionLength(data, offset) {
5027
+ var _data_, _data_1;
5028
+ 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);
5029
+ }
5030
+ },
4428
5031
  {
4429
5032
  key: "parseScte35Binary",
4430
5033
  value: function parseScte35Binary(data) {
@@ -4464,10 +5067,28 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4464
5067
  value: function skipBits(n) {
4465
5068
  this.readBits(n);
4466
5069
  }
5070
+ },
5071
+ {
5072
+ key: "bytePosition",
5073
+ get: function get() {
5074
+ return this.bitPos === 0 ? this.bytePos : this.bytePos + 1;
5075
+ }
4467
5076
  }
4468
5077
  ]);
4469
5078
  return BitReader;
4470
5079
  }();
5080
+ var readSpliceTime = function readSpliceTime(r2, ptsAdjustmentTicks2) {
5081
+ var timeSpecifiedFlag = r2.readBits(1) === 1;
5082
+ if (!timeSpecifiedFlag) {
5083
+ r2.readBits(7);
5084
+ return void 0;
5085
+ }
5086
+ r2.readBits(6);
5087
+ var high = r2.readBits(1);
5088
+ var low = r2.readBits(32);
5089
+ var ptsTicks = (high * 4294967296 + low + ptsAdjustmentTicks2) % 8589934592;
5090
+ return ptsTicks / 9e4;
5091
+ };
4471
5092
  var r = new BitReader(data);
4472
5093
  var tableId = r.readBits(8);
4473
5094
  if (tableId !== 252) return void 0;
@@ -4475,77 +5096,206 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4475
5096
  r.readBits(1);
4476
5097
  r.readBits(2);
4477
5098
  var sectionLength = r.readBits(12);
5099
+ if (sectionLength <= 0 || data.length < 3 + sectionLength) {
5100
+ return void 0;
5101
+ }
4478
5102
  r.readBits(8);
4479
5103
  r.readBits(1);
4480
5104
  r.readBits(6);
4481
5105
  var ptsAdjHigh = r.readBits(1);
4482
5106
  var ptsAdjLow = r.readBits(32);
4483
- void ptsAdjHigh;
4484
- void ptsAdjLow;
5107
+ var ptsAdjustmentTicks = ptsAdjHigh * 4294967296 + ptsAdjLow;
4485
5108
  r.readBits(8);
4486
5109
  r.readBits(12);
4487
5110
  var spliceCommandLength = r.readBits(12);
4488
5111
  var spliceCommandType = r.readBits(8);
4489
- if (spliceCommandType !== 5) {
5112
+ var markerType;
5113
+ var durationSeconds = void 0;
5114
+ var ptsSeconds = void 0;
5115
+ var spliceEventId = void 0;
5116
+ var commandBodyStart = r.bytePosition;
5117
+ if (spliceCommandType === 5) {
5118
+ spliceEventId = r.readBits(32);
5119
+ var cancel = r.readBits(1) === 1;
5120
+ r.readBits(7);
5121
+ if (cancel) return void 0;
5122
+ var outOfNetwork = r.readBits(1) === 1;
5123
+ var programSpliceFlag = r.readBits(1) === 1;
5124
+ var durationFlag = r.readBits(1) === 1;
5125
+ var spliceImmediateFlag = r.readBits(1) === 1;
5126
+ r.readBits(4);
5127
+ markerType = outOfNetwork ? "start" : "end";
5128
+ if (programSpliceFlag && !spliceImmediateFlag) {
5129
+ ptsSeconds = readSpliceTime(r, ptsAdjustmentTicks);
5130
+ } else if (!programSpliceFlag) {
5131
+ var componentCount = r.readBits(8);
5132
+ for(var i = 0; i < componentCount; i++){
5133
+ r.readBits(8);
5134
+ if (!spliceImmediateFlag) {
5135
+ var componentPtsSeconds = readSpliceTime(r, ptsAdjustmentTicks);
5136
+ if (ptsSeconds === void 0) {
5137
+ ptsSeconds = componentPtsSeconds;
5138
+ }
5139
+ }
5140
+ }
5141
+ }
5142
+ if (durationFlag) {
5143
+ r.readBits(6);
5144
+ r.readBits(1);
5145
+ var high = r.readBits(1);
5146
+ var low = r.readBits(32);
5147
+ var durationTicks = high * 4294967296 + low;
5148
+ durationSeconds = durationTicks / 9e4;
5149
+ }
5150
+ r.readBits(16);
5151
+ r.readBits(8);
5152
+ r.readBits(8);
5153
+ } else if (spliceCommandType === 6) {
5154
+ ptsSeconds = readSpliceTime(r, ptsAdjustmentTicks);
5155
+ } else {
4490
5156
  return void 0;
4491
5157
  }
4492
- r.readBits(32);
4493
- var cancel = r.readBits(1) === 1;
4494
- r.readBits(7);
4495
- if (cancel) return void 0;
4496
- var outOfNetwork = r.readBits(1) === 1;
4497
- var programSpliceFlag = r.readBits(1) === 1;
4498
- var durationFlag = r.readBits(1) === 1;
4499
- var spliceImmediateFlag = r.readBits(1) === 1;
4500
- r.readBits(4);
4501
- if (programSpliceFlag && !spliceImmediateFlag) {
4502
- var timeSpecifiedFlag = r.readBits(1) === 1;
4503
- if (timeSpecifiedFlag) {
4504
- r.readBits(6);
4505
- r.readBits(33);
4506
- } else {
4507
- r.readBits(7);
4508
- }
4509
- } else if (!programSpliceFlag) {
4510
- var componentCount = r.readBits(8);
4511
- for(var i = 0; i < componentCount; i++){
4512
- r.readBits(8);
4513
- if (!spliceImmediateFlag) {
4514
- var timeSpecifiedFlag1 = r.readBits(1) === 1;
4515
- if (timeSpecifiedFlag1) {
4516
- r.readBits(6);
4517
- r.readBits(33);
4518
- } else {
4519
- r.readBits(7);
4520
- }
5158
+ var expectedDescriptorOffset = spliceCommandLength === 4095 ? r.bytePosition : commandBodyStart + spliceCommandLength;
5159
+ var descriptorMarker = this.parseScte35SegmentationDescriptors(data, expectedDescriptorOffset);
5160
+ if (descriptorMarker) {
5161
+ markerType = descriptorMarker.type;
5162
+ if (durationSeconds === void 0) {
5163
+ durationSeconds = descriptorMarker.durationSeconds;
5164
+ }
5165
+ }
5166
+ if (!markerType) {
5167
+ return void 0;
5168
+ }
5169
+ var marker = _object_spread_props(_object_spread({
5170
+ type: markerType
5171
+ }, ptsSeconds !== void 0 ? {
5172
+ ptsSeconds: ptsSeconds
5173
+ } : {}, durationSeconds !== void 0 ? {
5174
+ durationSeconds: durationSeconds
5175
+ } : {}), {
5176
+ raw: _object_spread({
5177
+ splice_command_type: spliceCommandType
5178
+ }, spliceEventId !== void 0 ? {
5179
+ splice_event_id: spliceEventId
5180
+ } : {}, (descriptorMarker === null || descriptorMarker === void 0 ? void 0 : descriptorMarker.segmentationEventId) !== void 0 ? {
5181
+ segmentation_event_id: descriptorMarker.segmentationEventId
5182
+ } : {})
5183
+ });
5184
+ return marker;
5185
+ }
5186
+ },
5187
+ {
5188
+ key: "parseScte35SegmentationDescriptors",
5189
+ value: function parseScte35SegmentationDescriptors(data, offset) {
5190
+ var _data_offset, _data_;
5191
+ if (offset + 2 > data.length) {
5192
+ return void 0;
5193
+ }
5194
+ 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);
5195
+ var descriptorOffset = offset + 2;
5196
+ var descriptorEnd = Math.min(data.length, descriptorOffset + descriptorLoopLength);
5197
+ while(descriptorOffset + 2 <= descriptorEnd){
5198
+ var _data_descriptorOffset, _data_1;
5199
+ var descriptorTag = (_data_descriptorOffset = data[descriptorOffset]) !== null && _data_descriptorOffset !== void 0 ? _data_descriptorOffset : 0;
5200
+ var descriptorLength = (_data_1 = data[descriptorOffset + 1]) !== null && _data_1 !== void 0 ? _data_1 : 0;
5201
+ var descriptorDataStart = descriptorOffset + 2;
5202
+ var descriptorDataEnd = descriptorDataStart + descriptorLength;
5203
+ if (descriptorDataEnd > descriptorEnd) {
5204
+ return void 0;
5205
+ }
5206
+ if (descriptorTag === 2) {
5207
+ var descriptorMarker = this.parseScte35SegmentationDescriptor(data.subarray(descriptorDataStart, descriptorDataEnd));
5208
+ if (descriptorMarker) {
5209
+ return descriptorMarker;
4521
5210
  }
4522
5211
  }
5212
+ descriptorOffset = descriptorDataEnd;
5213
+ }
5214
+ return void 0;
5215
+ }
5216
+ },
5217
+ {
5218
+ key: "parseScte35SegmentationDescriptor",
5219
+ value: function parseScte35SegmentationDescriptor(descriptor) {
5220
+ var _descriptor_, _descriptor_1, _descriptor_2, _descriptor_3, _descriptor_4, _descriptor_5, _descriptor_6, _descriptor_7, _descriptor_8, _descriptor_9, _descriptor_offset, _descriptor_offset1;
5221
+ if (descriptor.length < 11) {
5222
+ return void 0;
5223
+ }
5224
+ 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);
5225
+ if (identifier !== "CUEI") {
5226
+ return void 0;
5227
+ }
5228
+ 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);
5229
+ var cancelIndicator = (((_descriptor_8 = descriptor[8]) !== null && _descriptor_8 !== void 0 ? _descriptor_8 : 0) & 128) !== 0;
5230
+ if (cancelIndicator) {
5231
+ return void 0;
5232
+ }
5233
+ var flags = (_descriptor_9 = descriptor[9]) !== null && _descriptor_9 !== void 0 ? _descriptor_9 : 0;
5234
+ var programSegmentationFlag = (flags & 128) !== 0;
5235
+ var segmentationDurationFlag = (flags & 64) !== 0;
5236
+ var offset = 10;
5237
+ if (!programSegmentationFlag) {
5238
+ var _descriptor_offset2;
5239
+ var componentCount = (_descriptor_offset2 = descriptor[offset]) !== null && _descriptor_offset2 !== void 0 ? _descriptor_offset2 : 0;
5240
+ offset += 1 + componentCount * 6;
5241
+ if (offset > descriptor.length) {
5242
+ return void 0;
5243
+ }
4523
5244
  }
4524
5245
  var durationSeconds = void 0;
4525
- if (durationFlag) {
4526
- r.readBits(6);
4527
- r.readBits(1);
4528
- var high = r.readBits(1);
4529
- var low = r.readBits(32);
4530
- var durationTicks = high * 4294967296 + low;
5246
+ if (segmentationDurationFlag) {
5247
+ var _descriptor_offset3, _descriptor_10, _descriptor_11, _descriptor_12, _descriptor_13;
5248
+ if (offset + 5 > descriptor.length) {
5249
+ return void 0;
5250
+ }
5251
+ 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);
4531
5252
  durationSeconds = durationTicks / 9e4;
5253
+ offset += 5;
4532
5254
  }
4533
- r.readBits(16);
4534
- r.readBits(8);
4535
- r.readBits(8);
4536
- if (outOfNetwork) {
4537
- var marker = _object_spread_props(_object_spread({
4538
- type: "start"
4539
- }, durationSeconds !== void 0 ? {
4540
- durationSeconds: durationSeconds
4541
- } : {}), {
4542
- raw: {
4543
- splice_command_type: 5
4544
- }
4545
- });
4546
- return marker;
5255
+ if (offset + 2 > descriptor.length) {
5256
+ return void 0;
5257
+ }
5258
+ offset += 1;
5259
+ var segmentationUpidLength = (_descriptor_offset = descriptor[offset]) !== null && _descriptor_offset !== void 0 ? _descriptor_offset : 0;
5260
+ offset += 1 + segmentationUpidLength;
5261
+ if (offset >= descriptor.length) {
5262
+ return void 0;
5263
+ }
5264
+ var segmentationTypeId = (_descriptor_offset1 = descriptor[offset]) !== null && _descriptor_offset1 !== void 0 ? _descriptor_offset1 : 0;
5265
+ var markerType = this.markerTypeFromSegmentationTypeId(segmentationTypeId);
5266
+ if (!markerType) {
5267
+ return void 0;
5268
+ }
5269
+ return _object_spread_props(_object_spread({
5270
+ type: markerType
5271
+ }, durationSeconds !== void 0 ? {
5272
+ durationSeconds: durationSeconds
5273
+ } : {}), {
5274
+ segmentationEventId: segmentationEventId
5275
+ });
5276
+ }
5277
+ },
5278
+ {
5279
+ key: "markerTypeFromSegmentationTypeId",
5280
+ value: function markerTypeFromSegmentationTypeId(segmentationTypeId) {
5281
+ switch(segmentationTypeId){
5282
+ case 16:
5283
+ case 34:
5284
+ case 48:
5285
+ case 50:
5286
+ case 52:
5287
+ case 54:
5288
+ return "start";
5289
+ case 17:
5290
+ case 35:
5291
+ case 49:
5292
+ case 51:
5293
+ case 53:
5294
+ case 55:
5295
+ return "end";
5296
+ default:
5297
+ return void 0;
4547
5298
  }
4548
- return void 0;
4549
5299
  }
4550
5300
  },
4551
5301
  {
@@ -4942,7 +5692,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4942
5692
  },
4943
5693
  {
4944
5694
  key: "startAdPrefetch",
4945
- value: function startAdPrefetch(marker, fragmentSn) {
5695
+ value: function startAdPrefetch(marker, fragmentSn, cueKey) {
4946
5696
  if (this.pendingAdBreak || this.inAdBreak) {
4947
5697
  return;
4948
5698
  }
@@ -4970,6 +5720,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4970
5720
  isFetching: false,
4971
5721
  fetchStartTime: Date.now()
4972
5722
  });
5723
+ this.pendingScte35CueKey = cueKey;
4973
5724
  this.adDetectSentForCurrentBreak = true;
4974
5725
  var detectPayload = {};
4975
5726
  if (marker.durationSeconds != null) detectPayload.durationSeconds = marker.durationSeconds;
@@ -4991,6 +5742,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4991
5742
  this.prefetchTimerId = void 0;
4992
5743
  }
4993
5744
  this.pendingAdBreak = null;
5745
+ this.pendingScte35CueKey = void 0;
4994
5746
  }
4995
5747
  },
4996
5748
  {
@@ -6474,18 +7226,17 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
6474
7226
  key: "scheduleAdStartIn",
6475
7227
  value: function scheduleAdStartIn(delayMs) {
6476
7228
  var _this = this;
7229
+ var marker = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {
7230
+ type: "start"
7231
+ }, cueKey = arguments.length > 2 ? arguments[2] : void 0;
6477
7232
  this.clearAdStartTimer();
6478
7233
  var ms = Math.max(0, Math.floor(delayMs));
6479
7234
  if (ms === 0) {
6480
- this.handleAdStart({
6481
- type: "start"
6482
- }).catch(function() {});
7235
+ this.startScte35AdBreak(marker, cueKey, marker.durationSeconds != null ? marker.durationSeconds * 1e3 : void 0);
6483
7236
  return;
6484
7237
  }
6485
7238
  this.adStartTimerId = window.setTimeout(function() {
6486
- _this.handleAdStart({
6487
- type: "start"
6488
- }).catch(function() {});
7239
+ _this.startScte35AdBreak(marker, cueKey, marker.durationSeconds != null ? marker.durationSeconds * 1e3 : _this.expectedAdBreakDurationMs);
6489
7240
  }, ms);
6490
7241
  }
6491
7242
  },
@@ -6916,6 +7667,8 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
6916
7667
  this.adRequestQueue = [];
6917
7668
  this.inAdBreak = false;
6918
7669
  this.adDetectSentForCurrentBreak = false;
7670
+ this.activeScte35BreakKey = void 0;
7671
+ this.scheduledScte35BreakKey = void 0;
6919
7672
  this.expectedAdBreakDurationMs = void 0;
6920
7673
  this.currentAdBreakStartWallClockMs = void 0;
6921
7674
  this.clearAdStartTimer();