Skip to content

Commit

Permalink
feat(web): move videoOff and muted to preferences on localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocroh committed May 25, 2024
1 parent 91b0c5e commit 9f3ae6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
8 changes: 8 additions & 0 deletions apps/web/hooks/usePreferencesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import { createJSONStorage, persist } from "zustand/middleware";
interface PreferencesState {
video: string;
audio: string;
muted: boolean;
videoOff: boolean;
set: (deviceId: string, type: "audio" | "video") => void;
toggleMute: () => void;
toggleVideoOff: () => void;
}

export const usePreferencesStore = create<PreferencesState>()(
persist(
(set) => ({
audio: "default",
video: "default",
muted: false,
videoOff: false,
set: (deviceId, type) => set({ [type]: deviceId }),
toggleMute: () => set((state) => ({muted: !state.muted})),
toggleVideoOff: () => set((state) => ({videoOff: !state.videoOff})),
}),
{
name: "user-preferences",
Expand Down
41 changes: 22 additions & 19 deletions apps/web/hooks/useUserMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ type MediaConstraints = {

export const useUserMedia = () => {
const preferences = usePreferencesStore();
const [muted, setMuted] = useState<boolean>(false);
const [videoOff, setVideoOff] = useState<boolean>(false);
const [stream, setStream] = useState<MediaStream | null>(null);
const [activeStream, setActiveStream] = useState<MediaStream | null>(null);
const [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
Expand Down Expand Up @@ -60,6 +58,14 @@ export const useUserMedia = () => {
audio: { deviceId: { exact: constraints.audio } }
});

_stream.getVideoTracks().forEach(track => {
track.enabled = !preferences.videoOff
})

_stream.getAudioTracks().forEach(track => {
track.enabled = !preferences.muted
})

setActiveStream(_stream);
setReady(true);
return _stream
Expand All @@ -73,22 +79,19 @@ export const useUserMedia = () => {
}, [setDevices]);

const toggleMute = useCallback(async () => {
setMuted(_muted => {
stream?.getAudioTracks().forEach(track => {
track.enabled = _muted
})
return !_muted
console.log({muted: preferences.muted})
activeStream?.getAudioTracks().forEach(track => {
track.enabled = preferences.muted
})
}, [stream, setMuted])
preferences.toggleMute()
}, [preferences, activeStream])

const toggleVideo = useCallback(async () => {
setVideoOff(_videoOff => {
activeStream?.getVideoTracks().forEach(track => {
track.enabled = _videoOff
})
return !_videoOff
activeStream?.getVideoTracks().forEach(track => {
track.enabled = preferences.videoOff
})
}, [activeStream, setVideoOff])
preferences.toggleVideoOff()
}, [preferences, activeStream])

const switchInput = useCallback(async (deviceId: string, type: 'audio' | 'video') => {
let newStream: MediaStream;
Expand All @@ -114,8 +117,8 @@ export const useUserMedia = () => {
const newVideoTrack = newStream.getVideoTracks()[0]!;
const newAudioTrack = newStream.getAudioTracks()[0]!;

newVideoTrack.enabled = !videoOff
newAudioTrack.enabled = !muted
newVideoTrack.enabled = !preferences.videoOff
newAudioTrack.enabled = !preferences.muted

stopStreaming(activeStream!)
setActiveStream(newStream)
Expand All @@ -127,7 +130,7 @@ export const useUserMedia = () => {
newAudioTrack,
newStream,
};
}, [activeStream, preferences, stopStreaming, muted, videoOff]);
}, [activeStream, preferences, stopStreaming]);


const stopAllStreaming = useCallback(async () => {
Expand Down Expand Up @@ -196,7 +199,7 @@ export const useUserMedia = () => {
stopAllStreaming,
toggleMute,
toggleVideo,
muted,
videoOff
muted: preferences.muted,
videoOff: preferences.videoOff
};
};

0 comments on commit 9f3ae6d

Please sign in to comment.