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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* jshint -W117 */
  2. var JingleSession = require("./JingleSession");
  3. function CallIncomingJingle(sid, connection) {
  4. var sess = connection.jingle.sessions[sid];
  5. // TODO: do we check activecall == null?
  6. activecall = sess;
  7. statistics.onConferenceCreated(sess);
  8. RTC.onConferenceCreated(sess);
  9. // TODO: check affiliation and/or role
  10. console.log('emuc data for', sess.peerjid, connection.emuc.members[sess.peerjid]);
  11. sess.usedrip = true; // not-so-naive trickle ice
  12. sess.sendAnswer();
  13. sess.accept();
  14. };
  15. module.exports = function(XMPP)
  16. {
  17. Strophe.addConnectionPlugin('jingle', {
  18. connection: null,
  19. sessions: {},
  20. jid2session: {},
  21. ice_config: {iceServers: []},
  22. pc_constraints: {},
  23. media_constraints: {
  24. mandatory: {
  25. 'OfferToReceiveAudio': true,
  26. 'OfferToReceiveVideo': true
  27. }
  28. // MozDontOfferDataChannel: true when this is firefox
  29. },
  30. init: function (conn) {
  31. this.connection = conn;
  32. if (this.connection.disco) {
  33. // http://xmpp.org/extensions/xep-0167.html#support
  34. // http://xmpp.org/extensions/xep-0176.html#support
  35. this.connection.disco.addFeature('urn:xmpp:jingle:1');
  36. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:1');
  37. this.connection.disco.addFeature('urn:xmpp:jingle:transports:ice-udp:1');
  38. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:audio');
  39. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:video');
  40. // this is dealt with by SDP O/A so we don't need to annouce this
  41. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtcp-fb:0'); // XEP-0293
  42. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'); // XEP-0294
  43. if (config.useRtcpMux) {
  44. this.connection.disco.addFeature('urn:ietf:rfc:5761'); // rtcp-mux
  45. }
  46. if (config.useBundle) {
  47. this.connection.disco.addFeature('urn:ietf:rfc:5888'); // a=group, e.g. bundle
  48. }
  49. //this.connection.disco.addFeature('urn:ietf:rfc:5576'); // a=ssrc
  50. }
  51. this.connection.addHandler(this.onJingle.bind(this), 'urn:xmpp:jingle:1', 'iq', 'set', null, null);
  52. },
  53. onJingle: function (iq) {
  54. var sid = $(iq).find('jingle').attr('sid');
  55. var action = $(iq).find('jingle').attr('action');
  56. var fromJid = iq.getAttribute('from');
  57. // send ack first
  58. var ack = $iq({type: 'result',
  59. to: fromJid,
  60. id: iq.getAttribute('id')
  61. });
  62. console.log('on jingle ' + action + ' from ' + fromJid, iq);
  63. var sess = this.sessions[sid];
  64. if ('session-initiate' != action) {
  65. if (sess === null) {
  66. ack.type = 'error';
  67. ack.c('error', {type: 'cancel'})
  68. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  69. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  70. this.connection.send(ack);
  71. return true;
  72. }
  73. // compare from to sess.peerjid (bare jid comparison for later compat with message-mode)
  74. // local jid is not checked
  75. if (Strophe.getBareJidFromJid(fromJid) != Strophe.getBareJidFromJid(sess.peerjid)) {
  76. console.warn('jid mismatch for session id', sid, fromJid, sess.peerjid);
  77. ack.type = 'error';
  78. ack.c('error', {type: 'cancel'})
  79. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  80. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  81. this.connection.send(ack);
  82. return true;
  83. }
  84. } else if (sess !== undefined) {
  85. // existing session with same session id
  86. // this might be out-of-order if the sess.peerjid is the same as from
  87. ack.type = 'error';
  88. ack.c('error', {type: 'cancel'})
  89. .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
  90. console.warn('duplicate session id', sid);
  91. this.connection.send(ack);
  92. return true;
  93. }
  94. // FIXME: check for a defined action
  95. this.connection.send(ack);
  96. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  97. switch (action) {
  98. case 'session-initiate':
  99. sess = new JingleSession(
  100. $(iq).attr('to'), $(iq).find('jingle').attr('sid'),
  101. this.connection, XMPP);
  102. // configure session
  103. sess.media_constraints = this.media_constraints;
  104. sess.pc_constraints = this.pc_constraints;
  105. sess.ice_config = this.ice_config;
  106. sess.initiate(fromJid, false);
  107. // FIXME: setRemoteDescription should only be done when this call is to be accepted
  108. sess.setRemoteDescription($(iq).find('>jingle'), 'offer');
  109. this.sessions[sess.sid] = sess;
  110. this.jid2session[sess.peerjid] = sess;
  111. // the callback should either
  112. // .sendAnswer and .accept
  113. // or .sendTerminate -- not necessarily synchronus
  114. CallIncomingJingle(sess.sid, this.connection);
  115. break;
  116. case 'session-accept':
  117. sess.setRemoteDescription($(iq).find('>jingle'), 'answer');
  118. sess.accept();
  119. $(document).trigger('callaccepted.jingle', [sess.sid]);
  120. break;
  121. case 'session-terminate':
  122. // If this is not the focus sending the terminate, we have
  123. // nothing more to do here.
  124. if (Object.keys(this.sessions).length < 1
  125. || !(this.sessions[Object.keys(this.sessions)[0]]
  126. instanceof JingleSession))
  127. {
  128. break;
  129. }
  130. console.log('terminating...', sess.sid);
  131. sess.terminate();
  132. this.terminate(sess.sid);
  133. if ($(iq).find('>jingle>reason').length) {
  134. $(document).trigger('callterminated.jingle', [
  135. sess.sid,
  136. sess.peerjid,
  137. $(iq).find('>jingle>reason>:first')[0].tagName,
  138. $(iq).find('>jingle>reason>text').text()
  139. ]);
  140. } else {
  141. $(document).trigger('callterminated.jingle',
  142. [sess.sid, sess.peerjid]);
  143. }
  144. break;
  145. case 'transport-info':
  146. sess.addIceCandidate($(iq).find('>jingle>content'));
  147. break;
  148. case 'session-info':
  149. var affected;
  150. if ($(iq).find('>jingle>ringing[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  151. $(document).trigger('ringing.jingle', [sess.sid]);
  152. } else if ($(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  153. affected = $(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  154. $(document).trigger('mute.jingle', [sess.sid, affected]);
  155. } else if ($(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  156. affected = $(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  157. $(document).trigger('unmute.jingle', [sess.sid, affected]);
  158. }
  159. break;
  160. case 'addsource': // FIXME: proprietary, un-jingleish
  161. case 'source-add': // FIXME: proprietary
  162. sess.addSource($(iq).find('>jingle>content'), fromJid);
  163. break;
  164. case 'removesource': // FIXME: proprietary, un-jingleish
  165. case 'source-remove': // FIXME: proprietary
  166. sess.removeSource($(iq).find('>jingle>content'), fromJid);
  167. break;
  168. default:
  169. console.warn('jingle action not implemented', action);
  170. break;
  171. }
  172. return true;
  173. },
  174. initiate: function (peerjid, myjid) { // initiate a new jinglesession to peerjid
  175. var sess = new JingleSession(myjid || this.connection.jid,
  176. Math.random().toString(36).substr(2, 12), // random string
  177. this.connection, XMPP);
  178. // configure session
  179. sess.media_constraints = this.media_constraints;
  180. sess.pc_constraints = this.pc_constraints;
  181. sess.ice_config = this.ice_config;
  182. sess.initiate(peerjid, true);
  183. this.sessions[sess.sid] = sess;
  184. this.jid2session[sess.peerjid] = sess;
  185. sess.sendOffer();
  186. return sess;
  187. },
  188. terminate: function (sid, reason, text) { // terminate by sessionid (or all sessions)
  189. if (sid === null || sid === undefined) {
  190. for (sid in this.sessions) {
  191. if (this.sessions[sid].state != 'ended') {
  192. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  193. this.sessions[sid].terminate();
  194. }
  195. delete this.jid2session[this.sessions[sid].peerjid];
  196. delete this.sessions[sid];
  197. }
  198. } else if (this.sessions.hasOwnProperty(sid)) {
  199. if (this.sessions[sid].state != 'ended') {
  200. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  201. this.sessions[sid].terminate();
  202. }
  203. delete this.jid2session[this.sessions[sid].peerjid];
  204. delete this.sessions[sid];
  205. }
  206. },
  207. // Used to terminate a session when an unavailable presence is received.
  208. terminateByJid: function (jid) {
  209. if (this.jid2session.hasOwnProperty(jid)) {
  210. var sess = this.jid2session[jid];
  211. if (sess) {
  212. sess.terminate();
  213. console.log('peer went away silently', jid);
  214. delete this.sessions[sess.sid];
  215. delete this.jid2session[jid];
  216. $(document).trigger('callterminated.jingle',
  217. [sess.sid, jid], 'gone');
  218. }
  219. }
  220. },
  221. terminateRemoteByJid: function (jid, reason) {
  222. if (this.jid2session.hasOwnProperty(jid)) {
  223. var sess = this.jid2session[jid];
  224. if (sess) {
  225. sess.sendTerminate(reason || (!sess.active()) ? 'kick' : null);
  226. sess.terminate();
  227. console.log('terminate peer with jid', sess.sid, jid);
  228. delete this.sessions[sess.sid];
  229. delete this.jid2session[jid];
  230. $(document).trigger('callterminated.jingle',
  231. [sess.sid, jid, 'kicked']);
  232. }
  233. }
  234. },
  235. getStunAndTurnCredentials: function () {
  236. // get stun and turn configuration from server via xep-0215
  237. // uses time-limited credentials as described in
  238. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  239. //
  240. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  241. // for a prosody module which implements this
  242. //
  243. // currently, this doesn't work with updateIce and therefore credentials with a long
  244. // validity have to be fetched before creating the peerconnection
  245. // TODO: implement refresh via updateIce as described in
  246. // https://code.google.com/p/webrtc/issues/detail?id=1650
  247. var self = this;
  248. this.connection.sendIQ(
  249. $iq({type: 'get', to: this.connection.domain})
  250. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  251. function (res) {
  252. var iceservers = [];
  253. $(res).find('>services>service').each(function (idx, el) {
  254. el = $(el);
  255. var dict = {};
  256. var type = el.attr('type');
  257. switch (type) {
  258. case 'stun':
  259. dict.url = 'stun:' + el.attr('host');
  260. if (el.attr('port')) {
  261. dict.url += ':' + el.attr('port');
  262. }
  263. iceservers.push(dict);
  264. break;
  265. case 'turn':
  266. case 'turns':
  267. dict.url = type + ':';
  268. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  269. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  270. dict.url += el.attr('username') + '@';
  271. } else {
  272. dict.username = el.attr('username'); // only works in M28
  273. }
  274. }
  275. dict.url += el.attr('host');
  276. if (el.attr('port') && el.attr('port') != '3478') {
  277. dict.url += ':' + el.attr('port');
  278. }
  279. if (el.attr('transport') && el.attr('transport') != 'udp') {
  280. dict.url += '?transport=' + el.attr('transport');
  281. }
  282. if (el.attr('password')) {
  283. dict.credential = el.attr('password');
  284. }
  285. iceservers.push(dict);
  286. break;
  287. }
  288. });
  289. self.ice_config.iceServers = iceservers;
  290. },
  291. function (err) {
  292. console.warn('getting turn credentials failed', err);
  293. console.warn('is mod_turncredentials or similar installed?');
  294. }
  295. );
  296. // implement push?
  297. },
  298. /**
  299. * Populates the log data
  300. */
  301. populateData: function () {
  302. var data = {};
  303. Object.keys(this.sessions).forEach(function (sid) {
  304. var session = this.sessions[sid];
  305. if (session.peerconnection && session.peerconnection.updateLog) {
  306. // FIXME: should probably be a .dump call
  307. data["jingle_" + session.sid] = {
  308. updateLog: session.peerconnection.updateLog,
  309. stats: session.peerconnection.stats,
  310. url: window.location.href
  311. };
  312. }
  313. });
  314. return data;
  315. }
  316. });
  317. };