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

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