feat(rerun): retain live playback and recover isolated viewer resources
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import type {LiveRerunViewer} from './rerunHost';
|
||||
|
||||
export type LivePlaybackState = {
|
||||
recordingId:string|null; rangeNs:{min:number;max:number}|null;
|
||||
currentNs:number|null; playing:boolean; live:boolean;
|
||||
};
|
||||
export const emptyLivePlayback:LivePlaybackState={recordingId:null,rangeNs:null,currentNs:null,playing:false,live:true};
|
||||
|
||||
/** Controls only the native retained recording. Never closes its input channel. */
|
||||
export function liveRerunPlayback(viewer:LiveRerunViewer){
|
||||
let state:LivePlaybackState={...emptyLivePlayback};
|
||||
const timeline='stream_time';
|
||||
const clamp=(time:number,range:{min:number;max:number})=>Math.min(Math.max(time,range.min),range.max);
|
||||
const read=()=>{
|
||||
const id=viewer.get_active_recording_id();
|
||||
if(!id)return state;
|
||||
if(id!==state.recordingId)state={...emptyLivePlayback,recordingId:id};
|
||||
const next=viewer.get_time_range(id,timeline);
|
||||
if(!next||!Number.isFinite(next.min)||!Number.isFinite(next.max)||next.max<=next.min)return state;
|
||||
const previous=state.rangeNs;
|
||||
const current=viewer.get_current_time?.(id,timeline)??next.max;
|
||||
if(state.live){
|
||||
// Advance only when new data arrives, not on every status poll. The native
|
||||
// store remains open while a historical cursor is paused or playing.
|
||||
if(!previous||next.max!==previous.max){
|
||||
viewer.set_current_time(id,timeline,next.max);viewer.set_playing(id,false);
|
||||
}
|
||||
state={...state,rangeNs:next,currentNs:next.max,playing:true};
|
||||
}else{
|
||||
const bounded=clamp(current,next);
|
||||
if(bounded!==current)viewer.set_current_time(id,timeline,bounded);
|
||||
state={...state,rangeNs:next,currentNs:bounded,playing:viewer.get_playing?.(id)??false};
|
||||
}
|
||||
return state;
|
||||
};
|
||||
const seek=(time:number)=>{
|
||||
if(!Number.isFinite(time))return state;
|
||||
const {recordingId:id,rangeNs:range}=read();if(!id||!range)return state;
|
||||
const currentNs=clamp(time,range);
|
||||
viewer.set_current_time(id,timeline,currentNs);viewer.set_playing(id,false);
|
||||
return state={...state,currentNs,playing:false,live:false};
|
||||
};
|
||||
const setPlaying=(playing:boolean)=>{
|
||||
const {recordingId:id,rangeNs:range,currentNs}=read();if(!id||!range)return state;
|
||||
if(state.live&&!playing)viewer.set_current_time(id,timeline,currentNs??range.max);
|
||||
viewer.set_playing(id,playing);
|
||||
return state={...state,live:false,playing};
|
||||
};
|
||||
const jumpToLive=()=>{
|
||||
const {recordingId:id,rangeNs:range}=read();if(!id||!range)return state;
|
||||
viewer.set_current_time(id,timeline,range.max);viewer.set_playing(id,false);
|
||||
return state={...state,currentNs:range.max,playing:true,live:true};
|
||||
};
|
||||
return {read,seek,setPlaying,jumpToLive};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
export * from './contracts';
|
||||
export type * from './enrollment';
|
||||
export type * from './rerunHost';
|
||||
export * from './useLiveRerunPlayback';
|
||||
export type * from './extensions';
|
||||
export type * from './observation';
|
||||
export {ObservationHeader} from './ObservationHeader';
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
/** Native renderer capability supplied by each host, independent of device APIs. */
|
||||
export interface LiveRerunViewer {
|
||||
readonly ready?:boolean;
|
||||
start:(source:string|string[]|null,parent:HTMLElement,options:{width:string;height:string;hide_welcome_screen:boolean;enable_history:boolean;theme?:'dark'|'light';allow_fullscreen?:boolean;panel_state_overrides?:Record<string,string>})=>Promise<void>;
|
||||
open_channel:(name:string)=>{ready:boolean;send_rrd:(bytes:Uint8Array)=>void;close:()=>void};
|
||||
override_panel_state:(panel:'top'|'blueprint'|'selection'|'time',state:'hidden'|'collapsed'|'expanded')=>void;
|
||||
get_camera_eye?:()=>{position:readonly number[];lookTarget:readonly number[];eyeUp:readonly number[]}|null;
|
||||
set_height_slice?:(lower:number|null,upper:number|null)=>boolean;
|
||||
get_height_bounds?:()=>{min:number;max:number}|null;
|
||||
get_active_recording_id:()=>string|null;
|
||||
get_active_timeline:(id:string)=>string|null;
|
||||
get_current_time?:(id:string,timeline:string)=>number;
|
||||
get_playing?:(id:string)=>boolean;
|
||||
get_time_range:(id:string,timeline:string)=>{min:number;max:number}|null;
|
||||
set_active_timeline:(id:string,timeline:string)=>void;
|
||||
set_current_time:(id:string,timeline:string,time:number)=>void;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import {useEffect,useRef,useState} from 'react';
|
||||
import type {LiveRerunViewer} from './rerunHost';
|
||||
import {emptyLivePlayback,liveRerunPlayback} from './liveRerunPlayback';
|
||||
|
||||
export function useLiveRerunPlayback(viewer:LiveRerunViewer|null,acquisition:string){
|
||||
const controller=useRef<ReturnType<typeof liveRerunPlayback>|null>(null);
|
||||
const [state,setState]=useState(emptyLivePlayback);
|
||||
useEffect(()=>{
|
||||
setState(emptyLivePlayback);
|
||||
if(!viewer)return;
|
||||
const current=liveRerunPlayback(viewer);controller.current=current;
|
||||
const poll=()=>{try{setState(current.read());}catch{/* Native recording may be between frames. */}};
|
||||
poll();const timer=setInterval(poll,250);
|
||||
return()=>{clearInterval(timer);if(controller.current===current)controller.current=null;};
|
||||
},[viewer,acquisition]);
|
||||
const update=(action:(value:NonNullable<typeof controller.current>)=>typeof state)=>{
|
||||
const current=controller.current;if(!current)return;
|
||||
try{setState(action(current));}catch{/* Teardown does not own acquisition control. */}
|
||||
};
|
||||
return {state,seek:(time:number)=>update(c=>c.seek(time)),setPlaying:(playing:boolean)=>update(c=>c.setPlaying(playing)),jumpToLive:()=>update(c=>c.jumpToLive())};
|
||||
}
|
||||
Reference in New Issue
Block a user