mozilla/talkilla — pull request #149 by n1k0 (2013-07-07 09:58:57+00:00)
Title: Introducing new WebRTC object, replacement for the WebRTCCall model

This pull request introduces a new `WebRTC` object to replace the current `WebRTCCall` Backbone model.

Sample usage:

```js
var offerer = new WebRTC(new mozRTCPeerConnection());
var answerer = new WebRTC(new mozRTCPeerConnection());

offerer.on('offer-ready', function(offer) {
answerer.answer(offer).on('answer-ready', function(answer) {
offerer.establish(answer);
});
});

offerer.initiate({video: true, audio: true});
```

`getUserMedia` calls are handled under the hood; to wait for local and remote streams to be attached to video elements:

```js
var localVideo = document.getElementById('video#local');
var remoteVideo = document.getElementById('video#remote');

offerer.on('local-stream:ready', function(stream) {
localVideo.mozSrcObject = stream;
localVideo.play();
});

offerer.on('remote-stream:ready', function(stream) {
remoteVideo.mozSrcObject = stream;
remoteVideo.play();
});

offerer.on('connection-terminated', function() {
localVideo.mozSrcObject = null;
});

answerer.on('connection-terminated', function() {
remoteVideo.mozSrcObject = null;
});
```

Data channels are also supported:

```js
offerer.on('dc:open', function() {
this.send('hi!');
});

offerer.on('dc:message-in', function(event) {
console.log('incoming message', event.data); // "hi!"
});
```

This is possibly something to become a standalone library, though it still have a dependency to `Backbone.Events` — which may be replaced by something else, or events handling may be implemented by hand if needed.