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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* jshint -W117 */
  2. var JingleSession = require("./JingleSessionPC");
  3. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  4. var RTCBrowserType = require("../RTC/RTCBrowserType");
  5. module.exports = function(XMPP, eventEmitter) {
  6. Strophe.addConnectionPlugin('jingle', {
  7. connection: null,
  8. sessions: {},
  9. jid2session: {},
  10. ice_config: {iceServers: []},
  11. pc_constraints: {},
  12. activecall: null,
  13. media_constraints: {
  14. mandatory: {
  15. 'OfferToReceiveAudio': true,
  16. 'OfferToReceiveVideo': true
  17. }
  18. // MozDontOfferDataChannel: true when this is firefox
  19. },
  20. init: function (conn) {
  21. this.connection = conn;
  22. if (this.connection.disco) {
  23. // http://xmpp.org/extensions/xep-0167.html#support
  24. // http://xmpp.org/extensions/xep-0176.html#support
  25. this.connection.disco.addFeature('urn:xmpp:jingle:1');
  26. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:1');
  27. this.connection.disco.addFeature('urn:xmpp:jingle:transports:ice-udp:1');
  28. this.connection.disco.addFeature('urn:xmpp:jingle:apps:dtls:0');
  29. this.connection.disco.addFeature('urn:xmpp:jingle:transports:dtls-sctp:1');
  30. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:audio');
  31. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:video');
  32. if (RTCBrowserType.isChrome() || RTCBrowserType.isOpera() ||
  33. RTCBrowserType.isTemasysPluginUsed()) {
  34. this.connection.disco.addFeature('urn:ietf:rfc:4588');
  35. }
  36. // this is dealt with by SDP O/A so we don't need to announce this
  37. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtcp-fb:0'); // XEP-0293
  38. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'); // XEP-0294
  39. this.connection.disco.addFeature('urn:ietf:rfc:5761'); // rtcp-mux
  40. this.connection.disco.addFeature('urn:ietf:rfc:5888'); // a=group, e.g. bundle
  41. //this.connection.disco.addFeature('urn:ietf:rfc:5576'); // a=ssrc
  42. }
  43. this.connection.addHandler(this.onJingle.bind(this), 'urn:xmpp:jingle:1', 'iq', 'set', null, null);
  44. },
  45. onJingle: function (iq) {
  46. var sid = $(iq).find('jingle').attr('sid');
  47. var action = $(iq).find('jingle').attr('action');
  48. var fromJid = iq.getAttribute('from');
  49. // send ack first
  50. var ack = $iq({type: 'result',
  51. to: fromJid,
  52. id: iq.getAttribute('id')
  53. });
  54. console.log('on jingle ' + action + ' from ' + fromJid, iq);
  55. var sess = this.sessions[sid];
  56. if ('session-initiate' != action) {
  57. if (sess === null) {
  58. ack.type = 'error';
  59. ack.c('error', {type: 'cancel'})
  60. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  61. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  62. this.connection.send(ack);
  63. return true;
  64. }
  65. // local jid is not checked
  66. if (fromJid != sess.peerjid) {
  67. console.warn('jid mismatch for session id', sid, fromJid, sess.peerjid);
  68. ack.type = 'error';
  69. ack.c('error', {type: 'cancel'})
  70. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  71. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  72. this.connection.send(ack);
  73. return true;
  74. }
  75. } else if (sess !== undefined) {
  76. // existing session with same session id
  77. // this might be out-of-order if the sess.peerjid is the same as from
  78. ack.type = 'error';
  79. ack.c('error', {type: 'cancel'})
  80. .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
  81. console.warn('duplicate session id', sid);
  82. this.connection.send(ack);
  83. return true;
  84. }
  85. // FIXME: check for a defined action
  86. this.connection.send(ack);
  87. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  88. switch (action) {
  89. case 'session-initiate':
  90. var startMuted = $(iq).find('jingle>startmuted');
  91. if (startMuted && startMuted.length > 0) {
  92. var audioMuted = startMuted.attr("audio");
  93. var videoMuted = startMuted.attr("video");
  94. eventEmitter.emit(XMPPEvents.START_MUTED_FROM_FOCUS,
  95. audioMuted === "true", videoMuted === "true");
  96. }
  97. sess = new JingleSession(
  98. $(iq).attr('to'), $(iq).find('jingle').attr('sid'),
  99. this.connection, XMPP, eventEmitter);
  100. // configure session
  101. sess.media_constraints = this.media_constraints;
  102. sess.pc_constraints = this.pc_constraints;
  103. sess.ice_config = this.ice_config;
  104. sess.initialize(fromJid, false);
  105. // FIXME: setRemoteDescription should only be done when this call is to be accepted
  106. sess.setOffer($(iq).find('>jingle'));
  107. this.sessions[sess.sid] = sess;
  108. this.jid2session[sess.peerjid] = sess;
  109. // the callback should either
  110. // .sendAnswer and .accept
  111. // or .sendTerminate -- not necessarily synchronous
  112. // TODO: do we check activecall == null?
  113. this.connection.jingle.activecall = sess;
  114. eventEmitter.emit(XMPPEvents.CALL_INCOMING, sess);
  115. // TODO: check affiliation and/or role
  116. console.log('emuc data for', sess.peerjid,
  117. this.connection.emuc.members[sess.peerjid]);
  118. sess.sendAnswer();
  119. sess.accept();
  120. break;
  121. case 'session-accept':
  122. sess.setAnswer($(iq).find('>jingle'));
  123. sess.accept();
  124. $(document).trigger('callaccepted.jingle', [sess.sid]);
  125. break;
  126. case 'session-terminate':
  127. if (!sess) {
  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, eventEmitter);
  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.initialize(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. * Returns the data saved in 'updateLog' in a format to be logged.
  300. */
  301. getLog: function () {
  302. var data = {};
  303. var self = this;
  304. Object.keys(this.sessions).forEach(function (sid) {
  305. var session = self.sessions[sid];
  306. if (session.peerconnection && session.peerconnection.updateLog) {
  307. // FIXME: should probably be a .dump call
  308. data["jingle_" + session.sid] = {
  309. updateLog: session.peerconnection.updateLog,
  310. stats: session.peerconnection.stats,
  311. url: window.location.href
  312. };
  313. }
  314. });
  315. return data;
  316. }
  317. });
  318. };