henry-rodrick / hlsjs-ipfs-loader

A js-ipfs loader for the hls.js JavaScript HLS client
MIT License
60 stars 31 forks source link

m3u8 files that references directories do not load work. #19

Closed georgyo closed 3 years ago

georgyo commented 3 years ago

Attemping to play mainfest.m3u8 from either QmTzZF9GBEGq7m8cWdhoiCLB9U4YY2zEkQGV4sWqdZ2YrT or QmZ82qHYfPC2vd3BXYZMTBojjAUfXQPKMyky2SSBTQvqUk both fail because the something is removing the directories from the path.

mainfest.m3u8 references stream0/stream0.m3u8 however this returns the error:

Error: File not found: QmTzZF9GBEGq7m8cWdhoiCLB9U4YY2zEkQGV4sWqdZ2YrT/stream0.m3u8 at getFile (index.js:120)

/ipfs/QmZ82qHYfPC2vd3BXYZMTBojjAUfXQPKMyky2SSBTQvqUk/manifest.m3u8 looks like this to ease ticket viewing.

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2149280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=1280x720,NAME="720"
stream0/stream0.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=246440,CODECS="mp4a.40.5,avc1.42000d",RESOLUTION=320x184,NAME="240"
stream1/stream1.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=460560,CODECS="mp4a.40.5,avc1.420016",RESOLUTION=512x288,NAME="380"
stream2/stream2.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=836280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=848x480,NAME="480"
stream3/stream3.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=6221600,CODECS="mp4a.40.2,avc1.640028",RESOLUTION=1920x1080,NAME="1080"
stream4/stream4.m3u8
vaultec81 commented 3 years ago

This is most likely a possible bug as manifest.m3u8 specifies the correct path per the directory structure, and file plays correctly using VLC. Could you provide some addition details? Such as extra logs if any, go-ipfs or js-ipfs version, and version of hlsjs-ipfs-loader?

georgyo commented 3 years ago

I have never coded any javascript before, so I am not going to submit a PR, but I believe this fixes it.

Demo of patched code @ https://shamm.as/video/

diff --git a/src/index.js b/src/index.js
index 4a40a38..9ad06cc 100644
--- a/src/index.js
+++ b/src/index.js
@@ -60,8 +60,7 @@ class HlsjsIPFSLoader {
     stats.tfirst = Math.max(performance.now(), stats.trequest)
     stats.loaded = 0

-    const urlParts = context.url.split("/")
-    const filename = urlParts[urlParts.length - 1]
+    const filename = context.url.replace(window.location.href, "")

     const options = {}
     if (Number.isFinite(context.rangeStart)) {
@@ -115,12 +114,18 @@ class HlsjsIPFSLoader {

 async function getFile(ipfs, rootHash, filename, options, debug, abortFlag) {
   debug(`Fetching hash for '${rootHash}/${filename}'`)
+  const path = `${rootHash}/${filename}`
+  const pathParts = path.split("/")
+  const short_filename = pathParts.pop()
+  const basePath = pathParts.join("/")
+
+
   if(filename === null) {
     return cat(rootHash, options, ipfs, debug, abortFlag)
   }

-  for await (const link of ipfs.ls(rootHash)) {
-    if (link.name !== filename) {
+  for await (const link of ipfs.ls(basePath)) {
+    if (link.name !== short_filename) {
       continue
     }