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 55KB

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