Skip to content

Commit 2f4b687

Browse files
committed
Merge branch 'idle-FUCKING_FIX'
2 parents e42e5f8 + e5db600 commit 2f4b687

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

src/components/EngineStream.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ export const EngineStream = (props: {
622622
/>
623623
{![
624624
EngineStreamState.Playing,
625+
EngineStreamState.Pausing,
625626
EngineStreamState.Paused,
626627
EngineStreamState.Resuming,
627628
].some((s) => s === engineStreamState.value) && (

src/lang/std/engineConnection.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,10 +1308,7 @@ class EngineConnection extends EventTarget {
13081308
)
13091309
} else {
13101310
this.state = {
1311-
type: EngineConnectionStateType.Disconnecting,
1312-
value: {
1313-
type: DisconnectingType.Pause,
1314-
},
1311+
type: EngineConnectionStateType.Disconnected,
13151312
}
13161313
}
13171314
this.triggeredStart = false

src/machines/engineStreamMachine.ts

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import type { MutableRefObject } from 'react'
22
import type { ActorRefFrom } from 'xstate'
33
import { assign, fromPromise, setup } from 'xstate'
44
import type { AppMachineContext } from '@src/lib/types'
5+
import { EngineConnectionStateType } from '@src/lang/std/engineConnection'
56

67
export enum EngineStreamState {
78
WaitingForDependencies = 'waiting-for-dependencies',
89
WaitingForMediaStream = 'waiting-for-media-stream',
910
WaitingToPlay = 'waiting-to-play',
1011
Playing = 'playing',
1112
Reconfiguring = 'reconfiguring',
13+
Pausing = 'pausing',
1214
Paused = 'paused',
1315
Stopped = 'stopped',
1416
// The is the state in-between Paused and Playing *specifically that order*.
@@ -239,25 +241,55 @@ export const engineStreamMachine = setup({
239241
console.warn('Save remote camera state timed out', e)
240242
}
241243

242-
// Make sure we're on the next frame for no flickering between canvas
243-
// and the video elements.
244-
window.requestAnimationFrame(
245-
() =>
246-
void (async () => {
247-
// Destroy the media stream. We will re-establish it. We could
248-
// leave everything at pausing, preventing video decoders from running
249-
// but we can do even better by significantly reducing network
250-
// cards also.
251-
context.mediaStream?.getVideoTracks()[0].stop()
252-
context.mediaStream = null
253-
254-
if (context.videoRef.current) {
255-
context.videoRef.current.srcObject = null
256-
}
257-
258-
rootContext.engineCommandManager.tearDown({ idleMode: true })
259-
})()
260-
)
244+
await new Promise<void>((resolve, reject) => {
245+
// Make sure we're on the next frame for no flickering between canvas
246+
// and the video elements.
247+
window.requestAnimationFrame(
248+
() =>
249+
void (async () => {
250+
// Destroy the media stream. We will re-establish it. We could
251+
// leave everything at pausing, preventing video decoders from running
252+
// but we can do even better by significantly reducing network
253+
// cards also.
254+
context.mediaStream?.getVideoTracks()[0].stop()
255+
context.mediaStream = null
256+
257+
if (context.videoRef.current) {
258+
context.videoRef.current.srcObject = null
259+
}
260+
261+
let oldEngineConnection =
262+
rootContext.engineCommandManager.engineConnection
263+
setTimeout(() => {
264+
rootContext.engineCommandManager.tearDown({ idleMode: true })
265+
}, 1000)
266+
267+
let timeoutCheckId: ReturnType<typeof setTimeout>
268+
const timeoutEjectId = setTimeout(() => {
269+
clearTimeout(timeoutCheckId)
270+
reject()
271+
}, 5000)
272+
273+
const checkClosed = () => {
274+
timeoutCheckId = setTimeout(() => {
275+
if (
276+
oldEngineConnection?.state?.type !==
277+
EngineConnectionStateType.Disconnected
278+
) {
279+
checkClosed()
280+
return
281+
}
282+
// Finally disconnected.
283+
clearTimeout(timeoutEjectId)
284+
resolve()
285+
}, 100)
286+
}
287+
checkClosed()
288+
})()
289+
)
290+
})
291+
292+
return {}
261293
}
262294
),
263295
},
@@ -356,7 +388,7 @@ export const engineStreamMachine = setup({
356388
target: EngineStreamState.Reconfiguring,
357389
},
358390
[EngineStreamTransition.Pause]: {
359-
target: EngineStreamState.Paused,
391+
target: EngineStreamState.Pausing,
360392
},
361393
[EngineStreamTransition.Stop]: {
362394
target: EngineStreamState.Stopped,
@@ -374,14 +406,18 @@ export const engineStreamMachine = setup({
374406
onDone: [{ target: EngineStreamState.Playing }],
375407
},
376408
},
377-
[EngineStreamState.Paused]: {
409+
[EngineStreamState.Pausing]: {
378410
invoke: {
379411
src: EngineStreamTransition.Pause,
380412
input: (args) => ({
381413
context: args.context,
382414
rootContext: args.self.system.get('root').getSnapshot().context,
383415
}),
416+
onDone: [{ target: EngineStreamState.Paused }],
417+
onError: [{ target: EngineStreamState.Stopped }],
384418
},
419+
},
420+
[EngineStreamState.Paused]: {
385421
on: {
386422
[EngineStreamTransition.Resume]: {
387423
target: EngineStreamState.Resuming,

0 commit comments

Comments
 (0)