侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

JS 多线程

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 121 阅读 / 2,810 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
不能操作dom,因为dom传不进来, jQuery也不能用,应为没有document对象,没有window对象,加载时会直接报搓,即使用奇淫技巧加载成功,也不能用来操作dom,因为根本就接触不到dom.
postMessage 和 onMessage 是worker的一个通信机制 , 而且是双向的 都可以postMessage和onMessage
var realTimeWorker = new Worker('js/worker-realtime.js');

realTimeWorker.postMessage({ cmd: 'finish' });

w.terminate();




(function () {
  'use strict';

importScripts('lame.min.js');

var mp3Encoder, maxSamples = 1152, samplesMono, lame, config, dataBuffer;
var ws;
var clearBuffer = function ()
;

var appendToBuffer = function (mp3Buf) {
dataBuffer.push(new Int8Array(mp3Buf));
};

var init = function (prefConfig) {
config = prefConfig || {};
lame = new lamejs();
mp3Encoder = new lame.Mp3Encoder(1, config.sampleRate, config.bitRate);
clearBuffer();
self.postMessage({
cmd: 'init'
});
};

var floatTo16BitPCM = function floatTo16BitPCM(input, output) {
for (var i = 0; i < input.length; i++) {
var s = Math.max(-1, Math.min(1, input[i]));
output[i] = (s < 0 ? s * 0x8000 : s * 0x7FFF);
}
};

var convertBuffer = function (arrayBuffer) {
var data = new Float32Array(arrayBuffer);
var out = new Int16Array(arrayBuffer.length);
floatTo16BitPCM(data, out)
return out;
};

var encode = function (arrayBuffer) {
samplesMono = convertBuffer(arrayBuffer);
var remaining = samplesMono.length;

  for (var i = 0; remaining &gt;= 0; i += maxSamples) {
      var left = samplesMono.subarray(i, i + maxSamples);
      var mp3buf = mp3Encoder.encodeBuffer(left);
	if(mp3buf.length &gt; 0)
	{
		var mp3Data = [];
		mp3Data.push(mp3buf);
		let blob = new Blob(mp3Data, {type: 'audio/mp3'});
		ws.send(blob);
	}
      appendToBuffer(mp3buf);
      remaining -= maxSamples;
  }

};

var finish = function () {
appendToBuffer(mp3Encoder.flush());
self.postMessage({
cmd: 'end',
buf: dataBuffer
});
clearBuffer();
};
var wsinit = 0;
self.onmessage = function (e) {
switch (e.data.cmd) {
case 'init':
init(e.data.config);
break;
case 'encode':
if(wsinit == 0)
{
// var url="http://localhost:8090/vmp/dvDevice/getAudioUrl?id=34020000001320000002@34020000001320000002&format=flv&transporttype=udp";
// var request= new XMLHttpRequest();
// request.open("POST",url,false);
// request.setRequestHeader('Access-Control-Allow-Headers','');
// request.setRequestHeader("Access-Control-Allow-Origin",'
');
//
// request.send(null);
// var obj = JSON.parse(request.responseText);
// var port = obj["TransportPort"];
// var pushUrl = "ws://127.0.0.1:12345/cgmedia/websocket/pushaudiostream?id=34020000001320000002@34020000001320000002&transportport=";
// pushUrl += port;
// pushUrl += "&format=mp3";
var pushUrl = "ws://127.0.0.1:12345/cgmedia/28181/getaudio?id=34020000001320000002@34020000001320000002&format=flv&transportport=20086&transporttype=udp"
ws = new WebSocket(pushUrl);
ws.binaryType = 'arraybuffer';
ws.onmessage = function(evt) {
console.log('Receive Message');
};
ws.onopen = function() {
console.log('连接成功');
};
ws.onclose = function() {
console.log('连接关闭');
};
ws.onerror = function(){
console.log('连接错误');
};
wsinit = 1;
}
encode(e.data.buf);
break;
case 'finish':
finish();
break;
}
};

})();

0

评论区