mirror of https://github.com/jetkvm/kvm.git
Compare commits
3 Commits
9450f65fca
...
fbee0b2925
| Author | SHA1 | Date |
|---|---|---|
|
|
fbee0b2925 | |
|
|
cd7a098f76 | |
|
|
ef86af8afc |
|
|
@ -26,8 +26,10 @@ import { m } from "@localizations/messages.js";
|
||||||
export default function WebRTCVideo({ hasConnectionIssues }: { hasConnectionIssues: boolean }) {
|
export default function WebRTCVideo({ hasConnectionIssues }: { hasConnectionIssues: boolean }) {
|
||||||
// Video and stream related refs and states
|
// Video and stream related refs and states
|
||||||
const videoElm = useRef<HTMLVideoElement>(null);
|
const videoElm = useRef<HTMLVideoElement>(null);
|
||||||
|
const audioElementsRef = useRef<HTMLAudioElement[]>([]);
|
||||||
const { mediaStream, peerConnectionState } = useRTCStore();
|
const { mediaStream, peerConnectionState } = useRTCStore();
|
||||||
const [isPlaying, setIsPlaying] = useState(false);
|
const [isPlaying, setIsPlaying] = useState(false);
|
||||||
|
const [audioAutoplayBlocked, setAudioAutoplayBlocked] = useState(false);
|
||||||
const [isPointerLockActive, setIsPointerLockActive] = useState(false);
|
const [isPointerLockActive, setIsPointerLockActive] = useState(false);
|
||||||
const [isKeyboardLockActive, setIsKeyboardLockActive] = useState(false);
|
const [isKeyboardLockActive, setIsKeyboardLockActive] = useState(false);
|
||||||
|
|
||||||
|
|
@ -330,7 +332,6 @@ export default function WebRTCVideo({ hasConnectionIssues }: { hasConnectionIssu
|
||||||
if (!peerConnection) return;
|
if (!peerConnection) return;
|
||||||
const abortController = new AbortController();
|
const abortController = new AbortController();
|
||||||
const signal = abortController.signal;
|
const signal = abortController.signal;
|
||||||
const audioElements: HTMLAudioElement[] = [];
|
|
||||||
|
|
||||||
peerConnection.addEventListener(
|
peerConnection.addEventListener(
|
||||||
"track",
|
"track",
|
||||||
|
|
@ -339,11 +340,17 @@ export default function WebRTCVideo({ hasConnectionIssues }: { hasConnectionIssu
|
||||||
addStreamToVideoElm(e.streams[0]);
|
addStreamToVideoElm(e.streams[0]);
|
||||||
} else if (e.track.kind === "audio") {
|
} else if (e.track.kind === "audio") {
|
||||||
const audioElm = document.createElement("audio");
|
const audioElm = document.createElement("audio");
|
||||||
audioElm.autoplay = true;
|
|
||||||
audioElm.srcObject = e.streams[0];
|
audioElm.srcObject = e.streams[0];
|
||||||
audioElm.style.display = "none";
|
audioElm.style.display = "none";
|
||||||
document.body.appendChild(audioElm);
|
document.body.appendChild(audioElm);
|
||||||
audioElements.push(audioElm);
|
audioElementsRef.current.push(audioElm);
|
||||||
|
|
||||||
|
audioElm.play().then(() => {
|
||||||
|
setAudioAutoplayBlocked(false);
|
||||||
|
}).catch(() => {
|
||||||
|
console.debug("[Audio] Autoplay blocked, will be started by user interaction");
|
||||||
|
setAudioAutoplayBlocked(true);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ signal },
|
{ signal },
|
||||||
|
|
@ -351,10 +358,12 @@ export default function WebRTCVideo({ hasConnectionIssues }: { hasConnectionIssu
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
abortController.abort();
|
abortController.abort();
|
||||||
audioElements.forEach((audioElm) => {
|
audioElementsRef.current.forEach((audioElm) => {
|
||||||
audioElm.srcObject = null;
|
audioElm.srcObject = null;
|
||||||
audioElm.remove();
|
audioElm.remove();
|
||||||
});
|
});
|
||||||
|
audioElementsRef.current = [];
|
||||||
|
setAudioAutoplayBlocked(false);
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
[addStreamToVideoElm, peerConnection],
|
[addStreamToVideoElm, peerConnection],
|
||||||
|
|
@ -468,11 +477,12 @@ export default function WebRTCVideo({ hasConnectionIssues }: { hasConnectionIssu
|
||||||
|
|
||||||
const hasNoAutoPlayPermissions = useMemo(() => {
|
const hasNoAutoPlayPermissions = useMemo(() => {
|
||||||
if (peerConnection?.connectionState !== "connected") return false;
|
if (peerConnection?.connectionState !== "connected") return false;
|
||||||
if (isPlaying) return false;
|
|
||||||
if (hdmiError) return false;
|
if (hdmiError) return false;
|
||||||
if (videoHeight === 0 || videoWidth === 0) return false;
|
if (videoHeight === 0 || videoWidth === 0) return false;
|
||||||
return true;
|
if (!isPlaying) return true;
|
||||||
}, [hdmiError, isPlaying, peerConnection?.connectionState, videoHeight, videoWidth]);
|
if (audioAutoplayBlocked) return true;
|
||||||
|
return false;
|
||||||
|
}, [audioAutoplayBlocked, hdmiError, isPlaying, peerConnection?.connectionState, videoHeight, videoWidth]);
|
||||||
|
|
||||||
const showPointerLockBar = useMemo(() => {
|
const showPointerLockBar = useMemo(() => {
|
||||||
if (settings.mouseMode !== "relative") return false;
|
if (settings.mouseMode !== "relative") return false;
|
||||||
|
|
@ -564,6 +574,11 @@ export default function WebRTCVideo({ hasConnectionIssues }: { hasConnectionIssu
|
||||||
show={hasNoAutoPlayPermissions}
|
show={hasNoAutoPlayPermissions}
|
||||||
onPlayClick={() => {
|
onPlayClick={() => {
|
||||||
videoElm.current?.play();
|
videoElm.current?.play();
|
||||||
|
audioElementsRef.current.forEach(audioElm => {
|
||||||
|
audioElm.play().then(() => {
|
||||||
|
setAudioAutoplayBlocked(false);
|
||||||
|
}).catch(() => undefined);
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue