Add useAudioOutputDevice()

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner 2022-08-12 09:36:46 +02:00
parent c09380644b
commit 0e34f9a464
No known key found for this signature in database
GPG key ID: D1D45825D60C24D2
3 changed files with 48 additions and 20 deletions

View file

@ -48,6 +48,7 @@ import { useShowInspector } from "../settings/useSetting";
import { useModalTriggerState } from "../Modal"; import { useModalTriggerState } from "../Modal";
import { useAudioContext } from "../video-grid/useMediaStream"; import { useAudioContext } from "../video-grid/useMediaStream";
import { useFullscreen } from "../video-grid/useFullscreen"; import { useFullscreen } from "../video-grid/useFullscreen";
import { useAudioOutputDevice } from "../video-grid/useAudioOutputDevice";
const canScreenshare = "getDisplayMedia" in (navigator.mediaDevices ?? {}); const canScreenshare = "getDisplayMedia" in (navigator.mediaDevices ?? {});
// There is currently a bug in Safari our our code with cloning and sending MediaStreams // There is currently a bug in Safari our our code with cloning and sending MediaStreams
@ -114,6 +115,8 @@ export function InCallView({
const { modalState: feedbackModalState, modalProps: feedbackModalProps } = const { modalState: feedbackModalState, modalProps: feedbackModalProps } =
useModalTriggerState(); useModalTriggerState();
useAudioOutputDevice(audioRef, audioOutput);
const items = useMemo(() => { const items = useMemo(() => {
const participants: Participant[] = []; const participants: Participant[] = [];

View file

@ -0,0 +1,40 @@
/*
Copyright 2022 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { RefObject, useEffect } from "react";
export function useAudioOutputDevice(
mediaRef: RefObject<MediaElement>,
audioOutputDevice: string
): void {
useEffect(() => {
if (
mediaRef.current &&
mediaRef.current !== undefined &&
audioOutputDevice
) {
if (mediaRef.current.setSinkId) {
console.log(
`useMediaStream setting output setSinkId ${audioOutputDevice}`
);
// Chrome for Android doesn't support this
mediaRef.current.setSinkId(audioOutputDevice);
} else {
console.log("Can't set output - no setsinkid");
}
}
}, [mediaRef, audioOutputDevice]);
}

View file

@ -22,6 +22,7 @@ import {
} from "matrix-js-sdk/src/webrtc/audioContext"; } from "matrix-js-sdk/src/webrtc/audioContext";
import { useSpatialAudio } from "../settings/useSetting"; import { useSpatialAudio } from "../settings/useSetting";
import { useAudioOutputDevice } from "./useAudioOutputDevice";
declare global { declare global {
interface Window { interface Window {
@ -38,6 +39,8 @@ export const useMediaStream = (
): RefObject<MediaElement> => { ): RefObject<MediaElement> => {
const mediaRef = useRef<MediaElement>(); const mediaRef = useRef<MediaElement>();
useAudioOutputDevice(mediaRef, audioOutputDevice);
useEffect(() => { useEffect(() => {
console.log( console.log(
`useMediaStream update stream mediaRef.current ${!!mediaRef.current} stream ${ `useMediaStream update stream mediaRef.current ${!!mediaRef.current} stream ${
@ -67,24 +70,6 @@ export const useMediaStream = (
} }
}, [stream, mute]); }, [stream, mute]);
useEffect(() => {
if (
mediaRef.current &&
audioOutputDevice &&
mediaRef.current !== undefined
) {
if (mediaRef.current.setSinkId) {
console.log(
`useMediaStream setting output setSinkId ${audioOutputDevice}`
);
// Chrome for Android doesn't support this
mediaRef.current.setSinkId(audioOutputDevice);
} else {
console.log("Can't set output - no setsinkid");
}
}
}, [audioOutputDevice]);
useEffect(() => { useEffect(() => {
if (!mediaRef.current) return; if (!mediaRef.current) return;
if (localVolume === null || localVolume === undefined) return; if (localVolume === null || localVolume === undefined) return;
@ -156,11 +141,11 @@ const createLoopback = async (stream: MediaStream): Promise<MediaStream> => {
export const useAudioContext = (): [ export const useAudioContext = (): [
AudioContext, AudioContext,
AudioNode, AudioNode,
RefObject<HTMLAudioElement> RefObject<MediaElement>
] => { ] => {
const context = useRef<AudioContext>(); const context = useRef<AudioContext>();
const destination = useRef<AudioNode>(); const destination = useRef<AudioNode>();
const audioRef = useRef<HTMLAudioElement>(); const audioRef = useRef<MediaElement>();
useEffect(() => { useEffect(() => {
if (audioRef.current && !context.current) { if (audioRef.current && !context.current) {