Skip to content

Commit a612987

Browse files
committed
Add 'stun_servers' and 'turn_servers' configuration parameters
Which allow setting multiple stun and turn servers
2 parents 1a58156 + d0dbde3 commit a612987

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

src/MediaSession.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,20 @@ JsSIP.MediaSession.prototype = {
107107
* @param {Function} onSuccess Fired when there are no more ICE candidates
108108
*/
109109
start: function(onSuccess) {
110-
var
110+
var idx, server,
111111
session = this,
112112
sent = false,
113-
stun_uri = this.session.ua.configuration.stun_server,
114-
turn_uri = this.session.ua.configuration.turn_uri,
115-
turn_password = this.session.ua.configuration.turn_password,
116-
servers = [ {"url": stun_uri} ];
113+
servers = [];
117114

118-
if (turn_uri) {
119-
servers.push({"url": turn_uri, "credential": turn_password});
120-
}
115+
for (idx in this.session.ua.configuration.stun_servers) {
116+
server = this.session.ua.configuration.stun_server[idx];
117+
servers.push({'url': server});
118+
}
119+
120+
for (idx in this.session.ua.configuration.turn_servers) {
121+
server = this.session.ua.configuration.turn_servers[idx];
122+
servers.push({'url': server.username +'@'+ server.server, 'credential': server.password});
123+
}
121124

122125
this.peerConnection = new webkitRTCPeerConnection({"iceServers": servers});
123126

src/UA.js

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ JsSIP.UA.prototype.loadConfig = function(configuration) {
638638

639639
// Session parameters
640640
no_answer_timeout: 60,
641-
stun_server: 'stun:stun.l.google.com:19302',
641+
stun_servers: ['stun:stun.l.google.com:19302'],
642642

643643
// Logging parameters
644644
trace_sip: false,
@@ -699,14 +699,6 @@ JsSIP.UA.prototype.loadConfig = function(configuration) {
699699
return false;
700700
}
701701

702-
// Turn
703-
if (settings.turn_server) {
704-
if (!settings.turn_username || !settings.turn_password) {
705-
console.error('TURN username and password must be specified');
706-
return false;
707-
}
708-
}
709-
710702
// Post Configuration Process
711703

712704
// Instance-id for GRUU
@@ -752,19 +744,19 @@ JsSIP.UA.prototype.loadConfig = function(configuration) {
752744

753745
}
754746

755-
// TURN URI
756-
if (settings.turn_server) {
757-
uri = JsSIP.grammar.parse(settings.turn_server, 'turn_URI');
758-
settings.turn_uri = uri.scheme + ':';
759-
settings.turn_uri += settings.turn_username + '@';
760-
settings.turn_uri += settings.turn_server.substr(uri.scheme.length + 1);
761-
}
762-
763747
contact = {
764748
uri: {value: 'sip:' + uri.user + '@' + settings.via_host + ';transport=ws', writable: false, configurable: false}
765749
};
766750
Object.defineProperties(this.contact, contact);
767751

752+
// Stun servers
753+
for (idx in configuration.stun_servers) {
754+
uri = configuration.stun_servers[idx];
755+
if (!(/^stuns?:/.test(uri))){
756+
configuration.stun_servers[idx] = 'stun:' + uri;
757+
}
758+
}
759+
768760
// Fill the value of the configuration_skeleton
769761
for(attribute in settings) {
770762
JsSIP.UA.configuration_skeleton[attribute].value = settings[attribute];
@@ -813,11 +805,8 @@ JsSIP.UA.configuration_skeleton = (function() {
813805
"hack_via_tcp", // false.
814806
"hack_ip_in_contact", //false
815807
"password",
816-
"stun_server",
817-
"turn_server",
818-
"turn_username",
819-
"turn_password",
820-
"turn_uri",
808+
"stun_servers",
809+
"turn_servers",
821810
"no_answer_timeout", // 30 seconds.
822811
"register_expires", // 600 seconds.
823812
"trace_sip",
@@ -935,33 +924,41 @@ JsSIP.UA.configuration_check = {
935924
return true;
936925
}
937926
},
938-
stun_server: function(stun_server) {
939-
if(JsSIP.grammar.parse(stun_server, 'stun_URI') === -1) {
940-
return false;
941-
} else {
942-
return true;
943-
}
944-
},
945-
turn_server: function(turn_server) {
946-
if(JsSIP.grammar.parse(turn_server, 'turn_URI') === -1) {
947-
return false;
948-
} else {
949-
return true;
950-
}
951-
},
952-
turn_username: function(turn_username) {
953-
if(JsSIP.grammar.parse(turn_username, 'user') === -1) {
954-
return false;
955-
} else {
956-
return true;
927+
stun_servers: function(stun_servers) {
928+
var idx, stun_server;
929+
930+
for (idx in stun_servers) {
931+
stun_server = stun_servers[idx];
932+
if (!(/^stuns?:/.test(stun_server))){
933+
stun_server = 'stun:' + stun_server;
934+
}
935+
936+
if(JsSIP.grammar.parse(stun_server, 'stun_URI') === -1) {
937+
return false;
938+
}
957939
}
940+
return true;
958941
},
959-
turn_password: function(turn_password) {
960-
if(JsSIP.grammar.parse(turn_password, 'password') === -1) {
961-
return false;
962-
} else {
963-
return true;
942+
turn_servers: function(turn_servers) {
943+
var idx, turn_server;
944+
945+
for (idx in turn_servers) {
946+
turn_server = turn_servers[idx];
947+
if (!turn_server.server || !turn_server.username || !turn_server.password) {
948+
return false;
949+
} else if (!(/^turns?:/.test(turn_server.server))){
950+
turn_server.server = 'turn:' + turn_server.server;
951+
}
952+
953+
if(JsSIP.grammar.parse(turn_server.server, 'turn_URI') === -1) {
954+
return false;
955+
} else if(JsSIP.grammar.parse(turn_server.username, 'user') === -1) {
956+
return false;
957+
} else if(JsSIP.grammar.parse(turn_server.password, 'password') === -1) {
958+
return false;
959+
}
964960
}
961+
return true;
965962
},
966963
no_answer_timeout: function(no_answer_timeout) {
967964
if(!Number(no_answer_timeout)) {

0 commit comments

Comments
 (0)