Commit 068f3c3a authored by martin hou's avatar martin hou

fix: 取消递归调用,改为循环模式

parent 0ee097e4
...@@ -307,7 +307,7 @@ declare class Jensen { ...@@ -307,7 +307,7 @@ declare class Jensen {
stopRealtime: (seconds?: number) => Promise<ReturnStruct['common']>; stopRealtime: (seconds?: number) => Promise<ReturnStruct['common']>;
// 获取实时音频流数据 // 获取实时音频流数据
getRealtime: (frames: number, mute: boolean, seconds?: number) => Promise<{ data: Uint8Array | null; rest: number, muted: boolean }>; getRealtime: (frames: number, seconds?: number) => Promise<{ data: Uint8Array | null; rest: number, muted: boolean }>;
// 录音文件随机读取,从指定位置起读取指定数量个字节 // 录音文件随机读取,从指定位置起读取指定数量个字节
readFile: (fname: string, offset: number, length: number) => Promise<Uint8Array>; readFile: (fname: string, offset: number, length: number) => Promise<Uint8Array>;
......
...@@ -538,7 +538,7 @@ export function Home() { ...@@ -538,7 +538,7 @@ export function Home() {
const scheduleLiveTick = async () => { const scheduleLiveTick = async () => {
let jensen = getJensen(); let jensen = getJensen();
if (jensen == null) return; if (jensen == null) return;
let live = await jensen.getRealtime(1, muteRef.current, 1); let live = await jensen.getRealtime(1, 1);
// 若无数据直接调度下一次 // 若无数据直接调度下一次
if (live?.data == null) return; if (live?.data == null) return;
setLiveStates('live: ' + live.data.length + ' rest: ' + live.rest + ' muted: ' + live.muted + ' == ' + muteRef.current); setLiveStates('live: ' + live.data.length + ' rest: ' + live.rest + ' muted: ' + live.muted + ' == ' + muteRef.current);
......
...@@ -80,6 +80,10 @@ const COMMAND_NAMES = { ...@@ -80,6 +80,10 @@ const COMMAND_NAMES = {
let Logger = null; let Logger = null;
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
function Jensen(log, conn) { function Jensen(log, conn) {
Logger = log || internalLogger; Logger = log || internalLogger;
let device = conn; let device = conn;
...@@ -95,6 +99,7 @@ function Jensen(log, conn) { ...@@ -95,6 +99,7 @@ function Jensen(log, conn) {
let totalBytes = 0; let totalBytes = 0;
let handlers = {}; let handlers = {};
let pid = 0; let pid = 0;
let recvable = false;
let self = this; let self = this;
this.data = {}; this.data = {};
...@@ -110,6 +115,7 @@ function Jensen(log, conn) { ...@@ -110,6 +115,7 @@ function Jensen(log, conn) {
this.onerror = null; this.onerror = null;
const RECV_BUFF_SIZE = 512000; const RECV_BUFF_SIZE = 512000;
// let cache = new ArrayBuffer(RECV_BUFF_SIZE * 2);
const _check_conn_status = () => { const _check_conn_status = () => {
if (device?.opened === false) { if (device?.opened === false) {
...@@ -172,6 +178,10 @@ function Jensen(log, conn) { ...@@ -172,6 +178,10 @@ function Jensen(log, conn) {
} catch (err) { } catch (err) {
Logger.error('jensen', 'setup', err); Logger.error('jensen', 'setup', err);
} }
setTimeout(async () => {
receive();
}, 0);
}; };
this.initialize = async function() { this.initialize = async function() {
...@@ -335,7 +345,7 @@ function Jensen(log, conn) { ...@@ -335,7 +345,7 @@ function Jensen(log, conn) {
} }
*/ */
totalBytes = 0; totalBytes = 0;
if (recv == false) tryReceive(); if (recv == false) startReceive();
else recv = true; else recv = true;
}; };
...@@ -403,19 +413,13 @@ function Jensen(log, conn) { ...@@ -403,19 +413,13 @@ function Jensen(log, conn) {
} }
}; };
const tryReceive = function () { const startReceive = function () {
if (device) recvable = true;
// console.log('trace', device); }
device.transferIn(2, RECV_BUFF_SIZE).then((r) => {
Logger.save?.('jensen', 'tryReceive', r?.data); const stopReceive = function () {
console.log(pid + '-Receive', new Date().toLocaleString(), r?.data); recvable = false;
receive(r); }
}).catch((e) => {
console.log('tryReceive error', e)
self.onerror?.(e);
});
};
const read_int = function (a, b, c, d) { const read_int = function (a, b, c, d) {
if (arguments.length === 2) { if (arguments.length === 2) {
...@@ -425,24 +429,31 @@ function Jensen(log, conn) { ...@@ -425,24 +429,31 @@ function Jensen(log, conn) {
} }
}; };
const receive = function (result) { const receive = async function () {
if(result instanceof Error) { while (true) {
console.log('receive error', result); let r = await tryReceive();
return if (r == null || r instanceof Error) {
console.log('receive error', r);
await sleep(30);
continue;
}
totalBytes += r.data.byteLength;
blocks.push(r.data);
if (self.decodeTimeout) clearTimeout(self.decodeTimeout);
self.decodeTimeout = setTimeout(function () { tryDecode(); }, self.timewait);
self.onreceive?.(totalBytes);
} }
totalBytes += result.data.byteLength; };
// 做一个回调,怎么样找到它呢?
blocks.push(result.data); const tryReceive = async function () {
tryReceive(); let r = null;
if (self.decodeTimeout) clearTimeout(self.decodeTimeout); if (device && recvable)
self.decodeTimeout = setTimeout(function () { {
tryDecode(); r = await device.transferIn(2, RECV_BUFF_SIZE);
}, self.timewait); Logger.save?.('jensen', 'tryReceive', r?.data);
if (self.onreceive) { console.log(pid + '-Receive', new Date().toLocaleString(), r?.data);
try {
self.onreceive(totalBytes);
} catch (e) {}
} }
return r;
}; };
const tryDecode = function () { const tryDecode = function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment