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ů.

JingleSessionPC.js 65KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  1. /* global __filename, $, $iq, Strophe */
  2. import async from 'async';
  3. import { getLogger } from 'jitsi-meet-logger';
  4. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  5. import JingleSession from './JingleSession';
  6. import SDP from './SDP';
  7. import SDPDiffer from './SDPDiffer';
  8. import SDPUtil from './SDPUtil';
  9. import SignalingLayerImpl from './SignalingLayerImpl';
  10. import Statistics from '../statistics/statistics';
  11. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  12. import * as JingleSessionState from './JingleSessionState';
  13. const logger = getLogger(__filename);
  14. /**
  15. * Constant tells how long we're going to wait for IQ response, before timeout
  16. * error is triggered.
  17. * @type {number}
  18. */
  19. const IQ_TIMEOUT = 10000;
  20. /**
  21. *
  22. */
  23. export default class JingleSessionPC extends JingleSession {
  24. /* eslint-disable max-params */
  25. /**
  26. * Creates new <tt>JingleSessionPC</tt>
  27. * @param {string} sid the Jingle Session ID - random string which
  28. * identifies the session
  29. * @param {string} me our JID
  30. * @param {string} peerjid remote peer JID
  31. * @param {Strophe.Connection} connection Strophe XMPP connection instance
  32. * used to send packets.
  33. * @param mediaConstraints the media constraints object passed to
  34. * createOffer/Answer, as defined by the WebRTC standard
  35. * @param iceConfig the ICE servers config object as defined by the WebRTC
  36. * standard.
  37. * @param {boolean} isP2P indicates whether this instance is
  38. * meant to be used in a direct, peer to peer connection or <tt>false</tt>
  39. * if it's a JVB connection.
  40. * @param {boolean} isInitiator indicates whether or not we are the side
  41. * which sends the 'session-intiate'.
  42. * @param {object} options a set of config options
  43. * @param {boolean} options.webrtcIceUdpDisable <tt>true</tt> to block UDP
  44. * candidates.
  45. * @param {boolean} options.webrtcIceTcpDisable <tt>true</tt> to block TCP
  46. * candidates.
  47. * @param {boolean} options.failICE it's an option used in the tests. Set to
  48. * <tt>true</tt> to block any real candidates and make the ICE fail.
  49. *
  50. * @constructor
  51. *
  52. * @implements {SignalingLayer}
  53. */
  54. constructor(
  55. sid,
  56. me,
  57. peerjid,
  58. connection,
  59. mediaConstraints,
  60. iceConfig,
  61. isP2P,
  62. isInitiator,
  63. options) {
  64. super(sid, me, peerjid, connection, mediaConstraints, iceConfig);
  65. this.lasticecandidate = false;
  66. this.closed = false;
  67. /**
  68. * Indicates whether this instance is an initiator or an answerer of
  69. * the Jingle session.
  70. * @type {boolean}
  71. */
  72. this.isInitiator = isInitiator;
  73. /**
  74. * Indicates whether or not this <tt>JingleSessionPC</tt> is used in
  75. * a peer to peer type of session.
  76. * @type {boolean} <tt>true</tt> if it's a peer to peer
  77. * session or <tt>false</tt> if it's a JVB session
  78. */
  79. this.isP2P = isP2P;
  80. /**
  81. * Stores a state for
  82. * {@link TraceablePeerConnection.mediaTransferActive} until
  83. * {@link JingleSessionPC.peerconnection} is initialised and capable of
  84. * handling the value.
  85. * @type {boolean}
  86. * @private
  87. */
  88. this.mediaTransferActive = true;
  89. /**
  90. * The signaling layer implementation.
  91. * @type {SignalingLayerImpl}
  92. */
  93. this.signalingLayer = new SignalingLayerImpl();
  94. this.webrtcIceUdpDisable = Boolean(options.webrtcIceUdpDisable);
  95. this.webrtcIceTcpDisable = Boolean(options.webrtcIceTcpDisable);
  96. /**
  97. * Flag used to enforce ICE failure through the URL parameter for
  98. * the automatic testing purpose.
  99. * @type {boolean}
  100. */
  101. this.failICE = Boolean(options.failICE);
  102. this.modificationQueue
  103. = async.queue(this._processQueueTasks.bind(this), 1);
  104. /**
  105. * This is the MUC JID which will be used to add "owner" extension to
  106. * each of the local SSRCs signaled over Jingle.
  107. * Usually those are added automatically by Jicofo, but it is not
  108. * involved in a P2P session.
  109. * @type {string}
  110. */
  111. this.ssrcOwnerJid = null;
  112. /**
  113. * Flag used to guarantee that the connection established event is
  114. * triggered just once.
  115. * @type {boolean}
  116. */
  117. this.wasConnected = false;
  118. }
  119. /**
  120. * Checks whether or not this session instance has been ended and eventually
  121. * logs a message which mentions that given <tt>actionName</tt> was
  122. * cancelled.
  123. * @param {string} actionName
  124. * @return {boolean} <tt>true</tt> if this {@link JingleSessionPC} has
  125. * entered {@link JingleSessionState.ENDED} or <tt>false</tt> otherwise.
  126. * @private
  127. */
  128. _assertNotEnded(actionName) {
  129. if (this.state === JingleSessionState.ENDED) {
  130. logger.log(
  131. `The session has ended - cancelling action: ${actionName}`);
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. * Finds all "source" elements under RTC "description" in given Jingle IQ
  138. * and adds 'ssrc-info' with the owner attribute set to
  139. * {@link ssrcOwnerJid}.
  140. * @param jingleIq the IQ to be modified
  141. * @private
  142. */
  143. _markAsSSRCOwner(jingleIq) {
  144. $(jingleIq).find('description source')
  145. .append(
  146. '<ssrc-info xmlns="http://jitsi.org/jitmeet" '
  147. + `owner="${this.ssrcOwnerJid}"></ssrc-info>`);
  148. }
  149. /**
  150. * Sets the JID which will be as an owner value for the local SSRCs
  151. * signaled over Jingle. Should be our MUC JID.
  152. * @param {string} ownerJid
  153. */
  154. setSSRCOwnerJid(ownerJid) {
  155. this.ssrcOwnerJid = ownerJid;
  156. }
  157. /* eslint-enable max-params */
  158. /**
  159. *
  160. */
  161. doInitialize() {
  162. this.lasticecandidate = false;
  163. // True if reconnect is in progress
  164. this.isreconnect = false;
  165. // Set to true if the connection was ever stable
  166. this.wasstable = false;
  167. // Create new peer connection instance
  168. this.peerconnection
  169. = this.rtc.createPeerConnection(
  170. this.signalingLayer,
  171. this.iceConfig,
  172. this.isP2P,
  173. {
  174. disableSimulcast: this.room.options.disableSimulcast,
  175. disableRtx: this.room.options.disableRtx,
  176. preferH264: this.room.options.preferH264
  177. });
  178. this.peerconnection.setMediaTransferActive(this.mediaTransferActive);
  179. this.peerconnection.onicecandidate = ev => {
  180. if (!ev) {
  181. // There was an incomplete check for ev before which left
  182. // the last line of the function unprotected from a potential
  183. // throw of an exception. Consequently, it may be argued that
  184. // the check is unnecessary. Anyway, I'm leaving it and making
  185. // the check complete.
  186. return;
  187. }
  188. // XXX this is broken, candidate is not parsed.
  189. const candidate = ev.candidate;
  190. if (candidate) {
  191. // Discard candidates of disabled protocols.
  192. let protocol = candidate.protocol;
  193. if (typeof protocol === 'string') {
  194. protocol = protocol.toLowerCase();
  195. if (protocol === 'tcp' || protocol === 'ssltcp') {
  196. if (this.webrtcIceTcpDisable) {
  197. return;
  198. }
  199. } else if (protocol === 'udp') {
  200. if (this.webrtcIceUdpDisable) {
  201. return;
  202. }
  203. }
  204. }
  205. }
  206. this.sendIceCandidate(candidate);
  207. };
  208. // Note there is a change in the spec about closed:
  209. // This value moved into the RTCPeerConnectionState enum in
  210. // the May 13, 2016 draft of the specification, as it reflects the state
  211. // of the RTCPeerConnection, not the signaling connection. You now
  212. // detect a closed connection by checking for connectionState to be
  213. // "closed" instead.
  214. // I suppose at some point this will be moved to onconnectionstatechange
  215. this.peerconnection.onsignalingstatechange = () => {
  216. if (!this.peerconnection) {
  217. return;
  218. }
  219. if (this.peerconnection.signalingState === 'stable') {
  220. this.wasstable = true;
  221. } else if (
  222. (this.peerconnection.signalingState === 'closed'
  223. || this.peerconnection.connectionState === 'closed')
  224. && !this.closed) {
  225. this.room.eventEmitter.emit(XMPPEvents.SUSPEND_DETECTED, this);
  226. }
  227. };
  228. /**
  229. * The oniceconnectionstatechange event handler contains the code to
  230. * execute when the iceconnectionstatechange event, of type Event,
  231. * is received by this RTCPeerConnection. Such an event is sent when
  232. * the value of RTCPeerConnection.iceConnectionState changes.
  233. */
  234. this.peerconnection.oniceconnectionstatechange = () => {
  235. if (!this.peerconnection
  236. || !this._assertNotEnded('oniceconnectionstatechange')) {
  237. return;
  238. }
  239. const now = window.performance.now();
  240. if (!this.isP2P) {
  241. this.room.connectionTimes[
  242. `ice.state.${this.peerconnection.iceConnectionState}`]
  243. = now;
  244. }
  245. logger.log(
  246. `(TIME) ICE ${this.peerconnection.iceConnectionState}`
  247. + ` P2P? ${this.isP2P}:\t`,
  248. now);
  249. Statistics.analytics.sendEvent(
  250. `ice.${this.peerconnection.iceConnectionState}`,
  251. { value: now });
  252. this.room.eventEmitter.emit(
  253. XMPPEvents.ICE_CONNECTION_STATE_CHANGED,
  254. this,
  255. this.peerconnection.iceConnectionState);
  256. switch (this.peerconnection.iceConnectionState) {
  257. case 'connected':
  258. // Informs interested parties that the connection has been
  259. // restored.
  260. if (this.peerconnection.signalingState === 'stable') {
  261. if (this.isreconnect) {
  262. this.room.eventEmitter.emit(
  263. XMPPEvents.CONNECTION_RESTORED, this);
  264. } else if (!this.wasConnected) {
  265. this.room.eventEmitter.emit(
  266. XMPPEvents.CONNECTION_ESTABLISHED, this);
  267. }
  268. this.wasConnected = true;
  269. }
  270. this.isreconnect = false;
  271. break;
  272. case 'disconnected':
  273. if (this.closed) {
  274. break;
  275. }
  276. this.isreconnect = true;
  277. // Informs interested parties that the connection has been
  278. // interrupted.
  279. if (this.wasstable) {
  280. this.room.eventEmitter.emit(
  281. XMPPEvents.CONNECTION_INTERRUPTED, this);
  282. }
  283. break;
  284. case 'failed':
  285. this.room.eventEmitter.emit(
  286. XMPPEvents.CONNECTION_ICE_FAILED, this);
  287. this.room.eventEmitter.emit(
  288. XMPPEvents.CONFERENCE_SETUP_FAILED,
  289. this,
  290. new Error('ICE fail'));
  291. break;
  292. }
  293. };
  294. this.peerconnection.onnegotiationneeded = () => {
  295. this.room.eventEmitter.emit(XMPPEvents.PEERCONNECTION_READY, this);
  296. };
  297. // The signaling layer will bind it's listeners at this point
  298. this.signalingLayer.setChatRoom(this.room);
  299. }
  300. /**
  301. * Sends given candidate in Jingle 'transport-info' message.
  302. * @param {RTCIceCandidate} candidate the WebRTC ICE candidate instance
  303. * @private
  304. */
  305. sendIceCandidate(candidate) {
  306. const localSDP = new SDP(this.peerconnection.localDescription.sdp);
  307. if (candidate && !this.lasticecandidate) {
  308. const ice
  309. = SDPUtil.iceparams(
  310. localSDP.media[candidate.sdpMLineIndex], localSDP.session);
  311. const jcand = SDPUtil.candidateToJingle(candidate.candidate);
  312. if (!(ice && jcand)) {
  313. const errorMesssage = 'failed to get ice && jcand';
  314. GlobalOnErrorHandler.callErrorHandler(new Error(errorMesssage));
  315. logger.error(errorMesssage);
  316. return;
  317. }
  318. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  319. if (this.usedrip) {
  320. if (this.dripContainer.length === 0) {
  321. // start 20ms callout
  322. setTimeout(() => {
  323. if (this.dripContainer.length === 0) {
  324. return;
  325. }
  326. this.sendIceCandidates(this.dripContainer);
  327. this.dripContainer = [];
  328. }, 20);
  329. }
  330. this.dripContainer.push(candidate);
  331. } else {
  332. this.sendIceCandidates([ candidate ]);
  333. }
  334. } else {
  335. logger.log('sendIceCandidate: last candidate.');
  336. // FIXME: remember to re-think in ICE-restart
  337. this.lasticecandidate = true;
  338. }
  339. }
  340. /**
  341. * Sends given candidates in Jingle 'transport-info' message.
  342. * @param {Array<RTCIceCandidate>} candidates an array of the WebRTC ICE
  343. * candidate instances
  344. * @private
  345. */
  346. sendIceCandidates(candidates) {
  347. if (!this._assertNotEnded('sendIceCandidates')) {
  348. return;
  349. }
  350. logger.log('sendIceCandidates', candidates);
  351. const cand = $iq({ to: this.peerjid,
  352. type: 'set' })
  353. .c('jingle', { xmlns: 'urn:xmpp:jingle:1',
  354. action: 'transport-info',
  355. initiator: this.initiator,
  356. sid: this.sid });
  357. const localSDP = new SDP(this.peerconnection.localDescription.sdp);
  358. for (let mid = 0; mid < localSDP.media.length; mid++) {
  359. const cands = candidates.filter(el => el.sdpMLineIndex === mid);
  360. const mline
  361. = SDPUtil.parseMLine(localSDP.media[mid].split('\r\n')[0]);
  362. if (cands.length > 0) {
  363. const ice
  364. = SDPUtil.iceparams(localSDP.media[mid], localSDP.session);
  365. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  366. cand.c('content', {
  367. creator: this.initiator === this.localJid
  368. ? 'initiator' : 'responder',
  369. name: cands[0].sdpMid ? cands[0].sdpMid : mline.media
  370. }).c('transport', ice);
  371. for (let i = 0; i < cands.length; i++) {
  372. const candidate
  373. = SDPUtil.candidateToJingle(cands[i].candidate);
  374. // Mangle ICE candidate if 'failICE' test option is enabled
  375. if (this.failICE) {
  376. candidate.ip = '1.1.1.1';
  377. }
  378. cand.c('candidate', candidate).up();
  379. }
  380. // add fingerprint
  381. const fingerprintLine
  382. = SDPUtil.findLine(
  383. localSDP.media[mid],
  384. 'a=fingerprint:', localSDP.session);
  385. if (fingerprintLine) {
  386. const tmp = SDPUtil.parseFingerprint(fingerprintLine);
  387. tmp.required = true;
  388. cand.c(
  389. 'fingerprint',
  390. { xmlns: 'urn:xmpp:jingle:apps:dtls:0' })
  391. .t(tmp.fingerprint);
  392. delete tmp.fingerprint;
  393. cand.attrs(tmp);
  394. cand.up();
  395. }
  396. cand.up(); // transport
  397. cand.up(); // content
  398. }
  399. }
  400. // might merge last-candidate notification into this, but it is called
  401. // a lot later. See webrtc issue #2340
  402. // logger.log('was this the last candidate', this.lasticecandidate);
  403. this.connection.sendIQ(
  404. cand, null, this.newJingleErrorHandler(cand, error => {
  405. GlobalOnErrorHandler.callErrorHandler(
  406. new Error(`Jingle error: ${JSON.stringify(error)}`));
  407. }), IQ_TIMEOUT);
  408. }
  409. /**
  410. * {@inheritDoc}
  411. */
  412. addIceCandidates(elem) {
  413. if (this.peerconnection.signalingState === 'closed') {
  414. logger.warn('Ignored add ICE candidate when in closed state');
  415. return;
  416. }
  417. const iceCandidates = [];
  418. elem.find('>content>transport>candidate')
  419. .each((idx, candidate) => {
  420. let line = SDPUtil.candidateFromJingle(candidate);
  421. line = line.replace('\r\n', '').replace('a=', '');
  422. // FIXME this code does not care to handle
  423. // non-bundle transport
  424. const rtcCandidate = new RTCIceCandidate({
  425. sdpMLineIndex: 0,
  426. // FF comes up with more complex names like audio-23423,
  427. // Given that it works on both Chrome and FF without
  428. // providing it, let's leave it like this for the time
  429. // being...
  430. // sdpMid: 'audio',
  431. candidate: line
  432. });
  433. iceCandidates.push(rtcCandidate);
  434. });
  435. if (!iceCandidates.length) {
  436. logger.error(
  437. 'No ICE candidates to add ?', elem[0] && elem[0].outerHTML);
  438. return;
  439. }
  440. // We want to have this task queued, so that we know it is executed,
  441. // after the initial sRD/sLD offer/answer cycle was done (based on
  442. // the assumption that candidates are spawned after the offer/answer
  443. // and XMPP preserves order).
  444. const workFunction = () => {
  445. for (const iceCandidate of iceCandidates) {
  446. this.peerconnection.addIceCandidate(
  447. iceCandidate,
  448. () => {
  449. logger.debug('addIceCandidate ok!');
  450. },
  451. error => {
  452. logger.error('addIceCandidate failed!', error);
  453. });
  454. }
  455. // There's no need to renegotiate for ICE candidates added with
  456. // 'peerconnection.addIceCandidate'
  457. return false;
  458. };
  459. logger.debug(`Queued add ICE candidates(${iceCandidates.length}) task`);
  460. this._doRenegotiate('add ICE candidate', workFunction)
  461. .then(() => {
  462. logger.debug('Add ICE candidate done !');
  463. },
  464. error => {
  465. logger.error(
  466. 'Failed to add ICE candidate',
  467. elem[0] && elem[0].outerHTML,
  468. error);
  469. });
  470. }
  471. /**
  472. *
  473. * @param contents
  474. */
  475. readSsrcInfo(contents) {
  476. $(contents).each((i1, content) => {
  477. const ssrcs
  478. = $(content).find(
  479. 'description>'
  480. + 'source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  481. ssrcs.each((i2, ssrcElement) => {
  482. const ssrc = ssrcElement.getAttribute('ssrc');
  483. $(ssrcElement)
  484. .find('>ssrc-info[xmlns="http://jitsi.org/jitmeet"]')
  485. .each((i3, ssrcInfoElement) => {
  486. const owner = ssrcInfoElement.getAttribute('owner');
  487. if (owner && owner.length) {
  488. this.signalingLayer.setSSRCOwner(
  489. ssrc, Strophe.getResourceFromJid(owner));
  490. }
  491. }
  492. );
  493. });
  494. });
  495. }
  496. /**
  497. * Makes the underlying TraceablePeerConnection generate new SSRC for
  498. * the recvonly video stream.
  499. * @deprecated
  500. */
  501. generateRecvonlySsrc() {
  502. if (this.peerconnection) {
  503. this.peerconnection.generateRecvonlySsrc();
  504. } else {
  505. logger.error(
  506. 'Unable to generate recvonly SSRC - no peerconnection');
  507. }
  508. }
  509. /* eslint-disable max-params */
  510. /**
  511. * Accepts incoming Jingle 'session-initiate' and should send
  512. * 'session-accept' in result.
  513. * @param jingleOffer jQuery selector pointing to the jingle element of
  514. * the offer IQ
  515. * @param success callback called when we accept incoming session
  516. * successfully and receive RESULT packet to 'session-accept' sent.
  517. * @param failure function(error) called if for any reason we fail to accept
  518. * the incoming offer. 'error' argument can be used to log some details
  519. * about the error.
  520. * @param {Array<JitsiLocalTrack>} [localTracks] the optional list of
  521. * the local tracks that will be added, before the offer/answer cycle
  522. * executes. We allow the localTracks to optionally be passed in so that
  523. * the addition of the local tracks and the processing of the initial offer
  524. * can all be done atomically. We want to make sure that any other
  525. * operations which originate in the XMPP Jingle messages related with
  526. * this session to be executed with an assumption that the initial
  527. * offer/answer cycle has been executed already.
  528. */
  529. acceptOffer(jingleOffer, success, failure, localTracks) {
  530. this.setOfferAnswerCycle(
  531. jingleOffer,
  532. () => {
  533. this.state = JingleSessionState.ACTIVE;
  534. // FIXME we may not care about RESULT packet for session-accept
  535. // then we should either call 'success' here immediately or
  536. // modify sendSessionAccept method to do that
  537. this.sendSessionAccept(success, failure);
  538. },
  539. failure,
  540. localTracks);
  541. }
  542. /* eslint-enable max-params */
  543. /**
  544. * Creates an offer and sends Jingle 'session-initiate' to the remote peer.
  545. * @param {Array<JitsiLocalTrack>} localTracks the local tracks that will be
  546. * added, before the offer/answer cycle executes (for the local track
  547. * addition to be an atomic operation together with the offer/answer).
  548. */
  549. invite(localTracks) {
  550. if (!this.isInitiator) {
  551. throw new Error('Trying to invite from the responder session');
  552. }
  553. for (const localTrack of localTracks) {
  554. this.peerconnection.addTrack(localTrack);
  555. }
  556. this.peerconnection.createOffer(
  557. this.sendSessionInitiate.bind(this),
  558. error => logger.error('Failed to create offer', error),
  559. this.mediaConstraints);
  560. }
  561. /**
  562. * Sends 'session-initiate' to the remote peer.
  563. * @param {object} sdp the local session description object as defined by
  564. * the WebRTC standard.
  565. * @private
  566. */
  567. sendSessionInitiate(sdp) {
  568. logger.log('createdOffer', sdp);
  569. const sendJingle = () => {
  570. let init = $iq({
  571. to: this.peerjid,
  572. type: 'set'
  573. }).c('jingle', {
  574. xmlns: 'urn:xmpp:jingle:1',
  575. action: 'session-initiate',
  576. initiator: this.initiator,
  577. sid: this.sid
  578. });
  579. const localSDP = new SDP(this.peerconnection.localDescription.sdp);
  580. localSDP.toJingle(
  581. init,
  582. this.initiator === this.me ? 'initiator' : 'responder');
  583. init = init.tree();
  584. this._markAsSSRCOwner(init);
  585. logger.info('Session-initiate: ', init);
  586. this.connection.sendIQ(init,
  587. () => {
  588. logger.info('Got RESULT for "session-initiate"');
  589. },
  590. error => {
  591. logger.error('"session-initiate" error', error);
  592. },
  593. IQ_TIMEOUT);
  594. };
  595. this.peerconnection.setLocalDescription(
  596. sdp, sendJingle,
  597. error => {
  598. logger.error('session-init setLocalDescription failed', error);
  599. }
  600. );
  601. }
  602. /**
  603. * Sets the answer received from the remote peer.
  604. * @param jingleAnswer
  605. */
  606. setAnswer(jingleAnswer) {
  607. if (!this.isInitiator) {
  608. throw new Error('Trying to set an answer on the responder session');
  609. }
  610. this.setOfferAnswerCycle(
  611. jingleAnswer,
  612. () => {
  613. this.state = JingleSessionState.ACTIVE;
  614. logger.info('setAnswer - succeeded');
  615. },
  616. error => {
  617. logger.error('setAnswer failed: ', error);
  618. });
  619. }
  620. /* eslint-disable max-params */
  621. /**
  622. * This is a setRemoteDescription/setLocalDescription cycle which starts at
  623. * converting Strophe Jingle IQ into remote offer SDP. Once converted
  624. * setRemoteDescription, createAnswer and setLocalDescription calls follow.
  625. * @param jingleOfferAnswerIq jQuery selector pointing to the jingle element
  626. * of the offer (or answer) IQ
  627. * @param success callback called when sRD/sLD cycle finishes successfully.
  628. * @param failure callback called with an error object as an argument if we
  629. * fail at any point during setRD, createAnswer, setLD.
  630. * @param {Array<JitsiLocalTrack>} [localTracks] the optional list of
  631. * the local tracks that will be added, before the offer/answer cycle
  632. * executes (for the local track addition to be an atomic operation together
  633. * with the offer/answer).
  634. */
  635. setOfferAnswerCycle(jingleOfferAnswerIq, success, failure, localTracks) {
  636. const workFunction = finishedCallback => {
  637. if (localTracks) {
  638. for (const track of localTracks) {
  639. this.peerconnection.addTrack(track);
  640. }
  641. }
  642. const newRemoteSdp
  643. = this._processNewJingleOfferIq(jingleOfferAnswerIq);
  644. this._renegotiate(newRemoteSdp)
  645. .then(() => {
  646. finishedCallback();
  647. }, error => {
  648. logger.error(
  649. `Error renegotiating after setting new remote ${
  650. (this.isInitiator ? 'answer: ' : 'offer: ')
  651. }${error}`, newRemoteSdp);
  652. JingleSessionPC.onJingleFatalError(this, error);
  653. finishedCallback(error);
  654. });
  655. };
  656. this.modificationQueue.push(
  657. workFunction,
  658. error => {
  659. error ? failure(error) : success();
  660. });
  661. }
  662. /* eslint-enable max-params */
  663. /**
  664. * Although it states "replace transport" it does accept full Jingle offer
  665. * which should contain new ICE transport details.
  666. * @param jingleOfferElem an element Jingle IQ that contains new offer and
  667. * transport info.
  668. * @param success callback called when we succeed to accept new offer.
  669. * @param failure function(error) called when we fail to accept new offer.
  670. */
  671. replaceTransport(jingleOfferElem, success, failure) {
  672. // We need to first set an offer without the 'data' section to have the
  673. // SCTP stack cleaned up. After that the original offer is set to have
  674. // the SCTP connection established with the new bridge.
  675. this.room.eventEmitter.emit(XMPPEvents.ICE_RESTARTING, this);
  676. const originalOffer = jingleOfferElem.clone();
  677. jingleOfferElem.find('>content[name=\'data\']').remove();
  678. // First set an offer without the 'data' section
  679. this.setOfferAnswerCycle(
  680. jingleOfferElem,
  681. () => {
  682. // Now set the original offer(with the 'data' section)
  683. this.setOfferAnswerCycle(
  684. originalOffer,
  685. () => {
  686. const localSDP
  687. = new SDP(this.peerconnection.localDescription.sdp);
  688. this.sendTransportAccept(localSDP, success, failure);
  689. },
  690. failure);
  691. },
  692. failure
  693. );
  694. }
  695. /**
  696. * Sends Jingle 'session-accept' message.
  697. * @param {function()} success callback called when we receive 'RESULT'
  698. * packet for the 'session-accept'
  699. * @param {function(error)} failure called when we receive an error response
  700. * or when the request has timed out.
  701. * @private
  702. */
  703. sendSessionAccept(success, failure) {
  704. // NOTE: since we're just reading from it, we don't need to be within
  705. // the modification queue to access the local description
  706. const localSDP = new SDP(this.peerconnection.localDescription.sdp);
  707. let accept = $iq({ to: this.peerjid,
  708. type: 'set' })
  709. .c('jingle', { xmlns: 'urn:xmpp:jingle:1',
  710. action: 'session-accept',
  711. initiator: this.initiator,
  712. responder: this.responder,
  713. sid: this.sid });
  714. if (this.webrtcIceTcpDisable) {
  715. localSDP.removeTcpCandidates = true;
  716. }
  717. if (this.webrtcIceUdpDisable) {
  718. localSDP.removeUdpCandidates = true;
  719. }
  720. if (this.failICE) {
  721. localSDP.failICE = true;
  722. }
  723. localSDP.toJingle(
  724. accept,
  725. this.initiator === this.localJid ? 'initiator' : 'responder',
  726. null);
  727. // Calling tree() to print something useful
  728. accept = accept.tree();
  729. this._markAsSSRCOwner(accept);
  730. logger.info('Sending session-accept', accept);
  731. this.connection.sendIQ(accept,
  732. success,
  733. this.newJingleErrorHandler(accept, error => {
  734. failure(error);
  735. // 'session-accept' is a critical timeout and we'll
  736. // have to restart
  737. this.room.eventEmitter.emit(
  738. XMPPEvents.SESSION_ACCEPT_TIMEOUT, this);
  739. }),
  740. IQ_TIMEOUT);
  741. // XXX Videobridge needs WebRTC's answer (ICE ufrag and pwd, DTLS
  742. // fingerprint and setup) ASAP in order to start the connection
  743. // establishment.
  744. //
  745. // FIXME Flushing the connection at this point triggers an issue with
  746. // BOSH request handling in Prosody on slow connections.
  747. //
  748. // The problem is that this request will be quite large and it may take
  749. // time before it reaches Prosody. In the meantime Strophe may decide
  750. // to send the next one. And it was observed that a small request with
  751. // 'transport-info' usually follows this one. It does reach Prosody
  752. // before the previous one was completely received. 'rid' on the server
  753. // is increased and Prosody ignores the request with 'session-accept'.
  754. // It will never reach Jicofo and everything in the request table is
  755. // lost. Removing the flush does not guarantee it will never happen, but
  756. // makes it much less likely('transport-info' is bundled with
  757. // 'session-accept' and any immediate requests).
  758. //
  759. // this.connection.flush();
  760. }
  761. /**
  762. * Sends Jingle 'transport-accept' message which is a response to
  763. * 'transport-replace'.
  764. * @param localSDP the 'SDP' object with local session description
  765. * @param success callback called when we receive 'RESULT' packet for
  766. * 'transport-replace'
  767. * @param failure function(error) called when we receive an error response
  768. * or when the request has timed out.
  769. * @private
  770. */
  771. sendTransportAccept(localSDP, success, failure) {
  772. let transportAccept = $iq({ to: this.peerjid,
  773. type: 'set' })
  774. .c('jingle', {
  775. xmlns: 'urn:xmpp:jingle:1',
  776. action: 'transport-accept',
  777. initiator: this.initiator,
  778. sid: this.sid
  779. });
  780. localSDP.media.forEach((medialines, idx) => {
  781. const mline = SDPUtil.parseMLine(medialines.split('\r\n')[0]);
  782. transportAccept.c('content',
  783. {
  784. creator:
  785. this.initiator === this.localJid
  786. ? 'initiator'
  787. : 'responder',
  788. name: mline.media
  789. }
  790. );
  791. localSDP.transportToJingle(idx, transportAccept);
  792. transportAccept.up();
  793. });
  794. // Calling tree() to print something useful to the logger
  795. transportAccept = transportAccept.tree();
  796. logger.info('Sending transport-accept: ', transportAccept);
  797. this.connection.sendIQ(transportAccept,
  798. success,
  799. this.newJingleErrorHandler(transportAccept, failure),
  800. IQ_TIMEOUT);
  801. }
  802. /**
  803. * Sends Jingle 'transport-reject' message which is a response to
  804. * 'transport-replace'.
  805. * @param success callback called when we receive 'RESULT' packet for
  806. * 'transport-replace'
  807. * @param failure function(error) called when we receive an error response
  808. * or when the request has timed out.
  809. *
  810. * FIXME method should be marked as private, but there's some spaghetti that
  811. * needs to be fixed prior doing that
  812. */
  813. sendTransportReject(success, failure) {
  814. // Send 'transport-reject', so that the focus will
  815. // know that we've failed
  816. let transportReject = $iq({ to: this.peerjid,
  817. type: 'set' })
  818. .c('jingle', {
  819. xmlns: 'urn:xmpp:jingle:1',
  820. action: 'transport-reject',
  821. initiator: this.initiator,
  822. sid: this.sid
  823. });
  824. transportReject = transportReject.tree();
  825. logger.info('Sending \'transport-reject', transportReject);
  826. this.connection.sendIQ(transportReject,
  827. success,
  828. this.newJingleErrorHandler(transportReject, failure),
  829. IQ_TIMEOUT);
  830. }
  831. /* eslint-disable max-params */
  832. /**
  833. * @inheritDoc
  834. */
  835. terminate(reason, text, success, failure) {
  836. let sessionTerminate = $iq({
  837. to: this.peerjid,
  838. type: 'set'
  839. })
  840. .c('jingle', {
  841. xmlns: 'urn:xmpp:jingle:1',
  842. action: 'session-terminate',
  843. initiator: this.initiator,
  844. sid: this.sid
  845. })
  846. .c('reason')
  847. .c(reason || 'success');
  848. if (text) {
  849. // eslint-disable-next-line newline-per-chained-call
  850. sessionTerminate.up().c('text').t(text);
  851. }
  852. // Calling tree() to print something useful
  853. sessionTerminate = sessionTerminate.tree();
  854. logger.info('Sending session-terminate', sessionTerminate);
  855. this.connection.sendIQ(
  856. sessionTerminate,
  857. success,
  858. this.newJingleErrorHandler(sessionTerminate, failure), IQ_TIMEOUT);
  859. // this should result in 'onTerminated' being called by strope.jingle.js
  860. this.connection.jingle.terminate(this.sid);
  861. }
  862. /* eslint-enable max-params */
  863. /**
  864. *
  865. * @param reasonCondition
  866. * @param reasonText
  867. */
  868. onTerminated(reasonCondition, reasonText) {
  869. this.state = JingleSessionState.ENDED;
  870. // Do something with reason and reasonCondition when we start to care
  871. // this.reasonCondition = reasonCondition;
  872. // this.reasonText = reasonText;
  873. logger.info('Session terminated', this, reasonCondition, reasonText);
  874. this.close();
  875. }
  876. /**
  877. * Parse the information from the xml sourceAddElem and translate it
  878. * into sdp lines
  879. * @param {jquery xml element} sourceAddElem the source-add
  880. * element from jingle
  881. * @param {SDP object} currentRemoteSdp the current remote
  882. * sdp (as of this new source-add)
  883. * @returns {list} a list of SDP line strings that should
  884. * be added to the remote SDP
  885. */
  886. _parseSsrcInfoFromSourceAdd(sourceAddElem, currentRemoteSdp) {
  887. const addSsrcInfo = [];
  888. $(sourceAddElem).each((i1, content) => {
  889. const name = $(content).attr('name');
  890. let lines = '';
  891. $(content)
  892. .find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  893. .each(function() {
  894. // eslint-disable-next-line no-invalid-this
  895. const semantics = this.getAttribute('semantics');
  896. const ssrcs
  897. = $(this) // eslint-disable-line no-invalid-this
  898. .find('>source')
  899. .map(function() {
  900. // eslint-disable-next-line no-invalid-this
  901. return this.getAttribute('ssrc');
  902. })
  903. .get();
  904. if (ssrcs.length) {
  905. lines
  906. += `a=ssrc-group:${semantics} ${ssrcs.join(' ')
  907. }\r\n`;
  908. }
  909. });
  910. // handles both >source and >description>source
  911. const tmp
  912. = $(content).find(
  913. 'source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  914. /* eslint-disable no-invalid-this */
  915. tmp.each(function() {
  916. const ssrc = $(this).attr('ssrc');
  917. if (currentRemoteSdp.containsSSRC(ssrc)) {
  918. logger.warn(
  919. `Source-add request for existing SSRC: ${ssrc}`);
  920. return;
  921. }
  922. // eslint-disable-next-line newline-per-chained-call
  923. $(this).find('>parameter').each(function() {
  924. lines += `a=ssrc:${ssrc} ${$(this).attr('name')}`;
  925. if ($(this).attr('value') && $(this).attr('value').length) {
  926. lines += `:${$(this).attr('value')}`;
  927. }
  928. lines += '\r\n';
  929. });
  930. });
  931. /* eslint-enable no-invalid-this */
  932. currentRemoteSdp.media.forEach((media, i2) => {
  933. if (!SDPUtil.findLine(media, `a=mid:${name}`)) {
  934. return;
  935. }
  936. if (!addSsrcInfo[i2]) {
  937. addSsrcInfo[i2] = '';
  938. }
  939. addSsrcInfo[i2] += lines;
  940. });
  941. });
  942. return addSsrcInfo;
  943. }
  944. /**
  945. * Handles a Jingle source-add message for this Jingle session.
  946. * @param elem An array of Jingle "content" elements.
  947. */
  948. addRemoteStream(elem) {
  949. if (!this.peerconnection.localDescription) {
  950. logger.warn('addSource - localDescription not ready yet');
  951. if (this._assertNotEnded('addRemoteStream')) {
  952. setTimeout(() => this.addRemoteStream(elem), 200);
  953. }
  954. return;
  955. }
  956. logger.log('Processing add remote stream');
  957. logger.log(
  958. 'ICE connection state: ', this.peerconnection.iceConnectionState);
  959. this.readSsrcInfo(elem);
  960. const workFunction = () => {
  961. const sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  962. const addSsrcInfo = this._parseSsrcInfoFromSourceAdd(elem, sdp);
  963. const newRemoteSdp = this._processRemoteAddSource(addSsrcInfo);
  964. return newRemoteSdp;
  965. };
  966. // Queue and execute
  967. this._doRenegotiate('source-add', workFunction)
  968. .then(() => {
  969. logger.info('source-add - done!');
  970. })
  971. .catch(error => {
  972. logger.error('source-add error:', error);
  973. });
  974. }
  975. /**
  976. * Handles a Jingle source-remove message for this Jingle session.
  977. * @param elem An array of Jingle "content" elements.
  978. */
  979. removeRemoteStream(elem) {
  980. if (!this.peerconnection.localDescription) {
  981. logger.warn('removeSource - localDescription not ready yet');
  982. if (this._assertNotEnded('removeRemoteStream')) {
  983. setTimeout(() => this.removeRemoteStream(elem), 200);
  984. }
  985. return;
  986. }
  987. const workFunction = () => {
  988. logger.log('Remove remote stream');
  989. logger.log(
  990. 'ICE connection state: ',
  991. this.peerconnection.iceConnectionState);
  992. const sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  993. const removeSsrcInfo
  994. = this._parseSsrcInfoFromSourceRemove(elem, sdp);
  995. const newRemoteSdp
  996. = this._processRemoteRemoveSource(removeSsrcInfo);
  997. return newRemoteSdp;
  998. };
  999. // Queue and execute
  1000. this._doRenegotiate('source-remove', workFunction)
  1001. .then(() => {
  1002. logger.info('source-remove - done!');
  1003. })
  1004. .catch(error => {
  1005. logger.error('source-remove error:', error);
  1006. });
  1007. }
  1008. /**
  1009. * The 'task' function will be given a callback it MUST call with either:
  1010. * 1) No arguments if it was successful or
  1011. * 2) An error argument if there was an error
  1012. * If the task wants to process the success or failure of the task, it
  1013. * should pass a handler to the .push function, e.g.:
  1014. * queue.push(task, (err) => {
  1015. * if (err) {
  1016. * // error handling
  1017. * } else {
  1018. * // success handling
  1019. * }
  1020. * });
  1021. */
  1022. _processQueueTasks(task, finishedCallback) {
  1023. task(finishedCallback);
  1024. }
  1025. /**
  1026. * Takes in a jingle offer iq, returns the new sdp offer
  1027. * @param {jquery xml element} offerIq the incoming offer
  1028. * @returns {SDP object} the jingle offer translated to SDP
  1029. */
  1030. _processNewJingleOfferIq(offerIq) {
  1031. const remoteSdp = new SDP('');
  1032. if (this.webrtcIceTcpDisable) {
  1033. remoteSdp.removeTcpCandidates = true;
  1034. }
  1035. if (this.webrtcIceUdpDisable) {
  1036. remoteSdp.removeUdpCandidates = true;
  1037. }
  1038. if (this.failICE) {
  1039. remoteSdp.failICE = true;
  1040. }
  1041. remoteSdp.fromJingle(offerIq);
  1042. this.readSsrcInfo($(offerIq).find('>content'));
  1043. return remoteSdp;
  1044. }
  1045. /**
  1046. * Remove the given ssrc lines from the current remote sdp
  1047. * @param {list} removeSsrcInfo a list of SDP line strings that
  1048. * should be removed from the remote SDP
  1049. * @returns type {SDP Object} the new remote SDP (after removing the lines
  1050. * in removeSsrcInfo
  1051. */
  1052. _processRemoteRemoveSource(removeSsrcInfo) {
  1053. const remoteSdp = new SDP(this.peerconnection.remoteDescription.sdp);
  1054. removeSsrcInfo.forEach((lines, idx) => {
  1055. // eslint-disable-next-line no-param-reassign
  1056. lines = lines.split('\r\n');
  1057. lines.pop(); // remove empty last element;
  1058. lines.forEach(line => {
  1059. remoteSdp.media[idx]
  1060. = remoteSdp.media[idx].replace(`${line}\r\n`, '');
  1061. });
  1062. });
  1063. remoteSdp.raw = remoteSdp.session + remoteSdp.media.join('');
  1064. return remoteSdp;
  1065. }
  1066. /**
  1067. * Add the given ssrc lines to the current remote sdp
  1068. * @param {list} addSsrcInfo a list of SDP line strings that
  1069. * should be added to the remote SDP
  1070. * @returns type {SDP Object} the new remote SDP (after removing the lines
  1071. * in removeSsrcInfo
  1072. */
  1073. _processRemoteAddSource(addSsrcInfo) {
  1074. const remoteSdp = new SDP(this.peerconnection.remoteDescription.sdp);
  1075. addSsrcInfo.forEach((lines, idx) => {
  1076. remoteSdp.media[idx] += lines;
  1077. });
  1078. remoteSdp.raw = remoteSdp.session + remoteSdp.media.join('');
  1079. return remoteSdp;
  1080. }
  1081. /**
  1082. * Do a new o/a flow using the existing remote description
  1083. * @param {SDP object} optionalRemoteSdp optional remote sdp
  1084. * to use. If not provided, the remote sdp from the
  1085. * peerconnection will be used
  1086. * @returns {Promise} promise which resolves when the
  1087. * o/a flow is complete with no arguments or
  1088. * rejects with an error {string}
  1089. */
  1090. _renegotiate(optionalRemoteSdp) {
  1091. const remoteSdp
  1092. = optionalRemoteSdp
  1093. || new SDP(this.peerconnection.remoteDescription.sdp);
  1094. const remoteDescription = new RTCSessionDescription({
  1095. type: this.isInitiator ? 'answer' : 'offer',
  1096. sdp: remoteSdp.raw
  1097. });
  1098. return new Promise((resolve, reject) => {
  1099. if (this.peerconnection.signalingState === 'closed') {
  1100. reject('Attempted to renegotiate in state closed');
  1101. return;
  1102. }
  1103. if (this.isInitiator) {
  1104. this._initiatorRenegotiate(remoteDescription, resolve, reject);
  1105. } else {
  1106. this._responderRenegotiate(remoteDescription, resolve, reject);
  1107. }
  1108. });
  1109. }
  1110. /**
  1111. * Renegotiate cycle implementation for the responder case.
  1112. * @param {object} remoteDescription the SDP object as defined by the WebRTC
  1113. * which will be used as remote description in the cycle.
  1114. * @param {function} resolve the success callback
  1115. * @param {function} reject the failure callback
  1116. * @private
  1117. */
  1118. _responderRenegotiate(remoteDescription, resolve, reject) {
  1119. // FIXME use WebRTC promise API to simplify things
  1120. logger.debug('Renegotiate: setting remote description');
  1121. this.peerconnection.setRemoteDescription(
  1122. remoteDescription,
  1123. () => {
  1124. logger.debug('Renegotiate: creating answer');
  1125. this.peerconnection.createAnswer(
  1126. answer => {
  1127. logger.debug('Renegotiate: setting local description');
  1128. this.peerconnection.setLocalDescription(
  1129. answer,
  1130. () => {
  1131. resolve();
  1132. },
  1133. error => {
  1134. reject(
  1135. `setLocalDescription failed: ${error}`);
  1136. }
  1137. );
  1138. },
  1139. error => reject(`createAnswer failed: ${error}`),
  1140. this.mediaConstraints
  1141. );
  1142. },
  1143. error => reject(`setRemoteDescription failed: ${error}`)
  1144. );
  1145. }
  1146. /**
  1147. * Renegotiate cycle implementation for the initiator's case.
  1148. * @param {object} remoteDescription the SDP object as defined by the WebRTC
  1149. * which will be used as remote description in the cycle.
  1150. * @param {function} resolve the success callback
  1151. * @param {function} reject the failure callback
  1152. * @private
  1153. */
  1154. _initiatorRenegotiate(remoteDescription, resolve, reject) {
  1155. // FIXME use WebRTC promise API to simplify things
  1156. if (this.peerconnection.signalingState === 'have-local-offer') {
  1157. // Skip createOffer and setLocalDescription or FF will fail
  1158. logger.debug(
  1159. 'Renegotiate: setting remote description');
  1160. this.peerconnection.setRemoteDescription(
  1161. remoteDescription,
  1162. () => {
  1163. resolve();
  1164. },
  1165. error => reject(`setRemoteDescription failed: ${error}`)
  1166. );
  1167. } else {
  1168. logger.debug('Renegotiate: creating offer');
  1169. this.peerconnection.createOffer(
  1170. offer => {
  1171. logger.debug('Renegotiate: setting local description');
  1172. this.peerconnection.setLocalDescription(offer,
  1173. () => {
  1174. logger.debug(
  1175. 'Renegotiate: setting remote description');
  1176. this.peerconnection.setRemoteDescription(
  1177. remoteDescription,
  1178. () => {
  1179. resolve();
  1180. },
  1181. error => reject(
  1182. `setRemoteDescription failed: ${error}`)
  1183. );
  1184. },
  1185. error => {
  1186. reject('setLocalDescription failed: ', error);
  1187. });
  1188. },
  1189. error => reject(`createOffer failed: ${error}`),
  1190. this.mediaConstraints);
  1191. }
  1192. }
  1193. /**
  1194. * Replaces <tt>oldTrack</tt> with <tt>newTrack</tt> and performs a single
  1195. * offer/answer cycle after both operations are done. Either
  1196. * <tt>oldTrack</tt> or <tt>newTrack</tt> can be null; replacing a valid
  1197. * <tt>oldTrack</tt> with a null <tt>newTrack</tt> effectively just removes
  1198. * <tt>oldTrack</tt>
  1199. * @param {JitsiLocalTrack|null} oldTrack the current track in use to be
  1200. * replaced
  1201. * @param {JitsiLocalTrack|null} newTrack the new track to use
  1202. * @returns {Promise} which resolves once the replacement is complete
  1203. * with no arguments or rejects with an error {string}
  1204. */
  1205. replaceTrack(oldTrack, newTrack) {
  1206. const workFunction = () => {
  1207. // NOTE the code below assumes that no more than 1 video track
  1208. // can be added to the peer connection.
  1209. // Transition from no video to video (possibly screen sharing)
  1210. if (!oldTrack && newTrack && newTrack.isVideoTrack()) {
  1211. // Clearing current primary SSRC will make
  1212. // the SdpConsistency generate a new one which will result
  1213. // with:
  1214. // 1. source-remove for the recvonly
  1215. // 2. source-add for the new video stream
  1216. this.peerconnection.clearRecvonlySsrc();
  1217. // Transition from video to no video
  1218. } else if (oldTrack && oldTrack.isVideoTrack() && !newTrack) {
  1219. // Clearing current primary SSRC and generating the recvonly
  1220. // will result in:
  1221. // 1. source-remove for the old video stream
  1222. // 2. source-add for the recvonly stream
  1223. this.peerconnection.clearRecvonlySsrc();
  1224. this.peerconnection.generateRecvonlySsrc();
  1225. }
  1226. if (oldTrack) {
  1227. this.peerconnection.removeTrack(oldTrack);
  1228. }
  1229. if (newTrack) {
  1230. this.peerconnection.addTrack(newTrack);
  1231. }
  1232. return Boolean(oldTrack || newTrack); // Try to renegotiate
  1233. };
  1234. return this._doRenegotiate('replaceTrack', workFunction);
  1235. }
  1236. /**
  1237. * Parse the information from the xml sourceRemoveElem and translate it
  1238. * into sdp lines
  1239. * @param {jquery xml element} sourceRemoveElem the source-remove
  1240. * element from jingle
  1241. * @param {SDP object} currentRemoteSdp the current remote
  1242. * sdp (as of this new source-remove)
  1243. * @returns {list} a list of SDP line strings that should
  1244. * be removed from the remote SDP
  1245. */
  1246. _parseSsrcInfoFromSourceRemove(sourceRemoveElem, currentRemoteSdp) {
  1247. const removeSsrcInfo = [];
  1248. $(sourceRemoveElem).each((i1, content) => {
  1249. const name = $(content).attr('name');
  1250. let lines = '';
  1251. $(content)
  1252. .find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  1253. .each(function() {
  1254. /* eslint-disable no-invalid-this */
  1255. const semantics = this.getAttribute('semantics');
  1256. const ssrcs
  1257. = $(this)
  1258. .find('>source')
  1259. .map(function() {
  1260. return this.getAttribute('ssrc');
  1261. })
  1262. .get();
  1263. if (ssrcs.length) {
  1264. lines
  1265. += `a=ssrc-group:${semantics} ${ssrcs.join(' ')
  1266. }\r\n`;
  1267. }
  1268. /* eslint-enable no-invalid-this */
  1269. });
  1270. const ssrcs = [];
  1271. // handles both >source and >description>source versions
  1272. const tmp
  1273. = $(content).find(
  1274. 'source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  1275. tmp.each(function() {
  1276. // eslint-disable-next-line no-invalid-this
  1277. const ssrc = $(this).attr('ssrc');
  1278. ssrcs.push(ssrc);
  1279. });
  1280. currentRemoteSdp.media.forEach((media, i2) => {
  1281. if (!SDPUtil.findLine(media, `a=mid:${name}`)) {
  1282. return;
  1283. }
  1284. if (!removeSsrcInfo[i2]) {
  1285. removeSsrcInfo[i2] = '';
  1286. }
  1287. ssrcs.forEach(ssrc => {
  1288. const ssrcLines
  1289. = SDPUtil.findLines(media, `a=ssrc:${ssrc}`);
  1290. if (ssrcLines.length) {
  1291. removeSsrcInfo[i2] += `${ssrcLines.join('\r\n')}\r\n`;
  1292. }
  1293. });
  1294. removeSsrcInfo[i2] += lines;
  1295. });
  1296. });
  1297. return removeSsrcInfo;
  1298. }
  1299. /**
  1300. * Adds local track back to this session, as part of the unmute operation.
  1301. * @param {JitsiLocalTrack} track
  1302. * @return {Promise} a promise that will resolve once the local track is
  1303. * added back to this session and renegotiation succeeds. Will be rejected
  1304. * with a <tt>string</tt> that provides some error details in case something
  1305. * goes wrong.
  1306. */
  1307. addTrackAsUnmute(track) {
  1308. if (!track) {
  1309. return Promise.reject('invalid "track" argument value');
  1310. }
  1311. const workFunction = () => {
  1312. if (!this.peerconnection) {
  1313. return 'Error: '
  1314. + 'tried adding track with no active peer connection';
  1315. }
  1316. return this.peerconnection.addTrackUnmute(track);
  1317. };
  1318. return this._doRenegotiate('addStreamAsUnmute', workFunction);
  1319. }
  1320. /**
  1321. * Remove local track as part of the mute operation.
  1322. * @param {JitsiLocalTrack} track the local track to be removed
  1323. * @return {Promise} a promise which will be resolved once the local track
  1324. * is removed from this session and the renegotiation is performed.
  1325. * The promise will be rejected with a <tt>string</tt> that the describes
  1326. * the error if anything goes wrong.
  1327. */
  1328. removeTrackAsMute(track) {
  1329. if (!track) {
  1330. return Promise.reject('invalid "stream" argument value');
  1331. }
  1332. const workFunction = () => {
  1333. if (!this.peerconnection) {
  1334. return false;
  1335. }
  1336. return this.peerconnection.removeTrackMute(track);
  1337. };
  1338. return this._doRenegotiate('remove-as-mute', workFunction);
  1339. }
  1340. /**
  1341. * A worker function that is scheduled and executed on the
  1342. * {@link modificationQueue}. Local SDPs from before and after the function
  1343. * is executed are compared and 'source-add'/'source-remove' notifications
  1344. * are being sent to remote participants (to propagate changes in local
  1345. * streams description).
  1346. * @name JingleSessionPC~WorkerFunction
  1347. * @function
  1348. * @returns {boolean|string|SDP} there are several things that this
  1349. * function can return:
  1350. * <tt>true</tt> if the renegotiation should follow the function execution
  1351. * <tt>false</tt> if there should be no renegotiation
  1352. * <tt>SDP</tt> that will be set as the remote description in the
  1353. * renegotiation process
  1354. * <tt>string</tt> which stands for the error description, no renegotiation
  1355. * will be done in that case it's returned
  1356. */
  1357. /**
  1358. * Does the logic of doing the session renegotiation by updating local and
  1359. * remote session descriptions. Will compare the local description, before
  1360. * and after the renegotiation to update local streams description (sends
  1361. * "source-add"/"source-remove" notifications).
  1362. * @param {string} actionName the name of the action which will appear in
  1363. * the events logged to the logger.
  1364. * @param {WorkerFunction} workFunction a function that will be executed on
  1365. * the queue. See type description for moe info.
  1366. * @private
  1367. */
  1368. _doRenegotiate(actionName, workFunction) {
  1369. const workFunctionWrap = finishedCallback => {
  1370. // Remember localSDP from before any modifications are done, by
  1371. // the worker function
  1372. let oldSdp = this.peerconnection.localDescription.sdp;
  1373. let remoteSdp = null;
  1374. let modifySources = workFunction();
  1375. // workFunction can return error description
  1376. if (typeof modifySources === 'string') {
  1377. finishedCallback(modifySources);
  1378. return;
  1379. } else if (typeof modifySources === 'object') {
  1380. remoteSdp = modifySources;
  1381. modifySources = true;
  1382. }
  1383. if (!oldSdp) {
  1384. logger.info(
  1385. `${this}: ${actionName} - will NOT modify sources, `
  1386. + 'because there is no local SDP yet');
  1387. modifySources = false;
  1388. } else if (!this.peerconnection.remoteDescription.sdp) {
  1389. logger.info(
  1390. `${this}: ${actionName} - will NOT modify sources, `
  1391. + 'because there is no remote SDP yet');
  1392. modifySources = false;
  1393. }
  1394. if (!modifySources) {
  1395. // ABORT
  1396. finishedCallback();
  1397. return;
  1398. }
  1399. // Convert to SDP object
  1400. oldSdp = new SDP(oldSdp);
  1401. this._renegotiate(remoteSdp)
  1402. .then(() => {
  1403. const newSdp
  1404. = new SDP(this.peerconnection.localDescription.sdp);
  1405. logger.log(`${actionName} - OK, SDPs: `, oldSdp, newSdp);
  1406. this.notifyMySSRCUpdate(oldSdp, newSdp);
  1407. finishedCallback();
  1408. }, error => {
  1409. logger.error(`${actionName} renegotiate failed: `, error);
  1410. finishedCallback(error);
  1411. });
  1412. };
  1413. return new Promise((resolve, reject) => {
  1414. this.modificationQueue.push(
  1415. workFunctionWrap,
  1416. error => {
  1417. if (error) {
  1418. reject(error);
  1419. } else {
  1420. resolve();
  1421. }
  1422. }
  1423. );
  1424. });
  1425. }
  1426. /**
  1427. * Resumes or suspends media transfer over the underlying peer connection.
  1428. * @param {boolean} active <tt>true</tt> to enable media transfer or
  1429. * <tt>false</tt> to suspend any media transmission.
  1430. * @return {Promise} a <tt>Promise</tt> which will resolve once
  1431. * the operation is done. It will be rejected with an error description as
  1432. * a string in case anything goes wrong.
  1433. */
  1434. setMediaTransferActive(active) {
  1435. const workFunction = () => {
  1436. this.mediaTransferActive = active;
  1437. if (this.peerconnection) {
  1438. this.peerconnection.setMediaTransferActive(
  1439. this.mediaTransferActive);
  1440. // Will do the sRD/sLD cycle to update SDPs and adjust the media
  1441. // direction
  1442. return true;
  1443. }
  1444. return false; // Do not renegotiate
  1445. };
  1446. const logStr = active ? 'active' : 'inactive';
  1447. return this._doRenegotiate(
  1448. `make media transfer ${logStr}`, workFunction);
  1449. }
  1450. /**
  1451. * Figures out added/removed ssrcs and send update IQs.
  1452. * @param oldSDP SDP object for old description.
  1453. * @param newSDP SDP object for new description.
  1454. */
  1455. notifyMySSRCUpdate(oldSDP, newSDP) {
  1456. if (this.state !== JingleSessionState.ACTIVE) {
  1457. logger.warn(`Skipping SSRC update in '${this.state} ' state.`);
  1458. return;
  1459. }
  1460. // send source-remove IQ.
  1461. let sdpDiffer = new SDPDiffer(newSDP, oldSDP);
  1462. const remove = $iq({ to: this.peerjid,
  1463. type: 'set' })
  1464. .c('jingle', {
  1465. xmlns: 'urn:xmpp:jingle:1',
  1466. action: 'source-remove',
  1467. initiator: this.initiator,
  1468. sid: this.sid
  1469. }
  1470. );
  1471. const removedAnySSRCs = sdpDiffer.toJingle(remove);
  1472. if (removedAnySSRCs) {
  1473. logger.info('Sending source-remove', remove.tree());
  1474. this.connection.sendIQ(
  1475. remove, null,
  1476. this.newJingleErrorHandler(remove, error => {
  1477. GlobalOnErrorHandler.callErrorHandler(
  1478. new Error(`Jingle error: ${JSON.stringify(error)}`));
  1479. }), IQ_TIMEOUT);
  1480. } else {
  1481. logger.log('removal not necessary');
  1482. }
  1483. // send source-add IQ.
  1484. sdpDiffer = new SDPDiffer(oldSDP, newSDP);
  1485. const add = $iq({ to: this.peerjid,
  1486. type: 'set' })
  1487. .c('jingle', {
  1488. xmlns: 'urn:xmpp:jingle:1',
  1489. action: 'source-add',
  1490. initiator: this.initiator,
  1491. sid: this.sid
  1492. }
  1493. );
  1494. const containsNewSSRCs = sdpDiffer.toJingle(add);
  1495. if (containsNewSSRCs) {
  1496. logger.info('Sending source-add', add.tree());
  1497. this.connection.sendIQ(
  1498. add, null, this.newJingleErrorHandler(add, error => {
  1499. GlobalOnErrorHandler.callErrorHandler(
  1500. new Error(`Jingle error: ${JSON.stringify(error)}`));
  1501. }), IQ_TIMEOUT);
  1502. } else {
  1503. logger.log('addition not necessary');
  1504. }
  1505. }
  1506. /**
  1507. * Method returns function(errorResponse) which is a callback to be passed
  1508. * to Strophe connection.sendIQ method. An 'error' structure is created that
  1509. * is passed as 1st argument to given <tt>failureCb</tt>. The format of this
  1510. * structure is as follows:
  1511. * {
  1512. * code: {XMPP error response code}
  1513. * reason: {the name of XMPP error reason element or 'timeout' if the
  1514. * request has timed out within <tt>IQ_TIMEOUT</tt> milliseconds}
  1515. * source: {request.tree() that provides original request}
  1516. * session: {JingleSessionPC instance on which the error occurred}
  1517. * }
  1518. * @param request Strophe IQ instance which is the request to be dumped into
  1519. * the error structure
  1520. * @param failureCb function(error) called when error response was returned
  1521. * or when a timeout has occurred.
  1522. * @returns {function(this:JingleSessionPC)}
  1523. */
  1524. newJingleErrorHandler(request, failureCb) {
  1525. return function(errResponse) {
  1526. const error = {};
  1527. // Get XMPP error code and condition(reason)
  1528. const errorElSel = $(errResponse).find('error');
  1529. if (errorElSel.length) {
  1530. error.code = errorElSel.attr('code');
  1531. const errorReasonSel = $(errResponse).find('error :first');
  1532. if (errorReasonSel.length) {
  1533. error.reason = errorReasonSel[0].tagName;
  1534. }
  1535. }
  1536. if (!errResponse) {
  1537. error.reason = 'timeout';
  1538. }
  1539. error.source = request;
  1540. if (request && typeof request.tree === 'function') {
  1541. error.source = request.tree();
  1542. }
  1543. if (error.source && error.source.outerHTML) {
  1544. error.source = error.source.outerHTML;
  1545. }
  1546. // Commented to fix JSON.stringify(error) exception for circular
  1547. // dependancies when we print that error.
  1548. // FIXME: Maybe we can include part of the session object
  1549. // error.session = this;
  1550. logger.error('Jingle error', error);
  1551. if (failureCb) {
  1552. failureCb(error);
  1553. }
  1554. };
  1555. }
  1556. /**
  1557. *
  1558. * @param session
  1559. * @param error
  1560. */
  1561. static onJingleFatalError(session, error) {
  1562. if (this.room) {
  1563. this.room.eventEmitter.emit(
  1564. XMPPEvents.CONFERENCE_SETUP_FAILED, session, error);
  1565. this.room.eventEmitter.emit(
  1566. XMPPEvents.JINGLE_FATAL_ERROR, session, error);
  1567. }
  1568. }
  1569. /**
  1570. * Returns the ice connection state for the peer connection.
  1571. * @returns the ice connection state for the peer connection.
  1572. */
  1573. getIceConnectionState() {
  1574. return this.peerconnection.iceConnectionState;
  1575. }
  1576. /**
  1577. * Closes the peerconnection.
  1578. */
  1579. close() {
  1580. this.closed = true;
  1581. // The signaling layer will remove it's listeners
  1582. this.signalingLayer.setChatRoom(null);
  1583. // do not try to close if already closed.
  1584. this.peerconnection
  1585. && ((this.peerconnection.signalingState
  1586. && this.peerconnection.signalingState !== 'closed')
  1587. || (this.peerconnection.connectionState
  1588. && this.peerconnection.connectionState !== 'closed'))
  1589. && this.peerconnection.close();
  1590. }
  1591. }