From 12df87e791ee6e468df31a6c468595b8c124a2b4 Mon Sep 17 00:00:00 2001 From: Chris Hiszpanski Date: Sat, 21 May 2022 01:04:33 -0700 Subject: Bare bones demo * ICE candidate parser * Simple demo.html * Outline of tinyrtc.h --- example/index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 example/index.js (limited to 'example/index.js') diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..35fdbba --- /dev/null +++ b/example/index.js @@ -0,0 +1,48 @@ +window.onload = function demo() { + // create peer connection + let pc = new RTCPeerConnection({ + iceCandidatePoolSize: 4 + }); + + // create websocket for signaling channel + let ws = new WebSocket('ws://localhost:8080'); + + // (callback) called upon discovery of new local ICE candidate + pc.onicecandidate = function(event) { + if (event.candidate) { + console.log("local candidate:\n%c%s", "color: green", event.candidate.candidate); + ws.send(JSON.stringify(event.candidate)); + } + }; + + // (callback) called upon reception of new remote video track + pc.ontrack = function(event) { + document.getElementById("remoteVideo").srcObject = event.streams[0]; + }; + + // receive remote answer or candidate + ws.onmessage = function(event) { + try { + let d = JSON.parse(event.data); + if ('candidate' in d) { + if (d.candidate) { + console.log("remote candidate:\n%c%s", "color: blue;", event.data); + } + } else if ('sdp' in d) { + console.log("remote answer:\n%c%s", "color: blue;", event.data); + } + } catch(e) { + console.log("remote error:\n%c%s", "color: red;", event.data); + } + }; + + // create offer and set to remote peer + ws.onopen = function(event) { + pc.createOffer({ offerToReceiveVideo: true }).then(function(offer) { + console.log("local offer:\n%c%s", "color: green;", offer.sdp); + pc.setLocalDescription(offer); + ws.send(JSON.stringify(offer)); + }); + }; +} + -- cgit v1.2.3