File: source/ice-connection.js

  1. /**
  2. * Function that filters and configures the ICE servers received from Signaling
  3. * based on the <code>init()</code> configuration and returns the updated
  4. * list of ICE servers to be used when constructing Peer connection.
  5. * @method _setIceServers
  6. * @private
  7. * @for Skylink
  8. * @since 0.5.4
  9. */
  10. Skylink.prototype._setIceServers = function(passedIceServers) {
  11. var self = this;
  12. var iceServerName = null;
  13. var iceServerProtocol = 'stun';
  14. var iceServerPorts = {
  15. udp: [3478, 19302, 19303, 19304],
  16. tcp: [80, 443],
  17. both: [19305, 19306, 19307, 19308]
  18. };
  19. var iceServers = [
  20. // Public
  21. { urls: [] },
  22. // Private
  23. { urls: [] }
  24. ];
  25.  
  26. // Note: Provide only 1 single TURN! turn:xxxx.io for custom TURN servers. Ignores custom ports.
  27. passedIceServers.forEach(function (server) {
  28. if (server.url.indexOf('stun:') === 0) {
  29. if (server.url.indexOf('temasys') > 0) {
  30. // server[?transport=xxx]
  31. iceServerName = (server.url.split(':')[1] || '').split('?')[0] || null;
  32. } else {
  33. iceServers[0].urls.push(server.url);
  34. }
  35.  
  36. } else if (server.url.indexOf('turn:') === 0 && server.url.indexOf('@') > 0 && server.credential &&
  37. !(iceServers[1].username || iceServers[1].credential)) {
  38. var parts = server.url.split(':');
  39. var urlParts = (parts[1] || '').split('@');
  40.  
  41. // server[?transport=xxx]
  42. iceServerName = (urlParts[1] || '').split('?')[0];
  43. iceServers[1].username = urlParts[0];
  44. iceServers[1].credential = server.credential;
  45. iceServerProtocol = 'turn';
  46. }
  47. });
  48.  
  49. if (self._initOptions.iceServer) {
  50. iceServers = [{
  51. urls: self._initOptions.iceServer.urls,
  52. username: iceServers[1].username || null,
  53. credential: iceServers[1].credential || null
  54. }];
  55.  
  56. } else {
  57. iceServerName = iceServerName || 'turn.temasys.io';
  58.  
  59. if (iceServerProtocol === 'turn' && !self._initOptions.enableTURNServer && !self._initOptions.forceTURNSSL) {
  60. iceServerProtocol = 'stun';
  61.  
  62. } else if (AdapterJS.webrtcDetectedBrowser === 'edge') {
  63. iceServerPorts.udp = [3478];
  64. iceServerPorts.tcp = [];
  65. iceServerPorts.both = [];
  66. iceServerProtocol = 'turn';
  67.  
  68. } else if (self._initOptions.forceTURNSSL) {
  69. if (AdapterJS.webrtcDetectedBrowser === 'firefox' && AdapterJS.webrtcDetectedVersion < 53) {
  70. iceServerPorts.udp = [];
  71. iceServerPorts.tcp = [443];
  72. iceServerPorts.both = [];
  73. iceServerProtocol = 'turn';
  74.  
  75. } else {
  76. iceServerPorts.udp = [];
  77. iceServerProtocol = 'turns';
  78. }
  79.  
  80. // Limit the number of ports..
  81. } else if (AdapterJS.webrtcDetectedBrowser === 'firefox') {
  82. iceServerPorts.udp = [3478];
  83. iceServerPorts.tcp = [443, 80];
  84. }
  85.  
  86. if (self._initOptions.TURNServerTransport === self.TURN_TRANSPORT.UDP && !self._initOptions.forceTURNSSL) {
  87. iceServerPorts.udp = iceServerPorts.udp.concat(iceServerPorts.both);
  88. iceServerPorts.tcp = [];
  89. iceServerPorts.both = [];
  90.  
  91. } else if (self._initOptions.TURNServerTransport === self.TURN_TRANSPORT.TCP) {
  92. iceServerPorts.tcp = iceServerPorts.tcp.concat(iceServerPorts.both);
  93. iceServerPorts.udp = [];
  94. iceServerPorts.both = [];
  95.  
  96. } else if (self._initOptions.TURNServerTransport === self.TURN_TRANSPORT.NONE) {
  97. iceServerPorts.tcp = [];
  98. iceServerPorts.udp = [];
  99. }
  100.  
  101. if (iceServerProtocol === 'stun') {
  102. iceServerPorts.tcp = [];
  103. }
  104.  
  105. if (iceServerProtocol === 'stun' && !self._initOptions.enableSTUNServer) {
  106. iceServers = [];
  107.  
  108. } else {
  109. iceServerPorts.tcp.forEach(function (tcpPort) {
  110. iceServers[1].urls.push(iceServerProtocol + ':' + iceServerName + ':' + tcpPort + '?transport=tcp');
  111. });
  112.  
  113. iceServerPorts.udp.forEach(function (udpPort) {
  114. iceServers[1].urls.push(iceServerProtocol + ':' + iceServerName + ':' + udpPort + '?transport=udp');
  115. });
  116.  
  117. iceServerPorts.both.forEach(function (bothPort) {
  118. iceServers[1].urls.push(iceServerProtocol + ':' + iceServerName + ':' + bothPort);
  119. });
  120.  
  121. if (!self._initOptions.usePublicSTUN) {
  122. iceServers.splice(0, 1);
  123. }
  124. }
  125. }
  126.  
  127. log.log('Output iceServers configuration:', iceServers);
  128.  
  129. return {
  130. iceServers: iceServers
  131. };
  132. };