My YGLF 2015 Talk
Recently browsers have been introduced with interesting tools for work with binary stream of data. Technologies like XHR2 and File API allow us to fetch binary blobs from urls and the file system, and from there, a whole new world is opened before us: we can use Media Source Extensions to implement live streaming audio/video protocols like DASH and HLS, WebRTC to transmit data P2P bittorrent style, WebGL to draw shapes from arrays of binary position data, and more. Furthermore, using technologies like asm.js and tools like Emscripten to transpile C++ code to JavaScript, we can do amazing things with all of the above, and achieve superior performance. In this talk we'll explore some of these techniques, and learn about how we are solving interesting problems with them.
7. In JS, all numbers are of type Float64
But all bitwise ops are Int32!
Bitwise OR: number | 0
Bitwise NOT: ~~number
Shift: number >> 0
parseInt: parseInt(number, 10)
floor: Math.floor(number)
modulo: number - number % 1
10. WHAT MAKES
TYPED ARRAYS FAST
• Passed to native interfaces completely AS IS (direct memory access)
• Native “endianness” - watch out!
• DataView adapter
Byte order
0x12345678 Hex inside a Uint32Array of 4
Little-endian: 78 56 34 12
12 34 56 78Big-endian:
11. STREAMING VIDEO
WITH JS
• No plugins (Netflix: silverlight, Facebook: flash)
• MSE: Pure JS DASH/HLS (YouTube)
• DRM Support (EME)
• MSE + WebRTC: Bittorrent, in the browser! (WebTorrent)
13. a subset of JS for handling numbers faster
• OdinMonkey - AOT in FF (fallback to IonMonkey JIT)
• Support by all major browsers (to an extent)
• Typed Array used as “memory”
• All add/sub are 32 bit (number | 0)
• DI: function(stdlib, foreign, heap)
‘USE ASM’
14.
15.
16. KRIPKEN TO THE RESCUE
C/C++ > LLVM > Emscripten > asm.js!
• Huge code bases (SQLite, BananaBread,J2ME VM, …)
- close to impossible by hand
• Enjoy Clang/LLVM optimizations
- decades of work done to optimize compiled code
• OpenGL > WebGL “for free”
- directly map a lot of OpenGL ES 2.0 command to WebGL
- not only for graphics rendering, also for GPU offloading
17. • Use Docker to run Emscripten
- lots of software (emscripten, emscripten-fastcomp, emscripten-
fastcomp-clang, llvm clang)
- build your own or use one from DockerHub
KRIPKEN TO THE RESCUE
19. • Obtain bytes of video (and audio) by downloading them
• Decode the video (and audio) using native decoders in the browser/OS
• Animate the frames to create the video experience
VIDEO TAG EMULATION
20. • Obtain bytes of video (and audio) by downloading them
> XHR 2.0, response type “arraybuffer”
• Decode the video (and audio) using native decoders in the browser/OS
> Cross compile a native decoder to JS using Emscripten
• Animate the frames to create the video experience
> For each decoded pixel, create a 2D Texture, transform it to RGBA using
a fragment shader, and render it with WebGL using canvas
(“experimental-webgl”).
VIDEO TAG EMULATION
21. YUV -> RGB
• Luma + (2 * Chroma) vs
3 * (Chroma + Luma)
• Different needs, different inputs
• Color space conversion heavily
computational (floating point coefficients
matrix multiplication)