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

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