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
@@ -1,18 +1,35 @@
import {perform,type Sensor,type SensorTransport} from '../../../../packages/sensor-ui/src/contracts';
import {perform,SensorOperationError,type Sensor,type SensorTransport} from '../../../../packages/sensor-ui/src/contracts';
import {currentObservationDevice,observationOperation} from '../../../../packages/sensor-ui/src/observationStart';
import {cameraStatus} from './model';
export function startX4Observation(device:Sensor,transport:SensorTransport,restart=false){
export function startX4Observation(device:Sensor,transport:SensorTransport,restart=false,allowRecovery=true,onRecovery?:()=>void){
return observationOperation(device,async()=>{
let current=await currentObservationDevice(device,transport);let status=cameraStatus(current);
if(!status.connected)throw new Error('Камера не подключена.');
if(status.preview===1&&!restart)return;
if(status.preview===1&&!restart){
// An active SDK flag does not prove that its decoder still receives frames.
// Verification must not interrupt recording (including an unknown recording state).
if(status.recording!==0)return 'active';
try{await perform(transport,current,'verify',{},20000);return 'active';}
catch(error){
// Installed brokers expose a stable public message, not the internal error code.
// Only this confirmed terminal result permits recovery; unknown/transport failures do not.
if(!allowRecovery||!(error instanceof SensorOperationError)||error.action!=='verify'||error.state!=='error'||error.message!=='Не поступают свежие кадры. Остановите и снова начните просмотр.')throw error;
}
current=await currentObservationDevice(device,transport);status=cameraStatus(current);
if(status.preview!==1)throw new Error('Состояние просмотра изменилось. Повторите просмотр.');
restart=true;
}
if(restart){
if(!status.connected||![0,1].includes(status.preview??-1))throw new Error('Состояние просмотра пока не подтверждено.');
if(status.recording!==0)throw new Error('Не удалось восстановить просмотр во время записи.');
onRecovery?.();
if(status.preview===1)await perform(transport,current,'preview.stop');
current=await currentObservationDevice(device,transport);status=cameraStatus(current);
if(!status.connected)throw new Error('Камера не подключена.');
if(status.recording!==0)throw new Error('Состояние записи изменилось. Повторите просмотр после её завершения.');
}else if(status.preview!==0)throw new Error('Состояние просмотра пока не подтверждено.');
await perform(transport,current,'preview.start');
return restart?'restarted':'started';
});
}