Rewrite WebRTC HTML pages
This commit is contained in:
+39
-37
@@ -5,61 +5,63 @@
|
|||||||
<title>go2rtc - WebRTC</title>
|
<title>go2rtc - WebRTC</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
|
background-color: black;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
html, body {
|
html, body, video {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
#video {
|
|
||||||
/* video "container" size */
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
background: black;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<!-- muted is important for autoplay -->
|
|
||||||
<video id="video" autoplay controls playsinline muted></video>
|
<video id="video" autoplay controls playsinline muted></video>
|
||||||
<script>
|
<script>
|
||||||
// support api_path
|
function PeerConnection(userMedia) {
|
||||||
const baseUrl = location.origin + location.pathname.substr(
|
return new Promise((resolve, reject) => {
|
||||||
0, location.pathname.lastIndexOf("/")
|
|
||||||
);
|
|
||||||
|
|
||||||
const pc = new RTCPeerConnection({
|
const pc = new RTCPeerConnection({
|
||||||
iceServers: [
|
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
|
||||||
{urls: 'stun:stun.l.google.com:19302'},
|
})
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
pc.addEventListener('icegatheringstatechange', async () => {
|
pc.addEventListener('icegatheringstatechange', () => {
|
||||||
if (pc.iceGatheringState !== 'complete') return;
|
if (pc.iceGatheringState === 'complete') resolve(pc)
|
||||||
|
})
|
||||||
|
|
||||||
let r = await fetch(`${baseUrl}/api/webrtc${location.search}`, {
|
document.getElementById('video').srcObject = new MediaStream([
|
||||||
method: 'POST', body: pc.localDescription.sdp,
|
pc.addTransceiver('video', {direction: 'recvonly'}).receiver.track,
|
||||||
});
|
pc.addTransceiver('audio', {direction: 'recvonly'}).receiver.track
|
||||||
await pc.setRemoteDescription({
|
])
|
||||||
type: 'answer', sdp: await r.text()
|
|
||||||
});
|
if (userMedia) {
|
||||||
});
|
userMedia.getTracks().forEach(track => {
|
||||||
pc.addEventListener('track', ev => {
|
pc.addTransceiver(track, {direction: 'sendonly'})
|
||||||
let video = document.getElementById('video');
|
})
|
||||||
if (video.srcObject === null) {
|
|
||||||
video.srcObject = ev.streams[0];
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
pc.addTransceiver('video', {direction: 'recvonly'});
|
pc.createOffer().then(offer => pc.setLocalDescription(offer))
|
||||||
pc.addTransceiver('audio', {direction: 'recvonly'});
|
|
||||||
|
|
||||||
pc.createOffer().then(offer => {
|
setTimeout(() => resolve(pc), 3000)
|
||||||
pc.setLocalDescription(offer);
|
})
|
||||||
});
|
}
|
||||||
|
|
||||||
|
async function userMedia() {
|
||||||
|
try {
|
||||||
|
return await navigator.mediaDevices.getUserMedia({audio: true})
|
||||||
|
} catch (e) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function connect() {
|
||||||
|
const pc = await PeerConnection(await userMedia())
|
||||||
|
const url = new URL('api/webrtc' + location.search, location.href)
|
||||||
|
const r = await fetch(url, {method: 'POST', body: pc.localDescription.sdp})
|
||||||
|
await pc.setRemoteDescription({type: 'answer', sdp: await r.text()})
|
||||||
|
}
|
||||||
|
|
||||||
|
connect()
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
+48
-79
@@ -5,108 +5,77 @@
|
|||||||
<title>go2rtc - WebRTC</title>
|
<title>go2rtc - WebRTC</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
|
background-color: black;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
html, body {
|
html, body, video {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
#video {
|
|
||||||
/* video "container" size */
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
background: black;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<video id="video" autoplay controls playsinline muted></video>
|
<video id="video" autoplay controls playsinline muted></video>
|
||||||
<script>
|
<script>
|
||||||
const baseUrl = location.origin + location.pathname.substr(
|
function PeerConnection(userMedia) {
|
||||||
0, location.pathname.lastIndexOf("/")
|
|
||||||
);
|
|
||||||
|
|
||||||
function init(stream) {
|
|
||||||
// support api_path
|
|
||||||
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(() => {
|
|
||||||
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: '0'});
|
|
||||||
} else if (msg.type === 'webrtc/answer') {
|
|
||||||
pc.setRemoteDescription({type: 'answer', sdp: msg.value});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const pc = new RTCPeerConnection({
|
const pc = new RTCPeerConnection({
|
||||||
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
|
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
|
||||||
});
|
})
|
||||||
pc.onicecandidate = ev => {
|
|
||||||
console.debug("pc.onicecandidate", ev.candidate);
|
|
||||||
|
|
||||||
if (ev.candidate !== null) {
|
document.getElementById('video').srcObject = new MediaStream([
|
||||||
ws.send(JSON.stringify({
|
pc.addTransceiver('video', {direction: 'recvonly'}).receiver.track,
|
||||||
type: 'webrtc/candidate', value: ev.candidate.candidate
|
pc.addTransceiver('audio', {direction: 'recvonly'}).receiver.track
|
||||||
}));
|
])
|
||||||
}
|
|
||||||
}
|
|
||||||
pc.ontrack = ev => {
|
|
||||||
const video = document.getElementById('video');
|
|
||||||
console.debug('pc.ontrack', video.srcObject !== null);
|
|
||||||
|
|
||||||
// when audio track not exist in Chrome
|
if (userMedia) {
|
||||||
if (ev.streams.length === 0) return;
|
userMedia.getTracks().forEach(track => {
|
||||||
|
pc.addTransceiver(track, {direction: 'sendonly'})
|
||||||
// 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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stream) {
|
return pc
|
||||||
stream.getTracks().forEach(track => {
|
|
||||||
pc.addTransceiver('audio', {direction: 'sendonly'});
|
|
||||||
const sender = pc.addTrack(track, stream);
|
|
||||||
// track.stop();
|
|
||||||
// setTimeout(() => {
|
|
||||||
// navigator.mediaDevices.getUserMedia({audio: true}).then(stream => {
|
|
||||||
// stream.getTracks().forEach(track => {
|
|
||||||
// sender.replaceTrack(track);
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
// }, 10000);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safari don't support "offerToReceiveVideo"
|
async function userMedia() {
|
||||||
// so need to create transeivers manually
|
try {
|
||||||
pc.addTransceiver('video', {direction: 'recvonly'});
|
return await navigator.mediaDevices.getUserMedia({audio: true})
|
||||||
pc.addTransceiver('audio', {direction: 'recvonly'});
|
} catch (e) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (navigator.mediaDevices) {
|
async function connect() {
|
||||||
navigator.mediaDevices.getUserMedia({audio: true}).then(init).catch(() => init());
|
const pc = PeerConnection(await userMedia())
|
||||||
} else {
|
|
||||||
init();
|
const url = new URL('api/ws' + location.search, location.href)
|
||||||
|
const ws = new WebSocket('ws' + url.toString().substring(4))
|
||||||
|
|
||||||
|
ws.addEventListener('open', () => {
|
||||||
|
pc.addEventListener('icecandidate', ev => {
|
||||||
|
if (!ev.candidate) return
|
||||||
|
const msg = {type: 'webrtc/candidate', value: ev.candidate.candidate}
|
||||||
|
ws.send(JSON.stringify(msg))
|
||||||
|
})
|
||||||
|
|
||||||
|
pc.createOffer().then(offer => pc.setLocalDescription(offer)).then(() => {
|
||||||
|
const msg = {type: 'webrtc/offer', value: pc.localDescription.sdp}
|
||||||
|
ws.send(JSON.stringify(msg))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
ws.addEventListener('message', ev => {
|
||||||
|
const msg = JSON.parse(ev.data)
|
||||||
|
if (msg.type === 'webrtc/candidate') {
|
||||||
|
pc.addIceCandidate({candidate: msg.value, sdpMid: '0'})
|
||||||
|
} else if (msg.type === 'webrtc/answer') {
|
||||||
|
pc.setRemoteDescription({type: 'answer', sdp: msg.value})
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
connect()
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user