2022-10-15 03:02:10 +03:00
|
|
|
import { h } from 'preact';
|
|
|
|
|
import { useRef, useEffect } from 'preact/hooks';
|
|
|
|
|
|
2022-10-15 22:42:14 +03:00
|
|
|
let ws;
|
2022-10-15 03:02:10 +03:00
|
|
|
|
2022-10-15 23:04:54 +03:00
|
|
|
function initStream(camera) {
|
|
|
|
|
//ws = new WebSocket('ws://127.0.0.1:1984/api/ws?src=garage_cam');
|
|
|
|
|
ws = new WebSocket(`ws://${window.location.hostname}:${window.location.port}/restream-ws/api/ws?src=${camera}`);
|
2022-10-15 22:42:14 +03:00
|
|
|
ws.onopen = () => {
|
2022-10-15 23:04:54 +03:00
|
|
|
console.debug('ws.onopen');
|
2022-10-15 22:42:14 +03:00
|
|
|
pc.createOffer().then(offer => {
|
|
|
|
|
pc.setLocalDescription(offer).then(() => {
|
|
|
|
|
console.log(offer.sdp);
|
|
|
|
|
const msg = {type: 'webrtc/offer', value: pc.localDescription.sdp};
|
|
|
|
|
ws.send(JSON.stringify(msg));
|
|
|
|
|
});
|
2022-10-15 03:02:10 +03:00
|
|
|
});
|
2022-10-15 22:42:14 +03:00
|
|
|
}
|
|
|
|
|
ws.onmessage = ev => {
|
|
|
|
|
const msg = JSON.parse(ev.data);
|
2022-10-15 23:04:54 +03:00
|
|
|
console.debug('ws.onmessage', msg);
|
2022-10-15 03:02:10 +03:00
|
|
|
|
2022-10-15 22:42:14 +03:00
|
|
|
if (msg.type === 'webrtc/candidate') {
|
|
|
|
|
pc.addIceCandidate({candidate: msg.value, sdpMid: ''});
|
|
|
|
|
} else if (msg.type === 'webrtc/answer') {
|
|
|
|
|
pc.setRemoteDescription({type: 'answer', sdp: msg.value});
|
|
|
|
|
pc.getTransceivers().forEach(t => {
|
|
|
|
|
if (t.receiver.track.kind === 'audio') {
|
|
|
|
|
t.currentDirection
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-15 03:02:10 +03:00
|
|
|
|
2022-10-15 22:42:14 +03:00
|
|
|
const pc = new RTCPeerConnection({
|
|
|
|
|
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
|
|
|
|
|
});
|
|
|
|
|
pc.onicecandidate = ev => {
|
|
|
|
|
if (ev.candidate !== null) {
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
|
type: 'webrtc/candidate', value: ev.candidate.toJSON().candidate,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pc.ontrack = ev => {
|
|
|
|
|
const video = document.getElementById('video');
|
2022-10-15 03:02:10 +03:00
|
|
|
|
2022-10-15 22:42:14 +03:00
|
|
|
// when audio track not exist in Chrome
|
|
|
|
|
if (ev.streams.length === 0) return;
|
|
|
|
|
// when audio track not exist in Firefox
|
|
|
|
|
if (ev.streams[0].id[0] === '{') return;
|
|
|
|
|
// when stream already init
|
|
|
|
|
if (video.srcObject !== null) return;
|
2022-10-15 03:02:10 +03:00
|
|
|
|
2022-10-15 22:42:14 +03:00
|
|
|
video.srcObject = ev.streams[0];
|
|
|
|
|
}
|
2022-10-15 03:02:10 +03:00
|
|
|
|
2022-10-15 22:42:14 +03:00
|
|
|
// Safari don't support "offerToReceiveVideo"
|
|
|
|
|
// so need to create transeivers manually
|
|
|
|
|
pc.addTransceiver('video', {direction: 'recvonly'});
|
|
|
|
|
pc.addTransceiver('audio', {direction: 'recvonly'});
|
|
|
|
|
}
|
2022-10-15 03:02:10 +03:00
|
|
|
|
2022-10-15 22:42:14 +03:00
|
|
|
export default function WebRtcPlayer({ camera, width, height }) {
|
2022-10-15 23:04:54 +03:00
|
|
|
initStream(camera);
|
2022-10-15 03:02:10 +03:00
|
|
|
return (
|
|
|
|
|
<div>
|
2022-10-15 22:42:14 +03:00
|
|
|
<video id='video' autoplay playsinline controls muted width={width} height={height} />
|
2022-10-15 03:02:10 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|