-
Notifications
You must be signed in to change notification settings - Fork 24
Add support for python adapters when compiling with emscripten #95
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
base: main
Are you sure you want to change the base?
Changes from all commits
12e7d39
5a4adf1
8048851
6668919
3d2e268
3866b82
e210eca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| <meta charset="utf-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/> | ||
| <title>Raven</title> | ||
| <script src="https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.js"></script> | ||
| <style> | ||
| body { margin: 0; background-color: black } | ||
| .emscripten { | ||
|
|
@@ -47,9 +48,110 @@ | |
| // ... And free it when we're done. | ||
| _free(stringOnWasmHeap); | ||
| } | ||
|
|
||
| // https://stackoverflow.com/a/69935189 | ||
| Module.LoadStringFromEvent = function (element_event) { | ||
| const file_reader = new FileReader(); | ||
| let file_name = element_event.target.files[0].name; | ||
|
|
||
| file_reader.onload = (evt) => { | ||
| if (file_name.endsWith(".otio")) { | ||
| timeline_json = evt.target.result; | ||
| } else { | ||
| console.log("Loading using pyodide") | ||
| // Get adapter | ||
| timeline_json = pyodide.runPython(` | ||
| import tempfile | ||
|
|
||
| import opentimelineio | ||
|
|
||
| mode = 'w' | ||
| if not isinstance(data, str): | ||
| mode = 'wb' | ||
|
|
||
| with tempfile.NamedTemporaryFile(mode=mode, suffix="." + file_name.split(".")[-1]) as f: | ||
| f.write(data) | ||
|
|
||
| f.flush() | ||
| f.seek(0) | ||
|
|
||
| timeline = opentimelineio.adapters.read_from_file(f.name) | ||
| output = opentimelineio.adapters.write_to_string(timeline, adapter_name='otio_json', indent=0) | ||
| output | ||
| `, {locals: pyodide.toPy({data: evt.target.result, file_name})}); | ||
| } | ||
|
|
||
| // This is annoying, but we need to allocate memory manually on the wasm heap | ||
| // to avoid a stack overflow if you pass in a very large string. | ||
| var lengthBytes = lengthBytesUTF8(timeline_json) + 1; | ||
| var stringOnWasmHeap = _malloc(lengthBytes); | ||
| stringToUTF8(timeline_json, stringOnWasmHeap, lengthBytes); | ||
|
|
||
| Module.ccall("js_LoadString", "number", ["number"], [stringOnWasmHeap]); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not call this for otioz (which raven supports natively) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it support OTIOZ when running in WASM? If so, how well does it handle the memory mapping situation? We may end up wanting to go down the route of the new File System API (Chromium support ✅, but no Safari/Firefox I'm pretty sure), so that it doesn't have to read the whole thing into memory directly.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have not tried, but I don't see why it wouldn't work. I took a quick look at the new shiny file system API stuff but didn't go deep enough to understand the pros and cons memory wise. Do you have a link that would talk more exactly that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://developer.chrome.com/docs/capabilities/web-apis/file-system-access Give this a quick glance. I don't really know this translates into WASM but my thinking was really just that we could just access the file in-situ without passing it all the way into the WASM-mapped 2GB/4GB memory this way. I just figure that with an OTIOZ when there's actually media, you'll hit the limit pretty much immediately. |
||
|
|
||
| // ... And free it when we're done. | ||
| _free(stringOnWasmHeap); | ||
| } | ||
| if (element_event.target.files[0].name.endsWith('.aaf')) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AAF and OTIOZ are binary, the rest are all text. |
||
| file_reader.readAsArrayBuffer(element_event.target.files[0]); | ||
| } else { | ||
| file_reader.readAsBinaryString(element_event.target.files[0]); | ||
| } | ||
| } | ||
| Module.LoadUrl = Module.cwrap("js_LoadUrl", "number", ["string"]); | ||
| } | ||
|
|
||
| async function initializePyodide() { | ||
| window.initializePyodidePromise = new Promise(async function(resolve, reject) { | ||
| try { | ||
| console.log("Loading pyodide"); | ||
| let pyodide = await loadPyodide(); | ||
| console.log("Loaded pyodide"); | ||
|
|
||
| console.log("Installing micropip"); | ||
| await pyodide.loadPackage("micropip"); | ||
| console.log("Installed micropip"); | ||
|
|
||
| console.log("Installing OTIO python bindings and plugins"); | ||
| const micropip = pyodide.pyimport("micropip"); | ||
| await micropip.install( | ||
| [ | ||
| 'https://jcmorin.dev/otio-wasm/opentimelineio-0.18.0.dev1-cp312-cp312-pyodide_2024_0_wasm32.whl', | ||
| 'otio-aaf-adapter', | ||
| 'otio-burnins-adapter', | ||
| 'otio-xges-adapter', | ||
| 'otio-ale-adapter', | ||
| 'otio-hls-playlist-adapter', | ||
| 'otio-fcpx-xml-adapter', | ||
| 'otio-maya-sequencer-adapter', | ||
| 'otio-cmx3600-adapter', | ||
| 'otio-fcp-adapter', | ||
| ] | ||
| ); | ||
| console.log("Installed OTIO python bindings and plugins"); | ||
|
|
||
| // This is a hack. See https://github.com/pyodide/pyodide/pull/4836 | ||
| pyodide._module.reportUndefinedSymbols(); | ||
|
|
||
| window.pyodide = pyodide; | ||
| } catch (e) { | ||
| console.error(e); | ||
| reject(e); | ||
| } | ||
| resolve(); | ||
| }); | ||
| } | ||
|
|
||
| async function waitForPyodide() { | ||
| addRunDependency("pyodide"); | ||
|
|
||
| console.log("Waiting for pyodide"); | ||
| await window.initializePyodidePromise; | ||
| console.log("Pyodide loaded"); | ||
|
|
||
| removeRunDependency("pyodide"); | ||
| } | ||
|
|
||
| /** | ||
| * Once raven loads, load any OTIO timeline that was passed in. | ||
| */ | ||
|
|
@@ -64,8 +166,8 @@ | |
| var Module = { | ||
| // otioLoadUrl: "", | ||
| // otioLoadString: "", | ||
| preRun: [exposeRavenFunctions], | ||
| postRun: [ravenPostRun], | ||
| preRun: [exposeRavenFunctions, initializePyodide], | ||
| postRun: [ravenPostRun, waitForPyodide], | ||
| print: (function() { | ||
| return function(text) { | ||
| text = Array.prototype.slice.call(arguments).join(' '); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting pyodide from a CDN, but we should probably look at other options.