Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

strophe.jingle.js 16KB

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