From c02eb7be9568dbc1d086fe64b205b1dd5e891177 Mon Sep 17 00:00:00 2001
From: Mehdi Sabwat <mehdi@videolabs.io>
Date: Fri, 5 Nov 2021 14:50:07 +0100
Subject: [PATCH] wip: test emscripten filesystem access

---
 create_main.sh       |  5 ++---
 lib/module-loader.js | 29 +++++++++++++++++++++++++++--
 vlc.html             |  4 +++-
 3 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/create_main.sh b/create_main.sh
index 540c17f..54ae4b8 100755
--- a/create_main.sh
+++ b/create_main.sh
@@ -24,7 +24,6 @@ if [ ! -d vlc ]; then
 fi
 
 PATH_VLC=${PATH_VLC:=./vlc}
-SAMPLE_DIR=${SAMPLE_DIR:=./samples}
 
 # For release builds, remove '--profiling-funcs' and add '-Os'
 # Note that we use '-s MODULARIZE', but no '-s EXPORT_ES6', which would
@@ -32,7 +31,7 @@ SAMPLE_DIR=${SAMPLE_DIR:=./samples}
 emcc --bind -s USE_PTHREADS=1 -s TOTAL_MEMORY=1GB -s PTHREAD_POOL_SIZE=15 \
     -s OFFSCREEN_FRAMEBUFFER=1 -s USE_WEBGL2=1 --profiling-funcs \
     -s MODULARIZE=1 -s EXPORT_NAME="VlcModule" \
-    -s EXPORTED_RUNTIME_METHODS="allocateUTF8" \
+    -s EXPORTED_RUNTIME_METHODS="allocateUTF8" -s FORCE_FILESYSTEM=1 \
     -I $PATH_VLC/include/ \
     main.c exports_media_player.c exports_media.c \
     $PATH_VLC/build-emscripten/lib/.libs/libvlc.a \
@@ -42,4 +41,4 @@ emcc --bind -s USE_PTHREADS=1 -s TOTAL_MEMORY=1GB -s PTHREAD_POOL_SIZE=15 \
     $PATH_VLC/build-emscripten/src/.libs/libvlccore.a \
     $PATH_VLC/build-emscripten/compat/.libs/libcompat.a \
      --js-library lib/wasm-imports.js \
-    -o experimental.js --preload-file ${SAMPLE_DIR}
+    -o experimental.js
diff --git a/lib/module-loader.js b/lib/module-loader.js
index 9ac5fa1..fd55e88 100644
--- a/lib/module-loader.js
+++ b/lib/module-loader.js
@@ -7,9 +7,34 @@ var was_clicked = false;
 
 var VlcModuleExt = {
   preRun: [ function() {
-    window.display_overlay = true
+    window.display_overlay = true;
   }],
   onRuntimeInitialized: function() {
+    if (window.chooseFileSystemEntries === undefined) {
+	document.getElementById("nativeFsFileManager").style.display = "none";
+    }
+    else {
+	document.getElementById("fileManager").style.display = "none";
+    }
+    document.getElementById("fileManager").addEventListener('change', function(event) {
+	VlcModuleExt["fileHandle"] = event.target.files[0];
+	console.log("file api : sent file handle all pthread workers");
+	VlcModuleExt["fileHandle"].arrayBuffer().then( (result) => {
+	    var data = new Uint8Array(result);
+	    VlcModuleExt.FS_createDataFile("/", "mediafile", data, true, true, true);
+	});
+
+    }, false);
+    document.getElementById("nativeFsFileManager").addEventListener('click', async (e) => {
+	VlcModuleExt["fileHandle"] = await window.chooseFileSystemEntries();
+	console.log("native fs: sent file handle all pthread workers");
+	VlcModuleExt["fileHandle"].arrayBuffer().then( (result) => {
+	    var data = new Uint8Array(result);
+	    VlcModuleExt.FS_createDataFile("/", "mediafile", data, true, true, true);
+	});
+
+    });  
+
     // This should run after the wasm module is instantiated
     // before, the Pthread object won't be available
     VlcModuleExt.PThread.receiveObjectTransfer = function (data) {
@@ -20,7 +45,7 @@ var VlcModuleExt = {
       window.dispatchEvent(event);
     };
   },
-
+  fileHandle: undefined, 
   print: (function() {
     var element = document.getElementById('output');
     if (element) element.value = ''; // clear browser cache
diff --git a/vlc.html b/vlc.html
index 29e1ebe..30edb59 100644
--- a/vlc.html
+++ b/vlc.html
@@ -99,6 +99,8 @@
 
     <script src="./lib/module-loader.js"></script>
     <script src="./experimental.js"></script>
+    <button type="button" id="nativeFsFileManager">NativeFSOpen</button>
+    <input type="file" id="fileManager" multiple/>
 
     <script type="module">
       import { update_overlay, on_overlay_click } from "./lib/overlay.js";
@@ -121,7 +123,7 @@
       };
 
       const overlay = document.getElementById("overlay");
-      const media_player = new MediaPlayer(Module, "./samples/BigBuckBunny.mp4");
+      const media_player = new MediaPlayer(Module, "/mediafile");
 
       // FIXME
       Module._set_global_media_player(media_player.media_player_ptr);
-- 
GitLab