stormcloud-video-player 0.7.2 → 0.7.4

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.
@@ -385,185 +385,8 @@ __export(StormcloudVideoPlayer_exports, {
385
385
  module.exports = __toCommonJS(StormcloudVideoPlayer_exports);
386
386
  var import_react2 = __toESM(require("react"), 1);
387
387
  // src/player/StormcloudVideoPlayer.ts
388
- var import_hls2 = __toESM(require("hls.js"), 1);
388
+ var import_hls = __toESM(require("hls.js"), 1);
389
389
  // src/sdk/vastParser.ts
390
- function isHlsType(type) {
391
- return type === "application/x-mpegURL" || type.includes("m3u8");
392
- }
393
- function isMp4Type(type) {
394
- return type === "video/mp4" || type.includes("mp4");
395
- }
396
- function parseVastXml(xmlString) {
397
- var filter = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "all", logPrefix = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "[VastParser]";
398
- try {
399
- var _xmlDoc_querySelector, _xmlDoc_querySelector1, _xmlDoc_querySelector_textContent, _xmlDoc_querySelector2;
400
- var parser = new DOMParser();
401
- var xmlDoc = parser.parseFromString(xmlString, "text/xml");
402
- var parserError = xmlDoc.querySelector("parsererror");
403
- if (parserError) {
404
- console.error("".concat(logPrefix, " XML parsing error (malformed VAST XML):"), parserError.textContent);
405
- return null;
406
- }
407
- var adElement = xmlDoc.querySelector("Ad");
408
- if (!adElement) {
409
- console.warn("".concat(logPrefix, " No Ad element found in VAST XML"));
410
- return null;
411
- }
412
- var adId = adElement.getAttribute("id") || "unknown";
413
- var title = ((_xmlDoc_querySelector = xmlDoc.querySelector("AdTitle")) === null || _xmlDoc_querySelector === void 0 ? void 0 : _xmlDoc_querySelector.textContent) || "Ad";
414
- var isNoAdAvailable = adId === "empty" || title.toLowerCase().includes("no ad available") || title.toLowerCase() === "no ad available";
415
- var durationText = ((_xmlDoc_querySelector1 = xmlDoc.querySelector("Duration")) === null || _xmlDoc_querySelector1 === void 0 ? void 0 : _xmlDoc_querySelector1.textContent) || "00:00:30";
416
- var durationParts = durationText.split(":");
417
- var duration = parseInt(durationParts[0] || "0", 10) * 3600 + parseInt(durationParts[1] || "0", 10) * 60 + Math.round(parseFloat(durationParts[2] || "0"));
418
- var mediaFileElements = xmlDoc.querySelectorAll("MediaFile");
419
- var mediaFiles = [];
420
- console.log("".concat(logPrefix, " Found ").concat(mediaFileElements.length, " MediaFile element(s) in VAST XML"));
421
- mediaFileElements.forEach(function(mf, index) {
422
- var _mf_textContent;
423
- var type = mf.getAttribute("type") || "";
424
- var url = ((_mf_textContent = mf.textContent) === null || _mf_textContent === void 0 ? void 0 : _mf_textContent.trim()) || "";
425
- var width = mf.getAttribute("width") || "";
426
- var height = mf.getAttribute("height") || "";
427
- console.log("".concat(logPrefix, " MediaFile ").concat(index, ': type="').concat(type, '", url="').concat(url.substring(0, 80), '...", width="').concat(width, '", height="').concat(height, '"'));
428
- if (!url) {
429
- console.warn("".concat(logPrefix, " MediaFile ").concat(index, " has empty URL"));
430
- return;
431
- }
432
- var isHls = isHlsType(type);
433
- var isMp4 = isMp4Type(type);
434
- var accepted = false;
435
- if (filter === "hls-only") {
436
- accepted = isHls;
437
- } else if (filter === "mp4-first") {
438
- accepted = isMp4 || isHls;
439
- } else {
440
- accepted = true;
441
- }
442
- if (!accepted) {
443
- console.log("".concat(logPrefix, " MediaFile ").concat(index, ' ignored (type="').concat(type, '" not accepted by filter "').concat(filter, '")'));
444
- return;
445
- }
446
- var bitrateAttr = mf.getAttribute("bitrate");
447
- var bitrateValue = bitrateAttr ? parseInt(bitrateAttr, 10) : void 0;
448
- mediaFiles.push({
449
- url: url,
450
- type: type,
451
- width: parseInt(width || "1920", 10),
452
- height: parseInt(height || "1080", 10),
453
- bitrate: bitrateValue && bitrateValue > 0 ? bitrateValue : void 0
454
- });
455
- console.log("".concat(logPrefix, ' Added MediaFile: type="').concat(type, '" url="').concat(url.substring(0, 80), '..."'));
456
- });
457
- if (filter === "mp4-first" && mediaFiles.length > 1) {
458
- mediaFiles.sort(function(a, b) {
459
- var aIsMp4 = isMp4Type(a.type) ? 0 : 1;
460
- var bIsMp4 = isMp4Type(b.type) ? 0 : 1;
461
- return aIsMp4 - bIsMp4;
462
- });
463
- }
464
- if (mediaFiles.length === 0) {
465
- if (isNoAdAvailable) {
466
- console.warn("".concat(logPrefix, " No ads available (VAST response indicates no ads)"));
467
- } else {
468
- console.warn("".concat(logPrefix, " No compatible media files found in VAST XML"));
469
- }
470
- return null;
471
- }
472
- var trackingUrls = {
473
- impression: [],
474
- start: [],
475
- firstQuartile: [],
476
- midpoint: [],
477
- thirdQuartile: [],
478
- complete: [],
479
- mute: [],
480
- unmute: [],
481
- pause: [],
482
- resume: [],
483
- fullscreen: [],
484
- exitFullscreen: [],
485
- skip: [],
486
- error: []
487
- };
488
- xmlDoc.querySelectorAll("Impression").forEach(function(el) {
489
- var _el_textContent;
490
- var url = (_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim();
491
- if (url) trackingUrls.impression.push(url);
492
- });
493
- xmlDoc.querySelectorAll("Tracking").forEach(function(el) {
494
- var _el_textContent;
495
- var event = el.getAttribute("event");
496
- var url = (_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim();
497
- if (event && url) {
498
- var eventKey = event;
499
- if (trackingUrls[eventKey]) {
500
- trackingUrls[eventKey].push(url);
501
- }
502
- }
503
- });
504
- var clickThrough = (_xmlDoc_querySelector2 = xmlDoc.querySelector("ClickThrough")) === null || _xmlDoc_querySelector2 === void 0 ? void 0 : (_xmlDoc_querySelector_textContent = _xmlDoc_querySelector2.textContent) === null || _xmlDoc_querySelector_textContent === void 0 ? void 0 : _xmlDoc_querySelector_textContent.trim();
505
- return {
506
- id: adId,
507
- title: title,
508
- duration: duration,
509
- mediaFiles: mediaFiles,
510
- trackingUrls: trackingUrls,
511
- clickThrough: clickThrough
512
- };
513
- } catch (error) {
514
- console.error("".concat(logPrefix, " Error parsing VAST XML:"), error);
515
- return null;
516
- }
517
- }
518
- function fetchAndParseVastAd(vastTagUrl) {
519
- var filter = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "all", logPrefix = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "[VastParser]";
520
- return _async_to_generator(function() {
521
- var response, vastXml;
522
- return _ts_generator(this, function(_state) {
523
- switch(_state.label){
524
- case 0:
525
- return [
526
- 4,
527
- fetch(vastTagUrl, {
528
- mode: "cors",
529
- credentials: "include",
530
- headers: {
531
- Accept: "application/xml, text/xml, */*"
532
- },
533
- referrerPolicy: "no-referrer-when-downgrade"
534
- })
535
- ];
536
- case 1:
537
- response = _state.sent();
538
- if (!response.ok) {
539
- throw new Error("Failed to fetch VAST: ".concat(response.statusText));
540
- }
541
- return [
542
- 4,
543
- response.text()
544
- ];
545
- case 2:
546
- vastXml = _state.sent();
547
- console.log("".concat(logPrefix, " VAST XML received"));
548
- console.log("".concat(logPrefix, " VAST XML content (first 2000 chars):"), vastXml.substring(0, 2e3));
549
- return [
550
- 2,
551
- parseVastXml(vastXml, filter, logPrefix)
552
- ];
553
- }
554
- });
555
- })();
556
- }
557
- function createEmptyTrackingState() {
558
- return {
559
- impression: false,
560
- start: false,
561
- firstQuartile: false,
562
- midpoint: false,
563
- thirdQuartile: false,
564
- complete: false
565
- };
566
- }
567
390
  function firePixelWithRetry(url) {
568
391
  var retries = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 2, delayMs = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 500, logPrefix = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : "[VastParser]";
569
392
  return _async_to_generator(function() {
@@ -686,16 +509,164 @@ function fireTrackingPixels(urls, sessionId) {
686
509
  }
687
510
  });
688
511
  }
689
- // src/sdk/vastManager.ts
690
- var VAST_TAG_URL = "https://pubads.g.doubleclick.net/gampad/ads?iu=/21821455290/Airy-Android&description_url=http%3A%2F%2Fairy.tv&tfcd=0&npa=0&sz=1x1%7C300x250%7C400x300%7C640x480&gdfp_req=1&unviewed_position_start=1&correlator=[placeholder]&vpos=preroll&output=vast&env=vp&vpmute=0&vpa=click";
691
- var DEFAULT_TIMEOUT_MS = 5e3;
692
- var MAX_RETRIES = 3;
693
- var RETRY_BACKOFF_MS = 1500;
694
- function createVastManager() {
695
- var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
696
- var _options_debug;
697
- var initialized = false;
698
- var debug = (_options_debug = options.debug) !== null && _options_debug !== void 0 ? _options_debug : false;
512
+ // src/sdk/adstormPlayer.ts
513
+ var SUPPORTED_VIDEO_EXTENSIONS = [
514
+ ".mp4",
515
+ ".webm",
516
+ ".ogg",
517
+ ".m3u8",
518
+ ".ts"
519
+ ];
520
+ var UNSUPPORTED_VIDEO_EXTENSIONS = [
521
+ ".flv",
522
+ ".f4v",
523
+ ".swf",
524
+ ".wmv",
525
+ ".avi",
526
+ ".mov",
527
+ ".mkv"
528
+ ];
529
+ var REQUEST_TIMEOUT_MS = 5e3;
530
+ var REQUEST_MAX_RETRIES = 3;
531
+ var REQUEST_RETRY_BACKOFF_MS = 1500;
532
+ var AD_LAYER_Z_INDEX = "2147483646";
533
+ var COUNTDOWN_Z_INDEX = "2147483647";
534
+ var STALL_TIMEOUT_MS = 8e3;
535
+ function getFileExtension(url) {
536
+ try {
537
+ var pathname = new URL(url, "http://dummy").pathname;
538
+ var lastDot = pathname.lastIndexOf(".");
539
+ if (lastDot === -1) return "";
540
+ return pathname.slice(lastDot).toLowerCase();
541
+ } catch (unused) {
542
+ var lastDot1 = url.lastIndexOf(".");
543
+ if (lastDot1 === -1) return "";
544
+ var ext = url.slice(lastDot1).split(/[?#]/)[0];
545
+ return (ext || "").toLowerCase();
546
+ }
547
+ }
548
+ function isUnsupportedFormat(url) {
549
+ var ext = getFileExtension(url);
550
+ return UNSUPPORTED_VIDEO_EXTENSIONS.indexOf(ext) !== -1;
551
+ }
552
+ function replaceFlvExtension(url) {
553
+ var ext = getFileExtension(url);
554
+ if (ext === ".flv") {
555
+ return url.replace(/\.flv(\?|$)/i, ".mp4$1");
556
+ }
557
+ return url;
558
+ }
559
+ function isSupportedFormat(url, mimeType) {
560
+ if (isUnsupportedFormat(url)) {
561
+ return false;
562
+ }
563
+ var ext = getFileExtension(url);
564
+ if (SUPPORTED_VIDEO_EXTENSIONS.indexOf(ext) !== -1) {
565
+ return true;
566
+ }
567
+ if (ext === "" || ext === ".") {
568
+ return mimeType.includes("video/mp4") || mimeType.includes("video/webm") || mimeType.includes("m3u8") || mimeType.includes("application/x-mpegurl");
569
+ }
570
+ return false;
571
+ }
572
+ function createAdStormPlayer(contentVideo, options) {
573
+ var licenseKey = options.licenseKey, _options_debug = options.debug, debug = _options_debug === void 0 ? false : _options_debug;
574
+ var adPlaying = false;
575
+ var originalMutedState = false;
576
+ var originalVolume = Math.max(0, Math.min(1, contentVideo.volume || 1));
577
+ var listeners = /* @__PURE__ */ new Map();
578
+ var adVideoElement;
579
+ var adContainerEl;
580
+ var adCountdownEl;
581
+ var currentAd;
582
+ var destroyed = false;
583
+ var tornDown = false;
584
+ var continueLiveStreamDuringAds = false;
585
+ var sessionId;
586
+ var adStallTimerId;
587
+ var adCountdownTimerId;
588
+ var adHideTimerId;
589
+ var lastCountdownSecond = -1;
590
+ var adListenersBound = false;
591
+ var parentPositionOverridden = false;
592
+ var adHandlers = {
593
+ timeupdate: function timeupdate() {
594
+ if (!currentAd || !adVideoElement || destroyed || tornDown) return;
595
+ var progress = adVideoElement.currentTime / currentAd.duration;
596
+ if (progress >= 0.25 && !trackingFired.firstQuartile) {
597
+ trackingFired.firstQuartile = true;
598
+ fireTrackingPixels2(currentAd.trackingUrls.firstQuartile);
599
+ }
600
+ if (progress >= 0.5 && !trackingFired.midpoint) {
601
+ trackingFired.midpoint = true;
602
+ fireTrackingPixels2(currentAd.trackingUrls.midpoint);
603
+ }
604
+ if (progress >= 0.75 && !trackingFired.thirdQuartile) {
605
+ trackingFired.thirdQuartile = true;
606
+ fireTrackingPixels2(currentAd.trackingUrls.thirdQuartile);
607
+ }
608
+ updateAdCountdown();
609
+ },
610
+ playing: function playing() {
611
+ clearAdStallTimer();
612
+ if (!currentAd || trackingFired.start || destroyed || tornDown) return;
613
+ trackingFired.start = true;
614
+ fireTrackingPixels2(currentAd.trackingUrls.start);
615
+ startAdCountdown();
616
+ log("Ad started playing");
617
+ },
618
+ ended: function ended() {
619
+ if (!currentAd || trackingFired.complete || destroyed || tornDown) return;
620
+ trackingFired.complete = true;
621
+ fireTrackingPixels2(currentAd.trackingUrls.complete);
622
+ log("Ad completed");
623
+ handleAdComplete();
624
+ },
625
+ error: function error(e) {
626
+ if (destroyed || tornDown) return;
627
+ console.error("[AdStormPlayer] Ad video error:", e);
628
+ if (currentAd) fireTrackingPixels2(currentAd.trackingUrls.error);
629
+ handleAdError();
630
+ },
631
+ waiting: function waiting() {
632
+ clearAdStallTimer();
633
+ adStallTimerId = setTimeout(function() {
634
+ adStallTimerId = void 0;
635
+ if (!adPlaying || destroyed || tornDown) return;
636
+ console.warn("[AdStormPlayer] Ad playback stalled too long");
637
+ handleAdError();
638
+ }, STALL_TIMEOUT_MS);
639
+ },
640
+ volumechange: function volumechange() {
641
+ if (!currentAd || !adVideoElement || destroyed || tornDown) return;
642
+ if (adVideoElement.muted || adVideoElement.volume <= 0) {
643
+ fireTrackingPixels2(currentAd.trackingUrls.mute);
644
+ } else {
645
+ fireTrackingPixels2(currentAd.trackingUrls.unmute);
646
+ }
647
+ },
648
+ pause: function pause() {
649
+ if (!currentAd || !adVideoElement || destroyed || tornDown) return;
650
+ if (!adVideoElement.ended) {
651
+ fireTrackingPixels2(currentAd.trackingUrls.pause);
652
+ }
653
+ },
654
+ play: function play() {
655
+ if (!currentAd || !adVideoElement || destroyed || tornDown) return;
656
+ if (adVideoElement.currentTime > 0) {
657
+ fireTrackingPixels2(currentAd.trackingUrls.resume);
658
+ }
659
+ }
660
+ };
661
+ var trackingFired = {
662
+ impression: false,
663
+ start: false,
664
+ firstQuartile: false,
665
+ midpoint: false,
666
+ thirdQuartile: false,
667
+ complete: false
668
+ };
669
+ var preloadSlots = /* @__PURE__ */ new Map();
699
670
  function log() {
700
671
  for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
701
672
  args[_key] = arguments[_key];
@@ -703,58 +674,340 @@ function createVastManager() {
703
674
  if (debug) {
704
675
  var _console;
705
676
  (_console = console).log.apply(_console, [
706
- "[VastManager]"
677
+ "[AdStormPlayer]"
707
678
  ].concat(_to_consumable_array(args)));
708
679
  }
709
680
  }
710
- function warn() {
711
- for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
712
- args[_key] = arguments[_key];
681
+ function emit(event, payload) {
682
+ var set = listeners.get(event);
683
+ if (!set) return;
684
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
685
+ try {
686
+ for(var _iterator = Array.from(set)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
687
+ var fn = _step.value;
688
+ try {
689
+ fn(payload);
690
+ } catch (error) {
691
+ console.warn("[AdStormPlayer] Error in event listener for ".concat(event, ":"), error);
692
+ }
693
+ }
694
+ } catch (err) {
695
+ _didIteratorError = true;
696
+ _iteratorError = err;
697
+ } finally{
698
+ try {
699
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
700
+ _iterator.return();
701
+ }
702
+ } finally{
703
+ if (_didIteratorError) {
704
+ throw _iteratorError;
705
+ }
706
+ }
713
707
  }
714
- var _console;
715
- (_console = console).warn.apply(_console, [
716
- "[VastManager]"
717
- ].concat(_to_consumable_array(args)));
718
708
  }
719
- function initialize() {
720
- return _async_to_generator(function() {
721
- return _ts_generator(this, function(_state) {
722
- if (initialized) return [
723
- 2
724
- ];
725
- initialized = true;
726
- log("Initialized, VAST tag URL:", VAST_TAG_URL.split("?")[0]);
727
- return [
728
- 2
729
- ];
709
+ function fireTrackingPixels2(urls) {
710
+ fireTrackingPixels(urls, sessionId, "[AdStormPlayer]");
711
+ }
712
+ function clearAdStallTimer() {
713
+ if (adStallTimerId) {
714
+ clearTimeout(adStallTimerId);
715
+ adStallTimerId = void 0;
716
+ }
717
+ }
718
+ function clearAdCountdownTimer() {
719
+ if (adCountdownTimerId) {
720
+ clearInterval(adCountdownTimerId);
721
+ adCountdownTimerId = void 0;
722
+ }
723
+ lastCountdownSecond = -1;
724
+ }
725
+ function updateAdCountdown() {
726
+ if (!adCountdownEl || !adVideoElement || !currentAd || !adPlaying) return;
727
+ var remainingSec = Math.max(0, Math.ceil((currentAd.duration || 0) - adVideoElement.currentTime));
728
+ if (remainingSec === lastCountdownSecond) return;
729
+ lastCountdownSecond = remainingSec;
730
+ adCountdownEl.textContent = "Ad ".concat(remainingSec, "s");
731
+ emit("ad_countdown", {
732
+ remainingSec: remainingSec,
733
+ durationSec: currentAd.duration,
734
+ currentTimeSec: adVideoElement.currentTime
735
+ });
736
+ }
737
+ function startAdCountdown() {
738
+ clearAdCountdownTimer();
739
+ updateAdCountdown();
740
+ adCountdownTimerId = setInterval(updateAdCountdown, 250);
741
+ }
742
+ function generateSessionId() {
743
+ return "adstorm-".concat(Date.now(), "-").concat(Math.random().toString(36).slice(2, 10));
744
+ }
745
+ function bindAdEventListeners() {
746
+ if (!adVideoElement || adListenersBound) return;
747
+ adVideoElement.addEventListener("timeupdate", adHandlers.timeupdate);
748
+ adVideoElement.addEventListener("playing", adHandlers.playing);
749
+ adVideoElement.addEventListener("ended", adHandlers.ended);
750
+ adVideoElement.addEventListener("error", adHandlers.error);
751
+ adVideoElement.addEventListener("waiting", adHandlers.waiting);
752
+ adVideoElement.addEventListener("volumechange", adHandlers.volumechange);
753
+ adVideoElement.addEventListener("pause", adHandlers.pause);
754
+ adVideoElement.addEventListener("play", adHandlers.play);
755
+ adListenersBound = true;
756
+ }
757
+ function unbindAdEventListeners() {
758
+ if (!adVideoElement || !adListenersBound) return;
759
+ adVideoElement.removeEventListener("timeupdate", adHandlers.timeupdate);
760
+ adVideoElement.removeEventListener("playing", adHandlers.playing);
761
+ adVideoElement.removeEventListener("ended", adHandlers.ended);
762
+ adVideoElement.removeEventListener("error", adHandlers.error);
763
+ adVideoElement.removeEventListener("waiting", adHandlers.waiting);
764
+ adVideoElement.removeEventListener("volumechange", adHandlers.volumechange);
765
+ adVideoElement.removeEventListener("pause", adHandlers.pause);
766
+ adVideoElement.removeEventListener("play", adHandlers.play);
767
+ adListenersBound = false;
768
+ }
769
+ function teardownCurrentPlayback() {
770
+ unbindAdEventListeners();
771
+ clearAdStallTimer();
772
+ clearAdCountdownTimer();
773
+ if (!adVideoElement) return;
774
+ adVideoElement.pause();
775
+ adVideoElement.removeAttribute("src");
776
+ adVideoElement.load();
777
+ }
778
+ function buildVastUrl(durationSeconds, metadata) {
779
+ var baseUrl = "https://adstorm.co/api-adstorm-dev/adstorm/vast/".concat(licenseKey, "/pod");
780
+ var defaultMetadata = {
781
+ video: {
782
+ codec: "h264",
783
+ width: contentVideo.videoWidth || 1280,
784
+ height: contentVideo.videoHeight || 720,
785
+ fps: 29.97,
786
+ bitrate: 5e3,
787
+ profile: "high",
788
+ pix_fmt: "yuv420p",
789
+ has_b_frames: 0
790
+ },
791
+ audio: {
792
+ codec: "aac",
793
+ sample_rate: 48e3,
794
+ bitrate: 128
795
+ }
796
+ };
797
+ var finalMetadata = metadata || defaultMetadata;
798
+ var metadataStr = encodeURIComponent(JSON.stringify(finalMetadata));
799
+ return "".concat(baseUrl, "?duration=").concat(Math.ceil(durationSeconds), "&metadata=").concat(metadataStr);
800
+ }
801
+ function parseVastXml(xmlString) {
802
+ var ads = [];
803
+ try {
804
+ var parser = new DOMParser();
805
+ var xmlDoc = parser.parseFromString(xmlString, "text/xml");
806
+ var parserError = xmlDoc.querySelector("parsererror");
807
+ if (parserError) {
808
+ console.error("[AdStormPlayer] XML parsing error:", parserError.textContent);
809
+ return [];
810
+ }
811
+ var adElements = xmlDoc.querySelectorAll("Ad");
812
+ adElements.forEach(function(adElement) {
813
+ var _adElement_querySelector, _adElement_querySelector1, _adElement_querySelector_textContent, _adElement_querySelector2;
814
+ var adId = adElement.getAttribute("id") || "unknown";
815
+ var title = ((_adElement_querySelector = adElement.querySelector("AdTitle")) === null || _adElement_querySelector === void 0 ? void 0 : _adElement_querySelector.textContent) || "Ad";
816
+ var durationText = ((_adElement_querySelector1 = adElement.querySelector("Duration")) === null || _adElement_querySelector1 === void 0 ? void 0 : _adElement_querySelector1.textContent) || "00:00:30";
817
+ var durationParts = durationText.split(":");
818
+ var duration = parseInt(durationParts[0] || "0", 10) * 3600 + parseInt(durationParts[1] || "0", 10) * 60 + parseFloat(durationParts[2] || "0");
819
+ var mediaFileElements = adElement.querySelectorAll("MediaFile");
820
+ var mediaFiles = [];
821
+ mediaFileElements.forEach(function(mf) {
822
+ var _mf_textContent;
823
+ var type = mf.getAttribute("type") || "";
824
+ var url = ((_mf_textContent = mf.textContent) === null || _mf_textContent === void 0 ? void 0 : _mf_textContent.trim()) || "";
825
+ var width = parseInt(mf.getAttribute("width") || "1920", 10);
826
+ var height = parseInt(mf.getAttribute("height") || "1080", 10);
827
+ var bitrate = mf.getAttribute("bitrate") ? parseInt(mf.getAttribute("bitrate"), 10) : void 0;
828
+ if (!url) {
829
+ log("Skipping empty MediaFile URL");
830
+ return;
831
+ }
832
+ var originalUrl = url;
833
+ url = replaceFlvExtension(url);
834
+ if (url !== originalUrl) {
835
+ log("Converted FLV to MP4: ".concat(originalUrl, " -> ").concat(url));
836
+ }
837
+ if (isUnsupportedFormat(url)) {
838
+ var ext = getFileExtension(url);
839
+ log("Skipping unsupported format: ".concat(url, " (extension: ").concat(ext, ", declared type: ").concat(type, ")"));
840
+ return;
841
+ }
842
+ if (isSupportedFormat(url, type)) {
843
+ mediaFiles.push({
844
+ url: url,
845
+ type: type,
846
+ width: width,
847
+ height: height,
848
+ bitrate: bitrate
849
+ });
850
+ log("Found media file: ".concat(url, " (").concat(type, ", ").concat(width, "x").concat(height, ")"));
851
+ } else {
852
+ log("Skipping incompatible media file: ".concat(url, " (type: ").concat(type, ")"));
853
+ }
854
+ });
855
+ if (mediaFiles.length === 0) {
856
+ log("No valid media files found in ad:", adId);
857
+ return;
858
+ }
859
+ var trackingUrls = {
860
+ impression: [],
861
+ start: [],
862
+ firstQuartile: [],
863
+ midpoint: [],
864
+ thirdQuartile: [],
865
+ complete: [],
866
+ mute: [],
867
+ unmute: [],
868
+ pause: [],
869
+ resume: [],
870
+ error: []
871
+ };
872
+ adElement.querySelectorAll("Impression").forEach(function(el) {
873
+ var _el_textContent;
874
+ var url = (_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim();
875
+ if (url) trackingUrls.impression.push(url);
876
+ });
877
+ adElement.querySelectorAll("Tracking").forEach(function(el) {
878
+ var _el_textContent;
879
+ var event = el.getAttribute("event");
880
+ var url = (_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim();
881
+ if (event && url) {
882
+ var eventKey = event;
883
+ if (trackingUrls[eventKey]) {
884
+ trackingUrls[eventKey].push(url);
885
+ }
886
+ }
887
+ });
888
+ var clickThrough = (_adElement_querySelector2 = adElement.querySelector("ClickThrough")) === null || _adElement_querySelector2 === void 0 ? void 0 : (_adElement_querySelector_textContent = _adElement_querySelector2.textContent) === null || _adElement_querySelector_textContent === void 0 ? void 0 : _adElement_querySelector_textContent.trim();
889
+ ads.push({
890
+ id: adId,
891
+ title: title,
892
+ duration: duration,
893
+ mediaFiles: mediaFiles,
894
+ trackingUrls: trackingUrls,
895
+ clickThrough: clickThrough
896
+ });
897
+ log("Parsed ad: ".concat(title, ", duration: ").concat(duration, "s, media files: ").concat(mediaFiles.length));
730
898
  });
731
- })();
899
+ } catch (error) {
900
+ console.error("[AdStormPlayer] Error parsing VAST XML:", error);
901
+ }
902
+ return ads;
903
+ }
904
+ function selectBestMediaFile(mediaFiles) {
905
+ if (mediaFiles.length === 0) return null;
906
+ if (mediaFiles.length === 1) return mediaFiles[0];
907
+ var mp4Files = mediaFiles.filter(function(mf) {
908
+ return mf.type.includes("video/mp4");
909
+ });
910
+ var candidates = mp4Files.length > 0 ? mp4Files : mediaFiles;
911
+ var targetWidth = contentVideo.videoWidth || 1280;
912
+ var targetHeight = contentVideo.videoHeight || 720;
913
+ candidates.sort(function(a, b) {
914
+ var diffA = Math.abs(a.width - targetWidth) + Math.abs(a.height - targetHeight);
915
+ var diffB = Math.abs(b.width - targetWidth) + Math.abs(b.height - targetHeight);
916
+ return diffA - diffB;
917
+ });
918
+ return candidates[0] || null;
919
+ }
920
+ function createAdVideoElement() {
921
+ var video = document.createElement("video");
922
+ video.style.position = "absolute";
923
+ video.style.left = "0";
924
+ video.style.top = "0";
925
+ video.style.width = "100%";
926
+ video.style.height = "100%";
927
+ video.style.objectFit = "contain";
928
+ video.style.backgroundColor = "#000";
929
+ video.style.zIndex = "1";
930
+ video.playsInline = true;
931
+ video.preload = "auto";
932
+ video.muted = originalMutedState;
933
+ video.volume = originalMutedState ? 0 : originalVolume;
934
+ return video;
935
+ }
936
+ function setAdPlayingFlag(isPlaying) {
937
+ if (isPlaying) {
938
+ contentVideo.dataset.stormcloudAdPlaying = "true";
939
+ } else {
940
+ delete contentVideo.dataset.stormcloudAdPlaying;
941
+ }
942
+ }
943
+ function setupAdEventListeners() {
944
+ bindAdEventListeners();
945
+ }
946
+ function handleAdComplete() {
947
+ if (destroyed || tornDown) return;
948
+ log("Handling ad completion");
949
+ adPlaying = false;
950
+ setAdPlayingFlag(false);
951
+ clearAdStallTimer();
952
+ clearAdCountdownTimer();
953
+ if (adContainerEl) {
954
+ adContainerEl.style.opacity = "0";
955
+ adHideTimerId = setTimeout(function() {
956
+ if (adContainerEl) {
957
+ adContainerEl.style.display = "none";
958
+ adContainerEl.style.pointerEvents = "none";
959
+ }
960
+ }, 300);
961
+ }
962
+ contentVideo.style.visibility = "visible";
963
+ contentVideo.style.opacity = "1";
964
+ contentVideo.muted = originalMutedState;
965
+ contentVideo.volume = originalVolume;
966
+ currentAd = void 0;
967
+ emit("content_resume");
968
+ emit("all_ads_completed");
969
+ }
970
+ function handleAdError() {
971
+ if (destroyed || tornDown) return;
972
+ log("Handling ad error");
973
+ if (!adPlaying) return;
974
+ adPlaying = false;
975
+ setAdPlayingFlag(false);
976
+ clearAdStallTimer();
977
+ clearAdCountdownTimer();
978
+ contentVideo.muted = originalMutedState;
979
+ contentVideo.volume = originalVolume;
980
+ contentVideo.style.visibility = "visible";
981
+ contentVideo.style.opacity = "1";
982
+ if (adContainerEl) {
983
+ adContainerEl.style.display = "none";
984
+ adContainerEl.style.pointerEvents = "none";
985
+ }
986
+ currentAd = void 0;
987
+ emit("ad_error");
988
+ emit("content_resume");
732
989
  }
733
- function requestBids(_context) {
990
+ function fetchVastOnce(durationSeconds) {
734
991
  return _async_to_generator(function() {
735
- var correlator, url, controller, timeoutId, _ref, _ref1, _vastAd_mediaFiles_, _vastAd_mediaFiles_1, fetchOptions, response, vastXml, vastAd, bid, error;
992
+ var vastUrl, controller, timeoutId, requestInit, response, xmlText;
736
993
  return _ts_generator(this, function(_state) {
737
994
  switch(_state.label){
738
995
  case 0:
739
- if (!initialized) {
740
- throw new Error("VastManager not initialized. Call initialize() first.");
741
- }
742
- correlator = Math.floor(Math.random() * 1e12).toString();
743
- url = VAST_TAG_URL.replace("[placeholder]", correlator);
744
- log("Fetching VAST tag, correlator:", correlator);
996
+ vastUrl = buildVastUrl(durationSeconds);
997
+ log("Fetching VAST from:", vastUrl);
745
998
  controller = typeof AbortController !== "undefined" ? new AbortController() : null;
746
999
  timeoutId = setTimeout(function() {
747
1000
  return controller === null || controller === void 0 ? void 0 : controller.abort();
748
- }, DEFAULT_TIMEOUT_MS);
1001
+ }, REQUEST_TIMEOUT_MS);
749
1002
  _state.label = 1;
750
1003
  case 1:
751
1004
  _state.trys.push([
752
1005
  1,
753
- 4,
754
1006
  ,
1007
+ 4,
755
1008
  5
756
1009
  ]);
757
- fetchOptions = {
1010
+ requestInit = {
758
1011
  method: "GET",
759
1012
  mode: "cors",
760
1013
  credentials: "omit",
@@ -763,62 +1016,34 @@ function createVastManager() {
763
1016
  },
764
1017
  referrerPolicy: "no-referrer-when-downgrade"
765
1018
  };
766
- if (controller) fetchOptions.signal = controller.signal;
1019
+ if (controller) {
1020
+ requestInit.signal = controller.signal;
1021
+ }
767
1022
  return [
768
1023
  4,
769
- fetch(url, fetchOptions)
1024
+ fetch(vastUrl, requestInit)
770
1025
  ];
771
1026
  case 2:
772
1027
  response = _state.sent();
773
- clearTimeout(timeoutId);
774
1028
  if (!response.ok) {
775
- throw new Error("VAST request returned HTTP ".concat(response.status));
1029
+ throw new Error("Failed to fetch VAST: ".concat(response.status, " ").concat(response.statusText));
776
1030
  }
777
1031
  return [
778
1032
  4,
779
1033
  response.text()
780
1034
  ];
781
1035
  case 3:
782
- vastXml = _state.sent();
783
- log("VAST XML received, length:", vastXml.length);
784
- vastAd = parseVastXml(vastXml, "mp4-first", "[VastManager]");
785
- if (!vastAd) {
786
- log("VAST parsed but no usable ad found");
787
- return [
788
- 2,
789
- []
790
- ];
791
- }
792
- log("Ad parsed: id=".concat(vastAd.id, ", duration=").concat(vastAd.duration, "s, mediaFiles=").concat(vastAd.mediaFiles.length));
793
- bid = {
794
- bidder: "vast-direct",
795
- cpm: 0,
796
- vastXml: vastXml,
797
- width: (_ref = (_vastAd_mediaFiles_ = vastAd.mediaFiles[0]) === null || _vastAd_mediaFiles_ === void 0 ? void 0 : _vastAd_mediaFiles_.width) !== null && _ref !== void 0 ? _ref : 0,
798
- height: (_ref1 = (_vastAd_mediaFiles_1 = vastAd.mediaFiles[0]) === null || _vastAd_mediaFiles_1 === void 0 ? void 0 : _vastAd_mediaFiles_1.height) !== null && _ref1 !== void 0 ? _ref1 : 0,
799
- adId: vastAd.id,
800
- impId: correlator,
801
- creativeId: vastAd.id,
802
- currency: "USD",
803
- durationSec: vastAd.duration
804
- };
1036
+ xmlText = _state.sent();
1037
+ log("VAST response received, length:", xmlText.length);
805
1038
  return [
806
1039
  2,
807
- [
808
- bid
809
- ]
1040
+ parseVastXml(xmlText)
810
1041
  ];
811
1042
  case 4:
812
- error = _state.sent();
813
1043
  clearTimeout(timeoutId);
814
- if ((error === null || error === void 0 ? void 0 : error.name) === "AbortError") {
815
- warn("VAST request timed out after ".concat(DEFAULT_TIMEOUT_MS, "ms"));
816
- return [
817
- 2,
818
- []
819
- ];
820
- }
821
- throw error;
1044
+ return [
1045
+ 7
1046
+ ];
822
1047
  case 5:
823
1048
  return [
824
1049
  2
@@ -827,14 +1052,14 @@ function createVastManager() {
827
1052
  });
828
1053
  })();
829
1054
  }
830
- function requestBidsUntilResponse(context) {
1055
+ function fetchVast(durationSeconds) {
831
1056
  return _async_to_generator(function() {
832
1057
  var _loop, lastError, attempt, _ret;
833
1058
  return _ts_generator(this, function(_state) {
834
1059
  switch(_state.label){
835
1060
  case 0:
836
1061
  _loop = function(attempt) {
837
- var bids, err, delay;
1062
+ var ads, error, delay;
838
1063
  return _ts_generator(this, function(_state) {
839
1064
  switch(_state.label){
840
1065
  case 0:
@@ -846,39 +1071,39 @@ function createVastManager() {
846
1071
  ]);
847
1072
  return [
848
1073
  4,
849
- requestBids(context)
1074
+ fetchVastOnce(durationSeconds)
850
1075
  ];
851
1076
  case 1:
852
- bids = _state.sent();
853
- if (bids.length > 0) {
854
- log("requestBidsUntilResponse: got ".concat(bids.length, " ad(s) on attempt ").concat(attempt));
855
- return [
856
- 2,
857
- {
858
- v: bids
859
- }
860
- ];
861
- }
862
- log("requestBidsUntilResponse: no ads on attempt ".concat(attempt, "/").concat(MAX_RETRIES));
1077
+ ads = _state.sent();
1078
+ if (ads.length > 0) return [
1079
+ 2,
1080
+ {
1081
+ v: ads
1082
+ }
1083
+ ];
1084
+ log("No ad returned from VAST on attempt ".concat(attempt, "/").concat(REQUEST_MAX_RETRIES));
863
1085
  return [
864
1086
  3,
865
1087
  3
866
1088
  ];
867
1089
  case 2:
868
- err = _state.sent();
869
- lastError = err;
870
- warn("requestBidsUntilResponse: attempt ".concat(attempt, "/").concat(MAX_RETRIES, " failed:"), err);
1090
+ error = _state.sent();
1091
+ lastError = error;
1092
+ if ((error === null || error === void 0 ? void 0 : error.name) === "AbortError") {
1093
+ console.warn("[AdStormPlayer] VAST request timed out (".concat(REQUEST_TIMEOUT_MS, "ms), attempt ").concat(attempt, "/").concat(REQUEST_MAX_RETRIES));
1094
+ } else {
1095
+ console.warn("[AdStormPlayer] VAST request failed on attempt ".concat(attempt, "/").concat(REQUEST_MAX_RETRIES, ":"), error);
1096
+ }
871
1097
  return [
872
1098
  3,
873
1099
  3
874
1100
  ];
875
1101
  case 3:
876
- if (!(attempt < MAX_RETRIES)) return [
1102
+ if (!(attempt < REQUEST_MAX_RETRIES)) return [
877
1103
  3,
878
1104
  5
879
1105
  ];
880
- delay = RETRY_BACKOFF_MS * attempt;
881
- log("requestBidsUntilResponse: waiting ".concat(delay, "ms before retry"));
1106
+ delay = REQUEST_RETRY_BACKOFF_MS * attempt;
882
1107
  return [
883
1108
  4,
884
1109
  new Promise(function(resolve) {
@@ -895,13 +1120,10 @@ function createVastManager() {
895
1120
  }
896
1121
  });
897
1122
  };
898
- if (!initialized) {
899
- throw new Error("VastManager not initialized. Call initialize() first.");
900
- }
901
1123
  attempt = 1;
902
1124
  _state.label = 1;
903
1125
  case 1:
904
- if (!(attempt <= MAX_RETRIES)) return [
1126
+ if (!(attempt <= REQUEST_MAX_RETRIES)) return [
905
1127
  3,
906
1128
  4
907
1129
  ];
@@ -923,7 +1145,9 @@ function createVastManager() {
923
1145
  1
924
1146
  ];
925
1147
  case 4:
926
- if (_instanceof(lastError, Error)) throw lastError;
1148
+ if (_instanceof(lastError, Error)) {
1149
+ throw lastError;
1150
+ }
927
1151
  return [
928
1152
  2,
929
1153
  []
@@ -932,971 +1156,468 @@ function createVastManager() {
932
1156
  });
933
1157
  })();
934
1158
  }
935
- function destroy() {
936
- initialized = false;
937
- log("Destroyed");
938
- }
939
- return {
940
- initialize: initialize,
941
- requestBids: requestBids,
942
- requestBidsUntilResponse: requestBidsUntilResponse,
943
- destroy: destroy,
944
- get isInitialized () {
945
- return initialized;
946
- }
947
- };
948
- }
949
- // src/sdk/vastAdLayer.ts
950
- var import_hls = __toESM(require("hls.js"), 1);
951
- var LOG = "[VastAdLayer]";
952
- function resolveBidToVastAd(winner, logPrefix) {
953
- if (winner.vastXml) {
954
- var ad = parseVastXml(winner.vastXml, "mp4-first", logPrefix);
955
- return Promise.resolve(ad);
956
- }
957
- if (winner.vastUrl) {
958
- return fetchAndParseVastAd(winner.vastUrl, "mp4-first", logPrefix);
959
- }
960
- return Promise.resolve(null);
961
- }
962
- function createVastAdLayer(contentVideo, options) {
963
- var _ref, _ref1, _ref2, _ref3, _ref4;
964
- var adPlaying = false;
965
- var originalMutedState = false;
966
- var originalVolume = Math.max(0, Math.min(1, contentVideo.volume || 1));
967
- var listeners = /* @__PURE__ */ new Map();
968
- var mainHlsInstance = options === null || options === void 0 ? void 0 : options.mainHlsInstance;
969
- var continueLiveStreamDuringAds = (_ref = options === null || options === void 0 ? void 0 : options.continueLiveStreamDuringAds) !== null && _ref !== void 0 ? _ref : false;
970
- var smartTVMode = (_ref1 = options === null || options === void 0 ? void 0 : options.smartTVMode) !== null && _ref1 !== void 0 ? _ref1 : false;
971
- var singleElementMode = (_ref2 = options === null || options === void 0 ? void 0 : options.singleElementMode) !== null && _ref2 !== void 0 ? _ref2 : false;
972
- var forceMP4Ads = (_ref3 = options === null || options === void 0 ? void 0 : options.forceMP4Ads) !== null && _ref3 !== void 0 ? _ref3 : smartTVMode || singleElementMode;
973
- var debug = (_ref4 = options === null || options === void 0 ? void 0 : options.debug) !== null && _ref4 !== void 0 ? _ref4 : false;
974
- var adVideoElement;
975
- var adHls;
976
- var adContainerEl;
977
- var currentAd;
978
- var sessionId;
979
- var destroyed = false;
980
- var tornDown = false;
981
- var trackingFired = createEmptyTrackingState();
982
- var adStallTimerId;
983
- var savedContentVideoStyles;
984
- var currentAdEventHandlers;
985
- var preloadSlots = /* @__PURE__ */ new Map();
986
- function emit(event, payload) {
987
- var set = listeners.get(event);
988
- if (!set) return;
989
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
990
- try {
991
- for(var _iterator = Array.from(set)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
992
- var fn = _step.value;
993
- try {
994
- fn(payload);
995
- } catch (error) {
996
- console.warn("".concat(LOG, " Error in event listener for ").concat(event, ":"), error);
997
- }
998
- }
999
- } catch (err) {
1000
- _didIteratorError = true;
1001
- _iteratorError = err;
1002
- } finally{
1003
- try {
1004
- if (!_iteratorNormalCompletion && _iterator.return != null) {
1005
- _iterator.return();
1006
- }
1007
- } finally{
1008
- if (_didIteratorError) {
1009
- throw _iteratorError;
1010
- }
1011
- }
1012
- }
1013
- }
1014
- function generateSessionId() {
1015
- return "session-".concat(Date.now(), "-").concat(Math.random().toString(36).substr(2, 9));
1016
- }
1017
- function fireTrackingPixels2(urls) {
1018
- fireTrackingPixels(urls, sessionId, LOG);
1019
- }
1020
- function getMainStreamQuality() {
1021
- if (!(mainHlsInstance === null || mainHlsInstance === void 0 ? void 0 : mainHlsInstance.levels)) return null;
1022
- var currentLevel = mainHlsInstance.currentLevel;
1023
- if (currentLevel === -1 || !mainHlsInstance.levels[currentLevel]) {
1024
- var autoLevel = mainHlsInstance.loadLevel;
1025
- if (autoLevel !== -1 && mainHlsInstance.levels[autoLevel]) {
1026
- var level2 = mainHlsInstance.levels[autoLevel];
1027
- return {
1028
- width: level2.width || 1920,
1029
- height: level2.height || 1080,
1030
- bitrate: level2.bitrate || 5e6
1031
- };
1032
- }
1033
- return null;
1034
- }
1035
- var level = mainHlsInstance.levels[currentLevel];
1036
- return {
1037
- width: level.width || 1920,
1038
- height: level.height || 1080,
1039
- bitrate: level.bitrate || 5e6
1040
- };
1041
- }
1042
- function selectBestMediaFile(mediaFiles) {
1043
- var _ref;
1044
- var _scoredFiles_;
1045
- if (mediaFiles.length === 0) throw new Error("No media files available");
1046
- var candidates = mediaFiles;
1047
- if (forceMP4Ads) {
1048
- var mp4Only = candidates.filter(function(f) {
1049
- return !isHlsMediaFile(f);
1050
- });
1051
- if (mp4Only.length > 0) {
1052
- candidates = mp4Only;
1053
- if (debug) console.log("".concat(LOG, " forceMP4Ads: filtered to ").concat(mp4Only.length, " MP4-only file(s)"));
1054
- } else if (debug) {
1055
- console.warn("".concat(LOG, " forceMP4Ads: no MP4 files available, falling back to all media files"));
1056
- }
1057
- } else {
1058
- var mp4Only1 = candidates.filter(function(f) {
1059
- return !isHlsMediaFile(f);
1060
- });
1061
- if (mp4Only1.length > 0) {
1062
- candidates = mp4Only1;
1063
- if (debug) console.log("".concat(LOG, " Preferring ").concat(mp4Only1.length, " MP4 file(s) over HLS (mp4-first)"));
1064
- }
1065
- }
1066
- var firstFile = candidates[0];
1067
- if (candidates.length === 1) return firstFile;
1068
- var mainQuality = getMainStreamQuality();
1069
- if (!mainQuality) {
1070
- if (debug) console.log("".concat(LOG, " No main stream quality info, using first media file"));
1071
- return firstFile;
1072
- }
1073
- var scoredFiles = candidates.map(function(file) {
1074
- var widthDiff = Math.abs(file.width - mainQuality.width);
1075
- var heightDiff = Math.abs(file.height - mainQuality.height);
1076
- var resolutionDiff = widthDiff + heightDiff;
1077
- var fileBitrate = (file.bitrate || 5e3) * 1e3;
1078
- var bitrateDiff = Math.abs(fileBitrate - mainQuality.bitrate);
1079
- var score = resolutionDiff * 2 + bitrateDiff / 1e3;
1080
- return {
1081
- file: file,
1082
- score: score
1083
- };
1084
- });
1085
- scoredFiles.sort(function(a, b) {
1086
- return a.score - b.score;
1087
- });
1088
- return (_ref = (_scoredFiles_ = scoredFiles[0]) === null || _scoredFiles_ === void 0 ? void 0 : _scoredFiles_.file) !== null && _ref !== void 0 ? _ref : firstFile;
1089
- }
1090
- function isHlsMediaFile(file) {
1091
- return file.type === "application/x-mpegURL" || file.type.includes("m3u8");
1092
- }
1093
- function createAdVideoElement() {
1094
- var video = document.createElement("video");
1095
- video.style.position = "absolute";
1096
- video.style.left = "0";
1097
- video.style.top = "0";
1098
- video.style.width = "100%";
1099
- video.style.height = "100%";
1100
- video.style.objectFit = "cover";
1101
- video.style.backgroundColor = "#000";
1102
- video.playsInline = true;
1103
- video.muted = false;
1104
- video.volume = 1;
1105
- return video;
1106
- }
1107
- function clearAdStallTimer() {
1108
- if (adStallTimerId != null) {
1109
- clearTimeout(adStallTimerId);
1110
- adStallTimerId = void 0;
1111
- }
1112
- }
1113
- function removeAdEventListeners() {
1114
- clearAdStallTimer();
1115
- if (!currentAdEventHandlers || !adVideoElement) return;
1116
- var el = adVideoElement;
1117
- el.removeEventListener("timeupdate", currentAdEventHandlers.timeupdate);
1118
- el.removeEventListener("playing", currentAdEventHandlers.playing);
1119
- el.removeEventListener("ended", currentAdEventHandlers.ended);
1120
- el.removeEventListener("error", currentAdEventHandlers.error);
1121
- el.removeEventListener("volumechange", currentAdEventHandlers.volumechange);
1122
- el.removeEventListener("pause", currentAdEventHandlers.pause);
1123
- el.removeEventListener("play", currentAdEventHandlers.play);
1124
- el.removeEventListener("waiting", currentAdEventHandlers.waiting);
1125
- currentAdEventHandlers = void 0;
1126
- }
1127
- function setupAdEventListeners() {
1128
- if (!adVideoElement) return;
1129
- removeAdEventListeners();
1130
- var handlers = {
1131
- timeupdate: function timeupdate() {
1132
- var ad = currentAd;
1133
- if (!ad || !adVideoElement) return;
1134
- var progress = adVideoElement.currentTime / ad.duration;
1135
- if (progress >= 0.25 && !trackingFired.firstQuartile) {
1136
- trackingFired.firstQuartile = true;
1137
- fireTrackingPixels2(ad.trackingUrls.firstQuartile);
1138
- }
1139
- if (progress >= 0.5 && !trackingFired.midpoint) {
1140
- trackingFired.midpoint = true;
1141
- fireTrackingPixels2(ad.trackingUrls.midpoint);
1142
- }
1143
- if (progress >= 0.75 && !trackingFired.thirdQuartile) {
1144
- trackingFired.thirdQuartile = true;
1145
- fireTrackingPixels2(ad.trackingUrls.thirdQuartile);
1146
- }
1147
- },
1148
- playing: function playing() {
1149
- clearAdStallTimer();
1150
- var ad = currentAd;
1151
- if (!ad || trackingFired.start) return;
1152
- trackingFired.start = true;
1153
- fireTrackingPixels2(ad.trackingUrls.start);
1154
- if (debug) console.log("".concat(LOG, " Ad started playing"));
1155
- },
1156
- ended: function ended() {
1157
- if (tornDown || !currentAd || trackingFired.complete) return;
1158
- trackingFired.complete = true;
1159
- fireTrackingPixels2(currentAd.trackingUrls.complete);
1160
- if (debug) console.log("".concat(LOG, " Ad completed"));
1161
- handleAdComplete();
1162
- },
1163
- error: function error(e) {
1164
- if (tornDown) return;
1165
- console.error("".concat(LOG, " Ad video error:"), e);
1166
- if (currentAd) fireTrackingPixels2(currentAd.trackingUrls.error);
1167
- handleAdError();
1168
- },
1169
- volumechange: function volumechange() {
1170
- if (!currentAd || !adVideoElement) return;
1171
- if (adVideoElement.muted) {
1172
- fireTrackingPixels2(currentAd.trackingUrls.mute);
1173
- } else {
1174
- fireTrackingPixels2(currentAd.trackingUrls.unmute);
1175
- }
1176
- },
1177
- pause: function pause() {
1178
- if (currentAd && adVideoElement && !adVideoElement.ended) {
1179
- fireTrackingPixels2(currentAd.trackingUrls.pause);
1180
- }
1181
- },
1182
- play: function play() {
1183
- if (currentAd && adVideoElement && adVideoElement.currentTime > 0) {
1184
- fireTrackingPixels2(currentAd.trackingUrls.resume);
1185
- }
1186
- },
1187
- waiting: function waiting() {
1188
- clearAdStallTimer();
1189
- adStallTimerId = setTimeout(function() {
1190
- adStallTimerId = void 0;
1191
- if (adPlaying) {
1192
- if (debug) console.warn("".concat(LOG, " Ad video stalled for too long, treating as error"));
1193
- handleAdError();
1194
- }
1195
- }, 8e3);
1196
- }
1197
- };
1198
- adVideoElement.addEventListener("timeupdate", handlers.timeupdate);
1199
- adVideoElement.addEventListener("playing", handlers.playing);
1200
- adVideoElement.addEventListener("ended", handlers.ended);
1201
- adVideoElement.addEventListener("error", handlers.error);
1202
- adVideoElement.addEventListener("volumechange", handlers.volumechange);
1203
- adVideoElement.addEventListener("pause", handlers.pause);
1204
- adVideoElement.addEventListener("play", handlers.play);
1205
- adVideoElement.addEventListener("waiting", handlers.waiting);
1206
- currentAdEventHandlers = handlers;
1207
- }
1208
- function setAdPlayingFlag(isPlaying) {
1209
- if (isPlaying) {
1210
- contentVideo.dataset.stormcloudAdPlaying = "true";
1211
- } else {
1212
- delete contentVideo.dataset.stormcloudAdPlaying;
1213
- }
1214
- }
1215
- function applyContentVideoAdCoverStyles() {
1216
- if (!singleElementMode) return;
1217
- savedContentVideoStyles = {
1218
- objectFit: contentVideo.style.objectFit,
1219
- width: contentVideo.style.width,
1220
- height: contentVideo.style.height
1221
- };
1222
- contentVideo.style.objectFit = "cover";
1223
- contentVideo.style.width = "100%";
1224
- contentVideo.style.height = "100%";
1225
- }
1226
- function restoreContentVideoStyles() {
1227
- if (!singleElementMode || !savedContentVideoStyles) return;
1228
- contentVideo.style.objectFit = savedContentVideoStyles.objectFit;
1229
- contentVideo.style.width = savedContentVideoStyles.width;
1230
- contentVideo.style.height = savedContentVideoStyles.height;
1231
- savedContentVideoStyles = void 0;
1232
- }
1233
- function handleAdComplete() {
1234
- if (tornDown) return;
1235
- clearAdStallTimer();
1236
- if (debug) console.log("".concat(LOG, " Handling ad completion"));
1237
- adPlaying = false;
1238
- setAdPlayingFlag(false);
1239
- restoreContentVideoStyles();
1240
- if (adContainerEl) {
1241
- adContainerEl.style.display = "none";
1242
- adContainerEl.style.pointerEvents = "none";
1243
- }
1244
- emit("ad_impression");
1245
- emit("content_resume");
1246
- }
1247
- function handleAdError() {
1248
- if (tornDown) return;
1249
- if (!adPlaying) return;
1250
- clearAdStallTimer();
1251
- if (debug) console.log("".concat(LOG, " Handling ad error"));
1252
- adPlaying = false;
1253
- setAdPlayingFlag(false);
1254
- restoreContentVideoStyles();
1255
- if (adContainerEl) {
1256
- adContainerEl.style.display = "none";
1257
- adContainerEl.style.pointerEvents = "none";
1258
- }
1259
- emit("ad_error");
1260
- }
1261
- function teardownCurrentPlayback() {
1262
- removeAdEventListeners();
1263
- if (adHls) {
1264
- adHls.destroy();
1265
- adHls = void 0;
1266
- }
1267
- if (adVideoElement) {
1268
- if (singleElementMode && adVideoElement === contentVideo) {
1269
- contentVideo.pause();
1270
- } else {
1271
- adVideoElement.pause();
1272
- adVideoElement.removeAttribute("src");
1273
- adVideoElement.load();
1274
- }
1275
- }
1276
- }
1277
- function startNativePlayback(mediaFile) {
1278
- if (!adVideoElement) return;
1279
- if (debug) console.log("".concat(LOG, " Starting native MP4 playback: ").concat(mediaFile.url));
1280
- adVideoElement.src = mediaFile.url;
1281
- adVideoElement.load();
1282
- adVideoElement.play().catch(function(error) {
1283
- console.error("".concat(LOG, " Error starting native ad playback:"), error);
1284
- handleAdError();
1285
- });
1286
- }
1287
- function startHlsPlayback(mediaFile) {
1288
- if (!adVideoElement) return;
1289
- if (debug) console.log("".concat(LOG, " Starting HLS playback: ").concat(mediaFile.url));
1290
- if (import_hls.default.isSupported()) {
1291
- if (adHls) {
1292
- adHls.destroy();
1293
- adHls = void 0;
1294
- }
1295
- adHls = new import_hls.default({
1296
- enableWorker: true,
1297
- lowLatencyMode: false
1298
- });
1299
- adHls.loadSource(mediaFile.url);
1300
- adHls.attachMedia(adVideoElement);
1301
- adHls.on(import_hls.default.Events.MANIFEST_PARSED, function() {
1302
- if (!adPlaying) return;
1303
- adVideoElement.play().catch(function(error) {
1304
- console.error("".concat(LOG, " Error starting HLS ad playback:"), error);
1305
- handleAdError();
1306
- });
1307
- });
1308
- var nonFatalNetworkErrors = 0;
1309
- adHls.on(import_hls.default.Events.ERROR, function(_event, data) {
1310
- if (data.fatal) {
1311
- handleAdError();
1312
- } else if (data.type === import_hls.default.ErrorTypes.NETWORK_ERROR) {
1313
- nonFatalNetworkErrors++;
1314
- if (nonFatalNetworkErrors >= 3) {
1315
- if (debug) console.warn("".concat(LOG, " Too many non-fatal HLS network errors (").concat(nonFatalNetworkErrors, "), treating as fatal"));
1316
- handleAdError();
1317
- }
1318
- }
1319
- });
1320
- } else if (adVideoElement.canPlayType("application/vnd.apple.mpegurl")) {
1321
- adVideoElement.src = mediaFile.url;
1322
- adVideoElement.play().catch(function(error) {
1323
- console.error("".concat(LOG, " Error starting native HLS ad playback:"), error);
1324
- handleAdError();
1325
- });
1326
- } else {
1327
- console.error("".concat(LOG, " HLS not supported on this platform"));
1328
- handleAdError();
1329
- }
1330
- }
1331
- function startPlayback(mediaFile) {
1332
- if (!adVideoElement) return;
1333
- if (singleElementMode && isHlsMediaFile(mediaFile)) {
1334
- var mp4Fallback = currentAd === null || currentAd === void 0 ? void 0 : currentAd.mediaFiles.find(function(f) {
1335
- return !isHlsMediaFile(f);
1336
- });
1337
- if (mp4Fallback) {
1338
- if (debug) console.log("".concat(LOG, " singleElementMode: HLS ad blocked, using MP4 fallback"));
1339
- startNativePlayback(mp4Fallback);
1340
- return;
1341
- }
1159
+ function getDurationSecondsFromContext(requestContext) {
1160
+ var _ctx_remainingBreakSec;
1161
+ if (!requestContext || (typeof requestContext === "undefined" ? "undefined" : _type_of(requestContext)) !== "object") {
1162
+ return 30;
1342
1163
  }
1343
- if (isHlsMediaFile(mediaFile)) {
1344
- startHlsPlayback(mediaFile);
1345
- } else {
1346
- startNativePlayback(mediaFile);
1164
+ var ctx = requestContext;
1165
+ var value = (_ctx_remainingBreakSec = ctx.remainingBreakSec) !== null && _ctx_remainingBreakSec !== void 0 ? _ctx_remainingBreakSec : ctx.breakDurationSec;
1166
+ if (typeof value !== "number" || Number.isNaN(value)) {
1167
+ return 30;
1347
1168
  }
1169
+ return Math.max(1, Math.ceil(value));
1348
1170
  }
1349
- function playAd(bids) {
1171
+ function requestAdFromApi(requestContext) {
1350
1172
  return _async_to_generator(function() {
1351
- var winner, ad, contentVolume, adVolume2, mediaFile2, _contentVideo_parentElement, container, adVolume, mediaFile;
1173
+ var durationSeconds, ads;
1352
1174
  return _ts_generator(this, function(_state) {
1353
1175
  switch(_state.label){
1354
1176
  case 0:
1355
- if (destroyed) {
1356
- return [
1357
- 2,
1358
- Promise.reject(new Error("Layer has been destroyed"))
1359
- ];
1360
- }
1361
- if (bids.length === 0) {
1362
- return [
1363
- 2,
1364
- Promise.reject(new Error("No bids provided"))
1365
- ];
1366
- }
1367
- winner = bids[0];
1368
- if (debug) {
1369
- console.log("".concat(LOG, " Winning bid: ").concat(winner.bidder, " $").concat(winner.cpm.toFixed(2), " ").concat(winner.currency));
1370
- }
1177
+ durationSeconds = getDurationSecondsFromContext(requestContext);
1371
1178
  return [
1372
1179
  4,
1373
- resolveBidToVastAd(winner, LOG)
1180
+ fetchVast(durationSeconds)
1374
1181
  ];
1375
1182
  case 1:
1376
- ad = _state.sent();
1377
- if (!ad) {
1378
- if (debug) console.warn("".concat(LOG, " Winning bid has no VAST URL or XML"));
1379
- emit("ad_error");
1380
- return [
1381
- 2,
1382
- Promise.reject(new Error("No VAST from bid"))
1383
- ];
1384
- }
1385
- if (debug) {
1386
- console.log("".concat(LOG, " Ad parsed: ").concat(ad.title, ", duration: ").concat(ad.duration, "s, mediaFiles: ").concat(ad.mediaFiles.length));
1387
- }
1388
- sessionId = generateSessionId();
1389
- currentAd = ad;
1390
- trackingFired = _object_spread({}, createEmptyTrackingState());
1391
- fireTrackingPixels2(ad.trackingUrls.impression);
1392
- trackingFired.impression = true;
1393
- contentVolume = contentVideo.volume;
1394
- originalVolume = Math.max(0, Math.min(1, contentVolume || originalVolume));
1395
- if (!singleElementMode) return [
1396
- 3,
1397
- 3
1398
- ];
1399
- mainHlsInstance === null || mainHlsInstance === void 0 ? void 0 : mainHlsInstance.detachMedia();
1400
- teardownCurrentPlayback();
1401
- adVideoElement = contentVideo;
1402
- adHls = void 0;
1403
- adPlaying = true;
1404
- setAdPlayingFlag(true);
1405
- contentVideo.removeAttribute("src");
1406
- contentVideo.load();
1407
- if (!continueLiveStreamDuringAds) {
1408
- contentVideo.pause();
1409
- }
1410
- contentVideo.muted = true;
1411
- contentVideo.volume = 0;
1412
- return [
1413
- 4,
1414
- new Promise(function(resolve) {
1415
- return setTimeout(resolve, 200);
1416
- })
1417
- ];
1418
- case 2:
1419
- _state.sent();
1420
- if (destroyed || tornDown) return [
1421
- 2
1422
- ];
1423
- contentVideo.style.visibility = "visible";
1424
- contentVideo.style.opacity = "1";
1425
- applyContentVideoAdCoverStyles();
1426
- emit("content_pause");
1427
- setupAdEventListeners();
1428
- adVolume2 = originalMutedState ? 1 : originalVolume;
1429
- adVideoElement.volume = Math.max(0, Math.min(1, adVolume2));
1430
- adVideoElement.muted = false;
1431
- mediaFile2 = selectBestMediaFile(ad.mediaFiles);
1432
- if (debug) console.log("".concat(LOG, " Loading ad from: ").concat(mediaFile2.url));
1433
- startPlayback(mediaFile2);
1434
- return [
1435
- 2
1436
- ];
1437
- case 3:
1438
- if (!adContainerEl) {
1439
- ;
1440
- container = document.createElement("div");
1441
- container.style.position = "absolute";
1442
- container.style.left = "0";
1443
- container.style.top = "0";
1444
- container.style.right = "0";
1445
- container.style.bottom = "0";
1446
- container.style.display = "none";
1447
- container.style.alignItems = "center";
1448
- container.style.justifyContent = "center";
1449
- container.style.pointerEvents = "none";
1450
- container.style.zIndex = "10";
1451
- container.style.backgroundColor = "#000";
1452
- (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
1453
- adContainerEl = container;
1454
- }
1455
- if (!adVideoElement) {
1456
- adVideoElement = createAdVideoElement();
1457
- adContainerEl.appendChild(adVideoElement);
1458
- setupAdEventListeners();
1459
- } else {
1460
- teardownCurrentPlayback();
1461
- }
1462
- if (!continueLiveStreamDuringAds) {
1463
- contentVideo.pause();
1464
- }
1465
- contentVideo.muted = true;
1466
- contentVideo.volume = 0;
1467
- adPlaying = true;
1468
- setAdPlayingFlag(true);
1469
- adVolume = originalMutedState ? 1 : originalVolume;
1470
- adVideoElement.volume = Math.max(0, Math.min(1, adVolume));
1471
- adVideoElement.muted = false;
1472
- if (adContainerEl) {
1473
- adContainerEl.style.display = "flex";
1474
- adContainerEl.style.pointerEvents = "auto";
1475
- }
1476
- emit("content_pause");
1477
- mediaFile = selectBestMediaFile(ad.mediaFiles);
1478
- if (debug) console.log("".concat(LOG, " Loading ad from: ").concat(mediaFile.url));
1479
- startPlayback(mediaFile);
1183
+ ads = _state.sent();
1480
1184
  return [
1481
- 2
1185
+ 2,
1186
+ ads[0] || null
1482
1187
  ];
1483
1188
  }
1484
1189
  });
1485
1190
  })();
1486
1191
  }
1487
- function ensureAdContainer() {
1488
- if (!adContainerEl) {
1489
- var _contentVideo_parentElement;
1490
- var container = document.createElement("div");
1491
- container.style.position = "absolute";
1492
- container.style.left = "0";
1493
- container.style.top = "0";
1494
- container.style.right = "0";
1495
- container.style.bottom = "0";
1496
- container.style.display = "none";
1497
- container.style.alignItems = "center";
1498
- container.style.justifyContent = "center";
1499
- container.style.pointerEvents = "none";
1500
- container.style.zIndex = "10";
1501
- container.style.backgroundColor = "#000";
1502
- (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
1503
- adContainerEl = container;
1504
- }
1505
- return adContainerEl;
1192
+ function assignCurrentAd(ad) {
1193
+ currentAd = ad;
1194
+ sessionId = generateSessionId();
1195
+ trackingFired = {
1196
+ impression: false,
1197
+ start: false,
1198
+ firstQuartile: false,
1199
+ midpoint: false,
1200
+ thirdQuartile: false,
1201
+ complete: false
1202
+ };
1203
+ fireTrackingPixels2(currentAd.trackingUrls.impression);
1204
+ trackingFired.impression = true;
1205
+ emit("ad_impression");
1506
1206
  }
1507
- function preloadAd(bids, token) {
1508
- return _async_to_generator(function() {
1509
- var winner, ad, mediaFile, slot, videoEl, container, hls, slot1, slot2;
1510
- return _ts_generator(this, function(_state) {
1511
- switch(_state.label){
1512
- case 0:
1513
- if (destroyed) return [
1514
- 2
1515
- ];
1516
- winner = bids[0];
1517
- if (!winner) return [
1518
- 2
1519
- ];
1520
- if (debug) console.log("".concat(LOG, " [preload] Resolving VAST for token=").concat(token));
1521
- return [
1522
- 4,
1523
- resolveBidToVastAd(winner, LOG)
1524
- ];
1525
- case 1:
1526
- ad = _state.sent();
1527
- if (!ad || destroyed) return [
1528
- 2
1529
- ];
1530
- mediaFile = selectBestMediaFile(ad.mediaFiles);
1531
- if (!mediaFile) return [
1532
- 2
1533
- ];
1534
- if (smartTVMode || singleElementMode) {
1535
- slot = {
1536
- bids: bids,
1537
- ad: ad,
1538
- mediaFile: mediaFile,
1539
- videoEl: null,
1540
- ready: true
1541
- };
1542
- preloadSlots.set(token, slot);
1543
- if (debug) console.log("".concat(LOG, " [preload] Metadata-only preload (smartTV/singleElement), token=").concat(token, ", url=").concat(mediaFile.url));
1207
+ return {
1208
+ initialize: function initialize() {
1209
+ log("Initializing");
1210
+ if (!adContainerEl) {
1211
+ var _contentVideo_parentElement;
1212
+ var parent = contentVideo.parentElement;
1213
+ if (parent) {
1214
+ var computed = window.getComputedStyle(parent).position;
1215
+ if (computed === "static") {
1216
+ parent.style.position = "relative";
1217
+ parentPositionOverridden = true;
1218
+ }
1219
+ }
1220
+ var container = document.createElement("div");
1221
+ container.style.position = "absolute";
1222
+ container.style.left = "0";
1223
+ container.style.top = "0";
1224
+ container.style.right = "0";
1225
+ container.style.bottom = "0";
1226
+ container.style.display = "none";
1227
+ container.style.alignItems = "center";
1228
+ container.style.justifyContent = "center";
1229
+ container.style.pointerEvents = "none";
1230
+ container.style.zIndex = AD_LAYER_Z_INDEX;
1231
+ container.style.backgroundColor = "#000";
1232
+ container.style.transition = "opacity 0.3s ease-in-out";
1233
+ container.style.opacity = "0";
1234
+ container.style.isolation = "isolate";
1235
+ var countdown = document.createElement("div");
1236
+ countdown.style.position = "absolute";
1237
+ countdown.style.right = "12px";
1238
+ countdown.style.top = "12px";
1239
+ countdown.style.padding = "4px 8px";
1240
+ countdown.style.borderRadius = "4px";
1241
+ countdown.style.background = "rgba(0,0,0,0.75)";
1242
+ countdown.style.color = "#fff";
1243
+ countdown.style.fontFamily = "sans-serif";
1244
+ countdown.style.fontSize = "12px";
1245
+ countdown.style.lineHeight = "1.2";
1246
+ countdown.style.pointerEvents = "none";
1247
+ countdown.style.zIndex = COUNTDOWN_Z_INDEX;
1248
+ countdown.textContent = "Ad";
1249
+ container.appendChild(countdown);
1250
+ (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
1251
+ adContainerEl = container;
1252
+ adCountdownEl = countdown;
1253
+ }
1254
+ },
1255
+ requestAds: function requestAds(duration) {
1256
+ return _async_to_generator(function() {
1257
+ var durationSeconds, parsed, ads, error;
1258
+ return _ts_generator(this, function(_state) {
1259
+ switch(_state.label){
1260
+ case 0:
1261
+ log("Requesting ads for duration:", duration);
1262
+ if (adPlaying) {
1263
+ return [
1264
+ 2,
1265
+ Promise.reject(new Error("Ad already playing"))
1266
+ ];
1267
+ }
1268
+ if (destroyed) {
1269
+ return [
1270
+ 2,
1271
+ Promise.reject(new Error("Player has been destroyed"))
1272
+ ];
1273
+ }
1274
+ _state.label = 1;
1275
+ case 1:
1276
+ _state.trys.push([
1277
+ 1,
1278
+ 3,
1279
+ ,
1280
+ 4
1281
+ ]);
1282
+ tornDown = false;
1283
+ durationSeconds = 30;
1284
+ parsed = parseInt(duration || "", 10);
1285
+ if (!isNaN(parsed) && parsed > 0) {
1286
+ durationSeconds = parsed;
1287
+ }
1288
+ return [
1289
+ 4,
1290
+ fetchVast(durationSeconds)
1291
+ ];
1292
+ case 2:
1293
+ ads = _state.sent();
1294
+ if (ads.length === 0) {
1295
+ log("No ads available from VAST response");
1296
+ emit("ad_error");
1297
+ return [
1298
+ 2,
1299
+ Promise.resolve()
1300
+ ];
1301
+ }
1302
+ assignCurrentAd(ads[0]);
1303
+ log("Ad loaded: ".concat(currentAd.title, ", duration: ").concat(currentAd.duration, "s"));
1304
+ return [
1305
+ 2,
1306
+ Promise.resolve()
1307
+ ];
1308
+ case 3:
1309
+ error = _state.sent();
1310
+ console.error("[AdStormPlayer] Error requesting ads:", error);
1311
+ emit("ad_error");
1544
1312
  return [
1545
- 2
1313
+ 2,
1314
+ Promise.reject(error)
1546
1315
  ];
1547
- }
1548
- videoEl = createAdVideoElement();
1549
- videoEl.style.visibility = "hidden";
1550
- videoEl.style.pointerEvents = "none";
1551
- videoEl.preload = "auto";
1552
- container = ensureAdContainer();
1553
- container.appendChild(videoEl);
1554
- if (isHlsMediaFile(mediaFile) && import_hls.default.isSupported()) {
1555
- hls = new import_hls.default({
1556
- enableWorker: true,
1557
- lowLatencyMode: false
1558
- });
1559
- hls.loadSource(mediaFile.url);
1560
- hls.attachMedia(videoEl);
1561
- slot1 = {
1562
- bids: bids,
1563
- ad: ad,
1564
- mediaFile: mediaFile,
1565
- videoEl: videoEl,
1566
- hlsInstance: hls,
1567
- ready: false
1568
- };
1569
- preloadSlots.set(token, slot1);
1570
- hls.on(import_hls.default.Events.MANIFEST_PARSED, function() {
1571
- var s = preloadSlots.get(token);
1572
- if (s) s.ready = true;
1573
- if (debug) console.log("".concat(LOG, " [preload] HLS manifest parsed, token=").concat(token));
1574
- });
1575
- hls.on(import_hls.default.Events.ERROR, function(_evt, data) {
1576
- if (!preloadSlots.has(token)) return;
1577
- if (data.fatal) {
1578
- if (debug) console.warn("".concat(LOG, " [preload] HLS error for token=").concat(token));
1579
- preloadSlots.delete(token);
1580
- hls.destroy();
1581
- videoEl.remove();
1582
- }
1583
- });
1584
- } else {
1585
- videoEl.src = mediaFile.url;
1586
- videoEl.load();
1587
- slot2 = {
1588
- bids: bids,
1589
- ad: ad,
1590
- mediaFile: mediaFile,
1591
- videoEl: videoEl,
1592
- ready: false
1593
- };
1594
- preloadSlots.set(token, slot2);
1595
- videoEl.addEventListener("canplay", function() {
1596
- var s = preloadSlots.get(token);
1597
- if (s) s.ready = true;
1598
- if (debug) console.log("".concat(LOG, " [preload] canplay fired, token=").concat(token));
1599
- }, {
1600
- once: true
1601
- });
1602
- }
1603
- if (debug) console.log("".concat(LOG, " [preload] Started buffering token=").concat(token, ", url=").concat(mediaFile.url));
1604
- return [
1605
- 2
1606
- ];
1607
- }
1608
- });
1609
- })();
1610
- }
1611
- function playPreloaded(token) {
1612
- return _async_to_generator(function() {
1613
- var slot, contentVolume, adVolume2, videoEl, container2, adVolume21, nonFatalNetworkErrors, adVolume, container;
1614
- return _ts_generator(this, function(_state) {
1615
- switch(_state.label){
1616
- case 0:
1617
- if (destroyed) return [
1618
- 2,
1619
- Promise.reject(new Error("Layer has been destroyed"))
1620
- ];
1621
- slot = preloadSlots.get(token);
1622
- if (!slot) {
1623
- if (debug) console.warn("".concat(LOG, " [preload] No slot found for token=").concat(token, ", nothing to play"));
1316
+ case 4:
1624
1317
  return [
1625
1318
  2
1626
1319
  ];
1627
- }
1628
- preloadSlots.delete(token);
1629
- if (debug) console.log("".concat(LOG, " [preload] Playing preloaded ad, token=").concat(token, ", ready=").concat(slot.ready));
1630
- contentVolume = contentVideo.volume;
1631
- originalVolume = Math.max(0, Math.min(1, contentVolume || originalVolume));
1632
- sessionId = generateSessionId();
1633
- currentAd = slot.ad;
1634
- trackingFired = _object_spread({}, createEmptyTrackingState());
1635
- fireTrackingPixels2(slot.ad.trackingUrls.impression);
1636
- trackingFired.impression = true;
1637
- if (!singleElementMode) return [
1638
- 3,
1639
- 2
1640
- ];
1641
- mainHlsInstance === null || mainHlsInstance === void 0 ? void 0 : mainHlsInstance.detachMedia();
1642
- teardownCurrentPlayback();
1643
- adVideoElement = contentVideo;
1644
- adHls = void 0;
1645
- adPlaying = true;
1646
- setAdPlayingFlag(true);
1647
- contentVideo.removeAttribute("src");
1648
- contentVideo.load();
1649
- contentVideo.muted = true;
1650
- contentVideo.volume = 0;
1651
- return [
1652
- 4,
1653
- new Promise(function(resolve) {
1654
- return setTimeout(resolve, 200);
1655
- })
1656
- ];
1657
- case 1:
1658
- _state.sent();
1659
- if (destroyed || tornDown) return [
1660
- 2
1661
- ];
1662
- contentVideo.style.visibility = "visible";
1663
- contentVideo.style.opacity = "1";
1664
- applyContentVideoAdCoverStyles();
1665
- emit("content_pause");
1666
- setupAdEventListeners();
1667
- adVolume2 = originalMutedState ? 1 : originalVolume;
1668
- contentVideo.volume = Math.max(0, Math.min(1, adVolume2));
1669
- contentVideo.muted = false;
1670
- if (debug) console.log("".concat(LOG, " [preload] singleElementMode: attaching ad to contentVideo, url=").concat(slot.mediaFile.url));
1671
- startPlayback(slot.mediaFile);
1672
- return [
1673
- 2
1674
- ];
1675
- case 2:
1676
- if (smartTVMode && !slot.videoEl) {
1677
- teardownCurrentPlayback();
1678
- if (adVideoElement) {
1679
- adVideoElement.remove();
1680
- adVideoElement = void 0;
1320
+ }
1321
+ });
1322
+ })();
1323
+ },
1324
+ play: function play() {
1325
+ return _async_to_generator(function() {
1326
+ var mediaFile, error;
1327
+ return _ts_generator(this, function(_state) {
1328
+ switch(_state.label){
1329
+ case 0:
1330
+ if (!currentAd) {
1331
+ return [
1332
+ 2,
1333
+ Promise.reject(new Error("No ad loaded"))
1334
+ ];
1335
+ }
1336
+ if (destroyed) {
1337
+ return [
1338
+ 2,
1339
+ Promise.reject(new Error("Player has been destroyed"))
1340
+ ];
1341
+ }
1342
+ log("Starting ad playback");
1343
+ _state.label = 1;
1344
+ case 1:
1345
+ _state.trys.push([
1346
+ 1,
1347
+ 3,
1348
+ ,
1349
+ 4
1350
+ ]);
1351
+ tornDown = false;
1352
+ if (adHideTimerId) {
1353
+ clearTimeout(adHideTimerId);
1354
+ adHideTimerId = void 0;
1355
+ }
1356
+ if (!adVideoElement) {
1357
+ adVideoElement = createAdVideoElement();
1358
+ adContainerEl === null || adContainerEl === void 0 ? void 0 : adContainerEl.appendChild(adVideoElement);
1359
+ } else {
1360
+ teardownCurrentPlayback();
1681
1361
  }
1682
- videoEl = createAdVideoElement();
1683
- videoEl.style.visibility = "visible";
1684
- videoEl.style.pointerEvents = "none";
1685
- container2 = ensureAdContainer();
1686
- container2.appendChild(videoEl);
1687
- adVideoElement = videoEl;
1688
1362
  setupAdEventListeners();
1363
+ trackingFired = {
1364
+ impression: trackingFired.impression,
1365
+ start: false,
1366
+ firstQuartile: false,
1367
+ midpoint: false,
1368
+ thirdQuartile: false,
1369
+ complete: false
1370
+ };
1371
+ contentVideo.style.transition = "opacity 0.3s ease-in-out";
1372
+ contentVideo.style.opacity = "0";
1373
+ setTimeout(function() {
1374
+ contentVideo.style.visibility = "hidden";
1375
+ }, 300);
1376
+ contentVideo.muted = true;
1377
+ contentVideo.volume = 0;
1689
1378
  if (!continueLiveStreamDuringAds) {
1690
1379
  contentVideo.pause();
1691
1380
  }
1692
- contentVideo.muted = true;
1693
- contentVideo.volume = 0;
1694
1381
  adPlaying = true;
1695
1382
  setAdPlayingFlag(true);
1696
- adVolume21 = originalMutedState ? 1 : originalVolume;
1697
- adVideoElement.volume = Math.max(0, Math.min(1, adVolume21));
1698
- adVideoElement.muted = false;
1699
- container2.style.display = "flex";
1700
- container2.style.pointerEvents = "auto";
1383
+ if (adVideoElement) {
1384
+ adVideoElement.volume = originalMutedState ? 0 : originalVolume;
1385
+ adVideoElement.muted = originalMutedState;
1386
+ }
1387
+ if (adContainerEl) {
1388
+ adContainerEl.style.display = "flex";
1389
+ adContainerEl.style.pointerEvents = "auto";
1390
+ adContainerEl.offsetHeight;
1391
+ adContainerEl.style.opacity = "1";
1392
+ }
1701
1393
  emit("content_pause");
1702
- if (debug) console.log("".concat(LOG, " [preload] smartTVMode deferred: creating video element and loading, url=").concat(slot.mediaFile.url));
1703
- startPlayback(slot.mediaFile);
1394
+ mediaFile = selectBestMediaFile(currentAd.mediaFiles);
1395
+ if (!mediaFile) {
1396
+ throw new Error("No media file available");
1397
+ }
1398
+ log("Playing media file:", mediaFile.url);
1399
+ adVideoElement.src = mediaFile.url;
1400
+ adVideoElement.load();
1704
1401
  return [
1705
- 2
1402
+ 4,
1403
+ adVideoElement.play()
1706
1404
  ];
1707
- }
1708
- teardownCurrentPlayback();
1709
- if (adVideoElement && adVideoElement !== slot.videoEl) {
1710
- adVideoElement.remove();
1711
- }
1712
- slot.videoEl.style.visibility = "visible";
1713
- slot.videoEl.style.pointerEvents = "none";
1714
- adVideoElement = slot.videoEl;
1715
- adHls = slot.hlsInstance;
1716
- if (adHls) {
1717
- nonFatalNetworkErrors = 0;
1718
- adHls.on(import_hls.default.Events.ERROR, function(_event, data) {
1719
- if (!adPlaying) return;
1720
- if (data.fatal) {
1721
- handleAdError();
1722
- } else if (data.type === import_hls.default.ErrorTypes.NETWORK_ERROR) {
1723
- nonFatalNetworkErrors++;
1724
- if (nonFatalNetworkErrors >= 3) {
1725
- if (debug) console.warn("".concat(LOG, " [preload] Too many non-fatal HLS network errors during playback, treating as fatal"));
1726
- handleAdError();
1727
- }
1728
- }
1729
- });
1730
- }
1731
- setupAdEventListeners();
1732
- if (!continueLiveStreamDuringAds) {
1733
- contentVideo.pause();
1734
- }
1735
- contentVideo.muted = true;
1736
- contentVideo.volume = 0;
1737
- adPlaying = true;
1738
- setAdPlayingFlag(true);
1739
- adVolume = originalMutedState ? 1 : originalVolume;
1740
- adVideoElement.volume = Math.max(0, Math.min(1, adVolume));
1741
- adVideoElement.muted = false;
1742
- container = ensureAdContainer();
1743
- container.style.display = "flex";
1744
- container.style.pointerEvents = "auto";
1745
- emit("content_pause");
1746
- adVideoElement.play().catch(function(error) {
1747
- console.error("".concat(LOG, " [preload] Error playing preloaded ad:"), error);
1405
+ case 2:
1406
+ _state.sent();
1407
+ return [
1408
+ 2,
1409
+ Promise.resolve()
1410
+ ];
1411
+ case 3:
1412
+ error = _state.sent();
1413
+ console.error("[AdStormPlayer] Error playing ad:", error);
1748
1414
  handleAdError();
1749
- });
1750
- return [
1751
- 2
1752
- ];
1753
- }
1754
- });
1755
- })();
1756
- }
1757
- function cancelPreload(token) {
1758
- var slot = preloadSlots.get(token);
1759
- if (!slot) return;
1760
- preloadSlots.delete(token);
1761
- if (slot.hlsInstance) {
1762
- slot.hlsInstance.destroy();
1763
- }
1764
- if (slot.videoEl) {
1765
- slot.videoEl.pause();
1766
- slot.videoEl.removeAttribute("src");
1767
- slot.videoEl.load();
1768
- slot.videoEl.remove();
1769
- }
1770
- if (debug) console.log("".concat(LOG, " [preload] Cancelled and cleaned up token=").concat(token));
1771
- }
1772
- return {
1773
- initialize: function initialize() {
1774
- if (debug) console.log("".concat(LOG, " Initializing"));
1775
- },
1776
- updateOptions: function updateOptions(opts) {
1777
- if (opts.continueLiveStreamDuringAds !== void 0) {
1778
- continueLiveStreamDuringAds = opts.continueLiveStreamDuringAds;
1779
- }
1780
- if (opts.mainHlsInstance !== void 0) {
1781
- var _opts_mainHlsInstance;
1782
- mainHlsInstance = (_opts_mainHlsInstance = opts.mainHlsInstance) !== null && _opts_mainHlsInstance !== void 0 ? _opts_mainHlsInstance : void 0;
1783
- }
1784
- },
1785
- playAd: playAd,
1786
- preloadAd: preloadAd,
1787
- playPreloaded: playPreloaded,
1788
- hasPreloaded: function hasPreloaded(token) {
1789
- return preloadSlots.has(token);
1790
- },
1791
- cancelPreload: cancelPreload,
1792
- pause: function pause() {
1793
- if (!adPlaying || !adVideoElement) return;
1794
- try {
1795
- if (!adVideoElement.paused) adVideoElement.pause();
1796
- } catch (error) {
1797
- if (debug) console.warn("".concat(LOG, " Error pausing ad:"), error);
1798
- }
1799
- },
1800
- resume: function resume() {
1801
- if (!adPlaying || !adVideoElement) return;
1802
- try {
1803
- if (adVideoElement.paused) adVideoElement.play().catch(function() {});
1804
- } catch (error) {
1805
- if (debug) console.warn("".concat(LOG, " Error resuming ad:"), error);
1806
- }
1415
+ return [
1416
+ 2,
1417
+ Promise.reject(error)
1418
+ ];
1419
+ case 4:
1420
+ return [
1421
+ 2
1422
+ ];
1423
+ }
1424
+ });
1425
+ })();
1807
1426
  },
1808
1427
  stop: function stop() {
1809
1428
  return _async_to_generator(function() {
1810
1429
  return _ts_generator(this, function(_state) {
1430
+ log("Stopping ad");
1811
1431
  tornDown = true;
1812
- if (debug) console.log("".concat(LOG, " Stopping ad"));
1813
1432
  adPlaying = false;
1814
1433
  setAdPlayingFlag(false);
1815
- restoreContentVideoStyles();
1816
- contentVideo.muted = originalMutedState;
1817
- contentVideo.volume = originalMutedState ? 0 : originalVolume;
1434
+ clearAdStallTimer();
1435
+ clearAdCountdownTimer();
1436
+ if (adContainerEl) {
1437
+ adContainerEl.style.opacity = "0";
1438
+ adHideTimerId = setTimeout(function() {
1439
+ if (adContainerEl) {
1440
+ adContainerEl.style.display = "none";
1441
+ adContainerEl.style.pointerEvents = "none";
1442
+ }
1443
+ }, 300);
1444
+ }
1445
+ teardownCurrentPlayback();
1818
1446
  contentVideo.style.visibility = "visible";
1819
1447
  contentVideo.style.opacity = "1";
1820
- if (singleElementMode) {
1821
- teardownCurrentPlayback();
1822
- contentVideo.removeAttribute("src");
1823
- contentVideo.load();
1824
- adVideoElement = void 0;
1825
- } else {
1826
- if (adContainerEl) {
1827
- adContainerEl.style.display = "none";
1828
- adContainerEl.style.pointerEvents = "none";
1829
- }
1830
- if (continueLiveStreamDuringAds) {
1831
- contentVideo.play().catch(function() {});
1832
- }
1833
- teardownCurrentPlayback();
1834
- if (adVideoElement) {
1835
- adVideoElement.pause();
1836
- adVideoElement.removeAttribute("src");
1837
- adVideoElement.load();
1838
- }
1839
- }
1448
+ contentVideo.muted = originalMutedState;
1449
+ contentVideo.volume = originalVolume;
1840
1450
  currentAd = void 0;
1841
1451
  tornDown = false;
1842
1452
  return [
1843
- 2
1453
+ 2,
1454
+ Promise.resolve()
1844
1455
  ];
1845
1456
  });
1846
1457
  })();
1847
1458
  },
1459
+ pause: function pause() {
1460
+ if (!adPlaying || !adVideoElement) return;
1461
+ try {
1462
+ if (!adVideoElement.paused) adVideoElement.pause();
1463
+ } catch (error) {
1464
+ console.warn("[AdStormPlayer] Error pausing ad:", error);
1465
+ }
1466
+ },
1467
+ resume: function resume() {
1468
+ if (!adPlaying || !adVideoElement) return;
1469
+ try {
1470
+ if (adVideoElement.paused) adVideoElement.play().catch(function() {});
1471
+ } catch (error) {
1472
+ console.warn("[AdStormPlayer] Error resuming ad:", error);
1473
+ }
1474
+ },
1848
1475
  destroy: function destroy() {
1849
- tornDown = true;
1850
- if (debug) console.log("".concat(LOG, " Destroying"));
1476
+ log("Destroying");
1851
1477
  destroyed = true;
1478
+ tornDown = true;
1852
1479
  adPlaying = false;
1853
1480
  setAdPlayingFlag(false);
1854
- restoreContentVideoStyles();
1481
+ clearAdStallTimer();
1482
+ clearAdCountdownTimer();
1483
+ if (adHideTimerId) {
1484
+ clearTimeout(adHideTimerId);
1485
+ adHideTimerId = void 0;
1486
+ }
1855
1487
  contentVideo.muted = originalMutedState;
1856
1488
  contentVideo.volume = originalVolume;
1857
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1858
- try {
1859
- for(var _iterator = Array.from(preloadSlots.entries())[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1860
- var _step_value = _sliced_to_array(_step.value, 1), token = _step_value[0];
1861
- cancelPreload(token);
1862
- }
1863
- } catch (err) {
1864
- _didIteratorError = true;
1865
- _iteratorError = err;
1866
- } finally{
1867
- try {
1868
- if (!_iteratorNormalCompletion && _iterator.return != null) {
1869
- _iterator.return();
1870
- }
1871
- } finally{
1872
- if (_didIteratorError) {
1873
- throw _iteratorError;
1874
- }
1875
- }
1876
- }
1489
+ contentVideo.style.visibility = "visible";
1490
+ contentVideo.style.opacity = "1";
1877
1491
  teardownCurrentPlayback();
1878
- if (adVideoElement) {
1879
- if (singleElementMode && adVideoElement === contentVideo) {
1880
- contentVideo.removeAttribute("src");
1881
- contentVideo.load();
1882
- } else {
1883
- adVideoElement.pause();
1884
- adVideoElement.removeAttribute("src");
1885
- adVideoElement.remove();
1886
- }
1887
- adVideoElement = void 0;
1888
- }
1492
+ adVideoElement === null || adVideoElement === void 0 ? void 0 : adVideoElement.remove();
1493
+ adVideoElement = void 0;
1889
1494
  if (adContainerEl === null || adContainerEl === void 0 ? void 0 : adContainerEl.parentElement) {
1890
1495
  adContainerEl.parentElement.removeChild(adContainerEl);
1891
1496
  }
1892
1497
  adContainerEl = void 0;
1498
+ adCountdownEl = void 0;
1893
1499
  currentAd = void 0;
1500
+ sessionId = void 0;
1501
+ preloadSlots.clear();
1894
1502
  listeners.clear();
1503
+ if (parentPositionOverridden && contentVideo.parentElement) {
1504
+ contentVideo.parentElement.style.position = "";
1505
+ parentPositionOverridden = false;
1506
+ }
1507
+ },
1508
+ updateOptions: function updateOptions(opts) {
1509
+ if (opts.continueLiveStreamDuringAds !== void 0) {
1510
+ continueLiveStreamDuringAds = opts.continueLiveStreamDuringAds;
1511
+ }
1512
+ },
1513
+ playAd: function playAd(requestContext) {
1514
+ return _async_to_generator(function() {
1515
+ var ad;
1516
+ return _ts_generator(this, function(_state) {
1517
+ switch(_state.label){
1518
+ case 0:
1519
+ if (destroyed) return [
1520
+ 2,
1521
+ Promise.reject(new Error("Player has been destroyed"))
1522
+ ];
1523
+ if (!!currentAd) return [
1524
+ 3,
1525
+ 2
1526
+ ];
1527
+ return [
1528
+ 4,
1529
+ requestAdFromApi(requestContext)
1530
+ ];
1531
+ case 1:
1532
+ ad = _state.sent();
1533
+ if (!ad) {
1534
+ emit("ad_error", {
1535
+ message: "No valid ad from AdStorm API"
1536
+ });
1537
+ return [
1538
+ 2,
1539
+ Promise.reject(new Error("No valid ad from AdStorm API"))
1540
+ ];
1541
+ }
1542
+ assignCurrentAd(ad);
1543
+ _state.label = 2;
1544
+ case 2:
1545
+ return [
1546
+ 2,
1547
+ this.play()
1548
+ ];
1549
+ }
1550
+ });
1551
+ }).call(this);
1552
+ },
1553
+ preloadAd: function preloadAd(arg1, arg2) {
1554
+ return _async_to_generator(function() {
1555
+ var token, requestContext, ad;
1556
+ return _ts_generator(this, function(_state) {
1557
+ switch(_state.label){
1558
+ case 0:
1559
+ if (destroyed) return [
1560
+ 2
1561
+ ];
1562
+ token = typeof arg1 === "string" ? arg1 : typeof arg2 === "string" ? arg2 : void 0;
1563
+ if (!token) return [
1564
+ 2
1565
+ ];
1566
+ requestContext = typeof arg1 === "string" ? arg2 : arg1;
1567
+ return [
1568
+ 4,
1569
+ requestAdFromApi(requestContext)
1570
+ ];
1571
+ case 1:
1572
+ ad = _state.sent();
1573
+ if (!ad) return [
1574
+ 2
1575
+ ];
1576
+ preloadSlots.set(token, {
1577
+ ad: ad
1578
+ });
1579
+ return [
1580
+ 2
1581
+ ];
1582
+ }
1583
+ });
1584
+ })();
1585
+ },
1586
+ playPreloaded: function playPreloaded(token) {
1587
+ return _async_to_generator(function() {
1588
+ var slot;
1589
+ return _ts_generator(this, function(_state) {
1590
+ if (destroyed) return [
1591
+ 2,
1592
+ Promise.reject(new Error("Player has been destroyed"))
1593
+ ];
1594
+ slot = preloadSlots.get(token);
1595
+ if (!slot) {
1596
+ return [
1597
+ 2,
1598
+ Promise.reject(new Error("No preloaded ad for token ".concat(token)))
1599
+ ];
1600
+ }
1601
+ preloadSlots.delete(token);
1602
+ assignCurrentAd(slot.ad);
1603
+ return [
1604
+ 2,
1605
+ this.play()
1606
+ ];
1607
+ });
1608
+ }).call(this);
1609
+ },
1610
+ hasPreloaded: function hasPreloaded(token) {
1611
+ return preloadSlots.has(token);
1612
+ },
1613
+ cancelPreload: function cancelPreload(token) {
1614
+ preloadSlots.delete(token);
1895
1615
  },
1896
1616
  isAdPlaying: function isAdPlaying() {
1897
1617
  return adPlaying;
1898
1618
  },
1899
1619
  resize: function resize(width, height) {
1620
+ log("Resizing to ".concat(width, "x").concat(height));
1900
1621
  if (adContainerEl) {
1901
1622
  adContainerEl.style.width = "".concat(width, "px");
1902
1623
  adContainerEl.style.height = "".concat(height, "px");
@@ -1916,6 +1637,7 @@ function createVastAdLayer(contentVideo, options) {
1916
1637
  },
1917
1638
  updateOriginalMutedState: function updateOriginalMutedState(muted, volume) {
1918
1639
  var nextVolume = typeof volume === "number" && !Number.isNaN(volume) ? Math.max(0, Math.min(1, volume)) : originalVolume;
1640
+ log("updateOriginalMutedState: muted=".concat(muted, ", volume=").concat(nextVolume));
1919
1641
  originalMutedState = muted;
1920
1642
  originalVolume = nextVolume;
1921
1643
  },
@@ -1928,6 +1650,7 @@ function createVastAdLayer(contentVideo, options) {
1928
1650
  setAdVolume: function setAdVolume(volume) {
1929
1651
  if (adVideoElement && adPlaying) {
1930
1652
  adVideoElement.volume = Math.max(0, Math.min(1, volume));
1653
+ adVideoElement.muted = volume === 0;
1931
1654
  }
1932
1655
  },
1933
1656
  getAdVolume: function getAdVolume() {
@@ -1937,11 +1660,16 @@ function createVastAdLayer(contentVideo, options) {
1937
1660
  return 1;
1938
1661
  },
1939
1662
  showPlaceholder: function showPlaceholder() {
1940
- if (singleElementMode) return;
1941
- contentVideo.style.opacity = "0";
1942
- contentVideo.style.visibility = "hidden";
1943
1663
  if (!adContainerEl) {
1944
1664
  var _contentVideo_parentElement;
1665
+ var parent = contentVideo.parentElement;
1666
+ if (parent) {
1667
+ var computed = window.getComputedStyle(parent).position;
1668
+ if (computed === "static") {
1669
+ parent.style.position = "relative";
1670
+ parentPositionOverridden = true;
1671
+ }
1672
+ }
1945
1673
  var container = document.createElement("div");
1946
1674
  container.style.position = "absolute";
1947
1675
  container.style.left = "0";
@@ -1952,24 +1680,43 @@ function createVastAdLayer(contentVideo, options) {
1952
1680
  container.style.alignItems = "center";
1953
1681
  container.style.justifyContent = "center";
1954
1682
  container.style.pointerEvents = "none";
1955
- container.style.zIndex = "10";
1683
+ container.style.zIndex = AD_LAYER_Z_INDEX;
1956
1684
  container.style.backgroundColor = "#000";
1685
+ container.style.isolation = "isolate";
1686
+ var countdown = document.createElement("div");
1687
+ countdown.style.position = "absolute";
1688
+ countdown.style.right = "12px";
1689
+ countdown.style.top = "12px";
1690
+ countdown.style.padding = "4px 8px";
1691
+ countdown.style.borderRadius = "4px";
1692
+ countdown.style.background = "rgba(0,0,0,0.75)";
1693
+ countdown.style.color = "#fff";
1694
+ countdown.style.fontFamily = "sans-serif";
1695
+ countdown.style.fontSize = "12px";
1696
+ countdown.style.lineHeight = "1.2";
1697
+ countdown.style.pointerEvents = "none";
1698
+ countdown.style.zIndex = COUNTDOWN_Z_INDEX;
1699
+ countdown.textContent = "Ad";
1700
+ container.appendChild(countdown);
1957
1701
  (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
1958
1702
  adContainerEl = container;
1703
+ adCountdownEl = countdown;
1959
1704
  }
1960
1705
  if (adContainerEl) {
1961
1706
  adContainerEl.style.display = "flex";
1707
+ adContainerEl.style.opacity = "1";
1962
1708
  adContainerEl.style.pointerEvents = "auto";
1963
1709
  }
1964
1710
  },
1965
1711
  hidePlaceholder: function hidePlaceholder() {
1966
1712
  if (adContainerEl) {
1967
- adContainerEl.style.display = "none";
1968
- adContainerEl.style.pointerEvents = "none";
1969
- }
1970
- if (!adPlaying) {
1971
- contentVideo.style.visibility = "visible";
1972
- contentVideo.style.opacity = "1";
1713
+ adContainerEl.style.opacity = "0";
1714
+ setTimeout(function() {
1715
+ if (adContainerEl) {
1716
+ adContainerEl.style.display = "none";
1717
+ adContainerEl.style.pointerEvents = "none";
1718
+ }
1719
+ }, 300);
1973
1720
  }
1974
1721
  }
1975
1722
  };
@@ -3072,6 +2819,9 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3072
2819
  this.backoffBaseMs = 1e3;
3073
2820
  this.maxBackoffMs = 15e3;
3074
2821
  this.MIN_AD_REMAINING_MS = 15e3;
2822
+ this.adRequestTimeoutMs = 5e3;
2823
+ this.adRequestMaxRetries = 3;
2824
+ this.adRequestRetryBackoffMs = 1500;
3075
2825
  this.preloadedTokens = [];
3076
2826
  initializePolyfills();
3077
2827
  var browserOverrides = getBrowserConfigOverrides();
@@ -3079,24 +2829,22 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3079
2829
  this.video = config.videoElement;
3080
2830
  this.adTransitionGapMs = (_this_config_adTransitionGapMs = this.config.adTransitionGapMs) !== null && _this_config_adTransitionGapMs !== void 0 ? _this_config_adTransitionGapMs : 100;
3081
2831
  logBrowserInfo(config.debugAdTiming);
3082
- this.vastManager = createVastManager(config.debugAdTiming !== void 0 ? {
3083
- debug: !!config.debugAdTiming
3084
- } : {});
3085
2832
  var browserForAdLayer = detectBrowser();
3086
2833
  var isSinglePipeline = browserForAdLayer.isSmartTV || !!this.config.singlePipelineMode;
3087
- this.adLayer = createVastAdLayer(this.video, {
3088
- continueLiveStreamDuringAds: false,
3089
- smartTVMode: isSinglePipeline,
3090
- singleElementMode: isSinglePipeline,
3091
- forceMP4Ads: isSinglePipeline,
2834
+ this.adLayer = createAdStormPlayer(this.video, {
2835
+ licenseKey: this.config.licenseKey || "",
3092
2836
  debug: !!config.debugAdTiming
3093
2837
  });
2838
+ this.adLayer.updateOptions({
2839
+ continueLiveStreamDuringAds: !isSinglePipeline && this.shouldContinueLiveStreamDuringAds()
2840
+ });
3094
2841
  }
3095
2842
  _create_class(StormcloudVideoPlayer, [
3096
2843
  {
3097
2844
  key: "adRequest",
3098
2845
  value: function adRequest(context) {
3099
2846
  return _async_to_generator(function() {
2847
+ var _ref, _ref1, durationSeconds;
3100
2848
  return _ts_generator(this, function(_state) {
3101
2849
  switch(_state.label){
3102
2850
  case 0:
@@ -3104,15 +2852,28 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3104
2852
  2,
3105
2853
  []
3106
2854
  ];
2855
+ durationSeconds = Math.max(1, Math.ceil((_ref = (_ref1 = context === null || context === void 0 ? void 0 : context.remainingBreakSec) !== null && _ref1 !== void 0 ? _ref1 : context === null || context === void 0 ? void 0 : context.breakDurationSec) !== null && _ref !== void 0 ? _ref : 30));
3107
2856
  return [
3108
2857
  4,
3109
- this.vastManager.initialize()
2858
+ this.adLayer.requestAds(String(durationSeconds))
3110
2859
  ];
3111
2860
  case 1:
3112
2861
  _state.sent();
3113
2862
  return [
3114
2863
  2,
3115
- this.vastManager.requestBidsUntilResponse(context)
2864
+ [
2865
+ {
2866
+ bidder: "adstorm-direct",
2867
+ cpm: 0,
2868
+ width: 0,
2869
+ height: 0,
2870
+ adId: "adstorm",
2871
+ impId: "",
2872
+ creativeId: "adstorm",
2873
+ currency: "USD",
2874
+ durationSec: durationSeconds
2875
+ }
2876
+ ]
3116
2877
  ];
3117
2878
  }
3118
2879
  });
@@ -3169,7 +2930,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3169
2930
  2
3170
2931
  ];
3171
2932
  case 3:
3172
- this.hls = new import_hls2.default(_object_spread_props(_object_spread({
2933
+ this.hls = new import_hls.default(_object_spread_props(_object_spread({
3173
2934
  enableWorker: true,
3174
2935
  backBufferLength: 30,
3175
2936
  liveDurationInfinity: true,
@@ -3187,11 +2948,11 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3187
2948
  nudgeMaxRetry: 3,
3188
2949
  startPosition: -1
3189
2950
  }));
3190
- this.hls.on(import_hls2.default.Events.MEDIA_ATTACHED, function() {
2951
+ this.hls.on(import_hls.default.Events.MEDIA_ATTACHED, function() {
3191
2952
  var _this_hls;
3192
2953
  (_this_hls = _this.hls) === null || _this_hls === void 0 ? void 0 : _this_hls.loadSource(_this.config.src);
3193
2954
  });
3194
- this.hls.on(import_hls2.default.Events.MANIFEST_PARSED, function(_, data) {
2955
+ this.hls.on(import_hls.default.Events.MANIFEST_PARSED, function(_, data) {
3195
2956
  return _async_to_generator(function() {
3196
2957
  var _this_config_minSegmentsBeforePlay, _ref, _this_hls_levels, _this_hls, adBehavior, _this_hls1, minSegments, _this_video_play;
3197
2958
  return _ts_generator(this, function(_state) {
@@ -3253,7 +3014,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3253
3014
  });
3254
3015
  }).call(_this);
3255
3016
  });
3256
- this.hls.on(import_hls2.default.Events.LEVEL_LOADED, function(_evt, data) {
3017
+ this.hls.on(import_hls.default.Events.LEVEL_LOADED, function(_evt, data) {
3257
3018
  if (_this.inAdBreak || _this.pendingAdBreak) {
3258
3019
  return;
3259
3020
  }
@@ -3326,7 +3087,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3326
3087
  }
3327
3088
  }
3328
3089
  });
3329
- this.hls.on(import_hls2.default.Events.FRAG_BUFFERED, function(_evt, data) {
3090
+ this.hls.on(import_hls.default.Events.FRAG_BUFFERED, function(_evt, data) {
3330
3091
  return _async_to_generator(function() {
3331
3092
  var _this, _this_config_minSegmentsBeforePlay, minSegments, _this_video_play;
3332
3093
  return _ts_generator(this, function(_state) {
@@ -3386,7 +3147,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3386
3147
  });
3387
3148
  }).call(_this);
3388
3149
  });
3389
- this.hls.on(import_hls2.default.Events.FRAG_PARSING_METADATA, function(_evt, data) {
3150
+ this.hls.on(import_hls.default.Events.FRAG_PARSING_METADATA, function(_evt, data) {
3390
3151
  var id3Tags = ((data === null || data === void 0 ? void 0 : data.samples) || []).map(function(s) {
3391
3152
  return {
3392
3153
  key: "ID3",
@@ -3398,7 +3159,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3398
3159
  return _this.onId3Tag(tag);
3399
3160
  });
3400
3161
  });
3401
- this.hls.on(import_hls2.default.Events.FRAG_CHANGED, function(_evt, data) {
3162
+ this.hls.on(import_hls.default.Events.FRAG_CHANGED, function(_evt, data) {
3402
3163
  var frag = data === null || data === void 0 ? void 0 : data.frag;
3403
3164
  var tagList = frag === null || frag === void 0 ? void 0 : frag.tagList;
3404
3165
  if (!Array.isArray(tagList)) return;
@@ -3507,14 +3268,14 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3507
3268
  }
3508
3269
  }
3509
3270
  });
3510
- this.hls.on(import_hls2.default.Events.ERROR, function(_evt, data) {
3271
+ this.hls.on(import_hls.default.Events.ERROR, function(_evt, data) {
3511
3272
  if (data === null || data === void 0 ? void 0 : data.fatal) {
3512
3273
  switch(data.type){
3513
- case import_hls2.default.ErrorTypes.NETWORK_ERROR:
3274
+ case import_hls.default.ErrorTypes.NETWORK_ERROR:
3514
3275
  var _this_hls;
3515
3276
  (_this_hls = _this.hls) === null || _this_hls === void 0 ? void 0 : _this_hls.startLoad();
3516
3277
  break;
3517
- case import_hls2.default.ErrorTypes.MEDIA_ERROR:
3278
+ case import_hls.default.ErrorTypes.MEDIA_ERROR:
3518
3279
  var _this_hls1;
3519
3280
  (_this_hls1 = _this.hls) === null || _this_hls1 === void 0 ? void 0 : _this_hls1.recoverMediaError();
3520
3281
  break;
@@ -3606,7 +3367,11 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3606
3367
  }
3607
3368
  });
3608
3369
  this.adLayer.on("content_resume", function() {
3370
+ var _ref, _ref1;
3371
+ var _this_savedMutedStateBeforeScte, _this_savedMutedStateBeforeScte1;
3609
3372
  var remaining = _this.getRemainingAdMs();
3373
+ var breakMuted = (_ref = (_this_savedMutedStateBeforeScte = _this.savedMutedStateBeforeScte) === null || _this_savedMutedStateBeforeScte === void 0 ? void 0 : _this_savedMutedStateBeforeScte.muted) !== null && _ref !== void 0 ? _ref : _this.adLayer.getOriginalMutedState();
3374
+ var breakVolume = (_ref1 = (_this_savedMutedStateBeforeScte1 = _this.savedMutedStateBeforeScte) === null || _this_savedMutedStateBeforeScte1 === void 0 ? void 0 : _this_savedMutedStateBeforeScte1.volume) !== null && _ref1 !== void 0 ? _ref1 : _this.adLayer.getOriginalVolume();
3610
3375
  if (_this.config.debugAdTiming) {
3611
3376
  console.log("[StormcloudVideoPlayer] content_resume received, inAdBreak=%s, remaining=%s, preloadedTokens=%d, pendingNext=%s", _this.inAdBreak, remaining, _this.preloadedTokens.length, !!_this.pendingNextAdBids);
3612
3377
  }
@@ -3621,7 +3386,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3621
3386
  return;
3622
3387
  }
3623
3388
  _this.consecutiveFailures = 0;
3624
- if (!_this.video.muted) {
3389
+ if (!_this.config.disableFiller && !_this.video.muted) {
3625
3390
  _this.video.muted = true;
3626
3391
  _this.video.volume = 0;
3627
3392
  }
@@ -3688,6 +3453,18 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3688
3453
  if (!_this.config.disableFiller) {
3689
3454
  _this.showPlaceholderLayer();
3690
3455
  _this.adLayer.showPlaceholder();
3456
+ } else {
3457
+ _this.adLayer.hidePlaceholder();
3458
+ if (_this.video.muted !== breakMuted) {
3459
+ _this.video.muted = breakMuted;
3460
+ }
3461
+ if (Math.abs(_this.video.volume - breakVolume) > 0.01) {
3462
+ _this.video.volume = breakVolume;
3463
+ }
3464
+ if (_this.video.paused) {
3465
+ var _this_video_play;
3466
+ (_this_video_play = _this.video.play()) === null || _this_video_play === void 0 ? void 0 : _this_video_play.catch(function() {});
3467
+ }
3691
3468
  }
3692
3469
  _this.stopContinuousFetching();
3693
3470
  return;
@@ -4415,6 +4192,16 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4415
4192
  return this.totalAdsInBreak;
4416
4193
  }
4417
4194
  },
4195
+ {
4196
+ key: "getRemainingAdSeconds",
4197
+ value: function getRemainingAdSeconds() {
4198
+ var remainingMs = this.getRemainingAdMs();
4199
+ if (!Number.isFinite(remainingMs) || remainingMs <= 0 || remainingMs === Number.MAX_SAFE_INTEGER) {
4200
+ return 0;
4201
+ }
4202
+ return Math.ceil(remainingMs / 1e3);
4203
+ }
4204
+ },
4418
4205
  {
4419
4206
  key: "isAdPlaying",
4420
4207
  value: function isAdPlaying() {
@@ -5644,7 +5431,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5644
5431
  });
5645
5432
  };
5646
5433
  var onManifestParsedRestore = function onManifestParsedRestore1() {
5647
- hlsRef.off(import_hls2.default.Events.MANIFEST_PARSED, onManifestParsedRestore);
5434
+ hlsRef.off(import_hls.default.Events.MANIFEST_PARSED, onManifestParsedRestore);
5648
5435
  if (!_this.inAdBreak && !_this.adLayer.isAdPlaying()) {
5649
5436
  if (videoRef.muted !== savedMuted) videoRef.muted = savedMuted;
5650
5437
  if (Math.abs(videoRef.volume - savedVolume) > 0.01) videoRef.volume = savedVolume;
@@ -5657,7 +5444,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5657
5444
  }
5658
5445
  }
5659
5446
  };
5660
- hlsRef.on(import_hls2.default.Events.MANIFEST_PARSED, onManifestParsedRestore);
5447
+ hlsRef.on(import_hls.default.Events.MANIFEST_PARSED, onManifestParsedRestore);
5661
5448
  var pipelineDelayMs = 300;
5662
5449
  if (debugEnabled) {
5663
5450
  console.log("[StormcloudVideoPlayer] Smart TV: waiting ".concat(pipelineDelayMs, "ms for hardware pipeline release before re-attach"));
@@ -7271,7 +7058,8 @@ var StormcloudVideoPlayerComponent = import_react2.default.memo(function(props)
7271
7058
  var _import_react2_default_useState = _sliced_to_array(import_react2.default.useState({
7272
7059
  showAds: false,
7273
7060
  currentIndex: 0,
7274
- totalAds: 0
7061
+ totalAds: 0,
7062
+ remainingSeconds: 0
7275
7063
  }), 2), adStatus = _import_react2_default_useState[0], setAdStatus = _import_react2_default_useState[1];
7276
7064
  var _import_react2_default_useState1 = _sliced_to_array(import_react2.default.useState(true), 2), shouldShowNativeControls = _import_react2_default_useState1[0], setShouldShowNativeControls = _import_react2_default_useState1[1];
7277
7065
  var _import_react2_default_useState2 = _sliced_to_array(import_react2.default.useState(false), 2), isMuted = _import_react2_default_useState2[0], setIsMuted = _import_react2_default_useState2[1];
@@ -7523,15 +7311,17 @@ var StormcloudVideoPlayerComponent = import_react2.default.memo(function(props)
7523
7311
  var showAds = showAdsFromMethod || showAdsFromAttribute;
7524
7312
  var currentIndex = playerRef.current.getCurrentAdIndex();
7525
7313
  var totalAds = playerRef.current.getTotalAdsInBreak();
7314
+ var remainingSeconds = playerRef.current.getRemainingAdSeconds();
7526
7315
  setAdStatus(function(prev) {
7527
- if (prev.showAds !== showAds || prev.currentIndex !== currentIndex || prev.totalAds !== totalAds) {
7316
+ if (prev.showAds !== showAds || prev.currentIndex !== currentIndex || prev.totalAds !== totalAds || prev.remainingSeconds !== remainingSeconds) {
7528
7317
  if (showAds && !prev.showAds) {
7529
7318
  setShowCenterPlay(false);
7530
7319
  }
7531
7320
  return {
7532
7321
  showAds: showAds,
7533
7322
  currentIndex: currentIndex,
7534
- totalAds: totalAds
7323
+ totalAds: totalAds,
7324
+ remainingSeconds: remainingSeconds
7535
7325
  };
7536
7326
  }
7537
7327
  return prev;
@@ -7921,6 +7711,22 @@ var StormcloudVideoPlayerComponent = import_react2.default.memo(function(props)
7921
7711
  " of ",
7922
7712
  Math.max(adStatus.totalAds, adStatus.currentIndex)
7923
7713
  ]
7714
+ }),
7715
+ adStatus.remainingSeconds > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", {
7716
+ style: {
7717
+ background: "rgba(0, 0, 0, 0.5)",
7718
+ backdropFilter: "blur(12px)",
7719
+ color: "rgba(255, 255, 255, 0.9)",
7720
+ padding: "".concat(4 * responsiveScale, "px ").concat(10 * responsiveScale, "px"),
7721
+ borderRadius: "6px",
7722
+ fontSize: "".concat(11 * responsiveScale, "px"),
7723
+ fontWeight: "600",
7724
+ border: "1px solid rgba(255, 255, 255, 0.08)"
7725
+ },
7726
+ children: [
7727
+ adStatus.remainingSeconds,
7728
+ "s"
7729
+ ]
7924
7730
  })
7925
7731
  ]
7926
7732
  }),