- From: Adam Bergkvist <adam.bergkvist@ericsson.com>
- Date: Thu, 6 Jun 2013 13:54:34 +0200
- To: "public-webrtc@w3.org" <public-webrtc@w3.org>
Hi I played around a bit with slightlyoff's Future/Promise polyfill, redefined some of the methods on PeerConnection and tested it in chrome. Here's how our current simple example from the spec could look with futures. It's not the simplest re-write (method(success, error) -> method().then(success, error)); it uses some chaining. An alternative would be to not listen for the negotiationneeded event and chain offer generation with the call to getUserMedia(). Here's the code if someone is interested. /Adam var remotePeer = new SignalingChannel(); var configuration = { "iceServers": [{ "url": "stun:stun.example.org" }] }; var pc; // call start() to initiate function start() { pc = new RTCPeerConnection(configuration); // send any ice candidates to the other peer pc.onicecandidate = function (evt) { if (evt.candidate) remotePeer.send(JSON.stringify( { "candidate": evt.candidate })); }; // let the "negotiationneeded" event trigger offer generation pc.onnegotiationneeded = function () { pc.createOffer().then(function (offer) { // set the newly created offer return pc.setLocalDescription(offer); }, logError) .then(function () { // send the offer to the other peer remotePeer.send(JSON.stringify( { "sdp": pc.localDescription })); }, logError); }; // once remote stream arrives, show it in the remote video element pc.onaddstream = function (evt) { remoteVideo.src = URL.createObjectURL(evt.stream); }; // get a local stream, show it in a self-view and add it to be sent navigator.getUserMedia({ "audio": true, "video": true }) .then(function (stream) { selfVideo.src = URL.createObjectURL(stream); pc.addStream(stream); }); } remotePeer.onmessage = function (evt) { // got signaling message from other peer if (!pc) start(); var message = JSON.parse(evt.data); if (message.sdp) { var desc = new RTCSessionDescription(message.sdp); pc.setRemoteDescription(desc).then(function () { // if we received an offer, we need to create an answer if (pc.remoteDescription.type == "offer") { pc.createAnswer().then(function (answer) { // set answer locally return pc.setLocalDescription(answer); }, logError) .then(function () { // send our updated local description to ther peer remotePeer.send(JSON.stringify( { "sdp": pc.localDescription })); }, logError); } }, logError); } else pc.addIceCandidate(new RTCIceCandidate(message.candidate)); }; function logError(error) { log(error.name + ": " + error.message); }
Received on Thursday, 6 June 2013 11:55:00 UTC