Commit 2589667c authored by martin hou's avatar martin hou

fix: 增加主动连接

parent f3e5c915
...@@ -40,7 +40,7 @@ export function Home() { ...@@ -40,7 +40,7 @@ export function Home() {
{ {
let dev = devices[i]; let dev = devices[i];
console.log(dev); console.log(dev);
if (dev.vendorId != 0x10d6) continue; if (dev.vendorId != 0x10d6 && dev.productId != 0x3887) continue;
await dev.open(); await dev.open();
let inst = new Jensen(Logger, dev); let inst = new Jensen(Logger, dev);
await inst.initialize(); await inst.initialize();
...@@ -52,15 +52,16 @@ export function Home() { ...@@ -52,15 +52,16 @@ export function Home() {
return; return;
} }
} }
// let dev = await usb.requestDevice({ let dev = await usb.requestDevice({
// filters: [{ vendorId: 0x10d6 }] filters: [{ vendorId: 0x10d6 }, { vendorId: 0x3887 }]
// }); });
// if (!dev) return alert('没有找到设备'); if (!dev) return alert('没有找到设备');
// await dev.open(); await dev.open();
// jensen.setUSBDevice(dev); let inst = new Jensen(Logger, dev);
// await jensen.initialize(); await inst.initialize();
// let info = await jensen.getDeviceInfo(); let info = await inst.getDeviceInfo();
// setDsn(info.sn); setDsn(info.sn);
setJensen(inst);
} }
const disconnectBTDevice = async () => { const disconnectBTDevice = async () => {
...@@ -307,10 +308,23 @@ export function Home() { ...@@ -307,10 +308,23 @@ export function Home() {
mediaSource.endOfStream(); mediaSource.endOfStream();
audioElement.play(); audioElement.play();
} }
if (mp3.length > 0)
{
sourceBuffer.appendBuffer(mp3.shift());
}
}); });
}); });
} }
const enqueue = (clip: Uint8Array) => {
if (!sourceBuffer) return;
if (!sourceBuffer.updating && mp3.length === 0) {
sourceBuffer.appendBuffer(clip);
} else {
mp3.push(clip);
}
}
const test = async () => { const test = async () => {
let idx = prompt('请输入文件序号', '0'); let idx = prompt('请输入文件序号', '0');
if (idx === null || idx === undefined) return; if (idx === null || idx === undefined) return;
...@@ -320,21 +334,7 @@ export function Home() { ...@@ -320,21 +334,7 @@ export function Home() {
jensen.transferFile(file.name, file.length, (data : Uint8Array | 'fail') => { jensen.transferFile(file.name, file.length, (data : Uint8Array | 'fail') => {
if (data instanceof Uint8Array) if (data instanceof Uint8Array)
{ {
// 发送到audio-worker中进行解码 enqueue(data);
// audioWorker.postMessage(data);
mp3.push(data);
totalBytes += data.length;
if (totalBytes == file.length)
{
let mp3Data = new Uint8Array(totalBytes);
for (let i = 0; i < mp3.length; i++)
{
mp3Data.set(mp3[i], i * mp3[i].length);
}
audioWorker.postMessage(mp3Data);
// 创建MSE实例,把mp3Data发送给MSE进行播放
sourceBuffer?.appendBuffer(mp3Data);
}
} }
}); });
} }
......
...@@ -649,9 +649,10 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) { ...@@ -649,9 +649,10 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) {
}; };
Jensen.prototype.listFiles = async function () { Jensen.prototype.listFiles = async function () {
let tag = 'filelist-' + this.serialNumber; let tag = 'filelist-' + this.serialNumber + '-' + this.sequence();
if (this[tag] != null) return null; if (this[tag] != null) return null;
/*
let fc = null; let fc = null;
if (typeof this.versionNumber == 'undefined' || this.versionNumber <= 327722) { if (typeof this.versionNumber == 'undefined' || this.versionNumber <= 327722) {
fc = await this.getFileCount(5); fc = await this.getFileCount(5);
...@@ -659,15 +660,24 @@ Jensen.prototype.listFiles = async function () { ...@@ -659,15 +660,24 @@ Jensen.prototype.listFiles = async function () {
} }
if (fc && fc.count == 0) return null; if (fc && fc.count == 0) return null;
*/
let self = this; let self = this;
// let tag = 'data_' + new Date().getTime(); // let tag = 'data_' + new Date().getTime();
this[tag] = []; this[tag] = [];
this.registerHandler(QUERY_FILE_LIST, (msg, jensen) => { this.registerHandler(QUERY_FILE_LIST, (msg, jensen) => {
if (msg.body.length == 0) { console.log('tag', tag);
if (msg.body.length == 0)
{
console.log('remove tag1: ' + tag);
jensen[tag] = null; jensen[tag] = null;
return []; return [];
} }
if (jensen[tag] == null)
{
console.trace('no tag for: ' + tag);
return 'x';
}
jensen[tag].push(msg.body); jensen[tag].push(msg.body);
let data = []; let data = [];
let files = []; let files = [];
...@@ -754,19 +764,46 @@ Jensen.prototype.listFiles = async function () { ...@@ -754,19 +764,46 @@ Jensen.prototype.listFiles = async function () {
length: flen, length: flen,
signature: sign.join('') signature: sign.join('')
}); });
// console.log(`Sign1: ${sign.join('')}, Sign2: ${sign2}`);
} }
// if (fcount == -1 && (fc)) // if (fcount == -1 && (fc))
// 如果没有判断数量的依据 // 如果没有判断数量的依据
if (fcount == -1) { if (fcount == -1) {
// return []; // return [];
} }
if ((fc && files.length >= fc.count) || (fcount > -1 && files.length >= fcount)) {
// delete jensen[tag]; // 如果files有重复的signature,那就持续改为不重复的为止
jensen[tag] = null; // Check for duplicate signatures and regenerate if needed
return files.filter((f) => { const signatureCounts = {};
return Boolean(f.time); files.forEach(file => {
signatureCounts[file.signature] = (signatureCounts[file.signature] || 0) + 1;
});
// Only regenerate signatures for items that have duplicates
files.forEach(file => {
if (signatureCounts[file.signature] > 1) {
// Add timestamp to make signature unique
file.signature = md5(`${file.name}${file.length}`);
}
}); });
// 如果已经等待过长的时间了,那就直接返回好了
let kv = tag + '-timer';
let now = new Date().getTime();
if (kv in jensen)
{
window.clearTimeout(jensen[kv]);
} }
jensen[kv] = now;
// 要怎么样触发这个回调?
jensen[kv] = window.setTimeout(() => {
console.log('remove tag: ' + tag);
jensen[tag] = null;
jensen._trigger(files.filter((f) => { return Boolean(f.time); }), QUERY_FILE_LIST);
console.log('jensen', 'list-files', 'timed return');
}, 1000);
}); });
return this.send(new Command(QUERY_FILE_LIST)); return this.send(new Command(QUERY_FILE_LIST));
......
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