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.

strophe.jingle.adapter.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. function TraceablePeerConnection(ice_config, constraints) {
  2. var self = this;
  3. var RTCPeerconnection = navigator.mozGetUserMedia ? mozRTCPeerConnection : webkitRTCPeerConnection;
  4. this.peerconnection = new RTCPeerconnection(ice_config, constraints);
  5. this.updateLog = [];
  6. this.stats = {};
  7. this.statsinterval = null;
  8. this.maxstats = 0; // limit to 300 values, i.e. 5 minutes; set to 0 to disable
  9. /**
  10. * Array of ssrcs that will be added on next modifySources call.
  11. * @type {Array}
  12. */
  13. this.addssrc = [];
  14. /**
  15. * Array of ssrcs that will be added on next modifySources call.
  16. * @type {Array}
  17. */
  18. this.removessrc = [];
  19. /**
  20. * Pending operation that will be done during modifySources call.
  21. * Currently 'mute'/'unmute' operations are supported.
  22. *
  23. * @type {String}
  24. */
  25. this.pendingop = null;
  26. /**
  27. * Flag indicates that peer connection stream have changed and modifySources should proceed.
  28. * @type {boolean}
  29. */
  30. this.switchstreams = false;
  31. // override as desired
  32. this.trace = function (what, info) {
  33. //console.warn('WTRACE', what, info);
  34. self.updateLog.push({
  35. time: new Date(),
  36. type: what,
  37. value: info || ""
  38. });
  39. };
  40. this.onicecandidate = null;
  41. this.peerconnection.onicecandidate = function (event) {
  42. self.trace('onicecandidate', JSON.stringify(event.candidate, null, ' '));
  43. if (self.onicecandidate !== null) {
  44. self.onicecandidate(event);
  45. }
  46. };
  47. this.onaddstream = null;
  48. this.peerconnection.onaddstream = function (event) {
  49. self.trace('onaddstream', event.stream.id);
  50. if (self.onaddstream !== null) {
  51. self.onaddstream(event);
  52. }
  53. };
  54. this.onremovestream = null;
  55. this.peerconnection.onremovestream = function (event) {
  56. self.trace('onremovestream', event.stream.id);
  57. if (self.onremovestream !== null) {
  58. self.onremovestream(event);
  59. }
  60. };
  61. this.onsignalingstatechange = null;
  62. this.peerconnection.onsignalingstatechange = function (event) {
  63. self.trace('onsignalingstatechange', self.signalingState);
  64. if (self.onsignalingstatechange !== null) {
  65. self.onsignalingstatechange(event);
  66. }
  67. };
  68. this.oniceconnectionstatechange = null;
  69. this.peerconnection.oniceconnectionstatechange = function (event) {
  70. self.trace('oniceconnectionstatechange', self.iceConnectionState);
  71. if (self.oniceconnectionstatechange !== null) {
  72. self.oniceconnectionstatechange(event);
  73. }
  74. };
  75. this.onnegotiationneeded = null;
  76. this.peerconnection.onnegotiationneeded = function (event) {
  77. self.trace('onnegotiationneeded');
  78. if (self.onnegotiationneeded !== null) {
  79. self.onnegotiationneeded(event);
  80. }
  81. };
  82. self.ondatachannel = null;
  83. this.peerconnection.ondatachannel = function (event) {
  84. self.trace('ondatachannel', event);
  85. if (self.ondatachannel !== null) {
  86. self.ondatachannel(event);
  87. }
  88. };
  89. if (!navigator.mozGetUserMedia && this.maxstats) {
  90. this.statsinterval = window.setInterval(function() {
  91. self.peerconnection.getStats(function(stats) {
  92. var results = stats.result();
  93. for (var i = 0; i < results.length; ++i) {
  94. //console.log(results[i].type, results[i].id, results[i].names())
  95. var now = new Date();
  96. results[i].names().forEach(function (name) {
  97. var id = results[i].id + '-' + name;
  98. if (!self.stats[id]) {
  99. self.stats[id] = {
  100. startTime: now,
  101. endTime: now,
  102. values: [],
  103. times: []
  104. };
  105. }
  106. self.stats[id].values.push(results[i].stat(name));
  107. self.stats[id].times.push(now.getTime());
  108. if (self.stats[id].values.length > self.maxstats) {
  109. self.stats[id].values.shift();
  110. self.stats[id].times.shift();
  111. }
  112. self.stats[id].endTime = now;
  113. });
  114. }
  115. });
  116. }, 1000);
  117. }
  118. };
  119. dumpSDP = function(description) {
  120. return 'type: ' + description.type + '\r\n' + description.sdp;
  121. }
  122. if (TraceablePeerConnection.prototype.__defineGetter__ !== undefined) {
  123. TraceablePeerConnection.prototype.__defineGetter__('signalingState', function() { return this.peerconnection.signalingState; });
  124. TraceablePeerConnection.prototype.__defineGetter__('iceConnectionState', function() { return this.peerconnection.iceConnectionState; });
  125. TraceablePeerConnection.prototype.__defineGetter__('localDescription', function() {
  126. var publicLocalDescription = simulcast.reverseTransformLocalDescription(this.peerconnection.localDescription);
  127. return publicLocalDescription;
  128. });
  129. TraceablePeerConnection.prototype.__defineGetter__('remoteDescription', function() {
  130. var publicRemoteDescription = simulcast.reverseTransformRemoteDescription(this.peerconnection.remoteDescription);
  131. return publicRemoteDescription;
  132. });
  133. }
  134. TraceablePeerConnection.prototype.addStream = function (stream) {
  135. this.trace('addStream', stream.id);
  136. simulcast.resetSender();
  137. try
  138. {
  139. this.peerconnection.addStream(stream);
  140. }
  141. catch (e)
  142. {
  143. console.error(e);
  144. return;
  145. }
  146. };
  147. TraceablePeerConnection.prototype.removeStream = function (stream, stopStreams) {
  148. this.trace('removeStream', stream.id);
  149. simulcast.resetSender();
  150. if(stopStreams) {
  151. stream.getAudioTracks().forEach(function (track) {
  152. track.stop();
  153. });
  154. stream.getVideoTracks().forEach(function (track) {
  155. track.stop();
  156. });
  157. }
  158. this.peerconnection.removeStream(stream);
  159. };
  160. TraceablePeerConnection.prototype.createDataChannel = function (label, opts) {
  161. this.trace('createDataChannel', label, opts);
  162. return this.peerconnection.createDataChannel(label, opts);
  163. };
  164. TraceablePeerConnection.prototype.setLocalDescription = function (description, successCallback, failureCallback) {
  165. var self = this;
  166. description = simulcast.transformLocalDescription(description);
  167. this.trace('setLocalDescription', dumpSDP(description));
  168. this.peerconnection.setLocalDescription(description,
  169. function () {
  170. self.trace('setLocalDescriptionOnSuccess');
  171. successCallback();
  172. },
  173. function (err) {
  174. self.trace('setLocalDescriptionOnFailure', err);
  175. failureCallback(err);
  176. }
  177. );
  178. /*
  179. if (this.statsinterval === null && this.maxstats > 0) {
  180. // start gathering stats
  181. }
  182. */
  183. };
  184. TraceablePeerConnection.prototype.setRemoteDescription = function (description, successCallback, failureCallback) {
  185. var self = this;
  186. description = simulcast.transformRemoteDescription(description);
  187. this.trace('setRemoteDescription', dumpSDP(description));
  188. this.peerconnection.setRemoteDescription(description,
  189. function () {
  190. self.trace('setRemoteDescriptionOnSuccess');
  191. successCallback();
  192. },
  193. function (err) {
  194. self.trace('setRemoteDescriptionOnFailure', err);
  195. failureCallback(err);
  196. }
  197. );
  198. /*
  199. if (this.statsinterval === null && this.maxstats > 0) {
  200. // start gathering stats
  201. }
  202. */
  203. };
  204. TraceablePeerConnection.prototype.hardMuteVideo = function (muted) {
  205. this.pendingop = muted ? 'mute' : 'unmute';
  206. };
  207. TraceablePeerConnection.prototype.enqueueAddSsrc = function(channel, ssrcLines) {
  208. if (!this.addssrc[channel]) {
  209. this.addssrc[channel] = '';
  210. }
  211. this.addssrc[channel] += ssrcLines;
  212. };
  213. TraceablePeerConnection.prototype.addSource = function (elem) {
  214. console.log('addssrc', new Date().getTime());
  215. console.log('ice', this.iceConnectionState);
  216. var sdp = new SDP(this.remoteDescription.sdp);
  217. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  218. var self = this;
  219. $(elem).each(function (idx, content) {
  220. var name = $(content).attr('name');
  221. var lines = '';
  222. tmp = $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  223. var semantics = this.getAttribute('semantics');
  224. var ssrcs = $(this).find('>source').map(function () {
  225. return this.getAttribute('ssrc');
  226. }).get();
  227. if (ssrcs.length != 0) {
  228. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  229. }
  230. });
  231. tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  232. tmp.each(function () {
  233. var ssrc = $(this).attr('ssrc');
  234. if(mySdp.containsSSRC(ssrc)){
  235. /**
  236. * This happens when multiple participants change their streams at the same time and
  237. * ColibriFocus.modifySources have to wait for stable state. In the meantime multiple
  238. * addssrc are scheduled for update IQ. See
  239. */
  240. console.warn("Got add stream request for my own ssrc: "+ssrc);
  241. return;
  242. }
  243. $(this).find('>parameter').each(function () {
  244. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  245. if ($(this).attr('value') && $(this).attr('value').length)
  246. lines += ':' + $(this).attr('value');
  247. lines += '\r\n';
  248. });
  249. });
  250. sdp.media.forEach(function(media, idx) {
  251. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  252. return;
  253. sdp.media[idx] += lines;
  254. self.enqueueAddSsrc(idx, lines);
  255. });
  256. sdp.raw = sdp.session + sdp.media.join('');
  257. });
  258. };
  259. TraceablePeerConnection.prototype.enqueueRemoveSsrc = function(channel, ssrcLines) {
  260. if (!this.removessrc[channel]){
  261. this.removessrc[channel] = '';
  262. }
  263. this.removessrc[channel] += ssrcLines;
  264. };
  265. TraceablePeerConnection.prototype.removeSource = function (elem) {
  266. console.log('removessrc', new Date().getTime());
  267. console.log('ice', this.iceConnectionState);
  268. var sdp = new SDP(this.remoteDescription.sdp);
  269. var mySdp = new SDP(this.peerconnection.localDescription.sdp);
  270. var self = this;
  271. $(elem).each(function (idx, content) {
  272. var name = $(content).attr('name');
  273. var lines = '';
  274. tmp = $(content).find('ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  275. var semantics = this.getAttribute('semantics');
  276. var ssrcs = $(this).find('>source').map(function () {
  277. return this.getAttribute('ssrc');
  278. }).get();
  279. if (ssrcs.length != 0) {
  280. lines += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  281. }
  282. });
  283. tmp = $(content).find('source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]'); // can handle both >source and >description>source
  284. tmp.each(function () {
  285. var ssrc = $(this).attr('ssrc');
  286. // This should never happen, but can be useful for bug detection
  287. if(mySdp.containsSSRC(ssrc)){
  288. console.error("Got remove stream request for my own ssrc: "+ssrc);
  289. return;
  290. }
  291. $(this).find('>parameter').each(function () {
  292. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  293. if ($(this).attr('value') && $(this).attr('value').length)
  294. lines += ':' + $(this).attr('value');
  295. lines += '\r\n';
  296. });
  297. });
  298. sdp.media.forEach(function(media, idx) {
  299. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  300. return;
  301. sdp.media[idx] += lines;
  302. self.enqueueRemoveSsrc(idx, lines);
  303. });
  304. sdp.raw = sdp.session + sdp.media.join('');
  305. });
  306. };
  307. TraceablePeerConnection.prototype.modifySources = function(successCallback) {
  308. var self = this;
  309. if (this.signalingState == 'closed') return;
  310. if (!(this.addssrc.length || this.removessrc.length || this.pendingop !== null || this.switchstreams)){
  311. // There is nothing to do since scheduled job might have been executed by another succeeding call
  312. if(successCallback){
  313. successCallback();
  314. }
  315. return;
  316. }
  317. // FIXME: this is a big hack
  318. // https://code.google.com/p/webrtc/issues/detail?id=2688
  319. // ^ has been fixed.
  320. if (!(this.signalingState == 'stable' && this.iceConnectionState == 'connected')) {
  321. console.warn('modifySources not yet', this.signalingState, this.iceConnectionState);
  322. this.wait = true;
  323. window.setTimeout(function() { self.modifySources(successCallback); }, 250);
  324. return;
  325. }
  326. if (this.wait) {
  327. window.setTimeout(function() { self.modifySources(successCallback); }, 2500);
  328. this.wait = false;
  329. return;
  330. }
  331. // Reset switch streams flag
  332. this.switchstreams = false;
  333. var sdp = new SDP(this.remoteDescription.sdp);
  334. // add sources
  335. this.addssrc.forEach(function(lines, idx) {
  336. sdp.media[idx] += lines;
  337. });
  338. this.addssrc = [];
  339. // remove sources
  340. this.removessrc.forEach(function(lines, idx) {
  341. lines = lines.split('\r\n');
  342. lines.pop(); // remove empty last element;
  343. lines.forEach(function(line) {
  344. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  345. });
  346. });
  347. this.removessrc = [];
  348. // FIXME:
  349. // this was a hack for the situation when only one peer exists
  350. // in the conference.
  351. // check if still required and remove
  352. if (sdp.media[0])
  353. sdp.media[0] = sdp.media[0].replace('a=recvonly', 'a=sendrecv');
  354. if (sdp.media[1])
  355. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  356. sdp.raw = sdp.session + sdp.media.join('');
  357. this.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  358. function() {
  359. if(self.signalingState == 'closed') {
  360. console.error("createAnswer attempt on closed state");
  361. return;
  362. }
  363. self.createAnswer(
  364. function(modifiedAnswer) {
  365. // change video direction, see https://github.com/jitsi/jitmeet/issues/41
  366. if (self.pendingop !== null) {
  367. var sdp = new SDP(modifiedAnswer.sdp);
  368. if (sdp.media.length > 1) {
  369. switch(self.pendingop) {
  370. case 'mute':
  371. sdp.media[1] = sdp.media[1].replace('a=sendrecv', 'a=recvonly');
  372. break;
  373. case 'unmute':
  374. sdp.media[1] = sdp.media[1].replace('a=recvonly', 'a=sendrecv');
  375. break;
  376. }
  377. sdp.raw = sdp.session + sdp.media.join('');
  378. modifiedAnswer.sdp = sdp.raw;
  379. }
  380. self.pendingop = null;
  381. }
  382. // FIXME: pushing down an answer while ice connection state
  383. // is still checking is bad...
  384. //console.log(self.peerconnection.iceConnectionState);
  385. // trying to work around another chrome bug
  386. //modifiedAnswer.sdp = modifiedAnswer.sdp.replace(/a=setup:active/g, 'a=setup:actpass');
  387. self.setLocalDescription(modifiedAnswer,
  388. function() {
  389. //console.log('modified setLocalDescription ok');
  390. if(successCallback){
  391. successCallback();
  392. }
  393. },
  394. function(error) {
  395. console.error('modified setLocalDescription failed', error);
  396. }
  397. );
  398. },
  399. function(error) {
  400. console.error('modified answer failed', error);
  401. }
  402. );
  403. },
  404. function(error) {
  405. console.error('modify failed', error);
  406. }
  407. );
  408. };
  409. TraceablePeerConnection.prototype.close = function () {
  410. this.trace('stop');
  411. if (this.statsinterval !== null) {
  412. window.clearInterval(this.statsinterval);
  413. this.statsinterval = null;
  414. }
  415. this.peerconnection.close();
  416. };
  417. TraceablePeerConnection.prototype.createOffer = function (successCallback, failureCallback, constraints) {
  418. var self = this;
  419. this.trace('createOffer', JSON.stringify(constraints, null, ' '));
  420. this.peerconnection.createOffer(
  421. function (offer) {
  422. self.trace('createOfferOnSuccess', dumpSDP(offer));
  423. successCallback(offer);
  424. },
  425. function(err) {
  426. self.trace('createOfferOnFailure', err);
  427. failureCallback(err);
  428. },
  429. constraints
  430. );
  431. };
  432. TraceablePeerConnection.prototype.createAnswer = function (successCallback, failureCallback, constraints) {
  433. var self = this;
  434. this.trace('createAnswer', JSON.stringify(constraints, null, ' '));
  435. this.peerconnection.createAnswer(
  436. function (answer) {
  437. answer = simulcast.transformAnswer(answer);
  438. self.trace('createAnswerOnSuccess', dumpSDP(answer));
  439. successCallback(answer);
  440. },
  441. function(err) {
  442. self.trace('createAnswerOnFailure', err);
  443. failureCallback(err);
  444. },
  445. constraints
  446. );
  447. };
  448. TraceablePeerConnection.prototype.addIceCandidate = function (candidate, successCallback, failureCallback) {
  449. var self = this;
  450. this.trace('addIceCandidate', JSON.stringify(candidate, null, ' '));
  451. this.peerconnection.addIceCandidate(candidate);
  452. /* maybe later
  453. this.peerconnection.addIceCandidate(candidate,
  454. function () {
  455. self.trace('addIceCandidateOnSuccess');
  456. successCallback();
  457. },
  458. function (err) {
  459. self.trace('addIceCandidateOnFailure', err);
  460. failureCallback(err);
  461. }
  462. );
  463. */
  464. };
  465. TraceablePeerConnection.prototype.getStats = function(callback, errback) {
  466. if (navigator.mozGetUserMedia) {
  467. // ignore for now...
  468. if(!errback)
  469. errback = function () {
  470. }
  471. this.peerconnection.getStats(null,callback,errback);
  472. } else {
  473. this.peerconnection.getStats(callback);
  474. }
  475. };
  476. // mozilla chrome compat layer -- very similar to adapter.js
  477. function setupRTC() {
  478. var RTC = null;
  479. if (navigator.mozGetUserMedia) {
  480. console.log('This appears to be Firefox');
  481. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  482. if (version >= 22) {
  483. RTC = {
  484. peerconnection: mozRTCPeerConnection,
  485. browser: 'firefox',
  486. getUserMedia: navigator.mozGetUserMedia.bind(navigator),
  487. attachMediaStream: function (element, stream) {
  488. element[0].mozSrcObject = stream;
  489. element[0].play();
  490. },
  491. pc_constraints: {},
  492. getLocalSSRC: function (session, callback) {
  493. // NOTE(gp) latest FF nightlies seem to provide the local
  494. // SSRCs in their SDP so there's no longer necessary to
  495. // take it from the peer connection stats.
  496. /*session.peerconnection.getStats(function (s) {
  497. var ssrcs = {};
  498. s.forEach(function (item) {
  499. if (item.type == "outboundrtp" && !item.isRemote)
  500. {
  501. ssrcs[item.id.split('_')[2]] = item.ssrc;
  502. }
  503. });
  504. session.localStreamsSSRC = {
  505. "audio": ssrcs.audio,//for stable 0
  506. "video": ssrcs.video// for stable 1
  507. };
  508. callback(session.localStreamsSSRC);
  509. },
  510. function () {
  511. callback(null);
  512. });*/
  513. callback(null);
  514. },
  515. getStreamID: function (stream) {
  516. var tracks = stream.getVideoTracks();
  517. if(!tracks || tracks.length == 0)
  518. {
  519. tracks = stream.getAudioTracks();
  520. }
  521. return tracks[0].id.replace(/[\{,\}]/g,"");
  522. },
  523. getVideoSrc: function (element) {
  524. return element.mozSrcObject;
  525. },
  526. setVideoSrc: function (element, src) {
  527. element.mozSrcObject = src;
  528. }
  529. };
  530. if (!MediaStream.prototype.getVideoTracks)
  531. MediaStream.prototype.getVideoTracks = function () { return []; };
  532. if (!MediaStream.prototype.getAudioTracks)
  533. MediaStream.prototype.getAudioTracks = function () { return []; };
  534. RTCSessionDescription = mozRTCSessionDescription;
  535. RTCIceCandidate = mozRTCIceCandidate;
  536. }
  537. } else if (navigator.webkitGetUserMedia) {
  538. console.log('This appears to be Chrome');
  539. RTC = {
  540. peerconnection: webkitRTCPeerConnection,
  541. browser: 'chrome',
  542. getUserMedia: navigator.webkitGetUserMedia.bind(navigator),
  543. attachMediaStream: function (element, stream) {
  544. element.attr('src', webkitURL.createObjectURL(stream));
  545. },
  546. // DTLS should now be enabled by default but..
  547. pc_constraints: {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]},
  548. getLocalSSRC: function (session, callback) {
  549. callback(null);
  550. },
  551. getStreamID: function (stream) {
  552. // streams from FF endpoints have the characters '{' and '}'
  553. // that make jQuery choke.
  554. return stream.id.replace(/[\{,\}]/g,"");
  555. },
  556. getVideoSrc: function (element) {
  557. return element.getAttribute("src");
  558. },
  559. setVideoSrc: function (element, src) {
  560. element.setAttribute("src", src);
  561. }
  562. };
  563. if (navigator.userAgent.indexOf('Android') != -1) {
  564. RTC.pc_constraints = {}; // disable DTLS on Android
  565. }
  566. if (!webkitMediaStream.prototype.getVideoTracks) {
  567. webkitMediaStream.prototype.getVideoTracks = function () {
  568. return this.videoTracks;
  569. };
  570. }
  571. if (!webkitMediaStream.prototype.getAudioTracks) {
  572. webkitMediaStream.prototype.getAudioTracks = function () {
  573. return this.audioTracks;
  574. };
  575. }
  576. }
  577. if (RTC === null) {
  578. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  579. }
  580. return RTC;
  581. }
  582. function getUserMediaWithConstraints(um, success_callback, failure_callback, resolution, bandwidth, fps, desktopStream) {
  583. var constraints = {audio: false, video: false};
  584. if (um.indexOf('video') >= 0) {
  585. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  586. }
  587. if (um.indexOf('audio') >= 0) {
  588. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  589. }
  590. if (um.indexOf('screen') >= 0) {
  591. constraints.video = {
  592. mandatory: {
  593. chromeMediaSource: "screen",
  594. googLeakyBucket: true,
  595. maxWidth: window.screen.width,
  596. maxHeight: window.screen.height,
  597. maxFrameRate: 3
  598. },
  599. optional: []
  600. };
  601. }
  602. if (um.indexOf('desktop') >= 0) {
  603. constraints.video = {
  604. mandatory: {
  605. chromeMediaSource: "desktop",
  606. chromeMediaSourceId: desktopStream,
  607. googLeakyBucket: true,
  608. maxWidth: window.screen.width,
  609. maxHeight: window.screen.height,
  610. maxFrameRate: 3
  611. },
  612. optional: []
  613. }
  614. }
  615. if (constraints.audio) {
  616. // if it is good enough for hangouts...
  617. constraints.audio.optional.push(
  618. {googEchoCancellation: true},
  619. {googAutoGainControl: true},
  620. {googNoiseSupression: true},
  621. {googHighpassFilter: true},
  622. {googNoisesuppression2: true},
  623. {googEchoCancellation2: true},
  624. {googAutoGainControl2: true}
  625. );
  626. }
  627. if (constraints.video) {
  628. constraints.video.optional.push(
  629. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  630. );
  631. if (um.indexOf('video') >= 0) {
  632. constraints.video.optional.push(
  633. {googLeakyBucket: true}
  634. );
  635. }
  636. }
  637. // Check if we are running on Android device
  638. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  639. if (resolution && !constraints.video || isAndroid) {
  640. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  641. }
  642. // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
  643. switch (resolution) {
  644. // 16:9 first
  645. case '1080':
  646. case 'fullhd':
  647. constraints.video.mandatory.minWidth = 1920;
  648. constraints.video.mandatory.minHeight = 1080;
  649. break;
  650. case '720':
  651. case 'hd':
  652. constraints.video.mandatory.minWidth = 1280;
  653. constraints.video.mandatory.minHeight = 720;
  654. break;
  655. case '360':
  656. constraints.video.mandatory.minWidth = 640;
  657. constraints.video.mandatory.minHeight = 360;
  658. break;
  659. case '180':
  660. constraints.video.mandatory.minWidth = 320;
  661. constraints.video.mandatory.minHeight = 180;
  662. break;
  663. // 4:3
  664. case '960':
  665. constraints.video.mandatory.minWidth = 960;
  666. constraints.video.mandatory.minHeight = 720;
  667. break;
  668. case '640':
  669. case 'vga':
  670. constraints.video.mandatory.minWidth = 640;
  671. constraints.video.mandatory.minHeight = 480;
  672. break;
  673. case '320':
  674. constraints.video.mandatory.minWidth = 320;
  675. constraints.video.mandatory.minHeight = 240;
  676. break;
  677. default:
  678. if (isAndroid) {
  679. constraints.video.mandatory.minWidth = 320;
  680. constraints.video.mandatory.minHeight = 240;
  681. constraints.video.mandatory.maxFrameRate = 15;
  682. }
  683. break;
  684. }
  685. if (constraints.video.mandatory.minWidth)
  686. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  687. if (constraints.video.mandatory.minHeight)
  688. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  689. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  690. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  691. constraints.video.optional.push({bandwidth: bandwidth});
  692. }
  693. if (fps) { // for some cameras it might be necessary to request 30fps
  694. // so they choose 30fps mjpg over 10fps yuy2
  695. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  696. constraints.video.mandatory.minFrameRate = fps;
  697. }
  698. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  699. try {
  700. if (config.enableSimulcast
  701. && constraints.video
  702. && constraints.video.chromeMediaSource !== 'screen'
  703. && constraints.video.chromeMediaSource !== 'desktop'
  704. && !isAndroid
  705. // We currently do not support FF, as it doesn't have multistream support.
  706. && !isFF) {
  707. simulcast.getUserMedia(constraints, function (stream) {
  708. console.log('onUserMediaSuccess');
  709. success_callback(stream);
  710. },
  711. function (error) {
  712. console.warn('Failed to get access to local media. Error ', error);
  713. if (failure_callback) {
  714. failure_callback(error);
  715. }
  716. });
  717. } else {
  718. RTC.getUserMedia(constraints,
  719. function (stream) {
  720. console.log('onUserMediaSuccess');
  721. success_callback(stream);
  722. },
  723. function (error) {
  724. console.warn('Failed to get access to local media. Error ', error);
  725. if (failure_callback) {
  726. failure_callback(error);
  727. }
  728. });
  729. }
  730. } catch (e) {
  731. console.error('GUM failed: ', e);
  732. if(failure_callback) {
  733. failure_callback(e);
  734. }
  735. }
  736. }