You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strophe.jingle.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 === null) {
  57. ack.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. this.connection.send(ack);
  62. return true;
  63. }
  64. // local jid is not checked
  65. if (fromJid != sess.peerjid) {
  66. logger.warn('jid mismatch for session id', sid, fromJid, sess.peerjid);
  67. ack.type = 'error';
  68. ack.c('error', {type: 'cancel'})
  69. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  70. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  71. this.connection.send(ack);
  72. return true;
  73. }
  74. } else if (sess !== undefined) {
  75. // existing session with same session id
  76. // this might be out-of-order if the sess.peerjid is the same as from
  77. ack.type = 'error';
  78. ack.c('error', {type: 'cancel'})
  79. .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
  80. logger.warn('duplicate session id', sid);
  81. this.connection.send(ack);
  82. return true;
  83. }
  84. // FIXME: check for a defined action
  85. this.connection.send(ack);
  86. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  87. switch (action) {
  88. case 'session-initiate':
  89. console.log("(TIME) received session-initiate:\t",
  90. window.performance.now());
  91. var startMuted = $(iq).find('jingle>startmuted');
  92. if (startMuted && startMuted.length > 0) {
  93. var audioMuted = startMuted.attr("audio");
  94. var videoMuted = startMuted.attr("video");
  95. eventEmitter.emit(XMPPEvents.START_MUTED_FROM_FOCUS,
  96. audioMuted === "true", videoMuted === "true");
  97. }
  98. sess = new JingleSession(
  99. $(iq).attr('to'), $(iq).find('jingle').attr('sid'),
  100. this.connection, XMPP);
  101. // configure session
  102. var fromBareJid = Strophe.getBareJidFromJid(fromJid);
  103. this.connection.emuc.setJingleSession(fromBareJid, sess);
  104. sess.media_constraints = this.media_constraints;
  105. sess.ice_config = this.ice_config;
  106. sess.initialize(fromJid, false);
  107. eventEmitter.emit(XMPPEvents.CALL_INCOMING, sess);
  108. // FIXME: setRemoteDescription should only be done when this call is to be accepted
  109. sess.setOffer($(iq).find('>jingle'));
  110. this.sessions[sess.sid] = sess;
  111. this.jid2session[sess.peerjid] = sess;
  112. // the callback should either
  113. // .sendAnswer and .accept
  114. // or .sendTerminate -- not necessarily synchronous
  115. sess.sendAnswer();
  116. sess.accept();
  117. break;
  118. case 'session-accept':
  119. sess.setAnswer($(iq).find('>jingle'));
  120. sess.accept();
  121. break;
  122. case 'session-terminate':
  123. // If this is not the focus sending the terminate, we have
  124. // nothing more to do here.
  125. if (Object.keys(this.sessions).length < 1
  126. || !(this.sessions[Object.keys(this.sessions)[0]]
  127. instanceof JingleSession))
  128. {
  129. break;
  130. }
  131. logger.log('terminating...', sess.sid);
  132. sess.terminate();
  133. this.terminate(sess.sid);
  134. if ($(iq).find('>jingle>reason').length) {
  135. $(document).trigger('callterminated.jingle', [
  136. sess.sid,
  137. sess.peerjid,
  138. $(iq).find('>jingle>reason>:first')[0].tagName,
  139. $(iq).find('>jingle>reason>text').text()
  140. ]);
  141. } else {
  142. $(document).trigger('callterminated.jingle',
  143. [sess.sid, sess.peerjid]);
  144. }
  145. break;
  146. case 'transport-info':
  147. sess.addIceCandidate($(iq).find('>jingle>content'));
  148. break;
  149. case 'session-info':
  150. var affected;
  151. if ($(iq).find('>jingle>ringing[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  152. $(document).trigger('ringing.jingle', [sess.sid]);
  153. } else if ($(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  154. affected = $(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  155. $(document).trigger('mute.jingle', [sess.sid, affected]);
  156. } else if ($(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  157. affected = $(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  158. $(document).trigger('unmute.jingle', [sess.sid, affected]);
  159. }
  160. break;
  161. case 'addsource': // FIXME: proprietary, un-jingleish
  162. case 'source-add': // FIXME: proprietary
  163. sess.addSource($(iq).find('>jingle>content'));
  164. break;
  165. case 'removesource': // FIXME: proprietary, un-jingleish
  166. case 'source-remove': // FIXME: proprietary
  167. sess.removeSource($(iq).find('>jingle>content'));
  168. break;
  169. default:
  170. logger.warn('jingle action not implemented', action);
  171. break;
  172. }
  173. return true;
  174. },
  175. terminate: function (sid, reason, text) { // terminate by sessionid (or all sessions)
  176. if (sid === null || sid === undefined) {
  177. for (sid in this.sessions) {
  178. if (this.sessions[sid].state != 'ended') {
  179. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  180. this.sessions[sid].terminate();
  181. }
  182. delete this.jid2session[this.sessions[sid].peerjid];
  183. delete this.sessions[sid];
  184. }
  185. } else if (this.sessions.hasOwnProperty(sid)) {
  186. if (this.sessions[sid].state != 'ended') {
  187. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  188. this.sessions[sid].terminate();
  189. }
  190. delete this.jid2session[this.sessions[sid].peerjid];
  191. delete this.sessions[sid];
  192. }
  193. },
  194. getStunAndTurnCredentials: function () {
  195. // get stun and turn configuration from server via xep-0215
  196. // uses time-limited credentials as described in
  197. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  198. //
  199. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  200. // for a prosody module which implements this
  201. //
  202. // currently, this doesn't work with updateIce and therefore credentials with a long
  203. // validity have to be fetched before creating the peerconnection
  204. // TODO: implement refresh via updateIce as described in
  205. // https://code.google.com/p/webrtc/issues/detail?id=1650
  206. var self = this;
  207. this.connection.sendIQ(
  208. $iq({type: 'get', to: this.connection.domain})
  209. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  210. function (res) {
  211. var iceservers = [];
  212. $(res).find('>services>service').each(function (idx, el) {
  213. el = $(el);
  214. var dict = {};
  215. var type = el.attr('type');
  216. switch (type) {
  217. case 'stun':
  218. dict.url = 'stun:' + el.attr('host');
  219. if (el.attr('port')) {
  220. dict.url += ':' + el.attr('port');
  221. }
  222. iceservers.push(dict);
  223. break;
  224. case 'turn':
  225. case 'turns':
  226. dict.url = type + ':';
  227. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  228. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  229. dict.url += el.attr('username') + '@';
  230. } else {
  231. dict.username = el.attr('username'); // only works in M28
  232. }
  233. }
  234. dict.url += el.attr('host');
  235. if (el.attr('port') && el.attr('port') != '3478') {
  236. dict.url += ':' + el.attr('port');
  237. }
  238. if (el.attr('transport') && el.attr('transport') != 'udp') {
  239. dict.url += '?transport=' + el.attr('transport');
  240. }
  241. if (el.attr('password')) {
  242. dict.credential = el.attr('password');
  243. }
  244. iceservers.push(dict);
  245. break;
  246. }
  247. });
  248. self.ice_config.iceServers = iceservers;
  249. },
  250. function (err) {
  251. logger.warn('getting turn credentials failed', err);
  252. logger.warn('is mod_turncredentials or similar installed?');
  253. }
  254. );
  255. // implement push?
  256. },
  257. /**
  258. * Returns the data saved in 'updateLog' in a format to be logged.
  259. */
  260. getLog: function () {
  261. var data = {};
  262. var self = this;
  263. Object.keys(this.sessions).forEach(function (sid) {
  264. var session = self.sessions[sid];
  265. if (session.peerconnection && session.peerconnection.updateLog) {
  266. // FIXME: should probably be a .dump call
  267. data["jingle_" + session.sid] = {
  268. updateLog: session.peerconnection.updateLog,
  269. stats: session.peerconnection.stats,
  270. url: window.location.href
  271. };
  272. }
  273. });
  274. return data;
  275. }
  276. });
  277. };