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

fix: 增加主动连接

parent f3e5c915
......@@ -40,7 +40,7 @@ export function Home() {
{
let dev = devices[i];
console.log(dev);
if (dev.vendorId != 0x10d6) continue;
if (dev.vendorId != 0x10d6 && dev.productId != 0x3887) continue;
await dev.open();
let inst = new Jensen(Logger, dev);
await inst.initialize();
......@@ -52,15 +52,16 @@ export function Home() {
return;
}
}
// let dev = await usb.requestDevice({
// filters: [{ vendorId: 0x10d6 }]
// });
// if (!dev) return alert('没有找到设备');
// await dev.open();
// jensen.setUSBDevice(dev);
// await jensen.initialize();
// let info = await jensen.getDeviceInfo();
// setDsn(info.sn);
let dev = await usb.requestDevice({
filters: [{ vendorId: 0x10d6 }, { vendorId: 0x3887 }]
});
if (!dev) return alert('没有找到设备');
await dev.open();
let inst = new Jensen(Logger, dev);
await inst.initialize();
let info = await inst.getDeviceInfo();
setDsn(info.sn);
setJensen(inst);
}
const disconnectBTDevice = async () => {
......@@ -307,10 +308,23 @@ export function Home() {
mediaSource.endOfStream();
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 () => {
let idx = prompt('请输入文件序号', '0');
if (idx === null || idx === undefined) return;
......@@ -320,21 +334,7 @@ export function Home() {
jensen.transferFile(file.name, file.length, (data : Uint8Array | 'fail') => {
if (data instanceof Uint8Array)
{
// 发送到audio-worker中进行解码
// 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);
}
enqueue(data);
}
});
}
......
......@@ -649,9 +649,10 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) {
};
Jensen.prototype.listFiles = async function () {
let tag = 'filelist-' + this.serialNumber;
let tag = 'filelist-' + this.serialNumber + '-' + this.sequence();
if (this[tag] != null) return null;
/*
let fc = null;
if (typeof this.versionNumber == 'undefined' || this.versionNumber <= 327722) {
fc = await this.getFileCount(5);
......@@ -659,15 +660,24 @@ Jensen.prototype.listFiles = async function () {
}
if (fc && fc.count == 0) return null;
*/
let self = this;
// let tag = 'data_' + new Date().getTime();
this[tag] = [];
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;
return [];
}
if (jensen[tag] == null)
{
console.trace('no tag for: ' + tag);
return 'x';
}
jensen[tag].push(msg.body);
let data = [];
let files = [];
......@@ -754,19 +764,46 @@ Jensen.prototype.listFiles = async function () {
length: flen,
signature: sign.join('')
});
// console.log(`Sign1: ${sign.join('')}, Sign2: ${sign2}`);
}
// if (fcount == -1 && (fc))
// 如果没有判断数量的依据
if (fcount == -1) {
// return [];
}
if ((fc && files.length >= fc.count) || (fcount > -1 && files.length >= fcount)) {
// delete jensen[tag];
jensen[tag] = null;
return files.filter((f) => {
return Boolean(f.time);
});
// 如果files有重复的signature,那就持续改为不重复的为止
// Check for duplicate signatures and regenerate if needed
const signatureCounts = {};
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));
......
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