You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JingleSessionPC.js 58KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. /* jshint -W117 */
  2. /* jshint -W101 */
  3. var JingleSession = require("./JingleSession");
  4. var TraceablePeerConnection = require("./TraceablePeerConnection");
  5. var SDPDiffer = require("./SDPDiffer");
  6. var SDPUtil = require("./SDPUtil");
  7. var SDP = require("./SDP");
  8. var async = require("async");
  9. var transform = require("sdp-transform");
  10. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  11. var RTCEvents = require("../../service/RTC/RTCEvents");
  12. var RTCBrowserType = require("../RTC/RTCBrowserType");
  13. // Jingle stuff
  14. function JingleSessionPC(me, sid, connection, service, eventEmitter) {
  15. JingleSession.call(this, me, sid, connection, service, eventEmitter);
  16. this.initiator = null;
  17. this.responder = null;
  18. this.peerjid = null;
  19. this.state = null;
  20. this.localSDP = null;
  21. this.remoteSDP = null;
  22. this.pc_constraints = null;
  23. this.usetrickle = true;
  24. this.usepranswer = false; // early transport warmup -- mind you, this might fail. depends on webrtc issue 1718
  25. this.hadstuncandidate = false;
  26. this.hadturncandidate = false;
  27. this.lasticecandidate = false;
  28. this.statsinterval = null;
  29. this.reason = null;
  30. this.addssrc = [];
  31. this.removessrc = [];
  32. this.pendingop = null;
  33. this.switchstreams = false;
  34. this.wait = true;
  35. /**
  36. * A map that stores SSRCs of local streams
  37. * @type {{}} maps media type('audio' or 'video') to SSRC number
  38. */
  39. this.localStreamsSSRC = {};
  40. this.ssrcOwners = {};
  41. this.ssrcVideoTypes = {};
  42. this.eventEmitter = eventEmitter;
  43. /**
  44. * The indicator which determines whether the (local) video has been muted
  45. * in response to a user command in contrast to an automatic decision made
  46. * by the application logic.
  47. */
  48. this.videoMuteByUser = false;
  49. this.modifySourcesQueue = async.queue(this._modifySources.bind(this), 1);
  50. // We start with the queue paused. We resume it when the signaling state is
  51. // stable and the ice connection state is connected.
  52. this.modifySourcesQueue.pause();
  53. }
  54. //XXX this is badly broken...
  55. JingleSessionPC.prototype = JingleSession.prototype;
  56. JingleSessionPC.prototype.constructor = JingleSessionPC;
  57. JingleSessionPC.prototype.setOffer = function(offer) {
  58. this.setRemoteDescription(offer, 'offer');
  59. };
  60. JingleSessionPC.prototype.setAnswer = function(answer) {
  61. this.setRemoteDescription(answer, 'answer');
  62. };
  63. JingleSessionPC.prototype.updateModifySourcesQueue = function() {
  64. var signalingState = this.peerconnection.signalingState;
  65. var iceConnectionState = this.peerconnection.iceConnectionState;
  66. if (signalingState === 'stable' && iceConnectionState === 'connected') {
  67. this.modifySourcesQueue.resume();
  68. } else {
  69. this.modifySourcesQueue.pause();
  70. }
  71. };
  72. JingleSessionPC.prototype.doInitialize = function () {
  73. var self = this;
  74. this.hadstuncandidate = false;
  75. this.hadturncandidate = false;
  76. this.lasticecandidate = false;
  77. // True if reconnect is in progress
  78. this.isreconnect = false;
  79. // Set to true if the connection was ever stable
  80. this.wasstable = false;
  81. this.peerconnection = new TraceablePeerConnection(
  82. this.connection.jingle.ice_config,
  83. this.connection.jingle.pc_constraints,
  84. this);
  85. this.peerconnection.onicecandidate = function (event) {
  86. var protocol;
  87. if (event && event.candidate) {
  88. protocol = (typeof event.candidate.protocol === 'string')
  89. ? event.candidate.protocol.toLowerCase() : '';
  90. if ((config.webrtcIceTcpDisable && protocol == 'tcp') ||
  91. (config.webrtcIceUdpDisable && protocol == 'udp')) {
  92. return;
  93. }
  94. }
  95. self.sendIceCandidate(event.candidate);
  96. };
  97. this.peerconnection.onaddstream = function (event) {
  98. if (event.stream.id === 'default') {
  99. // This is a recvonly stream. Clients that implement Unified Plan,
  100. // such as Firefox use recvonly "streams/channels/tracks" for
  101. // receiving remote stream/tracks, as opposed to Plan B where there
  102. // are only 3 channels: audio, video and data.
  103. console.log("RECVONLY REMOTE STREAM IGNORED: " + event.stream +
  104. " - " + event.stream.id);
  105. return;
  106. }
  107. console.log("REMOTE STREAM ADDED: ", event.stream, event.stream.id);
  108. self.remoteStreamAdded(event);
  109. };
  110. this.peerconnection.onremovestream = function (event) {
  111. // Remove the stream from remoteStreams?
  112. console.log("We are ignoring a removestream event: " + event);
  113. };
  114. this.peerconnection.onsignalingstatechange = function (event) {
  115. if (!(self && self.peerconnection)) return;
  116. console.info("Signaling: " + this.peerconnection.signalingState);
  117. if (self.peerconnection.signalingState === 'stable') {
  118. self.wasstable = true;
  119. }
  120. self.updateModifySourcesQueue();
  121. };
  122. /**
  123. * The oniceconnectionstatechange event handler contains the code to execute when the iceconnectionstatechange event,
  124. * of type Event, is received by this RTCPeerConnection. Such an event is sent when the value of
  125. * RTCPeerConnection.iceConnectionState changes.
  126. *
  127. * @param event the event containing information about the change
  128. */
  129. this.peerconnection.oniceconnectionstatechange = function (event) {
  130. if (!(self && self.peerconnection)) return;
  131. console.log("(TIME) ICE " + self.peerconnection.iceConnectionState +
  132. ":\t", window.performance.now());
  133. self.updateModifySourcesQueue();
  134. switch (self.peerconnection.iceConnectionState) {
  135. case 'connected':
  136. // Informs interested parties that the connection has been restored.
  137. if (self.peerconnection.signalingState === 'stable' && self.isreconnect)
  138. self.eventEmitter.emit(XMPPEvents.CONNECTION_RESTORED);
  139. self.isreconnect = false;
  140. break;
  141. case 'disconnected':
  142. self.isreconnect = true;
  143. // Informs interested parties that the connection has been interrupted.
  144. if (self.wasstable)
  145. self.eventEmitter.emit(XMPPEvents.CONNECTION_INTERRUPTED);
  146. break;
  147. case 'failed':
  148. self.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  149. break;
  150. }
  151. onIceConnectionStateChange(self.sid, self);
  152. };
  153. this.peerconnection.onnegotiationneeded = function (event) {
  154. self.eventEmitter.emit(XMPPEvents.PEERCONNECTION_READY, self);
  155. };
  156. if (APP.RTC.localAudio) {
  157. self.peerconnection.addStream(APP.RTC.localAudio.getOriginalStream());
  158. }
  159. if (APP.RTC.localVideo) {
  160. self.peerconnection.addStream(APP.RTC.localVideo.getOriginalStream());
  161. }
  162. };
  163. function onIceConnectionStateChange(sid, session) {
  164. switch (session.peerconnection.iceConnectionState) {
  165. case 'checking':
  166. session.timeChecking = (new Date()).getTime();
  167. session.firstconnect = true;
  168. break;
  169. case 'completed': // on caller side
  170. case 'connected':
  171. if (session.firstconnect) {
  172. session.firstconnect = false;
  173. var metadata = {};
  174. metadata.setupTime
  175. = (new Date()).getTime() - session.timeChecking;
  176. session.peerconnection.getStats(function (res) {
  177. if(res && res.result) {
  178. res.result().forEach(function (report) {
  179. if (report.type == 'googCandidatePair' &&
  180. report.stat('googActiveConnection') == 'true') {
  181. metadata.localCandidateType
  182. = report.stat('googLocalCandidateType');
  183. metadata.remoteCandidateType
  184. = report.stat('googRemoteCandidateType');
  185. // log pair as well so we can get nice pie
  186. // charts
  187. metadata.candidatePair
  188. = report.stat('googLocalCandidateType') +
  189. ';' +
  190. report.stat('googRemoteCandidateType');
  191. if (report.stat('googRemoteAddress').indexOf('[') === 0)
  192. {
  193. metadata.ipv6 = true;
  194. }
  195. }
  196. });
  197. }
  198. });
  199. }
  200. break;
  201. }
  202. }
  203. JingleSessionPC.prototype.accept = function () {
  204. this.state = 'active';
  205. var pranswer = this.peerconnection.localDescription;
  206. if (!pranswer || pranswer.type != 'pranswer') {
  207. return;
  208. }
  209. console.log('going from pranswer to answer');
  210. if (this.usetrickle) {
  211. // remove candidates already sent from session-accept
  212. var lines = SDPUtil.find_lines(pranswer.sdp, 'a=candidate:');
  213. for (var i = 0; i < lines.length; i++) {
  214. pranswer.sdp = pranswer.sdp.replace(lines[i] + '\r\n', '');
  215. }
  216. }
  217. while (SDPUtil.find_line(pranswer.sdp, 'a=inactive')) {
  218. // FIXME: change any inactive to sendrecv or whatever they were originally
  219. pranswer.sdp = pranswer.sdp.replace('a=inactive', 'a=sendrecv');
  220. }
  221. var prsdp = new SDP(pranswer.sdp);
  222. if (config.webrtcIceTcpDisable) {
  223. prsdp.removeTcpCandidates = true;
  224. }
  225. if (config.webrtcIceUdpDisable) {
  226. prsdp.removeUdpCandidates = true;
  227. }
  228. var accept = $iq({to: this.peerjid,
  229. type: 'set'})
  230. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  231. action: 'session-accept',
  232. initiator: this.initiator,
  233. responder: this.responder,
  234. sid: this.sid });
  235. // FIXME why do we generate session-accept in 3 different places ?
  236. prsdp.toJingle(
  237. accept,
  238. this.initiator == this.me ? 'initiator' : 'responder');
  239. var sdp = this.peerconnection.localDescription.sdp;
  240. while (SDPUtil.find_line(sdp, 'a=inactive')) {
  241. // FIXME: change any inactive to sendrecv or whatever they were originally
  242. sdp = sdp.replace('a=inactive', 'a=sendrecv');
  243. }
  244. var self = this;
  245. this.peerconnection.setLocalDescription(new RTCSessionDescription({type: 'answer', sdp: sdp}),
  246. function () {
  247. //console.log('setLocalDescription success');
  248. self.setLocalDescription();
  249. self.connection.sendIQ(accept,
  250. function () {
  251. var ack = {};
  252. ack.source = 'answer';
  253. $(document).trigger('ack.jingle', [self.sid, ack]);
  254. },
  255. function (stanza) {
  256. var error = ($(stanza).find('error').length) ? {
  257. code: $(stanza).find('error').attr('code'),
  258. reason: $(stanza).find('error :first')[0].tagName
  259. }:{};
  260. error.source = 'answer';
  261. JingleSessionPC.onJingleError(self.sid, error);
  262. },
  263. 10000);
  264. },
  265. function (e) {
  266. console.error('setLocalDescription failed', e);
  267. self.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  268. }
  269. );
  270. };
  271. JingleSessionPC.prototype.terminate = function (reason) {
  272. this.state = 'ended';
  273. this.reason = reason;
  274. this.peerconnection.close();
  275. if (this.statsinterval !== null) {
  276. window.clearInterval(this.statsinterval);
  277. this.statsinterval = null;
  278. }
  279. };
  280. JingleSessionPC.prototype.active = function () {
  281. return this.state == 'active';
  282. };
  283. JingleSessionPC.prototype.sendIceCandidate = function (candidate) {
  284. var self = this;
  285. if (candidate && !this.lasticecandidate) {
  286. var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session);
  287. var jcand = SDPUtil.candidateToJingle(candidate.candidate);
  288. if (!(ice && jcand)) {
  289. console.error('failed to get ice && jcand');
  290. return;
  291. }
  292. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  293. if (jcand.type === 'srflx') {
  294. this.hadstuncandidate = true;
  295. } else if (jcand.type === 'relay') {
  296. this.hadturncandidate = true;
  297. }
  298. if (this.usetrickle) {
  299. if (this.usedrip) {
  300. if (this.drip_container.length === 0) {
  301. // start 20ms callout
  302. window.setTimeout(function () {
  303. if (self.drip_container.length === 0) return;
  304. self.sendIceCandidates(self.drip_container);
  305. self.drip_container = [];
  306. }, 20);
  307. }
  308. this.drip_container.push(candidate);
  309. return;
  310. } else {
  311. self.sendIceCandidate([candidate]);
  312. }
  313. }
  314. } else {
  315. //console.log('sendIceCandidate: last candidate.');
  316. if (!this.usetrickle) {
  317. //console.log('should send full offer now...');
  318. //FIXME why do we generate session-accept in 3 different places ?
  319. var init = $iq({to: this.peerjid,
  320. type: 'set'})
  321. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  322. action: this.peerconnection.localDescription.type == 'offer' ? 'session-initiate' : 'session-accept',
  323. initiator: this.initiator,
  324. sid: this.sid});
  325. this.localSDP = new SDP(this.peerconnection.localDescription.sdp);
  326. if (config.webrtcIceTcpDisable) {
  327. this.localSDP.removeTcpCandidates = true;
  328. }
  329. if (config.webrtcIceUdpDisable) {
  330. this.localSDP.removeUdpCandidates = true;
  331. }
  332. var sendJingle = function (ssrc) {
  333. if(!ssrc)
  334. ssrc = {};
  335. self.localSDP.toJingle(
  336. init,
  337. self.initiator == self.me ? 'initiator' : 'responder',
  338. ssrc);
  339. self.connection.sendIQ(init,
  340. function () {
  341. //console.log('session initiate ack');
  342. var ack = {};
  343. ack.source = 'offer';
  344. $(document).trigger('ack.jingle', [self.sid, ack]);
  345. },
  346. function (stanza) {
  347. self.state = 'error';
  348. self.peerconnection.close();
  349. var error = ($(stanza).find('error').length) ? {
  350. code: $(stanza).find('error').attr('code'),
  351. reason: $(stanza).find('error :first')[0].tagName,
  352. }:{};
  353. error.source = 'offer';
  354. JingleSessionPC.onJingleError(self.sid, error);
  355. },
  356. 10000);
  357. };
  358. sendJingle();
  359. }
  360. this.lasticecandidate = true;
  361. console.log('Have we encountered any srflx candidates? ' + this.hadstuncandidate);
  362. console.log('Have we encountered any relay candidates? ' + this.hadturncandidate);
  363. if (!(this.hadstuncandidate || this.hadturncandidate) && this.peerconnection.signalingState != 'closed') {
  364. $(document).trigger('nostuncandidates.jingle', [this.sid]);
  365. }
  366. }
  367. };
  368. JingleSessionPC.prototype.sendIceCandidates = function (candidates) {
  369. console.log('sendIceCandidates', candidates);
  370. var cand = $iq({to: this.peerjid, type: 'set'})
  371. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  372. action: 'transport-info',
  373. initiator: this.initiator,
  374. sid: this.sid});
  375. for (var mid = 0; mid < this.localSDP.media.length; mid++) {
  376. var cands = candidates.filter(function (el) { return el.sdpMLineIndex == mid; });
  377. var mline = SDPUtil.parse_mline(this.localSDP.media[mid].split('\r\n')[0]);
  378. if (cands.length > 0) {
  379. var ice = SDPUtil.iceparams(this.localSDP.media[mid], this.localSDP.session);
  380. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  381. cand.c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
  382. name: (cands[0].sdpMid? cands[0].sdpMid : mline.media)
  383. }).c('transport', ice);
  384. for (var i = 0; i < cands.length; i++) {
  385. cand.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
  386. }
  387. // add fingerprint
  388. if (SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session)) {
  389. var tmp = SDPUtil.parse_fingerprint(SDPUtil.find_line(this.localSDP.media[mid], 'a=fingerprint:', this.localSDP.session));
  390. tmp.required = true;
  391. cand.c(
  392. 'fingerprint',
  393. {xmlns: 'urn:xmpp:jingle:apps:dtls:0'})
  394. .t(tmp.fingerprint);
  395. delete tmp.fingerprint;
  396. cand.attrs(tmp);
  397. cand.up();
  398. }
  399. cand.up(); // transport
  400. cand.up(); // content
  401. }
  402. }
  403. // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
  404. //console.log('was this the last candidate', this.lasticecandidate);
  405. this.connection.sendIQ(cand,
  406. function () {
  407. var ack = {};
  408. ack.source = 'transportinfo';
  409. $(document).trigger('ack.jingle', [this.sid, ack]);
  410. },
  411. function (stanza) {
  412. var error = ($(stanza).find('error').length) ? {
  413. code: $(stanza).find('error').attr('code'),
  414. reason: $(stanza).find('error :first')[0].tagName,
  415. }:{};
  416. error.source = 'transportinfo';
  417. JingleSessionPC.onJingleError(this.sid, error);
  418. },
  419. 10000);
  420. };
  421. JingleSessionPC.prototype.sendOffer = function () {
  422. //console.log('sendOffer...');
  423. var self = this;
  424. this.peerconnection.createOffer(function (sdp) {
  425. self.createdOffer(sdp);
  426. },
  427. function (e) {
  428. console.error('createOffer failed', e);
  429. },
  430. this.media_constraints
  431. );
  432. };
  433. // FIXME createdOffer is never used in jitsi-meet
  434. JingleSessionPC.prototype.createdOffer = function (sdp) {
  435. //console.log('createdOffer', sdp);
  436. var self = this;
  437. this.localSDP = new SDP(sdp.sdp);
  438. //this.localSDP.mangle();
  439. var sendJingle = function () {
  440. var init = $iq({to: this.peerjid,
  441. type: 'set'})
  442. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  443. action: 'session-initiate',
  444. initiator: this.initiator,
  445. sid: this.sid});
  446. self.localSDP.toJingle(
  447. init,
  448. this.initiator == this.me ? 'initiator' : 'responder');
  449. self.connection.sendIQ(init,
  450. function () {
  451. var ack = {};
  452. ack.source = 'offer';
  453. $(document).trigger('ack.jingle', [self.sid, ack]);
  454. },
  455. function (stanza) {
  456. self.state = 'error';
  457. self.peerconnection.close();
  458. var error = ($(stanza).find('error').length) ? {
  459. code: $(stanza).find('error').attr('code'),
  460. reason: $(stanza).find('error :first')[0].tagName,
  461. }:{};
  462. error.source = 'offer';
  463. JingleSessionPC.onJingleError(self.sid, error);
  464. },
  465. 10000);
  466. };
  467. sdp.sdp = this.localSDP.raw;
  468. this.peerconnection.setLocalDescription(sdp,
  469. function () {
  470. if(self.usetrickle)
  471. {
  472. sendJingle();
  473. }
  474. self.setLocalDescription();
  475. //console.log('setLocalDescription success');
  476. },
  477. function (e) {
  478. console.error('setLocalDescription failed', e);
  479. self.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  480. }
  481. );
  482. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  483. for (var i = 0; i < cands.length; i++) {
  484. var cand = SDPUtil.parse_icecandidate(cands[i]);
  485. if (cand.type == 'srflx') {
  486. this.hadstuncandidate = true;
  487. } else if (cand.type == 'relay') {
  488. this.hadturncandidate = true;
  489. }
  490. }
  491. };
  492. JingleSessionPC.prototype.readSsrcInfo = function (contents) {
  493. var self = this;
  494. $(contents).each(function (idx, content) {
  495. var name = $(content).attr('name');
  496. var mediaType = this.getAttribute('name');
  497. var ssrcs = $(content).find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  498. ssrcs.each(function () {
  499. var ssrc = this.getAttribute('ssrc');
  500. $(this).find('>ssrc-info[xmlns="http://jitsi.org/jitmeet"]').each(
  501. function () {
  502. var owner = this.getAttribute('owner');
  503. self.ssrcOwners[ssrc] = owner;
  504. }
  505. );
  506. });
  507. });
  508. };
  509. JingleSessionPC.prototype.getSsrcOwner = function (ssrc) {
  510. return this.ssrcOwners[ssrc];
  511. };
  512. /**
  513. * Returns the SSRC of local audio stream.
  514. * @param mediaType 'audio' or 'video' media type
  515. * @returns {*} the SSRC number of local audio or video stream.
  516. */
  517. JingleSessionPC.prototype.getLocalSSRC = function (mediaType) {
  518. return this.localStreamsSSRC[mediaType];
  519. };
  520. JingleSessionPC.prototype.setRemoteDescription = function (elem, desctype) {
  521. this.remoteSDP = new SDP('');
  522. if (config.webrtcIceTcpDisable) {
  523. this.remoteSDP.removeTcpCandidates = true;
  524. }
  525. if (config.webrtcIceUdpDisable) {
  526. this.remoteSDP.removeUdpCandidates = true;
  527. }
  528. this.remoteSDP.fromJingle(elem);
  529. this.readSsrcInfo($(elem).find(">content"));
  530. if (this.peerconnection.remoteDescription !== null) {
  531. console.log('setRemoteDescription when remote description is not null, should be pranswer', this.peerconnection.remoteDescription);
  532. if (this.peerconnection.remoteDescription.type == 'pranswer') {
  533. var pranswer = new SDP(this.peerconnection.remoteDescription.sdp);
  534. for (var i = 0; i < pranswer.media.length; i++) {
  535. // make sure we have ice ufrag and pwd
  536. if (!SDPUtil.find_line(this.remoteSDP.media[i], 'a=ice-ufrag:', this.remoteSDP.session)) {
  537. if (SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session)) {
  538. this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session) + '\r\n';
  539. } else {
  540. console.warn('no ice ufrag?');
  541. }
  542. if (SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session)) {
  543. this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session) + '\r\n';
  544. } else {
  545. console.warn('no ice pwd?');
  546. }
  547. }
  548. // copy over candidates
  549. var lines = SDPUtil.find_lines(pranswer.media[i], 'a=candidate:');
  550. for (var j = 0; j < lines.length; j++) {
  551. this.remoteSDP.media[i] += lines[j] + '\r\n';
  552. }
  553. }
  554. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  555. }
  556. }
  557. var remotedesc = new RTCSessionDescription({type: desctype, sdp: this.remoteSDP.raw});
  558. this.peerconnection.setRemoteDescription(remotedesc,
  559. function () {
  560. //console.log('setRemoteDescription success');
  561. },
  562. function (e) {
  563. console.error('setRemoteDescription error', e);
  564. JingleSessionPC.onJingleFatalError(self, e);
  565. }
  566. );
  567. };
  568. /**
  569. * Adds remote ICE candidates to this Jingle session.
  570. * @param elem An array of Jingle "content" elements?
  571. */
  572. JingleSessionPC.prototype.addIceCandidate = function (elem) {
  573. var self = this;
  574. if (this.peerconnection.signalingState == 'closed') {
  575. return;
  576. }
  577. if (!this.peerconnection.remoteDescription && this.peerconnection.signalingState == 'have-local-offer') {
  578. console.log('trickle ice candidate arriving before session accept...');
  579. // create a PRANSWER for setRemoteDescription
  580. if (!this.remoteSDP) {
  581. var cobbled = 'v=0\r\n' +
  582. 'o=- 1923518516 2 IN IP4 0.0.0.0\r\n' +// FIXME
  583. 's=-\r\n' +
  584. 't=0 0\r\n';
  585. // first, take some things from the local description
  586. for (var i = 0; i < this.localSDP.media.length; i++) {
  587. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'm=') + '\r\n';
  588. cobbled += SDPUtil.find_lines(this.localSDP.media[i], 'a=rtpmap:').join('\r\n') + '\r\n';
  589. if (SDPUtil.find_line(this.localSDP.media[i], 'a=mid:')) {
  590. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'a=mid:') + '\r\n';
  591. }
  592. cobbled += 'a=inactive\r\n';
  593. }
  594. this.remoteSDP = new SDP(cobbled);
  595. }
  596. // then add things like ice and dtls from remote candidate
  597. elem.each(function () {
  598. for (var i = 0; i < self.remoteSDP.media.length; i++) {
  599. if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  600. self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  601. if (!SDPUtil.find_line(self.remoteSDP.media[i], 'a=ice-ufrag:')) {
  602. var tmp = $(this).find('transport');
  603. self.remoteSDP.media[i] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  604. self.remoteSDP.media[i] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  605. tmp = $(this).find('transport>fingerprint');
  606. if (tmp.length) {
  607. self.remoteSDP.media[i] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  608. } else {
  609. console.log('no dtls fingerprint (webrtc issue #1718?)');
  610. self.remoteSDP.media[i] += 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:BAADBAADBAADBAADBAADBAADBAADBAADBAADBAAD\r\n';
  611. }
  612. break;
  613. }
  614. }
  615. }
  616. });
  617. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  618. // we need a complete SDP with ice-ufrag/ice-pwd in all parts
  619. // this makes the assumption that the PRANSWER is constructed such that the ice-ufrag is in all mediaparts
  620. // but it could be in the session part as well. since the code above constructs this sdp this can't happen however
  621. var iscomplete = this.remoteSDP.media.filter(function (mediapart) {
  622. return SDPUtil.find_line(mediapart, 'a=ice-ufrag:');
  623. }).length == this.remoteSDP.media.length;
  624. if (iscomplete) {
  625. console.log('setting pranswer');
  626. try {
  627. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'pranswer', sdp: this.remoteSDP.raw }),
  628. function() {
  629. },
  630. function(e) {
  631. console.log('setRemoteDescription pranswer failed', e.toString());
  632. });
  633. } catch (e) {
  634. console.error('setting pranswer failed', e);
  635. }
  636. } else {
  637. //console.log('not yet setting pranswer');
  638. }
  639. }
  640. // operate on each content element
  641. elem.each(function () {
  642. // would love to deactivate this, but firefox still requires it
  643. var idx = -1;
  644. var i;
  645. for (i = 0; i < self.remoteSDP.media.length; i++) {
  646. if (SDPUtil.find_line(self.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  647. self.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  648. idx = i;
  649. break;
  650. }
  651. }
  652. if (idx == -1) { // fall back to localdescription
  653. for (i = 0; i < self.localSDP.media.length; i++) {
  654. if (SDPUtil.find_line(self.localSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  655. self.localSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  656. idx = i;
  657. break;
  658. }
  659. }
  660. }
  661. var name = $(this).attr('name');
  662. // TODO: check ice-pwd and ice-ufrag?
  663. $(this).find('transport>candidate').each(function () {
  664. var line, candidate;
  665. var protocol = this.getAttribute('protocol');
  666. protocol =
  667. (typeof protocol === 'string') ? protocol.toLowerCase() : '';
  668. if ((config.webrtcIceTcpDisable && protocol == 'tcp') ||
  669. (config.webrtcIceUdpDisable && protocol == 'udp')) {
  670. return;
  671. }
  672. line = SDPUtil.candidateFromJingle(this);
  673. candidate = new RTCIceCandidate({sdpMLineIndex: idx,
  674. sdpMid: name,
  675. candidate: line});
  676. try {
  677. self.peerconnection.addIceCandidate(candidate);
  678. } catch (e) {
  679. console.error('addIceCandidate failed', e.toString(), line);
  680. self.eventEmitter.emit(RTCEvents.ADD_ICE_CANDIDATE_FAILED, err, self.peerconnection);
  681. }
  682. });
  683. });
  684. };
  685. JingleSessionPC.prototype.sendAnswer = function (provisional) {
  686. //console.log('createAnswer', provisional);
  687. var self = this;
  688. this.peerconnection.createAnswer(
  689. function (sdp) {
  690. self.createdAnswer(sdp, provisional);
  691. },
  692. function (e) {
  693. console.error('createAnswer failed', e);
  694. self.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  695. },
  696. this.media_constraints
  697. );
  698. };
  699. JingleSessionPC.prototype.createdAnswer = function (sdp, provisional) {
  700. //console.log('createAnswer callback');
  701. var self = this;
  702. this.localSDP = new SDP(sdp.sdp);
  703. //this.localSDP.mangle();
  704. this.usepranswer = provisional === true;
  705. if (this.usetrickle) {
  706. if (this.usepranswer) {
  707. sdp.type = 'pranswer';
  708. for (var i = 0; i < this.localSDP.media.length; i++) {
  709. this.localSDP.media[i] = this.localSDP.media[i].replace('a=sendrecv\r\n', 'a=inactive\r\n');
  710. }
  711. this.localSDP.raw = this.localSDP.session + '\r\n' + this.localSDP.media.join('');
  712. }
  713. }
  714. var sendJingle = function (ssrcs) {
  715. // FIXME why do we generate session-accept in 3 different places ?
  716. var accept = $iq({to: self.peerjid,
  717. type: 'set'})
  718. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  719. action: 'session-accept',
  720. initiator: self.initiator,
  721. responder: self.responder,
  722. sid: self.sid });
  723. if (config.webrtcIceTcpDisable) {
  724. self.localSDP.removeTcpCandidates = true;
  725. }
  726. if (config.webrtcIceUdpDisable) {
  727. self.localSDP.removeUdpCandidates = true;
  728. }
  729. self.localSDP.toJingle(
  730. accept,
  731. self.initiator == self.me ? 'initiator' : 'responder',
  732. ssrcs);
  733. self.connection.sendIQ(accept,
  734. function () {
  735. var ack = {};
  736. ack.source = 'answer';
  737. $(document).trigger('ack.jingle', [self.sid, ack]);
  738. },
  739. function (stanza) {
  740. var error = ($(stanza).find('error').length) ? {
  741. code: $(stanza).find('error').attr('code'),
  742. reason: $(stanza).find('error :first')[0].tagName,
  743. }:{};
  744. error.source = 'answer';
  745. JingleSessionPC.onJingleError(self.sid, error);
  746. },
  747. 10000);
  748. };
  749. sdp.sdp = this.localSDP.raw;
  750. this.peerconnection.setLocalDescription(sdp,
  751. function () {
  752. //console.log('setLocalDescription success');
  753. if (self.usetrickle && !self.usepranswer) {
  754. sendJingle();
  755. }
  756. self.setLocalDescription();
  757. },
  758. function (e) {
  759. console.error('setLocalDescription failed', e);
  760. self.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  761. }
  762. );
  763. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  764. for (var j = 0; j < cands.length; j++) {
  765. var cand = SDPUtil.parse_icecandidate(cands[j]);
  766. if (cand.type == 'srflx') {
  767. this.hadstuncandidate = true;
  768. } else if (cand.type == 'relay') {
  769. this.hadturncandidate = true;
  770. }
  771. }
  772. };
  773. JingleSessionPC.prototype.sendTerminate = function (reason, text) {
  774. var self = this,
  775. term = $iq({to: this.peerjid,
  776. type: 'set'})
  777. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  778. action: 'session-terminate',
  779. initiator: this.initiator,
  780. sid: this.sid})
  781. .c('reason')
  782. .c(reason || 'success');
  783. if (text) {
  784. term.up().c('text').t(text);
  785. }
  786. this.connection.sendIQ(term,
  787. function () {
  788. self.peerconnection.close();
  789. self.peerconnection = null;
  790. self.terminate();
  791. var ack = {};
  792. ack.source = 'terminate';
  793. $(document).trigger('ack.jingle', [self.sid, ack]);
  794. },
  795. function (stanza) {
  796. var error = ($(stanza).find('error').length) ? {
  797. code: $(stanza).find('error').attr('code'),
  798. reason: $(stanza).find('error :first')[0].tagName,
  799. }:{};
  800. $(document).trigger('ack.jingle', [self.sid, error]);
  801. },
  802. 10000);
  803. if (this.statsinterval !== null) {
  804. window.clearInterval(this.statsinterval);
  805. this.statsinterval = null;
  806. }
  807. };
  808. /**
  809. * Handles a Jingle source-add message for this Jingle session.
  810. * @param elem An array of Jingle "content" elements.
  811. */
  812. JingleSessionPC.prototype.addSource = function (elem) {
  813. var self = this;
  814. // FIXME: dirty waiting
  815. if (!this.peerconnection.localDescription) {
  816. console.warn("addSource - localDescription not ready yet");
  817. setTimeout(function()
  818. {
  819. self.addSource(elem);
  820. },
  821. 200
  822. );
  823. return;
  824. }
  825. console.log('addssrc', new Date().getTime());
  826. console.log('ice', this.peerconnection.iceConnectionState);
  827. this.readSsrcInfo(elem);
  828. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  829. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  830. $(elem).each(function (idx, content) {
  831. var name = $(content).attr('name');
  832. var lines = '';
  833. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  834. var semantics = this.getAttribute('semantics');
  835. var ssrcs = $(this).find('>source').map(function () {
  836. return this.getAttribute('ssrc');
  837. }).get();
  838. if (ssrcs.length) {
  839. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  840. }
  841. });
  842. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  843. tmp.each(function () {
  844. var ssrc = $(this).attr('ssrc');
  845. if(mySdp.containsSSRC(ssrc)){
  846. /**
  847. * This happens when multiple participants change their streams at the same time and
  848. * ColibriFocus.modifySources have to wait for stable state. In the meantime multiple
  849. * addssrc are scheduled for update IQ. See
  850. */
  851. console.warn("Got add stream request for my own ssrc: "+ssrc);
  852. return;
  853. }
  854. if (sdp.containsSSRC(ssrc)) {
  855. console.warn("Source-add request for existing SSRC: " + ssrc);
  856. return;
  857. }
  858. $(this).find('>parameter').each(function () {
  859. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  860. if ($(this).attr('value') && $(this).attr('value').length)
  861. lines += ':' + $(this).attr('value');
  862. lines += '\r\n';
  863. });
  864. });
  865. sdp.media.forEach(function(media, idx) {
  866. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  867. return;
  868. sdp.media[idx] += lines;
  869. if (!self.addssrc[idx]) self.addssrc[idx] = '';
  870. self.addssrc[idx] += lines;
  871. });
  872. sdp.raw = sdp.session + sdp.media.join('');
  873. });
  874. this.modifySourcesQueue.push(function() {
  875. // When a source is added and if this is FF, a new channel is allocated
  876. // for receiving the added source. We need to diffuse the SSRC of this
  877. // new recvonly channel to the rest of the peers.
  878. console.log('modify sources done');
  879. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  880. console.log("SDPs", mySdp, newSdp);
  881. self.notifyMySSRCUpdate(mySdp, newSdp);
  882. });
  883. };
  884. /**
  885. * Handles a Jingle source-remove message for this Jingle session.
  886. * @param elem An array of Jingle "content" elements.
  887. */
  888. JingleSessionPC.prototype.removeSource = function (elem) {
  889. var self = this;
  890. // FIXME: dirty waiting
  891. if (!this.peerconnection.localDescription) {
  892. console.warn("removeSource - localDescription not ready yet");
  893. setTimeout(function() {
  894. self.removeSource(elem);
  895. },
  896. 200
  897. );
  898. return;
  899. }
  900. console.log('removessrc', new Date().getTime());
  901. console.log('ice', this.peerconnection.iceConnectionState);
  902. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  903. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  904. $(elem).each(function (idx, content) {
  905. var name = $(content).attr('name');
  906. var lines = '';
  907. $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  908. var semantics = this.getAttribute('semantics');
  909. var ssrcs = $(this).find('>source').map(function () {
  910. return this.getAttribute('ssrc');
  911. }).get();
  912. if (ssrcs.length) {
  913. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  914. }
  915. });
  916. var tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  917. tmp.each(function () {
  918. var ssrc = $(this).attr('ssrc');
  919. // This should never happen, but can be useful for bug detection
  920. if(mySdp.containsSSRC(ssrc)){
  921. console.error("Got remove stream request for my own ssrc: "+ssrc);
  922. return;
  923. }
  924. $(this).find('>parameter').each(function () {
  925. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  926. if ($(this).attr('value') && $(this).attr('value').length)
  927. lines += ':' + $(this).attr('value');
  928. lines += '\r\n';
  929. });
  930. });
  931. sdp.media.forEach(function(media, idx) {
  932. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  933. return;
  934. sdp.media[idx] += lines;
  935. if (!self.removessrc[idx]) self.removessrc[idx] = '';
  936. self.removessrc[idx] += lines;
  937. });
  938. sdp.raw = sdp.session + sdp.media.join('');
  939. });
  940. this.modifySourcesQueue.push(function() {
  941. // When a source is removed and if this is FF, the recvonly channel that
  942. // receives the remote stream is deactivated . We need to diffuse the
  943. // recvonly SSRC removal to the rest of the peers.
  944. console.log('modify sources done');
  945. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  946. console.log("SDPs", mySdp, newSdp);
  947. self.notifyMySSRCUpdate(mySdp, newSdp);
  948. });
  949. };
  950. JingleSessionPC.prototype._modifySources = function (successCallback, queueCallback) {
  951. var self = this;
  952. if (this.peerconnection.signalingState == 'closed') return;
  953. if (!(this.addssrc.length || this.removessrc.length || this.pendingop !== null || this.switchstreams)){
  954. // There is nothing to do since scheduled job might have been executed by another succeeding call
  955. this.setLocalDescription();
  956. if(successCallback){
  957. successCallback();
  958. }
  959. queueCallback();
  960. return;
  961. }
  962. // Reset switch streams flag
  963. this.switchstreams = false;
  964. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  965. // add sources
  966. this.addssrc.forEach(function(lines, idx) {
  967. sdp.media[idx] += lines;
  968. });
  969. this.addssrc = [];
  970. // remove sources
  971. this.removessrc.forEach(function(lines, idx) {
  972. lines = lines.split('\r\n');
  973. lines.pop(); // remove empty last element;
  974. lines.forEach(function(line) {
  975. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  976. });
  977. });
  978. this.removessrc = [];
  979. sdp.raw = sdp.session + sdp.media.join('');
  980. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  981. function() {
  982. if(self.signalingState == 'closed') {
  983. console.error("createAnswer attempt on closed state");
  984. queueCallback("createAnswer attempt on closed state");
  985. return;
  986. }
  987. self.peerconnection.createAnswer(
  988. function(modifiedAnswer) {
  989. // change video direction, see https://github.com/jitsi/jitmeet/issues/41
  990. if (self.pendingop !== null) {
  991. var sdp = new SDP(modifiedAnswer.sdp);
  992. if (sdp.media.length > 1) {
  993. switch(self.pendingop) {
  994. case 'mute':
  995. sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
  996. break;
  997. case 'unmute':
  998. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  999. break;
  1000. }
  1001. sdp.raw = sdp.session + sdp.media.join('');
  1002. modifiedAnswer.sdp = sdp.raw;
  1003. }
  1004. self.pendingop = null;
  1005. }
  1006. // FIXME: pushing down an answer while ice connection state
  1007. // is still checking is bad...
  1008. //console.log(self.peerconnection.iceConnectionState);
  1009. // trying to work around another chrome bug
  1010. //modifiedAnswer.sdp = modifiedAnswer.sdp.replace(/a=setup:active/g, 'a=setup:actpass');
  1011. self.peerconnection.setLocalDescription(modifiedAnswer,
  1012. function() {
  1013. //console.log('modified setLocalDescription ok');
  1014. self.setLocalDescription();
  1015. if(successCallback){
  1016. successCallback();
  1017. }
  1018. queueCallback();
  1019. },
  1020. function(error) {
  1021. console.error('modified setLocalDescription failed', error);
  1022. queueCallback(error);
  1023. }
  1024. );
  1025. },
  1026. function(error) {
  1027. console.error('modified answer failed', error);
  1028. queueCallback(error);
  1029. }
  1030. );
  1031. },
  1032. function(error) {
  1033. console.error('modify failed', error);
  1034. queueCallback(error);
  1035. }
  1036. );
  1037. };
  1038. /**
  1039. * Switches video streams.
  1040. * @param newStream new stream that will be used as video of this session.
  1041. * @param oldStream old video stream of this session.
  1042. * @param successCallback callback executed after successful stream switch.
  1043. * @param isAudio whether the streams are audio (if true) or video (if false).
  1044. */
  1045. JingleSessionPC.prototype.switchStreams =
  1046. function (newStream, oldStream, successCallback, isAudio) {
  1047. var self = this;
  1048. var sender, newTrack;
  1049. var senderKind = isAudio ? 'audio' : 'video';
  1050. // Remember SDP to figure out added/removed SSRCs
  1051. var oldSdp = null;
  1052. if (self.peerconnection) {
  1053. if (self.peerconnection.localDescription) {
  1054. oldSdp = new SDP(self.peerconnection.localDescription.sdp);
  1055. }
  1056. if (RTCBrowserType.getBrowserType() ===
  1057. RTCBrowserType.RTC_BROWSER_FIREFOX) {
  1058. // On Firefox we don't replace MediaStreams as this messes up the
  1059. // m-lines (which can't be removed in Plan Unified) and brings a lot
  1060. // of complications. Instead, we use the RTPSender and replace just
  1061. // the track.
  1062. // Find the right sender (for audio or video)
  1063. self.peerconnection.peerconnection.getSenders().some(function (s) {
  1064. if (s.track && s.track.kind === senderKind) {
  1065. sender = s;
  1066. return true;
  1067. }
  1068. });
  1069. if (sender) {
  1070. // We assume that our streams have a single track, either audio
  1071. // or video.
  1072. newTrack = isAudio ? newStream.getAudioTracks()[0] :
  1073. newStream.getVideoTracks()[0];
  1074. sender.replaceTrack(newTrack)
  1075. .then(function() {
  1076. console.log("Replaced a track, isAudio=" + isAudio);
  1077. })
  1078. .catch(function(err) {
  1079. console.log("Failed to replace a track: " + err);
  1080. });
  1081. } else {
  1082. console.log("Cannot switch tracks: no RTPSender.");
  1083. }
  1084. } else {
  1085. self.peerconnection.removeStream(oldStream, true);
  1086. if (newStream) {
  1087. self.peerconnection.addStream(newStream);
  1088. }
  1089. }
  1090. }
  1091. // Conference is not active
  1092. if (!oldSdp) {
  1093. successCallback();
  1094. return;
  1095. }
  1096. self.switchstreams = true;
  1097. self.modifySourcesQueue.push(function() {
  1098. console.log('modify sources done');
  1099. successCallback();
  1100. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  1101. console.log("SDPs", oldSdp, newSdp);
  1102. self.notifyMySSRCUpdate(oldSdp, newSdp);
  1103. });
  1104. };
  1105. /**
  1106. * Figures out added/removed ssrcs and send update IQs.
  1107. * @param old_sdp SDP object for old description.
  1108. * @param new_sdp SDP object for new description.
  1109. */
  1110. JingleSessionPC.prototype.notifyMySSRCUpdate = function (old_sdp, new_sdp) {
  1111. if (!(this.peerconnection.signalingState == 'stable' &&
  1112. this.peerconnection.iceConnectionState == 'connected')){
  1113. console.log("Too early to send updates");
  1114. return;
  1115. }
  1116. // send source-remove IQ.
  1117. sdpDiffer = new SDPDiffer(new_sdp, old_sdp);
  1118. var remove = $iq({to: this.peerjid, type: 'set'})
  1119. .c('jingle', {
  1120. xmlns: 'urn:xmpp:jingle:1',
  1121. action: 'source-remove',
  1122. initiator: this.initiator,
  1123. sid: this.sid
  1124. }
  1125. );
  1126. var removed = sdpDiffer.toJingle(remove);
  1127. if (removed && remove) {
  1128. console.info("Sending source-remove", remove);
  1129. this.connection.sendIQ(remove,
  1130. function (res) {
  1131. console.info('got remove result', res);
  1132. },
  1133. function (err) {
  1134. console.error('got remove error', err);
  1135. }
  1136. );
  1137. } else {
  1138. console.log('removal not necessary');
  1139. }
  1140. // send source-add IQ.
  1141. var sdpDiffer = new SDPDiffer(old_sdp, new_sdp);
  1142. var add = $iq({to: this.peerjid, type: 'set'})
  1143. .c('jingle', {
  1144. xmlns: 'urn:xmpp:jingle:1',
  1145. action: 'source-add',
  1146. initiator: this.initiator,
  1147. sid: this.sid
  1148. }
  1149. );
  1150. var added = sdpDiffer.toJingle(add);
  1151. if (added && add) {
  1152. console.info("Sending source-add", add);
  1153. this.connection.sendIQ(add,
  1154. function (res) {
  1155. console.info('got add result', res);
  1156. },
  1157. function (err) {
  1158. console.error('got add error', err);
  1159. }
  1160. );
  1161. } else {
  1162. console.log('addition not necessary');
  1163. }
  1164. };
  1165. /**
  1166. * Mutes/unmutes the (local) video i.e. enables/disables all video tracks.
  1167. *
  1168. * @param mute <tt>true</tt> to mute the (local) video i.e. to disable all video
  1169. * tracks; otherwise, <tt>false</tt>
  1170. * @param callback a function to be invoked with <tt>mute</tt> after all video
  1171. * tracks have been enabled/disabled. The function may, optionally, return
  1172. * another function which is to be invoked after the whole mute/unmute operation
  1173. * has completed successfully.
  1174. * @param options an object which specifies optional arguments such as the
  1175. * <tt>boolean</tt> key <tt>byUser</tt> with default value <tt>true</tt> which
  1176. * specifies whether the method was initiated in response to a user command (in
  1177. * contrast to an automatic decision made by the application logic)
  1178. */
  1179. JingleSessionPC.prototype.setVideoMute = function (mute, callback, options) {
  1180. var byUser;
  1181. if (options) {
  1182. byUser = options.byUser;
  1183. if (typeof byUser === 'undefined') {
  1184. byUser = true;
  1185. }
  1186. } else {
  1187. byUser = true;
  1188. }
  1189. // The user's command to mute the (local) video takes precedence over any
  1190. // automatic decision made by the application logic.
  1191. if (byUser) {
  1192. this.videoMuteByUser = mute;
  1193. } else if (this.videoMuteByUser) {
  1194. return;
  1195. }
  1196. this.hardMuteVideo(mute);
  1197. var self = this;
  1198. var oldSdp = null;
  1199. if(self.peerconnection) {
  1200. if(self.peerconnection.localDescription) {
  1201. oldSdp = new SDP(self.peerconnection.localDescription.sdp);
  1202. }
  1203. }
  1204. this.modifySourcesQueue.push(function() {
  1205. console.log('modify sources done');
  1206. callback(mute);
  1207. var newSdp = new SDP(self.peerconnection.localDescription.sdp);
  1208. console.log("SDPs", oldSdp, newSdp);
  1209. self.notifyMySSRCUpdate(oldSdp, newSdp);
  1210. });
  1211. };
  1212. JingleSessionPC.prototype.hardMuteVideo = function (muted) {
  1213. this.pendingop = muted ? 'mute' : 'unmute';
  1214. };
  1215. JingleSessionPC.prototype.sendMute = function (muted, content) {
  1216. var info = $iq({to: this.peerjid,
  1217. type: 'set'})
  1218. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1219. action: 'session-info',
  1220. initiator: this.initiator,
  1221. sid: this.sid });
  1222. info.c(muted ? 'mute' : 'unmute', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  1223. info.attrs({'creator': this.me == this.initiator ? 'creator' : 'responder'});
  1224. if (content) {
  1225. info.attrs({'name': content});
  1226. }
  1227. this.connection.send(info);
  1228. };
  1229. JingleSessionPC.prototype.sendRinging = function () {
  1230. var info = $iq({to: this.peerjid,
  1231. type: 'set'})
  1232. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1233. action: 'session-info',
  1234. initiator: this.initiator,
  1235. sid: this.sid });
  1236. info.c('ringing', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  1237. this.connection.send(info);
  1238. };
  1239. JingleSessionPC.prototype.getStats = function (interval) {
  1240. var self = this;
  1241. var recv = {audio: 0, video: 0};
  1242. var lost = {audio: 0, video: 0};
  1243. var lastrecv = {audio: 0, video: 0};
  1244. var lastlost = {audio: 0, video: 0};
  1245. var loss = {audio: 0, video: 0};
  1246. var delta = {audio: 0, video: 0};
  1247. this.statsinterval = window.setInterval(function () {
  1248. if (self && self.peerconnection && self.peerconnection.getStats) {
  1249. self.peerconnection.getStats(function (stats) {
  1250. var results = stats.result();
  1251. // TODO: there are so much statistics you can get from this..
  1252. for (var i = 0; i < results.length; ++i) {
  1253. if (results[i].type == 'ssrc') {
  1254. var packetsrecv = results[i].stat('packetsReceived');
  1255. var packetslost = results[i].stat('packetsLost');
  1256. if (packetsrecv && packetslost) {
  1257. packetsrecv = parseInt(packetsrecv, 10);
  1258. packetslost = parseInt(packetslost, 10);
  1259. if (results[i].stat('googFrameRateReceived')) {
  1260. lastlost.video = lost.video;
  1261. lastrecv.video = recv.video;
  1262. recv.video = packetsrecv;
  1263. lost.video = packetslost;
  1264. } else {
  1265. lastlost.audio = lost.audio;
  1266. lastrecv.audio = recv.audio;
  1267. recv.audio = packetsrecv;
  1268. lost.audio = packetslost;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. delta.audio = recv.audio - lastrecv.audio;
  1274. delta.video = recv.video - lastrecv.video;
  1275. loss.audio = (delta.audio > 0) ? Math.ceil(100 * (lost.audio - lastlost.audio) / delta.audio) : 0;
  1276. loss.video = (delta.video > 0) ? Math.ceil(100 * (lost.video - lastlost.video) / delta.video) : 0;
  1277. $(document).trigger('packetloss.jingle', [self.sid, loss]);
  1278. });
  1279. }
  1280. }, interval || 3000);
  1281. return this.statsinterval;
  1282. };
  1283. JingleSessionPC.onJingleError = function (session, error)
  1284. {
  1285. console.error("Jingle error", error);
  1286. };
  1287. JingleSessionPC.onJingleFatalError = function (session, error)
  1288. {
  1289. this.service.sessionTerminated = true;
  1290. this.connection.emuc.doLeave();
  1291. this.eventEmitter.emit(XMPPEvents.CONFERENCE_SETUP_FAILED);
  1292. this.eventEmitter.emit(XMPPEvents.JINGLE_FATAL_ERROR, session, error);
  1293. };
  1294. JingleSessionPC.prototype.setLocalDescription = function () {
  1295. var self = this;
  1296. var newssrcs = [];
  1297. var session = transform.parse(this.peerconnection.localDescription.sdp);
  1298. var i;
  1299. session.media.forEach(function (media) {
  1300. if (media.ssrcs && media.ssrcs.length > 0) {
  1301. // TODO(gp) maybe exclude FID streams?
  1302. media.ssrcs.forEach(function (ssrc) {
  1303. if (ssrc.attribute !== 'cname') {
  1304. return;
  1305. }
  1306. newssrcs.push({
  1307. 'ssrc': ssrc.id,
  1308. 'type': media.type
  1309. });
  1310. // In FF we have multiple local SSRC per media type, 1 that is
  1311. // sending and some that are receive only. The
  1312. // localStramsSSRC['audio'] needs to be set to the one that is
  1313. // sending! We find it by checking for an msid. Note that
  1314. // self.localStreamsSSRC is primarily used by the tests atm.
  1315. var isSending = media.ssrcs.some(function (ssrc$1) {
  1316. return ssrc$1.id == ssrc.id && ssrc$1.attribute == 'msid';
  1317. });
  1318. if (!isSending) {
  1319. return;
  1320. }
  1321. // FIXME allows for only one SSRC per media type
  1322. self.localStreamsSSRC[media.type] = ssrc.id;
  1323. });
  1324. }
  1325. });
  1326. console.log('new ssrcs', newssrcs);
  1327. // Bind us as local SSRCs owner
  1328. if (newssrcs.length > 0) {
  1329. if (config.advertiseSSRCsInPresence) {
  1330. // This is only for backward compatibility with clients which
  1331. // don't support getting sources from Jingle (i.e. jirecon).
  1332. this.connection.emuc.clearPresenceMedia();
  1333. }
  1334. for (i = 0; i < newssrcs.length; i++) {
  1335. var ssrc = newssrcs[i].ssrc;
  1336. var myJid = self.connection.emuc.myroomjid;
  1337. self.ssrcOwners[ssrc] = myJid;
  1338. if (config.advertiseSSRCsInPresence) {
  1339. // This is only for backward compatibility with clients which
  1340. // don't support getting sources from Jingle (i.e. jirecon).
  1341. this.connection.emuc.addMediaToPresence(
  1342. i+1, newssrcs[i].type, ssrc, newssrcs[i].direction);
  1343. }
  1344. }
  1345. if (config.advertiseSSRCsInPresence) {
  1346. this.connection.emuc.sendPresence();
  1347. }
  1348. }
  1349. };
  1350. /**
  1351. * Handles 'onaddstream' events from the RTCPeerConnection.
  1352. * @param event the 'onaddstream' event.
  1353. */
  1354. JingleSessionPC.prototype.remoteStreamAdded = function (event) {
  1355. var self = this;
  1356. var ssrc;
  1357. var ssrclines;
  1358. var streamId = APP.RTC.getStreamID(event.stream);
  1359. // look up an associated JID for a stream id
  1360. if (!streamId) {
  1361. console.error("No stream ID for", event.stream);
  1362. } else if (streamId.indexOf('mixedmslabel') === -1) {
  1363. // look only at a=ssrc: and _not_ at a=ssrc-group: lines
  1364. ssrclines = SDPUtil.find_lines(
  1365. this.peerconnection.remoteDescription.sdp,
  1366. 'a=ssrc:');
  1367. ssrclines = ssrclines.filter(function (line) {
  1368. // NOTE(gp) previously we filtered on the mslabel, but that property
  1369. // is not always present.
  1370. // return line.indexOf('mslabel:' + event.stream.label) !== -1;
  1371. if (RTCBrowserType.isTemasysPluginUsed()) {
  1372. return ((line.indexOf('mslabel:' + streamId) !== -1));
  1373. } else {
  1374. return ((line.indexOf('msid:' + streamId) !== -1));
  1375. }
  1376. });
  1377. if (ssrclines.length) {
  1378. ssrc = ssrclines[0].substring(7).split(' ')[0];
  1379. if (!self.ssrcOwners[ssrc]) {
  1380. console.error("No SSRC owner known for: " + ssrc);
  1381. return;
  1382. }
  1383. event.peerjid = self.ssrcOwners[ssrc];
  1384. console.log('Adding remote stream, SSRC ' + ssrc +
  1385. ', associated jid ' + event.peerjid);
  1386. } else {
  1387. console.error("No SSRC lines for ", streamId);
  1388. }
  1389. }
  1390. APP.RTC.createRemoteStream(event, ssrc);
  1391. };
  1392. module.exports = JingleSessionPC;