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

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