206:d0bde0e3cc62
Anton Shestakov <av6@dwimlabs.net>, Wed, 01 Jun 2016 21:26:22 +0800
index: expand, but also deduplicate existence checks for previous and contact

next change 247:fcca53a2f523
previous change 134:46bc2e4d7a20

js/webrtc.js

Permissions: -rw-r--r--

Other formats: Feeds:
// Generated by CoffeeScript 1.10.0
(function() {
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Tram.WebRTCInterface = (function() {
WebRTCInterface.prototype.lstream = null;
WebRTCInterface.prototype.rstream = null;
function WebRTCInterface(contact) {
this.contact = contact;
this.fail = bind(this.fail, this);
this.connect = bind(this.connect, this);
this.peerJid = this.contact.get('jid');
}
WebRTCInterface.prototype.init = function(initiator, constraints) {
this.initiator = initiator;
if (constraints.audio || constraints.video) {
return navigator.getUserMedia(constraints, this.connect, this.fail);
} else {
return this.connect();
}
};
WebRTCInterface.prototype.connect = function(stream) {
this.call = calls.add({
jid: this.peerJid
});
this.pc = new RTCPeerConnection({
iceServers: Tram.config.iceServers
});
if (stream != null) {
this.lstream = stream;
this.pc.addStream(stream);
this.call.set('local/stream', stream);
}
this.pc.onicecandidate = (function(_this) {
return function(event) {
if (event.candidate) {
return _this.sendPayload(event.candidate, 'ice');
}
};
})(this);
this.pc.onaddstream = (function(_this) {
return function(event) {
_this.rstream = event.stream;
_this.call.set('remote/stream', event.stream);
return _this.contact.set('callstate', 'established');
};
})(this);
if (this.initiator) {
return this.sendIntent('initiate');
} else {
return this.createOffer();
}
};
WebRTCInterface.prototype.onPayload = function(stanza) {
var el, signal;
if (this.call == null) {
return;
}
el = stanza.getElementsByTagNameNS(Tram.NS.WEBRTC, 'payload')[0];
signal = JSON.parse(el.firstChild.nodeValue);
if (signal.sdp) {
if (signal.type === 'offer') {
return this.receiveOffer(signal);
} else if (signal.type === 'answer') {
return this.receiveAnswer(signal);
}
} else if (signal.candidate) {
return this.pc.addIceCandidate(new RTCIceCandidate(signal));
}
};
WebRTCInterface.prototype.sendPayload = function(data, type) {
var msg, payload;
payload = JSON.stringify(data);
msg = $msg({
to: this.peerJid,
type: 'chat'
}).c('payload', {
xmlns: Tram.NS.WEBRTC,
type: type
}).t(payload);
return X.conn.send(msg.tree());
};
WebRTCInterface.prototype.sendIntent = function(intent) {
var msg;
msg = $msg({
to: this.peerJid,
type: 'chat'
}).c('intent', {
xmlns: Tram.NS.WEBRTC
}).t(intent);
return X.conn.send(msg.tree());
};
WebRTCInterface.prototype.createOffer = function() {
return this.pc.createOffer((function(_this) {
return function(offer) {
return _this.pc.setLocalDescription(offer, function() {
return _this.sendPayload(_this.pc.localDescription, 'offer');
}, _this.fail);
};
})(this), this.fail);
};
WebRTCInterface.prototype.receiveOffer = function(offer) {
return this.pc.setRemoteDescription(new RTCSessionDescription(offer), (function(_this) {
return function() {
return _this.pc.createAnswer(function(answer) {
return _this.pc.setLocalDescription(answer, function() {
return _this.sendPayload(_this.pc.localDescription, 'answer');
}, _this.fail);
}, _this.fail);
};
})(this), this.fail);
};
WebRTCInterface.prototype.receiveAnswer = function(answer) {
return this.pc.setRemoteDescription(new RTCSessionDescription(answer));
};
WebRTCInterface.prototype.fail = function() {
this.disconnect();
return this.contact.unset('callstate');
};
WebRTCInterface.prototype.disconnect = function() {
if (this.call != null) {
this.call.unset('remote/stream');
this.call.unset('local/stream');
calls.remove(this.call);
}
if (this.rstream != null) {
this.stopStream(this.rstream);
}
if (this.lstream != null) {
this.stopStream(this.lstream);
}
if (this.pc != null) {
this.pc.close();
return this.pc = null;
}
};
WebRTCInterface.prototype.stopStream = function(stream) {
var i, j, len, len1, ref, ref1, track;
if (stream.getAudioTracks != null) {
ref = stream.getAudioTracks();
for (i = 0, len = ref.length; i < len; i++) {
track = ref[i];
if (typeof track.stop === "function") {
track.stop();
}
}
}
if (stream.getVideoTracks != null) {
ref1 = stream.getVideoTracks();
for (j = 0, len1 = ref1.length; j < len1; j++) {
track = ref1[j];
if (typeof track.stop === "function") {
track.stop();
}
}
}
return typeof stream.stop === "function" ? stream.stop() : void 0;
};
return WebRTCInterface;
})();
}).call(this);
//# sourceMappingURL=webrtc.js.map