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

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