@mottosports/motto-video-player 1.0.1-rc.65 → 1.0.1-rc.67

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/dist/index.mjs CHANGED
@@ -1185,7 +1185,7 @@ var supportsWidevinePersistentLicenses = () => {
1185
1185
  import initShakaPlayerMux from "@mux/mux-data-shakaplayer";
1186
1186
 
1187
1187
  // package.json
1188
- var version = "1.0.1-rc.65";
1188
+ var version = "1.0.1-rc.67";
1189
1189
 
1190
1190
  // src/utils/licenseCache.ts
1191
1191
  var PERSISTENT_LICENSE_PREFIX = "motto_lic_";
@@ -1353,6 +1353,10 @@ var useShakaPlayer = ({
1353
1353
  const playerRef = useRef(null);
1354
1354
  const [isRetrying, setIsRetrying] = useState(false);
1355
1355
  const videoElementRef = useRef(null);
1356
+ const destroyInProgressRef = useRef(null);
1357
+ const isDestroyingRef = useRef(false);
1358
+ const initSequenceRef = useRef(0);
1359
+ const activeInitIdRef = useRef(null);
1356
1360
  const waitingForKeyTimerRef = useRef(null);
1357
1361
  const waitingForKeyHandlerRef = useRef(null);
1358
1362
  const playbackResumedHandlerRef = useRef(null);
@@ -1376,11 +1380,22 @@ var useShakaPlayer = ({
1376
1380
  }, [src]);
1377
1381
  const initializePlayerInternal = useCallback(async (video) => {
1378
1382
  try {
1383
+ if (destroyInProgressRef.current) {
1384
+ try {
1385
+ await destroyInProgressRef.current;
1386
+ } catch {
1387
+ }
1388
+ }
1389
+ const myInitId = ++initSequenceRef.current;
1390
+ activeInitIdRef.current = myInitId;
1379
1391
  videoElementRef.current = video;
1380
1392
  shaka.polyfill.installAll();
1381
1393
  if (!shaka.Player.isBrowserSupported()) {
1382
1394
  throw new Error("Browser not supported by Shaka Player");
1383
1395
  }
1396
+ if (isDestroyingRef.current) {
1397
+ return;
1398
+ }
1384
1399
  const player = new shaka.Player();
1385
1400
  playerRef.current = player;
1386
1401
  await player.attach(video);
@@ -1391,6 +1406,14 @@ var useShakaPlayer = ({
1391
1406
  let playlistId = src.id;
1392
1407
  const isDRM = Boolean(src.drm);
1393
1408
  storedPersistentThisLoadRef.current = false;
1409
+ if (activeInitIdRef.current !== myInitId || isDestroyingRef.current) {
1410
+ try {
1411
+ await player.destroy();
1412
+ } catch {
1413
+ }
1414
+ if (playerRef.current === player) playerRef.current = null;
1415
+ return;
1416
+ }
1394
1417
  let storedSessionsMetadata = [];
1395
1418
  if (isDRM && playlistId) {
1396
1419
  storedSessionsMetadata = retrievePersistentLicense(playlistId, src.drm.licenseCacheKey ?? "");
@@ -1488,13 +1511,19 @@ var useShakaPlayer = ({
1488
1511
  const netEngine = player.getNetworkingEngine();
1489
1512
  if (netEngine) {
1490
1513
  netEngine.registerRequestFilter((type, request) => {
1491
- if (type === shaka.net.NetworkingEngine.RequestType.LICENSE) {
1492
- if (publicKey) {
1493
- request.headers["authorization"] = `Bearer ${publicKey}`;
1494
- }
1495
- if (mottoToken) {
1496
- request.headers["x-motto-token"] = mottoToken;
1497
- }
1514
+ switch (type) {
1515
+ case shaka.net.NetworkingEngine.RequestType.LICENSE:
1516
+ if (publicKey) {
1517
+ request.headers["authorization"] = `Bearer ${publicKey}`;
1518
+ }
1519
+ if (mottoToken) {
1520
+ request.headers["x-motto-token"] = mottoToken;
1521
+ }
1522
+ break;
1523
+ case shaka.net.NetworkingEngine.RequestType.MANIFEST:
1524
+ case shaka.net.NetworkingEngine.RequestType.SEGMENT:
1525
+ request.allowCrossSiteCredentials = true;
1526
+ break;
1498
1527
  }
1499
1528
  });
1500
1529
  netEngine.registerResponseFilter((type, response) => {
@@ -1600,6 +1629,14 @@ var useShakaPlayer = ({
1600
1629
  console.error("Failed to initialize Mux Analytics:", error);
1601
1630
  }
1602
1631
  }
1632
+ if (activeInitIdRef.current !== myInitId || isDestroyingRef.current) {
1633
+ try {
1634
+ await player.destroy();
1635
+ } catch {
1636
+ }
1637
+ if (playerRef.current === player) playerRef.current = null;
1638
+ return;
1639
+ }
1603
1640
  await player.load(manifestUrl);
1604
1641
  onPlayerReady?.(player);
1605
1642
  return player;
@@ -1619,6 +1656,8 @@ var useShakaPlayer = ({
1619
1656
  const playerInstance = playerRef.current;
1620
1657
  if (playerInstance) {
1621
1658
  try {
1659
+ isDestroyingRef.current = true;
1660
+ activeInitIdRef.current = null;
1622
1661
  if (videoElementRef.current) {
1623
1662
  try {
1624
1663
  if (waitingForKeyHandlerRef.current) {
@@ -1646,7 +1685,21 @@ var useShakaPlayer = ({
1646
1685
  } finally {
1647
1686
  drmExpirationHandlerRef.current = null;
1648
1687
  }
1649
- await playerInstance.destroy();
1688
+ const performDestroy = async () => {
1689
+ try {
1690
+ await playerInstance.destroy();
1691
+ } finally {
1692
+ try {
1693
+ if (videoElementRef.current) {
1694
+ videoElementRef.current.removeAttribute("src");
1695
+ videoElementRef.current.load();
1696
+ }
1697
+ } catch {
1698
+ }
1699
+ }
1700
+ };
1701
+ destroyInProgressRef.current = performDestroy();
1702
+ await destroyInProgressRef.current;
1650
1703
  } catch (error) {
1651
1704
  console.warn("Error destroying Shaka Player:", error);
1652
1705
  } finally {
@@ -1654,6 +1707,8 @@ var useShakaPlayer = ({
1654
1707
  playerRef.current = null;
1655
1708
  }
1656
1709
  storedPersistentThisLoadRef.current = false;
1710
+ isDestroyingRef.current = false;
1711
+ destroyInProgressRef.current = null;
1657
1712
  }
1658
1713
  }
1659
1714
  }, [playerRef]);