stormcloud-video-player 0.8.49 → 0.8.51

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.
@@ -198,6 +198,150 @@ __export(hlsAdPlayer_exports, {
198
198
  });
199
199
  module.exports = __toCommonJS(hlsAdPlayer_exports);
200
200
  var import_hls = __toESM(require("hls.js"), 1);
201
+ // src/utils/browserCompat.ts
202
+ function getChromeVersion(ua) {
203
+ var match = ua.match(/Chrome\/(\d+)/);
204
+ return match && match[1] ? parseInt(match[1], 10) : 0;
205
+ }
206
+ function getWebKitVersion(ua) {
207
+ var match = ua.match(/AppleWebKit\/(\d+)/);
208
+ return match && match[1] ? parseInt(match[1], 10) : 0;
209
+ }
210
+ function getPlatform() {
211
+ var _navigator_userAgentData;
212
+ if ("userAgentData" in navigator && ((_navigator_userAgentData = navigator.userAgentData) === null || _navigator_userAgentData === void 0 ? void 0 : _navigator_userAgentData.platform)) {
213
+ return navigator.userAgentData.platform;
214
+ }
215
+ var ua = navigator.userAgent;
216
+ if (/Mac|iPhone|iPad|iPod/i.test(ua)) {
217
+ return /iPhone|iPad|iPod/i.test(ua) ? "iPhone" : "MacIntel";
218
+ }
219
+ if (/Win/i.test(ua)) {
220
+ return "Win32";
221
+ }
222
+ if (/Linux/i.test(ua)) {
223
+ return /Android/i.test(ua) ? "Linux armv8l" : "Linux x86_64";
224
+ }
225
+ if (/CrOS/i.test(ua)) {
226
+ return "CrOS";
227
+ }
228
+ return navigator.platform || "Unknown";
229
+ }
230
+ function detectBrowser() {
231
+ var ua = navigator.userAgent;
232
+ var platform = getPlatform();
233
+ var name = "Unknown";
234
+ var version = "0";
235
+ var majorVersion = 0;
236
+ var isSmartTV = false;
237
+ var isLegacyTV = false;
238
+ var supportsModernJS = true;
239
+ var webOSVersion;
240
+ var tizenVersion;
241
+ var chromeVersionNum;
242
+ var chromeVersion = getChromeVersion(ua);
243
+ var webkitVersion = getWebKitVersion(ua);
244
+ chromeVersionNum = chromeVersion > 0 ? chromeVersion : void 0;
245
+ if (/Web0S|webOS|LG Browser|LGSTB/i.test(ua)) {
246
+ name = "LG WebOS";
247
+ isSmartTV = true;
248
+ var match = ua.match(/Web0S[/\s]*([\d.]+)/i) || ua.match(/webOS[/\s]*([\d.]+)/i);
249
+ if (!match || !match[1]) {
250
+ match = ua.match(/webOSTV[/\s-]*([\d.]+)/i) || ua.match(/webOS\.TV[/\s-]*([\d.]+)/i);
251
+ }
252
+ if (match && match[1]) {
253
+ version = match[1];
254
+ var parts = version.split(".");
255
+ majorVersion = parts[0] ? parseInt(parts[0], 10) : 0;
256
+ webOSVersion = majorVersion;
257
+ } else if (chromeVersion > 0) {
258
+ if (chromeVersion >= 79) {
259
+ webOSVersion = 6;
260
+ version = "6.0";
261
+ majorVersion = 6;
262
+ } else if (chromeVersion >= 68) {
263
+ webOSVersion = 5;
264
+ version = "5.0";
265
+ majorVersion = 5;
266
+ } else if (chromeVersion >= 53) {
267
+ webOSVersion = 4;
268
+ version = "4.0";
269
+ majorVersion = 4;
270
+ } else if (chromeVersion >= 38) {
271
+ webOSVersion = 3;
272
+ version = "3.0";
273
+ majorVersion = 3;
274
+ } else {
275
+ webOSVersion = 2;
276
+ version = "2.0";
277
+ majorVersion = 2;
278
+ }
279
+ } else {
280
+ version = "Unknown";
281
+ webOSVersion = void 0;
282
+ }
283
+ isLegacyTV = webOSVersion !== void 0 && webOSVersion < 3;
284
+ } else if (/Tizen/i.test(ua)) {
285
+ name = "Samsung Tizen";
286
+ isSmartTV = true;
287
+ var match1 = ua.match(/Tizen[/\s]*([\d.]+)/i);
288
+ version = match1 && match1[1] ? match1[1] : "Unknown";
289
+ if (version !== "Unknown") {
290
+ var parts1 = version.split(".");
291
+ majorVersion = parts1[0] ? parseInt(parts1[0], 10) : 0;
292
+ tizenVersion = majorVersion;
293
+ }
294
+ isLegacyTV = tizenVersion !== void 0 && tizenVersion < 3;
295
+ } else if (/SMART-TV|SmartTV/i.test(ua)) {
296
+ name = "Smart TV";
297
+ isSmartTV = true;
298
+ isLegacyTV = chromeVersion > 0 && chromeVersion < 53;
299
+ } else if (/NetCast/i.test(ua)) {
300
+ name = "LG NetCast";
301
+ isSmartTV = true;
302
+ isLegacyTV = true;
303
+ } else if (/BRAVIA/i.test(ua)) {
304
+ name = "Sony BRAVIA";
305
+ isSmartTV = true;
306
+ isLegacyTV = chromeVersion > 0 && chromeVersion < 53;
307
+ } else {
308
+ if (chromeVersion > 0) {
309
+ name = "Chrome";
310
+ version = chromeVersion.toString();
311
+ majorVersion = chromeVersion;
312
+ if (chromeVersion < 50) {
313
+ supportsModernJS = false;
314
+ }
315
+ }
316
+ if (webkitVersion > 0 && webkitVersion < 600) {
317
+ supportsModernJS = false;
318
+ }
319
+ }
320
+ if (typeof Promise === "undefined" || typeof Map === "undefined" || typeof Set === "undefined") {
321
+ supportsModernJS = false;
322
+ }
323
+ if (typeof URLSearchParams === "undefined") {
324
+ supportsModernJS = false;
325
+ }
326
+ return {
327
+ name: name,
328
+ version: version,
329
+ majorVersion: majorVersion,
330
+ isSmartTV: isSmartTV,
331
+ isLegacyTV: isLegacyTV,
332
+ platform: platform,
333
+ supportsModernJS: supportsModernJS,
334
+ webOSVersion: webOSVersion,
335
+ tizenVersion: tizenVersion,
336
+ chromeVersion: chromeVersionNum
337
+ };
338
+ }
339
+ function requiresMediaPipelineResetAfterAds() {
340
+ var _browser_tizenVersion;
341
+ var browser = detectBrowser();
342
+ return browser.name === "Samsung Tizen" && ((_browser_tizenVersion = browser.tizenVersion) !== null && _browser_tizenVersion !== void 0 ? _browser_tizenVersion : 0) >= 5;
343
+ }
344
+ // src/sdk/hlsAdPlayer.ts
201
345
  var MAX_VAST_WRAPPER_DEPTH = 5;
