diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..cb576d5f --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +### Project ### +build/ + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* diff --git a/README.md b/README.md index 3009ca39..1b65a2ac 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ var player = new JSMpeg.Player('video.ts' {loop: true, autoplay: true}); or HTML ```html -
``` @@ -185,7 +185,7 @@ ffmpeg \ http://localhost:8081/supersecret ``` -You should now see a live webcam image in your browser. +You should now see a live webcam image in your browser. If ffmpeg failed to open the input video, it's likely that your webcam does not support the given resolution, format or framerate. To get a list of compatible modes run: @@ -245,9 +245,78 @@ Have a look a the [jsmpeg.js source](https://github.com/phoboslab/jsmpeg/blob/ma Using parts of the library without creating a full player should also be fairly straightforward. E.g. you can create a stand-alone instance of the `JSMpeg.Decoder.MPEG1Video` class, `.connect()` a renderer, `.write()` some data to it and `.decode()` a frame, without touching JSMpeg's other parts. +## Building yourself +In case of you needs build youself or debug your application. + +### Prerequisites + - GNU make (search for your platform); + - Emscripten EMCC (See the installation instructions in [download and install page](https://emscripten.org/docs/getting_started/downloads.html#download-and-install)); + - GNU base64 (search for your platform); + - NODE uglifyjs (search for your platform) + +### Building +```bash +# Change to jsmpeg directory +$ cd to/jsmpeg/dir +# Run the make +$ make +``` +> The output files will be generated in `build/` directory. -## Previous Version +### Make options +Build the `jsmpeg.min.js` file: +```bash +$ make jsmpeg.min.js +``` -The JSMpeg version currently living in this repo is a complete rewrite of the original jsmpeg library that was just able to decode raw mpeg1video. If you're looking for the old version, see the [v0.2 tag](https://github.com/phoboslab/jsmpeg/releases/tag/v0.2). +Build the `jsmpeg.js` file: +```bash +$ make jsmpeg.js +``` +Build the `jsmpeg.wasm.js` file: +```bash +$ make jsmpeg.wasm.js +``` + +Build the `jsmpeg.wasm` file: +```bash +$ make jsmpeg.wasm +``` +To clean the `build/` directory: +```bash +$ make clean +``` + +### Loading the JS files individually +To load the JS files individually, put the ` + + + + + + + + + + + + + + + + + + + +``` +> Don't forguet to build the `jsmpeg.wasm.js` file. + +## Previous Version + +The JSMpeg version currently living in this repo is a complete rewrite of the original jsmpeg library that was just able to decode raw mpeg1video. If you're looking for the old version, see the [v0.2 tag](https://github.com/phoboslab/jsmpeg/releases/tag/v0.2). diff --git a/build.sh b/build.sh deleted file mode 100755 index 7b9e5f10..00000000 --- a/build.sh +++ /dev/null @@ -1,91 +0,0 @@ -#!/bin/sh - - -# Build the .wasm Module first - -# Since we're compiling a side module here, so that we can load it without the -# runtime cruft, we have to explicitly compile in support for malloc and -# friends. -# Note memcpy, memmove and memset are explicitly exported, otherwise they will -# be eliminated by the SIDE_MODULE=2 setting - not sure why that happens. -emcc \ - src/wasm/mpeg1.c \ - src/wasm/mp2.c \ - src/wasm/buffer.c \ - $EMSCRIPTEN/system/lib/emmalloc.cpp \ - $EMSCRIPTEN/system/lib/libc/musl/src/string/memcpy.c \ - $EMSCRIPTEN/system/lib/libc/musl/src/string/memmove.c \ - $EMSCRIPTEN/system/lib/libc/musl/src/string/memset.c \ - -s WASM=1 \ - -s SIDE_MODULE=2 \ - -s TOTAL_STACK=5242880\ - -s USE_PTHREADS=0 \ - -s LEGALIZE_JS_FFI=0\ - -s NO_FILESYSTEM=1 \ - -s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE="[]" \ - -s "EXPORTED_FUNCTIONS=[ - '_memcpy', - '_memmove', - '_memset', - '_mpeg1_decoder_create', - '_mpeg1_decoder_destroy', - '_mpeg1_decoder_get_write_ptr', - '_mpeg1_decoder_get_index', - '_mpeg1_decoder_set_index', - '_mpeg1_decoder_did_write', - '_mpeg1_decoder_has_sequence_header', - '_mpeg1_decoder_get_frame_rate', - '_mpeg1_decoder_get_coded_size', - '_mpeg1_decoder_get_width', - '_mpeg1_decoder_get_height', - '_mpeg1_decoder_get_y_ptr', - '_mpeg1_decoder_get_cr_ptr', - '_mpeg1_decoder_get_cb_ptr', - '_mpeg1_decoder_decode', - '_mp2_decoder_create', - '_mp2_decoder_destroy', - '_mp2_decoder_get_write_ptr', - '_mp2_decoder_get_index', - '_mp2_decoder_set_index', - '_mp2_decoder_did_write', - '_mp2_decoder_get_left_channel_ptr', - '_mp2_decoder_get_right_channel_ptr', - '_mp2_decoder_get_sample_rate', - '_mp2_decoder_decode']" \ - -O3 \ - -o jsmpeg.wasm - - -# Concat all .js sources -cat \ - src/jsmpeg.js \ - src/video-element.js \ - src/player.js \ - src/buffer.js \ - src/ajax.js \ - src/ajax-progressive.js \ - src/websocket.js \ - src/ts.js \ - src/decoder.js \ - src/mpeg1.js \ - src/mpeg1-wasm.js \ - src/mp2.js \ - src/mp2-wasm.js \ - src/webgl.js \ - src/canvas2d.js \ - src/webaudio.js \ - src/wasm-module.js \ - > jsmpeg.js - -# Append the .wasm module to the .js source as base64 string -echo "JSMpeg.WASM_BINARY_INLINED='$(base64 -w 0 jsmpeg.wasm)';" \ - >> jsmpeg.js - - -# Minify -uglifyjs jsmpeg.js -o jsmpeg.min.js - -# Cleanup -rm jsmpeg.js -rm jsmpeg.wasm - diff --git a/makefile b/makefile new file mode 100644 index 00000000..215f3c8b --- /dev/null +++ b/makefile @@ -0,0 +1,96 @@ +# General settings +PROJECT_NAME = jsmpeg +SRC_DIR = src +WASM_SRC_DIR = $(SRC_DIR)/wasm +BUILD_DIR = build + +# Javascript source files setup +JS_SRC_FILES = src/jsmpeg.js src/video-element.js src/player.js src/buffer.js \ + src/ajax.js src/ajax-progressive.js src/websocket.js src/ts.js \ + src/decoder.js src/mpeg1.js src/mpeg1-wasm.js src/mp2.js \ + src/mp2-wasm.js src/webgl.js src/canvas2d.js src/webaudio.js \ + src/wasm-module.js + +# WASM source files setup +WASM_SRC_FILES = $(shell find $(WASM_SRC_DIR) -type f \( -name "*.c" \) ) + +# Tools setup +MINJS = uglifyjs +B64 = base64 +B64FLAGS = -w 0 + +# Emscripten Compiler setup +WASMCC = emcc +EMCCROOT = ${EMSCRIPTEN} +WASMCCFLAGS = -s WASM=1 -s SIDE_MODULE=2 -s TOTAL_STACK=5242880 \ + -s USE_PTHREADS=0 -s LEGALIZE_JS_FFI=0 -s NO_FILESYSTEM=1 \ + -s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE="[]" +WASMCCEXTRAFLAGS = -O3 + +#EMCC core library files will be used +WASM_EMCC_FILES = system/lib/emmalloc.cpp \ + system/lib/libc/musl/src/string/memcpy.c \ + system/lib/libc/musl/src/string/memmove.c \ + system/lib/libc/musl/src/string/memset.c +WASM_EMCC_FILES := $(addprefix $(EMCCROOT)/, $(WASM_EMCC_FILES)) + +# Functions that will be used +WASM_FUNCTIONS = memcpy memmove memset mpeg1_decoder_create mpeg1_decoder_destroy \ + mpeg1_decoder_get_write_ptr mpeg1_decoder_get_index \ + mpeg1_decoder_set_index mpeg1_decoder_did_write \ + mpeg1_decoder_has_sequence_header mpeg1_decoder_get_frame_rate \ + mpeg1_decoder_get_coded_size mpeg1_decoder_get_width \ + mpeg1_decoder_get_height mpeg1_decoder_get_y_ptr \ + mpeg1_decoder_get_cr_ptr mpeg1_decoder_get_cb_ptr \ + mpeg1_decoder_decode mp2_decoder_create mp2_decoder_destroy \ + mp2_decoder_get_write_ptr mp2_decoder_get_index \ + mp2_decoder_set_index mp2_decoder_did_write \ + mp2_decoder_get_left_channel_ptr mp2_decoder_get_right_channel_ptr \ + mp2_decoder_get_sample_rate mp2_decoder_decode + +WASM_FUNCTIONS := "EXPORTED_FUNCTIONS=[$(foreach i,$(WASM_FUNCTIONS),'_$(i)',)]" +STR_NEEDLE := ,] +WASMCCFLAGS += -s $(subst $(STR_NEEDLE),],$(WASM_FUNCTIONS)) + +VPATH = src: $(SRC_DIR) +vpath %.js $(SRC_DIR) +vpath %.c $(WASM_SRC_DIR) + +.PHONY: all clean $(PROJECT_NAME).js $(PROJECT_NAME).min.js $(PROJECT_NAME).wasm.js $(PROJECT_NAME).wasm + +# *********************************** RULES ************************************ + +all: $(PROJECT_NAME).min.js +clean: + rm -rf $(BUILD_DIR) + +# Build minified JS file +$(PROJECT_NAME).min.js: $(BUILD_DIR)/$(PROJECT_NAME).min.js +$(BUILD_DIR)/$(PROJECT_NAME).min.js: $(BUILD_DIR)/$(PROJECT_NAME).js + @mkdir -p $(@D) + $(MINJS) $? -o $@ + +# Build concatenated JS file +$(PROJECT_NAME).js: $(BUILD_DIR)/$(PROJECT_NAME).js +$(BUILD_DIR)/$(PROJECT_NAME).js: $(JS_SRC_FILES) $(BUILD_DIR)/$(PROJECT_NAME).wasm.js + @mkdir -p $(@D) + cat $? >> $@ + +# Build the WASM code embedded in JS file +$(PROJECT_NAME).wasm.js: $(BUILD_DIR)/$(PROJECT_NAME).wasm.js +$(BUILD_DIR)/$(PROJECT_NAME).wasm.js: $(BUILD_DIR)/$(PROJECT_NAME).wasm.b64 + @mkdir -p $(@D) + echo -n "JSMpeg.WASM_BINARY_INLINED='" >> $@ + cat $? >> $@ + echo "';" >> $@ + +# Genereate base64 file with WASM code +$(BUILD_DIR)/$(PROJECT_NAME).wasm.b64: $(BUILD_DIR)/$(PROJECT_NAME).wasm + @mkdir -p $(@D) + $(B64) $(B64FLAGS) $? >> $@ + +# Build the WASM code +$(PROJECT_NAME).wasm: $(BUILD_DIR)/$(PROJECT_NAME).wasm +$(BUILD_DIR)/$(PROJECT_NAME).wasm: $(WASM_SRC_FILES) + @mkdir -p $(@D) + $(WASMCC) $(WASM_SRC_FILES) $(WASM_EMCC_FILES) $(WASMCCFLAGS) $(WASMCCEXTRAFLAGS) -o $@