stormcloud-video-player 0.3.62 → 0.3.64

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.
@@ -2466,6 +2466,99 @@ function createHlsAdPlayer(contentVideo, options) {
2466
2466
  }
2467
2467
  };
2468
2468
  }
2469
+ // src/utils/mqttConfig.ts
2470
+ var DEFAULT_MQTT_CONFIG = {
2471
+ enabled: true,
2472
+ brokerAddress: "vecbae77.ala.us-east-1.emqxsl.com",
2473
+ brokerPort: 8883,
2474
+ wsPort: 8084,
2475
+ username: "for-sonifi",
2476
+ password: "sonifi-mqtt",
2477
+ topicPrefix: "adstorm/players",
2478
+ qos: 1
2479
+ };
2480
+ var mqttConfig = _object_spread({}, DEFAULT_MQTT_CONFIG);
2481
+ function isMQTTEnabled() {
2482
+ return mqttConfig.enabled;
2483
+ }
2484
+ function buildMQTTBrokerUrl() {
2485
+ if (mqttConfig.brokerUrl) return mqttConfig.brokerUrl;
2486
+ return "wss://".concat(mqttConfig.brokerAddress, ":").concat(mqttConfig.wsPort, "/mqtt");
2487
+ }
2488
+ function buildPlayerTopic(licenseKey, channel) {
2489
+ return "".concat(mqttConfig.topicPrefix, "/").concat(licenseKey, "/").concat(channel);
2490
+ }
2491
+ // src/utils/mqttClient.ts
2492
+ var import_mqtt = __toESM(require("mqtt"), 1);
2493
+ var LOG = "[StormcloudVideoPlayer][MQTT]";
2494
+ var client = null;
2495
+ var status = "disconnected";
2496
+ function initMQTTClient() {
2497
+ if (client || !isMQTTEnabled()) return;
2498
+ var url = buildMQTTBrokerUrl();
2499
+ status = "connecting";
2500
+ var clientId = "stormcloud-vp-".concat(Math.random().toString(36).slice(2, 9));
2501
+ try {
2502
+ client = import_mqtt.default.connect(url, {
2503
+ clientId: clientId,
2504
+ username: mqttConfig.username,
2505
+ password: mqttConfig.password,
2506
+ keepalive: 60,
2507
+ clean: true,
2508
+ reconnectPeriod: 5e3,
2509
+ connectTimeout: 1e4,
2510
+ queueQoSZero: false
2511
+ });
2512
+ } catch (err) {
2513
+ status = "error";
2514
+ console.warn("".concat(LOG, " connect() threw:"), err);
2515
+ return;
2516
+ }
2517
+ client.on("connect", function() {
2518
+ status = "connected";
2519
+ console.info("".concat(LOG, " connected to ").concat(url));
2520
+ });
2521
+ client.on("reconnect", function() {
2522
+ status = "connecting";
2523
+ console.info("".concat(LOG, " reconnecting…"));
2524
+ });
2525
+ client.on("offline", function() {
2526
+ status = "disconnected";
2527
+ console.warn("".concat(LOG, " offline"));
2528
+ });
2529
+ client.on("error", function(err) {
2530
+ status = "error";
2531
+ console.warn("".concat(LOG, " error:"), err.message);
2532
+ });
2533
+ client.on("close", function() {
2534
+ if (status === "connected") {
2535
+ status = "disconnected";
2536
+ }
2537
+ });
2538
+ }
2539
+ function ensureMQTTClient() {
2540
+ if (isMQTTEnabled() && !client) {
2541
+ initMQTTClient();
2542
+ }
2543
+ }
2544
+ function publishMQTT(topic, payload) {
2545
+ if (!isMQTTEnabled()) {
2546
+ return false;
2547
+ }
2548
+ ensureMQTTClient();
2549
+ if (!client) {
2550
+ return false;
2551
+ }
2552
+ try {
2553
+ client.publish(topic, JSON.stringify(payload), {
2554
+ qos: mqttConfig.qos
2555
+ });
2556
+ return true;
2557
+ } catch (err) {
2558
+ console.warn("".concat(LOG, " publish failed on ").concat(topic, ":"), err);
2559
+ return false;
2560
+ }
2561
+ }
2469
2562
  // src/utils/tracking.ts
2470
2563
  var cachedBrowserId = null;