202
346
  function createHlsAdPlayer(contentVideo, options) {
203
347
  var _contentVideo_AD_VIDEO_PROP, _contentVideo_AD_CONTAINER_PROP;
@@ -209,6 +353,7 @@ function createHlsAdPlayer(contentVideo, options) {
209
353
  var mainHlsInstance = options === null || options === void 0 ? void 0 : options.mainHlsInstance;
210
354
  var AD_CONTAINER_PROP = "__stormcloudAdContainer";
211
355
  var AD_VIDEO_PROP = "__stormcloudAdVideo";
356
+ var FRAME_COUNTER_RELIABLE_PROP = "__stormcloudFrameCounterReliable";
212
357
  var adVideoElement = (_contentVideo_AD_VIDEO_PROP = contentVideo[AD_VIDEO_PROP]) !== null && _contentVideo_AD_VIDEO_PROP !== void 0 ? _contentVideo_AD_VIDEO_PROP : void 0;
213
358
  var adHls;
214
359
  var adContainerEl = (_contentVideo_AD_CONTAINER_PROP = contentVideo[AD_CONTAINER_PROP]) !== null && _contentVideo_AD_CONTAINER_PROP !== void 0 ? _contentVideo_AD_CONTAINER_PROP : void 0;
@@ -228,6 +373,8 @@ function createHlsAdPlayer(contentVideo, options) {
228
373
  var lastAdProgressTime = 0;
229
374
  var lastAdProgressPosition = 0;
230
375
  var sawDecodedVideoFrame = false;
376
+ var videoFrameCallbackHandle;
377
+ var pendingPlayPromise;
231
378
  var trackingFired = {
232
379
  impression: false,
233
380
  start: false,
@@ -1031,6 +1178,60 @@ function createHlsAdPlayer(contentVideo, options) {
1031
1178
  complete: false
1032
1179
  };
1033
1180
  }
1181
+ function isRvfcSupported(video) {
1182
+ return typeof video.requestVideoFrameCallback === "function";
1183
+ }
1184
+ function settlePendingPlay() {
1185
+ pendingPlayPromise = void 0;
1186
+ }
1187
+ function startAdPlayback(context) {
1188
+ if (!adVideoElement) return;
1189
+ var playPromise = adVideoElement.play();
1190
+ pendingPlayPromise = playPromise;
1191
+ playPromise.catch(function(error) {
1192
+ if (pendingPlayPromise !== playPromise) {
1193
+ return;
1194
+ }
1195
+ console.error("[HlsAdPlayer] Error starting ".concat(context, ":"), error);
1196
+ handleAdError();
1197
+ }).finally(function() {
1198
+ if (pendingPlayPromise === playPromise) {
1199
+ pendingPlayPromise = void 0;
1200
+ }
1201
+ });
1202
+ }
1203
+ function prepareVideoElementForNewSource() {
1204
+ settlePendingPlay();
1205
+ if (adHls) {
1206
+ adHls.destroy();
1207
+ adHls = void 0;
1208
+ }
1209
+ if (!adVideoElement) return;
1210
+ try {
1211
+ adVideoElement.pause();
1212
+ adVideoElement.removeAttribute("src");
1213
+ adVideoElement.load();
1214
+ } catch (error) {
1215
+ console.warn("[HlsAdPlayer] Error preparing ad video for new source:", error);
1216
+ }
1217
+ }
1218
+ function recreateAdVideoElement() {
1219
+ console.log("[HlsAdPlayer] Recreating ad video element (single-decoder platform)");
1220
+ teardownAdEventListeners();
1221
+ prepareVideoElementForNewSource();
1222
+ if (adVideoElement) {
1223
+ try {
1224
+ adVideoElement.remove();
1225
+ } catch (error) {
1226
+ console.warn("[HlsAdPlayer] Error removing ad video element:", error);
1227
+ }
1228
+ }
1229
+ adVideoElement = createAdVideoElement();
1230
+ adContainerEl === null || adContainerEl === void 0 ? void 0 : adContainerEl.appendChild(adVideoElement);
1231
+ contentVideo[AD_VIDEO_PROP] = adVideoElement;
1232
+ adVideoElement.volume = Math.max(0, Math.min(1, originalVolume));
1233
+ adVideoElement.muted = originalMutedState;
1234
+ }
1034
1235
  function loadCurrentAdMedia() {
1035
1236
  if (!adVideoElement || !currentAd) {
1036
1237
  throw new Error("No ad video element or ad to load");
@@ -1053,10 +1254,7 @@ function createHlsAdPlayer(contentVideo, options) {
1053
1254
  adHls.attachMedia(adVideoElement);
1054
1255
  adHls.on(import_hls.default.Events.MANIFEST_PARSED, function() {
1055
1256
  console.log("[HlsAdPlayer] HLS manifest parsed, starting playback");
1056
- adVideoElement.play().catch(function(error) {
1057
- console.error("[HlsAdPlayer] Error starting ad playback:", error);
1058
- handleAdError();
1059
- });
1257
+ startAdPlayback("HLS ad playback");
1060
1258
  });
1061
1259
  adHls.on(import_hls.default.Events.ERROR, function(_event, data) {
1062
1260
  console.error("[HlsAdPlayer] HLS error:", data);
@@ -1066,10 +1264,7 @@ function createHlsAdPlayer(contentVideo, options) {
1066
1264
  });
1067
1265
  } else if (isHlsAd && adVideoElement.canPlayType("application/vnd.apple.mpegurl")) {
1068
1266
  adVideoElement.src = mediaFile.url;
1069
- adVideoElement.play().catch(function(error) {
1070
- console.error("[HlsAdPlayer] Error starting ad playback:", error);
1071
- handleAdError();
1072
- });
1267
+ startAdPlayback("native HLS ad playback");
1073
1268
  } else if (!isHlsAd && isProgressiveMediaFile(mediaFile)) {
1074
1269
  if (adHls) {
1075
1270
  adHls.destroy();
@@ -1077,10 +1272,7 @@ function createHlsAdPlayer(contentVideo, options) {
1077
1272
  }
1078
1273
  adVideoElement.src = mediaFile.url;
1079
1274
  adVideoElement.load();
1080
- adVideoElement.play().catch(function(error) {
1081
- console.error("[HlsAdPlayer] Error starting progressive ad playback:", error);
1082
- handleAdError();
1083
- });
1275
+ startAdPlayback("progressive ad playback");
1084
1276
  } else {
1085
1277
  throw new Error("Unsupported ad media file type: ".concat(mediaFile.type));
1086
1278
  }
@@ -1097,6 +1289,13 @@ function createHlsAdPlayer(contentVideo, options) {
1097
1289
  trackingFired.impression = true;
1098
1290
  console.log("[HlsAdPlayer] Advancing to next pod ad (".concat(podIndex + 1, "/").concat(podAds.length, "): ").concat(currentAd.title, ", duration: ").concat(currentAd.duration, "s"));
1099
1291
  }
1292
+ clearStallWatchdog();
1293
+ if (requiresMediaPipelineResetAfterAds()) {
1294
+ recreateAdVideoElement();
1295
+ setupAdEventListeners();
1296
+ } else {
1297
+ prepareVideoElementForNewSource();
1298
+ }
1100
1299
  try {
1101
1300
  loadCurrentAdMedia();
1102
1301
  emit("pod_ad_started", {
@@ -1114,6 +1313,7 @@ function createHlsAdPlayer(contentVideo, options) {
1114
1313
  clearInterval(stallWatchdogId);
1115
1314
  stallWatchdogId = void 0;
1116
1315
  }
1316
+ cancelVideoFrameProbe();
1117
1317
  }
1118
1318
  function noteAdProgress() {
1119
1319
  lastAdProgressTime = Date.now();
@@ -1149,6 +1349,39 @@ function createHlsAdPlayer(contentVideo, options) {
1149
1349
  }
1150
1350
  return void 0;
1151
1351
  }
1352
+ function isFrameCounterReliable() {
1353
+ return contentVideo[FRAME_COUNTER_RELIABLE_PROP] === true;
1354
+ }
1355
+ function markFrameCounterReliable() {
1356
+ contentVideo[FRAME_COUNTER_RELIABLE_PROP] = true;
1357
+ }
1358
+ function cancelVideoFrameProbe() {
1359
+ if (videoFrameCallbackHandle != null && adVideoElement && typeof adVideoElement.cancelVideoFrameCallback === "function") {
1360
+ try {
1361
+ adVideoElement.cancelVideoFrameCallback(videoFrameCallbackHandle);
1362
+ } catch (error) {
1363
+ console.warn("[HlsAdPlayer] Error cancelling video frame callback:", error);
1364
+ }
1365
+ }
1366
+ videoFrameCallbackHandle = void 0;
1367
+ }
1368
+ function startVideoFrameProbe() {
1369
+ cancelVideoFrameProbe();
1370
+ if (!adVideoElement) return;
1371
+ var rvfc = adVideoElement.requestVideoFrameCallback;
1372
+ if (typeof rvfc !== "function") return;
1373
+ var onPresentedFrame = function onPresentedFrame() {
1374
+ videoFrameCallbackHandle = void 0;
1375
+ if (!adPlaying) return;
1376
+ sawDecodedVideoFrame = true;
1377
+ };
1378
+ try {
1379
+ videoFrameCallbackHandle = rvfc.call(adVideoElement, onPresentedFrame);
1380
+ } catch (error) {
1381
+ console.warn("[HlsAdPlayer] Error requesting video frame callback:", error);
1382
+ videoFrameCallbackHandle = void 0;
1383
+ }
1384
+ }
1152
1385
  function handleVideoDecodeFailure() {
1153
1386
  console.warn("[HlsAdPlayer] Ad audio is advancing but no video frames decoded (black screen) - skipping this creative");
1154
1387
  clearStallWatchdog();
@@ -1164,6 +1397,7 @@ function createHlsAdPlayer(contentVideo, options) {
1164
1397
  clearStallWatchdog();
1165
1398
  noteAdProgress();
1166
1399
  sawDecodedVideoFrame = false;
1400
+ startVideoFrameProbe();
1167
1401
  stallWatchdogId = window.setInterval(function() {
1168
1402
  if (!adPlaying || !adVideoElement) {
1169
1403
  return;
@@ -1172,9 +1406,17 @@ function createHlsAdPlayer(contentVideo, options) {
1172
1406
  var decodedFrames = getDecodedVideoFrameCount(adVideoElement);
1173
1407
  if (decodedFrames !== void 0 && decodedFrames > 0) {
1174
1408
  sawDecodedVideoFrame = true;
1175
- } else if (decodedFrames === 0 && adVideoElement.currentTime >= BLACKFRAME_MIN_PLAYBACK_S) {
1176
- handleVideoDecodeFailure();
1177
- return;
1409
+ markFrameCounterReliable();
1410
+ } else if (adVideoElement.currentTime >= BLACKFRAME_MIN_PLAYBACK_S) {
1411
+ var timeAdvancing = adVideoElement.currentTime > lastAdProgressPosition + 0.05;
1412
+ if (isRvfcSupported(adVideoElement) && timeAdvancing) {
1413
+ handleVideoDecodeFailure();
1414
+ return;
1415
+ }
1416
+ if (decodedFrames === 0 && timeAdvancing && isFrameCounterReliable()) {
1417
+ handleVideoDecodeFailure();
1418
+ return;
1419
+ }
1178
1420
  }
1179
1421
  }
1180
1422
  if (adVideoElement.currentTime > lastAdProgressPosition + 0.05) {
@@ -1191,19 +1433,7 @@ function createHlsAdPlayer(contentVideo, options) {
1191
1433
  }, STALL_CHECK_INTERVAL_MS);
1192
1434
  }
1193
1435
  function releaseAdDecoder() {
1194
- if (adHls) {
1195
- adHls.destroy();
1196
- adHls = void 0;
1197
- }
1198
- if (adVideoElement) {
1199
- try {
1200
- adVideoElement.pause();
1201
- adVideoElement.removeAttribute("src");
1202
- adVideoElement.load();
1203
- } catch (error) {
1204
- console.warn("[HlsAdPlayer] Error releasing ad decoder:", error);
1205
- }
1206
- }
1436
+ prepareVideoElementForNewSource();
1207
1437
  if (adContainerEl) {
1208
1438
  adContainerEl.style.display = "none";
1209
1439
  adContainerEl.style.pointerEvents = "none";