Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

strophe.jingle.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* jshint -W117 */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var JingleSession = require("./JingleSessionPC");
  4. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  5. var RTCBrowserType = require("../RTC/RTCBrowserType");
  6. module.exports = function(XMPP, eventEmitter) {
  7. Strophe.addConnectionPlugin('jingle', {
  8. connection: null,
  9. sessions: {},
  10. jid2session: {},
  11. ice_config: {iceServers: []},
  12. media_constraints: {
  13. mandatory: {
  14. 'OfferToReceiveAudio': true,
  15. 'OfferToReceiveVideo': true
  16. }
  17. // MozDontOfferDataChannel: true when this is firefox
  18. },
  19. init: function (conn) {
  20. this.connection = conn;
  21. if (this.connection.disco) {
  22. // http://xmpp.org/extensions/xep-0167.html#support
  23. // http://xmpp.org/extensions/xep-0176.html#support
  24. this.connection.disco.addFeature('urn:xmpp:jingle:1');
  25. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:1');
  26. this.connection.disco.addFeature('urn:xmpp:jingle:transports:ice-udp:1');
  27. this.connection.disco.addFeature('urn:xmpp:jingle:apps:dtls:0');
  28. this.connection.disco.addFeature('urn:xmpp:jingle:transports:dtls-sctp:1');
  29. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:audio');
  30. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:video');
  31. if (RTCBrowserType.isChrome() || RTCBrowserType.isOpera()
  32. || RTCBrowserType.isTemasysPluginUsed()) {
  33. this.connection.disco.addFeature('urn:ietf:rfc:4588');
  34. }
  35. // this is dealt with by SDP O/A so we don't need to announce this
  36. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtcp-fb:0'); // XEP-0293
  37. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'); // XEP-0294
  38. this.connection.disco.addFeature('urn:ietf:rfc:5761'); // rtcp-mux
  39. this.connection.disco.addFeature('urn:ietf:rfc:5888'); // a=group, e.g. bundle
  40. //this.connection.disco.addFeature('urn:ietf:rfc:5576'); // a=ssrc
  41. }
  42. this.connection.addHandler(this.onJingle.bind(this), 'urn:xmpp:jingle:1', 'iq', 'set', null, null);
  43. },
  44. onJingle: function (iq) {
  45. var sid = $(iq).find('jingle').attr('sid');
  46. var action = $(iq).find('jingle').attr('action');
  47. var fromJid = iq.getAttribute('from');
  48. // send ack first
  49. var ack = $iq({type: 'result',
  50. to: fromJid,
  51. id: iq.getAttribute('id')
  52. });
  53. logger.log('on jingle ' + action + ' from ' + fromJid, iq);
  54. var sess = this.sessions[sid];
  55. if ('session-initiate' != action) {
  56. if (!sess) {
  57. ack.attrs({ type: 'error' });
  58. ack.c('error', {type: 'cancel'})
  59. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  60. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  61. logger.warn('invalid session id', iq);
  62. this.connection.send(ack);
  63. return true;
  64. }
  65. // local jid is not checked
  66. if (fromJid != sess.peerjid) {
  67. logger.warn(
  68. 'jid mismatch for session id', sid, sess.peerjid, iq);
  69. ack.attrs({ type: 'error' });
  70. ack.c('error', {type: 'cancel'})
  71. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  72. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  73. this.connection.send(ack);
  74. return true;
  75. }
  76. } else if (sess !== undefined) {
  77. // existing session with same session id
  78. // this might be out-of-order if the sess.peerjid is the same as from
  79. ack.attrs({ type: 'error' });
  80. ack.c('error', {type: 'cancel'})
  81. .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
  82. logger.warn('duplicate session id', sid, iq);
  83. this.connection.send(ack);
  84. return true;
  85. }
  86. // FIXME: check for a defined action
  87. this.connection.send(ack);
  88. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  89. switch (action) {
  90. case 'session-initiate':
  91. console.log("(TIME) received session-initiate:\t",
  92. window.performance.now());
  93. var startMuted = $(iq).find('jingle>startmuted');
  94. if (startMuted && startMuted.length > 0) {
  95. var audioMuted = startMuted.attr("audio");
  96. var videoMuted = startMuted.attr("video");
  97. eventEmitter.emit(XMPPEvents.START_MUTED_FROM_FOCUS,
  98. audioMuted === "true", videoMuted === "true");
  99. }
  100. sess = new JingleSession(
  101. $(iq).attr('to'), $(iq).find('jingle').attr('sid'),
  102. fromJid,
  103. this.connection,
  104. this.media_constraints,
  105. this.ice_config, XMPP);
  106. this.sessions[sess.sid] = sess;
  107. this.jid2session[sess.peerjid] = sess;
  108. var jingleOffer = $(iq).find('>jingle');
  109. eventEmitter.emit(XMPPEvents.CALL_INCOMING, sess, jingleOffer);
  110. break;
  111. case 'session-terminate':
  112. logger.log('terminating...', sess.sid);
  113. var reasonCondition = null;
  114. var reasonText = null;
  115. if ($(iq).find('>jingle>reason').length) {
  116. reasonCondition
  117. = $(iq).find('>jingle>reason>:first')[0].tagName;
  118. reasonText = $(iq).find('>jingle>reason>text').text();
  119. }
  120. this.terminate(sess.sid, reasonCondition, reasonText);
  121. break;
  122. case 'addsource': // FIXME: proprietary, un-jingleish
  123. case 'source-add': // FIXME: proprietary
  124. sess.addSource($(iq).find('>jingle>content'));
  125. break;
  126. case 'removesource': // FIXME: proprietary, un-jingleish
  127. case 'source-remove': // FIXME: proprietary
  128. sess.removeSource($(iq).find('>jingle>content'));
  129. break;
  130. default:
  131. logger.warn('jingle action not implemented', action);
  132. break;
  133. }
  134. return true;
  135. },
  136. terminate: function (sid, reasonCondition, reasonText) {
  137. if (this.sessions.hasOwnProperty(sid)) {
  138. if (this.sessions[sid].state != 'ended') {
  139. this.sessions[sid].onTerminated(reasonCondition, reasonText);
  140. }
  141. delete this.jid2session[this.sessions[sid].peerjid];
  142. delete this.sessions[sid];
  143. }
  144. },
  145. getStunAndTurnCredentials: function () {
  146. // get stun and turn configuration from server via xep-0215
  147. // uses time-limited credentials as described in
  148. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  149. //
  150. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  151. // for a prosody module which implements this
  152. //
  153. // currently, this doesn't work with updateIce and therefore credentials with a long
  154. // validity have to be fetched before creating the peerconnection
  155. // TODO: implement refresh via updateIce as described in
  156. // https://code.google.com/p/webrtc/issues/detail?id=1650
  157. var self = this;
  158. this.connection.sendIQ(
  159. $iq({type: 'get', to: this.connection.domain})
  160. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  161. function (res) {
  162. var iceservers = [];
  163. $(res).find('>services>service').each(function (idx, el) {
  164. el = $(el);
  165. var dict = {};
  166. var type = el.attr('type');
  167. switch (type) {
  168. case 'stun':
  169. dict.url = 'stun:' + el.attr('host');
  170. if (el.attr('port')) {
  171. dict.url += ':' + el.attr('port');
  172. }
  173. iceservers.push(dict);
  174. break;
  175. case 'turn':
  176. case 'turns':
  177. dict.url = type + ':';
  178. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  179. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  180. dict.url += el.attr('username') + '@';
  181. } else {
  182. dict.username = el.attr('username'); // only works in M28
  183. }
  184. }
  185. dict.url += el.attr('host');
  186. if (el.attr('port') && el.attr('port') != '3478') {
  187. dict.url += ':' + el.attr('port');
  188. }
  189. if (el.attr('transport') && el.attr('transport') != 'udp') {
  190. dict.url += '?transport=' + el.attr('transport');
  191. }
  192. if (el.attr('password')) {
  193. dict.credential = el.attr('password');
  194. }
  195. iceservers.push(dict);
  196. break;
  197. }
  198. });
  199. self.ice_config.iceServers = iceservers;
  200. },
  201. function (err) {
  202. logger.warn('getting turn credentials failed', err);
  203. logger.warn('is mod_turncredentials or similar installed?');
  204. }
  205. );
  206. // implement push?
  207. },
  208. /**
  209. * Returns the data saved in 'updateLog' in a format to be logged.
  210. */
  211. getLog: function () {
  212. var data = {};
  213. var self = this;
  214. Object.keys(this.sessions).forEach(function (sid) {
  215. var session = self.sessions[sid];
  216. if (session.peerconnection && session.peerconnection.updateLog) {
  217. // FIXME: should probably be a .dump call
  218. data["jingle_" + session.sid] = {
  219. updateLog: session.peerconnection.updateLog,
  220. stats: session.peerconnection.stats,
  221. url: window.location.href
  222. };
  223. }
  224. });
  225. return data;
  226. }
  227. });
  228. };