2023-03-06 20:13:34 +11:00
|
|
|
import { useEffect, useRef } from 'react'
|
|
|
|
import { PanelHeader } from '../components/PanelHeader'
|
|
|
|
|
|
|
|
export const Stream = () => {
|
|
|
|
const videoRef = useRef<HTMLVideoElement>(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-07 15:45:59 +11:00
|
|
|
if (
|
|
|
|
typeof window === 'undefined' ||
|
|
|
|
typeof RTCPeerConnection === 'undefined'
|
|
|
|
)
|
|
|
|
return
|
2023-03-06 20:13:34 +11:00
|
|
|
const url = 'wss://dev.api.kittycad.io/ws/channel'
|
|
|
|
const [pc, socket] = [new RTCPeerConnection(), new WebSocket(url)]
|
|
|
|
// Connection opened
|
|
|
|
socket.addEventListener('open', (event) => {
|
|
|
|
console.log('Connected to websocket, waiting for ICE servers')
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.addEventListener('close', (event) => {
|
|
|
|
console.log('websocket connection closed')
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.addEventListener('error', (event) => {
|
|
|
|
console.log('websocket connection error')
|
|
|
|
})
|
|
|
|
|
|
|
|
// Listen for messages
|
|
|
|
socket.addEventListener('message', (event) => {
|
|
|
|
//console.log('Message from server ', event.data);
|
|
|
|
if (event.data instanceof Blob) {
|
|
|
|
const reader = new FileReader()
|
|
|
|
|
|
|
|
reader.onload = () => {
|
|
|
|
//console.log("Result: " + reader.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.readAsText(event.data)
|
|
|
|
} else {
|
|
|
|
const message = JSON.parse(event.data)
|
|
|
|
if (message.type === 'SDPAnswer') {
|
|
|
|
pc.setRemoteDescription(new RTCSessionDescription(message.answer))
|
|
|
|
} else if (message.type === 'IceServerInfo') {
|
|
|
|
console.log('received IceServerInfo')
|
|
|
|
pc.setConfiguration({
|
|
|
|
iceServers: message.ice_servers,
|
|
|
|
})
|
|
|
|
pc.ontrack = function (event) {
|
2023-03-07 15:45:59 +11:00
|
|
|
console.log('has element?', videoRef.current)
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log('has element in timeout?', videoRef.current)
|
|
|
|
if (videoRef.current) {
|
|
|
|
videoRef.current.srcObject = event.streams[0]
|
|
|
|
videoRef.current.autoplay = true
|
|
|
|
videoRef.current.controls = true
|
|
|
|
}
|
|
|
|
})
|
2023-03-06 20:13:34 +11:00
|
|
|
}
|
|
|
|
pc.oniceconnectionstatechange = (e) =>
|
|
|
|
console.log(pc.iceConnectionState)
|
|
|
|
pc.onicecandidate = (event) => {
|
|
|
|
if (event.candidate === null) {
|
|
|
|
console.log('sent SDPOffer')
|
|
|
|
socket.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'SDPOffer',
|
|
|
|
offer: pc.localDescription,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offer to receive 1 video track
|
|
|
|
pc.addTransceiver('video', {
|
|
|
|
direction: 'sendrecv',
|
|
|
|
})
|
|
|
|
pc.createOffer()
|
|
|
|
.then((d) => pc.setLocalDescription(d))
|
|
|
|
.catch(console.log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
socket.close()
|
|
|
|
pc.close()
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<PanelHeader title="Stream" />
|
|
|
|
<video ref={videoRef} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|