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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  87. switch (action) {
  88. case 'session-initiate':
  89. var now = window.performance.now();
  90. logger.log("(TIME) received session-initiate:\t", 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. fromJid,
  101. this.connection,
  102. this.media_constraints,
  103. this.ice_config, XMPP);
  104. this.sessions[sess.sid] = sess;
  105. this.jid2session[sess.peerjid] = sess;
  106. var jingleOffer = $(iq).find('>jingle');
  107. // FIXME there's no nice way with event to get the reason
  108. // why the call was rejected
  109. eventEmitter.emit(XMPPEvents.CALL_INCOMING, sess, jingleOffer, now);
  110. if (!sess.active())
  111. {
  112. // Call not accepted
  113. ack.attrs({ type: 'error' });
  114. ack.c('error', {type: 'cancel'})
  115. .c('bad-request',
  116. { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' })
  117. .up();
  118. this.terminate(sess.sid);
  119. }
  120. break;
  121. case 'session-terminate':
  122. logger.log('terminating...', sess.sid);
  123. var reasonCondition = null;
  124. var reasonText = null;
  125. if ($(iq).find('>jingle>reason').length) {
  126. reasonCondition
  127. = $(iq).find('>jingle>reason>:first')[0].tagName;
  128. reasonText = $(iq).find('>jingle>reason>text').text();
  129. }
  130. this.terminate(sess.sid, reasonCondition, reasonText);
  131. break;
  132. case 'transport-replace':
  133. logger.info("(TIME) Start transport replace",
  134. window.performance.now());
  135. sess.replaceTransport($(iq).find('>jingle'),
  136. function () {
  137. logger.info(
  138. "(TIME) Transport replace success!",
  139. window.performance.now());
  140. },
  141. function(error) {
  142. logger.error('Transport replace failed', error);
  143. sess.sendTransportReject();
  144. });
  145. break;
  146. case 'addsource': // FIXME: proprietary, un-jingleish
  147. case 'source-add': // FIXME: proprietary
  148. sess.addSource($(iq).find('>jingle>content'));
  149. break;
  150. case 'removesource': // FIXME: proprietary, un-jingleish
  151. case 'source-remove': // FIXME: proprietary
  152. sess.removeSource($(iq).find('>jingle>content'));
  153. break;
  154. default:
  155. logger.warn('jingle action not implemented', action);
  156. ack.attrs({ type: 'error' });
  157. ack.c('error', {type: 'cancel'})
  158. .c('bad-request',
  159. { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' })
  160. .up();
  161. break;
  162. }
  163. this.connection.send(ack);
  164. return true;
  165. },
  166. terminate: function (sid, reasonCondition, reasonText) {
  167. if (this.sessions.hasOwnProperty(sid)) {
  168. if (this.sessions[sid].state != 'ended') {
  169. this.sessions[sid].onTerminated(reasonCondition, reasonText);
  170. }
  171. delete this.jid2session[this.sessions[sid].peerjid];
  172. delete this.sessions[sid];
  173. }
  174. },
  175. getStunAndTurnCredentials: function () {
  176. // get stun and turn configuration from server via xep-0215
  177. // uses time-limited credentials as described in
  178. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  179. //
  180. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  181. // for a prosody module which implements this
  182. //
  183. // currently, this doesn't work with updateIce and therefore credentials with a long
  184. // validity have to be fetched before creating the peerconnection
  185. // TODO: implement refresh via updateIce as described in
  186. // https://code.google.com/p/webrtc/issues/detail?id=1650
  187. var self = this;
  188. this.connection.sendIQ(
  189. $iq({type: 'get', to: this.connection.domain})
  190. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  191. function (res) {
  192. var iceservers = [];
  193. $(res).find('>services>service').each(function (idx, el) {
  194. el = $(el);
  195. var dict = {};
  196. var type = el.attr('type');
  197. switch (type) {
  198. case 'stun':
  199. dict.url = 'stun:' + el.attr('host');
  200. if (el.attr('port')) {
  201. dict.url += ':' + el.attr('port');
  202. }
  203. iceservers.push(dict);
  204. break;
  205. case 'turn':
  206. case 'turns':
  207. dict.url = type + ':';
  208. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  209. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  210. dict.url += el.attr('username') + '@';
  211. } else {
  212. dict.username = el.attr('username'); // only works in M28
  213. }
  214. }
  215. dict.url += el.attr('host');
  216. if (el.attr('port') && el.attr('port') != '3478') {
  217. dict.url += ':' + el.attr('port');
  218. }
  219. if (el.attr('transport') && el.attr('transport') != 'udp') {
  220. dict.url += '?transport=' + el.attr('transport');
  221. }
  222. if (el.attr('password')) {
  223. dict.credential = el.attr('password');
  224. }
  225. iceservers.push(dict);
  226. break;
  227. }
  228. });
  229. self.ice_config.iceServers = iceservers;
  230. },
  231. function (err) {
  232. logger.warn('getting turn credentials failed', err);
  233. logger.warn('is mod_turncredentials or similar installed?');
  234. }
  235. );
  236. // implement push?
  237. },
  238. /**
  239. * Returns the data saved in 'updateLog' in a format to be logged.
  240. */
  241. getLog: function () {
  242. var data = {};
  243. var self = this;
  244. Object.keys(this.sessions).forEach(function (sid) {
  245. var session = self.sessions[sid];
  246. if (session.peerconnection && session.peerconnection.updateLog) {
  247. // FIXME: should probably be a .dump call
  248. data["jingle_" + session.sid] = {
  249. updateLog: session.peerconnection.updateLog,
  250. stats: session.peerconnection.stats,
  251. url: window.location.href
  252. };
  253. }
  254. });
  255. return data;
  256. }
  257. });
  258. };