Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Move stopping of tracks on restart inside reducer (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
szebniok authored Nov 8, 2023
1 parent 70e061a commit 63bf11d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/useUserMedia/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ export const userMediaReducer = (state: UseUserMediaState, action: UseUserMediaA
} else if (action.type === "UseUserMedia-setAudioAndVideo") {
return { audio: action.audio, video: action.video };
} else if (action.type === "UseUserMedia-setMedia") {
if (action.video.restart) {
prevState?.video?.media?.track?.stop();
}

if (action.audio.restart) {
prevState?.audio?.media?.track?.stop();
}

const videoMedia: Media | null = action.video.restart
? {
stream: action.stream,
Expand Down Expand Up @@ -494,20 +502,12 @@ export const useUserMediaInternal = (
if (result.type === "OK") {
const stream = result.stream;

if (shouldRestartVideo) {
state?.video.media?.track?.stop();
}

const currentVideoDeviceId = result.stream.getVideoTracks()?.[0]?.getSettings()?.deviceId;
const videoInfo = currentVideoDeviceId ? getDeviceInfo(currentVideoDeviceId, state.video.devices ?? []) : null;
if (videoInfo) {
saveLastVideoDevice?.(videoInfo);
}

if (shouldRestartAudio) {
state?.audio.media?.track?.stop();
}

const currentAudioDeviceId = result.stream.getAudioTracks()?.[0]?.getSettings()?.deviceId;
const audioInfo = currentAudioDeviceId ? getDeviceInfo(currentAudioDeviceId, state.audio.devices ?? []) : null;

Expand Down

0 comments on commit 63bf11d

Please sign in to comment.