Skip to content

Commit

Permalink
fix(useMediaDevices): update the starting function
Browse files Browse the repository at this point in the history
  • Loading branch information
malkiii committed Jul 10, 2024
1 parent d00e21f commit 59934c8
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/hooks/src/useMediaDevices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ export const useMediaDevices = (

const startStreaming = useCallback(
async (constraints?: MediaStreamConstraints) => {
const currentTracks = stream?.getTracks();

// stop the current stream tracks so that the next tracks will start
stream?.getTracks().forEach(t => t.stop());
currentTracks?.forEach(t => t.stop());

const newStream = await navigator.mediaDevices.getUserMedia(constraints ?? args);

// enable the tracks that were enabled in the previous stream
if (stream) {
// disable the tracks that were enabled in the previous stream
if (currentTracks) {
for (const track of newStream.getTracks()) {
const newTrack = stream.getTracks().find(t => t.kind === track.kind);
if (newTrack) newTrack.enabled = track.enabled;
track.enabled = currentTracks.find(t => t.kind === track.kind)?.enabled ?? true;
}
}

Expand Down

0 comments on commit 59934c8

Please sign in to comment.