Top Banner
BINARY DATA ADVENTURES IN BROWSER JAVASCRIPT OR HILTCH / CTO @ STREAMRAIL
26

BINARY DATA ADVENTURES IN BROWSER JAVASCRIPT

Aug 19, 2015

Download

Technology

Or Hiltch
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

BINARY DATA ADVENTURES IN BROWSER JAVASCRIPT

OR HILTCH / CTO @ STREAMRAIL

Page 2: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

INTRO• Background • Evolution in the browser • The Binary Arsenal in JS (type system, inputs, outputs) • Solving Cool, Practical Problems

001101100111001001010101001000011

101010111010000010101001010101001

010100101110101010010101010100101

0101010111110010101000101011101001

01010101111010000001010101000011

010110010101010101111000010101010

Page 3: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

TRADITIONALLY• Outside of JS:

Low level stuff: Compression (gzip/deflate), Bitfields (ACLs), Networks (CRC32), Graphics (algorithms, positioning), ...

• Inside of JS: Solving weird issues

Page 4: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

$http.post('/someUrl', {score:42}). success(function(data, status, headers, config) { // not today :-( }). error(function(data, status, headers, config) { // data=Object {‘error’: ‘Float64 is not int’} }

Page 5: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

$http.post('/someUrl', {score:42}). success(function(data, status, headers, config) { // not today :-( }). error(function(data, status, headers, config) { // data=Object {‘error’: ‘Float64 is not int’} }

Page 6: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

typeof 3.14159 === typeof 3

Page 7: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

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

Page 8: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT
Page 9: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

BROWSERS, EVOLVED• Type system: Blob, ArrayBuffer, TypedArray, DataView • Inputs:

Input methods: XHR 2, File API, Canvas, WebSockets, WebRTC, ... • Outputs:

MSE, Canvas, WebSockets, WebRTC, WebGL, ...

Page 10: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

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:

Page 11: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

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)

Page 12: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

‘USE ASM’sys/libkern/strlen.c size_t strlen(const char *str) { const char *s; for (s = str; *s; ++s); return(s - str); }

asm.js function strlen(ptr) { ptr = ptr|0; var curr = 0; curr = ptr; while (MEM8[curr]|0 != 0) { curr = (curr + 1)|0; } return (curr - ptr)|0; }

Page 13: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

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’

Page 14: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT
Page 15: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT
Page 16: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

KRIPKEN TO THE RESCUEC/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

Page 17: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

• 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

Page 18: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

emscripten in real life

Page 19: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

• 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

Page 20: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

• 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

Page 21: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

YUV -> RGB• Luma + (2 * Chroma) vs

3 * (Chroma + Luma)

• Different needs, different inputs

• Color space conversion heavily computational (floating point coefficients matrix multiplication)

Page 22: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

WEBGL RASTERIZATION

Page 23: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

function yuv2canvas(buffer, width, height) { var lumaSize = width * height; var chromaSize = lumaSize >> 2;

webGLCanvas.YTexture .fill(buffer.subarray(0, lumaSize));

webGLCanvas.UTexture .fill(buffer.subarray(lumaSize, lumaSize + chromaSize));

webGLCanvas.VTexture .fill(buffer.subarray(lumaSize + chromaSize, lumaSize + 2 * chromaSize));

webGLCanvas.drawScene(); }

4:2:0

=

+

Page 24: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

precision highp float; varying highp vec2 vTextureCoord; uniform sampler2D YTexture; uniform sampler2D UTexture; uniform sampler2D VTexture; const mat4 YUV2RGB = mat4 ( 1.1643828125, 0 , 1.59602734375, -.87078515625, 1.1643828125, -0.39176171875, -0.81296875 , .52959375, 1.1643828125, 2.017234375 , 0 , -1.081390625, 0 , 0 , 0 , 1 );

void main(void) { gl_FragColor = vec4(texture2D(YTexture, vTextureCoord).x, texture2D(UTexture, vTextureCoord).x, texture2D(VTexture, vTextureCoord).x,

1) * YUV2RGB; }

<SCRIPT TYPE="X-SHADER/X-FRAGMENT">

Page 25: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

LIVE DEMO

Page 26: BINARY DATA ADVENTURES  IN BROWSER JAVASCRIPT

THANK YOU!

OR HILTCH / CTO @ STREAMRAIL