@mottosports/motto-video-player 1.0.1-rc.51 → 1.0.1-rc.53

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.js CHANGED
@@ -1211,7 +1211,7 @@ var supportsWidevinePersistentLicenses = () => {
1211
1211
  var import_mux_data_shakaplayer = __toESM(require("@mux/mux-data-shakaplayer"));
1212
1212
 
1213
1213
  // package.json
1214
- var version = "1.0.1-rc.51";
1214
+ var version = "1.0.1-rc.53";
1215
1215
 
1216
1216
  // src/utils/licenseCache.ts
1217
1217
  var PERSISTENT_LICENSE_PREFIX = "motto_lic_";
@@ -3598,6 +3598,81 @@ html[dir=rtl] .shaka-overflow-menu .shaka-overflow-button .material-svg-icon:fir
3598
3598
 
3599
3599
  // src/Player.tsx
3600
3600
  var import_tailwind_merge2 = require("tailwind-merge");
3601
+
3602
+ // src/utils/scriptLoader.ts
3603
+ var SCRIPT_CONFIGS = {
3604
+ ima: {
3605
+ src: "https://imasdk.googleapis.com/js/sdkloader/ima3.js",
3606
+ id: "ima-sdk",
3607
+ type: "text/javascript"
3608
+ },
3609
+ system73: {
3610
+ src: "//cdn.s73cloud.com/4/s73-sdk-shakaplayer.js",
3611
+ id: "system73-sdk",
3612
+ type: "application/javascript"
3613
+ }
3614
+ };
3615
+ function loadScript(config) {
3616
+ return new Promise((resolve, reject) => {
3617
+ if (document.getElementById(config.id)) {
3618
+ resolve();
3619
+ return;
3620
+ }
3621
+ const script = document.createElement("script");
3622
+ script.id = config.id;
3623
+ script.src = config.src;
3624
+ script.type = config.type || "text/javascript";
3625
+ script.async = true;
3626
+ script.onload = () => resolve();
3627
+ script.onerror = () => reject(new Error(`Failed to load script: ${config.src}`));
3628
+ document.head.appendChild(script);
3629
+ });
3630
+ }
3631
+ function getRequiredScriptLoaders(needsIma, needsSystem73) {
3632
+ const loaders = [];
3633
+ if (needsIma) {
3634
+ loaders.push(loadScript(SCRIPT_CONFIGS.ima));
3635
+ }
3636
+ if (needsSystem73) {
3637
+ loaders.push(loadScript(SCRIPT_CONFIGS.system73));
3638
+ }
3639
+ return loaders;
3640
+ }
3641
+ async function loadScripts(scriptLoaders) {
3642
+ if (scriptLoaders.length === 0) {
3643
+ return;
3644
+ }
3645
+ try {
3646
+ await Promise.all(scriptLoaders);
3647
+ } catch (error) {
3648
+ console.error("Error loading scripts:", error);
3649
+ throw error;
3650
+ }
3651
+ }
3652
+ function waitForGlobal(globalName, timeout = 5e3) {
3653
+ return new Promise((resolve, reject) => {
3654
+ const startTime = Date.now();
3655
+ function checkGlobal() {
3656
+ const global = window[globalName];
3657
+ if (global) {
3658
+ resolve(global);
3659
+ return;
3660
+ }
3661
+ if (Date.now() - startTime > timeout) {
3662
+ reject(new Error(`Timeout waiting for global: ${globalName}`));
3663
+ return;
3664
+ }
3665
+ setTimeout(checkGlobal, 100);
3666
+ }
3667
+ checkGlobal();
3668
+ });
3669
+ }
3670
+ async function waitForGlobals(globalNames, timeout = 5e3) {
3671
+ const promises = globalNames.map((name) => waitForGlobal(name, timeout));
3672
+ await Promise.all(promises);
3673
+ }
3674
+
3675
+ // src/Player.tsx
3601
3676
  var import_jsx_runtime8 = require("react/jsx-runtime");
3602
3677
  var Player = (0, import_react12.forwardRef)(
3603
3678
  ({
@@ -3625,6 +3700,7 @@ var Player = (0, import_react12.forwardRef)(
3625
3700
  }, ref) => {
3626
3701
  const videoRef = (0, import_react12.useRef)(null);
3627
3702
  const containerRef = (0, import_react12.useRef)(null);
3703
+ const [isScriptsLoaded, setIsScriptsLoaded] = (0, import_react12.useState)(false);
3628
3704
  (0, import_react12.useImperativeHandle)(ref, () => videoRef.current, []);
3629
3705
  const { playerRef, initializePlayer, destroyPlayer, isRetrying } = useShakaPlayer({
3630
3706
  src,
@@ -3715,7 +3791,11 @@ var Player = (0, import_react12.forwardRef)(
3715
3791
  if (!imaConfig?.adTagUrl || !playerRef.current || !videoRef.current || !uiRef.current) {
3716
3792
  return;
3717
3793
  }
3718
- console.log("Initializing ads...");
3794
+ if (!window.google?.ima) {
3795
+ console.error("Google IMA SDK not available when trying to initialize ads");
3796
+ return;
3797
+ }
3798
+ console.log("Initializing ads with autoplay:", autoPlay);
3719
3799
  try {
3720
3800
  const player = playerRef.current;
3721
3801
  const video = videoRef.current;
@@ -3727,23 +3807,53 @@ var Player = (0, import_react12.forwardRef)(
3727
3807
  console.error("Ad manager not available");
3728
3808
  return;
3729
3809
  }
3730
- adManager.initClientSide(container, video);
3731
3810
  const google = window.google;
3811
+ let adsRenderingSettings = null;
3812
+ if (imaConfig.adsRenderingSettings) {
3813
+ adsRenderingSettings = imaConfig.adsRenderingSettings;
3814
+ } else if (autoPlay) {
3815
+ adsRenderingSettings = new google.ima.AdsRenderingSettings();
3816
+ adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
3817
+ }
3818
+ adManager.initClientSide(container, video, adsRenderingSettings);
3732
3819
  const adsRequest = new google.ima.AdsRequest();
3733
3820
  adsRequest.adTagUrl = imaConfig.adTagUrl;
3734
3821
  adManager.requestClientSideAds(adsRequest);
3735
- console.log("Ads requested with tag:", imaConfig.adTagUrl);
3822
+ console.log("Ads requested successfully with tag:", imaConfig.adTagUrl, "autoplay:", autoPlay);
3736
3823
  } catch (error) {
3737
3824
  console.error("Error initializing ads:", error);
3738
3825
  }
3739
- }, [imaConfig]);
3826
+ }, [imaConfig, autoPlay]);
3827
+ (0, import_react12.useEffect)(() => {
3828
+ const loadRequiredScripts = async () => {
3829
+ try {
3830
+ const scriptLoaders = getRequiredScriptLoaders(!!imaConfig?.adTagUrl, !!system73Config?.apiKey);
3831
+ await loadScripts(scriptLoaders);
3832
+ const globalsToWait = [];
3833
+ if (imaConfig?.adTagUrl) {
3834
+ globalsToWait.push("google");
3835
+ }
3836
+ if (system73Config?.apiKey) {
3837
+ globalsToWait.push("S73ShakaPlayerWrapper");
3838
+ }
3839
+ if (globalsToWait.length > 0) {
3840
+ await waitForGlobals(globalsToWait);
3841
+ }
3842
+ setIsScriptsLoaded(true);
3843
+ } catch (error) {
3844
+ console.error("Error loading required scripts:", error);
3845
+ setIsScriptsLoaded(true);
3846
+ }
3847
+ };
3848
+ loadRequiredScripts();
3849
+ }, [imaConfig?.adTagUrl, system73Config?.apiKey]);
3740
3850
  (0, import_react12.useEffect)(() => {
3741
3851
  const video = videoRef.current;
3742
- if (!video) return;
3852
+ if (!video || !isScriptsLoaded) return;
3743
3853
  const initialize = async () => {
3744
3854
  try {
3745
3855
  let system73Wrapper = null;
3746
- if (system73Config?.apiKey) {
3856
+ if (system73Config?.apiKey && window.S73ShakaPlayerWrapper) {
3747
3857
  const playerConfig = { ...shakaConfig };
3748
3858
  system73Wrapper = initializeSystem73(playerConfig);
3749
3859
  if (system73Wrapper) {
@@ -3759,7 +3869,8 @@ var Player = (0, import_react12.forwardRef)(
3759
3869
  const cleanupQuality = setupQualityTracking();
3760
3870
  configureQuality();
3761
3871
  await initializeUI();
3762
- if (imaConfig) {
3872
+ if (imaConfig?.adTagUrl && window.google?.ima) {
3873
+ console.log("Initializing ads immediately after UI setup");
3763
3874
  initializeAds();
3764
3875
  }
3765
3876
  } catch (error) {
@@ -3774,16 +3885,15 @@ var Player = (0, import_react12.forwardRef)(
3774
3885
  destroyMux();
3775
3886
  destroyPlayer();
3776
3887
  };
3777
- }, [src]);
3888
+ }, [src, isScriptsLoaded]);
3778
3889
  (0, import_react12.useEffect)(() => {
3779
3890
  const video = videoRef.current;
3780
3891
  if (!video) return;
3781
3892
  video.autoplay = autoPlay;
3782
3893
  video.loop = loop;
3783
- video.muted = muted;
3784
3894
  video.controls = false;
3785
3895
  if (poster) video.poster = poster;
3786
- }, [autoPlay, loop, muted, poster]);
3896
+ }, [autoPlay, loop, muted, poster, imaConfig?.adTagUrl]);
3787
3897
  (0, import_react12.useEffect)(() => {
3788
3898
  const video = videoRef.current;
3789
3899
  if (!video) return;