Commit c43c9ea4 authored by Jakub Adam's avatar Jakub Adam
Browse files

Clean up WebRTC client code

parent f60f590a
...@@ -46,38 +46,7 @@ ...@@ -46,38 +46,7 @@
hls.loadSource('hls/playlist.m3u8'); hls.loadSource('hls/playlist.m3u8');
} }
var client = new mss.Client() var webrtc = new mss.WebRTCStream(document.getElementById("webrtc_video"))
var webrtc = new RTCPeerConnection()
client.onoffer = async (msg) => {
try {
console.log(msg.sdp)
await webrtc.setRemoteDescription({ type: 'offer', sdp: msg.sdp })
var answer = await webrtc.createAnswer()
console.log(answer)
webrtc.setLocalDescription(answer)
client.answer(answer.sdp)
} catch (err) {
console.log(err.message)
}
}
client.oncandidate = (c) => {
console.log(c)
webrtc.addIceCandidate(c)
}
webrtc.onicecandidate = (c) => {
if (c.candidate) {
client.candidate(c.candidate)
}
}
webrtc.ontrack = (event) => {
console.log(event)
var webrtc_video = document.getElementById("webrtc_video")
webrtc_video.srcObject = event.streams[0];
}
client.connect()
</script> </script>
</body> </body>
</html> </html>
"use strict"; "use strict";
export class Client { class Client {
constructor() { constructor() {
this.__requestId = 0 this.__requestId = 0
this.onopen = undefined this.onopen = undefined
...@@ -82,3 +82,39 @@ export class Client { ...@@ -82,3 +82,39 @@ export class Client {
this.__ws = undefined this.__ws = undefined
} }
} }
export class WebRTCStream {
constructor(video) {
this.__webrtc = new RTCPeerConnection()
this.__signaling = new Client()
this.__signaling.onoffer = async (msg) => {
try {
console.log(msg.sdp)
await this.__webrtc.setRemoteDescription({ type: 'offer', sdp: msg.sdp })
var answer = await this.__webrtc.createAnswer()
console.log(answer)
this.__webrtc.setLocalDescription(answer)
this.__signaling.answer(answer.sdp)
} catch (err) {
console.log(err.message)
}
}
this.__signaling.oncandidate = (c) => {
console.log(c)
this.__webrtc.addIceCandidate(c)
}
this.__webrtc.onicecandidate = (c) => {
if (c.candidate) {
this.__signaling.candidate(c.candidate)
}
}
this.__webrtc.ontrack = (event) => {
console.log(event)
video.srcObject = event.streams[0];
}
this.__signaling.connect()
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment