diff --git a/samples/js/demos/html/multiple-relay.html b/samples/js/demos/html/multiple-relay.html new file mode 100644 index 0000000000..051aad525f --- /dev/null +++ b/samples/js/demos/html/multiple-relay.html @@ -0,0 +1,98 @@ + + + +PeerConnection Demo 1 + + + + + + + + +
+ + + + +
+ + + + diff --git a/samples/js/demos/js/videopipe.js b/samples/js/demos/js/videopipe.js new file mode 100644 index 0000000000..da9e22502b --- /dev/null +++ b/samples/js/demos/js/videopipe.js @@ -0,0 +1,68 @@ +// +// A "videopipe" abstraction on top of WebRTC. +// +// The usage of this abstraction: +// var pipe = new VideoPipe(mediastream, handlerFunction); +// handlerFunction = function(mediastream) { +// do_something +// } +// pipe.close(); +// +// The VideoPipe will set up 2 PeerConnections, connect them to each +// other, and call HandlerFunction when the stream is available in the +// second PeerConnection. +// + +function errorHandler(context) { + return function(error) { + trace('Failure in ' + context + ': ' + error.toString); + } +} + +function successHandler(context) { + return function() { + trace('Success in ' + context); + } +} + +function noAction() { +} + + +function VideoPipe(stream, handler) { + var servers = null; + var pc1 = new RTCPeerConnection(servers); + var pc2 = new RTCPeerConnection(servers); + + pc1.addStream(stream); + pc1.onicecandidate = function(event) { + if (event.candidate) { + pc2.addIceCandidate(new RTCIceCandidate(event.candidate), + noAction, errorHandler('AddIceCandidate')); + } + } + pc2.onicecandidate = function(event) { + if (event.candidate) { + pc1.addIceCandidate(new RTCIceCandidate(event.candidate), + noAction, errorHandler('AddIceCandidate')); + } + } + pc2.onaddstream = function(e) { + handler(e.stream); + } + pc1.createOffer(function(desc) { + pc1.setLocalDescription(desc); + pc2.setRemoteDescription(desc); + pc2.createAnswer(function(desc2) { + pc2.setLocalDescription(desc2); + pc1.setRemoteDescription(desc2); + }, errorHandler('pc2.createAnswer')); + }, errorHandler('pc1.createOffer')); + this.pc1 = pc1; + this.pc2 = pc2; +} + +VideoPipe.prototype.close = function() { + this.pc1.close(); + this.pc2.close(); +}