mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-09-27 04:36:12 +08:00
112 lines
3.5 KiB
HTML
112 lines
3.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>go2rtc - WebRTC</title>
|
|
<style>
|
|
body {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
|
|
html, body {
|
|
height: 100%;
|
|
width: 100%;
|
|
}
|
|
|
|
#video {
|
|
/* video "container" size */
|
|
width: 100%;
|
|
height: 100%;
|
|
background: black;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<video id="video" autoplay controls playsinline muted></video>
|
|
<script>
|
|
function init(stream) {
|
|
// support api_path
|
|
const baseUrl = location.origin + location.pathname.substr(
|
|
0, location.pathname.lastIndexOf("/")
|
|
);
|
|
|
|
const ws = new WebSocket(`ws${baseUrl.substr(4)}/api/ws${location.search}`);
|
|
ws.onopen = () => {
|
|
console.debug('ws.onopen');
|
|
|
|
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));
|
|
});
|
|
});
|
|
}
|
|
ws.onmessage = ev => {
|
|
const msg = JSON.parse(ev.data);
|
|
console.debug('ws.onmessage', msg);
|
|
|
|
if (msg.type === 'webrtc/candidate') {
|
|
pc.addIceCandidate({candidate: msg.value, sdpMid: ''});
|
|
} else if (msg.type === 'webrtc/answer') {
|
|
pc.setRemoteDescription({type: 'answer', sdp: msg.value});
|
|
}
|
|
}
|
|
|
|
const pc = new RTCPeerConnection({
|
|
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
|
|
});
|
|
pc.onicecandidate = ev => {
|
|
console.debug("pc.onicecandidate", ev.candidate);
|
|
|
|
if (ev.candidate !== null) {
|
|
ws.send(JSON.stringify({
|
|
type: 'webrtc/candidate', value: ev.candidate.toJSON().candidate
|
|
}));
|
|
}
|
|
}
|
|
pc.ontrack = ev => {
|
|
const video = document.getElementById('video');
|
|
console.debug('pc.ontrack', video.srcObject !== null);
|
|
|
|
// 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;
|
|
|
|
video.srcObject = ev.streams[0];
|
|
}
|
|
|
|
// Safari don't support "offerToReceiveVideo"
|
|
// so need to create transeivers manually
|
|
pc.addTransceiver('video', {direction: 'recvonly'});
|
|
pc.addTransceiver('audio', {direction: 'recvonly'});
|
|
|
|
if (stream) {
|
|
stream.getTracks().forEach(track => {
|
|
const sender = pc.addTrack(track, stream)
|
|
// track.stop();
|
|
// setTimeout(() => {
|
|
// navigator.mediaDevices.getUserMedia({audio: true}).then(stream => {
|
|
// stream.getTracks().forEach(track => {
|
|
// sender.replaceTrack(track);
|
|
// });
|
|
// });
|
|
// }, 10000);
|
|
});
|
|
}
|
|
}
|
|
|
|
if (navigator.mediaDevices) {
|
|
navigator.mediaDevices.getUserMedia({audio: true}).then(init).catch(() => init());
|
|
} else {
|
|
init();
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |