feat(video): recover camera previews and reduce encoding and playout cost

Improve camera recovery, shared preview lifecycle and receiver playout diagnostics. Preserve qualified X4 runtime changes and restore async admission in loopback media tests.
This commit is contained in:
DCCONSTRUCTIONS
2026-10-29 09:10:01 +03:00
parent e06ff9a214
commit fa8bde584f
94 changed files with 8892 additions and 375 deletions
@@ -0,0 +1,41 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {build} from 'esbuild';
const result=await build({entryPoints:[new URL('../../../packages/sensor-ui/src/useObservationStart.ts',import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm',plugins:[{name:'fixture',setup(b){
b.onResolve({filter:/^react$/},()=>({path:'react',namespace:'fixture'}));
b.onLoad({filter:/.*/,namespace:'fixture'},()=>({contents:'export const useState=v=>globalThis.startUI.state(v),useRef=v=>globalThis.startUI.ref(v),useEffect=(f,d)=>globalThis.startUI.effect(f,d),useCallback=(f,d)=>globalThis.startUI.callback(f,d);'}));
}}]});
const {useObservationStart}=await import('data:text/javascript;base64,'+Buffer.from(result.outputFiles[0].contents).toString('base64'));
async function fixture(run){
const keys=['startUI','window','document','performance','setTimeout','clearTimeout'],old=new Map(keys.map(k=>[k,globalThis[k]]));
const values=[],effects=[],tasks=[],timers=new Map(),events=new Map();let cursor=0,dirty=true,now=0,id=0,closed=false;
const h={enabled:true,active:false,recover:true,view:null,calls:0,fail:true,
state(v){const i=cursor++;if(!(i in values))values[i]=v;return[values[i],v=>{values[i]=typeof v==='function'?v(values[i]):v;dirty=true;}];},
ref(v){return values[cursor++]??={current:v};},callback(f){return values[cursor++]??=f;},
effect(f,d){const i=cursor++,old=effects[i];if(!old||d.some((v,j)=>v!==old.d[j])){old?.cleanup?.();effects[i]={d};tasks.push(()=>{effects[i].cleanup=f();});}},
async start(){h.calls++;if(h.fail)throw Error('temporary timeout');},
async settle(){dirty=true;for(let n=0;n<35;n++){await Promise.resolve();if(dirty&&!closed){dirty=false;cursor=0;h.view=useObservationStart({id:'camera',snapshot:{context:{session_id:'s',execution:{node_id:'n'}}}},h.enabled,h.active,h.start,h.recover);tasks.splice(0).forEach(f=>f());}}},
async advance(ms){now+=ms;for(const[k,v]of [...timers])if(v.at<=now){timers.delete(k);v.f();}await h.settle();},
close(){closed=true;effects.forEach(e=>e?.cleanup?.());},timers,events,
};
const eventAPI={addEventListener:(key,f)=>events.set(key,f),removeEventListener:(key,f)=>{if(events.get(key)===f)events.delete(key);}};
globalThis.startUI=h;globalThis.window={...eventAPI};globalThis.document={...eventAPI,visibilityState:'visible'};globalThis.performance={now:()=>now};
globalThis.setTimeout=(f,delay)=>{timers.set(++id,{f,at:now+delay});return id;};globalThis.clearTimeout=id=>timers.delete(id);
try{await run(h);}finally{h.close();await h.settle();for(const[k,v]of old){if(v===undefined)delete globalThis[k];else globalThis[k]=v;}}
}
test('temporary activation failures retry with one bounded timer and stop after live recovery',()=>fixture(async h=>{
await h.settle();assert.equal(h.calls,1);assert.equal(h.timers.size,1);
await h.advance(1999);assert.equal(h.calls,1);await h.advance(1);assert.equal(h.calls,2);
await h.advance(3999);assert.equal(h.calls,2);await h.advance(1);assert.equal(h.calls,3);
h.fail=false;h.events.get('online')();await h.advance(0);assert.equal(h.calls,4);
h.active=true;await h.settle();await h.advance(60000);assert.equal(h.calls,4);assert.equal(h.view.error,'');assert.equal(h.timers.size,0);
}));
test('disabled, hidden and unmounted views cannot keep retrying activation',()=>fixture(async h=>{
await h.settle();document.visibilityState='hidden';h.events.get('visibilitychange')();await h.advance(60000);assert.equal(h.calls,1);
document.visibilityState='visible';h.events.get('visibilitychange')();await h.advance(0);assert.equal(h.calls,2);
h.enabled=false;await h.settle();await h.advance(60000);assert.equal(h.calls,2);assert.equal(h.timers.size,0);
h.close();assert.equal(h.events.size,0);
}));
test('other acquisition callers keep explicit-retry behavior unless opted in',()=>fixture(async h=>{
h.recover=false;await h.settle();await h.advance(60000);assert.equal(h.calls,1);assert.equal(h.timers.size,0);
}));