import { useEffect, useRef } from "react";
function DeviceStream({ whepUrl }: { whepUrl: string }) {
const videoRef = useRef<HTMLVideoElement>(null);
useEffect(() => {
if (!whepUrl) return;
const pc = new RTCPeerConnection({
iceServers: [{ urls: "stun:stun.cloudflare.com:3478" }],
});
pc.addTransceiver("video", { direction: "recvonly" });
pc.ontrack = (event) => {
if (videoRef.current) {
videoRef.current.srcObject = event.streams[0];
}
};
(async () => {
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
const resp = await fetch(whepUrl, {
method: "POST",
headers: { "Content-Type": "application/sdp" },
body: offer.sdp,
});
const answer = await resp.text();
await pc.setRemoteDescription({ type: "answer", sdp: answer });
})();
return () => pc.close();
}, [whepUrl]);
return <video ref={videoRef} autoPlay playsInline muted />;
}