-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add audio output setting when available
- Loading branch information
1 parent
81a763f
commit 2c3ebd4
Showing
7 changed files
with
180 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React, { | ||
useState, | ||
useEffect, | ||
useCallback, | ||
useMemo, | ||
useContext, | ||
createContext, | ||
} from "react"; | ||
|
||
const MediaHandlerContext = createContext(); | ||
|
||
export function MediaHandlerProvider({ client, children }) { | ||
const [ | ||
{ | ||
audioInput, | ||
videoInput, | ||
audioInputs, | ||
videoInputs, | ||
audioOutput, | ||
audioOutputs, | ||
}, | ||
setState, | ||
] = useState(() => { | ||
const mediaHandler = client.getMediaHandler(); | ||
|
||
return { | ||
audioInput: mediaHandler.audioInput, | ||
videoInput: mediaHandler.videoInput, | ||
audioOutput: undefined, | ||
audioInputs: [], | ||
videoInputs: [], | ||
audioOutputs: [], | ||
}; | ||
}); | ||
|
||
useEffect(() => { | ||
const mediaHandler = client.getMediaHandler(); | ||
|
||
function updateDevices() { | ||
navigator.mediaDevices.enumerateDevices().then((devices) => { | ||
const audioInputs = devices.filter( | ||
(device) => device.kind === "audioinput" | ||
); | ||
const videoInputs = devices.filter( | ||
(device) => device.kind === "videoinput" | ||
); | ||
const audioOutputs = devices.filter( | ||
(device) => device.kind === "audiooutput" | ||
); | ||
|
||
let audioOutput = undefined; | ||
|
||
const audioOutputPreference = localStorage.getItem( | ||
"matrix-audio-output" | ||
); | ||
|
||
if ( | ||
audioOutputPreference && | ||
audioOutputs.some( | ||
(device) => device.deviceId === audioOutputPreference | ||
) | ||
) { | ||
audioOutput = audioOutputPreference; | ||
} | ||
|
||
setState({ | ||
audioInput: mediaHandler.audioInput, | ||
videoInput: mediaHandler.videoInput, | ||
audioOutput, | ||
audioInputs, | ||
audioOutputs, | ||
videoInputs, | ||
}); | ||
}); | ||
} | ||
|
||
updateDevices(); | ||
|
||
mediaHandler.on("local_streams_changed", updateDevices); | ||
navigator.mediaDevices.addEventListener("devicechange", updateDevices); | ||
|
||
return () => { | ||
mediaHandler.removeListener("local_streams_changed", updateDevices); | ||
navigator.mediaDevices.removeEventListener("devicechange", updateDevices); | ||
}; | ||
}, [client]); | ||
|
||
const setAudioInput = useCallback( | ||
(deviceId) => { | ||
setState((prevState) => ({ ...prevState, audioInput: deviceId })); | ||
client.getMediaHandler().setAudioInput(deviceId); | ||
}, | ||
[client] | ||
); | ||
|
||
const setVideoInput = useCallback( | ||
(deviceId) => { | ||
setState((prevState) => ({ ...prevState, videoInput: deviceId })); | ||
client.getMediaHandler().setVideoInput(deviceId); | ||
}, | ||
[client] | ||
); | ||
|
||
const setAudioOutput = useCallback((deviceId) => { | ||
localStorage.setItem("matrix-audio-output", deviceId); | ||
setState((prevState) => ({ ...prevState, audioOutput: deviceId })); | ||
}, []); | ||
|
||
const context = useMemo( | ||
() => ({ | ||
audioInput, | ||
audioInputs, | ||
setAudioInput, | ||
videoInput, | ||
videoInputs, | ||
setVideoInput, | ||
audioOutput, | ||
audioOutputs, | ||
setAudioOutput, | ||
}), | ||
[ | ||
audioInput, | ||
audioInputs, | ||
setAudioInput, | ||
videoInput, | ||
videoInputs, | ||
setVideoInput, | ||
audioOutput, | ||
audioOutputs, | ||
setAudioOutput, | ||
] | ||
); | ||
|
||
return ( | ||
<MediaHandlerContext.Provider value={context}> | ||
{children} | ||
</MediaHandlerContext.Provider> | ||
); | ||
} | ||
|
||
export function useMediaHandler() { | ||
return useContext(MediaHandlerContext); | ||
} |