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

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