Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

strophe.jingle.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* jshint -W117 */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var JingleSession = require("./JingleSessionPC");
  4. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  5. var RTCBrowserType = require("../RTC/RTCBrowserType");
  6. module.exports = function(XMPP, eventEmitter) {
  7. Strophe.addConnectionPlugin('jingle', {
  8. connection: null,
  9. sessions: {},
  10. jid2session: {},
  11. ice_config: {iceServers: []},
  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. logger.log('on jingle ' + action + ' from ' + fromJid, iq);
  54. var sess = this.sessions[sid];
  55. if ('session-initiate' != action) {
  56. if (!sess) {
  57. ack.attrs({ 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. logger.warn('invalid session id', iq);
  62. this.connection.send(ack);
  63. return true;
  64. }
  65. // local jid is not checked
  66. if (fromJid != sess.peerjid) {
  67. logger.warn(
  68. 'jid mismatch for session id', sid, sess.peerjid, iq);
  69. ack.attrs({ 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. } else if (sess !== undefined) {
  77. // existing session with same session id
  78. // this might be out-of-order if the sess.peerjid is the same as from
  79. ack.attrs({ type: 'error' });
  80. ack.c('error', {type: 'cancel'})
  81. .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
  82. logger.warn('duplicate session id', sid, iq);
  83. this.connection.send(ack);
  84. return true;
  85. }
  86. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  87. switch (action) {
  88. case 'session-initiate':
  89. var now = window.performance.now();
  90. console.log("(TIME) received session-initiate:\t", now);
  91. var startMuted = $(iq).find('jingle>startmuted');
  92. if (startMuted && startMuted.length > 0) {
  93. var audioMuted = startMuted.attr("audio");
  94. var videoMuted = startMuted.attr("video");
  95. eventEmitter.emit(XMPPEvents.START_MUTED_FROM_FOCUS,
  96. audioMuted === "true", videoMuted === "true");
  97. }
  98. sess = new JingleSession(
  99. $(iq).attr('to'), $(iq).find('jingle').attr('sid'),
  100. fromJid,
  101. this.connection,
  102. this.media_constraints,
  103. this.ice_config, XMPP);
  104. this.sessions[sess.sid] = sess;
  105. this.jid2session[sess.peerjid] = sess;
  106. var jingleOffer = $(iq).find('>jingle');
  107. // FIXME there's no nice way with event to get the reason
  108. // why the call was rejected
  109. eventEmitter.emit(XMPPEvents.CALL_INCOMING, sess, jingleOffer, now);
  110. if (!sess.active())
  111. {
  112. // Call not accepted
  113. ack.attrs({ type: 'error' });
  114. ack.c('error', {type: 'cancel'})
  115. .c('bad-request',
  116. { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' })
  117. .up();
  118. this.terminate(sess.sid);
  119. }
  120. break;
  121. case 'session-terminate':
  122. logger.log('terminating...', sess.sid);
  123. var reasonCondition = null;
  124. var reasonText = null;
  125. if ($(iq).find('>jingle>reason').length) {
  126. reasonCondition
  127. = $(iq).find('>jingle>reason>:first')[0].tagName;
  128. reasonText = $(iq).find('>jingle>reason>text').text();
  129. }
  130. this.terminate(sess.sid, reasonCondition, reasonText);
  131. break;
  132. case 'addsource': // FIXME: proprietary, un-jingleish
  133. case 'source-add': // FIXME: proprietary
  134. sess.addSource($(iq).find('>jingle>content'));
  135. break;
  136. case 'removesource': // FIXME: proprietary, un-jingleish
  137. case 'source-remove': // FIXME: proprietary
  138. sess.removeSource($(iq).find('>jingle>content'));
  139. break;
  140. default:
  141. logger.warn('jingle action not implemented', action);
  142. ack.attrs({ type: 'error' });
  143. ack.c('error', {type: 'cancel'})
  144. .c('bad-request',
  145. { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' })
  146. .up();
  147. break;
  148. }
  149. this.connection.send(ack);
  150. return true;
  151. },
  152. terminate: function (sid, reasonCondition, reasonText) {
  153. if (this.sessions.hasOwnProperty(sid)) {
  154. if (this.sessions[sid].state != 'ended') {
  155. this.sessions[sid].onTerminated(reasonCondition, reasonText);
  156. }
  157. delete this.jid2session[this.sessions[sid].peerjid];
  158. delete this.sessions[sid];
  159. }
  160. },
  161. getStunAndTurnCredentials: function () {
  162. // get stun and turn configuration from server via xep-0215
  163. // uses time-limited credentials as described in
  164. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  165. //
  166. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  167. // for a prosody module which implements this
  168. //
  169. // currently, this doesn't work with updateIce and therefore credentials with a long
  170. // validity have to be fetched before creating the peerconnection
  171. // TODO: implement refresh via updateIce as described in
  172. // https://code.google.com/p/webrtc/issues/detail?id=1650
  173. var self = this;
  174. this.connection.sendIQ(
  175. $iq({type: 'get', to: this.connection.domain})
  176. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  177. function (res) {
  178. var iceservers = [];
  179. $(res).find('>services>service').each(function (idx, el) {
  180. el = $(el);
  181. var dict = {};
  182. var type = el.attr('type');
  183. switch (type) {
  184. case 'stun':
  185. dict.url = 'stun:' + el.attr('host');
  186. if (el.attr('port')) {
  187. dict.url += ':' + el.attr('port');
  188. }
  189. iceservers.push(dict);
  190. break;
  191. case 'turn':
  192. case 'turns':
  193. dict.url = type + ':';
  194. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  195. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  196. dict.url += el.attr('username') + '@';
  197. } else {
  198. dict.username = el.attr('username'); // only works in M28
  199. }
  200. }
  201. dict.url += el.attr('host');
  202. if (el.attr('port') && el.attr('port') != '3478') {
  203. dict.url += ':' + el.attr('port');
  204. }
  205. if (el.attr('transport') && el.attr('transport') != 'udp') {
  206. dict.url += '?transport=' + el.attr('transport');
  207. }
  208. if (el.attr('password')) {
  209. dict.credential = el.attr('password');
  210. }
  211. iceservers.push(dict);
  212. break;
  213. }
  214. });
  215. self.ice_config.iceServers = iceservers;
  216. },
  217. function (err) {
  218. logger.warn('getting turn credentials failed', err);
  219. logger.warn('is mod_turncredentials or similar installed?');
  220. }
  221. );
  222. // implement push?
  223. },
  224. /**
  225. * Returns the data saved in 'updateLog' in a format to be logged.
  226. */
  227. getLog: function () {
  228. var data = {};
  229. var self = this;
  230. Object.keys(this.sessions).forEach(function (sid) {
  231. var session = self.sessions[sid];
  232. if (session.peerconnection && session.peerconnection.updateLog) {
  233. // FIXME: should probably be a .dump call
  234. data["jingle_" + session.sid] = {
  235. updateLog: session.peerconnection.updateLog,
  236. stats: session.peerconnection.stats,
  237. url: window.location.href
  238. };
  239. }
  240. });
  241. return data;
  242. }
  243. });
  244. };