/* 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 { useRef, useEffect, RefObject, useState, useCallback } from "react"; import { acquireContext, releaseContext, } from "matrix-js-sdk/src/webrtc/audioContext"; import { logger } from "matrix-js-sdk/src/logger"; import { useSpatialAudio } from "../settings/useSetting"; import { useEventTarget } from "../useEvents"; import { useAudioOutputDevice } from "./useAudioOutputDevice"; declare global { interface Window { // For detecting whether this browser is Chrome or not chrome?: unknown; } } export const useMediaStreamTrackCount = ( stream: MediaStream | null ): [number, number] => { const latestAudioTrackCount = stream ? stream.getAudioTracks().length : 0; const latestVideoTrackCount = stream ? stream.getVideoTracks().length : 0; const [audioTrackCount, setAudioTrackCount] = useState( stream ? stream.getAudioTracks().length : 0 ); const [videoTrackCount, setVideoTrackCount] = useState( stream ? stream.getVideoTracks().length : 0 ); const tracksChanged = useCallback(() => { setAudioTrackCount(stream ? stream.getAudioTracks().length : 0); setVideoTrackCount(stream ? stream.getVideoTracks().length : 0); }, [stream]); useEventTarget(stream, "addtrack", tracksChanged); useEventTarget(stream, "removetrack", tracksChanged); if ( latestAudioTrackCount !== audioTrackCount || latestVideoTrackCount !== videoTrackCount ) { tracksChanged(); } return [audioTrackCount, videoTrackCount]; }; // Binds a media stream to a media output element, returning a ref for the // media element that should then be passed to the media element to be used. export const useMediaStream = ( stream: MediaStream | null, audioOutputDevice: string | null, mute = false, localVolume?: number ): RefObject => { const mediaRef = useRef(); useAudioOutputDevice(mediaRef, audioOutputDevice); useEffect(() => { console.log( `useMediaStream update stream mediaRef.current ${!!mediaRef.current} stream ${ stream && stream.id } muted ${mute}` ); if (mediaRef.current) { const mediaEl = mediaRef.current; if (stream) { mediaEl.muted = mute; mediaEl.srcObject = stream; mediaEl.play().catch((e) => { if (e.name !== "AbortError") throw e; }); // Unmuting the tab in Safari causes all video elements to be individually // unmuted, so we need to reset the mute state here to prevent audio loops const onVolumeChange = () => { mediaEl.muted = mute; }; mediaEl.addEventListener("volumechange", onVolumeChange); return () => mediaEl.removeEventListener("volumechange", onVolumeChange); } else { mediaRef.current.srcObject = null; } } }, [stream, mute]); useEffect(() => { if (!mediaRef.current) return; if (localVolume === null || localVolume === undefined) return; mediaRef.current.volume = localVolume; }, [localVolume]); useEffect(() => { const mediaEl = mediaRef.current; return () => { if (mediaEl) { // Ensure we set srcObject to null before unmounting to prevent memory leak // https://webrtchacks.com/srcobject-intervention/ mediaEl.srcObject = null; } }; }, []); return mediaRef; }; // Provides a properly refcounted instance of the shared audio context, // along with the context's destination audio node and a ref to be used // for the