Commit 2df77145 authored by Olivier FAURE's avatar Olivier FAURE
Browse files

Reorganize and comment code

parent 81f10519
......@@ -27,13 +27,15 @@ PATH_VLC=${PATH_VLC:=./vlc}
SAMPLE_DIR=${SAMPLE_DIR:=./samples}
PROJECT_DIR=${PROJECT_DIR:=./vlc/extras/package/wasm-emscripten/build}
# for release, remove profiling-funcs and add -Os
# For release builds, remove '--profiling-funcs' and add '-Os'
# Note that we use '-s MODULARIZE', but no '-s EXPORT_ES6', which would
# conflict with pthreads on Firefox.
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 EXTRA_EXPORTED_RUNTIME_METHODS="allocateUTF8" \
-I $PATH_VLC/include/ -I $PROJECT_DIR/wasm32-unknown-emscripten/include/ \
exports_media_player.c main.c \
main.c exports_media_player.c exports_media.c \
$PROJECT_DIR/build-emscripten/lib/.libs/libvlc.a \
$PROJECT_DIR/build-emscripten/vlc-modules.bc \
$PROJECT_DIR/build-emscripten/modules/.libs/*.a \
......
// Re-exports of functions defined in "include/vlc/libvlc/media/player.h"
// See exports_media_player.c for why this is necessary.
#include <vlc/vlc.h>
#include <vlc_common.h>
#include <emscripten.h>
#include <emscripten/html5.h>
// Singleton, defined in main.c
extern libvlc_instance_t *libvlc;
libvlc_media_t* EMSCRIPTEN_KEEPALIVE wasm_media_new_path(const char *path) {
return libvlc_media_new_path(libvlc, path);
}
void EMSCRIPTEN_KEEPALIVE wasm_media_retain( libvlc_media_t *media) {
libvlc_media_retain(media);
}
void EMSCRIPTEN_KEEPALIVE wasm_media_release( libvlc_media_t *media) {
libvlc_media_release(media);
}
// Re-exports of functions defined in "include/vlc/libvlc/media/player.h"
// We need to re-export these functions to make sure they're included as
// symbols in the wasm binary.
// Emscripten provides two ways to do that: EMSCRIPTEN_KEEPALIVE, and the
// EXPORTED_FUNCTIONS argument. EXPORTED_FUNCTIONS is not reliable, because
// symbols might be inlined in intermediary passes.
// Also, some functions need some marshalling of arguments to be callabled
// from JS.
#include <vlc/vlc.h>
#include <vlc_common.h>
#include <emscripten.h>
#include <emscripten/html5.h>
// Singleton, defined in main.c
extern libvlc_instance_t *libvlc;
// TODO - explain why we have to redefine everything
libvlc_media_player_t* EMSCRIPTEN_KEEPALIVE wasm_media_player_new() {
return libvlc_media_player_new(libvlc);
}
......@@ -217,7 +225,6 @@ int EMSCRIPTEN_KEEPALIVE wasm_audio_set_delay(libvlc_media_player_t *media_playe
return libvlc_audio_set_delay(media_player, delay);
}
// TODO - Export libvlc_media_player_role constants
int EMSCRIPTEN_KEEPALIVE wasm_media_player_get_role(libvlc_media_player_t *media_player) {
......
// Encapsulate functions exported from exports_*.c
// Encapsulates libvlc_media_player_t
export class MediaPlayer {
constructor(module, path) {
this.module = module;
......@@ -53,8 +56,6 @@ export class MediaPlayer {
return this.module._wasm_media_player_pause(this.media_player_ptr);
}
get_length() {
return this.module._wasm_media_player_get_length(this.media_player_ptr);
}
......@@ -121,7 +122,6 @@ export class MediaPlayer {
return this.module._wasm_media_player_set_rate(this.media_player_ptr, rate);
}
has_vout() {
return this.module._wasm_media_player_has_vout(this.media_player_ptr);
}
......@@ -166,7 +166,6 @@ export class MediaPlayer {
return { x, y };
}
toggle_mute() {
this.module._wasm_audio_toggle_mute(this.media_player_ptr);
}
......@@ -213,6 +212,7 @@ export class MediaPlayer {
}
// Encapsulates libvlc_media_t
export class Media {
constructor(module, path, raw_ptr) {
if (raw_ptr != null) {
......
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var spinnerElement = document.getElementById('spinner');
......@@ -9,6 +8,7 @@ var VlcModuleExt = {
// This should run after the wasm module is instantiated
// before, the Pthread object won't be available
VlcModuleExt.PThread.receiveObjectTransfer = function (data) {
// Transfer messages from worker threads to the main window.
let event = new CustomEvent('worker_message', {
detail: data.msg
});
......@@ -38,19 +38,6 @@ var VlcModuleExt = {
console.error(text);
},
onRuntimeInitialized: function () {
this.play_video = this._play_video;
this.pause_video = this._pause_video;
this.is_paused = this._is_paused;
this.get_position = this._get_position;
this.set_position = this._set_position;
this.get_volume = this._get_volume;
this.set_volume = this._set_volume;
this.toggle_mute = this._toggle_mute;
this.get_mute = this._get_mute;
this.set_mute = this._set_mute;
},
canvas: (function() {
var canvas = document.getElementById('canvas')
var overlay = document.getElementById('overlay')
......
// Functions to manage both the layout and user inputs of the video overlay
// It's very artisanal and would usually be done with a framework like
// React, but it's enough for the needs of the project.
const PROGRESS_BAR_COLOR = "#2a81d4";
const PROGRESS_BAR_BG_COLOR = "#2d2d2d";
const VOLUME_BAR_COLOR = "#4ad24a";
......@@ -55,6 +59,7 @@ volume_button.src = "vlc/modules/gui/qt/pixmaps/toolbar/volume-medium.png";
const muted_button = new Image(BUTTON_SIZE, BUTTON_SIZE);
muted_button.src = "vlc/modules/gui/qt/pixmaps/toolbar/volume-muted.png";
// Apply underlying changes (eg video is paused) to the displayed UI
export function update_overlay(overlay) {
const ctx = overlay.getContext("2d");
const media_player = window.media_player;
......@@ -166,7 +171,9 @@ export function update_overlay(overlay) {
export function on_overlay_click(overlay, mouse_event) {
const ctx = overlay.getContext("2d");
const media_player = window.media_player;
if (media_player == null) {
// Video isn't loaded yet
return;
}
......@@ -185,8 +192,6 @@ export function on_overlay_click(overlay, mouse_event) {
let x = mouse_event.clientX - canvas_rect.left;
let y = mouse_event.clientY - canvas_rect.top;
// TODO - initial state
// User clicked in menu bar
if (y > overlay.height - MENU_BAR_HEIGHT) {
// User clicked on play/pause button
......
// Functions injected into the wasm binary, can be
// called from C/C++ code.
// The top level of this JS file is executed at compile time.
// Functions bodies are copy-pasted into 'experimental.js'
// (unless they're culled from dead code elimination)
mergeInto(LibraryManager.library, {
// Apply underlying changes (eg video is paused) to the displayed UI
update_overlay: function() {
const overlay = document.getElementById("overlay");
update_overlay(overlay);
},
// Worker functions
// Worker functions - These are intended to be called from threads
// They can't access the browser APIs directly, so they send messages
// TODO - This essentially sends one message per frame; might be bad for perf
on_position_changed: function() {
postMessage({ cmd: "objectTransfer", msg: "on_position_changed" });
}
......
......@@ -58,6 +58,8 @@ int main() {
return 0;
}
// Used to make sure the UI (progress bar, play/pause button, etc) is
// updated as the video is read.
void EMSCRIPTEN_KEEPALIVE attach_update_events(libvlc_media_player_t *media_player) {
libvlc_event_manager_t* event_manager = libvlc_media_player_event_manager(media_player);
int res;
......@@ -76,17 +78,3 @@ void EMSCRIPTEN_KEEPALIVE attach_update_events(libvlc_media_player_t *media_play
);
assert(res == 0);
}
// ---
libvlc_media_t* EMSCRIPTEN_KEEPALIVE wasm_media_new_path(const char *path) {
return libvlc_media_new_path(libvlc, path);
}
void EMSCRIPTEN_KEEPALIVE wasm_media_retain( libvlc_media_t *media) {
libvlc_media_retain(media);
}
void EMSCRIPTEN_KEEPALIVE wasm_media_release( libvlc_media_t *media) {
libvlc_media_release(media);
}
......@@ -104,9 +104,13 @@
import { update_overlay, on_overlay_click } from "./lib/overlay.js";
import { MediaPlayer } from "./lib/libvlc.js";
// VlcModule is a function generated by emscripten in experimental.js,
// that loads the wasm file and generates a module object from it.
// VlcModuleExt is an object defined in 'lib/module-loader.js' with a
// bunch of options; also, all the fields of VlcModuleExt are added to
// the returned Module.
const Module = await VlcModule(VlcModuleExt);
Module.setStatus('Downloading...');
window.onerror = function(event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
Module.setStatus('Exception thrown, see JavaScript console');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment