var Initialized = false var Module = { onRuntimeInitialized: function() { Initialized = true respond_to_messages() } } function vi_to_i_le(vi) { var ret = 0 for(var i = vi.length - 1; i >= 0; i--) { ret = ret * 256 + vi[i] } return ret } var Codecs = {} var ReceivedMessages = [] function respond_to_messages() { if(!Initialized) { return } while(ReceivedMessages.length) { var msg = ReceivedMessages[0] if(msg.cmd == "create") { if(msg.codec == "V_VP8") { var ptr = Module._alloc_codec_vpx(0); Codecs[msg.id] = {type : msg.codec, ptr : ptr} } else if(msg.codec == "V_VP9") { var ptr = Module._alloc_codec_vpx(1); Codecs[msg.id] = {type : msg.codec, ptr : ptr} } else if(msg.codec == "A_VORBIS") { var privptr = Module._av_mallocz(msg.priv.length) Module.HEAPU8.set(msg.priv, privptr) Codecs[msg.id] = {type: msg.codec, ptr: Module._alloc_codec_vorbis(privptr, msg.priv.length), channels: msg.channels} Module._av_free(privptr) } } else if(msg.cmd == "decode" && Codecs[msg.id]) { var packet = Module._av_mallocz(msg.packet.length) Module.HEAPU8.set(msg.packet, packet) if(Codecs[msg.id].type == "V_VP8" || Codecs[msg.id].type == "V_VP9") { var startTime = performance.now() //debugger var status = Module._codec_vpx_push_packet(Codecs[msg.id].ptr, +msg.packet.kf, packet, msg.packet.length) var taken = performance.now() - startTime if(status) { var rgbaPtr = vi_to_i_le(Module.HEAPU8.subarray(Codecs[msg.id].ptr + 0, Codecs[msg.id].ptr + 4)); var w = vi_to_i_le(Module.HEAPU8.subarray(Codecs[msg.id].ptr + 4, Codecs[msg.id].ptr + 6)); var h = vi_to_i_le(Module.HEAPU8.subarray(Codecs[msg.id].ptr + 6, Codecs[msg.id].ptr + 8)); postMessage({t: msg.t, id: msg.id, width: w, height: h, data: Module.HEAPU8.subarray(rgbaPtr, rgbaPtr + 4 * w * h).slice(0), taken: taken}) } } else if(Codecs[msg.id].type == "A_VORBIS") { var startTime = performance.now() var samples = Module._codec_vorbis_push_packet(Codecs[msg.id].ptr, packet, msg.packet.length) var sampleChannelsPtr = vi_to_i_le(Module.HEAPU8.subarray(Codecs[msg.id].ptr, Codecs[msg.id].ptr + 4)) var sampleChannelLPtr = vi_to_i_le(Module.HEAPU8.subarray(sampleChannelsPtr + 0, sampleChannelsPtr + 4)); var sampleChannelRPtr = Codecs[msg.id].channels == 2 ? vi_to_i_le(Module.HEAPU8.subarray(sampleChannelsPtr + 4, sampleChannelsPtr + 8)) : undefined; var left = Module.HEAPF32.subarray(sampleChannelLPtr / 4, sampleChannelLPtr / 4 + samples).slice(0) var right = sampleChannelRPtr && Module.HEAPF32.subarray(sampleChannelRPtr / 4, sampleChannelRPtr / 4 + samples).slice(0) //debugger; postMessage({t: msg.t, id: msg.id, samples: samples, left: left, right: right, taken: performance.now() - startTime}, right ? [left.buffer, right.buffer] : [left.buffer]) } Module._av_free(packet) } ReceivedMessages.shift() } } onmessage = function(e) { ReceivedMessages.push(e.data) respond_to_messages() } importScripts("support.js")