Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RTCUtils.js 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. var tmp, i;
  31. for(i in Resolutions) {
  32. tmp = Resolutions[i];
  33. if (!res || (res.order < tmp.order && tmp.order < order)) {
  34. resName = i;
  35. res = tmp;
  36. }
  37. }
  38. return resName;
  39. }
  40. function setResolutionConstraints(constraints, resolution) {
  41. var isAndroid = RTCBrowserType.isAndroid();
  42. if (Resolutions[resolution]) {
  43. constraints.video.mandatory.minWidth = Resolutions[resolution].width;
  44. constraints.video.mandatory.minHeight = Resolutions[resolution].height;
  45. }
  46. else if (isAndroid) {
  47. // FIXME can't remember if the purpose of this was to always request
  48. // low resolution on Android ? if yes it should be moved up front
  49. constraints.video.mandatory.minWidth = 320;
  50. constraints.video.mandatory.minHeight = 240;
  51. constraints.video.mandatory.maxFrameRate = 15;
  52. }
  53. if (constraints.video.mandatory.minWidth)
  54. constraints.video.mandatory.maxWidth =
  55. constraints.video.mandatory.minWidth;
  56. if (constraints.video.mandatory.minHeight)
  57. constraints.video.mandatory.maxHeight =
  58. constraints.video.mandatory.minHeight;
  59. }
  60. /**
  61. * @param {string[]} um required user media types
  62. *
  63. * @param {Object} [options={}] optional parameters
  64. * @param {string} options.resolution
  65. * @param {number} options.bandwidth
  66. * @param {number} options.fps
  67. * @param {string} options.desktopStream
  68. * @param {string} options.cameraDeviceId
  69. * @param {string} options.micDeviceId
  70. * @param {bool} firefox_fake_device
  71. */
  72. function getConstraints(um, options) {
  73. var constraints = {audio: false, video: false};
  74. if (um.indexOf('video') >= 0) {
  75. // same behaviour as true
  76. constraints.video = { mandatory: {}, optional: [] };
  77. if (options.cameraDeviceId) {
  78. constraints.video.optional.push({
  79. sourceId: options.cameraDeviceId
  80. });
  81. }
  82. constraints.video.optional.push({ googLeakyBucket: true });
  83. setResolutionConstraints(constraints, options.resolution);
  84. }
  85. if (um.indexOf('audio') >= 0) {
  86. if (!RTCBrowserType.isFirefox()) {
  87. // same behaviour as true
  88. constraints.audio = { mandatory: {}, optional: []};
  89. if (options.micDeviceId) {
  90. constraints.audio.optional.push({
  91. sourceId: options.micDeviceId
  92. });
  93. }
  94. // if it is good enough for hangouts...
  95. constraints.audio.optional.push(
  96. {googEchoCancellation: true},
  97. {googAutoGainControl: true},
  98. {googNoiseSupression: true},
  99. {googHighpassFilter: true},
  100. {googNoisesuppression2: true},
  101. {googEchoCancellation2: true},
  102. {googAutoGainControl2: true}
  103. );
  104. } else {
  105. if (options.micDeviceId) {
  106. constraints.audio = {
  107. mandatory: {},
  108. optional: [{
  109. sourceId: options.micDeviceId
  110. }]};
  111. } else {
  112. constraints.audio = true;
  113. }
  114. }
  115. }
  116. if (um.indexOf('screen') >= 0) {
  117. if (RTCBrowserType.isChrome()) {
  118. constraints.video = {
  119. mandatory: {
  120. chromeMediaSource: "screen",
  121. googLeakyBucket: true,
  122. maxWidth: window.screen.width,
  123. maxHeight: window.screen.height,
  124. maxFrameRate: 3
  125. },
  126. optional: []
  127. };
  128. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  129. constraints.video = {
  130. optional: [
  131. {
  132. sourceId: AdapterJS.WebRTCPlugin.plugin.screensharingKey
  133. }
  134. ]
  135. };
  136. } else if (RTCBrowserType.isFirefox()) {
  137. constraints.video = {
  138. mozMediaSource: "window",
  139. mediaSource: "window"
  140. };
  141. } else {
  142. logger.error(
  143. "'screen' WebRTC media source is supported only in Chrome" +
  144. " and with Temasys plugin");
  145. }
  146. }
  147. if (um.indexOf('desktop') >= 0) {
  148. constraints.video = {
  149. mandatory: {
  150. chromeMediaSource: "desktop",
  151. chromeMediaSourceId: options.desktopStream,
  152. googLeakyBucket: true,
  153. maxWidth: window.screen.width,
  154. maxHeight: window.screen.height,
  155. maxFrameRate: 3
  156. },
  157. optional: []
  158. };
  159. }
  160. if (options.bandwidth) {
  161. if (!constraints.video) {
  162. //same behaviour as true
  163. constraints.video = {mandatory: {}, optional: []};
  164. }
  165. constraints.video.optional.push({bandwidth: options.bandwidth});
  166. }
  167. if (options.fps) {
  168. // for some cameras it might be necessary to request 30fps
  169. // so they choose 30fps mjpg over 10fps yuy2
  170. if (!constraints.video) {
  171. // same behaviour as true;
  172. constraints.video = {mandatory: {}, optional: []};
  173. }
  174. constraints.video.mandatory.minFrameRate = options.fps;
  175. }
  176. // we turn audio for both audio and video tracks, the fake audio & video seems to work
  177. // only when enabled in one getUserMedia call, we cannot get fake audio separate by fake video
  178. // this later can be a problem with some of the tests
  179. if(RTCBrowserType.isFirefox() && options.firefox_fake_device)
  180. {
  181. constraints.audio = true;
  182. constraints.fake = true;
  183. }
  184. return constraints;
  185. }
  186. function setAvailableDevices(um, available) {
  187. if (um.indexOf("video") != -1) {
  188. devices.video = available;
  189. }
  190. if (um.indexOf("audio") != -1) {
  191. devices.audio = available;
  192. }
  193. eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, devices);
  194. }
  195. // In case of IE we continue from 'onReady' callback
  196. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  197. // once it is initialized.
  198. function onReady () {
  199. rtcReady = true;
  200. eventEmitter.emit(RTCEvents.RTC_READY, true);
  201. };
  202. /**
  203. * Apply function with arguments if function exists.
  204. * Do nothing if function not provided.
  205. * @param {function} [fn] function to apply
  206. * @param {Array} [args=[]] arguments for function
  207. */
  208. function maybeApply(fn, args) {
  209. if (fn) {
  210. fn.apply(null, args || []);
  211. }
  212. }
  213. var getUserMediaStatus = {
  214. initialized: false,
  215. callbacks: []
  216. };
  217. /**
  218. * Wrap `getUserMedia` to allow others to know if it was executed at least
  219. * once or not. Wrapper function uses `getUserMediaStatus` object.
  220. * @param {Function} getUserMedia native function
  221. * @returns {Function} wrapped function
  222. */
  223. function wrapGetUserMedia(getUserMedia) {
  224. return function (constraints, successCallback, errorCallback) {
  225. getUserMedia(constraints, function (stream) {
  226. maybeApply(successCallback, [stream]);
  227. if (!getUserMediaStatus.initialized) {
  228. getUserMediaStatus.initialized = true;
  229. getUserMediaStatus.callbacks.forEach(function (callback) {
  230. callback();
  231. });
  232. getUserMediaStatus.callbacks.length = 0;
  233. }
  234. }, function (error) {
  235. maybeApply(errorCallback, [error]);
  236. });
  237. };
  238. }
  239. /**
  240. * Create stub device which equals to auto selected device.
  241. * @param {string} kind if that should be `audio` or `video` device
  242. * @returns {Object} stub device description in `enumerateDevices` format
  243. */
  244. function createAutoDeviceInfo(kind) {
  245. return {
  246. facing: null,
  247. label: 'Auto',
  248. kind: kind,
  249. deviceId: '',
  250. groupId: null
  251. };
  252. }
  253. /**
  254. * Execute function after getUserMedia was executed at least once.
  255. * @param {Function} callback function to execute after getUserMedia
  256. */
  257. function afterUserMediaInitialized(callback) {
  258. if (getUserMediaStatus.initialized) {
  259. callback();
  260. } else {
  261. getUserMediaStatus.callbacks.push(callback);
  262. }
  263. }
  264. /**
  265. * Wrapper function which makes enumerateDevices to wait
  266. * until someone executes getUserMedia first time.
  267. * @param {Function} enumerateDevices native function
  268. * @returns {Funtion} wrapped function
  269. */
  270. function wrapEnumerateDevices(enumerateDevices) {
  271. return function (callback) {
  272. // enumerate devices only after initial getUserMedia
  273. afterUserMediaInitialized(function () {
  274. enumerateDevices().then(function (devices) {
  275. //add auto devices
  276. devices.unshift(
  277. createAutoDeviceInfo('audioinput'),
  278. createAutoDeviceInfo('videoinput')
  279. );
  280. callback(devices);
  281. }, function (err) {
  282. console.error('cannot enumerate devices: ', err);
  283. // return only auto devices
  284. callback([createAutoDeviceInfo('audioInput'),
  285. createAutoDeviceInfo('videoinput')]);
  286. });
  287. });
  288. };
  289. }
  290. /**
  291. * Use old MediaStreamTrack to get devices list and
  292. * convert it to enumerateDevices format.
  293. * @param {Function} callback function to call when received devices list.
  294. */
  295. function enumerateDevicesThroughMediaStreamTrack (callback) {
  296. MediaStreamTrack.getSources(function (sources) {
  297. var devices = sources.map(function (source) {
  298. var kind = (source.kind || '').toLowerCase();
  299. return {
  300. facing: source.facing || null,
  301. label: source.label,
  302. kind: kind ? kind + 'input': null,
  303. deviceId: source.id,
  304. groupId: source.groupId || null
  305. };
  306. });
  307. //add auto devices
  308. devices.unshift(
  309. createAutoDeviceInfo('audioinput'),
  310. createAutoDeviceInfo('videoinput')
  311. );
  312. callback(devices);
  313. });
  314. }
  315. //Options parameter is to pass config options. Currently uses only "useIPv6".
  316. var RTCUtils = {
  317. init: function (options) {
  318. var self = this;
  319. if (RTCBrowserType.isFirefox()) {
  320. var FFversion = RTCBrowserType.getFirefoxVersion();
  321. if (FFversion >= 40) {
  322. this.peerconnection = mozRTCPeerConnection;
  323. this.getUserMedia = wrapGetUserMedia(navigator.mozGetUserMedia.bind(navigator));
  324. this.enumerateDevices = wrapEnumerateDevices(
  325. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
  326. );
  327. this.pc_constraints = {};
  328. this.attachMediaStream = function (element, stream) {
  329. // srcObject is being standardized and FF will eventually
  330. // support that unprefixed. FF also supports the
  331. // "element.src = URL.createObjectURL(...)" combo, but that
  332. // will be deprecated in favour of srcObject.
  333. //
  334. // https://groups.google.com/forum/#!topic/mozilla.dev.media/pKOiioXonJg
  335. // https://github.com/webrtc/samples/issues/302
  336. if (!element[0])
  337. return;
  338. element[0].mozSrcObject = stream;
  339. element[0].play();
  340. };
  341. this.getStreamID = function (stream) {
  342. var id = stream.id;
  343. if (!id) {
  344. var tracks = stream.getVideoTracks();
  345. if (!tracks || tracks.length === 0) {
  346. tracks = stream.getAudioTracks();
  347. }
  348. id = tracks[0].id;
  349. }
  350. return SDPUtil.filter_special_chars(id);
  351. };
  352. this.getVideoSrc = function (element) {
  353. if (!element)
  354. return null;
  355. return element.mozSrcObject;
  356. };
  357. this.setVideoSrc = function (element, src) {
  358. if (element)
  359. element.mozSrcObject = src;
  360. };
  361. RTCSessionDescription = mozRTCSessionDescription;
  362. RTCIceCandidate = mozRTCIceCandidate;
  363. } else {
  364. logger.error(
  365. "Firefox version too old: " + FFversion + ". Required >= 40.");
  366. window.location.href = 'unsupported_browser.html';
  367. return;
  368. }
  369. } else if (RTCBrowserType.isChrome() || RTCBrowserType.isOpera()) {
  370. this.peerconnection = webkitRTCPeerConnection;
  371. var getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  372. if (navigator.mediaDevices) {
  373. this.getUserMedia = wrapGetUserMedia(getUserMedia);
  374. this.enumerateDevices = wrapEnumerateDevices(
  375. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
  376. );
  377. } else {
  378. this.getUserMedia = getUserMedia;
  379. this.enumerateDevices = enumerateDevicesThroughMediaStreamTrack;
  380. }
  381. this.attachMediaStream = function (element, stream) {
  382. element.attr('src', webkitURL.createObjectURL(stream));
  383. };
  384. this.getStreamID = function (stream) {
  385. // streams from FF endpoints have the characters '{' and '}'
  386. // that make jQuery choke.
  387. return SDPUtil.filter_special_chars(stream.id);
  388. };
  389. this.getVideoSrc = function (element) {
  390. if (!element)
  391. return null;
  392. return element.getAttribute("src");
  393. };
  394. this.setVideoSrc = function (element, src) {
  395. if (element)
  396. element.setAttribute("src", src);
  397. };
  398. // DTLS should now be enabled by default but..
  399. this.pc_constraints = {'optional': [
  400. {'DtlsSrtpKeyAgreement': 'true'}
  401. ]};
  402. if (options.useIPv6) {
  403. // https://code.google.com/p/webrtc/issues/detail?id=2828
  404. this.pc_constraints.optional.push({googIPv6: true});
  405. }
  406. if (RTCBrowserType.isAndroid()) {
  407. this.pc_constraints = {}; // disable DTLS on Android
  408. }
  409. if (!webkitMediaStream.prototype.getVideoTracks) {
  410. webkitMediaStream.prototype.getVideoTracks = function () {
  411. return this.videoTracks;
  412. };
  413. }
  414. if (!webkitMediaStream.prototype.getAudioTracks) {
  415. webkitMediaStream.prototype.getAudioTracks = function () {
  416. return this.audioTracks;
  417. };
  418. }
  419. }
  420. // Detect IE/Safari
  421. else if (RTCBrowserType.isTemasysPluginUsed()) {
  422. //AdapterJS.WebRTCPlugin.setLogLevel(
  423. // AdapterJS.WebRTCPlugin.PLUGIN_LOG_LEVELS.VERBOSE);
  424. AdapterJS.webRTCReady(function (isPlugin) {
  425. self.peerconnection = RTCPeerConnection;
  426. self.getUserMedia = window.getUserMedia;
  427. self.enumerateDevices = enumerateDevicesThroughMediaStreamTrack;
  428. self.attachMediaStream = function (elSel, stream) {
  429. if (stream.id === "dummyAudio" || stream.id === "dummyVideo") {
  430. return;
  431. }
  432. attachMediaStream(elSel[0], stream);
  433. };
  434. self.getStreamID = function (stream) {
  435. var id = SDPUtil.filter_special_chars(stream.label);
  436. return id;
  437. };
  438. self.getVideoSrc = function (element) {
  439. if (!element) {
  440. logger.warn("Attempt to get video SRC of null element");
  441. return null;
  442. }
  443. var children = element.children;
  444. for (var i = 0; i !== children.length; ++i) {
  445. if (children[i].name === 'streamId') {
  446. return children[i].value;
  447. }
  448. }
  449. //logger.info(element.id + " SRC: " + src);
  450. return null;
  451. };
  452. self.setVideoSrc = function (element, src) {
  453. //logger.info("Set video src: ", element, src);
  454. if (!src) {
  455. logger.warn("Not attaching video stream, 'src' is null");
  456. return;
  457. }
  458. AdapterJS.WebRTCPlugin.WaitForPluginReady();
  459. var stream = AdapterJS.WebRTCPlugin.plugin
  460. .getStreamWithId(AdapterJS.WebRTCPlugin.pageId, src);
  461. attachMediaStream(element, stream);
  462. };
  463. onReady(isPlugin);
  464. });
  465. } else {
  466. try {
  467. logger.error('Browser does not appear to be WebRTC-capable');
  468. } catch (e) {
  469. }
  470. return;
  471. }
  472. // Call onReady() if Temasys plugin is not used
  473. if (!RTCBrowserType.isTemasysPluginUsed()) {
  474. onReady();
  475. }
  476. },
  477. /**
  478. * @param {string[]} um required user media types
  479. * @param {function} success_callback
  480. * @param {Function} failure_callback
  481. * @param {Object} [options] optional parameters
  482. * @param {string} options.resolution
  483. * @param {number} options.bandwidth
  484. * @param {number} options.fps
  485. * @param {string} options.desktopStream
  486. * @param {string} options.cameraDeviceId
  487. * @param {string} options.micDeviceId
  488. **/
  489. getUserMediaWithConstraints: function ( um, success_callback, failure_callback, options) {
  490. options = options || {};
  491. resolution = options.resolution;
  492. var constraints = getConstraints(
  493. um, options);
  494. logger.info("Get media constraints", constraints);
  495. try {
  496. this.getUserMedia(constraints,
  497. function (stream) {
  498. logger.log('onUserMediaSuccess');
  499. setAvailableDevices(um, true);
  500. success_callback(stream);
  501. },
  502. function (error) {
  503. setAvailableDevices(um, false);
  504. logger.warn('Failed to get access to local media. Error ',
  505. error, constraints);
  506. if (failure_callback) {
  507. failure_callback(error, resolution);
  508. }
  509. });
  510. } catch (e) {
  511. logger.error('GUM failed: ', e);
  512. if (failure_callback) {
  513. failure_callback(e);
  514. }
  515. }
  516. },
  517. /**
  518. * Creates the local MediaStreams.
  519. * @param {Object} [options] optional parameters
  520. * @param {Array} options.devices the devices that will be requested
  521. * @param {string} options.resolution resolution constraints
  522. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  523. * type: "audio" or "video", videoType: "camera" or "desktop"}
  524. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  525. * @param {string} options.cameraDeviceId
  526. * @param {string} options.micDeviceId
  527. * @returns {*} Promise object that will receive the new JitsiTracks
  528. */
  529. obtainAudioAndVideoPermissions: function (options) {
  530. var self = this;
  531. options = options || {};
  532. return new Promise(function (resolve, reject) {
  533. var successCallback = function (stream) {
  534. var streams = self.successCallback(stream, options.resolution);
  535. resolve(options.dontCreateJitsiTracks?
  536. streams: self.createLocalTracks(streams));
  537. };
  538. options.devices = options.devices || ['audio', 'video'];
  539. if (RTCBrowserType.isFirefox() || RTCBrowserType.isTemasysPluginUsed()) {
  540. // With FF/IE we can't split the stream into audio and video because FF
  541. // doesn't support media stream constructors. So, we need to get the
  542. // audio stream separately from the video stream using two distinct GUM
  543. // calls. Not very user friendly :-( but we don't have many other
  544. // options neither.
  545. //
  546. // Note that we pack those 2 streams in a single object and pass it to
  547. // the successCallback method.
  548. var obtainVideo = function (audioStream) {
  549. self.getUserMediaWithConstraints(
  550. ['video'],
  551. function (videoStream) {
  552. return successCallback({
  553. audioStream: audioStream,
  554. videoStream: videoStream
  555. });
  556. },
  557. function (error, resolution) {
  558. logger.error(
  559. 'failed to obtain video stream - stop', error);
  560. self.errorCallback(error, resolve, options);
  561. },
  562. {resolution: options.resolution || '360',
  563. cameraDeviceId: options.cameraDeviceId});
  564. };
  565. var obtainAudio = function () {
  566. self.getUserMediaWithConstraints(
  567. ['audio'],
  568. function (audioStream) {
  569. (options.devices.indexOf('video') === -1) ||
  570. obtainVideo(audioStream);
  571. },
  572. function (error) {
  573. logger.error(
  574. 'failed to obtain audio stream - stop', error);
  575. self.errorCallback(error, resolve, options);
  576. },{micDeviceId: options.micDeviceId});
  577. };
  578. if((options.devices.indexOf('audio') === -1))
  579. obtainVideo(null)
  580. else
  581. obtainAudio();
  582. } else {
  583. this.getUserMediaWithConstraints(
  584. options.devices,
  585. function (stream) {
  586. successCallback(stream);
  587. },
  588. function (error, resolution) {
  589. self.errorCallback(error, resolve, options);
  590. },
  591. {resolution: options.resolution || '360',
  592. cameraDeviceId: options.cameraDeviceId,
  593. micDeviceId: options.micDeviceId});
  594. }
  595. }.bind(this));
  596. },
  597. /**
  598. * Successful callback called from GUM.
  599. * @param stream the new MediaStream
  600. * @param resolution the resolution of the video stream.
  601. * @returns {*}
  602. */
  603. successCallback: function (stream, resolution) {
  604. // If this is FF or IE, the stream parameter is *not* a MediaStream object,
  605. // it's an object with two properties: audioStream, videoStream.
  606. if (stream && stream.getAudioTracks && stream.getVideoTracks)
  607. logger.log('got', stream, stream.getAudioTracks().length,
  608. stream.getVideoTracks().length);
  609. return this.handleLocalStream(stream, resolution);
  610. },
  611. /**
  612. * Error callback called from GUM. Retries the GUM call with different resolutions.
  613. * @param error the error
  614. * @param resolve the resolve funtion that will be called on success.
  615. * @param {Object} options with the following properties:
  616. * @param resolution the last resolution used for GUM.
  617. * @param dontCreateJitsiTracks if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  618. * type: "audio" or "video", videoType: "camera" or "desktop"}
  619. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  620. */
  621. errorCallback: function (error, resolve, options) {
  622. var self = this;
  623. options = options || {};
  624. logger.error('failed to obtain audio/video stream - trying audio only', error);
  625. var resolution = getPreviousResolution(options.resolution);
  626. if (typeof error == "object" && error.constraintName && error.name
  627. && (error.name == "ConstraintNotSatisfiedError" ||
  628. error.name == "OverconstrainedError") &&
  629. (error.constraintName == "minWidth" || error.constraintName == "maxWidth" ||
  630. error.constraintName == "minHeight" || error.constraintName == "maxHeight")
  631. && resolution) {
  632. self.getUserMediaWithConstraints(['audio', 'video'],
  633. function (stream) {
  634. var streams = self.successCallback(stream, resolution);
  635. resolve(options.dontCreateJitsiTracks? streams: self.createLocalTracks(streams));
  636. }, function (error, resolution) {
  637. return self.errorCallback(error, resolve,
  638. {resolution: resolution,
  639. dontCreateJitsiTracks: options.dontCreateJitsiTracks});
  640. },
  641. {resolution: options.resolution});
  642. }
  643. else {
  644. self.getUserMediaWithConstraints(
  645. ['audio'],
  646. function (stream) {
  647. var streams = self.successCallback(stream, resolution);
  648. resolve(options.dontCreateJitsiTracks? streams: self.createLocalTracks(streams));
  649. },
  650. function (error) {
  651. logger.error('failed to obtain audio/video stream - stop',
  652. error);
  653. var streams = self.successCallback(null);
  654. resolve(options.dontCreateJitsiTracks? streams: self.createLocalTracks(streams));
  655. }
  656. );
  657. }
  658. },
  659. /**
  660. * Handles the newly created Media Streams.
  661. * @param stream the new Media Streams
  662. * @param resolution the resolution of the video stream.
  663. * @returns {*[]} Promise object with the new Media Streams.
  664. */
  665. handleLocalStream: function (stream, resolution) {
  666. var audioStream, videoStream;
  667. // If this is FF, the stream parameter is *not* a MediaStream object, it's
  668. // an object with two properties: audioStream, videoStream.
  669. if (window.webkitMediaStream) {
  670. audioStream = new webkitMediaStream();
  671. videoStream = new webkitMediaStream();
  672. if (stream) {
  673. var audioTracks = stream.getAudioTracks();
  674. for (var i = 0; i < audioTracks.length; i++) {
  675. audioStream.addTrack(audioTracks[i]);
  676. }
  677. var videoTracks = stream.getVideoTracks();
  678. for (i = 0; i < videoTracks.length; i++) {
  679. videoStream.addTrack(videoTracks[i]);
  680. }
  681. }
  682. }
  683. else if (RTCBrowserType.isFirefox() || RTCBrowserType.isTemasysPluginUsed()) { // Firefox and Temasys plugin
  684. if (stream && stream.audioStream)
  685. audioStream = stream.audioStream;
  686. else
  687. audioStream = new DummyMediaStream("dummyAudio");
  688. if (stream && stream.videoStream)
  689. videoStream = stream.videoStream;
  690. else
  691. videoStream = new DummyMediaStream("dummyVideo");
  692. }
  693. return [
  694. {stream: audioStream, type: "audio", videoType: null},
  695. {stream: videoStream, type: "video", videoType: "camera",
  696. resolution: resolution}
  697. ];
  698. },
  699. createStream: function (stream, isVideo) {
  700. var newStream = null;
  701. if (window.webkitMediaStream) {
  702. newStream = new webkitMediaStream();
  703. if (newStream) {
  704. var tracks = (isVideo ? stream.getVideoTracks() : stream.getAudioTracks());
  705. for (var i = 0; i < tracks.length; i++) {
  706. newStream.addTrack(tracks[i]);
  707. }
  708. }
  709. } else {
  710. // FIXME: this is duplicated with 'handleLocalStream' !!!
  711. if (stream) {
  712. newStream = stream;
  713. } else {
  714. newStream =
  715. new DummyMediaStream(isVideo ? "dummyVideo" : "dummyAudio");
  716. }
  717. }
  718. return newStream;
  719. },
  720. addListener: function (eventType, listener) {
  721. eventEmitter.on(eventType, listener);
  722. },
  723. removeListener: function (eventType, listener) {
  724. eventEmitter.removeListener(eventType, listener);
  725. },
  726. getDeviceAvailability: function () {
  727. return devices;
  728. },
  729. isRTCReady: function () {
  730. return rtcReady;
  731. },
  732. createLocalTracks: function (streams) {
  733. var newStreams = []
  734. for (var i = 0; i < streams.length; i++) {
  735. var localStream = new JitsiLocalTrack(null, streams[i].stream,
  736. eventEmitter, streams[i].videoType, streams[i].resolution);
  737. newStreams.push(localStream);
  738. if (streams[i].isMuted === true)
  739. localStream.setMute(true);
  740. var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
  741. eventEmitter.emit(eventType, localStream);
  742. }
  743. return newStreams;
  744. },
  745. /**
  746. * Checks if its possible to enumerate available cameras/micropones.
  747. * @returns {boolean} true if available, false otherwise.
  748. */
  749. isDeviceListAvailable: function () {
  750. var isEnumerateDevicesAvailable = navigator.mediaDevices && navigator.mediaDevices.enumerateDevices;
  751. if (isEnumerateDevicesAvailable) {
  752. return true;
  753. }
  754. return (MediaStreamTrack && MediaStreamTrack.getSources)? true : false;
  755. },
  756. /**
  757. * A method to handle stopping of the stream.
  758. * One point to handle the differences in various implementations.
  759. * @param mediaStream MediaStream object to stop.
  760. */
  761. stopMediaStream: function (mediaStream) {
  762. mediaStream.getTracks().forEach(function (track) {
  763. // stop() not supported with IE
  764. if (track.stop) {
  765. track.stop();
  766. }
  767. });
  768. // leave stop for implementation still using it
  769. if (mediaStream.stop) {
  770. mediaStream.stop();
  771. }
  772. }
  773. };
  774. module.exports = RTCUtils;