2471
2564
  function getClientInfo() {
@@ -2612,7 +2705,7 @@ function getClientInfo() {
2612
2705
  }
2613
2706
  function getBrowserID(clientInfo) {
2614
2707
  return _async_to_generator(function() {
2615
- var fingerprintString, encodedData, utf8, buffer, i, hashBuffer, hashArray, hashHex, error, hash, i1, char, fallbackHash, timestamp, random;
2708
+ var _crypto_subtle, fingerprintString, encodedData, utf8, buffer, i, hashBuffer, hashHex, unused, hash, i1, char, fallbackHash, timestamp, random;
2616
2709
  return _ts_generator(this, function(_state) {
2617
2710
  switch(_state.label){
2618
2711
  case 0:
@@ -2623,7 +2716,7 @@ function getBrowserID(clientInfo) {
2623
2716
  ];
2624
2717
  }
2625
2718
  fingerprintString = JSON.stringify(clientInfo);
2626
- if (!(typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.digest)) return [
2719
+ if (!(typeof crypto !== "undefined" && ((_crypto_subtle = crypto.subtle) === null || _crypto_subtle === void 0 ? void 0 : _crypto_subtle.digest))) return [
2627
2720
  3,
2628
2721
  5
2629
2722
  ];
@@ -2661,8 +2754,7 @@ function getBrowserID(clientInfo) {
2661
2754
  ];
2662
2755
  case 3:
2663
2756
  hashBuffer = _state.sent();
2664
- hashArray = Array.from(new Uint8Array(hashBuffer));
2665
- hashHex = hashArray.map(function(b) {
2757
+ hashHex = Array.from(new Uint8Array(hashBuffer)).map(function(b) {
2666
2758
  return b.toString(16).padStart(2, "0");
2667
2759
  }).join("");
2668
2760
  cachedBrowserId = hashHex;
@@ -2671,8 +2763,8 @@ function getBrowserID(clientInfo) {
2671
2763
  hashHex
2672
2764
  ];
2673
2765
  case 4:
2674
- error = _state.sent();
2675
- console.warn("[StormcloudVideoPlayer] crypto.subtle.digest not supported, using fallback hash");
2766
+ unused = _state.sent();
2767
+ console.warn("[StormcloudVideoPlayer] crypto.subtle not supported, using fallback hash");
2676
2768
  return [
2677
2769
  3,
2678
2770
  5
@@ -2696,177 +2788,91 @@ function getBrowserID(clientInfo) {
2696
2788
  });
2697
2789
  })();
2698
2790
  }
2699
- var PLAYER_TRACKING_BASE_URL = "https://adstorm.co/api-adstorm-dev/adstorm/player-tracking";
2700
- var TRACK_URL = "".concat(PLAYER_TRACKING_BASE_URL, "/metrics/ingest");
2701
- var HEARTBEAT_URL = "".concat(PLAYER_TRACKING_BASE_URL, "/heartbeat");
2702
- var IMPRESSIONS_URL = "".concat(PLAYER_TRACKING_BASE_URL, "/impressions/ingest");
2703
- function buildHeaders(licenseKey) {
2704
- var headers = {
2705
- "Content-Type": "application/json"
2706
- };
2707
- if (licenseKey) {
2708
- headers["Authorization"] = "Bearer ".concat(licenseKey);
2709
- }
2710
- return headers;
2711
- }
2712
- function sendTrackRequest(licenseKey, body) {
2713
- return _async_to_generator(function() {
2714
- var response;
2715
- return _ts_generator(this, function(_state) {
2716
- switch(_state.label){
2717
- case 0:
2718
- return [
2719
- 4,
2720
- fetch(TRACK_URL, {
2721
- method: "POST",
2722
- headers: buildHeaders(licenseKey),
2723
- body: JSON.stringify(body)
2724
- })
2725
- ];
2726
- case 1:
2727
- response = _state.sent();
2728
- if (!response.ok) {
2729
- throw new Error("HTTP error! status: ".concat(response.status));
2730
- }
2731
- return [
2732
- 4,
2733
- response.json()
2734
- ];
2735
- case 2:
2736
- _state.sent();
2737
- return [
2738
- 2
2739
- ];
2740
- }
2741
- });
2742
- })();
2791
+ function canPublish(licenseKey) {
2792
+ return Boolean(isMQTTEnabled() && licenseKey);
2743
2793
  }
2744
- function postJson(url, licenseKey, body) {
2794
+ function buildPlayerMetricEvent() {
2745
2795
  return _async_to_generator(function() {
2746
- var response;
2747
- return _ts_generator(this, function(_state) {
2748
- switch(_state.label){
2749
- case 0:
2750
- return [
2751
- 4,
2752
- fetch(url, {
2753
- method: "POST",
2754
- headers: buildHeaders(licenseKey),
2755
- body: JSON.stringify(body)
2756
- })
2757
- ];
2758
- case 1:
2759
- response = _state.sent();
2760
- if (!response.ok) {
2761
- throw new Error("HTTP error! status: ".concat(response.status));
2762
- }
2763
- return [
2764
- 4,
2765
- response.json()
2766
- ];
2767
- case 2:
2768
- _state.sent();
2769
- return [
2770
- 2
2771
- ];
2772
- }
2773
- });
2774
- })();
2775
- }
2776
- function buildPlayerMetricEvent(_0) {
2777
- return _async_to_generator(function(licenseKey) {
2778
- var context, flags, _flags_captureAt, clientInfo, browserId, captureAt;
2796
+ var context, flags, _flags_captureAt, _flags_adLoaded, _flags_adDetect, clientInfo, playerId, captureAt;
2779
2797
  var _arguments = arguments;
2780
2798
  return _ts_generator(this, function(_state) {
2781
2799
  switch(_state.label){
2782
2800
  case 0:
2783
- context = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {}, flags = _arguments.length > 2 && _arguments[2] !== void 0 ? _arguments[2] : {};
2801
+ context = _arguments.length > 0 && _arguments[0] !== void 0 ? _arguments[0] : {}, flags = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
2784
2802
  clientInfo = getClientInfo();
2785
2803
  return [
2786
2804
  4,
2787
2805
  getBrowserID(clientInfo)
2788
2806
  ];
2789
2807
  case 1:
2790
- browserId = _state.sent();
2808
+ playerId = _state.sent();
2791
2809
  captureAt = (_flags_captureAt = flags.captureAt) !== null && _flags_captureAt !== void 0 ? _flags_captureAt : /* @__PURE__ */ new Date().toISOString();
2792
2810
  return [
2793
2811
  2,
2794
- {
2795
- player_id: browserId,
2796
- browserId: browserId,
2812
+ _object_spread({
2813
+ player_id: playerId,
2797
2814
  device_type: clientInfo.deviceType,
2798
- deviceType: clientInfo.deviceType,
2799
- input_stream_type: context.inputStreamType,
2800
- os: clientInfo.os,
2801
- ad_loaded: flags.adLoaded,
2802
- ad_detect: flags.adDetect,
2803
- license_key: licenseKey,
2804
- capture_at: captureAt,
2805
- timestamp: captureAt
2806
- }
2815
+ os: clientInfo.os.toLowerCase(),
2816
+ ad_loaded: (_flags_adLoaded = flags.adLoaded) !== null && _flags_adLoaded !== void 0 ? _flags_adLoaded : false,
2817
+ ad_detect: (_flags_adDetect = flags.adDetect) !== null && _flags_adDetect !== void 0 ? _flags_adDetect : false,
2818
+ capture_at: captureAt
2819
+ }, context.inputStreamType ? {
2820
+ input_stream_type: context.inputStreamType
2821
+ } : {})
2807
2822
  ];
2808
2823
  }
2809
2824
  });
2810
2825
  }).apply(this, arguments);
2811
2826
  }
2827
+ function publishTracking(licenseKey, channel, body) {
2828
+ ensureMQTTClient();
2829
+ publishMQTT(buildPlayerTopic(licenseKey, channel), body);
2830
+ }
2812
2831
  function sendInitialTracking(_0) {
2813
2832
  return _async_to_generator(function(licenseKey) {
2814
- var context, clientInfo, browserId, trackingData, error;
2833
+ var context, metricEvent, error;
2815
2834
  var _arguments = arguments;
2816
2835
  return _ts_generator(this, function(_state) {
2817
2836
  switch(_state.label){
2818
2837
  case 0:
2819
2838
  context = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
2839
+ if (!canPublish(licenseKey)) return [
2840
+ 2
2841
+ ];
2820
2842
  _state.label = 1;
2821
2843
  case 1:
2822
2844
  _state.trys.push([
2823
2845
  1,
2824
- 4,
2846
+ 3,
2825
2847
  ,
2826
- 5
2848
+ 4
2827
2849
  ]);
2828
- clientInfo = getClientInfo();
2829
2850
  return [
2830
2851
  4,
2831
- getBrowserID(clientInfo)
2832
- ];
2833
- case 2:
2834
- browserId = _state.sent();
2835
- trackingData = _object_spread({
2836
- browserId: browserId
2837
- }, clientInfo);
2838
- return [
2839
- 4,
2840
- sendTrackRequest(licenseKey, {
2841
- events: [
2842
- {
2843
- player_id: browserId,
2844
- device_type: clientInfo.deviceType,
2845
- input_stream_type: context.inputStreamType,
2846
- os: clientInfo.os,
2847
- ad_loaded: false,
2848
- ad_detect: false,
2849
- license_key: licenseKey,
2850
- capture_at: /* @__PURE__ */ new Date().toISOString()
2851
- }
2852
- ],
2853
- trackingData: trackingData
2852
+ buildPlayerMetricEvent(context, {
2853
+ adLoaded: false,
2854
+ adDetect: false
2854
2855
  })
2855
2856
  ];
2856
- case 3:
2857
- _state.sent();
2857
+ case 2:
2858
+ metricEvent = _state.sent();
2859
+ publishTracking(licenseKey, "metrics", {
2860
+ events: [
2861
+ metricEvent
2862
+ ]
2863
+ });
2858
2864
  return [
2859
2865
  3,
2860
- 5
2866
+ 4
2861
2867
  ];
2862
- case 4:
2868
+ case 3:
2863
2869
  error = _state.sent();
2864
2870
  console.error("[StormcloudVideoPlayer] Error sending initial tracking data:", error);
2865
2871
  return [
2866
2872
  3,
2867
- 5
2873
+ 4
2868
2874
  ];
2869
- case 5:
2875
+ case 4:
2870
2876
  return [
2871
2877
  2
2872
2878
  ];
@@ -2970,53 +2976,48 @@ function sendAdImpressionTracking(_0, _1) {
2970
2976
  switch(_state.label){
2971
2977
  case 0:
2972
2978
  context = _arguments.length > 2 && _arguments[2] !== void 0 ? _arguments[2] : {};
2979
+ if (!canPublish(licenseKey)) return [
2980
+ 2
2981
+ ];
2973
2982
  _state.label = 1;
2974
2983
  case 1:
2975
2984
  _state.trys.push([
2976
2985
  1,
2977
- 4,
2986
+ 3,
2978
2987
  ,
2979
- 5
2988
+ 4
2980
2989
  ]);
2981
2990
  return [
2982
2991
  4,
2983
- buildPlayerMetricEvent(licenseKey, context, {
2992
+ buildPlayerMetricEvent(context, {
2984
2993
  captureAt: adImpressionInfo.timestamp
2985
2994
  })
2986
2995
  ];
2987
2996
  case 2:
2988
2997
  metricEvent = _state.sent();
2989
- return [
2990
- 4,
2991
- Promise.all([
2992
- postJson(HEARTBEAT_URL, licenseKey, metricEvent),
2993
- postJson(IMPRESSIONS_URL, licenseKey, {
2994
- events: [
2995
- {
2996
- player_id: metricEvent.player_id,
2997
- ad_played_count: 1,
2998
- ad_url: adImpressionInfo.adUrl,
2999
- license_key: licenseKey,
3000
- capture_at: adImpressionInfo.timestamp
3001
- }
3002
- ]
3003
- })
3004
- ])
3005
- ];
3006
- case 3:
3007
- _state.sent();
2998
+ publishTracking(licenseKey, "heartbeat", metricEvent);
2999
+ publishTracking(licenseKey, "impressions", {
3000
+ events: [
3001
+ {
3002
+ player_id: metricEvent.player_id,
3003
+ ad_played_count: 1,
3004
+ ad_url: adImpressionInfo.adUrl,
3005
+ capture_at: adImpressionInfo.timestamp
3006
+ }
3007
+ ]
3008
+ });
3008
3009
  return [
3009
3010
  3,
3010
- 5
3011
+ 4
3011
3012
  ];
3012
- case 4:
3013
+ case 3:
3013
3014
  error = _state.sent();
3014
3015
  console.error("[StormcloudVideoPlayer] Error sending ad impression tracking:", error);
3015
3016
  return [
3016
3017
  3,
3017
- 5
3018
+ 4
3018
3019
  ];
3019
- case 5:
3020
+ case 4:
3020
3021
  return [
3021
3022
  2
3022
3023
  ];
@@ -3032,38 +3033,36 @@ function sendHeartbeat(_0) {
3032
3033
  switch(_state.label){
3033
3034
  case 0:
3034
3035
  context = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {}, flags = _arguments.length > 2 && _arguments[2] !== void 0 ? _arguments[2] : {};
3036
+ if (!canPublish(licenseKey)) return [
3037
+ 2
3038
+ ];
3035
3039
  _state.label = 1;
3036
3040
  case 1:
3037
3041
  _state.trys.push([
3038
3042
  1,
3039
- 4,
3043
+ 3,
3040
3044
  ,
3041
- 5
3045
+ 4
3042
3046
  ]);
3043
3047
  return [
3044
3048
  4,
3045
- buildPlayerMetricEvent(licenseKey, context, flags)
3049
+ buildPlayerMetricEvent(context, flags)
3046
3050
  ];
3047
3051
  case 2:
3048
3052
  heartbeatData = _state.sent();
3049
- return [
3050
- 4,
3051
- postJson(HEARTBEAT_URL, licenseKey, heartbeatData)
3052
- ];
3053
- case 3:
3054
- _state.sent();
3053
+ publishTracking(licenseKey, "heartbeat", heartbeatData);
3055
3054
  return [
3056
3055
  3,
3057
- 5
3056
+ 4
3058
3057
  ];
3059
- case 4:
3058
+ case 3:
3060
3059
  error = _state.sent();
3061
3060
  console.error("[StormcloudVideoPlayer] Error sending heartbeat:", error);
3062
3061
  return [
3063
3062
  3,
3064
- 5
3063
+ 4
3065
3064
  ];
3066
- case 5:
3065
+ case 4:
3067
3066
  return [
3068
3067
  2
3069
3068
  ];
@@ -3528,7 +3527,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
3528
3527
  var _level_details, _level_details1;
3529
3528
  return (level === null || level === void 0 ? void 0 : (_level_details = level.details) === null || _level_details === void 0 ? void 0 : _level_details.live) === true || (level === null || level === void 0 ? void 0 : (_level_details1 = level.details) === null || _level_details1 === void 0 ? void 0 : _level_details1.type) === "LIVE";
3530
3529
  })) !== null && _ref !== void 0 ? _ref : false;
3531
- if (!this.isLiveStream && this.vmapBreaks.length === 0 && this.apiVastTagUrl) {
3530
+ if (!this.isVmapEnabled() && !this.isLiveStream && this.vmapBreaks.length === 0 && this.apiVastTagUrl) {
3532
3531
  prerollKey = "synthetic-vod-preroll";
3533
3532
  if (!this.consumedVmapBreakIds.has(prerollKey)) {
3534
3533
  this.vmapBreaks = [
@@ -4111,6 +4110,10 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
4111
4110
  _this.onTimeUpdate(_this.video.currentTime);
4112
4111
  };
4113
4112
  this.video.addEventListener("timeupdate", this.timeUpdateHandler);
4113
+ this.endedHandler = function() {
4114
+ _this.onVideoEnded();
4115
+ };
4116
+ this.video.addEventListener("ended", this.endedHandler);
4114
4117
  this.emptiedHandler = function() {
4115
4118
  if (_this.nativeHlsMode && _this.videoSrcProtection && !_this.ima.isAdPlaying()) {
4116
4119
  if (_this.config.debugAdTiming) {
@@ -5422,6 +5425,13 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5422
5425
  }
5423
5426
  }
5424
5427
  },
5428
+ {
5429
+ key: "isVmapEnabled",
5430
+ value: function isVmapEnabled() {
5431
+ var _this_config_vmapUrl;
5432
+ return !!(this.config.isVmap && ((_this_config_vmapUrl = this.config.vmapUrl) === null || _this_config_vmapUrl === void 0 ? void 0 : _this_config_vmapUrl.trim()));
5433
+ }
5434
+ },
5425
5435
  {
5426
5436
  key: "fetchAdConfiguration",
5427
5437
  value: function fetchAdConfiguration() {
@@ -5430,7 +5440,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5430
5440
  return _ts_generator(this, function(_state) {
5431
5441
  switch(_state.label){
5432
5442
  case 0:
5433
- if (!this.config.vmapUrl) return [
5443
+ if (!this.isVmapEnabled()) return [
5434
5444
  3,
5435
5445
  2
5436
5446
  ];
@@ -5440,7 +5450,12 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5440
5450
  ];
5441
5451
  case 1:
5442
5452
  _state.sent();
5443
- _state.label = 2;
5453
+ if (this.config.debugAdTiming) {
5454
+ console.log("[StormcloudVideoPlayer] VMAP mode enabled");
5455
+ }
5456
+ return [
5457
+ 2
5458
+ ];
5444
5459
  case 2:
5445
5460
  vastMode = this.config.vastMode || "default";
5446
5461
  if (this.config.debugAdTiming) {
@@ -5608,7 +5623,16 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5608
5623
  }
5609
5624
  return [];
5610
5625
  }
5611
- var adBreakNodes = Array.from(doc.querySelectorAll("AdBreak, vmap\\:AdBreak"));
5626
+ var VMAP_NS = "http://www.iab.net/videosuite/vmap";
5627
+ var adBreakNodes = Array.from(doc.getElementsByTagNameNS(VMAP_NS, "AdBreak"));
5628
+ if (adBreakNodes.length === 0) {
5629
+ adBreakNodes = Array.from(doc.querySelectorAll("AdBreak, vmap\\:AdBreak"));
5630
+ }
5631
+ if (adBreakNodes.length === 0) {
5632
+ adBreakNodes = Array.from(doc.getElementsByTagName("*")).filter(function(el) {
5633
+ return el.localName === "AdBreak";
5634
+ });
5635
+ }
5612
5636
  var parsed = [];
5613
5637
  adBreakNodes.forEach(function(node, index) {
5614
5638
  var timeOffsetRaw = (node.getAttribute("timeOffset") || "").trim();
@@ -5616,8 +5640,17 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5616
5640
  if (startTimeMs == null) {
5617
5641
  return;
5618
5642
  }
5619
- var adTagNode = node.querySelector("AdTagURI, vmap\\:AdTagURI");
5620
- var adTagUrl = ((adTagNode === null || adTagNode === void 0 ? void 0 : adTagNode.textContent) || "").trim();
5643
+ var adTagNode = node.getElementsByTagNameNS(VMAP_NS, "AdTagURI")[0];
5644
+ if (!adTagNode) {
5645
+ var _node_querySelector;
5646
+ adTagNode = (_node_querySelector = node.querySelector("AdTagURI, vmap\\:AdTagURI")) !== null && _node_querySelector !== void 0 ? _node_querySelector : void 0;
5647
+ }
5648
+ if (!adTagNode) {
5649
+ adTagNode = Array.from(node.getElementsByTagName("*")).find(function(el) {
5650
+ return el.localName === "AdTagURI";
5651
+ });
5652
+ }
5653
+ var adTagUrl = _this.resolveVmapAdTagUrl(((adTagNode === null || adTagNode === void 0 ? void 0 : adTagNode.textContent) || "").trim());
5621
5654
  if (!adTagUrl) {
5622
5655
  return;
5623
5656
  }
@@ -5636,6 +5669,15 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5636
5669
  return parsed;
5637
5670
  }
5638
5671
  },
5672
+ {
5673
+ key: "resolveVmapAdTagUrl",
5674
+ value: function resolveVmapAdTagUrl(url) {
5675
+ if (!url) {
5676
+ return "";
5677
+ }
5678
+ return url.replace(/\[timestamp\]/gi, String(Date.now())).replace(/\$\{GDPR\}/gi, "0").trim();
5679
+ }
5680
+ },
5639
5681
  {
5640
5682
  key: "parseVmapTimeOffsetToMs",
5641
5683
  value: function parseVmapTimeOffsetToMs(timeOffset) {
@@ -5658,6 +5700,14 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5658
5700
  var millis = Number(ms.padEnd(3, "0").slice(0, 3));
5659
5701
  return (hours * 3600 + minutes * 60 + seconds) * 1e3 + millis;
5660
5702
  }
5703
+ var msOnly = timeOffset.match(/^(\d{1,2}):(\d{2})(?:\.(\d{1,3}))?$/);
5704
+ if (msOnly) {
5705
+ var _msOnly = _sliced_to_array(msOnly, 4), mm1 = _msOnly[1], ss1 = _msOnly[2], tmp1 = _msOnly[3], ms1 = tmp1 === void 0 ? "0" : tmp1;
5706
+ var minutes1 = Number(mm1);
5707
+ var seconds1 = Number(ss1);
5708
+ var millis1 = Number(ms1.padEnd(3, "0").slice(0, 3));
5709
+ return (minutes1 * 60 + seconds1) * 1e3 + millis1;
5710
+ }
5661
5711
  var percent = timeOffset.match(/^(\d+(?:\.\d+)?)%$/);
5662
5712
  if (percent) {
5663
5713
  var ratio = Number(percent[1]) / 100;
@@ -7173,23 +7223,49 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
7173
7223
  key: "onTimeUpdate",
7174
7224
  value: function onTimeUpdate(currentTimeSec) {
7175
7225
  var _this = this;
7226
+ if (!this.isVmapEnabled() || this.vmapBreaks.length === 0) {
7227
+ return;
7228
+ }
7176
7229
  if (this.ima.isAdPlaying() || this.inAdBreak) return;
7177
7230
  var nowMs = currentTimeSec * 1e3;
7178
7231
  var breakToPlay = this.findBreakForTime(nowMs);
7179
7232
  if (breakToPlay) {
7180
- void this.handleMidAdJoin(breakToPlay, nowMs).catch(function(error) {
7233
+ void this.handleVmapAdBreak(breakToPlay, nowMs).catch(function(error) {
7181
7234
  if (_this.config.debugAdTiming) {
7182
- console.warn("[StormcloudVideoPlayer] Mid-roll VMAP join failed gracefully:", error);
7235
+ console.warn("[StormcloudVideoPlayer] VMAP ad break failed gracefully:", error);
7183
7236
  }
7184
7237
  });
7185
7238
  }
7186
7239
  }
7187
7240
  },
7188
7241
  {
7189
- key: "handleMidAdJoin",
7190
- value: function handleMidAdJoin(adBreak, nowMs) {
7242
+ key: "onVideoEnded",
7243
+ value: function onVideoEnded() {
7244
+ var _this = this;
7245
+ if (!this.isVmapEnabled() || this.vmapBreaks.length === 0) {
7246
+ return;
7247
+ }
7248
+ if (this.ima.isAdPlaying() || this.inAdBreak) {
7249
+ return;
7250
+ }
7251
+ var durationMs = Number.isFinite(this.video.duration) ? Math.floor(this.video.duration * 1e3) : 0;
7252
+ var postroll = this.vmapBreaks.find(function(b) {
7253
+ return b.startTimeMs === -1 && !_this.consumedVmapBreakIds.has(_this.getAdBreakKey(b));
7254
+ });
7255
+ if (postroll) {
7256
+ void this.handleVmapAdBreak(postroll, durationMs).catch(function(error) {
7257
+ if (_this.config.debugAdTiming) {
7258
+ console.warn("[StormcloudVideoPlayer] VMAP post-roll failed gracefully:", error);
7259
+ }
7260
+ });
7261
+ }
7262
+ }
7263
+ },
7264
+ {
7265
+ key: "handleVmapAdBreak",
7266
+ value: function handleVmapAdBreak(adBreak, nowMs) {
7191
7267
  return _async_to_generator(function() {
7192
- var _adBreak_durationMs, _this_config_driftToleranceMs, key, breakStartMs, durationMs, endMs, tol, inWindow, remainingMs, tags, first, rest, error;
7268
+ var _adBreak_durationMs, key, breakStartMs, durationMs, endMs, inWindow, tags, first, rest, error;
7193
7269
  return _ts_generator(this, function(_state) {
7194
7270
  switch(_state.label){
7195
7271
  case 0:
@@ -7207,25 +7283,28 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
7207
7283
  }
7208
7284
  durationMs = (_adBreak_durationMs = adBreak.durationMs) !== null && _adBreak_durationMs !== void 0 ? _adBreak_durationMs : 0;
7209
7285
  endMs = breakStartMs + durationMs;
7210
- tol = (_this_config_driftToleranceMs = this.config.driftToleranceMs) !== null && _this_config_driftToleranceMs !== void 0 ? _this_config_driftToleranceMs : 1e3;
7211
- inWindow = durationMs > 0 ? nowMs > breakStartMs && nowMs < endMs : nowMs + tol >= breakStartMs;
7286
+ inWindow = durationMs > 0 ? nowMs >= breakStartMs && nowMs < endMs : nowMs >= breakStartMs;
7212
7287
  if (!inWindow) return [
7213
7288
  3,
7214
7289
  4
7215
7290
  ];
7216
7291
  this.consumedVmapBreakIds.add(key);
7217
- remainingMs = durationMs > 0 ? Math.max(0, endMs - nowMs) : 0;
7218
- tags = this.selectVastTagsForBreak(adBreak) || (this.apiVastTagUrl ? [
7219
- this.apiVastTagUrl
7220
- ] : void 0);
7221
- if (!(tags && tags.length > 0)) return [
7222
- 3,
7223
- 4
7224
- ];
7292
+ tags = this.selectVastTagsForBreak(adBreak);
7293
+ if (!tags || tags.length === 0) {
7294
+ return [
7295
+ 2
7296
+ ];
7297
+ }
7225
7298
  first = tags[0];
7226
7299
  rest = tags.slice(1);
7227
7300
  this.adPodQueue = rest;
7228
7301
  this.ima.updateOriginalMutedState(this.video.muted, this.video.volume);
7302
+ this.showAds = true;
7303
+ this.inAdBreak = true;
7304
+ this.currentAdBreakStartWallClockMs = Date.now();
7305
+ if (!this.video.paused) {
7306
+ this.video.pause();
7307
+ }
7229
7308
  _state.label = 1;
7230
7309
  case 1:
7231
7310
  _state.trys.push([
@@ -7240,10 +7319,6 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
7240
7319
  ];
7241
7320
  case 2:
7242
7321
  _state.sent();
7243
- this.inAdBreak = true;
7244
- this.expectedAdBreakDurationMs = remainingMs;
7245
- this.currentAdBreakStartWallClockMs = Date.now();
7246
- this.scheduleAdStopCountdown(remainingMs);
7247
7322
  return [
7248
7323
  3,
7249
7324
  4
@@ -7251,8 +7326,10 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
7251
7326
  case 3:
7252
7327
  error = _state.sent();
7253
7328
  this.adPodQueue = [];
7329
+ this.inAdBreak = false;
7330
+ this.showAds = false;
7254
7331
  if (this.config.debugAdTiming) {
7255
- console.warn("[StormcloudVideoPlayer] Mid-roll VMAP ad request failed:", error);
7332
+ console.warn("[StormcloudVideoPlayer] VMAP ad request failed:", error);
7256
7333
  }
7257
7334
  return [
7258
7335
  3,
@@ -7923,16 +8000,18 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
7923
8000
  {
7924
8001
  key: "selectVastTagsForBreak",
7925
8002
  value: function selectVastTagsForBreak(b) {
8003
+ var _this = this;
7926
8004
  if (!b || !b.vastTagUrl) return void 0;
7927
- if (b.vastTagUrl.includes(",")) {
7928
- return b.vastTagUrl.split(",").map(function(s) {
7929
- return s.trim();
8005
+ var resolvedUrl = this.resolveVmapAdTagUrl(b.vastTagUrl);
8006
+ if (resolvedUrl.includes(",")) {
8007
+ return resolvedUrl.split(",").map(function(s) {
8008
+ return _this.resolveVmapAdTagUrl(s.trim());
7930
8009
  }).filter(function(s) {
7931
8010
  return s.length > 0;
7932
8011
  });
7933
8012
  }
7934
8013
  return [
7935
- b.vastTagUrl
8014
+ resolvedUrl
7936
8015
  ];
7937
8016
  }
7938
8017
  },
@@ -7964,9 +8043,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
7964
8043
  {
7965
8044
  key: "findBreakForTime",
7966
8045
  value: function findBreakForTime(nowMs) {
7967
- var _this_config_driftToleranceMs;
7968
8046
  var schedule = this.vmapBreaks;
7969
- var tol = (_this_config_driftToleranceMs = this.config.driftToleranceMs) !== null && _this_config_driftToleranceMs !== void 0 ? _this_config_driftToleranceMs : 1e3;
7970
8047
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
7971
8048
  try {
7972
8049
  for(var _iterator = schedule[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
@@ -7978,9 +8055,14 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
7978
8055
  if (breakStartMs == null) {
7979
8056
  continue;
7980
8057
  }
7981
- var end = breakStartMs + (b.durationMs || 0);
7982
- var effectiveTol = breakStartMs === 0 ? Math.max(tol, 3e4) : tol;
7983
- if (nowMs >= breakStartMs && (b.durationMs ? nowMs < end : nowMs <= breakStartMs + effectiveTol)) {
8058
+ if (b.durationMs) {
8059
+ var end = breakStartMs + b.durationMs;
8060
+ if (nowMs >= breakStartMs && nowMs < end) {
8061
+ return b;
8062
+ }
8063
+ continue;
8064
+ }
8065
+ if (nowMs >= breakStartMs) {
7984
8066
  return b;
7985
8067
  }
7986
8068
  }
@@ -8188,6 +8270,10 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
8188
8270
  this.video.removeEventListener("timeupdate", this.timeUpdateHandler);
8189
8271
  delete this.timeUpdateHandler;
8190
8272
  }
8273
+ if (this.endedHandler) {
8274
+ this.video.removeEventListener("ended", this.endedHandler);
8275
+ delete this.endedHandler;
8276
+ }
8191
8277
  if (this.emptiedHandler) {
8192
8278
  this.video.removeEventListener("emptied", this.emptiedHandler);
8193
8279
  delete this.emptiedHandler;