Code refactoring

Code refactoring
This commit is contained in:
Alexey Khit
2023-03-01 18:03:11 +03:00
parent c70c3a58f1
commit c2cdf60ffc
7 changed files with 34 additions and 17 deletions
+13 -12
View File
@@ -27,14 +27,17 @@
<video id="video" autoplay controls playsinline muted></video>
<script>
// support api_path
let baseUrl = location.origin + location.pathname.substr(
const baseUrl = location.origin + location.pathname.substr(
0, location.pathname.lastIndexOf("/")
);
let pc = new RTCPeerConnection({
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
const pc = new RTCPeerConnection({
iceServers: [
{urls: 'stun:stun.l.google.com:19302'},
]
});
pc.onicegatheringstatechange = async () => {
pc.addEventListener('icegatheringstatechange', async () => {
if (pc.iceGatheringState !== 'complete') return;
let r = await fetch(`${baseUrl}/api/webrtc${location.search}`, {
@@ -43,20 +46,18 @@
await pc.setRemoteDescription({
type: 'answer', sdp: await r.text()
});
}
pc.ontrack = ev => {
});
pc.addEventListener('track', ev => {
let video = document.getElementById('video');
if (video.srcObject === null) {
video.srcObject = ev.streams[0];
}
}
});
pc.addTransceiver('video');
pc.addTransceiver('audio');
pc.addTransceiver('video', {direction: 'recvonly'});
pc.addTransceiver('audio', {direction: 'recvonly'});
pc.createOffer({
offerToReceiveVideo: true, offerToReceiveAudio: true
}).then(offer => {
pc.createOffer().then(offer => {
pc.setLocalDescription(offer);
});
</script>
+1 -1
View File
@@ -61,7 +61,7 @@
if (ev.candidate !== null) {
ws.send(JSON.stringify({
type: 'webrtc/candidate', value: ev.candidate.toJSON().candidate
type: 'webrtc/candidate', value: ev.candidate.candidate
}));
}
}