diff options
author | Chris Hiszpanski <chris.hiszpanski@verkada.com> | 2022-05-21 01:04:33 -0700 |
---|---|---|
committer | Chris Hiszpanski <chris.hiszpanski@verkada.com> | 2022-05-21 02:01:54 -0700 |
commit | 12df87e791ee6e468df31a6c468595b8c124a2b4 (patch) | |
tree | b3eea9989ed5412383ed9912bdaac3fb1412b570 /example/index.js |
Bare bones demo
* ICE candidate parser
* Simple demo.html
* Outline of tinyrtc.h
Diffstat (limited to 'example/index.js')
-rw-r--r-- | example/index.js | 48 |
1 files changed, 48 insertions, 0 deletions
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)); + }); + }; +} + |