@@ -49,7 +49,7 @@ export const EngineStream = (props: {
49
49
50
50
// These will be passed to the engineStreamActor to handle.
51
51
const videoRef = useRef < HTMLVideoElement > ( null )
52
- const canvasRef = useRef < HTMLVideoElement > ( null )
52
+ const canvasRef = useRef < HTMLCanvasElement > ( null )
53
53
54
54
// For attaching right-click menu events
55
55
const videoWrapperRef = useRef < HTMLDivElement > ( null )
@@ -68,10 +68,31 @@ export const EngineStream = (props: {
68
68
const streamIdleMode = settings . app . streamIdleMode . current
69
69
70
70
useEffect ( ( ) => {
71
- engineStreamActor . send ( { type : SetVideoRef , videoRef } )
72
- engineStreamActor . send ( { type : SetCanvasRef , canvasRef } )
73
- } , [ videoRef . current , canvasRef . current ] )
71
+ engineStreamActor . send ( { type : EngineStreamTransition . SetVideoRef , videoRef } )
72
+ } , [ videoRef . current ] )
74
73
74
+ useEffect ( ( ) => {
75
+ engineStreamActor . send ( { type : EngineStreamTransition . SetCanvasRef , canvasRef } )
76
+ } , [ canvasRef . current ] )
77
+
78
+ useEffect ( ( ) => {
79
+ engineStreamActor . send ( {
80
+ type : EngineStreamTransition . SetPool ,
81
+ pool : props . pool ,
82
+ } )
83
+ } , [ props . pool ] )
84
+
85
+ useEffect ( ( ) => {
86
+ engineStreamActor . send ( {
87
+ type : EngineStreamTransition . SetAuthToken ,
88
+ authToken : props . authToken ,
89
+ } )
90
+ } , [ props . authToken ] )
91
+
92
+ // We have to call this here because of the dependencies:
93
+ // modelingMachineActorSend, setAppState, settingsEngine
94
+ // It's possible to pass these in earlier but I (lee) don't want to
95
+ // restructure this further at the moment.
75
96
const startOrReconfigureEngine = ( ) => {
76
97
engineStreamActor . send ( {
77
98
type : EngineStreamTransition . StartOrReconfigureEngine ,
@@ -87,12 +108,39 @@ export const EngineStream = (props: {
87
108
} )
88
109
}
89
110
90
- // When the scene is ready play the stream and execute!
111
+ useEffect ( ( ) => {
112
+ if ( engineStreamState . value !== EngineStreamState . Configuring ) return
113
+ startOrReconfigureEngine ( )
114
+ } , [ engineStreamState , setAppState ] )
115
+
116
+ // ...Object.values(settingsEngine),
117
+
118
+ // I would inline this but it needs to be a function for removeEventListener.
91
119
const play = ( ) => {
92
120
engineStreamActor . send ( {
93
121
type : EngineStreamTransition . Play ,
94
122
} )
123
+ }
95
124
125
+ useEffect ( ( ) => {
126
+ engineCommandManager . addEventListener (
127
+ EngineCommandManagerEvents . SceneReady ,
128
+ play
129
+ )
130
+ return ( ) => {
131
+ engineCommandManager . removeEventListener (
132
+ EngineCommandManagerEvents . SceneReady ,
133
+ play
134
+ )
135
+ }
136
+ } , [ ] )
137
+
138
+ // When the scene is ready play the stream and execute!
139
+ const executeKcl = ( ) => {
140
+ // It's ok to position the camera before modeling commands
141
+ sceneInfra . camControls . restoreRemoteCameraStateAndTriggerSync ( ) . catch ( trap )
142
+
143
+ console . log ( 'scene is ready, execute kcl' )
96
144
const kmp = kclManager . executeCode ( ) . catch ( trap )
97
145
98
146
if ( ! firstPlay ) return
@@ -101,7 +149,7 @@ export const EngineStream = (props: {
101
149
// Reset the restart timeouts
102
150
setAttemptTimes ( [ 0 , 1000 ] )
103
151
104
- console . log ( 'scene is ready, execute kcl ' )
152
+ console . log ( 'firstPlay true, zoom to fit ' )
105
153
106
154
kmp
107
155
. then ( ( ) =>
@@ -125,91 +173,39 @@ export const EngineStream = (props: {
125
173
useEffect ( ( ) => {
126
174
engineCommandManager . addEventListener (
127
175
EngineCommandManagerEvents . SceneReady ,
128
- play
176
+ executeKcl
129
177
)
130
178
return ( ) => {
131
- engineCommandManager . removeEventListener (
132
- EngineCommandManagerEvents . SceneReady ,
133
- play
134
- )
179
+ engineStreamActor . send ( { type : EngineStreamTransition . Pause } )
135
180
}
136
- } , [ firstPlay ] )
137
-
138
- // We do a back-off restart, using a fibonacci sequence, since it
139
- // has a nice retry time curve (somewhat quick then exponential)
140
- const restart = ( ) => {
141
- setTimeout ( ( ) => {
142
- setFirstPlay ( false )
143
- setAttemptTimes ( [ attemptTimes [ 1 ] , attemptTimes [ 0 ] + attemptTimes [ 1 ] ] )
144
- engineCommandManager . tearDown ( )
145
- startOrReconfigureEngine ( )
146
- } , attemptTimes [ 0 ] + attemptTimes [ 1 ] )
147
- }
181
+ } , [ ] )
148
182
149
183
useEffect ( ( ) => {
150
- engineCommandManager . addEventListener (
151
- EngineCommandManagerEvents . SceneReady ,
152
- play
153
- )
184
+ // We do a back-off restart, using a fibonacci sequence, since it
185
+ // has a nice retry time curve (somewhat quick then exponential)
186
+ const restart = ( ) => {
187
+ setTimeout ( ( ) => {
188
+ setFirstPlay ( false )
189
+ setAttemptTimes ( [ attemptTimes [ 1 ] , attemptTimes [ 0 ] + attemptTimes [ 1 ] ] )
190
+ engineStreamState . context . videoRef . current . pause ( )
191
+ engineCommandManager . tearDown ( )
192
+ startOrReconfigureEngine ( )
193
+ } , attemptTimes [ 0 ] + attemptTimes [ 1 ] )
194
+ }
195
+
154
196
engineCommandManager . addEventListener (
155
197
EngineCommandManagerEvents . EngineRestartRequest ,
156
198
restart
157
199
)
158
- engineStreamActor . send ( {
159
- type : EngineStreamTransition . SetPool ,
160
- data : { pool : props . pool } ,
161
- } )
162
- engineStreamActor . send ( {
163
- type : EngineStreamTransition . SetAuthToken ,
164
- data : { authToken : props . authToken } ,
165
- } )
166
-
167
200
return ( ) => {
168
- engineStreamActor . send ( { type : EngineStreamTransition . Pause } )
169
- }
170
- } , [ ] )
171
-
172
- // In the past we'd try to play immediately, but the proper thing is to way
173
- // for the 'canplay' event to tell us data is ready.
174
- useEffect ( ( ) => {
175
- const videoRef = engineStreamState . context . videoRef . current
176
- if ( ! videoRef ) {
177
- return
178
- }
179
-
180
- const onCanPlay = ( ) => {
181
- videoRef . play ( ) . catch ( console . error )
182
- }
183
-
184
- // I (lee) believe that the engine handles serving video first better
185
- // than receiving commands when it's expected to. Only when the
186
- // browser reports we're receiving video do we trigger KCL execution.
187
- const onPlay = ( ) => {
188
- if ( ! engineCommandManager . engineConnection ) { return }
189
-
190
- engineCommandManager . engineConnection . state = {
191
- type : EngineConnectionStateType . ConnectionEstablished ,
192
- }
193
-
194
- engineCommandManager . inSequence = 1
195
-
196
- engineCommandManager . engineConnection . dispatchEvent (
197
- new CustomEvent ( EngineConnectionEvents . Opened , {
198
- detail : engineCommandManager . engineConnection ,
199
- } )
201
+ engineCommandManager . removeEventListener (
202
+ EngineCommandManagerEvents . EngineRestartRequest ,
203
+ restart
200
204
)
201
- markOnce ( 'code/endInitialEngineConnect' )
202
205
}
206
+ } , [ engineStreamState , attemptTimes ] )
203
207
204
- videoRef . addEventListener ( 'canplay' , onCanPlay )
205
- videoRef . addEventListener ( 'play' , onPlay )
206
- return ( ) => {
207
- videoRef . removeEventListener ( 'canplay' , onCanPlay )
208
- videoRef . removeEventListener ( 'play' , onPlay )
209
- }
210
- } , [ engineStreamState . context . videoRef . current ] )
211
-
212
- useEffect ( ( ) => {
208
+ useEffect ( ( ) => {
213
209
if ( engineStreamState . value === EngineStreamState . Reconfiguring ) return
214
210
const video = engineStreamState . context . videoRef ?. current
215
211
if ( ! video ) return
@@ -235,12 +231,6 @@ export const EngineStream = (props: {
235
231
} ) . observe ( document . body )
236
232
} , [ engineStreamState . value ] )
237
233
238
- // On settings change, reconfigure the engine. When paused this gets really tricky,
239
- // and also requires onMediaStream to be set!
240
- useEffect ( ( ) => {
241
- startOrReconfigureEngine ( )
242
- } , Object . values ( settingsEngine ) )
243
-
244
234
/**
245
235
* Subscribe to execute code when the file changes
246
236
* but only if the scene is already ready.
@@ -316,18 +306,7 @@ export const EngineStream = (props: {
316
306
}
317
307
318
308
if ( engineStreamState . value === EngineStreamState . Paused ) {
319
- engineStreamActor . send ( {
320
- type : EngineStreamTransition . StartOrReconfigureEngine ,
321
- modelingMachineActorSend,
322
- settings : settingsEngine ,
323
- setAppState,
324
- onMediaStream ( mediaStream : MediaStream ) {
325
- engineStreamActor . send ( {
326
- type : EngineStreamTransition . SetMediaStream ,
327
- mediaStream,
328
- } )
329
- } ,
330
- } )
309
+ startOrReconfigureEngine ( )
331
310
}
332
311
333
312
timeoutStart . current = Date . now ( )
0 commit comments