Don't re-run hook on every mute
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
parent
2234962acc
commit
317f27e5f9
2 changed files with 31 additions and 4 deletions
|
@ -72,7 +72,7 @@ export function VideoTileContainer({
|
||||||
audioOutputDevice,
|
audioOutputDevice,
|
||||||
audioContext,
|
audioContext,
|
||||||
audioDestination,
|
audioDestination,
|
||||||
isLocal || audioMuted,
|
isLocal,
|
||||||
localVolume
|
localVolume
|
||||||
);
|
);
|
||||||
const {
|
const {
|
||||||
|
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { useRef, useEffect, RefObject } from "react";
|
import { useRef, useEffect, RefObject, useState, useCallback } from "react";
|
||||||
import { parse as parseSdp, write as writeSdp } from "sdp-transform";
|
import { parse as parseSdp, write as writeSdp } from "sdp-transform";
|
||||||
import {
|
import {
|
||||||
acquireContext,
|
acquireContext,
|
||||||
|
@ -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 { useEventTarget } from "../useEvents";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
|
@ -30,6 +31,23 @@ declare global {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const useMediaStreamTrackCount = (
|
||||||
|
stream: MediaStream
|
||||||
|
): [number, number] => {
|
||||||
|
const [audioTrackCount, setAudioTrackCount] = useState(0);
|
||||||
|
const [videoTrackCount, setVideoTrackCount] = useState(0);
|
||||||
|
|
||||||
|
const tracksChanged = useCallback(() => {
|
||||||
|
setAudioTrackCount(stream.getAudioTracks().length);
|
||||||
|
setVideoTrackCount(stream.getVideoTracks().length);
|
||||||
|
}, [stream]);
|
||||||
|
|
||||||
|
useEventTarget(stream, "addtrack", tracksChanged);
|
||||||
|
useEventTarget(stream, "removetrack", tracksChanged);
|
||||||
|
|
||||||
|
return [audioTrackCount, videoTrackCount];
|
||||||
|
};
|
||||||
|
|
||||||
export const useMediaStream = (
|
export const useMediaStream = (
|
||||||
stream: MediaStream,
|
stream: MediaStream,
|
||||||
audioOutputDevice: string,
|
audioOutputDevice: string,
|
||||||
|
@ -207,13 +225,14 @@ export const useSpatialMediaStream = (
|
||||||
spatialAudio || mute,
|
spatialAudio || mute,
|
||||||
localVolume
|
localVolume
|
||||||
);
|
);
|
||||||
|
const [audioTrackCount] = useMediaStreamTrackCount(stream);
|
||||||
|
|
||||||
const gainNodeRef = useRef<GainNode>();
|
const gainNodeRef = useRef<GainNode>();
|
||||||
const pannerNodeRef = useRef<PannerNode>();
|
const pannerNodeRef = useRef<PannerNode>();
|
||||||
const sourceRef = useRef<MediaStreamAudioSourceNode>();
|
const sourceRef = useRef<MediaStreamAudioSourceNode>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (spatialAudio && audioContext && tileRef.current && !mute) {
|
if (spatialAudio && tileRef.current && !mute && audioTrackCount > 0) {
|
||||||
if (!pannerNodeRef.current) {
|
if (!pannerNodeRef.current) {
|
||||||
pannerNodeRef.current = new PannerNode(audioContext, {
|
pannerNodeRef.current = new PannerNode(audioContext, {
|
||||||
panningModel: "HRTF",
|
panningModel: "HRTF",
|
||||||
|
@ -261,7 +280,15 @@ export const useSpatialMediaStream = (
|
||||||
pannerNode.disconnect();
|
pannerNode.disconnect();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [stream, spatialAudio, audioContext, audioDestination, mute, localVolume]);
|
}, [
|
||||||
|
stream,
|
||||||
|
spatialAudio,
|
||||||
|
audioContext,
|
||||||
|
audioDestination,
|
||||||
|
mute,
|
||||||
|
localVolume,
|
||||||
|
audioTrackCount,
|
||||||
|
]);
|
||||||
|
|
||||||
return [tileRef, mediaRef];
|
return [tileRef, mediaRef];
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue