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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* jshint -W117 */
  2. var JingleSession = require("./JingleSession");
  3. module.exports = function(XMPP)
  4. {
  5. function CallIncomingJingle(sid, connection) {
  6. var sess = connection.jingle.sessions[sid];
  7. // TODO: do we check activecall == null?
  8. connection.jingle.activecall = sess;
  9. statistics.onConferenceCreated(sess);
  10. RTC.onConferenceCreated(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. sess = new JingleSession(
  102. $(iq).attr('to'), $(iq).find('jingle').attr('sid'),
  103. this.connection, XMPP);
  104. // configure session
  105. sess.media_constraints = this.media_constraints;
  106. sess.pc_constraints = this.pc_constraints;
  107. sess.ice_config = this.ice_config;
  108. sess.initiate(fromJid, false);
  109. // FIXME: setRemoteDescription should only be done when this call is to be accepted
  110. sess.setRemoteDescription($(iq).find('>jingle'), 'offer');
  111. this.sessions[sess.sid] = sess;
  112. this.jid2session[sess.peerjid] = sess;
  113. // the callback should either
  114. // .sendAnswer and .accept
  115. // or .sendTerminate -- not necessarily synchronus
  116. CallIncomingJingle(sess.sid, this.connection);
  117. break;
  118. case 'session-accept':
  119. sess.setRemoteDescription($(iq).find('>jingle'), 'answer');
  120. sess.accept();
  121. $(document).trigger('callaccepted.jingle', [sess.sid]);
  122. break;
  123. case 'session-terminate':
  124. // If this is not the focus sending the terminate, we have
  125. // nothing more to do here.
  126. if (Object.keys(this.sessions).length < 1
  127. || !(this.sessions[Object.keys(this.sessions)[0]]
  128. instanceof JingleSession))
  129. {
  130. break;
  131. }
  132. console.log('terminating...', sess.sid);
  133. sess.terminate();
  134. this.terminate(sess.sid);
  135. if ($(iq).find('>jingle>reason').length) {
  136. $(document).trigger('callterminated.jingle', [
  137. sess.sid,
  138. sess.peerjid,
  139. $(iq).find('>jingle>reason>:first')[0].tagName,
  140. $(iq).find('>jingle>reason>text').text()
  141. ]);
  142. } else {
  143. $(document).trigger('callterminated.jingle',
  144. [sess.sid, sess.peerjid]);
  145. }
  146. break;
  147. case 'transport-info':
  148. sess.addIceCandidate($(iq).find('>jingle>content'));
  149. break;
  150. case 'session-info':
  151. var affected;
  152. if ($(iq).find('>jingle>ringing[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  153. $(document).trigger('ringing.jingle', [sess.sid]);
  154. } else if ($(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  155. affected = $(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  156. $(document).trigger('mute.jingle', [sess.sid, affected]);
  157. } else if ($(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  158. affected = $(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  159. $(document).trigger('unmute.jingle', [sess.sid, affected]);
  160. }
  161. break;
  162. case 'addsource': // FIXME: proprietary, un-jingleish
  163. case 'source-add': // FIXME: proprietary
  164. sess.addSource($(iq).find('>jingle>content'), fromJid);
  165. break;
  166. case 'removesource': // FIXME: proprietary, un-jingleish
  167. case 'source-remove': // FIXME: proprietary
  168. sess.removeSource($(iq).find('>jingle>content'), fromJid);
  169. break;
  170. default:
  171. console.warn('jingle action not implemented', action);
  172. break;
  173. }
  174. return true;
  175. },
  176. initiate: function (peerjid, myjid) { // initiate a new jinglesession to peerjid
  177. var sess = new JingleSession(myjid || this.connection.jid,
  178. Math.random().toString(36).substr(2, 12), // random string
  179. this.connection, XMPP);
  180. // configure session
  181. sess.media_constraints = this.media_constraints;
  182. sess.pc_constraints = this.pc_constraints;
  183. sess.ice_config = this.ice_config;
  184. sess.initiate(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. * Populates the log data
  302. */
  303. populateData: function () {
  304. var data = {};
  305. Object.keys(this.sessions).forEach(function (sid) {
  306. var session = this.sessions[sid];
  307. if (session.peerconnection && session.peerconnection.updateLog) {
  308. // FIXME: should probably be a .dump call
  309. data["jingle_" + session.sid] = {
  310. updateLog: session.peerconnection.updateLog,
  311. stats: session.peerconnection.stats,
  312. url: window.location.href
  313. };
  314. }
  315. });
  316. return data;
  317. }
  318. });
  319. };