Created
August 20, 2015 08:27
-
-
Save SET001/1a28410f86c06828f3f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.6.3 | |
(function() { | |
window._RTCPeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.RTCPeerConnection; | |
window._RTCSessionDescription = window.webkitRTCSessionDescription || window.mozRTCSessionDescription || window.RTCSessionDescription; | |
window._IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate; | |
if (!window._RTCPeerConnection) { | |
console.error('Your browser doesn\'t support WebRTC'); | |
} | |
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia; | |
app.factory('PeerConnection', [ | |
'$rootScope', 'XMPP', 'Config', '$q', 'Account', 'LocalStorage', function($rootScope, XMPP, Config, $q, Account, LSParams) { | |
var PeerConnection; | |
return PeerConnection = (function() { | |
var new_ice_servers, _nick, _turnToken; | |
PeerConnection.prototype.connection = null; | |
PeerConnection.prototype.to = ''; | |
PeerConnection.prototype.candidates = []; | |
PeerConnection.prototype.channel = null; | |
PeerConnection.prototype.ls = null; | |
PeerConnection.prototype.remote_stream_url = ''; | |
new_ice_servers = []; | |
_nick = LSParams.getItem('nick'); | |
_turnToken = LSParams.getModuleParameter('turn', 'token'); | |
if (!!_nick && !!_turnToken) { | |
Config.iceServers.forEach(function(ice_server) { | |
var tempIceServer; | |
tempIceServer = {}; | |
tempIceServer.url = ice_server.url; | |
if (ice_server.url.indexOf('stun') >= 0 && Config.secureStun) { | |
tempIceServer.credential = _turnToken; | |
tempIceServer.username = _nick; | |
} else if (ice_server.url.indexOf('stun') === -1) { | |
tempIceServer.credential = _turnToken; | |
tempIceServer.username = _nick; | |
} | |
return new_ice_servers.push(tempIceServer); | |
}); | |
} | |
function PeerConnection(to, channel) { | |
var self, | |
_this = this; | |
self = this; | |
console.log("creating new connection for ", to.toString(), channel); | |
this.channel = channel; | |
this.to = to; | |
this.connection = new _RTCPeerConnection({ | |
iceServers: new_ice_servers | |
}); | |
this.connection.onconnection = function() { | |
return console.log('rtc connected'); | |
}; | |
this.connection.onicecandidate = function(ev) { | |
var request; | |
request = JSON.stringify({ | |
'candidate': ev.candidate | |
}); | |
return _this.sig_out(request, _this.to.toString()); | |
}; | |
this.connection.onclosedconnection = function() { | |
return console.log('rtc disconnected'); | |
}; | |
this.connection.onnegotiationneeded = function(e) { | |
return console.log("onnegotiationneeded", e); | |
}; | |
this.connection.onstream = function(stream) { | |
return console.log('STREAM!!!!!!!!!', stream); | |
}; | |
this.connection.onaddstream = function(event) { | |
return $rootScope.$broadcast('remote_stream_added', event, _this.channel, _this.to.getResource()); | |
}; | |
this.connection.onremovestream = function(event) { | |
return console.log("onremovestream", event); | |
}; | |
this.connection.oniceconnectionstatechange = function(event) { | |
console.log("oniceconnectionstatechange", event.iceConnectionState); | |
if (event.target.iceConnectionState === 'disconnected') { | |
console.log("################## C L O S I N G C O N N E C T I O N !!!!!!"); | |
_this.close(); | |
return $rootScope.$digest(); | |
} | |
}; | |
this.connection.onsignalingstatechange = function(event) { | |
return console.log("onsignalingstatechange", event); | |
}; | |
this.bandwidth = { | |
audio: 50, | |
video: 256 | |
}; | |
if (this.channel.is_muc) { | |
this.name = this.to.getResource(); | |
} else { | |
this.name = this.to.getNode(); | |
} | |
} | |
PeerConnection.prototype.set_bandwidth = function(sdp) { | |
if (this.bandwidth) { | |
if (this.bandwidth.audio || this.bandwidth.video || this.bandwidth.data) { | |
sdp = sdp.replace(/b=AS([^\r\n]+\r\n)/g, ''); | |
} | |
if (this.bandwidth.audio) { | |
sdp = sdp.replace(/a=mid:audio\r\n/g, 'a=mid:audio\r\nb=AS:' + this.bandwidth.audio + '\r\n'); | |
} | |
if (this.bandwidth.video) { | |
sdp = sdp.replace(/a=mid:video\r\n/g, 'a=mid:video\r\nb=AS:' + this.bandwidth.video + '\r\n'); | |
} | |
} | |
return sdp; | |
}; | |
PeerConnection.prototype.add_stream = function(stream) { | |
return this.connection.addStream(stream); | |
}; | |
PeerConnection.prototype.close = function() { | |
console.log("closing connection with " + this.name); | |
this.channel.remote_streems = _.reject(this.channel.remote_streems, { | |
url: this.remote_stream_url | |
}); | |
if (!this.channel.remote_streems) { | |
this.channel.remote_streems = []; | |
} | |
this.connection.close(); | |
return $(this.channel).trigger('peer_disconnected', this.name); | |
}; | |
PeerConnection.prototype.error_handler = function(e) { | |
return console.log(e); | |
}; | |
PeerConnection.prototype.constraints = { | |
mandatory: { | |
OfferToReceiveAudio: true, | |
OfferToReceiveVideo: true | |
} | |
}; | |
PeerConnection.prototype.accept_offer = function(sdp, success) { | |
var _this = this; | |
sdp = new _RTCSessionDescription(sdp); | |
return this.connection.setRemoteDescription(sdp, function() { | |
var candidate, _i, _len, _ref; | |
if (_this.candidates.length) { | |
_ref = _this.candidates; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
candidate = _ref[_i]; | |
_this.connection.addIceCandidate(candidate); | |
} | |
_this.candidates = []; | |
} | |
return _this.connection.createAnswer(function(description) { | |
description = new _RTCSessionDescription(description); | |
_this.local_description = description; | |
return _this.connection.setLocalDescription(_this.local_description, function() { | |
return success(); | |
}, _this.error_handler); | |
}, _this.error_handler, _this.constraints); | |
}, this.error_handler); | |
}; | |
PeerConnection.prototype.create_offer = function(success) { | |
var _this = this; | |
return this.connection.createOffer(function(desc) { | |
desc = new _RTCSessionDescription({ | |
sdp: _this.set_bandwidth(desc.sdp), | |
type: desc.type | |
}); | |
_this.local_description = desc; | |
_this.set_ld.call(_this, desc, success); | |
return console.log("offer created for", _this.to.toString(), _this.local_description, _this); | |
}, this.error_handler, this.constraints); | |
}; | |
PeerConnection.prototype.accept_candidate = function(candidate) { | |
candidate = new _IceCandidate(candidate); | |
if (this.connection.remoteDescription) { | |
return this.connection.addIceCandidate(candidate); | |
} else { | |
return this.candidates.push(candidate); | |
} | |
}; | |
PeerConnection.prototype.set_rd = function(desc, success) { | |
var _this = this; | |
desc = new _RTCSessionDescription(desc); | |
console.log("setting remote description for ", this.to.toString(), desc); | |
console.log("connections", this); | |
return this.connection.setRemoteDescription(desc, function() { | |
var candidate, _i, _len, _ref; | |
console.log("remote description set for", _this.to.toString()); | |
if (_this.candidates.length) { | |
console.log("accepting " + _this.candidates.length + " pending candidates..."); | |
_ref = _this.candidates; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
candidate = _ref[_i]; | |
_this.connection.addIceCandidate(candidate); | |
} | |
return _this.candidates = []; | |
} | |
}, this.error_handler); | |
}; | |
PeerConnection.prototype.set_ld = function(desc, success) { | |
var _this = this; | |
return this.connection.setLocalDescription(desc, function() { | |
console.log("Local description set for", _this.to.toString()); | |
if (success) { | |
return success(desc); | |
} | |
}, function(e) { | |
return console.log(e); | |
}); | |
}; | |
PeerConnection.prototype.sig_out = function(message, to) { | |
var type; | |
if (this.channel.is_muc) { | |
type = "chat"; | |
} else { | |
type = ""; | |
} | |
return XMPP.sendMsg(to, message, type); | |
}; | |
PeerConnection.prototype.sig_in = function(message, from) { | |
var _this = this; | |
if (message.sdp) { | |
console.log("sdp message for", this.to.toString()); | |
switch (message.sdp.type) { | |
case 'offer': | |
this.accept_offer(message.sdp, function() { | |
var answer; | |
answer = JSON.stringify({ | |
sdp: _this.local_description | |
}); | |
console.log("FINISHED! sending answer!"); | |
Account.user.set_status('busy'); | |
$rootScope.$apply(); | |
return _this.sig_out(answer, from.toString()); | |
}); | |
break; | |
case 'answer': | |
console.log("answer for", this.to.toString()); | |
this.set_rd(message.sdp); | |
} | |
} | |
if (message.dropcall) { | |
this.close(); | |
} | |
if (message.candidate) { | |
return this.accept_candidate(message.candidate); | |
} | |
}; | |
return PeerConnection; | |
})(); | |
} | |
]); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment