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

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