stormcloud-video-player 0.6.0 → 0.6.2

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.
@@ -599,14 +599,17 @@ function resolveBidToVastAd(winner, logPrefix) {
599
599
  return Promise.resolve(null);
600
600
  }
601
601
  function createVastAdLayer(contentVideo, options) {
602
- var _ref, _ref1;
602
+ var _ref, _ref1, _ref2, _ref3, _ref4;
603
603
  var adPlaying = false;
604
604
  var originalMutedState = false;
605
605
  var originalVolume = Math.max(0, Math.min(1, contentVideo.volume || 1));
606
606
  var listeners = /* @__PURE__ */ new Map();
607
607
  var mainHlsInstance = options === null || options === void 0 ? void 0 : options.mainHlsInstance;
608
608
  var continueLiveStreamDuringAds = (_ref = options === null || options === void 0 ? void 0 : options.continueLiveStreamDuringAds) !== null && _ref !== void 0 ? _ref : false;
609
- var debug = (_ref1 = options === null || options === void 0 ? void 0 : options.debug) !== null && _ref1 !== void 0 ? _ref1 : false;
609
+ var smartTVMode = (_ref1 = options === null || options === void 0 ? void 0 : options.smartTVMode) !== null && _ref1 !== void 0 ? _ref1 : false;
610
+ var singleElementMode = (_ref2 = options === null || options === void 0 ? void 0 : options.singleElementMode) !== null && _ref2 !== void 0 ? _ref2 : false;
611
+ var forceMP4Ads = (_ref3 = options === null || options === void 0 ? void 0 : options.forceMP4Ads) !== null && _ref3 !== void 0 ? _ref3 : smartTVMode || singleElementMode;
612
+ var debug = (_ref4 = options === null || options === void 0 ? void 0 : options.debug) !== null && _ref4 !== void 0 ? _ref4 : false;
610
613
  var adVideoElement;
611
614
  var adHls;
612
615
  var adContainerEl;
@@ -615,6 +618,7 @@ function createVastAdLayer(contentVideo, options) {
615
618
  var destroyed = false;
616
619
  var tornDown = false;
617
620
  var trackingFired = createEmptyTrackingState();
621
+ var currentAdEventHandlers;
618
622
  var preloadSlots = /* @__PURE__ */ new Map();
619
623
  function emit(event, payload) {
620
624
  var set = listeners.get(event);
@@ -676,14 +680,26 @@ function createVastAdLayer(contentVideo, options) {
676
680
  var _ref;
677
681
  var _scoredFiles_;
678
682
  if (mediaFiles.length === 0) throw new Error("No media files available");
679
- var firstFile = mediaFiles[0];
680
- if (mediaFiles.length === 1) return firstFile;
683
+ var candidates = mediaFiles;
684
+ if (forceMP4Ads) {
685
+ var mp4Only = candidates.filter(function(f) {
686
+ return !isHlsMediaFile(f);
687
+ });
688
+ if (mp4Only.length > 0) {
689
+ candidates = mp4Only;
690
+ if (debug) console.log("".concat(LOG, " forceMP4Ads: filtered to ").concat(mp4Only.length, " MP4-only file(s)"));
691
+ } else if (debug) {
692
+ console.warn("".concat(LOG, " forceMP4Ads: no MP4 files available, falling back to all media files"));
693
+ }
694
+ }
695
+ var firstFile = candidates[0];
696
+ if (candidates.length === 1) return firstFile;
681
697
  var mainQuality = getMainStreamQuality();
682
698
  if (!mainQuality) {
683
699
  if (debug) console.log("".concat(LOG, " No main stream quality info, using first media file"));
684
700
  return firstFile;
685
701
  }
686
- var scoredFiles = mediaFiles.map(function(file) {
702
+ var scoredFiles = candidates.map(function(file) {
687
703
  var widthDiff = Math.abs(file.width - mainQuality.width);
688
704
  var heightDiff = Math.abs(file.height - mainQuality.height);
689
705
  var resolutionDiff = widthDiff + heightDiff;
@@ -717,63 +733,86 @@ function createVastAdLayer(contentVideo, options) {
717
733
  video.volume = 1;
718
734
  return video;
719
735
  }
736
+ function removeAdEventListeners() {
737
+ if (!currentAdEventHandlers || !adVideoElement) return;
738
+ var el = adVideoElement;
739
+ el.removeEventListener("timeupdate", currentAdEventHandlers.timeupdate);
740
+ el.removeEventListener("playing", currentAdEventHandlers.playing);
741
+ el.removeEventListener("ended", currentAdEventHandlers.ended);
742
+ el.removeEventListener("error", currentAdEventHandlers.error);
743
+ el.removeEventListener("volumechange", currentAdEventHandlers.volumechange);
744
+ el.removeEventListener("pause", currentAdEventHandlers.pause);
745
+ el.removeEventListener("play", currentAdEventHandlers.play);
746
+ currentAdEventHandlers = void 0;
747
+ }
720
748
  function setupAdEventListeners() {
721
749
  if (!adVideoElement) return;
722
- adVideoElement.addEventListener("timeupdate", function() {
723
- var ad = currentAd;
724
- if (!ad || !adVideoElement) return;
725
- var progress = adVideoElement.currentTime / ad.duration;
726
- if (progress >= 0.25 && !trackingFired.firstQuartile) {
727
- trackingFired.firstQuartile = true;
728
- fireTrackingPixels2(ad.trackingUrls.firstQuartile);
729
- }
730
- if (progress >= 0.5 && !trackingFired.midpoint) {
731
- trackingFired.midpoint = true;
732
- fireTrackingPixels2(ad.trackingUrls.midpoint);
733
- }
734
- if (progress >= 0.75 && !trackingFired.thirdQuartile) {
735
- trackingFired.thirdQuartile = true;
736
- fireTrackingPixels2(ad.trackingUrls.thirdQuartile);
737
- }
738
- });
739
- adVideoElement.addEventListener("playing", function() {
740
- var ad = currentAd;
741
- if (!ad || trackingFired.start) return;
742
- trackingFired.start = true;
743
- fireTrackingPixels2(ad.trackingUrls.start);
744
- if (debug) console.log("".concat(LOG, " Ad started playing"));
745
- });
746
- adVideoElement.addEventListener("ended", function() {
747
- if (tornDown || !currentAd || trackingFired.complete) return;
748
- trackingFired.complete = true;
749
- fireTrackingPixels2(currentAd.trackingUrls.complete);
750
- if (debug) console.log("".concat(LOG, " Ad completed"));
751
- handleAdComplete();
752
- });
753
- adVideoElement.addEventListener("error", function(e) {
754
- if (tornDown) return;
755
- console.error("".concat(LOG, " Ad video error:"), e);
756
- if (currentAd) fireTrackingPixels2(currentAd.trackingUrls.error);
757
- handleAdError();
758
- });
759
- adVideoElement.addEventListener("volumechange", function() {
760
- if (!currentAd || !adVideoElement) return;
761
- if (adVideoElement.muted) {
762
- fireTrackingPixels2(currentAd.trackingUrls.mute);
763
- } else {
764
- fireTrackingPixels2(currentAd.trackingUrls.unmute);
765
- }
766
- });
767
- adVideoElement.addEventListener("pause", function() {
768
- if (currentAd && adVideoElement && !adVideoElement.ended) {
769
- fireTrackingPixels2(currentAd.trackingUrls.pause);
770
- }
771
- });
772
- adVideoElement.addEventListener("play", function() {
773
- if (currentAd && adVideoElement && adVideoElement.currentTime > 0) {
774
- fireTrackingPixels2(currentAd.trackingUrls.resume);
750
+ removeAdEventListeners();
751
+ var handlers = {
752
+ timeupdate: function timeupdate() {
753
+ var ad = currentAd;
754
+ if (!ad || !adVideoElement) return;
755
+ var progress = adVideoElement.currentTime / ad.duration;
756
+ if (progress >= 0.25 && !trackingFired.firstQuartile) {
757
+ trackingFired.firstQuartile = true;
758
+ fireTrackingPixels2(ad.trackingUrls.firstQuartile);
759
+ }
760
+ if (progress >= 0.5 && !trackingFired.midpoint) {
761
+ trackingFired.midpoint = true;
762
+ fireTrackingPixels2(ad.trackingUrls.midpoint);
763
+ }
764
+ if (progress >= 0.75 && !trackingFired.thirdQuartile) {
765
+ trackingFired.thirdQuartile = true;
766
+ fireTrackingPixels2(ad.trackingUrls.thirdQuartile);
767
+ }
768
+ },
769
+ playing: function playing() {
770
+ var ad = currentAd;
771
+ if (!ad || trackingFired.start) return;
772
+ trackingFired.start = true;
773
+ fireTrackingPixels2(ad.trackingUrls.start);
774
+ if (debug) console.log("".concat(LOG, " Ad started playing"));
775
+ },
776
+ ended: function ended() {
777
+ if (tornDown || !currentAd || trackingFired.complete) return;
778
+ trackingFired.complete = true;
779
+ fireTrackingPixels2(currentAd.trackingUrls.complete);
780
+ if (debug) console.log("".concat(LOG, " Ad completed"));
781
+ handleAdComplete();
782
+ },
783
+ error: function error(e) {
784
+ if (tornDown) return;
785
+ console.error("".concat(LOG, " Ad video error:"), e);
786
+ if (currentAd) fireTrackingPixels2(currentAd.trackingUrls.error);
787
+ handleAdError();
788
+ },
789
+ volumechange: function volumechange() {
790
+ if (!currentAd || !adVideoElement) return;
791
+ if (adVideoElement.muted) {
792
+ fireTrackingPixels2(currentAd.trackingUrls.mute);
793
+ } else {
794
+ fireTrackingPixels2(currentAd.trackingUrls.unmute);
795
+ }
796
+ },
797
+ pause: function pause() {
798
+ if (currentAd && adVideoElement && !adVideoElement.ended) {
799
+ fireTrackingPixels2(currentAd.trackingUrls.pause);
800
+ }
801
+ },
802
+ play: function play() {
803
+ if (currentAd && adVideoElement && adVideoElement.currentTime > 0) {
804
+ fireTrackingPixels2(currentAd.trackingUrls.resume);
805
+ }
775
806
  }
776
- });
807
+ };
808
+ adVideoElement.addEventListener("timeupdate", handlers.timeupdate);
809
+ adVideoElement.addEventListener("playing", handlers.playing);
810
+ adVideoElement.addEventListener("ended", handlers.ended);
811
+ adVideoElement.addEventListener("error", handlers.error);
812
+ adVideoElement.addEventListener("volumechange", handlers.volumechange);
813
+ adVideoElement.addEventListener("pause", handlers.pause);
814
+ adVideoElement.addEventListener("play", handlers.play);
815
+ currentAdEventHandlers = handlers;
777
816
  }
778
817
  function setAdPlayingFlag(isPlaying) {
779
818
  if (isPlaying) {
@@ -796,6 +835,7 @@ function createVastAdLayer(contentVideo, options) {
796
835
  }
797
836
  function handleAdError() {
798
837
  if (tornDown) return;
838
+ if (!adPlaying) return;
799
839
  if (debug) console.log("".concat(LOG, " Handling ad error"));
800
840
  adPlaying = false;
801
841
  setAdPlayingFlag(false);
@@ -806,14 +846,19 @@ function createVastAdLayer(contentVideo, options) {
806
846
  emit("ad_error");
807
847
  }
808
848
  function teardownCurrentPlayback() {
849
+ removeAdEventListeners();
809
850
  if (adHls) {
810
851
  adHls.destroy();
811
852
  adHls = void 0;
812
853
  }
813
854
  if (adVideoElement) {
814
- adVideoElement.pause();
815
- adVideoElement.removeAttribute("src");
816
- adVideoElement.load();
855
+ if (singleElementMode && adVideoElement === contentVideo) {
856
+ contentVideo.pause();
857
+ } else {
858
+ adVideoElement.pause();
859
+ adVideoElement.removeAttribute("src");
860
+ adVideoElement.load();
861
+ }
817
862
  }
818
863
  }
819
864
  function startNativePlayback(mediaFile) {
@@ -846,8 +891,17 @@ function createVastAdLayer(contentVideo, options) {
846
891
  handleAdError();
847
892
  });
848
893
  });
894
+ var nonFatalNetworkErrors = 0;
849
895
  adHls.on(import_hls.default.Events.ERROR, function(_event, data) {
850
- if (data.fatal) handleAdError();
896
+ if (data.fatal) {
897
+ handleAdError();
898
+ } else if (data.type === import_hls.default.ErrorTypes.NETWORK_ERROR) {
899
+ nonFatalNetworkErrors++;
900
+ if (nonFatalNetworkErrors >= 3) {
901
+ if (debug) console.warn("".concat(LOG, " Too many non-fatal HLS network errors (").concat(nonFatalNetworkErrors, "), treating as fatal"));
902
+ handleAdError();
903
+ }
904
+ }
851
905
  });
852
906
  } else if (adVideoElement.canPlayType("application/vnd.apple.mpegurl")) {
853
907
  adVideoElement.src = mediaFile.url;
@@ -862,6 +916,16 @@ function createVastAdLayer(contentVideo, options) {
862
916
  }
863
917
  function startPlayback(mediaFile) {
864
918
  if (!adVideoElement) return;
919
+ if (singleElementMode && isHlsMediaFile(mediaFile)) {
920
+ var mp4Fallback = currentAd === null || currentAd === void 0 ? void 0 : currentAd.mediaFiles.find(function(f) {
921
+ return !isHlsMediaFile(f);
922
+ });
923
+ if (mp4Fallback) {
924
+ if (debug) console.log("".concat(LOG, " singleElementMode: HLS ad blocked, using MP4 fallback"));
925
+ startNativePlayback(mp4Fallback);
926
+ return;
927
+ }
928
+ }
865
929
  if (isHlsMediaFile(mediaFile)) {
866
930
  startHlsPlayback(mediaFile);
867
931
  } else {
@@ -870,7 +934,7 @@ function createVastAdLayer(contentVideo, options) {
870
934
  }
871
935
  function playAd(bids) {
872
936
  return _async_to_generator(function() {
873
- var winner, ad, _contentVideo_parentElement, container, contentVolume, adVolume, mediaFile;
937
+ var winner, ad, contentVolume, _contentVideo_parentElement, container, adVolume, mediaFile;
874
938
  return _ts_generator(this, function(_state) {
875
939
  switch(_state.label){
876
940
  case 0:
@@ -912,32 +976,40 @@ function createVastAdLayer(contentVideo, options) {
912
976
  trackingFired = _object_spread({}, createEmptyTrackingState());
913
977
  fireTrackingPixels2(ad.trackingUrls.impression);
914
978
  trackingFired.impression = true;
915
- if (!adContainerEl) {
916
- ;
917
- container = document.createElement("div");
918
- container.style.position = "absolute";
919
- container.style.left = "0";
920
- container.style.top = "0";
921
- container.style.right = "0";
922
- container.style.bottom = "0";
923
- container.style.display = "none";
924
- container.style.alignItems = "center";
925
- container.style.justifyContent = "center";
926
- container.style.pointerEvents = "none";
927
- container.style.zIndex = "10";
928
- container.style.backgroundColor = "#000";
929
- (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
930
- adContainerEl = container;
931
- }
932
- if (!adVideoElement) {
933
- adVideoElement = createAdVideoElement();
934
- adContainerEl.appendChild(adVideoElement);
979
+ contentVolume = contentVideo.volume;
980
+ originalVolume = Math.max(0, Math.min(1, contentVolume || originalVolume));
981
+ if (singleElementMode) {
982
+ mainHlsInstance === null || mainHlsInstance === void 0 ? void 0 : mainHlsInstance.detachMedia();
983
+ teardownCurrentPlayback();
984
+ adVideoElement = contentVideo;
985
+ adHls = void 0;
935
986
  setupAdEventListeners();
936
987
  } else {
937
- teardownCurrentPlayback();
988
+ if (!adContainerEl) {
989
+ ;
990
+ container = document.createElement("div");
991
+ container.style.position = "absolute";
992
+ container.style.left = "0";
993
+ container.style.top = "0";
994
+ container.style.right = "0";
995
+ container.style.bottom = "0";
996
+ container.style.display = "none";
997
+ container.style.alignItems = "center";
998
+ container.style.justifyContent = "center";
999
+ container.style.pointerEvents = "none";
1000
+ container.style.zIndex = "10";
1001
+ container.style.backgroundColor = "#000";
1002
+ (_contentVideo_parentElement = contentVideo.parentElement) === null || _contentVideo_parentElement === void 0 ? void 0 : _contentVideo_parentElement.appendChild(container);
1003
+ adContainerEl = container;
1004
+ }
1005
+ if (!adVideoElement) {
1006
+ adVideoElement = createAdVideoElement();
1007
+ adContainerEl.appendChild(adVideoElement);
1008
+ setupAdEventListeners();
1009
+ } else {
1010
+ teardownCurrentPlayback();
1011
+ }
938
1012
  }
939
- contentVolume = contentVideo.volume;
940
- originalVolume = Math.max(0, Math.min(1, contentVolume || originalVolume));
941
1013
  if (!continueLiveStreamDuringAds) {
942
1014
  contentVideo.pause();
943
1015
  }
@@ -948,7 +1020,7 @@ function createVastAdLayer(contentVideo, options) {
948
1020
  adVolume = originalMutedState ? 1 : originalVolume;
949
1021
  adVideoElement.volume = Math.max(0, Math.min(1, adVolume));
950
1022
  adVideoElement.muted = false;
951
- if (adContainerEl) {
1023
+ if (!singleElementMode && adContainerEl) {
952
1024
  adContainerEl.style.display = "flex";
953
1025
  adContainerEl.style.pointerEvents = "auto";
954
1026
  }
@@ -985,7 +1057,7 @@ function createVastAdLayer(contentVideo, options) {
985
1057
  }
986
1058
  function preloadAd(bids, token) {
987
1059
  return _async_to_generator(function() {
988
- var winner, ad, mediaFile, videoEl, container, hls, slot, slot1;
1060
+ var winner, ad, mediaFile, slot, videoEl, container, hls, slot1, slot2;
989
1061
  return _ts_generator(this, function(_state) {
990
1062
  switch(_state.label){
991
1063
  case 0:
@@ -1010,6 +1082,20 @@ function createVastAdLayer(contentVideo, options) {
1010
1082
  if (!mediaFile) return [
1011
1083
  2
1012
1084
  ];
1085
+ if (smartTVMode || singleElementMode) {
1086
+ slot = {
1087
+ bids: bids,
1088
+ ad: ad,
1089
+ mediaFile: mediaFile,
1090
+ videoEl: null,
1091
+ ready: true
1092
+ };
1093
+ preloadSlots.set(token, slot);
1094
+ if (debug) console.log("".concat(LOG, " [preload] Metadata-only preload (smartTV/singleElement), token=").concat(token, ", url=").concat(mediaFile.url));
1095
+ return [
1096
+ 2
1097
+ ];
1098
+ }
1013
1099
  videoEl = createAdVideoElement();
1014
1100
  videoEl.style.visibility = "hidden";
1015
1101
  videoEl.style.pointerEvents = "none";
@@ -1023,7 +1109,7 @@ function createVastAdLayer(contentVideo, options) {
1023
1109
  });
1024
1110
  hls.loadSource(mediaFile.url);
1025
1111
  hls.attachMedia(videoEl);
1026
- slot = {
1112
+ slot1 = {
1027
1113
  bids: bids,
1028
1114
  ad: ad,
1029
1115
  mediaFile: mediaFile,
@@ -1031,7 +1117,7 @@ function createVastAdLayer(contentVideo, options) {
1031
1117
  hlsInstance: hls,
1032
1118
  ready: false
1033
1119
  };
1034
- preloadSlots.set(token, slot);
1120
+ preloadSlots.set(token, slot1);
1035
1121
  hls.on(import_hls.default.Events.MANIFEST_PARSED, function() {
1036
1122
  var s = preloadSlots.get(token);
1037
1123
  if (s) s.ready = true;
@@ -1048,14 +1134,14 @@ function createVastAdLayer(contentVideo, options) {
1048
1134
  } else {
1049
1135
  videoEl.src = mediaFile.url;
1050
1136
  videoEl.load();
1051
- slot1 = {
1137
+ slot2 = {
1052
1138
  bids: bids,
1053
1139
  ad: ad,
1054
1140
  mediaFile: mediaFile,
1055
1141
  videoEl: videoEl,
1056
1142
  ready: false
1057
1143
  };
1058
- preloadSlots.set(token, slot1);
1144
+ preloadSlots.set(token, slot2);
1059
1145
  videoEl.addEventListener("canplay", function() {
1060
1146
  var s = preloadSlots.get(token);
1061
1147
  if (s) s.ready = true;
@@ -1074,7 +1160,7 @@ function createVastAdLayer(contentVideo, options) {
1074
1160
  }
1075
1161
  function playPreloaded(token) {
1076
1162
  return _async_to_generator(function() {
1077
- var slot, contentVolume, adVolume, container;
1163
+ var slot, contentVolume, adVolume2, videoEl, container2, adVolume21, adVolume, container;
1078
1164
  return _ts_generator(this, function(_state) {
1079
1165
  if (destroyed) return [
1080
1166
  2,
@@ -1089,6 +1175,68 @@ function createVastAdLayer(contentVideo, options) {
1089
1175
  }
1090
1176
  preloadSlots.delete(token);
1091
1177
  if (debug) console.log("".concat(LOG, " [preload] Playing preloaded ad, token=").concat(token, ", ready=").concat(slot.ready));
1178
+ contentVolume = contentVideo.volume;
1179
+ originalVolume = Math.max(0, Math.min(1, contentVolume || originalVolume));
1180
+ sessionId = generateSessionId();
1181
+ currentAd = slot.ad;
1182
+ trackingFired = _object_spread({}, createEmptyTrackingState());
1183
+ fireTrackingPixels2(slot.ad.trackingUrls.impression);
1184
+ trackingFired.impression = true;
1185
+ if (singleElementMode) {
1186
+ mainHlsInstance === null || mainHlsInstance === void 0 ? void 0 : mainHlsInstance.detachMedia();
1187
+ teardownCurrentPlayback();
1188
+ adVideoElement = contentVideo;
1189
+ adHls = void 0;
1190
+ setupAdEventListeners();
1191
+ if (!continueLiveStreamDuringAds) {
1192
+ contentVideo.pause();
1193
+ }
1194
+ contentVideo.muted = true;
1195
+ contentVideo.volume = 0;
1196
+ adPlaying = true;
1197
+ setAdPlayingFlag(true);
1198
+ adVolume2 = originalMutedState ? 1 : originalVolume;
1199
+ contentVideo.volume = Math.max(0, Math.min(1, adVolume2));
1200
+ contentVideo.muted = false;
1201
+ emit("content_pause");
1202
+ if (debug) console.log("".concat(LOG, " [preload] singleElementMode: attaching ad to contentVideo, url=").concat(slot.mediaFile.url));
1203
+ startPlayback(slot.mediaFile);
1204
+ return [
1205
+ 2
1206
+ ];
1207
+ }
1208
+ if (smartTVMode && !slot.videoEl) {
1209
+ teardownCurrentPlayback();
1210
+ if (adVideoElement) {
1211
+ adVideoElement.remove();
1212
+ adVideoElement = void 0;
1213
+ }
1214
+ videoEl = createAdVideoElement();
1215
+ videoEl.style.visibility = "visible";
1216
+ videoEl.style.pointerEvents = "none";
1217
+ container2 = ensureAdContainer();
1218
+ container2.appendChild(videoEl);
1219
+ adVideoElement = videoEl;
1220
+ setupAdEventListeners();
1221
+ if (!continueLiveStreamDuringAds) {
1222
+ contentVideo.pause();
1223
+ }
1224
+ contentVideo.muted = true;
1225
+ contentVideo.volume = 0;
1226
+ adPlaying = true;
1227
+ setAdPlayingFlag(true);
1228
+ adVolume21 = originalMutedState ? 1 : originalVolume;
1229
+ adVideoElement.volume = Math.max(0, Math.min(1, adVolume21));
1230
+ adVideoElement.muted = false;
1231
+ container2.style.display = "flex";
1232
+ container2.style.pointerEvents = "auto";
1233
+ emit("content_pause");
1234
+ if (debug) console.log("".concat(LOG, " [preload] smartTVMode deferred: creating video element and loading, url=").concat(slot.mediaFile.url));
1235
+ startPlayback(slot.mediaFile);
1236
+ return [
1237
+ 2
1238
+ ];
1239
+ }
1092
1240
  teardownCurrentPlayback();
1093
1241
  if (adVideoElement && adVideoElement !== slot.videoEl) {
1094
1242
  adVideoElement.remove();
@@ -1098,13 +1246,6 @@ function createVastAdLayer(contentVideo, options) {
1098
1246
  adVideoElement = slot.videoEl;
1099
1247
  adHls = slot.hlsInstance;
1100
1248
  setupAdEventListeners();
1101
- sessionId = generateSessionId();
1102
- currentAd = slot.ad;
1103
- trackingFired = _object_spread({}, createEmptyTrackingState());
1104
- fireTrackingPixels2(slot.ad.trackingUrls.impression);
1105
- trackingFired.impression = true;
1106
- contentVolume = contentVideo.volume;
1107
- originalVolume = Math.max(0, Math.min(1, contentVolume || originalVolume));
1108
1249
  if (!continueLiveStreamDuringAds) {
1109
1250
  contentVideo.pause();
1110
1251
  }
@@ -1119,17 +1260,10 @@ function createVastAdLayer(contentVideo, options) {
1119
1260
  container.style.display = "flex";
1120
1261
  container.style.pointerEvents = "auto";
1121
1262
  emit("content_pause");
1122
- if (slot.hlsInstance) {
1123
- adVideoElement.play().catch(function(error) {
1124
- console.error("".concat(LOG, " [preload] Error playing preloaded HLS ad:"), error);
1125
- handleAdError();
1126
- });
1127
- } else {
1128
- adVideoElement.play().catch(function(error) {
1129
- console.error("".concat(LOG, " [preload] Error playing preloaded ad:"), error);
1130
- handleAdError();
1131
- });
1132
- }
1263
+ adVideoElement.play().catch(function(error) {
1264
+ console.error("".concat(LOG, " [preload] Error playing preloaded ad:"), error);
1265
+ handleAdError();
1266
+ });
1133
1267
  return [
1134
1268
  2
1135
1269
  ];
@@ -1143,10 +1277,12 @@ function createVastAdLayer(contentVideo, options) {
1143
1277
  if (slot.hlsInstance) {
1144
1278
  slot.hlsInstance.destroy();
1145
1279
  }
1146
- slot.videoEl.pause();
1147
- slot.videoEl.removeAttribute("src");
1148
- slot.videoEl.load();
1149
- slot.videoEl.remove();
1280
+ if (slot.videoEl) {
1281
+ slot.videoEl.pause();
1282
+ slot.videoEl.removeAttribute("src");
1283
+ slot.videoEl.load();
1284
+ slot.videoEl.remove();
1285
+ }
1150
1286
  if (debug) console.log("".concat(LOG, " [preload] Cancelled and cleaned up token=").concat(token));
1151
1287
  }
1152
1288
  return {
@@ -1194,20 +1330,27 @@ function createVastAdLayer(contentVideo, options) {
1194
1330
  setAdPlayingFlag(false);
1195
1331
  contentVideo.muted = originalMutedState;
1196
1332
  contentVideo.volume = originalMutedState ? 0 : originalVolume;
1197
- if (adContainerEl) {
1198
- adContainerEl.style.display = "none";
1199
- adContainerEl.style.pointerEvents = "none";
1200
- }
1201
1333
  contentVideo.style.visibility = "visible";
1202
1334
  contentVideo.style.opacity = "1";
1203
- if (continueLiveStreamDuringAds) {
1204
- contentVideo.play().catch(function() {});
1205
- }
1206
- teardownCurrentPlayback();
1207
- if (adVideoElement) {
1208
- adVideoElement.pause();
1209
- adVideoElement.removeAttribute("src");
1210
- adVideoElement.load();
1335
+ if (singleElementMode) {
1336
+ teardownCurrentPlayback();
1337
+ contentVideo.removeAttribute("src");
1338
+ contentVideo.load();
1339
+ adVideoElement = void 0;
1340
+ } else {
1341
+ if (adContainerEl) {
1342
+ adContainerEl.style.display = "none";
1343
+ adContainerEl.style.pointerEvents = "none";
1344
+ }
1345
+ if (continueLiveStreamDuringAds) {
1346
+ contentVideo.play().catch(function() {});
1347
+ }
1348
+ teardownCurrentPlayback();
1349
+ if (adVideoElement) {
1350
+ adVideoElement.pause();
1351
+ adVideoElement.removeAttribute("src");
1352
+ adVideoElement.load();
1353
+ }
1211
1354
  }
1212
1355
  currentAd = void 0;
1213
1356
  tornDown = false;
@@ -1247,9 +1390,14 @@ function createVastAdLayer(contentVideo, options) {
1247
1390
  }
1248
1391
  teardownCurrentPlayback();
1249
1392
  if (adVideoElement) {
1250
- adVideoElement.pause();
1251
- adVideoElement.removeAttribute("src");
1252
- adVideoElement.remove();
1393
+ if (singleElementMode && adVideoElement === contentVideo) {
1394
+ contentVideo.removeAttribute("src");
1395
+ contentVideo.load();
1396
+ } else {
1397
+ adVideoElement.pause();
1398
+ adVideoElement.removeAttribute("src");
1399
+ adVideoElement.remove();
1400
+ }
1253
1401
  adVideoElement = void 0;
1254
1402
  }
1255
1403
  if (adContainerEl === null || adContainerEl === void 0 ? void 0 : adContainerEl.parentElement) {