From 1e5ee5af404783790f06f5983c0455a5348beb38 Mon Sep 17 00:00:00 2001 From: Takeshi NAMAO Date: Wed, 13 Dec 2023 12:31:34 +0900 Subject: [PATCH] =?UTF-8?q?Sora=20=E3=81=AE=E5=88=87=E6=96=AD=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=81=A7=E9=9D=9E=E5=90=8C=E6=9C=9F=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=82=92=E6=9C=80=E5=B0=8F=E9=99=90=E3=81=AB=E7=B5=9E=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/actions.ts | 69 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/src/app/actions.ts b/src/app/actions.ts index 252ad1a1..e6fc8b10 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -869,7 +869,7 @@ function setSoraCallbacks( dispatch(slice.actions.removeRemoteMediaStream((event.target as MediaStream).id)) } }) - sora.on('disconnect', async (event) => { + sora.on('disconnect', (event) => { const message: Record = { type: event.type, title: event.title, @@ -888,11 +888,25 @@ function setSoraCallbacks( createSoraDevtoolsTimelineMessage('event-on-disconnect', message), ), ) - // ローカルの MediaStream の Track と MediaProcessor を止める - await stopLocalVideoTrack(dispatch, getState()) - stopLocalAudioTrack(dispatch, getState()) - const { fakeContents, soraContents, reconnect } = getState() - const { remoteMediaStreams } = soraContents + const { + fakeContents, + soraContents, + reconnect, + lightAdjustmentProcessor, + virtualBackgroundProcessor, + noiseSuppressionProcessor, + } = getState() + const { localMediaStream, remoteMediaStreams } = soraContents + ;(async () => { + // ローカルの MediaStream の Track と MediaProcessor を止める + await stopLocalVideoTrack( + dispatch, + localMediaStream, + lightAdjustmentProcessor, + virtualBackgroundProcessor, + ) + })() + stopLocalAudioTrack(dispatch, localMediaStream, noiseSuppressionProcessor) remoteMediaStreams.filter((mediaStream) => { mediaStream.getTracks().filter((track) => { track.stop() @@ -1193,9 +1207,11 @@ export const connectSora = () => { ) dispatch(slice.actions.setSoraConnectionStatus('preparing')) const state = getState() + let forceCreateMediaStream = false // 接続中の場合は切断する if (state.soraContents.sora) { await state.soraContents.sora.disconnect() + forceCreateMediaStream = true } // シグナリング候補のURLリストを作成する const signalingUrlCandidates = createSignalingURL( @@ -1226,7 +1242,7 @@ export const connectSora = () => { } } setSoraCallbacks(dispatch, getState, sora) - if (state.soraContents.localMediaStream) { + if (!forceCreateMediaStream && state.soraContents.localMediaStream) { mediaStream = state.soraContents.localMediaStream } else { ;[mediaStream, gainNode] = await createMediaStream(dispatch, state).catch((error) => { @@ -1247,7 +1263,7 @@ export const connectSora = () => { } } setSoraCallbacks(dispatch, getState, sora) - if (state.soraContents.localMediaStream) { + if (!forceCreateMediaStream && state.soraContents.localMediaStream) { mediaStream = state.soraContents.localMediaStream } else { ;[mediaStream, gainNode] = await createMediaStream(dispatch, state).catch((error) => { @@ -1341,7 +1357,7 @@ export const connectSora = () => { // disconnect 時に stream を止めないためのハック sora.stream = null dispatch(slice.actions.setSora(sora)) - if (mediaStream && state.soraContents.localMediaStream === null) { + if (mediaStream) { dispatch(slice.actions.setLocalMediaStream(mediaStream)) } if (gainNode) { @@ -1665,12 +1681,20 @@ export const setMicDevice = (micDevice: boolean) => { } } else if (state.soraContents.sora && state.soraContents.localMediaStream) { // Sora 接続中の場合 - stopLocalAudioTrack(dispatch, state) + stopLocalAudioTrack( + dispatch, + state.soraContents.localMediaStream, + state.noiseSuppressionProcessor, + ) state.soraContents.sora.stopAudioTrack(state.soraContents.localMediaStream) } else if (state.soraContents.localMediaStream) { // Sora は未接続で media access での表示を行っている場合 // localMediaStream の AudioTrack を停止して MediaStream から Track を削除する - stopLocalAudioTrack(dispatch, state) + stopLocalAudioTrack( + dispatch, + state.soraContents.localMediaStream, + state.noiseSuppressionProcessor, + ) } dispatch(slice.actions.setMicDevice(micDevice)) } @@ -1741,12 +1765,22 @@ export const setCameraDevice = (cameraDevice: boolean) => { } } else if (state.soraContents.sora && state.soraContents.localMediaStream) { // Sora 接続中の場合 - await stopLocalVideoTrack(dispatch, state) + await stopLocalVideoTrack( + dispatch, + state.soraContents.localMediaStream, + state.lightAdjustmentProcessor, + state.virtualBackgroundProcessor, + ) state.soraContents.sora.stopVideoTrack(state.soraContents.localMediaStream) } else if (state.soraContents.localMediaStream) { // Sora は未接続で media access での表示を行っている場合 // localMediaStream の VideoTrack を停止して MediaStream から Track を削除する - await stopLocalVideoTrack(dispatch, state) + await stopLocalVideoTrack( + dispatch, + state.soraContents.localMediaStream, + state.lightAdjustmentProcessor, + state.virtualBackgroundProcessor, + ) } dispatch(slice.actions.setCameraDevice(cameraDevice)) } @@ -1759,9 +1793,10 @@ export const setCameraDevice = (cameraDevice: boolean) => { */ const stopLocalVideoTrack = async ( dispatch: Dispatch, - { soraContents, lightAdjustmentProcessor, virtualBackgroundProcessor }: SoraDevtoolsState, + localMediaStream: MediaStream | null, + lightAdjustmentProcessor: LightAdjustmentProcessor | null, + virtualBackgroundProcessor: VirtualBackgroundProcessor | null, ): Promise => { - const { localMediaStream } = soraContents let originalTrack: MediaStreamTrack | undefined if (lightAdjustmentProcessor?.isProcessing()) { originalTrack = lightAdjustmentProcessor.getOriginalTrack() @@ -1812,9 +1847,9 @@ const stopLocalVideoTrack = async ( */ const stopLocalAudioTrack = ( dispatch: Dispatch, - { soraContents, noiseSuppressionProcessor }: SoraDevtoolsState, + localMediaStream: MediaStream | null, + noiseSuppressionProcessor: NoiseSuppressionProcessor | null, ): void => { - const { localMediaStream } = soraContents if (noiseSuppressionProcessor?.isProcessing()) { const originalTrack = noiseSuppressionProcessor.getOriginalTrack() if (originalTrack) {