build.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/sh
  2. # Build the .wasm Module first
  3. # Since we're compiling a side module here, so that we can load it without the
  4. # runtime cruft, we have to explicitly compile in support for malloc and
  5. # friends.
  6. # Note memcpy, memmove and memset are explicitly exported, otherwise they will
  7. # be eliminated by the SIDE_MODULE=2 setting - not sure why that happens.
  8. # This NEEDS to be compiled with emscripten 1.38.47. Newer versions mess with
  9. # malloc and friends and need some more glue code for side modules that I
  10. # haven't quite worked out yet. If you have any idea how to build a SIDE_MODULE
  11. # (or STANDALONE_WASM - as seems to be the new deal) with support for malloc,
  12. # please let me know or file a PR.
  13. # To install the correct version, issue the following in your emsdk directory:
  14. # ./emsdk install 1.38.47
  15. # ./emsdk activate 1.38.47
  16. # source ./emsdk_env.sh
  17. # The $EMSCRIPTEN_LIB var needs to point to the correct directory within the sdk
  18. # that has emmalloc.cpp. This is usually $EMSDK/fastcomp/emscripten/system/lib
  19. # but it might differ per system. I don't know.
  20. # There used to be an $EMSCRIPTEN var set by the emsdk_env script that pointed
  21. # to the correct directory, but this seems to have gone now.
  22. # In conclusion, emscripten encapsulates everything that I hate about native
  23. # development :/
  24. EMSCRIPTEN_LIB=$EMSDK/fastcomp/emscripten/system/lib
  25. emcc \
  26. src/wasm/mpeg1.c \
  27. src/wasm/mp2.c \
  28. src/wasm/buffer.c \
  29. $EMSCRIPTEN_LIB/emmalloc.cpp \
  30. $EMSCRIPTEN_LIB/libc/musl/src/string/memcpy.c \
  31. $EMSCRIPTEN_LIB/libc/musl/src/string/memmove.c \
  32. $EMSCRIPTEN_LIB/libc/musl/src/string/memset.c \
  33. -s WASM=1 \
  34. -s SIDE_MODULE=2 \
  35. -s TOTAL_STACK=5242880\
  36. -s USE_PTHREADS=0 \
  37. -s LEGALIZE_JS_FFI=0\
  38. -s NO_FILESYSTEM=1 \
  39. -s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE="[]" \
  40. -s "EXPORTED_FUNCTIONS=[
  41. '_memcpy',
  42. '_memmove',
  43. '_memset',
  44. '_mpeg1_decoder_create',
  45. '_mpeg1_decoder_destroy',
  46. '_mpeg1_decoder_get_write_ptr',
  47. '_mpeg1_decoder_get_index',
  48. '_mpeg1_decoder_set_index',
  49. '_mpeg1_decoder_did_write',
  50. '_mpeg1_decoder_has_sequence_header',
  51. '_mpeg1_decoder_get_frame_rate',
  52. '_mpeg1_decoder_get_coded_size',
  53. '_mpeg1_decoder_get_width',
  54. '_mpeg1_decoder_get_height',
  55. '_mpeg1_decoder_get_y_ptr',
  56. '_mpeg1_decoder_get_cr_ptr',
  57. '_mpeg1_decoder_get_cb_ptr',
  58. '_mpeg1_decoder_decode',
  59. '_mp2_decoder_create',
  60. '_mp2_decoder_destroy',
  61. '_mp2_decoder_get_write_ptr',
  62. '_mp2_decoder_get_index',
  63. '_mp2_decoder_set_index',
  64. '_mp2_decoder_did_write',
  65. '_mp2_decoder_get_left_channel_ptr',
  66. '_mp2_decoder_get_right_channel_ptr',
  67. '_mp2_decoder_get_sample_rate',
  68. '_mp2_decoder_decode']" \
  69. -O3 \
  70. -o jsmpeg.wasm
  71. # Concat all .js sources
  72. cat \
  73. src/jsmpeg.js \
  74. src/video-element.js \
  75. src/player.js \
  76. src/buffer.js \
  77. src/ajax.js \
  78. src/fetch.js \
  79. src/ajax-progressive.js \
  80. src/websocket.js \
  81. src/ts.js \
  82. src/decoder.js \
  83. src/mpeg1.js \
  84. src/mpeg1-wasm.js \
  85. src/mp2.js \
  86. src/mp2-wasm.js \
  87. src/webgl.js \
  88. src/canvas2d.js \
  89. src/webaudio.js \
  90. src/wasm-module.js \
  91. > jsmpeg.js
  92. # Append the .wasm module to the .js source as base64 string
  93. echo "JSMpeg.WASM_BINARY_INLINED='$(base64 -w 0 jsmpeg.wasm)';" \
  94. >> jsmpeg.js
  95. # Minify
  96. uglifyjs jsmpeg.js -o jsmpeg.min.js
  97. # Cleanup
  98. rm jsmpeg.js
  99. rm jsmpeg.wasm