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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. 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. if (RTCBrowserType.isChrome() || RTCBrowserType.isOpera()
  44. || RTCBrowserType.isTemasysPluginUsed()) {
  45. this.connection.disco.addFeature('urn:ietf:rfc:4588');
  46. }
  47. // this is dealt with by SDP O/A so we don't need to announce this
  48. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtcp-fb:0'); // XEP-0293
  49. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'); // XEP-0294
  50. this.connection.disco.addFeature('urn:ietf:rfc:5761'); // rtcp-mux
  51. this.connection.disco.addFeature('urn:ietf:rfc:5888'); // a=group, e.g. bundle
  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. // local jid is not checked
  77. if (fromJid != 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. var audioMuted = startMuted.attr("audio");
  104. var videoMuted = startMuted.attr("video");
  105. eventEmitter.emit(XMPPEvents.START_MUTED_FROM_FOCUS,
  106. audioMuted === "true", videoMuted === "true");
  107. }
  108. sess = new JingleSession(
  109. $(iq).attr('to'), $(iq).find('jingle').attr('sid'),
  110. this.connection, XMPP, eventEmitter);
  111. // configure session
  112. sess.media_constraints = this.media_constraints;
  113. sess.pc_constraints = this.pc_constraints;
  114. sess.ice_config = this.ice_config;
  115. sess.initiate(fromJid, false);
  116. // FIXME: setRemoteDescription should only be done when this call is to be accepted
  117. sess.setRemoteDescription($(iq).find('>jingle'), 'offer');
  118. this.sessions[sess.sid] = sess;
  119. this.jid2session[sess.peerjid] = sess;
  120. // the callback should either
  121. // .sendAnswer and .accept
  122. // or .sendTerminate -- not necessarily synchronus
  123. CallIncomingJingle(sess.sid, this.connection);
  124. break;
  125. case 'session-accept':
  126. sess.setRemoteDescription($(iq).find('>jingle'), 'answer');
  127. sess.accept();
  128. $(document).trigger('callaccepted.jingle', [sess.sid]);
  129. break;
  130. case 'session-terminate':
  131. // If this is not the focus sending the terminate, we have
  132. // nothing more to do here.
  133. if (Object.keys(this.sessions).length < 1
  134. || !(this.sessions[Object.keys(this.sessions)[0]]
  135. instanceof JingleSession))
  136. {
  137. break;
  138. }
  139. console.log('terminating...', sess.sid);
  140. sess.terminate();
  141. this.terminate(sess.sid);
  142. if ($(iq).find('>jingle>reason').length) {
  143. $(document).trigger('callterminated.jingle', [
  144. sess.sid,
  145. sess.peerjid,
  146. $(iq).find('>jingle>reason>:first')[0].tagName,
  147. $(iq).find('>jingle>reason>text').text()
  148. ]);
  149. } else {
  150. $(document).trigger('callterminated.jingle',
  151. [sess.sid, sess.peerjid]);
  152. }
  153. break;
  154. case 'transport-info':
  155. sess.addIceCandidate($(iq).find('>jingle>content'));
  156. break;
  157. case 'session-info':
  158. var affected;
  159. if ($(iq).find('>jingle>ringing[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  160. $(document).trigger('ringing.jingle', [sess.sid]);
  161. } else if ($(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  162. affected = $(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  163. $(document).trigger('mute.jingle', [sess.sid, affected]);
  164. } else if ($(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  165. affected = $(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  166. $(document).trigger('unmute.jingle', [sess.sid, affected]);
  167. }
  168. break;
  169. case 'addsource': // FIXME: proprietary, un-jingleish
  170. case 'source-add': // FIXME: proprietary
  171. sess.addSource($(iq).find('>jingle>content'), fromJid);
  172. break;
  173. case 'removesource': // FIXME: proprietary, un-jingleish
  174. case 'source-remove': // FIXME: proprietary
  175. sess.removeSource($(iq).find('>jingle>content'), fromJid);
  176. break;
  177. default:
  178. console.warn('jingle action not implemented', action);
  179. break;
  180. }
  181. return true;
  182. },
  183. initiate: function (peerjid, myjid) { // initiate a new jinglesession to peerjid
  184. var sess = new JingleSession(myjid || this.connection.jid,
  185. Math.random().toString(36).substr(2, 12), // random string
  186. this.connection, XMPP, eventEmitter);
  187. // configure session
  188. sess.media_constraints = this.media_constraints;
  189. sess.pc_constraints = this.pc_constraints;
  190. sess.ice_config = this.ice_config;
  191. sess.initiate(peerjid, true);
  192. this.sessions[sess.sid] = sess;
  193. this.jid2session[sess.peerjid] = sess;
  194. sess.sendOffer();
  195. return sess;
  196. },
  197. terminate: function (sid, reason, text) { // terminate by sessionid (or all sessions)
  198. if (sid === null || sid === undefined) {
  199. for (sid in this.sessions) {
  200. if (this.sessions[sid].state != 'ended') {
  201. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  202. this.sessions[sid].terminate();
  203. }
  204. delete this.jid2session[this.sessions[sid].peerjid];
  205. delete this.sessions[sid];
  206. }
  207. } else if (this.sessions.hasOwnProperty(sid)) {
  208. if (this.sessions[sid].state != 'ended') {
  209. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  210. this.sessions[sid].terminate();
  211. }
  212. delete this.jid2session[this.sessions[sid].peerjid];
  213. delete this.sessions[sid];
  214. }
  215. },
  216. // Used to terminate a session when an unavailable presence is received.
  217. terminateByJid: function (jid) {
  218. if (this.jid2session.hasOwnProperty(jid)) {
  219. var sess = this.jid2session[jid];
  220. if (sess) {
  221. sess.terminate();
  222. console.log('peer went away silently', jid);
  223. delete this.sessions[sess.sid];
  224. delete this.jid2session[jid];
  225. $(document).trigger('callterminated.jingle',
  226. [sess.sid, jid], 'gone');
  227. }
  228. }
  229. },
  230. terminateRemoteByJid: function (jid, reason) {
  231. if (this.jid2session.hasOwnProperty(jid)) {
  232. var sess = this.jid2session[jid];
  233. if (sess) {
  234. sess.sendTerminate(reason || (!sess.active()) ? 'kick' : null);
  235. sess.terminate();
  236. console.log('terminate peer with jid', sess.sid, jid);
  237. delete this.sessions[sess.sid];
  238. delete this.jid2session[jid];
  239. $(document).trigger('callterminated.jingle',
  240. [sess.sid, jid, 'kicked']);
  241. }
  242. }
  243. },
  244. getStunAndTurnCredentials: function () {
  245. // get stun and turn configuration from server via xep-0215
  246. // uses time-limited credentials as described in
  247. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  248. //
  249. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  250. // for a prosody module which implements this
  251. //
  252. // currently, this doesn't work with updateIce and therefore credentials with a long
  253. // validity have to be fetched before creating the peerconnection
  254. // TODO: implement refresh via updateIce as described in
  255. // https://code.google.com/p/webrtc/issues/detail?id=1650
  256. var self = this;
  257. this.connection.sendIQ(
  258. $iq({type: 'get', to: this.connection.domain})
  259. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  260. function (res) {
  261. var iceservers = [];
  262. $(res).find('>services>service').each(function (idx, el) {
  263. el = $(el);
  264. var dict = {};
  265. var type = el.attr('type');
  266. switch (type) {
  267. case 'stun':
  268. dict.url = 'stun:' + el.attr('host');
  269. if (el.attr('port')) {
  270. dict.url += ':' + el.attr('port');
  271. }
  272. iceservers.push(dict);
  273. break;
  274. case 'turn':
  275. case 'turns':
  276. dict.url = type + ':';
  277. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  278. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  279. dict.url += el.attr('username') + '@';
  280. } else {
  281. dict.username = el.attr('username'); // only works in M28
  282. }
  283. }
  284. dict.url += el.attr('host');
  285. if (el.attr('port') && el.attr('port') != '3478') {
  286. dict.url += ':' + el.attr('port');
  287. }
  288. if (el.attr('transport') && el.attr('transport') != 'udp') {
  289. dict.url += '?transport=' + el.attr('transport');
  290. }
  291. if (el.attr('password')) {
  292. dict.credential = el.attr('password');
  293. }
  294. iceservers.push(dict);
  295. break;
  296. }
  297. });
  298. self.ice_config.iceServers = iceservers;
  299. },
  300. function (err) {
  301. console.warn('getting turn credentials failed', err);
  302. console.warn('is mod_turncredentials or similar installed?');
  303. }
  304. );
  305. // implement push?
  306. },
  307. /**
  308. * Returns the data saved in 'updateLog' in a format to be logged.
  309. */
  310. getLog: function () {
  311. var data = {};
  312. var self = this;
  313. Object.keys(this.sessions).forEach(function (sid) {
  314. var session = self.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. };