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.

RTCUtils.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /* global config, require, attachMediaStream, getUserMedia */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var RTCBrowserType = require("./RTCBrowserType");
  4. var Resolutions = require("../../service/RTC/Resolutions");
  5. var RTCEvents = require("../../service/RTC/RTCEvents");
  6. var AdapterJS = require("./adapter.screenshare");
  7. var SDPUtil = require("../xmpp/SDPUtil");
  8. var EventEmitter = require("events");
  9. var JitsiLocalTrack = require("./JitsiLocalTrack");
  10. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  11. var eventEmitter = new EventEmitter();
  12. var devices = {
  13. audio: true,
  14. video: true
  15. };
  16. var rtcReady = false;
  17. function DummyMediaStream(id) {
  18. this.id = id;
  19. this.label = id;
  20. this.stop = function() { };
  21. this.getAudioTracks = function() { return []; };
  22. this.getVideoTracks = function() { return []; };
  23. }
  24. function getPreviousResolution(resolution) {
  25. if(!Resolutions[resolution])
  26. return null;
  27. var order = Resolutions[resolution].order;
  28. var res = null;
  29. var resName = null;
  30. for(var i in Resolutions) {
  31. var tmp = Resolutions[i];
  32. if(res == null || (res.order < tmp.order && tmp.order < order)) {
  33. resName = i;
  34. res = tmp;
  35. }
  36. }
  37. return resName;
  38. }
  39. function setResolutionConstraints(constraints, resolution) {
  40. var isAndroid = RTCBrowserType.isAndroid();
  41. if (Resolutions[resolution]) {
  42. constraints.video.mandatory.minWidth = Resolutions[resolution].width;
  43. constraints.video.mandatory.minHeight = Resolutions[resolution].height;
  44. }
  45. else if (isAndroid) {
  46. // FIXME can't remember if the purpose of this was to always request
  47. // low resolution on Android ? if yes it should be moved up front
  48. constraints.video.mandatory.minWidth = 320;
  49. constraints.video.mandatory.minHeight = 240;
  50. constraints.video.mandatory.maxFrameRate = 15;
  51. }
  52. if (constraints.video.mandatory.minWidth)
  53. constraints.video.mandatory.maxWidth =
  54. constraints.video.mandatory.minWidth;
  55. if (constraints.video.mandatory.minHeight)
  56. constraints.video.mandatory.maxHeight =
  57. constraints.video.mandatory.minHeight;
  58. }
  59. function getConstraints(um, resolution, bandwidth, fps, desktopStream) {
  60. var constraints = {audio: false, video: false};
  61. if (um.indexOf('video') >= 0) {
  62. // same behaviour as true
  63. constraints.video = { mandatory: {}, optional: [] };
  64. constraints.video.optional.push({ googLeakyBucket: true });
  65. setResolutionConstraints(constraints, resolution);
  66. }
  67. if (um.indexOf('audio') >= 0) {
  68. if (!RTCBrowserType.isFirefox()) {
  69. // same behaviour as true
  70. constraints.audio = { mandatory: {}, optional: []};
  71. // if it is good enough for hangouts...
  72. constraints.audio.optional.push(
  73. {googEchoCancellation: true},
  74. {googAutoGainControl: true},
  75. {googNoiseSupression: true},
  76. {googHighpassFilter: true},
  77. {googNoisesuppression2: true},
  78. {googEchoCancellation2: true},
  79. {googAutoGainControl2: true}
  80. );
  81. } else {
  82. constraints.audio = true;
  83. }
  84. }
  85. if (um.indexOf('screen') >= 0) {
  86. if (RTCBrowserType.isChrome()) {
  87. constraints.video = {
  88. mandatory: {
  89. chromeMediaSource: "screen",
  90. googLeakyBucket: true,
  91. maxWidth: window.screen.width,
  92. maxHeight: window.screen.height,
  93. maxFrameRate: 3
  94. },
  95. optional: []
  96. };
  97. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  98. constraints.video = {
  99. optional: [
  100. {
  101. sourceId: AdapterJS.WebRTCPlugin.plugin.screensharingKey
  102. }
  103. ]
  104. };
  105. } else {
  106. logger.error(
  107. "'screen' WebRTC media source is supported only in Chrome" +
  108. " and with Temasys plugin");
  109. }
  110. }
  111. if (um.indexOf('desktop') >= 0) {
  112. constraints.video = {
  113. mandatory: {
  114. chromeMediaSource: "desktop",
  115. chromeMediaSourceId: desktopStream,
  116. googLeakyBucket: true,
  117. maxWidth: window.screen.width,
  118. maxHeight: window.screen.height,
  119. maxFrameRate: 3
  120. },
  121. optional: []
  122. };
  123. }
  124. if (bandwidth) {
  125. if (!constraints.video) {
  126. //same behaviour as true
  127. constraints.video = {mandatory: {}, optional: []};
  128. }
  129. constraints.video.optional.push({bandwidth: bandwidth});
  130. }
  131. if (fps) {
  132. // for some cameras it might be necessary to request 30fps
  133. // so they choose 30fps mjpg over 10fps yuy2
  134. if (!constraints.video) {
  135. // same behaviour as true;
  136. constraints.video = {mandatory: {}, optional: []};
  137. }
  138. constraints.video.mandatory.minFrameRate = fps;
  139. }
  140. return constraints;
  141. }
  142. function setAvailableDevices(um, available) {
  143. var devices = {};
  144. if (um.indexOf("video") != -1) {
  145. devices.video = available;
  146. }
  147. if (um.indexOf("audio") != -1) {
  148. devices.audio = available;
  149. }
  150. eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, devices);
  151. }
  152. // In case of IE we continue from 'onReady' callback
  153. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  154. // once it is initialized.
  155. function onReady () {
  156. rtcReady = true;
  157. eventEmitter.emit(RTCEvents.RTC_READY, true);
  158. };
  159. //Options parameter is to pass config options. Currently uses only "useIPv6".
  160. var RTCUtils = {
  161. init: function (options) {
  162. var self = this;
  163. if (RTCBrowserType.isFirefox()) {
  164. var FFversion = RTCBrowserType.getFirefoxVersion();
  165. if (FFversion >= 40) {
  166. this.peerconnection = mozRTCPeerConnection;
  167. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  168. this.pc_constraints = {};
  169. this.attachMediaStream = function (element, stream) {
  170. // srcObject is being standardized and FF will eventually
  171. // support that unprefixed. FF also supports the
  172. // "element.src = URL.createObjectURL(...)" combo, but that
  173. // will be deprecated in favour of srcObject.
  174. //
  175. // https://groups.google.com/forum/#!topic/mozilla.dev.media/pKOiioXonJg
  176. // https://github.com/webrtc/samples/issues/302
  177. if (!element[0])
  178. return;
  179. element[0].mozSrcObject = stream;
  180. element[0].play();
  181. };
  182. this.getStreamID = function (stream) {
  183. var id = stream.id;
  184. if (!id) {
  185. var tracks = stream.getVideoTracks();
  186. if (!tracks || tracks.length === 0) {
  187. tracks = stream.getAudioTracks();
  188. }
  189. id = tracks[0].id;
  190. }
  191. return SDPUtil.filter_special_chars(id);
  192. };
  193. this.getVideoSrc = function (element) {
  194. if (!element)
  195. return null;
  196. return element.mozSrcObject;
  197. };
  198. this.setVideoSrc = function (element, src) {
  199. if (element)
  200. element.mozSrcObject = src;
  201. };
  202. RTCSessionDescription = mozRTCSessionDescription;
  203. RTCIceCandidate = mozRTCIceCandidate;
  204. } else {
  205. logger.error(
  206. "Firefox version too old: " + FFversion + ". Required >= 40.");
  207. window.location.href = 'unsupported_browser.html';
  208. return;
  209. }
  210. } else if (RTCBrowserType.isChrome() || RTCBrowserType.isOpera()) {
  211. this.peerconnection = webkitRTCPeerConnection;
  212. this.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  213. this.attachMediaStream = function (element, stream) {
  214. element.attr('src', webkitURL.createObjectURL(stream));
  215. };
  216. this.getStreamID = function (stream) {
  217. // streams from FF endpoints have the characters '{' and '}'
  218. // that make jQuery choke.
  219. return SDPUtil.filter_special_chars(stream.id);
  220. };
  221. this.getVideoSrc = function (element) {
  222. if (!element)
  223. return null;
  224. return element.getAttribute("src");
  225. };
  226. this.setVideoSrc = function (element, src) {
  227. if (element)
  228. element.setAttribute("src", src);
  229. };
  230. // DTLS should now be enabled by default but..
  231. this.pc_constraints = {'optional': [
  232. {'DtlsSrtpKeyAgreement': 'true'}
  233. ]};
  234. if (options.useIPv6) {
  235. // https://code.google.com/p/webrtc/issues/detail?id=2828
  236. this.pc_constraints.optional.push({googIPv6: true});
  237. }
  238. if (RTCBrowserType.isAndroid()) {
  239. this.pc_constraints = {}; // disable DTLS on Android
  240. }
  241. if (!webkitMediaStream.prototype.getVideoTracks) {
  242. webkitMediaStream.prototype.getVideoTracks = function () {
  243. return this.videoTracks;
  244. };
  245. }
  246. if (!webkitMediaStream.prototype.getAudioTracks) {
  247. webkitMediaStream.prototype.getAudioTracks = function () {
  248. return this.audioTracks;
  249. };
  250. }
  251. }
  252. // Detect IE/Safari
  253. else if (RTCBrowserType.isTemasysPluginUsed()) {
  254. //AdapterJS.WebRTCPlugin.setLogLevel(
  255. // AdapterJS.WebRTCPlugin.PLUGIN_LOG_LEVELS.VERBOSE);
  256. AdapterJS.webRTCReady(function (isPlugin) {
  257. self.peerconnection = RTCPeerConnection;
  258. self.getUserMedia = getUserMedia;
  259. self.attachMediaStream = function (elSel, stream) {
  260. if (stream.id === "dummyAudio" || stream.id === "dummyVideo") {
  261. return;
  262. }
  263. attachMediaStream(elSel[0], stream);
  264. };
  265. self.getStreamID = function (stream) {
  266. var id = SDPUtil.filter_special_chars(stream.label);
  267. return id;
  268. };
  269. self.getVideoSrc = function (element) {
  270. if (!element) {
  271. logger.warn("Attempt to get video SRC of null element");
  272. return null;
  273. }
  274. var children = element.children;
  275. for (var i = 0; i !== children.length; ++i) {
  276. if (children[i].name === 'streamId') {
  277. return children[i].value;
  278. }
  279. }
  280. //logger.info(element.id + " SRC: " + src);
  281. return null;
  282. };
  283. self.setVideoSrc = function (element, src) {
  284. //logger.info("Set video src: ", element, src);
  285. if (!src) {
  286. logger.warn("Not attaching video stream, 'src' is null");
  287. return;
  288. }
  289. AdapterJS.WebRTCPlugin.WaitForPluginReady();
  290. var stream = AdapterJS.WebRTCPlugin.plugin
  291. .getStreamWithId(AdapterJS.WebRTCPlugin.pageId, src);
  292. attachMediaStream(element, stream);
  293. };
  294. onReady(isPlugin);
  295. });
  296. } else {
  297. try {
  298. logger.error('Browser does not appear to be WebRTC-capable');
  299. } catch (e) {
  300. }
  301. return;
  302. }
  303. // Call onReady() if Temasys plugin is not used
  304. if (!RTCBrowserType.isTemasysPluginUsed()) {
  305. onReady();
  306. }
  307. },
  308. getUserMediaWithConstraints: function ( um, success_callback, failure_callback, resolution, bandwidth, fps, desktopStream) {
  309. var constraints = getConstraints(
  310. um, resolution, bandwidth, fps, desktopStream);
  311. logger.info("Get media constraints", constraints);
  312. try {
  313. this.getUserMedia(constraints,
  314. function (stream) {
  315. logger.log('onUserMediaSuccess');
  316. setAvailableDevices(um, true);
  317. success_callback(stream);
  318. },
  319. function (error) {
  320. setAvailableDevices(um, false);
  321. logger.warn('Failed to get access to local media. Error ',
  322. error, constraints);
  323. if (failure_callback) {
  324. failure_callback(error, resolution);
  325. }
  326. });
  327. } catch (e) {
  328. logger.error('GUM failed: ', e);
  329. if (failure_callback) {
  330. failure_callback(e);
  331. }
  332. }
  333. },
  334. /**
  335. * Creates the local MediaStreams.
  336. * @param devices the devices that will be requested
  337. * @param resolution resolution constraints
  338. * @param dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  339. * type: "audio" or "video", videoType: "camera" or "desktop"}
  340. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  341. * @returns {*} Promise object that will receive the new JitsiTracks
  342. */
  343. obtainAudioAndVideoPermissions: function (devices, resolution, dontCreateJitsiTracks) {
  344. var self = this;
  345. // Get AV
  346. return new Promise(function (resolve, reject) {
  347. var successCallback = function (stream) {
  348. var streams = self.successCallback(stream, resolution);
  349. resolve(dontCreateJitsiTracks? streams: self.createLocalTracks(streams));
  350. };
  351. if (!devices)
  352. devices = ['audio', 'video'];
  353. if (devices.length === 0) {
  354. successCallback();
  355. return;
  356. }
  357. if (RTCBrowserType.isFirefox() || RTCBrowserType.isTemasysPluginUsed()) {
  358. // With FF/IE we can't split the stream into audio and video because FF
  359. // doesn't support media stream constructors. So, we need to get the
  360. // audio stream separately from the video stream using two distinct GUM
  361. // calls. Not very user friendly :-( but we don't have many other
  362. // options neither.
  363. //
  364. // Note that we pack those 2 streams in a single object and pass it to
  365. // the successCallback method.
  366. var obtainVideo = function (audioStream) {
  367. self.getUserMediaWithConstraints(
  368. ['video'],
  369. function (videoStream) {
  370. return successCallback({
  371. audioStream: audioStream,
  372. videoStream: videoStream
  373. });
  374. },
  375. function (error, resolution) {
  376. logger.error(
  377. 'failed to obtain video stream - stop', error);
  378. self.errorCallback(error, resolve, resolution, dontCreateJitsiTracks);
  379. },
  380. resolution || '360');
  381. };
  382. var obtainAudio = function () {
  383. self.getUserMediaWithConstraints(
  384. ['audio'],
  385. function (audioStream) {
  386. if (devices.indexOf('video') !== -1)
  387. obtainVideo(audioStream);
  388. },
  389. function (error) {
  390. logger.error(
  391. 'failed to obtain audio stream - stop', error);
  392. self.errorCallback(error, resolve, null, dontCreateJitsiTracks);
  393. }
  394. );
  395. };
  396. if (devices.indexOf('audio') !== -1) {
  397. obtainAudio();
  398. } else {
  399. obtainVideo(null);
  400. }
  401. } else {
  402. this.getUserMediaWithConstraints(
  403. devices,
  404. function (stream) {
  405. successCallback(stream);
  406. },
  407. function (error, resolution) {
  408. self.errorCallback(error, resolve, resolution, dontCreateJitsiTracks);
  409. },
  410. resolution || '360');
  411. }
  412. }.bind(this));
  413. },
  414. /**
  415. * Successful callback called from GUM.
  416. * @param stream the new MediaStream
  417. * @param resolution the resolution of the video stream.
  418. * @returns {*}
  419. */
  420. successCallback: function (stream, resolution) {
  421. // If this is FF or IE, the stream parameter is *not* a MediaStream object,
  422. // it's an object with two properties: audioStream, videoStream.
  423. if (stream && stream.getAudioTracks && stream.getVideoTracks)
  424. logger.log('got', stream, stream.getAudioTracks().length,
  425. stream.getVideoTracks().length);
  426. return this.handleLocalStream(stream, resolution);
  427. },
  428. /**
  429. * Error callback called from GUM. Retries the GUM call with different resolutions.
  430. * @param error the error
  431. * @param resolve the resolve funtion that will be called on success.
  432. * @param currentResolution the last resolution used for GUM.
  433. * @param dontCreateJitsiTracks if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  434. * type: "audio" or "video", videoType: "camera" or "desktop"}
  435. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  436. */
  437. errorCallback: function (error, resolve, currentResolution, dontCreateJitsiTracks) {
  438. var self = this;
  439. logger.error('failed to obtain audio/video stream - trying audio only', error);
  440. var resolution = getPreviousResolution(currentResolution);
  441. if (typeof error == "object" && error.constraintName && error.name
  442. && (error.name == "ConstraintNotSatisfiedError" ||
  443. error.name == "OverconstrainedError") &&
  444. (error.constraintName == "minWidth" || error.constraintName == "maxWidth" ||
  445. error.constraintName == "minHeight" || error.constraintName == "maxHeight")
  446. && resolution != null) {
  447. self.getUserMediaWithConstraints(['audio', 'video'],
  448. function (stream) {
  449. var streams = self.successCallback(stream, resolution);
  450. resolve(dontCreateJitsiTracks? streams: self.createLocalTracks(streams));
  451. }, function (error, resolution) {
  452. return self.errorCallback(error, resolve, resolution, dontCreateJitsiTracks);
  453. }, resolution);
  454. }
  455. else {
  456. self.getUserMediaWithConstraints(
  457. ['audio'],
  458. function (stream) {
  459. var streams = self.successCallback(stream, resolution);
  460. resolve(dontCreateJitsiTracks? streams: self.createLocalTracks(streams));
  461. },
  462. function (error) {
  463. logger.error('failed to obtain audio/video stream - stop',
  464. error);
  465. var streams = self.successCallback(null);
  466. resolve(dontCreateJitsiTracks? streams: self.createLocalTracks(streams));
  467. }
  468. );
  469. }
  470. },
  471. /**
  472. * Handles the newly created Media Streams.
  473. * @param stream the new Media Streams
  474. * @param resolution the resolution of the video stream.
  475. * @returns {*[]} Promise object with the new Media Streams.
  476. */
  477. handleLocalStream: function (stream, resolution) {
  478. var audioStream, videoStream;
  479. // If this is FF, the stream parameter is *not* a MediaStream object, it's
  480. // an object with two properties: audioStream, videoStream.
  481. if (window.webkitMediaStream) {
  482. audioStream = new webkitMediaStream();
  483. videoStream = new webkitMediaStream();
  484. if (stream) {
  485. var audioTracks = stream.getAudioTracks();
  486. for (var i = 0; i < audioTracks.length; i++) {
  487. audioStream.addTrack(audioTracks[i]);
  488. }
  489. var videoTracks = stream.getVideoTracks();
  490. for (i = 0; i < videoTracks.length; i++) {
  491. videoStream.addTrack(videoTracks[i]);
  492. }
  493. }
  494. }
  495. else if (RTCBrowserType.isFirefox() || RTCBrowserType.isTemasysPluginUsed()) { // Firefox and Temasys plugin
  496. if (stream && stream.audioStream)
  497. audioStream = stream.audioStream;
  498. else
  499. audioStream = new DummyMediaStream("dummyAudio");
  500. if (stream && stream.videoStream)
  501. videoStream = stream.videoStream;
  502. else
  503. videoStream = new DummyMediaStream("dummyVideo");
  504. }
  505. return [
  506. {stream: audioStream, type: "audio", videoType: null},
  507. {stream: videoStream, type: "video", videoType: "camera",
  508. resolution: resolution}
  509. ];
  510. },
  511. createStream: function (stream, isVideo) {
  512. var newStream = null;
  513. if (window.webkitMediaStream) {
  514. newStream = new webkitMediaStream();
  515. if (newStream) {
  516. var tracks = (isVideo ? stream.getVideoTracks() : stream.getAudioTracks());
  517. for (var i = 0; i < tracks.length; i++) {
  518. newStream.addTrack(tracks[i]);
  519. }
  520. }
  521. } else {
  522. // FIXME: this is duplicated with 'handleLocalStream' !!!
  523. if (stream) {
  524. newStream = stream;
  525. } else {
  526. newStream =
  527. new DummyMediaStream(isVideo ? "dummyVideo" : "dummyAudio");
  528. }
  529. }
  530. return newStream;
  531. },
  532. addListener: function (eventType, listener) {
  533. eventEmitter.on(eventType, listener);
  534. },
  535. removeListener: function (eventType, listener) {
  536. eventEmitter.removeListener(eventType, listener);
  537. },
  538. getDeviceAvailability: function () {
  539. return devices;
  540. },
  541. isRTCReady: function () {
  542. return rtcReady;
  543. },
  544. createLocalTracks: function (streams) {
  545. var newStreams = []
  546. for (var i = 0; i < streams.length; i++) {
  547. var localStream = new JitsiLocalTrack(null, streams[i].stream,
  548. eventEmitter, streams[i].videoType, streams[i].resolution);
  549. newStreams.push(localStream);
  550. if (streams[i].isMuted === true)
  551. localStream.setMute(true);
  552. var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
  553. eventEmitter.emit(eventType, localStream);
  554. }
  555. return newStreams;
  556. }
  557. }
  558. module.exports = RTCUtils;