Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

strophe.jingle.js 17KB

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