Permalink
Browse files

Create 'Peer' module from 'User' and 'PC' modules

This consolidation will help to make the prototype code base more
understandable. It is also the first step in supporting multiple
simultaneous peer connections.
  • Loading branch information...
1 parent ff714fc commit d9fa758e547d1d456100c8711b74c48f661aea3c @jugglinmike jugglinmike committed Mar 25, 2013
@@ -1,7 +1,6 @@
require([
- 'modules/user', 'modules/transport', 'modules/pc', 'modules/layout',
- 'backbone', 'q'
- ], function(User, Transport, PC, Layout, Backbone, Q) {
+ 'modules/peer', 'modules/transport', 'modules/layout', 'backbone', 'q'
+ ], function(Peer, Transport, Layout, Backbone, Q) {
'use strict';
var config = {
@@ -37,8 +36,8 @@ require([
console.error('Create Answer failed');
}
- var user = new User();
- var pc = new PC();
+ var user = new Peer();
+ var pc = new Peer();
var layout = new Layout({
el: '#app',
user: user,
@@ -50,7 +49,7 @@ require([
var remotePeerID = 'creationix';
if (!pc.isActive() && transport.state === 'OPEN') {
- pc.init(config.pcConfig);
+ pc.connect(config.pcConfig);
pc.addStream(stream);
pc.createOffer(
function(sessionDescription) {
@@ -61,7 +60,7 @@ require([
}).then(function(findReply) {
console.log('Promise Resolved', findReply);
pc.setRemoteDescription(findReply.sessionDescription);
- window.TO_ID = findReply.from;
+ pc.set('locationID', findReply.from);
}, function() {
// TODO: Update the UI to reflect this failure.
console.error('Find request failed.');
@@ -73,7 +72,7 @@ require([
});
layout.on('hangup', function() {
transport.request('bye', {
- to: window.TO_ID
+ to: pc.get('locationID')
});
pc.destroy();
});
@@ -89,7 +88,7 @@ require([
console.log('Sending ICE candidate:', candidate);
transport.request('update', {
candidate: candidate,
- to: window.TO_ID
+ to: pc.get('locationID')
});
});
var transport = new Transport({
@@ -112,13 +111,13 @@ require([
'. Would you like to answer?');
if (!pc.isActive()) {
- pc.init(config.pcConfig);
+ pc.connect(config.pcConfig);
// TODO: Refactor so transport is not so tightly-coupled to the layout.
// This should also allow recieving calls without sharing the local
// stream.
pc.addStream(layout.localStreamView.getStream());
}
- window.TO_ID = request.username.from;
+ pc.set('locationID', request.username.from);
console.log('Creating remote session description:', remoteSession);
pc.setRemoteDescription(remoteSession);
console.log('Sending answer...');
@@ -1,14 +1,20 @@
define([
- 'modules/rtc-compat', '_', 'backbone'
- ], function(rtc, _, Backbone) {
+ 'modules/rtc-compat', 'backbone'
+ ], function(rtc, Backbone) {
'use strict';
- function PC() {
- }
-
- _.extend(PC.prototype, Backbone.Events);
+ var Peer = Backbone.Model.extend({
+ nameRegex: /^[0-9a-z\.-]+$/i,
+ validate: function(attrs) {
+ if (!attrs || !attrs.name) {
+ return new Error('No username specified');
+ } else if (!this.nameRegex.test(attrs.name)) {
+ return new Error('Invalid username');
+ }
+ }
+ });
- PC.prototype.init = function(options) {
+ Peer.prototype.connect = function(options) {
var peerConn;
if (this.peerConn) {
this.destroy();
@@ -28,30 +34,30 @@ define([
// addStream
// Add a stream object to the local stream set of the Peer Connection
// instance
- PC.prototype.addStream = function(stream) {
+ Peer.prototype.addStream = function(stream) {
this.peerConn.addStream(stream);
};
// setLocalDescription
// Create a valid WebRTC Session Description object from the provided data
// and set it as the local description of the Peer Connection instance
- PC.prototype.setLocalDescription = function(desc) {
+ Peer.prototype.setLocalDescription = function(desc) {
desc = new rtc.RTCSessionDescription(desc);
this.peerConn.setLocalDescription(desc);
};
// setRemoteDescription
// Create a valid WebRTC Session Description object from the provided data
// and set it as the remote description of the Peer Connection instance
- PC.prototype.setRemoteDescription = function(desc) {
+ Peer.prototype.setRemoteDescription = function(desc) {
desc = new rtc.RTCSessionDescription(desc);
this.peerConn.setRemoteDescription(desc);
};
// addIceCandidate
// Create a valid WebRTC Ice Candidate object from the provided data and add
// it to the Peer Connection instance
- PC.prototype.addIceCandidate = function(candidateData) {
+ Peer.prototype.addIceCandidate = function(candidateData) {
var candidate = new rtc.RTCIceCandidate({
sdpMLineIndex: candidateData.sdpMLineIndex,
sdpMid: candidateData.sdpMid,
@@ -60,7 +66,7 @@ define([
this.peerConn.addIceCandidate(candidate);
};
- PC.prototype._handleIceCandidate = function(evt) {
+ Peer.prototype._handleIceCandidate = function(evt) {
var candidate = evt && evt.candidate;
var msg;
if (candidate) {
@@ -76,27 +82,27 @@ define([
}
};
- PC.prototype._handleAddStream = function(event) {
+ Peer.prototype._handleAddStream = function(event) {
this.trigger('addstream', event.stream);
};
- PC.prototype._handleRemoveStream = function() {
+ Peer.prototype._handleRemoveStream = function() {
this.trigger('removestream');
};
- PC.prototype.isActive = function() {
+ Peer.prototype.isActive = function() {
return !!this.peerConn;
};
- PC.prototype.createOffer = function(success, failure, mediaConstraints) {
+ Peer.prototype.createOffer = function(success, failure, mediaConstraints) {
var cbs = this._wrapCallbacks({
success: success,
failure: failure
});
this.peerConn.createOffer(cbs.success, cbs.failure, mediaConstraints);
};
- PC.prototype.createAnswer = function(success, failure, mediaConstraints) {
+ Peer.prototype.createAnswer = function(success, failure, mediaConstraints) {
var cbs = this._wrapCallbacks({
success: success,
failure: failure
@@ -106,9 +112,9 @@ define([
// _wrapCallbacks
// Private method to create callbacks that will be invoked in the context of
- // the PC object. Avoiding `Function.prototype.bind` allows users to
+ // the Peer object. Avoiding `Function.prototype.bind` allows users to
// optionally override the context of their callback functions.
- PC.prototype._wrapCallbacks = function(callbacks) {
+ Peer.prototype._wrapCallbacks = function(callbacks) {
var self = this;
var success = callbacks.success;
var failure = callbacks.failure;
@@ -125,11 +131,11 @@ define([
return callbacks;
};
- PC.prototype.destroy = function() {
+ Peer.prototype.destroy = function() {
this.trigger('destroy');
this.peerConn.close();
delete this.peerConn;
};
- return PC;
+ return Peer;
});
@@ -18,7 +18,8 @@ define(function() {
}
};
- if (RTCPeerConnection.prototype && 'addEventListener' in RTCPeerConnection.prototype) {
+ if (RTCPeerConnection && RTCPeerConnection.prototype &&
+ 'addEventListener' in RTCPeerConnection.prototype) {
rtc.on = strats.on.webkit;
} else {
rtc.on = strats.on.moz;
@@ -1,16 +0,0 @@
-define(['backbone'], function(Backbone) {
- 'use strict';
-
- var User = Backbone.Model.extend({
- nameRegex: /^[0-9a-z\.-]+$/i,
- validate: function(attrs) {
- if (!attrs || !attrs.name) {
- return new Error('No username specified');
- } else if (!this.nameRegex.test(attrs.name)) {
- return new Error('Invalid username');
- }
- }
- });
-
- return User;
-});
@@ -1,3 +1,3 @@
define([
- 'tests/user'
+ 'tests/peer'
]);
@@ -0,0 +1,19 @@
+define(['modules/peer'], function(Peer) {
+ 'use strict';
+
+ suite('Peer', function() {
+ suite('#validate', function() {
+ var peer;
+ suiteSetup(function() {
+ peer = new Peer();
+ });
+ test('Rejects unspecified string names', function() {
+ assert.instanceOf(peer.validate(), Error);
+ assert.instanceOf(peer.validate({ name: '' }), Error);
+ });
+ test('Rejects invalid names', function() {
+ assert.instanceOf(peer.validate({ name: '!@#@#$' }), Error);
+ });
+ });
+ });
+});
@@ -1,19 +0,0 @@
-define(['modules/user'], function(User) {
- 'use strict';
-
- suite('User', function() {
- suite('#validate', function() {
- var user;
- suiteSetup(function() {
- user = new User();
- });
- test('Rejects unspecified string names', function() {
- assert.instanceOf(user.validate(), Error);
- assert.instanceOf(user.validate({ name: '' }), Error);
- });
- test('Rejects invalid names', function() {
- assert.instanceOf(user.validate({ name: '!@#@#$' }), Error);
- });
- });
- });
-});

0 comments on commit d9fa758

Please sign in to comment.