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

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