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.
package/lib/index.cjs CHANGED
@@ -458,15 +458,12 @@ __export(index_exports, {
458
458
  canPlay: function canPlay1() {
459
459
  return canPlay;
460
460
  },
461
+ createAdStormPlayer: function createAdStormPlayer1() {
462
+ return createAdStormPlayer;
463
+ },
461
464
  createStormcloudPlayer: function createStormcloudPlayer1() {
462
465
  return createStormcloudPlayer;
463
466
  },
464
- createVastAdLayer: function createVastAdLayer1() {
465
- return createVastAdLayer;
466
- },
467
- createVastManager: function createVastManager1() {
468
- return createVastManager;
469
- },
470
467
  default: function _default() {
471
468
  return StormcloudVideoPlayerComponent;
472
469
  },
@@ -541,185 +538,8 @@ module.exports = __toCommonJS(index_exports);
541
538
  // src/ui/StormcloudVideoPlayer.tsx
542
539
  var import_react2 = __toESM(require("react"), 1);
543
540
  // src/player/StormcloudVideoPlayer.ts
544
- var import_hls2 = __toESM(require("hls.js"), 1);
541
+ var import_hls = __toESM(require("hls.js"), 1);
545
542
  // src/sdk/vastParser.ts
546
- function isHlsType(type) {
547
- return type === "application/x-mpegURL" || type.includes("m3u8");
548
- }
549
- function isMp4Type(type) {
550
- return type === "video/mp4" || type.includes("mp4");
551
- }
552
- function parseVastXml(xmlString) {
553
- var filter = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "all", logPrefix = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "[VastParser]";
554
- try {
555
- var _xmlDoc_querySelector, _xmlDoc_querySelector1, _xmlDoc_querySelector_textContent, _xmlDoc_querySelector2;
556
- var parser = new DOMParser();
557
- var xmlDoc = parser.parseFromString(xmlString, "text/xml");
558
- var parserError = xmlDoc.querySelector("parsererror");
559
- if (parserError) {
560
- console.error("".concat(logPrefix, " XML parsing error (malformed VAST XML):"), parserError.textContent);
561
- return null;
562
- }
563
- var adElement = xmlDoc.querySelector("Ad");
564
- if (!adElement) {
565
- console.warn("".concat(logPrefix, " No Ad element found in VAST XML"));
566
- return null;
567
- }
568
- var adId = adElement.getAttribute("id") || "unknown";
569
- var title = ((_xmlDoc_querySelector = xmlDoc.querySelector("AdTitle")) === null || _xmlDoc_querySelector === void 0 ? void 0 : _xmlDoc_querySelector.textContent) || "Ad";
570
- var isNoAdAvailable = adId === "empty" || title.toLowerCase().includes("no ad available") || title.toLowerCase() === "no ad available";
571
- var durationText = ((_xmlDoc_querySelector1 = xmlDoc.querySelector("Duration")) === null || _xmlDoc_querySelector1 === void 0 ? void 0 : _xmlDoc_querySelector1.textContent) || "00:00:30";
572
- var durationParts = durationText.split(":");
573
- var duration = parseInt(durationParts[0] || "0", 10) * 3600 + parseInt(durationParts[1] || "0", 10) * 60 + Math.round(parseFloat(durationParts[2] || "0"));
574
- var mediaFileElements = xmlDoc.querySelectorAll("MediaFile");
575
- var mediaFiles = [];
576
- console.log("".concat(logPrefix, " Found ").concat(mediaFileElements.length, " MediaFile element(s) in VAST XML"));
577
- mediaFileElements.forEach(function(mf, index) {
578
- var _mf_textContent;
579
- var type = mf.getAttribute("type") || "";
580
- var url = ((_mf_textContent = mf.textContent) === null || _mf_textContent === void 0 ? void 0 : _mf_textContent.trim()) || "";
581
- var width = mf.getAttribute("width") || "";
582
- var height = mf.getAttribute("height") || "";
583
- console.log("".concat(logPrefix, " MediaFile ").concat(index, ': type="').concat(type, '", url="').concat(url.substring(0, 80), '...", width="').concat(width, '", height="').concat(height, '"'));
584
- if (!url) {
585
- console.warn("".concat(logPrefix, " MediaFile ").concat(index, " has empty URL"));
586
- return;
587
- }
588
- var isHls = isHlsType(type);
589
- var isMp4 = isMp4Type(type);
590
- var accepted = false;
591
- if (filter === "hls-only") {
592
- accepted = isHls;
593
- } else if (filter === "mp4-first") {
594
- accepted = isMp4 || isHls;
595
- } else {
596
- accepted = true;
597
- }
598
- if (!accepted) {
599
- console.log("".concat(logPrefix, " MediaFile ").concat(index, ' ignored (type="').concat(type, '" not accepted by filter "').concat(filter, '")'));
600
- return;
601
- }
602
- var bitrateAttr = mf.getAttribute("bitrate");
603
- var bitrateValue = bitrateAttr ? parseInt(bitrateAttr, 10) : void 0;
604
- mediaFiles.push({
605
- url: url,
606
- type: type,
607
- width: parseInt(width || "1920", 10),
608
- height: parseInt(height || "1080", 10),
609
- bitrate: bitrateValue && bitrateValue > 0 ? bitrateValue : void 0
610
- });
611
- console.log("".concat(logPrefix, ' Added MediaFile: type="').concat(type, '" url="').concat(url.substring(0, 80), '..."'));
612
- });
613
- if (filter === "mp4-first" && mediaFiles.length > 1) {
614
- mediaFiles.sort(function(a, b) {
615
- var aIsMp4 = isMp4Type(a.type) ? 0 : 1;
616
- var bIsMp4 = isMp4Type(b.type) ? 0 : 1;
617
- return aIsMp4 - bIsMp4;
618
- });
619
- }
620
- if (mediaFiles.length === 0) {
621
- if (isNoAdAvailable) {
622
- console.warn("".concat(logPrefix, " No ads available (VAST response indicates no ads)"));
623
- } else {
624
- console.warn("".concat(logPrefix, " No compatible media files found in VAST XML"));
625
- }
626
- return null;
627
- }
628
- var trackingUrls = {
629
- impression: [],
630
- start: [],
631
- firstQuartile: [],
632
- midpoint: [],
633
- thirdQuartile: [],
634
- complete: [],
635
- mute: [],
636
- unmute: [],
637
- pause: [],
638
- resume: [],
639
- fullscreen: [],
640
- exitFullscreen: [],
641
- skip: [],
642
- error: []
643
- };
644
- xmlDoc.querySelectorAll("Impression").forEach(function(el) {
645
- var _el_textContent;
646
- var url = (_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim();
647
- if (url) trackingUrls.impression.push(url);
648
- });
649
- xmlDoc.querySelectorAll("Tracking").forEach(function(el) {
650
- var _el_textContent;
651
- var event = el.getAttribute("event");
652
- var url = (_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim();
653
- if (event && url) {
654
- var eventKey = event;
655
- if (trackingUrls[eventKey]) {
656
- trackingUrls[eventKey].push(url);
657
- }
658
- }
659
- });
660
- 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();
661
- return {
662
- id: adId,
663
- title: title,
664
- duration: duration,
665
- mediaFiles: mediaFiles,
666
- trackingUrls: trackingUrls,
667
- clickThrough: clickThrough
668
- };
669
- } catch (error) {
670
- console.error("".concat(logPrefix, " Error parsing VAST XML:"), error);
671
- return null;
672
- }
673
- }
674
- function fetchAndParseVastAd(vastTagUrl) {
675
- var filter = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "all", logPrefix = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "[VastParser]";
676
- return _async_to_generator(function() {
677
- var response, vastXml;
678
- return _ts_generator(this, function(_state) {
679
- switch(_state.label){
680
- case 0:
681
- return [
682
- 4,
683
- fetch(vastTagUrl, {
684
- mode: "cors",
685
- credentials: "include",
686
- headers: {
687
- Accept: "application/xml, text/xml, */*"
688
- },
689
- referrerPolicy: "no-referrer-when-downgrade"
690
- })
691
- ];
692
- case 1:
693
- response = _state.sent();
694
- if (!response.ok) {
695
- throw new Error("Failed to fetch VAST: ".concat(response.statusText));
696
- }
697
- return [
698
- 4,
699
- response.text()
700
- ];
701
- case 2:
702
- vastXml = _state.sent();
703
- console.log("".concat(logPrefix, " VAST XML received"));
704
- console.log("".concat(logPrefix, " VAST XML content (first 2000 chars):"), vastXml.substring(0, 2e3));
705
- return [
706
- 2,
707
- parseVastXml(vastXml, filter, logPrefix)
708
- ];
709
- }
710
- });
711
- })();
712
- }
713
- function createEmptyTrackingState() {
714
- return {
715
- impression: false,
716
- start: false,
717
- firstQuartile: false,
718
- midpoint: false,
719
- thirdQuartile: false,
720
- complete: false
721
- };
722
- }
723
543
  function firePixelWithRetry(url) {
724
544
  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]";
725
545
  return _async_to_generator(function() {
@@ -842,16 +662,164 @@ function fireTrackingPixels(urls, sessionId) {
842
662
  }
843
663
  });
844
664
  }
845
- // src/sdk/vastManager.ts
846
- 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";
847
- var DEFAULT_TIMEOUT_MS = 5e3;
848
- var MAX_RETRIES = 3;
849
- var RETRY_BACKOFF_MS = 1500;
850
- function createVastManager() {
851
- var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
852
- var _options_debug;
853
- var initialized = false;
854
- var debug = (_options_debug = options.debug) !== null && _options_debug !== void 0 ? _options_debug : false;
665
+ // src/sdk/adstormPlayer.ts
666
+ var SUPPORTED_VIDEO_EXTENSIONS = [
667
+ ".mp4",
668
+ ".webm",
669
+ ".ogg",
670
+ ".m3u8",
671
+ ".ts"
672
+ ];
673
+ var UNSUPPORTED_VIDEO_EXTENSIONS = [
674
+ ".flv",
675
+ ".f4v",
676
+ ".swf",
677
+ ".wmv",
678
+ ".avi",
679
+ ".mov",
680
+ ".mkv"
681
+ ];
682
+ var REQUEST_TIMEOUT_MS = 5e3;
683
+ var REQUEST_MAX_RETRIES = 3;
684
+ var REQUEST_RETRY_BACKOFF_MS = 1500;
685
+ var AD_LAYER_Z_INDEX = "2147483646";
686
+ var COUNTDOWN_Z_INDEX = "2147483647";
687
+ var STALL_TIMEOUT_MS = 8e3;
688
+ function getFileExtension(url) {
689
+ try {
690
+ var pathname = new URL(url, "http://dummy").pathname;
691
+ var lastDot = pathname.lastIndexOf(".");
692
+ if (lastDot === -1) return "";
693
+ return pathname.slice(lastDot).toLowerCase();
694
+ } catch (unused) {
695
+ var lastDot1 = url.lastIndexOf(".");
696
+ if (lastDot1 === -1) return "";
697
+ var ext = url.slice(lastDot1).split(/[?#]/)[0];
698
+ return (ext || "").toLowerCase();
699
+ }
700
+ }
701
+ function isUnsupportedFormat(url) {
702
+ var ext = getFileExtension(url);
703
+ return UNSUPPORTED_VIDEO_EXTENSIONS.indexOf(ext) !== -1;
704
+ }
705
+ function replaceFlvExtension(url) {
706
+ var ext = getFileExtension(url);
707
+ if (ext === ".flv") {
708
+ return url.replace(/\.flv(\?|$)/i, ".mp4$1");
709
+ }
710
+ return url;
711
+ }
712
+ function isSupportedFormat(url, mimeType) {
713
+ if (isUnsupportedFormat(url)) {
714
+ return false;
715
+ }
716
+ var ext = getFileExtension(url);
717
+ if (SUPPORTED_VIDEO_EXTENSIONS.indexOf(ext) !== -1) {
718
+ return true;
719
+ }
720
+ if (ext === "" || ext === ".") {
721
+ return mimeType.includes("video/mp4") || mimeType.includes("video/webm") || mimeType.includes("m3u8") || mimeType.includes("application/x-mpegurl");
722
+ }
723
+ return false;
724
+ }
725
+ function createAdStormPlayer(contentVideo, options) {
726
+ var licenseKey = options.licenseKey, _options_debug = options.debug, debug = _options_debug === void 0 ? false : _options_debug;
727
+ var adPlaying = false;
728
+ var originalMutedState = false;
729
+ var originalVolume = Math.max(0, Math.min(1, contentVideo.volume || 1));
730
+ var listeners = /* @__PURE__ */ new Map();
731
+ var adVideoElement;
732
+ var adContainerEl;
733
+ var adCountdownEl;
734
+ var currentAd;
735
+ var destroyed = false;
736
+ var tornDown = false;
737
+ var continueLiveStreamDuringAds = false;
738
+ var sessionId;
739
+ var adStallTimerId;
740
+ var adCountdownTimerId;
741
+ var adHideTimerId;
742
+ var lastCountdownSecond = -1;
743
+ var adListenersBound = false;
744
+ var parentPositionOverridden = false;
745
+ var adHandlers = {
746
+ timeupdate: function timeupdate() {
747
+ if (!currentAd || !adVideoElement || destroyed || tornDown) return;
748
+ var progress = adVideoElement.currentTime / currentAd.duration;
749
+ if (progress >= 0.25 && !trackingFired.firstQuartile) {
750
+ trackingFired.firstQuartile = true;
751
+ fireTrackingPixels2(currentAd.trackingUrls.firstQuartile);
752
+ }
753
+ if (progress >= 0.5 && !trackingFired.midpoint) {
754
+ trackingFired.midpoint = true;
755
+ fireTrackingPixels2(currentAd.trackingUrls.midpoint);
756
+ }
757
+ if (progress >= 0.75 && !trackingFired.thirdQuartile) {
758
+ trackingFired.thirdQuartile = true;
759
+ fireTrackingPixels2(currentAd.trackingUrls.thirdQuartile);
760
+ }
761
+ updateAdCountdown();
762
+ },
763
+ playing: function playing() {
764
+ clearAdStallTimer();
765
+ if (!currentAd || trackingFired.start || destroyed || tornDown) return;
766
+ trackingFired.start = true;
767
+ fireTrackingPixels2(currentAd.trackingUrls.start);
768
+ startAdCountdown();
769
+ log("Ad started playing");
770
+ },
771
+ ended: function ended() {
772
+ if (!currentAd || trackingFired.complete || destroyed || tornDown) return;
773
+ trackingFired.complete = true;
774
+ fireTrackingPixels2(currentAd.trackingUrls.complete);
775
+ log("Ad completed");
776
+ handleAdComplete();
777
+ },
778
+ error: function error(e) {
779
+ if (destroyed || tornDown) return;
780
+ console.error("[AdStormPlayer] Ad video error:", e);
781
+ if (currentAd) fireTrackingPixels2(currentAd.trackingUrls.error);
782
+ handleAdError();
783
+ },
784
+ waiting: function waiting() {
785
+ clearAdStallTimer();
786
+ adStallTimerId = setTimeout(function() {
787
+ adStallTimerId = void 0;
788
+ if (!adPlaying || destroyed || tornDown) return;
789
+ console.warn("[AdStormPlayer] Ad playback stalled too long");
790
+ handleAdError();
791
+ }, STALL_TIMEOUT_MS);
792
+ },
793
+ volumechange: function volumechange() {
794
+ if (!currentAd || !adVideoElement || destroyed || tornDown) return;
795
+ if (adVideoElement.muted || adVideoElement.volume <= 0) {
796
+ fireTrackingPixels2(currentAd.trackingUrls.mute);
797
+ } else {
798
+ fireTrackingPixels2(currentAd.trackingUrls.unmute);
799
+ }
800
+ },
801
+ pause: function pause() {
802
+ if (!currentAd || !adVideoElement || destroyed || tornDown) return;
803
+ if (!adVideoElement.ended) {
804
+ fireTrackingPixels2(currentAd.trackingUrls.pause);
805
+ }
806
+ },
807
+ play: function play() {
808
+ if (!currentAd || !adVideoElement || destroyed || tornDown) return;
809
+ if (adVideoElement.currentTime > 0) {
810
+ fireTrackingPixels2(currentAd.trackingUrls.resume);
811
+ }
812
+ }
813
+ };
814
+ var trackingFired = {
815
+ impression: false,
816
+ start: false,
817
+ firstQuartile: false,
818
+ midpoint: false,
819
+ thirdQuartile: false,
820
+ complete: false
821
+ };
822
+ var preloadSlots = /* @__PURE__ */ new Map();
855
823
  function log() {
856
824
  for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
857
825
  args[_key] = arguments[_key];
@@ -859,58 +827,340 @@ function createVastManager() {
859
827
  if (debug) {
860
828
  var _console;
861
829
  (_console = console).log.apply(_console, [
862
- "[VastManager]"
830
+ "[AdStormPlayer]"
863
831
  ].concat(_to_consumable_array(args)));
864
832
  }
865
833
  }
866
- function warn() {
867
- for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
868
- args[_key] = arguments[_key];
834
+ function emit(event, payload) {
835
+ var set = listeners.get(event);
836
+ if (!set) return;
837
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
838
+ try {
839
+ for(var _iterator = Array.from(set)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
840
+ var fn = _step.value;
841
+ try {
842
+ fn(payload);
843
+ } catch (error) {
844
+ console.warn("[AdStormPlayer] Error in event listener for ".concat(event, ":"), error);
845
+ }
846
+ }
847
+ } catch (err) {
848
+ _didIteratorError = true;
849
+ _iteratorError = err;
850
+ } finally{
851
+ try {
852
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
853
+ _iterator.return();
854
+ }
855
+ } finally{
856
+ if (_didIteratorError) {
857
+ throw _iteratorError;
858
+ }
859
+ }
869
860
  }
870
- var _console;
871
- (_console = console).warn.apply(_console, [
872
- "[VastManager]"
873
- ].concat(_to_consumable_array(args)));
874
861
  }
875
- function initialize() {
876
- return _async_to_generator(function() {
877
- return _ts_generator(this, function(_state) {
878
- if (initialized) return [
879
- 2
880
- ];
881
- initialized = true;
882
- log("Initialized, VAST tag URL:", VAST_TAG_URL.split("?")[0]);
883
- return [
884
- 2
885
- ];
862
+ function fireTrackingPixels2(urls) {
863
+ fireTrackingPixels(urls, sessionId, "[AdStormPlayer]");
864
+ }
865
+ function clearAdStallTimer() {
866
+ if (adStallTimerId) {
867
+ clearTimeout(adStallTimerId);
868
+ adStallTimerId = void 0;
869
+ }
870
+ }
871
+ function clearAdCountdownTimer() {
872
+ if (adCountdownTimerId) {
873
+ clearInterval(adCountdownTimerId);
874
+ adCountdownTimerId = void 0;
875
+ }
876
+ lastCountdownSecond = -1;
877
+ }
878
+ function updateAdCountdown() {
879
+ if (!adCountdownEl || !adVideoElement || !currentAd || !adPlaying) return;
880
+ var remainingSec = Math.max(0, Math.ceil((currentAd.duration || 0) - adVideoElement.currentTime));
881
+ if (remainingSec === lastCountdownSecond) return;
882
+ lastCountdownSecond = remainingSec;
883
+ adCountdownEl.textContent = "Ad ".concat(remainingSec, "s");
884
+ emit("ad_countdown", {
885
+ remainingSec: remainingSec,
886
+ durationSec: currentAd.duration,
887
+ currentTimeSec: adVideoElement.currentTime
888
+ });
889
+ }
890
+ function startAdCountdown() {
891
+ clearAdCountdownTimer();
892
+ updateAdCountdown();
893
+ adCountdownTimerId = setInterval(updateAdCountdown, 250);
894
+ }
895
+ function generateSessionId() {
896
+ return "adstorm-".concat(Date.now(), "-").concat(Math.random().toString(36).slice(2, 10));
897
+ }
898
+ function bindAdEventListeners() {
899
+ if (!adVideoElement || adListenersBound) return;
900
+ adVideoElement.addEventListener("timeupdate", adHandlers.timeupdate);
901
+ adVideoElement.addEventListener("playing", adHandlers.playing);
902
+ adVideoElement.addEventListener("ended", adHandlers.ended);
903
+ adVideoElement.addEventListener("error", adHandlers.error);
904
+ adVideoElement.addEventListener("waiting", adHandlers.waiting);
905
+ adVideoElement.addEventListener("volumechange", adHandlers.volumechange);
906
+ adVideoElement.addEventListener("pause", adHandlers.pause);
907
+ adVideoElement.addEventListener("play", adHandlers.play);
908
+ adListenersBound = true;
909
+ }
910
+ function unbindAdEventListeners() {
911
+ if (!adVideoElement || !adListenersBound) return;
912
+ adVideoElement.removeEventListener("timeupdate", adHandlers.timeupdate);
913
+ adVideoElement.removeEventListener("playing", adHandlers.playing);
914
+ adVideoElement.removeEventListener("ended", adHandlers.ended);
915
+ adVideoElement.removeEventListener("error", adHandlers.error);
916
+ adVideoElement.removeEventListener("waiting", adHandlers.waiting);
917
+ adVideoElement.removeEventListener("volumechange", adHandlers.volumechange);
918
+ adVideoElement.removeEventListener("pause", adHandlers.pause);
919
+ adVideoElement.removeEventListener("play", adHandlers.play);
920
+ adListenersBound = false;
921
+ }
922
+ function teardownCurrentPlayback() {
923
+ unbindAdEventListeners();
924
+ clearAdStallTimer();
925
+ clearAdCountdownTimer();
926
+ if (!adVideoElement) return;
927
+ adVideoElement.pause();
928
+ adVideoElement.removeAttribute("src");
929
+ adVideoElement.load();
930
+ }
931
+ function buildVastUrl(durationSeconds, metadata) {
932
+ var baseUrl = "https://adstorm.co/api-adstorm-dev/adstorm/vast/".concat(licenseKey, "/pod");
933
+ var defaultMetadata = {
934
+ video: {
935
+ codec: "h264",
936
+ width: contentVideo.videoWidth || 1280,
937
+ height: contentVideo.videoHeight || 720,
938
+ fps: 29.97,
939
+ bitrate: 5e3,
940
+ profile: "high",
941
+ pix_fmt: "yuv420p",
942
+ has_b_frames: 0
943
+ },
944
+ audio: {
945
+ codec: "aac",
946
+ sample_rate: 48e3,
947
+ bitrate: 128
948
+ }
949
+ };
950
+ var finalMetadata = metadata || defaultMetadata;
951
+ var metadataStr = encodeURIComponent(JSON.stringify(finalMetadata));
952
+ return "".concat(baseUrl, "?duration=").concat(Math.ceil(durationSeconds), "&metadata=").concat(metadataStr);
953
+ }
954
+ function parseVastXml(xmlString) {
955
+ var ads = [];
956
+ try {
957
+ var parser = new DOMParser();
958
+ var xmlDoc = parser.parseFromString(xmlString, "text/xml");
959
+ var parserError = xmlDoc.querySelector("parsererror");
960
+ if (parserError) {
961
+ console.error("[AdStormPlayer] XML parsing error:", parserError.textContent);
962
+ return [];
963
+ }
964
+ var adElements = xmlDoc.querySelectorAll("Ad");
965
+ adElements.forEach(function(adElement) {
966
+ var _adElement_querySelector, _adElement_querySelector1, _adElement_querySelector_textContent, _adElement_querySelector2;
967
+ var adId = adElement.getAttribute("id") || "unknown";
968
+ var title = ((_adElement_querySelector = adElement.querySelector("AdTitle")) === null || _adElement_querySelector === void 0 ? void 0 : _adElement_querySelector.textContent) || "Ad";
969
+ var durationText = ((_adElement_querySelector1 = adElement.querySelector("Duration")) === null || _adElement_querySelector1 === void 0 ? void 0 : _adElement_querySelector1.textContent) || "00:00:30";
970
+ var durationParts = durationText.split(":");
971
+ var duration = parseInt(durationParts[0] || "0", 10) * 3600 + parseInt(durationParts[1] || "0", 10) * 60 + parseFloat(durationParts[2] || "0");
972
+ var mediaFileElements = adElement.querySelectorAll("MediaFile");
973
+ var mediaFiles = [];
974
+ mediaFileElements.forEach(function(mf) {
975
+ var _mf_textContent;
976
+ var type = mf.getAttribute("type") || "";
977
+ var url = ((_mf_textContent = mf.textContent) === null || _mf_textContent === void 0 ? void 0 : _mf_textContent.trim()) || "";
978
+ var width = parseInt(mf.getAttribute("width") || "1920", 10);
979
+ var height = parseInt(mf.getAttribute("height") || "1080", 10);
980
+ var bitrate = mf.getAttribute("bitrate") ? parseInt(mf.getAttribute("bitrate"), 10) : void 0;
981
+ if (!url) {
982
+ log("Skipping empty MediaFile URL");
983
+ return;
984
+ }
985
+ var originalUrl = url;
986
+ url = replaceFlvExtension(url);
987
+ if (url !== originalUrl) {
988
+ log("Converted FLV to MP4: ".concat(originalUrl, " -> ").concat(url));
989
+ }
990
+ if (isUnsupportedFormat(url)) {
991
+ var ext = getFileExtension(url);
992
+ log("Skipping unsupported format: ".concat(url, " (extension: ").concat(ext, ", declared type: ").concat(type, ")"));
993
+ return;
994
+ }
995
+ if (isSupportedFormat(url, type)) {
996
+ mediaFiles.push({
997
+ url: url,
998
+ type: type,
999
+ width: width,
1000
+ height: height,
1001
+ bitrate: bitrate
1002
+ });
1003
+ log("Found media file: ".concat(url, " (").concat(type, ", ").concat(width, "x").concat(height, ")"));
1004
+ } else {
1005
+ log("Skipping incompatible media file: ".concat(url, " (type: ").concat(type, ")"));
1006
+ }
1007
+ });
1008
+ if (mediaFiles.length === 0) {
1009
+ log("No valid media files found in ad:", adId);
1010
+ return;
1011
+ }
1012
+ var trackingUrls = {
1013
+ impression: [],
1014
+ start: [],
1015
+ firstQuartile: [],
1016
+ midpoint: [],
1017
+ thirdQuartile: [],
1018
+ complete: [],
1019
+ mute: [],
1020
+ unmute: [],
1021
+ pause: [],
1022
+ resume: [],
1023
+ error: []
1024
+ };
1025
+ adElement.querySelectorAll("Impression").forEach(function(el) {
1026
+ var _el_textContent;
1027
+ var url = (_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim();
1028
+ if (url) trackingUrls.impression.push(url);
1029
+ });
1030
+ adElement.querySelectorAll("Tracking").forEach(function(el) {
1031
+ var _el_textContent;
1032
+ var event = el.getAttribute("event");
1033
+ var url = (_el_textContent = el.textContent) === null || _el_textContent === void 0 ? void 0 : _el_textContent.trim();
1034
+ if (event && url) {
1035
+ var eventKey = event;
1036
+ if (trackingUrls[eventKey]) {
1037
+ trackingUrls[eventKey].push(url);
1038
+ }
1039
+ }
1040
+ });
1041
+ 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();
1042
+ ads.push({
1043
+ id: adId,
1044
+ title: title,
1045
+ duration: duration,
1046
+ mediaFiles: mediaFiles,
1047
+ trackingUrls: trackingUrls,
1048
+ clickThrough: clickThrough
1049
+ });
1050
+ log("Parsed ad: ".concat(title, ", duration: ").concat(duration, "s, media files: ").concat(mediaFiles.length));
886
1051
  });
887
- })();
1052
+ } catch (error) {
1053
+ console.error("[AdStormPlayer] Error parsing VAST XML:", error);
1054
+ }
1055
+ return ads;
1056
+ }
1057
+ function selectBestMediaFile(mediaFiles) {
1058
+ if (mediaFiles.length === 0) return null;
1059
+ if (mediaFiles.length === 1) return mediaFiles[0];
1060
+ var mp4Files = mediaFiles.filter(function(mf) {
1061
+ return mf.type.includes("video/mp4");
1062
+ });
1063
+ var candidates = mp4Files.length > 0 ? mp4Files : mediaFiles;
1064
+ var targetWidth = contentVideo.videoWidth || 1280;
1065
+ var targetHeight = contentVideo.videoHeight || 720;
1066
+ candidates.sort(function(a, b) {
1067
+ var diffA = Math.abs(a.width - targetWidth) + Math.abs(a.height - targetHeight);
1068
+ var diffB = Math.abs(b.width - targetWidth) + Math.abs(b.height - targetHeight);
1069
+ return diffA - diffB;
1070
+ });
1071
+ return candidates[0] || null;
1072
+ }
1073
+ function createAdVideoElement() {
1074
+ var video = document.createElement("video");
1075
+ video.style.position = "absolute";
1076
+ video.style.left = "0";
1077
+ video.style.top = "0";
1078
+ video.style.width = "100%";
1079
+ video.style.height = "100%";
1080
+ video.style.objectFit = "contain";
1081
+ video.style.backgroundColor = "#000";
1082
+ video.style.zIndex = "1";
1083
+ video.playsInline = true;
1084
+ video.preload = "auto";
1085
+ video.muted = originalMutedState;
1086
+ video.volume = originalMutedState ? 0 : originalVolume;
1087
+ return video;
1088
+ }
1089
+ function setAdPlayingFlag(isPlaying) {
1090
+ if (isPlaying) {
1091
+ contentVideo.dataset.stormcloudAdPlaying = "true";
1092
+ } else {
1093
+ delete contentVideo.dataset.stormcloudAdPlaying;
1094
+ }
1095
+ }
1096
+ function setupAdEventListeners() {
1097
+ bindAdEventListeners();
1098
+ }
1099
+ function handleAdComplete() {
1100
+ if (destroyed || tornDown) return;
1101
+ log("Handling ad completion");
1102
+ adPlaying = false;
1103
+ setAdPlayingFlag(false);
1104
+ clearAdStallTimer();
1105
+ clearAdCountdownTimer();
1106
+ if (adContainerEl) {
1107
+ adContainerEl.style.opacity = "0";
1108
+ adHideTimerId = setTimeout(function() {
1109
+ if (adContainerEl) {
1110
+ adContainerEl.style.display = "none";
1111
+ adContainerEl.style.pointerEvents = "none";
1112
+ }
1113
+ }, 300);
1114
+ }
1115
+ contentVideo.style.visibility = "visible";
1116
+ contentVideo.style.opacity = "1";
1117
+ contentVideo.muted = originalMutedState;
1118
+ contentVideo.volume = originalVolume;
1119
+ currentAd = void 0;
1120
+ emit("content_resume");
1121
+ emit("all_ads_completed");
1122
+ }
1123
+ function handleAdError() {
1124
+ if (destroyed || tornDown) return;
1125
+ log("Handling ad error");
1126
+ if (!adPlaying) return;
1127
+ adPlaying = false;
1128
+ setAdPlayingFlag(false);
1129
+ clearAdStallTimer();
1130
+ clearAdCountdownTimer();
1131
+ contentVideo.muted = originalMutedState;
1132
+ contentVideo.volume = originalVolume;
1133
+ contentVideo.style.visibility = "visible";
1134
+ contentVideo.style.opacity = "1";
1135
+ if (adContainerEl) {
1136
+ adContainerEl.style.display = "none";
1137
+ adContainerEl.style.pointerEvents = "none";
1138
+ }
1139
+ currentAd = void 0;
1140
+ emit("ad_error");
1141
+ emit("content_resume");
888
1142
  }
889
- function requestBids(_context) {
1143
+ function fetchVastOnce(durationSeconds) {
890
1144
  return _async_to_generator(function() {
891
- var correlator, url, controller, timeoutId, _ref, _ref1, _vastAd_mediaFiles_, _vastAd_mediaFiles_1, fetchOptions, response, vastXml, vastAd, bid, error;
1145
+ var vastUrl, controller, timeoutId, requestInit, response, xmlText;
892
1146
  return _ts_generator(this, function(_state) {
893
1147
  switch(_state.label){
894
1148
  case 0:
895
- if (!initialized) {
896
- throw new Error("VastManager not initialized. Call initialize() first.");
897
- }
898
- correlator = Math.floor(Math.random() * 1e12).toString();
899
- url = VAST_TAG_URL.replace("[placeholder]", correlator);
900
- log("Fetching VAST tag, correlator:", correlator);
1149
+ vastUrl = buildVastUrl(durationSeconds);
1150
+ log("Fetching VAST from:", vastUrl);
901
1151
  controller = typeof AbortController !== "undefined" ? new AbortController() : null;
902
1152
  timeoutId = setTimeout(function() {
903
1153
  return controller === null || controller === void 0 ? void 0 : controller.abort();
904
- }, DEFAULT_TIMEOUT_MS);
1154
+ }, REQUEST_TIMEOUT_MS);
905
1155
  _state.label = 1;
906
1156
  case 1:
907
1157
  _state.trys.push([
908
1158
  1,
909
- 4,
910
1159
  ,
1160
+ 4,
911
1161
  5
912
1162
  ]);
913
- fetchOptions = {
1163
+ requestInit = {
914
1164
  method: "GET",
915
1165
  mode: "cors",
916
1166
  credentials: "omit",
@@ -919,62 +1169,34 @@ function createVastManager() {
919
1169
  },
920
1170
  referrerPolicy: "no-referrer-when-downgrade"
921
1171
  };
922
- if (controller) fetchOptions.signal = controller.signal;
1172
+ if (controller) {
1173
+ requestInit.signal = controller.signal;
1174
+ }
923
1175
  return [
924
1176
  4,
925
- fetch(url, fetchOptions)
1177
+ fetch(vastUrl, requestInit)
926
1178
  ];
927
1179
  case 2:
928
1180
  response = _state.sent();
929
- clearTimeout(timeoutId);
930
1181
  if (!response.ok) {
931
- throw new Error("VAST request returned HTTP ".concat(response.status));
1182
+ throw new Error("Failed to fetch VAST: ".concat(response.status, " ").concat(response.statusText));
932
1183
  }
933
1184
  return [
934
1185
  4,
935
1186
  response.text()
936
1187
  ];
937
1188
  case 3:
938
- vastXml = _state.sent();
939
- log("VAST XML received, length:", vastXml.length);
940
- vastAd = parseVastXml(vastXml, "mp4-first", "[VastManager]");
941
- if (!vastAd) {
942
- log("VAST parsed but no usable ad found");
943
- return [
944
- 2,
945
- []
946
- ];
947
- }
948
- log("Ad parsed: id=".concat(vastAd.id, ", duration=").concat(vastAd.duration, "s, mediaFiles=").concat(vastAd.mediaFiles.length));
949
- bid = {
950
- bidder: "vast-direct",
951
- cpm: 0,
952
- vastXml: vastXml,
953
- width: (_ref = (_vastAd_mediaFiles_ = vastAd.mediaFiles[0]) === null || _vastAd_mediaFiles_ === void 0 ? void 0 : _vastAd_mediaFiles_.width) !== null && _ref !== void 0 ? _ref : 0,
954
- 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,
955
- adId: vastAd.id,
956
- impId: correlator,
957
- creativeId: vastAd.id,
958
- currency: "USD",
959
- durationSec: vastAd.duration
960
- };
1189
+ xmlText = _state.sent();
1190
+ log("VAST response received, length:", xmlText.length);
961
1191
  return [
962
1192
  2,
963
- [
964
- bid
965
- ]
1193
+ parseVastXml(xmlText)
966
1194
  ];
967
1195
  case 4:
968
- error = _state.sent();
969
1196
  clearTimeout(timeoutId);
970
- if ((error === null || error === void 0 ? void 0 : error.name) === "AbortError") {
971
- warn("VAST request timed out after ".concat(DEFAULT_TIMEOUT_MS, "ms"));
972
- return [
973
- 2,
974
- []
975
- ];
976
- }
977
- throw error;
1197
+ return [
1198
+ 7
1199
+ ];
978
1200
  case 5:
979
1201
  return [
980
1202
  2
@@ -983,14 +1205,14 @@ function createVastManager() {
983
1205
  });
984
1206
  })();
985
1207
  }
986
- function requestBidsUntilResponse(context) {
1208
+ function fetchVast(durationSeconds) {
987
1209
  return _async_to_generator(function() {
988
1210
  var _loop, lastError, attempt, _ret;
989
1211
  return _ts_generator(this, function(_state) {
990
1212
  switch(_state.label){
991
1213
  case 0:
992
1214
  _loop = function(attempt) {
993
- var bids, err, delay;
1215
+ var ads, error, delay;
994
1216
  return _ts_generator(this, function(_state) {
995
1217
  switch(_state.label){
996
1218
  case 0:
@@ -1002,39 +1224,39 @@ function createVastManager() {
1002
1224
  ]);
1003
1225
  return [
1004
1226
  4,
1005
- requestBids(context)
1227
+ fetchVastOnce(durationSeconds)
1006
1228
  ];
1007
1229
  case 1:
1008
- bids = _state.sent();
1009
- if (bids.length > 0) {
1010
- log("requestBidsUntilResponse: got ".concat(bids.length, " ad(s) on attempt ").concat(attempt));
1011
- return [
1012
- 2,
1013
- {
1014
- v: bids
1015
- }
1016
- ];
1017
- }
1018
- log("requestBidsUntilResponse: no ads on attempt ".concat(attempt, "/").concat(MAX_RETRIES));
1230
+ ads = _state.sent();
1231
+ if (ads.length > 0) return [
1232
+ 2,
1233
+ {
1234
+ v: ads
1235
+ }
1236
+ ];
1237
+ log("No ad returned from VAST on attempt ".concat(attempt, "/").concat(REQUEST_MAX_RETRIES));
1019
1238
  return [
1020
1239
  3,
1021
1240
  3
1022
1241
  ];
1023
1242
  case 2:
1024
- err = _state.sent();
1025
- lastError = err;
1026
- warn("requestBidsUntilResponse: attempt ".concat(attempt, "/").concat(MAX_RETRIES, " failed:"), err);
1243
+ error = _state.sent();
1244
+ lastError = error;
1245
+ if ((error === null || error === void 0 ? void 0 : error.name) === "AbortError") {
1246
+ console.warn("[AdStormPlayer] VAST request timed out (".concat(REQUEST_TIMEOUT_MS, "ms), attempt ").concat(attempt, "/").concat(REQUEST_MAX_RETRIES));
1247
+ } else {
1248
+ console.warn("[AdStormPlayer] VAST request failed on attempt ".concat(attempt, "/").concat(REQUEST_MAX_RETRIES, ":"), error);
1249
+ }
1027
1250
  return [
1028
1251
  3,
1029
1252
  3
1030
1253
  ];
1031
1254
  case 3:
1032
- if (!(attempt < MAX_RETRIES)) return [
1255
+ if (!(attempt < REQUEST_MAX_RETRIES)) return [
1033
1256
  3,
1034
1257
  5
1035
1258
  ];
1036
- delay = RETRY_BACKOFF_MS * attempt;
1037
- log("requestBidsUntilResponse: waiting ".concat(delay, "ms before retry"));
1259
+ delay = REQUEST_RETRY_BACKOFF_MS * attempt;
1038
1260
  return [
1039
1261
  4,
1040
1262
  new Promise(function(resolve) {
@@ -1051,13 +1273,10 @@ function createVastManager() {
1051
1273
  }
1052
1274
  });
1053
1275
  };
1054
- if (!initialized) {
1055
- throw new Error("VastManager not initialized. Call initialize() first.");
1056
- }
1057
1276
  attempt = 1;
1058
1277
  _state.label = 1;
1059
1278
  case 1:
1060
- if (!(attempt <= MAX_RETRIES)) return [
1279
+ if (!(attempt <= REQUEST_MAX_RETRIES)) return [
1061
1280
  3,
1062
1281
  4
1063
1282
  ];
@@ -1079,7 +1298,9 @@ function createVastManager() {
1079
1298
  1
1080
1299
  ];
1081
1300
  case 4:
1082
- if (_instanceof(lastError, Error)) throw lastError;
1301
+ if (_instanceof(lastError, Error)) {
1302
+ throw lastError;
1303
+ }
1083
1304
  return [
1084
1305
  2,
1085
1306
  []
@@ -1088,971 +1309,468 @@ function createVastManager() {
1088
1309
  });
1089
1310
  })();
1090
1311
  }
1091
- function destroy() {
1092
- initialized = false;
1093
- log("Destroyed");
1094
- }
1095
- return {
1096
- initialize: initialize,
1097
- requestBids: requestBids,
1098
- requestBidsUntilResponse: requestBidsUntilResponse,
1099
- destroy: destroy,
1100
- get isInitialized () {
1101
- return initialized;
1102
- }
1103
- };
1104
- }
1105
- // src/sdk/vastAdLayer.ts
1106
- var import_hls = __toESM(require("hls.js"), 1);
1107
- var LOG = "[VastAdLayer]";
1108
- function resolveBidToVastAd(winner, logPrefix) {
1109
- if (winner.vastXml) {
1110
- var ad = parseVastXml(winner.vastXml, "mp4-first", logPrefix);
1111
- return Promise.resolve(ad);
1112
- }
1113
- if (winner.vastUrl) {
1114
- return fetchAndParseVastAd(winner.vastUrl, "mp4-first", logPrefix);
1115
- }
1116
- return Promise.resolve(null);
1117
- }
1118
- function createVastAdLayer(contentVideo, options) {
1119
- var _ref, _ref1, _ref2, _ref3, _ref4;
1120
- var adPlaying = false;
1121
- var originalMutedState = false;
1122
- var originalVolume = Math.max(0, Math.min(1, contentVideo.volume || 1));
1123
- var listeners = /* @__PURE__ */ new Map();
1124
- var mainHlsInstance = options === null || options === void 0 ? void 0 : options.mainHlsInstance;
1125
- var continueLiveStreamDuringAds = (_ref = options === null || options === void 0 ? void 0 : options.continueLiveStreamDuringAds) !== null && _ref !== void 0 ? _ref : false;
1126
- var smartTVMode = (_ref1 = options === null || options === void 0 ? void 0 : options.smartTVMode) !== null && _ref1 !== void 0 ? _ref1 : false;
1127
- var singleElementMode = (_ref2 = options === null || options === void 0 ? void 0 : options.singleElementMode) !== null && _ref2 !== void 0 ? _ref2 : false;
1128
- var forceMP4Ads = (_ref3 = options === null || options === void 0 ? void 0 : options.forceMP4Ads) !== null && _ref3 !== void 0 ? _ref3 : smartTVMode || singleElementMode;
1129
- var debug = (_ref4 = options === null || options === void 0 ? void 0 : options.debug) !== null && _ref4 !== void 0 ? _ref4 : false;
1130
- var adVideoElement;
1131
- var adHls;
1132
- var adContainerEl;
1133
- var currentAd;
1134
- var sessionId;
1135
- var destroyed = false;
1136
- var tornDown = false;
1137
- var trackingFired = createEmptyTrackingState();
1138
- var adStallTimerId;
1139
- var savedContentVideoStyles;
1140
- var currentAdEventHandlers;
1141
- var preloadSlots = /* @__PURE__ */ new Map();
1142
- function emit(event, payload) {
1143
- var set = listeners.get(event);
1144
- if (!set) return;
1145
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1146
- try {
1147
- for(var _iterator = Array.from(set)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1148
- var fn = _step.value;
1149
- try {
1150
- fn(payload);
1151
- } catch (error) {
1152
- console.warn("".concat(LOG, " Error in event listener for ").concat(event, ":"), error);
1153
- }
1154
- }
1155
- } catch (err) {
1156
- _didIteratorError = true;
1157
- _iteratorError = err;
1158
- } finally{
1159
- try {
1160
- if (!_iteratorNormalCompletion && _iterator.return != null) {
1161
- _iterator.return();
1162
- }
1163
- } finally{
1164
- if (_didIteratorError) {
1165
- throw _iteratorError;
1166
- }
1167
- }
1312
+ function getDurationSecondsFromContext(requestContext) {
1313
+ var _ctx_remainingBreakSec;
1314
+ if (!requestContext || (typeof requestContext === "undefined" ? "undefined" : _type_of(requestContext)) !== "object") {
1315
+ return 30;
1168
1316
  }
1169
- }
1170
- function generateSessionId() {
1171
- return "session-".concat(Date.now(), "-").concat(Math.random().toString(36).substr(2, 9));
1172
- }
1173
- function fireTrackingPixels2(urls) {
1174
- fireTrackingPixels(urls, sessionId, LOG);
1175
- }
1176
- function getMainStreamQuality() {
1177
- if (!(mainHlsInstance === null || mainHlsInstance === void 0 ? void 0 : mainHlsInstance.levels)) return null;
1178
- var currentLevel = mainHlsInstance.currentLevel;
1179
- if (currentLevel === -1 || !mainHlsInstance.levels[currentLevel]) {
1180
- var autoLevel = mainHlsInstance.loadLevel;
1181
- if (autoLevel !== -1 && mainHlsInstance.levels[autoLevel]) {
1182
- var level2 = mainHlsInstance.levels[autoLevel];
1183
- return {
1184
- width: level2.width || 1920,
1185
- height: level2.height || 1080,
1186
- bitrate: level2.bitrate || 5e6
1187
- };
1188
- }
1189
- return null;
1317
+ var ctx = requestContext;
1318
+ var value = (_ctx_remainingBreakSec = ctx.remainingBreakSec) !== null && _ctx_remainingBreakSec !== void 0 ? _ctx_remainingBreakSec : ctx.breakDurationSec;
1319
+ if (typeof value !== "number" || Number.isNaN(value)) {
1320
+ return 30;
1190
1321
  }
1191
- var level = mainHlsInstance.levels[currentLevel];
1192
- return {
1193
- width: level.width || 1920,
1194
- height: level.height || 1080,
1195
- bitrate: level.bitrate || 5e6
1196
- };
1322
+ return Math.max(1, Math.ceil(value));
1197
1323
  }
1198
- function selectBestMediaFile(mediaFiles) {
1199
- var _ref;
1200
- var _scoredFiles_;
1201
- if (mediaFiles.length === 0) throw new Error("No media files available");
1202
- var candidates = mediaFiles;
1203
- if (forceMP4Ads) {
1204
- var mp4Only = candidates.filter(function(f) {
1205
- return !isHlsMediaFile(f);
1206
- });
1207
- if (mp4Only.length > 0) {
1208
- candidates = mp4Only;
1209
- if (debug) console.log("".concat(LOG, " forceMP4Ads: filtered to ").concat(mp4Only.length, " MP4-only file(s)"));
1210
- } else if (debug) {
1211
- console.warn("".concat(LOG, " forceMP4Ads: no MP4 files available, falling back to all media files"));
1212
- }
1213
- } else {
1214
- var mp4Only1 = candidates.filter(function(f) {
1215
- return !isHlsMediaFile(f);
1216
- });
1217
- if (mp4Only1.length > 0) {
1218
- candidates = mp4Only1;
1219
- if (debug) console.log("".concat(LOG, " Preferring ").concat(mp4Only1.length, " MP4 file(s) over HLS (mp4-first)"));
1220
- }
1221
- }
1222
- var firstFile = candidates[0];
1223
- if (candidates.length === 1) return firstFile;
1224
- var mainQuality = getMainStreamQuality();
1225
- if (!mainQuality) {
1226
- if (debug) console.log("".concat(LOG, " No main stream quality info, using first media file"));
1227
- return firstFile;
1228
- }
1229
- var scoredFiles = candidates.map(function(file) {
1230
- var widthDiff = Math.abs(file.width - mainQuality.width);
1231
- var heightDiff = Math.abs(file.height - mainQuality.height);
1232
- var resolutionDiff = widthDiff + heightDiff;
1233
- var fileBitrate = (file.bitrate || 5e3) * 1e3;
1234
- var bitrateDiff = Math.abs(fileBitrate - mainQuality.bitrate);
1235
- var score = resolutionDiff * 2 + bitrateDiff / 1e3;
1236
- return {
1237
- file: file,
1238
- score: score
1239
- };
1240
- });
1241
- scoredFiles.sort(function(a, b) {
1242
- return a.score - b.score;
1243
- });
1244
- return (_ref = (_scoredFiles_ = scoredFiles[0]) === null || _scoredFiles_ === void 0 ? void 0 : _scoredFiles_.file) !== null && _ref !== void 0 ? _ref : firstFile;
1245
- }
1246
- function isHlsMediaFile(file) {
1247
- return file.type === "application/x-mpegURL" || file.type.includes("m3u8");
1248
- }
1249
- function createAdVideoElement() {
1250
- var video = document.createElement("video");
1251
- video.style.position = "absolute";
1252
- video.style.left = "0";
1253
- video.style.top = "0";
1254
- video.style.width = "100%";
1255
- video.style.height = "100%";
1256
- video.style.objectFit = "cover";
1257
- video.style.backgroundColor = "#000";
1258
- video.playsInline = true;
1259
- video.muted = false;
1260
- video.volume = 1;
1261
- return video;
1262
- }
1263
- function clearAdStallTimer() {
1264
- if (adStallTimerId != null) {
1265
- clearTimeout(adStallTimerId);
1266
- adStallTimerId = void 0;
1267
- }
1268
- }
1269
- function removeAdEventListeners() {
1270
- clearAdStallTimer();
1271
- if (!currentAdEventHandlers || !adVideoElement) return;
1272
- var el = adVideoElement;
1273
- el.removeEventListener("timeupdate", currentAdEventHandlers.timeupdate);
1274
- el.removeEventListener("playing", currentAdEventHandlers.playing);
1275
- el.removeEventListener("ended", currentAdEventHandlers.ended);
1276
- el.removeEventListener("error", currentAdEventHandlers.error);
1277
- el.removeEventListener("volumechange", currentAdEventHandlers.volumechange);
1278
- el.removeEventListener("pause", currentAdEventHandlers.pause);
1279
- el.removeEventListener("play", currentAdEventHandlers.play);
1280
- el.removeEventListener("waiting", currentAdEventHandlers.waiting);
1281
- currentAdEventHandlers = void 0;
1282
- }
1283
- function setupAdEventListeners() {
1284
- if (!adVideoElement) return;
1285
- removeAdEventListeners();
1286
- var handlers = {
1287
- timeupdate: function timeupdate() {
1288
- var ad = currentAd;
1289
- if (!ad || !adVideoElement) return;
1290
- var progress = adVideoElement.currentTime / ad.duration;
1291
- if (progress >= 0.25 && !trackingFired.firstQuartile) {
1292
- trackingFired.firstQuartile = true;
1293
- fireTrackingPixels2(ad.trackingUrls.firstQuartile);
1294
- }
1295
- if (progress >= 0.5 && !trackingFired.midpoint) {
1296
- trackingFired.midpoint = true;
1297
- fireTrackingPixels2(ad.trackingUrls.midpoint);
1298
- }
1299
- if (progress >= 0.75 && !trackingFired.thirdQuartile) {
1300
- trackingFired.thirdQuartile = true;
1301
- fireTrackingPixels2(ad.trackingUrls.thirdQuartile);
1302
- }
1303
- },
1304
- playing: function playing() {
1305
- clearAdStallTimer();
1306
- var ad = currentAd;
1307
- if (!ad || trackingFired.start) return;
1308
- trackingFired.start = true;
1309
- fireTrackingPixels2(ad.trackingUrls.start);
1310
- if (debug) console.log("".concat(LOG, " Ad started playing"));
1311
- },
1312
- ended: function ended() {
1313
- if (tornDown || !currentAd || trackingFired.complete) return;
1314
- trackingFired.complete = true;
1315
- fireTrackingPixels2(currentAd.trackingUrls.complete);
1316
- if (debug) console.log("".concat(LOG, " Ad completed"));
1317
- handleAdComplete();
1318
- },
1319
- error: function error(e) {
1320
- if (tornDown) return;
1321
- console.error("".concat(LOG, " Ad video error:"), e);
1322
- if (currentAd) fireTrackingPixels2(currentAd.trackingUrls.error);
1323
- handleAdError();
1324
- },
1325
- volumechange: function volumechange() {
1326
- if (!currentAd || !adVideoElement) return;
1327
- if (adVideoElement.muted) {
1328
- fireTrackingPixels2(currentAd.trackingUrls.mute);
1329
- } else {
1330
- fireTrackingPixels2(currentAd.trackingUrls.unmute);
1331
- }
1332
- },
1333
- pause: function pause() {
1334
- if (currentAd && adVideoElement && !adVideoElement.ended) {
1335
- fireTrackingPixels2(currentAd.trackingUrls.pause);
1336
- }
1337
- },
1338
- play: function play() {
1339
- if (currentAd && adVideoElement && adVideoElement.currentTime > 0) {
1340
- fireTrackingPixels2(currentAd.trackingUrls.resume);
1341
- }
1342
- },
1343
- waiting: function waiting() {
1344
- clearAdStallTimer();
1345
- adStallTimerId = setTimeout(function() {
1346
- adStallTimerId = void 0;
1347
- if (adPlaying) {
1348
- if (debug) console.warn("".concat(LOG, " Ad video stalled for too long, treating as error"));
1349
- handleAdError();
1350
- }
1351
- }, 8e3);
1352
- }
1353
- };
1354
- adVideoElement.addEventListener("timeupdate", handlers.timeupdate);
1355
- adVideoElement.addEventListener("playing", handlers.playing);
1356
- adVideoElement.addEventListener("ended", handlers.ended);
1357
- adVideoElement.addEventListener("error", handlers.error);
1358
- adVideoElement.addEventListener("volumechange", handlers.volumechange);
1359
- adVideoElement.addEventListener("pause", handlers.pause);
1360
- adVideoElement.addEventListener("play", handlers.play);
1361
- adVideoElement.addEventListener("waiting", handlers.waiting);
1362
- currentAdEventHandlers = handlers;
1363
- }
1364
- function setAdPlayingFlag(isPlaying) {
1365
- if (isPlaying) {
1366
- contentVideo.dataset.stormcloudAdPlaying = "true";
1367
- } else {
1368
- delete contentVideo.dataset.stormcloudAdPlaying;
1369
- }
1370
- }
1371
- function applyContentVideoAdCoverStyles() {
1372
- if (!singleElementMode) return;
1373
- savedContentVideoStyles = {
1374
- objectFit: contentVideo.style.objectFit,
1375
- width: contentVideo.style.width,
1376
- height: contentVideo.style.height
1377
- };
1378
- contentVideo.style.objectFit = "cover";
1379
- contentVideo.style.width = "100%";
1380
- contentVideo.style.height = "100%";
1381
- }
1382
- function restoreContentVideoStyles() {
1383
- if (!singleElementMode || !savedContentVideoStyles) return;
1384
- contentVideo.style.objectFit = savedContentVideoStyles.objectFit;
1385
- contentVideo.style.width = savedContentVideoStyles.width;
1386
- contentVideo.style.height = savedContentVideoStyles.height;
1387
- savedContentVideoStyles = void 0;
1388
- }
1389
- function handleAdComplete() {
1390
- if (tornDown) return;
1391
- clearAdStallTimer();
1392
- if (debug) console.log("".concat(LOG, " Handling ad completion"));
1393
- adPlaying = false;
1394
- setAdPlayingFlag(false);
1395
- restoreContentVideoStyles();
1396
- if (adContainerEl) {
1397
- adContainerEl.style.display = "none";
1398
- adContainerEl.style.pointerEvents = "none";
1399
- }
1400
- emit("ad_impression");
1401
- emit("content_resume");
1402
- }
1403
- function handleAdError() {
1404
- if (tornDown) return;
1405
- if (!adPlaying) return;
1406
- clearAdStallTimer();
1407
- if (debug) console.log("".concat(LOG, " Handling ad error"));
1408
- adPlaying = false;
1409
- setAdPlayingFlag(false);
1410
- restoreContentVideoStyles();
1411
- if (adContainerEl) {
1412
- adContainerEl.style.display = "none";
1413
- adContainerEl.style.pointerEvents = "none";
1414
- }
1415
- emit("ad_error");
1416
- }
1417
- function teardownCurrentPlayback() {
1418
- removeAdEventListeners();
1419
- if (adHls) {
1420
- adHls.destroy();
1421
- adHls = void 0;
1422
- }
1423
- if (adVideoElement) {
1424
- if (singleElementMode && adVideoElement === contentVideo) {
1425
- contentVideo.pause();
1426
- } else {
1427
- adVideoElement.pause();
1428
- adVideoElement.removeAttribute("src");
1429
- adVideoElement.load();
1430
- }
1431
- }
1432
- }
1433
- function startNativePlayback(mediaFile) {
1434
- if (!adVideoElement) return;
1435
- if (debug) console.log("".concat(LOG, " Starting native MP4 playback: ").concat(mediaFile.url));
1436
- adVideoElement.src = mediaFile.url;
1437
- adVideoElement.load();
1438
- adVideoElement.play().catch(function(error) {
1439
- console.error("".concat(LOG, " Error starting native ad playback:"), error);
1440
- handleAdError();
1441
- });
1442
- }
1443
- function startHlsPlayback(mediaFile) {
1444
- if (!adVideoElement) return;
1445
- if (debug) console.log("".concat(LOG, " Starting HLS playback: ").concat(mediaFile.url));
1446
- if (import_hls.default.isSupported()) {
1447
- if (adHls) {
1448
- adHls.destroy();
1449
- adHls = void 0;
1450
- }
1451
- adHls = new import_hls.default({
1452
- enableWorker: true,
1453
- lowLatencyMode: false
1454
- });
1455
- adHls.loadSource(mediaFile.url);
1456
- adHls.attachMedia(adVideoElement);
1457
- adHls.on(import_hls.default.Events.MANIFEST_PARSED, function() {
1458
- if (!adPlaying) return;
1459
- adVideoElement.play().catch(function(error) {
1460
- console.error("".concat(LOG, " Error starting HLS ad playback:"), error);
1461
- handleAdError();
1462
- });
1463
- });
1464
- var nonFatalNetworkErrors = 0;
1465
- adHls.on(import_hls.default.Events.ERROR, function(_event, data) {
1466
- if (data.fatal) {
1467
- handleAdError();
1468
- } else if (data.type === import_hls.default.ErrorTypes.NETWORK_ERROR) {
1469
- nonFatalNetworkErrors++;
1470
- if (nonFatalNetworkErrors >= 3) {
1471
- if (debug) console.warn("".concat(LOG, " Too many non-fatal HLS network errors (").concat(nonFatalNetworkErrors, "), treating as fatal"));
1472
- handleAdError();
1473
- }
1474
- }
1475
- });
1476
- } else if (adVideoElement.canPlayType("application/vnd.apple.mpegurl")) {
1477
- adVideoElement.src = mediaFile.url;
1478
- adVideoElement.play().catch(function(error) {
1479
- console.error("".concat(LOG, " Error starting native HLS ad playback:"), error);
1480
- handleAdError();
1481
- });
1482
- } else {
1483
- console.error("".concat(LOG, " HLS not supported on this platform"));
1484
- handleAdError();
1485
- }
1486
- }
1487
- function startPlayback(mediaFile) {
1488
- if (!adVideoElement) return;
1489
- if (singleElementMode && isHlsMediaFile(mediaFile)) {
1490
- var mp4Fallback = currentAd === null || currentAd === void 0 ? void 0 : currentAd.mediaFiles.find(function(f) {
1491
- return !isHlsMediaFile(f);
1492
- });
1493
- if (mp4Fallback) {
1494
- if (debug) console.log("".concat(LOG, " singleElementMode: HLS ad blocked, using MP4 fallback"));
1495
- startNativePlayback(mp4Fallback);
1496
- return;
1497
- }
1498
- }
1499
- if (isHlsMediaFile(mediaFile)) {
1500
- startHlsPlayback(mediaFile);
1501
- } else {
1502
- startNativePlayback(mediaFile);
1503
- }
1504
- }
1505
- function playAd(bids) {
1324
+ function requestAdFromApi(requestContext) {
1506
1325
  return _async_to_generator(function() {
1507
- var winner, ad, contentVolume, adVolume2, mediaFile2, _contentVideo_parentElement, container, adVolume, mediaFile;
1326
+ var durationSeconds, ads;
1508
1327
  return _ts_generator(this, function(_state) {
1509
1328
  switch(_state.label){
1510
1329
  case 0:
1511
- if (destroyed) {
1512
- return [
1513
- 2,
1514
- Promise.reject(new Error("Layer has been destroyed"))
1515
- ];
1516
- }
1517
- if (bids.length === 0) {
1518
- return [
1519
- 2,
1520
- Promise.reject(new Error("No bids provided"))
1521
- ];
1522
- }
1523
- winner = bids[0];
1524
- if (debug) {
1525
- console.log("".concat(LOG, " Winning bid: ").concat(winner.bidder, " $").concat(winner.cpm.toFixed(2), " ").concat(winner.currency));
1526
- }
1330
+ durationSeconds = getDurationSecondsFromContext(requestContext);
1527
1331
  return [
1528
1332
  4,
1529
- resolveBidToVastAd(winner, LOG)
1333
+ fetchVast(durationSeconds)
1530
1334
  ];
1531
1335
  case 1:
1532
- ad = _state.sent();
1533
- if (!ad) {
1534
- if (debug) console.warn("".concat(LOG, " Winning bid has no VAST URL or XML"));
1535
- emit("ad_error");
1536
- return [
1537
- 2,
1538
- Promise.reject(new Error("No VAST from bid"))
1539
- ];
1540
- }
1541
- if (debug) {
1542
- console.log("".concat(LOG, " Ad parsed: ").concat(ad.title, ", duration: ").concat(ad.duration, "s, mediaFiles: ").concat(ad.mediaFiles.length));
1543
- }
1544
- sessionId = generateSessionId();
1545
- currentAd = ad;
1546
- trackingFired = _object_spread({}, createEmptyTrackingState());
1547
- fireTrackingPixels2(ad.trackingUrls.impression);
1548
- trackingFired.impression = true;
1549
- contentVolume = contentVideo.volume;
1550
- originalVolume = Math.max(0, Math.min(1, contentVolume || originalVolume));
1551
- if (!singleElementMode) return [
1552
- 3,
1553
- 3
1554
- ];
1555
- mainHlsInstance === null || mainHlsInstance === void 0 ? void 0 : mainHlsInstance.detachMedia();
1556
- teardownCurrentPlayback();
1557
- adVideoElement = contentVideo;
1558
- adHls = void 0;
1559
- adPlaying = true;
1560
- setAdPlayingFlag(true);
1561
- contentVideo.removeAttribute("src");
1562
- contentVideo.load();
1563
- if (!continueLiveStreamDuringAds) {
1564
- contentVideo.pause();
1565
- }
1566
- contentVideo.muted = true;
1567
- contentVideo.volume = 0;
1568
- return [
1569
- 4,
1570
- new Promise(function(resolve) {
1571
- return setTimeout(resolve, 200);
1572
- })
1573
- ];
1574
- case 2:
1575
- _state.sent();
1576
- if (destroyed || tornDown) return [
1577
- 2
1578
- ];
1579
- contentVideo.style.visibility = "visible";
1580
- contentVideo.style.opacity = "1";
1581
- applyContentVideoAdCoverStyles();
1582
- emit("content_pause");
1583
- setupAdEventListeners();
1584
- adVolume2 = originalMutedState ? 1 : originalVolume;
1585
- adVideoElement.volume = Math.max(0, Math.min(1, adVolume2));
1586
- adVideoElement.muted = false;
1587
- mediaFile2 = selectBestMediaFile(ad.mediaFiles);
1588
- if (debug) console.log("".concat(LOG, " Loading ad from: ").concat(mediaFile2.url));
1589
- startPlayback(mediaFile2);
1590
- return [
1591
- 2
1592
- ];
1593
- case 3:
1594
- if (!adContainerEl) {
1595
- ;
1596
- container = document.createElement("div");
1597
- container.style.position = "absolute";
1598
- container.style.left = "0";
1599
- container.style.top = "0";
1600
- container.style.right = "0";
1601
- container.style.bottom = "0";
1602
- container.style.display = "none";
1603
- container.style.alignItems = "center";
1604
- container.style.justifyContent = "center";
1605
- container.style.pointerEvents = "none";
1606
- container.style.zIndex = "10";
1607
- container.style.backgroundColor = "#000";
1608
- (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
1609
- adContainerEl = container;
1610
- }
1611
- if (!adVideoElement) {
1612
- adVideoElement = createAdVideoElement();
1613
- adContainerEl.appendChild(adVideoElement);
1614
- setupAdEventListeners();
1615
- } else {
1616
- teardownCurrentPlayback();
1617
- }
1618
- if (!continueLiveStreamDuringAds) {
1619
- contentVideo.pause();
1620
- }
1621
- contentVideo.muted = true;
1622
- contentVideo.volume = 0;
1623
- adPlaying = true;
1624
- setAdPlayingFlag(true);
1625
- adVolume = originalMutedState ? 1 : originalVolume;
1626
- adVideoElement.volume = Math.max(0, Math.min(1, adVolume));
1627
- adVideoElement.muted = false;
1628
- if (adContainerEl) {
1629
- adContainerEl.style.display = "flex";
1630
- adContainerEl.style.pointerEvents = "auto";
1631
- }
1632
- emit("content_pause");
1633
- mediaFile = selectBestMediaFile(ad.mediaFiles);
1634
- if (debug) console.log("".concat(LOG, " Loading ad from: ").concat(mediaFile.url));
1635
- startPlayback(mediaFile);
1336
+ ads = _state.sent();
1636
1337
  return [
1637
- 2
1338
+ 2,
1339
+ ads[0] || null
1638
1340
  ];
1639
1341
  }
1640
1342
  });
1641
1343
  })();
1642
1344
  }
1643
- function ensureAdContainer() {
1644
- if (!adContainerEl) {
1645
- var _contentVideo_parentElement;
1646
- var container = document.createElement("div");
1647
- container.style.position = "absolute";
1648
- container.style.left = "0";
1649
- container.style.top = "0";
1650
- container.style.right = "0";
1651
- container.style.bottom = "0";
1652
- container.style.display = "none";
1653
- container.style.alignItems = "center";
1654
- container.style.justifyContent = "center";
1655
- container.style.pointerEvents = "none";
1656
- container.style.zIndex = "10";
1657
- container.style.backgroundColor = "#000";
1658
- (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
1659
- adContainerEl = container;
1660
- }
1661
- return adContainerEl;
1345
+ function assignCurrentAd(ad) {
1346
+ currentAd = ad;
1347
+ sessionId = generateSessionId();
1348
+ trackingFired = {
1349
+ impression: false,
1350
+ start: false,
1351
+ firstQuartile: false,
1352
+ midpoint: false,
1353
+ thirdQuartile: false,
1354
+ complete: false
1355
+ };
1356
+ fireTrackingPixels2(currentAd.trackingUrls.impression);
1357
+ trackingFired.impression = true;
1358
+ emit("ad_impression");
1662
1359
  }
1663
- function preloadAd(bids, token) {
1664
- return _async_to_generator(function() {
1665
- var winner, ad, mediaFile, slot, videoEl, container, hls, slot1, slot2;
1666
- return _ts_generator(this, function(_state) {
1667
- switch(_state.label){
1668
- case 0:
1669
- if (destroyed) return [
1670
- 2
1671
- ];
1672
- winner = bids[0];
1673
- if (!winner) return [
1674
- 2
1675
- ];
1676
- if (debug) console.log("".concat(LOG, " [preload] Resolving VAST for token=").concat(token));
1677
- return [
1678
- 4,
1679
- resolveBidToVastAd(winner, LOG)
1680
- ];
1681
- case 1:
1682
- ad = _state.sent();
1683
- if (!ad || destroyed) return [
1684
- 2
1685
- ];
1686
- mediaFile = selectBestMediaFile(ad.mediaFiles);
1687
- if (!mediaFile) return [
1688
- 2
1689
- ];
1690
- if (smartTVMode || singleElementMode) {
1691
- slot = {
1692
- bids: bids,
1693
- ad: ad,
1694
- mediaFile: mediaFile,
1695
- videoEl: null,
1696
- ready: true
1697
- };
1698
- preloadSlots.set(token, slot);
1699
- if (debug) console.log("".concat(LOG, " [preload] Metadata-only preload (smartTV/singleElement), token=").concat(token, ", url=").concat(mediaFile.url));
1360
+ return {
1361
+ initialize: function initialize() {
1362
+ log("Initializing");
1363
+ if (!adContainerEl) {
1364
+ var _contentVideo_parentElement;
1365
+ var parent = contentVideo.parentElement;
1366
+ if (parent) {
1367
+ var computed = window.getComputedStyle(parent).position;
1368
+ if (computed === "static") {
1369
+ parent.style.position = "relative";
1370
+ parentPositionOverridden = true;
1371
+ }
1372
+ }
1373
+ var container = document.createElement("div");
1374
+ container.style.position = "absolute";
1375
+ container.style.left = "0";
1376
+ container.style.top = "0";
1377
+ container.style.right = "0";
1378
+ container.style.bottom = "0";
1379
+ container.style.display = "none";
1380
+ container.style.alignItems = "center";
1381
+ container.style.justifyContent = "center";
1382
+ container.style.pointerEvents = "none";
1383
+ container.style.zIndex = AD_LAYER_Z_INDEX;
1384
+ container.style.backgroundColor = "#000";
1385
+ container.style.transition = "opacity 0.3s ease-in-out";
1386
+ container.style.opacity = "0";
1387
+ container.style.isolation = "isolate";
1388
+ var countdown = document.createElement("div");
1389
+ countdown.style.position = "absolute";
1390
+ countdown.style.right = "12px";
1391
+ countdown.style.top = "12px";
1392
+ countdown.style.padding = "4px 8px";
1393
+ countdown.style.borderRadius = "4px";
1394
+ countdown.style.background = "rgba(0,0,0,0.75)";
1395
+ countdown.style.color = "#fff";
1396
+ countdown.style.fontFamily = "sans-serif";
1397
+ countdown.style.fontSize = "12px";
1398
+ countdown.style.lineHeight = "1.2";
1399
+ countdown.style.pointerEvents = "none";
1400
+ countdown.style.zIndex = COUNTDOWN_Z_INDEX;
1401
+ countdown.textContent = "Ad";
1402
+ container.appendChild(countdown);
1403
+ (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
1404
+ adContainerEl = container;
1405
+ adCountdownEl = countdown;
1406
+ }
1407
+ },
1408
+ requestAds: function requestAds(duration) {
1409
+ return _async_to_generator(function() {
1410
+ var durationSeconds, parsed, ads, error;
1411
+ return _ts_generator(this, function(_state) {
1412
+ switch(_state.label){
1413
+ case 0:
1414
+ log("Requesting ads for duration:", duration);
1415
+ if (adPlaying) {
1416
+ return [
1417
+ 2,
1418
+ Promise.reject(new Error("Ad already playing"))
1419
+ ];
1420
+ }
1421
+ if (destroyed) {
1422
+ return [
1423
+ 2,
1424
+ Promise.reject(new Error("Player has been destroyed"))
1425
+ ];
1426
+ }
1427
+ _state.label = 1;
1428
+ case 1:
1429
+ _state.trys.push([
1430
+ 1,
1431
+ 3,
1432
+ ,
1433
+ 4
1434
+ ]);
1435
+ tornDown = false;
1436
+ durationSeconds = 30;
1437
+ parsed = parseInt(duration || "", 10);
1438
+ if (!isNaN(parsed) && parsed > 0) {
1439
+ durationSeconds = parsed;
1440
+ }
1700
1441
  return [
1701
- 2
1442
+ 4,
1443
+ fetchVast(durationSeconds)
1702
1444
  ];
1703
- }
1704
- videoEl = createAdVideoElement();
1705
- videoEl.style.visibility = "hidden";
1706
- videoEl.style.pointerEvents = "none";
1707
- videoEl.preload = "auto";
1708
- container = ensureAdContainer();
1709
- container.appendChild(videoEl);
1710
- if (isHlsMediaFile(mediaFile) && import_hls.default.isSupported()) {
1711
- hls = new import_hls.default({
1712
- enableWorker: true,
1713
- lowLatencyMode: false
1714
- });
1715
- hls.loadSource(mediaFile.url);
1716
- hls.attachMedia(videoEl);
1717
- slot1 = {
1718
- bids: bids,
1719
- ad: ad,
1720
- mediaFile: mediaFile,
1721
- videoEl: videoEl,
1722
- hlsInstance: hls,
1723
- ready: false
1724
- };
1725
- preloadSlots.set(token, slot1);
1726
- hls.on(import_hls.default.Events.MANIFEST_PARSED, function() {
1727
- var s = preloadSlots.get(token);
1728
- if (s) s.ready = true;
1729
- if (debug) console.log("".concat(LOG, " [preload] HLS manifest parsed, token=").concat(token));
1730
- });
1731
- hls.on(import_hls.default.Events.ERROR, function(_evt, data) {
1732
- if (!preloadSlots.has(token)) return;
1733
- if (data.fatal) {
1734
- if (debug) console.warn("".concat(LOG, " [preload] HLS error for token=").concat(token));
1735
- preloadSlots.delete(token);
1736
- hls.destroy();
1737
- videoEl.remove();
1738
- }
1739
- });
1740
- } else {
1741
- videoEl.src = mediaFile.url;
1742
- videoEl.load();
1743
- slot2 = {
1744
- bids: bids,
1745
- ad: ad,
1746
- mediaFile: mediaFile,
1747
- videoEl: videoEl,
1748
- ready: false
1749
- };
1750
- preloadSlots.set(token, slot2);
1751
- videoEl.addEventListener("canplay", function() {
1752
- var s = preloadSlots.get(token);
1753
- if (s) s.ready = true;
1754
- if (debug) console.log("".concat(LOG, " [preload] canplay fired, token=").concat(token));
1755
- }, {
1756
- once: true
1757
- });
1758
- }
1759
- if (debug) console.log("".concat(LOG, " [preload] Started buffering token=").concat(token, ", url=").concat(mediaFile.url));
1760
- return [
1761
- 2
1762
- ];
1763
- }
1764
- });
1765
- })();
1766
- }
1767
- function playPreloaded(token) {
1768
- return _async_to_generator(function() {
1769
- var slot, contentVolume, adVolume2, videoEl, container2, adVolume21, nonFatalNetworkErrors, adVolume, container;
1770
- return _ts_generator(this, function(_state) {
1771
- switch(_state.label){
1772
- case 0:
1773
- if (destroyed) return [
1774
- 2,
1775
- Promise.reject(new Error("Layer has been destroyed"))
1776
- ];
1777
- slot = preloadSlots.get(token);
1778
- if (!slot) {
1779
- if (debug) console.warn("".concat(LOG, " [preload] No slot found for token=").concat(token, ", nothing to play"));
1445
+ case 2:
1446
+ ads = _state.sent();
1447
+ if (ads.length === 0) {
1448
+ log("No ads available from VAST response");
1449
+ emit("ad_error");
1450
+ return [
1451
+ 2,
1452
+ Promise.resolve()
1453
+ ];
1454
+ }
1455
+ assignCurrentAd(ads[0]);
1456
+ log("Ad loaded: ".concat(currentAd.title, ", duration: ").concat(currentAd.duration, "s"));
1457
+ return [
1458
+ 2,
1459
+ Promise.resolve()
1460
+ ];
1461
+ case 3:
1462
+ error = _state.sent();
1463
+ console.error("[AdStormPlayer] Error requesting ads:", error);
1464
+ emit("ad_error");
1465
+ return [
1466
+ 2,
1467
+ Promise.reject(error)
1468
+ ];
1469
+ case 4:
1780
1470
  return [
1781
1471
  2
1782
1472
  ];
1783
- }
1784
- preloadSlots.delete(token);
1785
- if (debug) console.log("".concat(LOG, " [preload] Playing preloaded ad, token=").concat(token, ", ready=").concat(slot.ready));
1786
- contentVolume = contentVideo.volume;
1787
- originalVolume = Math.max(0, Math.min(1, contentVolume || originalVolume));
1788
- sessionId = generateSessionId();
1789
- currentAd = slot.ad;
1790
- trackingFired = _object_spread({}, createEmptyTrackingState());
1791
- fireTrackingPixels2(slot.ad.trackingUrls.impression);
1792
- trackingFired.impression = true;
1793
- if (!singleElementMode) return [
1794
- 3,
1795
- 2
1796
- ];
1797
- mainHlsInstance === null || mainHlsInstance === void 0 ? void 0 : mainHlsInstance.detachMedia();
1798
- teardownCurrentPlayback();
1799
- adVideoElement = contentVideo;
1800
- adHls = void 0;
1801
- adPlaying = true;
1802
- setAdPlayingFlag(true);
1803
- contentVideo.removeAttribute("src");
1804
- contentVideo.load();
1805
- contentVideo.muted = true;
1806
- contentVideo.volume = 0;
1807
- return [
1808
- 4,
1809
- new Promise(function(resolve) {
1810
- return setTimeout(resolve, 200);
1811
- })
1812
- ];
1813
- case 1:
1814
- _state.sent();
1815
- if (destroyed || tornDown) return [
1816
- 2
1817
- ];
1818
- contentVideo.style.visibility = "visible";
1819
- contentVideo.style.opacity = "1";
1820
- applyContentVideoAdCoverStyles();
1821
- emit("content_pause");
1822
- setupAdEventListeners();
1823
- adVolume2 = originalMutedState ? 1 : originalVolume;
1824
- contentVideo.volume = Math.max(0, Math.min(1, adVolume2));
1825
- contentVideo.muted = false;
1826
- if (debug) console.log("".concat(LOG, " [preload] singleElementMode: attaching ad to contentVideo, url=").concat(slot.mediaFile.url));
1827
- startPlayback(slot.mediaFile);
1828
- return [
1829
- 2
1830
- ];
1831
- case 2:
1832
- if (smartTVMode && !slot.videoEl) {
1833
- teardownCurrentPlayback();
1834
- if (adVideoElement) {
1835
- adVideoElement.remove();
1836
- adVideoElement = void 0;
1473
+ }
1474
+ });
1475
+ })();
1476
+ },
1477
+ play: function play() {
1478
+ return _async_to_generator(function() {
1479
+ var mediaFile, error;
1480
+ return _ts_generator(this, function(_state) {
1481
+ switch(_state.label){
1482
+ case 0:
1483
+ if (!currentAd) {
1484
+ return [
1485
+ 2,
1486
+ Promise.reject(new Error("No ad loaded"))
1487
+ ];
1488
+ }
1489
+ if (destroyed) {
1490
+ return [
1491
+ 2,
1492
+ Promise.reject(new Error("Player has been destroyed"))
1493
+ ];
1494
+ }
1495
+ log("Starting ad playback");
1496
+ _state.label = 1;
1497
+ case 1:
1498
+ _state.trys.push([
1499
+ 1,
1500
+ 3,
1501
+ ,
1502
+ 4
1503
+ ]);
1504
+ tornDown = false;
1505
+ if (adHideTimerId) {
1506
+ clearTimeout(adHideTimerId);
1507
+ adHideTimerId = void 0;
1508
+ }
1509
+ if (!adVideoElement) {
1510
+ adVideoElement = createAdVideoElement();
1511
+ adContainerEl === null || adContainerEl === void 0 ? void 0 : adContainerEl.appendChild(adVideoElement);
1512
+ } else {
1513
+ teardownCurrentPlayback();
1837
1514
  }
1838
- videoEl = createAdVideoElement();
1839
- videoEl.style.visibility = "visible";
1840
- videoEl.style.pointerEvents = "none";
1841
- container2 = ensureAdContainer();
1842
- container2.appendChild(videoEl);
1843
- adVideoElement = videoEl;
1844
1515
  setupAdEventListeners();
1516
+ trackingFired = {
1517
+ impression: trackingFired.impression,
1518
+ start: false,
1519
+ firstQuartile: false,
1520
+ midpoint: false,
1521
+ thirdQuartile: false,
1522
+ complete: false
1523
+ };
1524
+ contentVideo.style.transition = "opacity 0.3s ease-in-out";
1525
+ contentVideo.style.opacity = "0";
1526
+ setTimeout(function() {
1527
+ contentVideo.style.visibility = "hidden";
1528
+ }, 300);
1529
+ contentVideo.muted = true;
1530
+ contentVideo.volume = 0;
1845
1531
  if (!continueLiveStreamDuringAds) {
1846
1532
  contentVideo.pause();
1847
1533
  }
1848
- contentVideo.muted = true;
1849
- contentVideo.volume = 0;
1850
1534
  adPlaying = true;
1851
1535
  setAdPlayingFlag(true);
1852
- adVolume21 = originalMutedState ? 1 : originalVolume;
1853
- adVideoElement.volume = Math.max(0, Math.min(1, adVolume21));
1854
- adVideoElement.muted = false;
1855
- container2.style.display = "flex";
1856
- container2.style.pointerEvents = "auto";
1536
+ if (adVideoElement) {
1537
+ adVideoElement.volume = originalMutedState ? 0 : originalVolume;
1538
+ adVideoElement.muted = originalMutedState;
1539
+ }
1540
+ if (adContainerEl) {
1541
+ adContainerEl.style.display = "flex";
1542
+ adContainerEl.style.pointerEvents = "auto";
1543
+ adContainerEl.offsetHeight;
1544
+ adContainerEl.style.opacity = "1";
1545
+ }
1857
1546
  emit("content_pause");
1858
- if (debug) console.log("".concat(LOG, " [preload] smartTVMode deferred: creating video element and loading, url=").concat(slot.mediaFile.url));
1859
- startPlayback(slot.mediaFile);
1547
+ mediaFile = selectBestMediaFile(currentAd.mediaFiles);
1548
+ if (!mediaFile) {
1549
+ throw new Error("No media file available");
1550
+ }
1551
+ log("Playing media file:", mediaFile.url);
1552
+ adVideoElement.src = mediaFile.url;
1553
+ adVideoElement.load();
1860
1554
  return [
1861
- 2
1555
+ 4,
1556
+ adVideoElement.play()
1862
1557
  ];
1863
- }
1864
- teardownCurrentPlayback();
1865
- if (adVideoElement && adVideoElement !== slot.videoEl) {
1866
- adVideoElement.remove();
1867
- }
1868
- slot.videoEl.style.visibility = "visible";
1869
- slot.videoEl.style.pointerEvents = "none";
1870
- adVideoElement = slot.videoEl;
1871
- adHls = slot.hlsInstance;
1872
- if (adHls) {
1873
- nonFatalNetworkErrors = 0;
1874
- adHls.on(import_hls.default.Events.ERROR, function(_event, data) {
1875
- if (!adPlaying) return;
1876
- if (data.fatal) {
1877
- handleAdError();
1878
- } else if (data.type === import_hls.default.ErrorTypes.NETWORK_ERROR) {
1879
- nonFatalNetworkErrors++;
1880
- if (nonFatalNetworkErrors >= 3) {
1881
- if (debug) console.warn("".concat(LOG, " [preload] Too many non-fatal HLS network errors during playback, treating as fatal"));
1882
- handleAdError();
1883
- }
1884
- }
1885
- });
1886
- }
1887
- setupAdEventListeners();
1888
- if (!continueLiveStreamDuringAds) {
1889
- contentVideo.pause();
1890
- }
1891
- contentVideo.muted = true;
1892
- contentVideo.volume = 0;
1893
- adPlaying = true;
1894
- setAdPlayingFlag(true);
1895
- adVolume = originalMutedState ? 1 : originalVolume;
1896
- adVideoElement.volume = Math.max(0, Math.min(1, adVolume));
1897
- adVideoElement.muted = false;
1898
- container = ensureAdContainer();
1899
- container.style.display = "flex";
1900
- container.style.pointerEvents = "auto";
1901
- emit("content_pause");
1902
- adVideoElement.play().catch(function(error) {
1903
- console.error("".concat(LOG, " [preload] Error playing preloaded ad:"), error);
1558
+ case 2:
1559
+ _state.sent();
1560
+ return [
1561
+ 2,
1562
+ Promise.resolve()
1563
+ ];
1564
+ case 3:
1565
+ error = _state.sent();
1566
+ console.error("[AdStormPlayer] Error playing ad:", error);
1904
1567
  handleAdError();
1905
- });
1906
- return [
1907
- 2
1908
- ];
1909
- }
1910
- });
1911
- })();
1912
- }
1913
- function cancelPreload(token) {
1914
- var slot = preloadSlots.get(token);
1915
- if (!slot) return;
1916
- preloadSlots.delete(token);
1917
- if (slot.hlsInstance) {
1918
- slot.hlsInstance.destroy();
1919
- }
1920
- if (slot.videoEl) {
1921
- slot.videoEl.pause();
1922
- slot.videoEl.removeAttribute("src");
1923
- slot.videoEl.load();
1924
- slot.videoEl.remove();
1925
- }
1926
- if (debug) console.log("".concat(LOG, " [preload] Cancelled and cleaned up token=").concat(token));
1927
- }
1928
- return {
1929
- initialize: function initialize() {
1930
- if (debug) console.log("".concat(LOG, " Initializing"));
1931
- },
1932
- updateOptions: function updateOptions(opts) {
1933
- if (opts.continueLiveStreamDuringAds !== void 0) {
1934
- continueLiveStreamDuringAds = opts.continueLiveStreamDuringAds;
1935
- }
1936
- if (opts.mainHlsInstance !== void 0) {
1937
- var _opts_mainHlsInstance;
1938
- mainHlsInstance = (_opts_mainHlsInstance = opts.mainHlsInstance) !== null && _opts_mainHlsInstance !== void 0 ? _opts_mainHlsInstance : void 0;
1939
- }
1940
- },
1941
- playAd: playAd,
1942
- preloadAd: preloadAd,
1943
- playPreloaded: playPreloaded,
1944
- hasPreloaded: function hasPreloaded(token) {
1945
- return preloadSlots.has(token);
1946
- },
1947
- cancelPreload: cancelPreload,
1948
- pause: function pause() {
1949
- if (!adPlaying || !adVideoElement) return;
1950
- try {
1951
- if (!adVideoElement.paused) adVideoElement.pause();
1952
- } catch (error) {
1953
- if (debug) console.warn("".concat(LOG, " Error pausing ad:"), error);
1954
- }
1955
- },
1956
- resume: function resume() {
1957
- if (!adPlaying || !adVideoElement) return;
1958
- try {
1959
- if (adVideoElement.paused) adVideoElement.play().catch(function() {});
1960
- } catch (error) {
1961
- if (debug) console.warn("".concat(LOG, " Error resuming ad:"), error);
1962
- }
1568
+ return [
1569
+ 2,
1570
+ Promise.reject(error)
1571
+ ];
1572
+ case 4:
1573
+ return [
1574
+ 2
1575
+ ];
1576
+ }
1577
+ });
1578
+ })();
1963
1579
  },
1964
1580
  stop: function stop() {
1965
1581
  return _async_to_generator(function() {
1966
1582
  return _ts_generator(this, function(_state) {
1583
+ log("Stopping ad");
1967
1584
  tornDown = true;
1968
- if (debug) console.log("".concat(LOG, " Stopping ad"));
1969
1585
  adPlaying = false;
1970
1586
  setAdPlayingFlag(false);
1971
- restoreContentVideoStyles();
1972
- contentVideo.muted = originalMutedState;
1973
- contentVideo.volume = originalMutedState ? 0 : originalVolume;
1587
+ clearAdStallTimer();
1588
+ clearAdCountdownTimer();
1589
+ if (adContainerEl) {
1590
+ adContainerEl.style.opacity = "0";
1591
+ adHideTimerId = setTimeout(function() {
1592
+ if (adContainerEl) {
1593
+ adContainerEl.style.display = "none";
1594
+ adContainerEl.style.pointerEvents = "none";
1595
+ }
1596
+ }, 300);
1597
+ }
1598
+ teardownCurrentPlayback();
1974
1599
  contentVideo.style.visibility = "visible";
1975
1600
  contentVideo.style.opacity = "1";
1976
- if (singleElementMode) {
1977
- teardownCurrentPlayback();
1978
- contentVideo.removeAttribute("src");
1979
- contentVideo.load();
1980
- adVideoElement = void 0;
1981
- } else {
1982
- if (adContainerEl) {
1983
- adContainerEl.style.display = "none";
1984
- adContainerEl.style.pointerEvents = "none";
1985
- }
1986
- if (continueLiveStreamDuringAds) {
1987
- contentVideo.play().catch(function() {});
1988
- }
1989
- teardownCurrentPlayback();
1990
- if (adVideoElement) {
1991
- adVideoElement.pause();
1992
- adVideoElement.removeAttribute("src");
1993
- adVideoElement.load();
1994
- }
1995
- }
1601
+ contentVideo.muted = originalMutedState;
1602
+ contentVideo.volume = originalVolume;
1996
1603
  currentAd = void 0;
1997
1604
  tornDown = false;
1998
1605
  return [
1999
- 2
1606
+ 2,
1607
+ Promise.resolve()
2000
1608
  ];
2001
1609
  });
2002
1610
  })();
2003
1611
  },
1612
+ pause: function pause() {
1613
+ if (!adPlaying || !adVideoElement) return;
1614
+ try {
1615
+ if (!adVideoElement.paused) adVideoElement.pause();
1616
+ } catch (error) {
1617
+ console.warn("[AdStormPlayer] Error pausing ad:", error);
1618
+ }
1619
+ },
1620
+ resume: function resume() {
1621
+ if (!adPlaying || !adVideoElement) return;
1622
+ try {
1623
+ if (adVideoElement.paused) adVideoElement.play().catch(function() {});
1624
+ } catch (error) {
1625
+ console.warn("[AdStormPlayer] Error resuming ad:", error);
1626
+ }
1627
+ },
2004
1628
  destroy: function destroy() {
2005
- tornDown = true;
2006
- if (debug) console.log("".concat(LOG, " Destroying"));
1629
+ log("Destroying");
2007
1630
  destroyed = true;
1631
+ tornDown = true;
2008
1632
  adPlaying = false;
2009
1633
  setAdPlayingFlag(false);
2010
- restoreContentVideoStyles();
1634
+ clearAdStallTimer();
1635
+ clearAdCountdownTimer();
1636
+ if (adHideTimerId) {
1637
+ clearTimeout(adHideTimerId);
1638
+ adHideTimerId = void 0;
1639
+ }
2011
1640
  contentVideo.muted = originalMutedState;
2012
1641
  contentVideo.volume = originalVolume;
2013
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2014
- try {
2015
- for(var _iterator = Array.from(preloadSlots.entries())[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2016
- var _step_value = _sliced_to_array(_step.value, 1), token = _step_value[0];
2017
- cancelPreload(token);
2018
- }
2019
- } catch (err) {
2020
- _didIteratorError = true;
2021
- _iteratorError = err;
2022
- } finally{
2023
- try {
2024
- if (!_iteratorNormalCompletion && _iterator.return != null) {
2025
- _iterator.return();
2026
- }
2027
- } finally{
2028
- if (_didIteratorError) {
2029
- throw _iteratorError;
2030
- }
2031
- }
2032
- }
1642
+ contentVideo.style.visibility = "visible";
1643
+ contentVideo.style.opacity = "1";
2033
1644
  teardownCurrentPlayback();
2034
- if (adVideoElement) {
2035
- if (singleElementMode && adVideoElement === contentVideo) {
2036
- contentVideo.removeAttribute("src");
2037
- contentVideo.load();
2038
- } else {
2039
- adVideoElement.pause();
2040
- adVideoElement.removeAttribute("src");
2041
- adVideoElement.remove();
2042
- }
2043
- adVideoElement = void 0;
2044
- }
1645
+ adVideoElement === null || adVideoElement === void 0 ? void 0 : adVideoElement.remove();
1646
+ adVideoElement = void 0;
2045
1647
  if (adContainerEl === null || adContainerEl === void 0 ? void 0 : adContainerEl.parentElement) {
2046
1648
  adContainerEl.parentElement.removeChild(adContainerEl);
2047
1649
  }
2048
1650
  adContainerEl = void 0;
1651
+ adCountdownEl = void 0;
2049
1652
  currentAd = void 0;
1653
+ sessionId = void 0;
1654
+ preloadSlots.clear();
2050
1655
  listeners.clear();
1656
+ if (parentPositionOverridden && contentVideo.parentElement) {
1657
+ contentVideo.parentElement.style.position = "";
1658
+ parentPositionOverridden = false;
1659
+ }
1660
+ },
1661
+ updateOptions: function updateOptions(opts) {
1662
+ if (opts.continueLiveStreamDuringAds !== void 0) {
1663
+ continueLiveStreamDuringAds = opts.continueLiveStreamDuringAds;
1664
+ }
1665
+ },
1666
+ playAd: function playAd(requestContext) {
1667
+ return _async_to_generator(function() {
1668
+ var ad;
1669
+ return _ts_generator(this, function(_state) {
1670
+ switch(_state.label){
1671
+ case 0:
1672
+ if (destroyed) return [
1673
+ 2,
1674
+ Promise.reject(new Error("Player has been destroyed"))
1675
+ ];
1676
+ if (!!currentAd) return [
1677
+ 3,
1678
+ 2
1679
+ ];
1680
+ return [
1681
+ 4,
1682
+ requestAdFromApi(requestContext)
1683
+ ];
1684
+ case 1:
1685
+ ad = _state.sent();
1686
+ if (!ad) {
1687
+ emit("ad_error", {
1688
+ message: "No valid ad from AdStorm API"
1689
+ });
1690
+ return [
1691
+ 2,
1692
+ Promise.reject(new Error("No valid ad from AdStorm API"))
1693
+ ];
1694
+ }
1695
+ assignCurrentAd(ad);
1696
+ _state.label = 2;
1697
+ case 2:
1698
+ return [
1699
+ 2,
1700
+ this.play()
1701
+ ];
1702
+ }
1703
+ });
1704
+ }).call(this);
1705
+ },
1706
+ preloadAd: function preloadAd(arg1, arg2) {
1707
+ return _async_to_generator(function() {
1708
+ var token, requestContext, ad;
1709
+ return _ts_generator(this, function(_state) {
1710
+ switch(_state.label){
1711
+ case 0:
1712
+ if (destroyed) return [
1713
+ 2
1714
+ ];
1715
+ token = typeof arg1 === "string" ? arg1 : typeof arg2 === "string" ? arg2 : void 0;
1716
+ if (!token) return [
1717
+ 2
1718
+ ];
1719
+ requestContext = typeof arg1 === "string" ? arg2 : arg1;
1720
+ return [
1721
+ 4,
1722
+ requestAdFromApi(requestContext)
1723
+ ];
1724
+ case 1:
1725
+ ad = _state.sent();
1726
+ if (!ad) return [
1727
+ 2
1728
+ ];
1729
+ preloadSlots.set(token, {
1730
+ ad: ad
1731
+ });
1732
+ return [
1733
+ 2
1734
+ ];
1735
+ }
1736
+ });
1737
+ })();
1738
+ },
1739
+ playPreloaded: function playPreloaded(token) {
1740
+ return _async_to_generator(function() {
1741
+ var slot;
1742
+ return _ts_generator(this, function(_state) {
1743
+ if (destroyed) return [
1744
+ 2,
1745
+ Promise.reject(new Error("Player has been destroyed"))
1746
+ ];
1747
+ slot = preloadSlots.get(token);
1748
+ if (!slot) {
1749
+ return [
1750
+ 2,
1751
+ Promise.reject(new Error("No preloaded ad for token ".concat(token)))
1752
+ ];
1753
+ }
1754
+ preloadSlots.delete(token);
1755
+ assignCurrentAd(slot.ad);
1756
+ return [
1757
+ 2,
1758
+ this.play()
1759
+ ];
1760
+ });
1761
+ }).call(this);
1762
+ },
1763
+ hasPreloaded: function hasPreloaded(token) {
1764
+ return preloadSlots.has(token);
1765
+ },
1766
+ cancelPreload: function cancelPreload(token) {
1767
+ preloadSlots.delete(token);
2051
1768
  },
2052
1769
  isAdPlaying: function isAdPlaying() {
2053
1770
  return adPlaying;
2054
1771
  },
2055
1772
  resize: function resize(width, height) {
1773
+ log("Resizing to ".concat(width, "x").concat(height));
2056
1774
  if (adContainerEl) {
2057
1775
  adContainerEl.style.width = "".concat(width, "px");
2058
1776
  adContainerEl.style.height = "".concat(height, "px");
@@ -2072,6 +1790,7 @@ function createVastAdLayer(contentVideo, options) {
2072
1790
  },
2073
1791
  updateOriginalMutedState: function updateOriginalMutedState(muted, volume) {
2074
1792
  var nextVolume = typeof volume === "number" && !Number.isNaN(volume) ? Math.max(0, Math.min(1, volume)) : originalVolume;
1793
+ log("updateOriginalMutedState: muted=".concat(muted, ", volume=").concat(nextVolume));
2075
1794
  originalMutedState = muted;
2076
1795
  originalVolume = nextVolume;
2077
1796
  },
@@ -2084,6 +1803,7 @@ function createVastAdLayer(contentVideo, options) {
2084
1803
  setAdVolume: function setAdVolume(volume) {
2085
1804
  if (adVideoElement && adPlaying) {
2086
1805
  adVideoElement.volume = Math.max(0, Math.min(1, volume));
1806
+ adVideoElement.muted = volume === 0;
2087
1807
  }
2088
1808
  },
2089
1809
  getAdVolume: function getAdVolume() {
@@ -2093,11 +1813,16 @@ function createVastAdLayer(contentVideo, options) {
2093
1813
  return 1;
2094
1814
  },
2095
1815
  showPlaceholder: function showPlaceholder() {
2096
- if (singleElementMode) return;
2097
- contentVideo.style.opacity = "0";
2098
- contentVideo.style.visibility = "hidden";
2099
1816
  if (!adContainerEl) {
2100
1817
  var _contentVideo_parentElement;
1818
+ var parent = contentVideo.parentElement;
1819
+ if (parent) {
1820
+ var computed = window.getComputedStyle(parent).position;
1821
+ if (computed === "static") {
1822
+ parent.style.position = "relative";
1823
+ parentPositionOverridden = true;
1824
+ }
1825
+ }
2101
1826
  var container = document.createElement("div");
2102
1827
  container.style.position = "absolute";
2103
1828
  container.style.left = "0";
@@ -2108,24 +1833,43 @@ function createVastAdLayer(contentVideo, options) {
2108
1833
  container.style.alignItems = "center";
2109
1834
  container.style.justifyContent = "center";
2110
1835
  container.style.pointerEvents = "none";
2111
- container.style.zIndex = "10";
1836
+ container.style.zIndex = AD_LAYER_Z_INDEX;
2112
1837
  container.style.backgroundColor = "#000";
1838
+ container.style.isolation = "isolate";
1839
+ var countdown = document.createElement("div");
1840
+ countdown.style.position = "absolute";
1841
+ countdown.style.right = "12px";
1842
+ countdown.style.top = "12px";
1843
+ countdown.style.padding = "4px 8px";
1844
+ countdown.style.borderRadius = "4px";
1845
+ countdown.style.background = "rgba(0,0,0,0.75)";
1846
+ countdown.style.color = "#fff";
1847
+ countdown.style.fontFamily = "sans-serif";
1848
+ countdown.style.fontSize = "12px";
1849
+ countdown.style.lineHeight = "1.2";
1850
+ countdown.style.pointerEvents = "none";
1851
+ countdown.style.zIndex = COUNTDOWN_Z_INDEX;
1852
+ countdown.textContent = "Ad";
1853
+ container.appendChild(countdown);
2113
1854
  (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
2114
1855
  adContainerEl = container;
1856
+ adCountdownEl = countdown;
2115
1857
  }
2116
1858
  if (adContainerEl) {
2117
1859
  adContainerEl.style.display = "flex";
1860
+ adContainerEl.style.opacity = "1";
2118
1861
  adContainerEl.style.pointerEvents = "auto";
2119
1862
  }
2120
1863
  },
2121
1864
  hidePlaceholder: function hidePlaceholder() {
2122
1865
  if (adContainerEl) {
2123
- adContainerEl.style.display = "none";
2124
- adContainerEl.style.pointerEvents = "none";
2125
- }
2126
- if (!adPlaying) {
2127
- contentVideo.style.visibility = "visible";
2128
- contentVideo.style.opacity = "1";
1866
+ adContainerEl.style.opacity = "0";
1867
+ setTimeout(function() {
1868
+ if (adContainerEl) {
1869
+ adContainerEl.style.display = "none";
1870
+ adContainerEl.style.pointerEvents = "none";
1871
+ }
1872
+ }, 300);
2129
1873
  }
2130
1874
  }
2131
1875
  };
@@ -3253,6 +2997,9 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3253
2997
  this.backoffBaseMs = 1e3;
3254
2998
  this.maxBackoffMs = 15e3;
3255
2999
  this.MIN_AD_REMAINING_MS = 15e3;
3000
+ this.adRequestTimeoutMs = 5e3;
3001
+ this.adRequestMaxRetries = 3;
3002
+ this.adRequestRetryBackoffMs = 1500;
3256
3003
  this.preloadedTokens = [];
3257
3004
  initializePolyfills();
3258
3005
  var browserOverrides = getBrowserConfigOverrides();
@@ -3260,24 +3007,22 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3260
3007
  this.video = config.videoElement;
3261
3008
  this.adTransitionGapMs = (_this_config_adTransitionGapMs = this.config.adTransitionGapMs) !== null && _this_config_adTransitionGapMs !== void 0 ? _this_config_adTransitionGapMs : 100;
3262
3009
  logBrowserInfo(config.debugAdTiming);
3263
- this.vastManager = createVastManager(config.debugAdTiming !== void 0 ? {
3264
- debug: !!config.debugAdTiming
3265
- } : {});
3266
3010
  var browserForAdLayer = detectBrowser();
3267
3011
  var isSinglePipeline = browserForAdLayer.isSmartTV || !!this.config.singlePipelineMode;
3268
- this.adLayer = createVastAdLayer(this.video, {
3269
- continueLiveStreamDuringAds: false,
3270
- smartTVMode: isSinglePipeline,
3271
- singleElementMode: isSinglePipeline,
3272
- forceMP4Ads: isSinglePipeline,
3012
+ this.adLayer = createAdStormPlayer(this.video, {
3013
+ licenseKey: this.config.licenseKey || "",
3273
3014
  debug: !!config.debugAdTiming
3274
3015
  });
3016
+ this.adLayer.updateOptions({
3017
+ continueLiveStreamDuringAds: !isSinglePipeline && this.shouldContinueLiveStreamDuringAds()
3018
+ });
3275
3019
  }
3276
3020
  _create_class(StormcloudVideoPlayer, [
3277
3021
  {
3278
3022
  key: "adRequest",
3279
3023
  value: function adRequest(context) {
3280
3024
  return _async_to_generator(function() {
3025
+ var _ref, _ref1, durationSeconds;
3281
3026
  return _ts_generator(this, function(_state) {
3282
3027
  switch(_state.label){
3283
3028
  case 0:
@@ -3285,15 +3030,28 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3285
3030
  2,
3286
3031
  []
3287
3032
  ];
3033
+ 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));
3288
3034
  return [
3289
3035
  4,
3290
- this.vastManager.initialize()
3036
+ this.adLayer.requestAds(String(durationSeconds))
3291
3037
  ];
3292
3038
  case 1:
3293
3039
  _state.sent();
3294
3040
  return [
3295
3041
  2,
3296
- this.vastManager.requestBidsUntilResponse(context)
3042
+ [
3043
+ {
3044
+ bidder: "adstorm-direct",
3045
+ cpm: 0,
3046
+ width: 0,
3047
+ height: 0,
3048
+ adId: "adstorm",
3049
+ impId: "",
3050
+ creativeId: "adstorm",
3051
+ currency: "USD",
3052
+ durationSec: durationSeconds
3053
+ }
3054
+ ]
3297
3055
  ];
3298
3056
  }
3299
3057
  });
@@ -3350,7 +3108,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3350
3108
  2
3351
3109
  ];
3352
3110
  case 3:
3353
- this.hls = new import_hls2.default(_object_spread_props(_object_spread({
3111
+ this.hls = new import_hls.default(_object_spread_props(_object_spread({
3354
3112
  enableWorker: true,
3355
3113
  backBufferLength: 30,
3356
3114
  liveDurationInfinity: true,
@@ -3368,11 +3126,11 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3368
3126
  nudgeMaxRetry: 3,
3369
3127
  startPosition: -1
3370
3128
  }));
3371
- this.hls.on(import_hls2.default.Events.MEDIA_ATTACHED, function() {
3129
+ this.hls.on(import_hls.default.Events.MEDIA_ATTACHED, function() {
3372
3130
  var _this_hls;
3373
3131
  (_this_hls = _this.hls) === null || _this_hls === void 0 ? void 0 : _this_hls.loadSource(_this.config.src);
3374
3132
  });
3375
- this.hls.on(import_hls2.default.Events.MANIFEST_PARSED, function(_, data) {
3133
+ this.hls.on(import_hls.default.Events.MANIFEST_PARSED, function(_, data) {
3376
3134
  return _async_to_generator(function() {
3377
3135
  var _this_config_minSegmentsBeforePlay, _ref, _this_hls_levels, _this_hls, adBehavior, _this_hls1, minSegments, _this_video_play;
3378
3136
  return _ts_generator(this, function(_state) {
@@ -3434,7 +3192,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3434
3192
  });
3435
3193
  }).call(_this);
3436
3194
  });
3437
- this.hls.on(import_hls2.default.Events.LEVEL_LOADED, function(_evt, data) {
3195
+ this.hls.on(import_hls.default.Events.LEVEL_LOADED, function(_evt, data) {
3438
3196
  if (_this.inAdBreak || _this.pendingAdBreak) {
3439
3197
  return;
3440
3198
  }
@@ -3507,7 +3265,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3507
3265
  }
3508
3266
  }
3509
3267
  });
3510
- this.hls.on(import_hls2.default.Events.FRAG_BUFFERED, function(_evt, data) {
3268
+ this.hls.on(import_hls.default.Events.FRAG_BUFFERED, function(_evt, data) {
3511
3269
  return _async_to_generator(function() {
3512
3270
  var _this, _this_config_minSegmentsBeforePlay, minSegments, _this_video_play;
3513
3271
  return _ts_generator(this, function(_state) {
@@ -3567,7 +3325,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3567
3325
  });
3568
3326
  }).call(_this);
3569
3327
  });
3570
- this.hls.on(import_hls2.default.Events.FRAG_PARSING_METADATA, function(_evt, data) {
3328
+ this.hls.on(import_hls.default.Events.FRAG_PARSING_METADATA, function(_evt, data) {
3571
3329
  var id3Tags = ((data === null || data === void 0 ? void 0 : data.samples) || []).map(function(s) {
3572
3330
  return {
3573
3331
  key: "ID3",
@@ -3579,7 +3337,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3579
3337
  return _this.onId3Tag(tag);
3580
3338
  });
3581
3339
  });
3582
- this.hls.on(import_hls2.default.Events.FRAG_CHANGED, function(_evt, data) {
3340
+ this.hls.on(import_hls.default.Events.FRAG_CHANGED, function(_evt, data) {
3583
3341
  var frag = data === null || data === void 0 ? void 0 : data.frag;
3584
3342
  var tagList = frag === null || frag === void 0 ? void 0 : frag.tagList;
3585
3343
  if (!Array.isArray(tagList)) return;
@@ -3688,14 +3446,14 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3688
3446
  }
3689
3447
  }
3690
3448
  });
3691
- this.hls.on(import_hls2.default.Events.ERROR, function(_evt, data) {
3449
+ this.hls.on(import_hls.default.Events.ERROR, function(_evt, data) {
3692
3450
  if (data === null || data === void 0 ? void 0 : data.fatal) {
3693
3451
  switch(data.type){
3694
- case import_hls2.default.ErrorTypes.NETWORK_ERROR:
3452
+ case import_hls.default.ErrorTypes.NETWORK_ERROR:
3695
3453
  var _this_hls;
3696
3454
  (_this_hls = _this.hls) === null || _this_hls === void 0 ? void 0 : _this_hls.startLoad();
3697
3455
  break;
3698
- case import_hls2.default.ErrorTypes.MEDIA_ERROR:
3456
+ case import_hls.default.ErrorTypes.MEDIA_ERROR:
3699
3457
  var _this_hls1;
3700
3458
  (_this_hls1 = _this.hls) === null || _this_hls1 === void 0 ? void 0 : _this_hls1.recoverMediaError();
3701
3459
  break;
@@ -3787,7 +3545,11 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3787
3545
  }
3788
3546
  });
3789
3547
  this.adLayer.on("content_resume", function() {
3548
+ var _ref, _ref1;
3549
+ var _this_savedMutedStateBeforeScte, _this_savedMutedStateBeforeScte1;
3790
3550
  var remaining = _this.getRemainingAdMs();
3551
+ 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();
3552
+ 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();
3791
3553
  if (_this.config.debugAdTiming) {
3792
3554
  console.log("[StormcloudVideoPlayer] content_resume received, inAdBreak=%s, remaining=%s, preloadedTokens=%d, pendingNext=%s", _this.inAdBreak, remaining, _this.preloadedTokens.length, !!_this.pendingNextAdBids);
3793
3555
  }
@@ -3802,7 +3564,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3802
3564
  return;
3803
3565
  }
3804
3566
  _this.consecutiveFailures = 0;
3805
- if (!_this.video.muted) {
3567
+ if (!_this.config.disableFiller && !_this.video.muted) {
3806
3568
  _this.video.muted = true;
3807
3569
  _this.video.volume = 0;
3808
3570
  }
@@ -3869,6 +3631,18 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3869
3631
  if (!_this.config.disableFiller) {
3870
3632
  _this.showPlaceholderLayer();
3871
3633
  _this.adLayer.showPlaceholder();
3634
+ } else {
3635
+ _this.adLayer.hidePlaceholder();
3636
+ if (_this.video.muted !== breakMuted) {
3637
+ _this.video.muted = breakMuted;
3638
+ }
3639
+ if (Math.abs(_this.video.volume - breakVolume) > 0.01) {
3640
+ _this.video.volume = breakVolume;
3641
+ }
3642
+ if (_this.video.paused) {
3643
+ var _this_video_play;
3644
+ (_this_video_play = _this.video.play()) === null || _this_video_play === void 0 ? void 0 : _this_video_play.catch(function() {});
3645
+ }
3872
3646
  }
3873
3647
  _this.stopContinuousFetching();
3874
3648
  return;
@@ -4596,6 +4370,16 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4596
4370
  return this.totalAdsInBreak;
4597
4371
  }
4598
4372
  },
4373
+ {
4374
+ key: "getRemainingAdSeconds",
4375
+ value: function getRemainingAdSeconds() {
4376
+ var remainingMs = this.getRemainingAdMs();
4377
+ if (!Number.isFinite(remainingMs) || remainingMs <= 0 || remainingMs === Number.MAX_SAFE_INTEGER) {
4378
+ return 0;
4379
+ }
4380
+ return Math.ceil(remainingMs / 1e3);
4381
+ }
4382
+ },
4599
4383
  {
4600
4384
  key: "isAdPlaying",
4601
4385
  value: function isAdPlaying() {
@@ -5825,7 +5609,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5825
5609
  });
5826
5610
  };
5827
5611
  var onManifestParsedRestore = function onManifestParsedRestore1() {
5828
- hlsRef.off(import_hls2.default.Events.MANIFEST_PARSED, onManifestParsedRestore);
5612
+ hlsRef.off(import_hls.default.Events.MANIFEST_PARSED, onManifestParsedRestore);
5829
5613
  if (!_this.inAdBreak && !_this.adLayer.isAdPlaying()) {
5830
5614
  if (videoRef.muted !== savedMuted) videoRef.muted = savedMuted;
5831
5615
  if (Math.abs(videoRef.volume - savedVolume) > 0.01) videoRef.volume = savedVolume;
@@ -5838,7 +5622,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5838
5622
  }
5839
5623
  }
5840
5624
  };
5841
- hlsRef.on(import_hls2.default.Events.MANIFEST_PARSED, onManifestParsedRestore);
5625
+ hlsRef.on(import_hls.default.Events.MANIFEST_PARSED, onManifestParsedRestore);
5842
5626
  var pipelineDelayMs = 300;
5843
5627
  if (debugEnabled) {
5844
5628
  console.log("[StormcloudVideoPlayer] Smart TV: waiting ".concat(pipelineDelayMs, "ms for hardware pipeline release before re-attach"));
@@ -7452,7 +7236,8 @@ var StormcloudVideoPlayerComponent = import_react2.default.memo(function(props)
7452
7236
  var _import_react2_default_useState = _sliced_to_array(import_react2.default.useState({
7453
7237
  showAds: false,
7454
7238
  currentIndex: 0,
7455
- totalAds: 0
7239
+ totalAds: 0,
7240
+ remainingSeconds: 0
7456
7241
  }), 2), adStatus = _import_react2_default_useState[0], setAdStatus = _import_react2_default_useState[1];
7457
7242
  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];
7458
7243
  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];
@@ -7704,15 +7489,17 @@ var StormcloudVideoPlayerComponent = import_react2.default.memo(function(props)
7704
7489
  var showAds = showAdsFromMethod || showAdsFromAttribute;
7705
7490
  var currentIndex = playerRef.current.getCurrentAdIndex();
7706
7491
  var totalAds = playerRef.current.getTotalAdsInBreak();
7492
+ var remainingSeconds = playerRef.current.getRemainingAdSeconds();
7707
7493
  setAdStatus(function(prev) {
7708
- if (prev.showAds !== showAds || prev.currentIndex !== currentIndex || prev.totalAds !== totalAds) {
7494
+ if (prev.showAds !== showAds || prev.currentIndex !== currentIndex || prev.totalAds !== totalAds || prev.remainingSeconds !== remainingSeconds) {
7709
7495
  if (showAds && !prev.showAds) {
7710
7496
  setShowCenterPlay(false);
7711
7497
  }
7712
7498
  return {
7713
7499
  showAds: showAds,
7714
7500
  currentIndex: currentIndex,
7715
- totalAds: totalAds
7501
+ totalAds: totalAds,
7502
+ remainingSeconds: remainingSeconds
7716
7503
  };
7717
7504
  }
7718
7505
  return prev;
@@ -8102,6 +7889,22 @@ var StormcloudVideoPlayerComponent = import_react2.default.memo(function(props)
8102
7889
  " of ",
8103
7890
  Math.max(adStatus.totalAds, adStatus.currentIndex)
8104
7891
  ]
7892
+ }),
7893
+ adStatus.remainingSeconds > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", {
7894
+ style: {
7895
+ background: "rgba(0, 0, 0, 0.5)",
7896
+ backdropFilter: "blur(12px)",
7897
+ color: "rgba(255, 255, 255, 0.9)",
7898
+ padding: "".concat(4 * responsiveScale, "px ").concat(10 * responsiveScale, "px"),
7899
+ borderRadius: "6px",
7900
+ fontSize: "".concat(11 * responsiveScale, "px"),
7901
+ fontWeight: "600",
7902
+ border: "1px solid rgba(255, 255, 255, 0.08)"
7903
+ },
7904
+ children: [
7905
+ adStatus.remainingSeconds,
7906
+ "s"
7907
+ ]
8105
7908
  })
8106
7909
  ]
8107
7910
  }),
@@ -9909,9 +9712,8 @@ var StormcloudPlayer_default = StormcloudPlayer;
9909
9712
  StormcloudVideoPlayer: StormcloudVideoPlayer,
9910
9713
  StormcloudVideoPlayerComponent: StormcloudVideoPlayerComponent,
9911
9714
  canPlay: canPlay,
9715
+ createAdStormPlayer: createAdStormPlayer,
9912
9716
  createStormcloudPlayer: createStormcloudPlayer,
9913
- createVastAdLayer: createVastAdLayer,
9914
- createVastManager: createVastManager,
9915
9717
  detectBrowser: detectBrowser,
9916
9718
  fetchProjectOverlays: fetchProjectOverlays,
9917
9719
  getBrowserConfigOverrides: getBrowserConfigOverrides,