Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lily/Video] add m3u8 support #1785

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
46 changes: 44 additions & 2 deletions extensions/Lily/Video.js
Copy link
Author

@jexjws jexjws Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!(await Scratch.canFetch(url))) return;

WARNING : If the resource URLs in a .m3u8 file are not on the same domain as the .m3u8 file itself, the client can fetch resources from other domains without using Scratch.fetch.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can do a check using a custom loader for hls.js before a playlist or fragment gets loaded. This is a quick mock up but haven't tested it at all:

this.hls = new Hls({
  loader: class CustomLoader extends Hls.DefaultConfig.loader {
    load(context, config, callbacks) {
      let { type, url } = context;

      // Custom behavior - do a check first
      Scratch.canFetch(url).then(isAllowed => {
        if (!isAllowed) {
          callbacks.onErrorCallback({
            code: 'Scratch_Error',
            text: `${err}`
          });
        } else {
          super.load(context, config, callbacks);
        }
      }).catch(err => callbacks.onErrorCallback({
        code: 'Scratch_Error',
        text: `${err}`
      }));
    }
  }
});

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
const runtime = vm.runtime;
const renderer = vm.renderer;
const Cast = Scratch.Cast;

// In some versions of Chrome, it seems that trying to render a <video> returns pure black
// if it's not in the DOM in a place the browser thinks is visible. That means we can't
// use display: none.
Expand Down Expand Up @@ -62,7 +61,25 @@
this.readyCallback();
this.markVideoDirty();
};
this.videoElement.src = videoSrc;


if (videoSrc.endsWith(".m3u8") && (!this.videoElement.canPlayType('application/vnd.apple.mpegurl'))) {
// try use hls.js for m3u8
this.loadHlsJsIfNeeded(() => {
// @ts-ignore
const Hls = window.Hls;
if (Hls.isSupported()) {
this.hls = new Hls();
this.hls.loadSource(videoSrc);
this.hls.attachMedia(this.videoElement);
} else {
this.videoElement.src = videoSrc;
}
})
} else {
this.videoElement.src = videoSrc;
}

this.videoElement.currentTime = 0;

// <video> must be in the DOM for it to render (see comments above)
Expand All @@ -74,6 +91,28 @@
this.reuploadVideo();
}

// for m3u8 support
loadHlsJsIfNeeded(callback) {
// @ts-ignore
if (window.Hls) {
callback();
return;
}
const script = document.createElement('script');
script.src = "https://cdn.jsdelivr.net/npm/hls.js@1";
script.async = true;

script.onload = () => {
if (typeof callback === 'function') {
callback();
}
};
script.onerror = () => {
console.error('Failed to load HLS.js.');
};
document.head.appendChild(script);
}

reuploadVideo() {
this.videoDirty = false;
if (this.videoError) {
Expand Down Expand Up @@ -126,6 +165,9 @@
super.dispose();
this.videoElement.pause();
this.videoElement.remove();
if (this.hls) {
this.hls.destroy();
}
}
}

Expand Down
Loading