modified lib-jitsi-meet dev repo
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

strophe.jingle.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* global $, __filename */
  2. import {
  3. ACTION_JINGLE_TR_RECEIVED,
  4. ACTION_JINGLE_TR_SUCCESS,
  5. createJingleEvent
  6. } from '../../service/statistics/AnalyticsEvents';
  7. import { getLogger } from 'jitsi-meet-logger';
  8. import { $iq, Strophe } from 'strophe.js';
  9. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  10. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  11. import RandomUtil from '../util/RandomUtil';
  12. import Statistics from '../statistics/statistics';
  13. import JingleSessionPC from './JingleSessionPC';
  14. import ConnectionPlugin from './ConnectionPlugin';
  15. const logger = getLogger(__filename);
  16. // XXX Strophe is build around the idea of chaining function calls so allow long
  17. // function call chains.
  18. /* eslint-disable newline-per-chained-call */
  19. /**
  20. *
  21. */
  22. class JingleConnectionPlugin extends ConnectionPlugin {
  23. /**
  24. * Creates new <tt>JingleConnectionPlugin</tt>
  25. * @param {XMPP} xmpp
  26. * @param {EventEmitter} eventEmitter
  27. * @param {Object} iceConfig an object that holds the iceConfig to be passed
  28. * to the p2p and the jvb <tt>PeerConnection</tt>.
  29. */
  30. constructor(xmpp, eventEmitter, iceConfig) {
  31. super();
  32. this.xmpp = xmpp;
  33. this.eventEmitter = eventEmitter;
  34. this.sessions = {};
  35. this.jvbIceConfig = iceConfig.jvb;
  36. this.p2pIceConfig = iceConfig.p2p;
  37. this.mediaConstraints = {
  38. mandatory: {
  39. 'OfferToReceiveAudio': true,
  40. 'OfferToReceiveVideo': true
  41. }
  42. // MozDontOfferDataChannel: true when this is firefox
  43. };
  44. }
  45. /**
  46. *
  47. * @param connection
  48. */
  49. init(connection) {
  50. super.init(connection);
  51. this.connection.addHandler(this.onJingle.bind(this),
  52. 'urn:xmpp:jingle:1', 'iq', 'set', null, null);
  53. }
  54. /**
  55. *
  56. * @param iq
  57. */
  58. onJingle(iq) {
  59. const sid = $(iq).find('jingle').attr('sid');
  60. const action = $(iq).find('jingle').attr('action');
  61. const fromJid = iq.getAttribute('from');
  62. // send ack first
  63. const ack = $iq({ type: 'result',
  64. to: fromJid,
  65. id: iq.getAttribute('id')
  66. });
  67. logger.log(`on jingle ${action} from ${fromJid}`, iq);
  68. let sess = this.sessions[sid];
  69. if (action !== 'session-initiate') {
  70. if (!sess) {
  71. ack.attrs({ type: 'error' });
  72. ack.c('error', { type: 'cancel' })
  73. .c('item-not-found', {
  74. xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'
  75. })
  76. .up()
  77. .c('unknown-session', {
  78. xmlns: 'urn:xmpp:jingle:errors:1'
  79. });
  80. logger.warn('invalid session id', iq);
  81. this.connection.send(ack);
  82. return true;
  83. }
  84. // local jid is not checked
  85. if (fromJid !== sess.remoteJid) {
  86. logger.warn(
  87. 'jid mismatch for session id', sid, sess.remoteJid, iq);
  88. ack.attrs({ type: 'error' });
  89. ack.c('error', { type: 'cancel' })
  90. .c('item-not-found', {
  91. xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'
  92. })
  93. .up()
  94. .c('unknown-session', {
  95. xmlns: 'urn:xmpp:jingle:errors:1'
  96. });
  97. this.connection.send(ack);
  98. return true;
  99. }
  100. } else if (sess !== undefined) {
  101. // Existing session with same session id. This might be out-of-order
  102. // if the sess.remoteJid is the same as from.
  103. ack.attrs({ type: 'error' });
  104. ack.c('error', { type: 'cancel' })
  105. .c('service-unavailable', {
  106. xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'
  107. })
  108. .up();
  109. logger.warn('duplicate session id', sid, iq);
  110. this.connection.send(ack);
  111. return true;
  112. }
  113. const now = window.performance.now();
  114. // FIXME that should work most of the time, but we'd have to
  115. // think how secure it is to assume that user with "focus"
  116. // nickname is Jicofo.
  117. const isP2P = Strophe.getResourceFromJid(fromJid) !== 'focus';
  118. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  119. switch (action) {
  120. case 'session-initiate': {
  121. logger.log('(TIME) received session-initiate:\t', now);
  122. const startMuted = $(iq).find('jingle>startmuted');
  123. if (startMuted && startMuted.length > 0) {
  124. const audioMuted = startMuted.attr('audio');
  125. const videoMuted = startMuted.attr('video');
  126. this.eventEmitter.emit(
  127. XMPPEvents.START_MUTED_FROM_FOCUS,
  128. audioMuted === 'true',
  129. videoMuted === 'true');
  130. }
  131. logger.info(
  132. `Marking session from ${fromJid
  133. } as ${isP2P ? '' : '*not*'} P2P`);
  134. sess
  135. = new JingleSessionPC(
  136. $(iq).find('jingle').attr('sid'),
  137. $(iq).attr('to'),
  138. fromJid,
  139. this.connection,
  140. this.mediaConstraints,
  141. isP2P ? this.p2pIceConfig : this.jvbIceConfig,
  142. isP2P,
  143. /* initiator */ false,
  144. this.xmpp.options);
  145. this.sessions[sess.sid] = sess;
  146. this.eventEmitter.emit(XMPPEvents.CALL_INCOMING,
  147. sess, $(iq).find('>jingle'), now);
  148. break;
  149. }
  150. case 'session-accept': {
  151. this.eventEmitter.emit(
  152. XMPPEvents.CALL_ACCEPTED, sess, $(iq).find('>jingle'));
  153. break;
  154. }
  155. case 'content-modify': {
  156. sess.modifyContents($(iq).find('>jingle'));
  157. break;
  158. }
  159. case 'transport-info': {
  160. this.eventEmitter.emit(
  161. XMPPEvents.TRANSPORT_INFO, sess, $(iq).find('>jingle'));
  162. break;
  163. }
  164. case 'session-terminate': {
  165. logger.log('terminating...', sess.sid);
  166. let reasonCondition = null;
  167. let reasonText = null;
  168. if ($(iq).find('>jingle>reason').length) {
  169. reasonCondition
  170. = $(iq).find('>jingle>reason>:first')[0].tagName;
  171. reasonText = $(iq).find('>jingle>reason>text').text();
  172. }
  173. this.terminate(sess.sid, reasonCondition, reasonText);
  174. this.eventEmitter.emit(XMPPEvents.CALL_ENDED,
  175. sess, reasonCondition, reasonText);
  176. break;
  177. }
  178. case 'transport-replace':
  179. logger.info('(TIME) Start transport replace', now);
  180. Statistics.sendAnalytics(createJingleEvent(
  181. ACTION_JINGLE_TR_RECEIVED,
  182. {
  183. p2p: isP2P,
  184. value: now
  185. }));
  186. sess.replaceTransport($(iq).find('>jingle'), () => {
  187. const successTime = window.performance.now();
  188. logger.info('(TIME) Transport replace success!', successTime);
  189. Statistics.sendAnalytics(createJingleEvent(
  190. ACTION_JINGLE_TR_SUCCESS,
  191. {
  192. p2p: isP2P,
  193. value: successTime
  194. }));
  195. }, error => {
  196. GlobalOnErrorHandler.callErrorHandler(error);
  197. logger.error('Transport replace failed', error);
  198. sess.sendTransportReject();
  199. });
  200. break;
  201. case 'addsource': // FIXME: proprietary, un-jingleish
  202. case 'source-add': // FIXME: proprietary
  203. sess.addRemoteStream($(iq).find('>jingle>content'));
  204. break;
  205. case 'removesource': // FIXME: proprietary, un-jingleish
  206. case 'source-remove': // FIXME: proprietary
  207. sess.removeRemoteStream($(iq).find('>jingle>content'));
  208. break;
  209. default:
  210. logger.warn('jingle action not implemented', action);
  211. ack.attrs({ type: 'error' });
  212. ack.c('error', { type: 'cancel' })
  213. .c('bad-request',
  214. { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' })
  215. .up();
  216. break;
  217. }
  218. this.connection.send(ack);
  219. return true;
  220. }
  221. /**
  222. * Creates new <tt>JingleSessionPC</tt> meant to be used in a direct P2P
  223. * connection, configured as 'initiator'.
  224. * @param {string} me our JID
  225. * @param {string} peer remote participant's JID
  226. * @return {JingleSessionPC}
  227. */
  228. newP2PJingleSession(me, peer) {
  229. const sess
  230. = new JingleSessionPC(
  231. RandomUtil.randomHexString(12),
  232. me,
  233. peer,
  234. this.connection,
  235. this.mediaConstraints,
  236. this.p2pIceConfig,
  237. /* P2P */ true,
  238. /* initiator */ true,
  239. this.xmpp.options);
  240. this.sessions[sess.sid] = sess;
  241. return sess;
  242. }
  243. /**
  244. *
  245. * @param sid
  246. * @param reasonCondition
  247. * @param reasonText
  248. */
  249. terminate(sid, reasonCondition, reasonText) {
  250. if (this.sessions.hasOwnProperty(sid)) {
  251. if (this.sessions[sid].state !== 'ended') {
  252. this.sessions[sid].onTerminated(reasonCondition, reasonText);
  253. }
  254. delete this.sessions[sid];
  255. }
  256. }
  257. /**
  258. *
  259. */
  260. getStunAndTurnCredentials() {
  261. // get stun and turn configuration from server via xep-0215
  262. // uses time-limited credentials as described in
  263. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  264. //
  265. // See https://code.google.com/p/prosody-modules/source/browse/
  266. // mod_turncredentials/mod_turncredentials.lua
  267. // for a prosody module which implements this.
  268. //
  269. // Currently, this doesn't work with updateIce and therefore credentials
  270. // with a long validity have to be fetched before creating the
  271. // peerconnection.
  272. // TODO: implement refresh via updateIce as described in
  273. // https://code.google.com/p/webrtc/issues/detail?id=1650
  274. this.connection.sendIQ(
  275. $iq({ type: 'get',
  276. to: this.connection.domain })
  277. .c('services', { xmlns: 'urn:xmpp:extdisco:1' })
  278. .c('service', { host: `turn.${this.connection.domain}` }),
  279. res => {
  280. const iceservers = [];
  281. $(res).find('>services>service').each((idx, el) => {
  282. // eslint-disable-next-line no-param-reassign
  283. el = $(el);
  284. const dict = {};
  285. const type = el.attr('type');
  286. switch (type) {
  287. case 'stun':
  288. dict.url = `stun:${el.attr('host')}`;
  289. if (el.attr('port')) {
  290. dict.url += `:${el.attr('port')}`;
  291. }
  292. iceservers.push(dict);
  293. break;
  294. case 'turn':
  295. case 'turns': {
  296. dict.url = `${type}:`;
  297. const username = el.attr('username');
  298. // https://code.google.com/p/webrtc/issues/detail
  299. // ?id=1508
  300. if (username) {
  301. const match
  302. = navigator.userAgent.match(
  303. /Chrom(e|ium)\/([0-9]+)\./);
  304. if (match && parseInt(match[2], 10) < 28) {
  305. dict.url += `${username}@`;
  306. } else {
  307. // only works in M28
  308. dict.username = username;
  309. }
  310. }
  311. dict.url += el.attr('host');
  312. const port = el.attr('port');
  313. if (port && port !== '3478') {
  314. dict.url += `:${el.attr('port')}`;
  315. }
  316. const transport = el.attr('transport');
  317. if (transport && transport !== 'udp') {
  318. dict.url += `?transport=${transport}`;
  319. }
  320. dict.credential = el.attr('password')
  321. || dict.credential;
  322. iceservers.push(dict);
  323. break;
  324. }
  325. }
  326. });
  327. const options = this.xmpp.options;
  328. if (options.useStunTurn) {
  329. this.jvbIceConfig.iceServers = iceservers;
  330. }
  331. if (options.p2p && options.p2p.useStunTurn) {
  332. this.p2pIceConfig.iceServers = iceservers;
  333. }
  334. }, err => {
  335. logger.warn('getting turn credentials failed', err);
  336. logger.warn('is mod_turncredentials or similar installed?');
  337. });
  338. // implement push?
  339. }
  340. /**
  341. * Returns the data saved in 'updateLog' in a format to be logged.
  342. */
  343. getLog() {
  344. const data = {};
  345. Object.keys(this.sessions).forEach(sid => {
  346. const session = this.sessions[sid];
  347. const pc = session.peerconnection;
  348. if (pc && pc.updateLog) {
  349. // FIXME: should probably be a .dump call
  350. data[`jingle_${sid}`] = {
  351. updateLog: pc.updateLog,
  352. stats: pc.stats,
  353. url: window.location.href
  354. };
  355. }
  356. });
  357. return data;
  358. }
  359. }
  360. /* eslint-enable newline-per-chained-call */
  361. /**
  362. *
  363. * @param XMPP
  364. * @param eventEmitter
  365. * @param iceConfig
  366. */
  367. export default function initJingle(XMPP, eventEmitter, iceConfig) {
  368. Strophe.addConnectionPlugin(
  369. 'jingle',
  370. new JingleConnectionPlugin(XMPP, eventEmitter, iceConfig));
  371. }