@@ -44,24 +44,22 @@ export class MediaStreamClip implements IClip {
4444 #ms: MediaStream ;
4545 constructor ( ms : MediaStream ) {
4646 this . #ms = ms ;
47+ this . audioTrack = ms . getAudioTracks ( ) [ 0 ] ?? null ;
48+ this . #meta. duration = Infinity ;
4749 const videoTrack = ms . getVideoTracks ( ) [ 0 ] ;
4850 if ( videoTrack != null ) {
49- const { width, height } = videoTrack . getSettings ( ) ;
5051 videoTrack . contentHint = 'motion' ;
51- this . #meta. width = width ?? 0 ;
52- this . #meta. height = height ?? 0 ;
53-
54- this . #cvs = new OffscreenCanvas ( width ?? 0 , height ?? 0 ) ;
55- this . #stopRenderCvs = renderVideoTrackToCvs (
56- this . #cvs. getContext ( '2d' ) ! ,
57- videoTrack ,
58- ) ;
52+ this . ready = new Promise ( ( resolve ) => {
53+ this . #stopRenderCvs = renderVideoTrackToCvs ( videoTrack , ( cvs ) => {
54+ this . #meta. width = cvs . width ;
55+ this . #meta. height = cvs . height ;
56+ this . #cvs = cvs ;
57+ resolve ( this . meta ) ;
58+ } ) ;
59+ } ) ;
60+ } else {
61+ this . ready = Promise . resolve ( this . meta ) ;
5962 }
60-
61- this . audioTrack = ms . getAudioTracks ( ) [ 0 ] ?? null ;
62-
63- this . #meta. duration = Infinity ;
64- this . ready = Promise . resolve ( this . meta ) ;
6563 }
6664
6765 async tick ( ) : Promise < {
@@ -91,15 +89,26 @@ export class MediaStreamClip implements IClip {
9189}
9290
9391function renderVideoTrackToCvs (
94- cvsCtx : OffscreenCanvasRenderingContext2D ,
9592 track : MediaStreamVideoTrack ,
93+ onOffscreenCanvasReady : ( cvs : OffscreenCanvas ) => void ,
9694) {
95+ let emitFF = false ;
96+ let cvsCtx : OffscreenCanvasRenderingContext2D ;
9797 return autoReadStream (
9898 new MediaStreamTrackProcessor ( {
9999 track,
100100 } ) . readable ,
101101 {
102102 onChunk : async ( frame ) => {
103+ if ( ! emitFF ) {
104+ const { displayHeight, displayWidth } = frame ;
105+ const width = displayWidth ?? 0 ;
106+ const height = displayHeight ?? 0 ;
107+ const cvs = new OffscreenCanvas ( width , height ) ;
108+ cvsCtx = cvs . getContext ( '2d' ) ! ;
109+ onOffscreenCanvasReady ( cvs ) ;
110+ emitFF = true ;
111+ }
103112 cvsCtx . drawImage ( frame , 0 , 0 ) ;
104113 frame . close ( ) ;
105114 } ,
0 commit comments