Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RTCUtils.js 47KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /* global config, require, attachMediaStream, getUserMedia,
  2. RTCPeerConnection, RTCSessionDescription, RTCIceCandidate, MediaStreamTrack,
  3. mozRTCPeerConnection, mozRTCSessionDescription, mozRTCIceCandidate,
  4. webkitRTCPeerConnection, webkitMediaStream, webkitURL
  5. */
  6. /* jshint -W101 */
  7. var logger = require("jitsi-meet-logger").getLogger(__filename);
  8. var RTCBrowserType = require("./RTCBrowserType");
  9. var Resolutions = require("../../service/RTC/Resolutions");
  10. var RTCEvents = require("../../service/RTC/RTCEvents");
  11. var AdapterJS = require("./adapter.screenshare");
  12. var SDPUtil = require("../xmpp/SDPUtil");
  13. var EventEmitter = require("events");
  14. var screenObtainer = require("./ScreenObtainer");
  15. var JitsiTrackErrors = require("../../JitsiTrackErrors");
  16. var JitsiTrackError = require("../../JitsiTrackError");
  17. var MediaType = require("../../service/RTC/MediaType");
  18. var VideoType = require("../../service/RTC/VideoType");
  19. var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
  20. var eventEmitter = new EventEmitter();
  21. var AVAILABLE_DEVICES_POLL_INTERVAL_TIME = 3000; // ms
  22. var devices = {
  23. audio: false,
  24. video: false
  25. };
  26. // Currently audio output device change is supported only in Chrome and
  27. // default output always has 'default' device ID
  28. var audioOutputDeviceId = 'default'; // default device
  29. // Disables Acoustic Echo Cancellation
  30. var disableAEC = false;
  31. // Disables Noise Suppression
  32. var disableNS = false;
  33. var featureDetectionAudioEl = document.createElement('audio');
  34. var isAudioOutputDeviceChangeAvailable =
  35. typeof featureDetectionAudioEl.setSinkId !== 'undefined';
  36. var currentlyAvailableMediaDevices;
  37. var rawEnumerateDevicesWithCallback = navigator.mediaDevices
  38. && navigator.mediaDevices.enumerateDevices
  39. ? function(callback) {
  40. navigator.mediaDevices.enumerateDevices().then(callback, function () {
  41. callback([]);
  42. });
  43. }
  44. : (MediaStreamTrack && MediaStreamTrack.getSources)
  45. ? function (callback) {
  46. MediaStreamTrack.getSources(function (sources) {
  47. callback(sources.map(convertMediaStreamTrackSource));
  48. });
  49. }
  50. : undefined;
  51. // TODO: currently no browser supports 'devicechange' event even in nightly
  52. // builds so no feature/browser detection is used at all. However in future this
  53. // should be changed to some expression. Progress on 'devicechange' event
  54. // implementation for Chrome/Opera/NWJS can be tracked at
  55. // https://bugs.chromium.org/p/chromium/issues/detail?id=388648, for Firefox -
  56. // at https://bugzilla.mozilla.org/show_bug.cgi?id=1152383. More information on
  57. // 'devicechange' event can be found in spec -
  58. // http://w3c.github.io/mediacapture-main/#event-mediadevices-devicechange
  59. // TODO: check MS Edge
  60. var isDeviceChangeEventSupported = false;
  61. var rtcReady = false;
  62. function setResolutionConstraints(constraints, resolution) {
  63. var isAndroid = RTCBrowserType.isAndroid();
  64. if (Resolutions[resolution]) {
  65. constraints.video.mandatory.minWidth = Resolutions[resolution].width;
  66. constraints.video.mandatory.minHeight = Resolutions[resolution].height;
  67. }
  68. else if (isAndroid) {
  69. // FIXME can't remember if the purpose of this was to always request
  70. // low resolution on Android ? if yes it should be moved up front
  71. constraints.video.mandatory.minWidth = 320;
  72. constraints.video.mandatory.minHeight = 180;
  73. constraints.video.mandatory.maxFrameRate = 15;
  74. }
  75. if (constraints.video.mandatory.minWidth)
  76. constraints.video.mandatory.maxWidth =
  77. constraints.video.mandatory.minWidth;
  78. if (constraints.video.mandatory.minHeight)
  79. constraints.video.mandatory.maxHeight =
  80. constraints.video.mandatory.minHeight;
  81. }
  82. /**
  83. * @param {string[]} um required user media types
  84. *
  85. * @param {Object} [options={}] optional parameters
  86. * @param {string} options.resolution
  87. * @param {number} options.bandwidth
  88. * @param {number} options.fps
  89. * @param {string} options.desktopStream
  90. * @param {string} options.cameraDeviceId
  91. * @param {string} options.micDeviceId
  92. * @param {'user'|'environment'} options.facingMode
  93. * @param {bool} firefox_fake_device
  94. */
  95. function getConstraints(um, options) {
  96. var constraints = {audio: false, video: false};
  97. // Don't mix new and old style settings for Chromium as this leads
  98. // to TypeError in new Chromium versions. @see
  99. // https://bugs.chromium.org/p/chromium/issues/detail?id=614716
  100. // This is a temporary solution, in future we will fully split old and
  101. // new style constraints when new versions of Chromium and Firefox will
  102. // have stable support of new constraints format. For more information
  103. // @see https://github.com/jitsi/lib-jitsi-meet/pull/136
  104. var isNewStyleConstraintsSupported =
  105. RTCBrowserType.isFirefox() ||
  106. RTCBrowserType.isReactNative() ||
  107. RTCBrowserType.isTemasysPluginUsed();
  108. if (um.indexOf('video') >= 0) {
  109. // same behaviour as true
  110. constraints.video = { mandatory: {}, optional: [] };
  111. if (options.cameraDeviceId) {
  112. if (isNewStyleConstraintsSupported) {
  113. // New style of setting device id.
  114. constraints.video.deviceId = options.cameraDeviceId;
  115. }
  116. // Old style.
  117. constraints.video.optional.push({
  118. sourceId: options.cameraDeviceId
  119. });
  120. } else {
  121. // Prefer the front i.e. user-facing camera (to the back i.e.
  122. // environment-facing camera, for example).
  123. // TODO: Maybe use "exact" syntax if options.facingMode is defined,
  124. // but this probably needs to be decided when updating other
  125. // constraints, as we currently don't use "exact" syntax anywhere.
  126. if (isNewStyleConstraintsSupported) {
  127. constraints.video.facingMode = options.facingMode || 'user';
  128. }
  129. constraints.video.optional.push({
  130. facingMode: options.facingMode || 'user'
  131. });
  132. }
  133. constraints.video.optional.push({ googLeakyBucket: true });
  134. setResolutionConstraints(constraints, options.resolution);
  135. }
  136. if (um.indexOf('audio') >= 0) {
  137. if (RTCBrowserType.isReactNative()) {
  138. // The react-native-webrtc project that we're currently using
  139. // expects the audio constraint to be a boolean.
  140. constraints.audio = true;
  141. } else if (!RTCBrowserType.isFirefox()) {
  142. // same behaviour as true
  143. constraints.audio = { mandatory: {}, optional: []};
  144. if (options.micDeviceId) {
  145. if (isNewStyleConstraintsSupported) {
  146. // New style of setting device id.
  147. constraints.audio.deviceId = options.micDeviceId;
  148. }
  149. // Old style.
  150. constraints.audio.optional.push({
  151. sourceId: options.micDeviceId
  152. });
  153. }
  154. // if it is good enough for hangouts...
  155. constraints.audio.optional.push(
  156. {googEchoCancellation: !disableAEC},
  157. {googAutoGainControl: true},
  158. {googNoiseSupression: !disableNS},
  159. {googHighpassFilter: true},
  160. {googNoiseSuppression2: !disableNS},
  161. {googEchoCancellation2: !disableAEC},
  162. {googAutoGainControl2: true}
  163. );
  164. } else {
  165. if (options.micDeviceId) {
  166. constraints.audio = {
  167. mandatory: {},
  168. deviceId: options.micDeviceId, // new style
  169. optional: [{
  170. sourceId: options.micDeviceId // old style
  171. }]};
  172. } else {
  173. constraints.audio = true;
  174. }
  175. }
  176. }
  177. if (um.indexOf('screen') >= 0) {
  178. if (RTCBrowserType.isChrome()) {
  179. constraints.video = {
  180. mandatory: {
  181. chromeMediaSource: "screen",
  182. googLeakyBucket: true,
  183. maxWidth: window.screen.width,
  184. maxHeight: window.screen.height,
  185. maxFrameRate: 3
  186. },
  187. optional: []
  188. };
  189. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  190. constraints.video = {
  191. optional: [
  192. {
  193. sourceId: AdapterJS.WebRTCPlugin.plugin.screensharingKey
  194. }
  195. ]
  196. };
  197. } else if (RTCBrowserType.isFirefox()) {
  198. constraints.video = {
  199. mozMediaSource: "window",
  200. mediaSource: "window"
  201. };
  202. } else {
  203. var errmsg
  204. = "'screen' WebRTC media source is supported only in Chrome"
  205. + " and with Temasys plugin";
  206. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  207. logger.error(errmsg);
  208. }
  209. }
  210. if (um.indexOf('desktop') >= 0) {
  211. constraints.video = {
  212. mandatory: {
  213. chromeMediaSource: "desktop",
  214. chromeMediaSourceId: options.desktopStream,
  215. googLeakyBucket: true,
  216. maxWidth: window.screen.width,
  217. maxHeight: window.screen.height,
  218. maxFrameRate: 3
  219. },
  220. optional: []
  221. };
  222. }
  223. if (options.bandwidth) {
  224. if (!constraints.video) {
  225. //same behaviour as true
  226. constraints.video = {mandatory: {}, optional: []};
  227. }
  228. constraints.video.optional.push({bandwidth: options.bandwidth});
  229. }
  230. if(options.minFps || options.maxFps || options.fps) {
  231. // for some cameras it might be necessary to request 30fps
  232. // so they choose 30fps mjpg over 10fps yuy2
  233. if (!constraints.video) {
  234. // same behaviour as true;
  235. constraints.video = {mandatory: {}, optional: []};
  236. }
  237. if(options.minFps || options.fps) {
  238. options.minFps = options.minFps || options.fps; //Fall back to options.fps for backwards compatibility
  239. constraints.video.mandatory.minFrameRate = options.minFps;
  240. }
  241. if(options.maxFps) {
  242. constraints.video.mandatory.maxFrameRate = options.maxFps;
  243. }
  244. }
  245. // we turn audio for both audio and video tracks, the fake audio & video seems to work
  246. // only when enabled in one getUserMedia call, we cannot get fake audio separate by fake video
  247. // this later can be a problem with some of the tests
  248. if(RTCBrowserType.isFirefox() && options.firefox_fake_device)
  249. {
  250. // seems to be fixed now, removing this experimental fix, as having
  251. // multiple audio tracks brake the tests
  252. //constraints.audio = true;
  253. constraints.fake = true;
  254. }
  255. return constraints;
  256. }
  257. function setAvailableDevices(um, available) {
  258. if (um.indexOf("video") != -1) {
  259. devices.video = available;
  260. }
  261. if (um.indexOf("audio") != -1) {
  262. devices.audio = available;
  263. }
  264. eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, devices);
  265. }
  266. /**
  267. * Checks if new list of available media devices differs from previous one.
  268. * @param {MediaDeviceInfo[]} newDevices - list of new devices.
  269. * @returns {boolean} - true if list is different, false otherwise.
  270. */
  271. function compareAvailableMediaDevices(newDevices) {
  272. if (newDevices.length !== currentlyAvailableMediaDevices.length) {
  273. return true;
  274. }
  275. return newDevices.map(mediaDeviceInfoToJSON).sort().join('') !==
  276. currentlyAvailableMediaDevices.map(mediaDeviceInfoToJSON).sort().join('');
  277. function mediaDeviceInfoToJSON(info) {
  278. return JSON.stringify({
  279. kind: info.kind,
  280. deviceId: info.deviceId,
  281. groupId: info.groupId,
  282. label: info.label,
  283. facing: info.facing
  284. });
  285. }
  286. }
  287. /**
  288. * Periodically polls enumerateDevices() method to check if list of media
  289. * devices has changed. This is temporary workaround until 'devicechange' event
  290. * will be supported by browsers.
  291. */
  292. function pollForAvailableMediaDevices() {
  293. // Here we use plain navigator.mediaDevices.enumerateDevices instead of
  294. // wrapped because we just need to know the fact the devices changed, labels
  295. // do not matter. This fixes situation when we have no devices initially,
  296. // and then plug in a new one.
  297. if (rawEnumerateDevicesWithCallback) {
  298. rawEnumerateDevicesWithCallback(function (devices) {
  299. // We don't fire RTCEvents.DEVICE_LIST_CHANGED for the first time
  300. // we call enumerateDevices(). This is the initial step.
  301. if (typeof currentlyAvailableMediaDevices === 'undefined') {
  302. currentlyAvailableMediaDevices = devices.slice(0);
  303. } else if (compareAvailableMediaDevices(devices)) {
  304. onMediaDevicesListChanged(devices);
  305. }
  306. window.setTimeout(pollForAvailableMediaDevices,
  307. AVAILABLE_DEVICES_POLL_INTERVAL_TIME);
  308. });
  309. }
  310. }
  311. /**
  312. * Event handler for the 'devicechange' event.
  313. * @param {MediaDeviceInfo[]} devices - list of media devices.
  314. * @emits RTCEvents.DEVICE_LIST_CHANGED
  315. */
  316. function onMediaDevicesListChanged(devices) {
  317. currentlyAvailableMediaDevices = devices.slice(0);
  318. logger.info('list of media devices has changed:', currentlyAvailableMediaDevices);
  319. var videoInputDevices = currentlyAvailableMediaDevices.filter(function (d) {
  320. return d.kind === 'videoinput';
  321. }),
  322. audioInputDevices = currentlyAvailableMediaDevices.filter(function (d) {
  323. return d.kind === 'audioinput';
  324. }),
  325. videoInputDevicesWithEmptyLabels = videoInputDevices.filter(
  326. function (d) {
  327. return d.label === '';
  328. }),
  329. audioInputDevicesWithEmptyLabels = audioInputDevices.filter(
  330. function (d) {
  331. return d.label === '';
  332. });
  333. if (videoInputDevices.length &&
  334. videoInputDevices.length === videoInputDevicesWithEmptyLabels.length) {
  335. setAvailableDevices(['video'], false);
  336. }
  337. if (audioInputDevices.length &&
  338. audioInputDevices.length === audioInputDevicesWithEmptyLabels.length) {
  339. setAvailableDevices(['audio'], false);
  340. }
  341. eventEmitter.emit(RTCEvents.DEVICE_LIST_CHANGED, devices);
  342. }
  343. // In case of IE we continue from 'onReady' callback
  344. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  345. // once it is initialized.
  346. function onReady (options, GUM) {
  347. rtcReady = true;
  348. eventEmitter.emit(RTCEvents.RTC_READY, true);
  349. screenObtainer.init(options, GUM);
  350. if (isDeviceChangeEventSupported && RTCUtils.isDeviceListAvailable()) {
  351. navigator.mediaDevices.addEventListener('devicechange', function () {
  352. RTCUtils.enumerateDevices(onMediaDevicesListChanged);
  353. });
  354. } else if (RTCUtils.isDeviceListAvailable()) {
  355. pollForAvailableMediaDevices();
  356. }
  357. }
  358. /**
  359. * Apply function with arguments if function exists.
  360. * Do nothing if function not provided.
  361. * @param {function} [fn] function to apply
  362. * @param {Array} [args=[]] arguments for function
  363. */
  364. function maybeApply(fn, args) {
  365. if (fn) {
  366. fn.apply(null, args || []);
  367. }
  368. }
  369. var getUserMediaStatus = {
  370. initialized: false,
  371. callbacks: []
  372. };
  373. /**
  374. * Wrap `getUserMedia` to allow others to know if it was executed at least
  375. * once or not. Wrapper function uses `getUserMediaStatus` object.
  376. * @param {Function} getUserMedia native function
  377. * @returns {Function} wrapped function
  378. */
  379. function wrapGetUserMedia(getUserMedia) {
  380. return function (constraints, successCallback, errorCallback) {
  381. getUserMedia(constraints, function (stream) {
  382. maybeApply(successCallback, [stream]);
  383. if (!getUserMediaStatus.initialized) {
  384. getUserMediaStatus.initialized = true;
  385. getUserMediaStatus.callbacks.forEach(function (callback) {
  386. callback();
  387. });
  388. getUserMediaStatus.callbacks.length = 0;
  389. }
  390. }, function (error) {
  391. maybeApply(errorCallback, [error]);
  392. });
  393. };
  394. }
  395. /**
  396. * Execute function after getUserMedia was executed at least once.
  397. * @param {Function} callback function to execute after getUserMedia
  398. */
  399. function afterUserMediaInitialized(callback) {
  400. if (getUserMediaStatus.initialized) {
  401. callback();
  402. } else {
  403. getUserMediaStatus.callbacks.push(callback);
  404. }
  405. }
  406. /**
  407. * Wrapper function which makes enumerateDevices to wait
  408. * until someone executes getUserMedia first time.
  409. * @param {Function} enumerateDevices native function
  410. * @returns {Funtion} wrapped function
  411. */
  412. function wrapEnumerateDevices(enumerateDevices) {
  413. return function (callback) {
  414. // enumerate devices only after initial getUserMedia
  415. afterUserMediaInitialized(function () {
  416. enumerateDevices().then(callback, function (err) {
  417. logger.error('cannot enumerate devices: ', err);
  418. callback([]);
  419. });
  420. });
  421. };
  422. }
  423. /**
  424. * Use old MediaStreamTrack to get devices list and
  425. * convert it to enumerateDevices format.
  426. * @param {Function} callback function to call when received devices list.
  427. */
  428. function enumerateDevicesThroughMediaStreamTrack (callback) {
  429. MediaStreamTrack.getSources(function (sources) {
  430. callback(sources.map(convertMediaStreamTrackSource));
  431. });
  432. }
  433. /**
  434. * Converts MediaStreamTrack Source to enumerateDevices format.
  435. * @param {Object} source
  436. */
  437. function convertMediaStreamTrackSource(source) {
  438. var kind = (source.kind || '').toLowerCase();
  439. return {
  440. facing: source.facing || null,
  441. label: source.label,
  442. // theoretically deprecated MediaStreamTrack.getSources should
  443. // not return 'audiooutput' devices but let's handle it in any
  444. // case
  445. kind: kind
  446. ? (kind === 'audiooutput' ? kind : kind + 'input')
  447. : null,
  448. deviceId: source.id,
  449. groupId: source.groupId || null
  450. };
  451. }
  452. function obtainDevices(options) {
  453. if(!options.devices || options.devices.length === 0) {
  454. return options.successCallback(options.streams || {});
  455. }
  456. var device = options.devices.splice(0, 1);
  457. var devices = [];
  458. devices.push(device);
  459. options.deviceGUM[device](function (stream) {
  460. options.streams = options.streams || {};
  461. options.streams[device] = stream;
  462. obtainDevices(options);
  463. },
  464. function (error) {
  465. Object.keys(options.streams).forEach(function(device) {
  466. RTCUtils.stopMediaStream(options.streams[device]);
  467. });
  468. logger.error(
  469. "failed to obtain " + device + " stream - stop", error);
  470. options.errorCallback(error);
  471. });
  472. }
  473. /**
  474. * Handles the newly created Media Streams.
  475. * @param streams the new Media Streams
  476. * @param resolution the resolution of the video streams
  477. * @returns {*[]} object that describes the new streams
  478. */
  479. function handleLocalStream(streams, resolution) {
  480. var audioStream, videoStream, desktopStream, res = [];
  481. // XXX The function obtainAudioAndVideoPermissions has examined the type of
  482. // the browser, its capabilities, etc. and has taken the decision whether to
  483. // invoke getUserMedia per device (e.g. Firefox) or once for both audio and
  484. // video (e.g. Chrome). In order to not duplicate the logic here, examine
  485. // the specified streams and figure out what we've received based on
  486. // obtainAudioAndVideoPermissions' decision.
  487. if (streams) {
  488. // As mentioned above, certian types of browser (e.g. Chrome) support
  489. // (with a result which meets our requirements expressed bellow) calling
  490. // getUserMedia once for both audio and video.
  491. var audioVideo = streams.audioVideo;
  492. if (audioVideo) {
  493. var audioTracks = audioVideo.getAudioTracks();
  494. if (audioTracks.length) {
  495. audioStream = new webkitMediaStream();
  496. for (var i = 0; i < audioTracks.length; i++) {
  497. audioStream.addTrack(audioTracks[i]);
  498. }
  499. }
  500. var videoTracks = audioVideo.getVideoTracks();
  501. if (videoTracks.length) {
  502. videoStream = new webkitMediaStream();
  503. for (var j = 0; j < videoTracks.length; j++) {
  504. videoStream.addTrack(videoTracks[j]);
  505. }
  506. }
  507. } else {
  508. // On other types of browser (e.g. Firefox) we choose (namely,
  509. // obtainAudioAndVideoPermissions) to call getUsermedia per device
  510. // (type).
  511. audioStream = streams.audio;
  512. videoStream = streams.video;
  513. }
  514. // Again, different choices on different types of browser.
  515. desktopStream = streams.desktopStream || streams.desktop;
  516. }
  517. if (desktopStream) {
  518. res.push({
  519. stream: desktopStream,
  520. track: desktopStream.getVideoTracks()[0],
  521. mediaType: MediaType.VIDEO,
  522. videoType: VideoType.DESKTOP
  523. });
  524. }
  525. if (audioStream) {
  526. res.push({
  527. stream: audioStream,
  528. track: audioStream.getAudioTracks()[0],
  529. mediaType: MediaType.AUDIO,
  530. videoType: null
  531. });
  532. }
  533. if (videoStream) {
  534. res.push({
  535. stream: videoStream,
  536. track: videoStream.getVideoTracks()[0],
  537. mediaType: MediaType.VIDEO,
  538. videoType: VideoType.CAMERA,
  539. resolution: resolution
  540. });
  541. }
  542. return res;
  543. }
  544. /**
  545. * Wraps original attachMediaStream function to set current audio output device
  546. * if this is supported.
  547. * @param {Function} origAttachMediaStream
  548. * @returns {Function}
  549. */
  550. function wrapAttachMediaStream(origAttachMediaStream) {
  551. return function(element, stream) {
  552. var res = origAttachMediaStream.apply(RTCUtils, arguments);
  553. if (stream
  554. && RTCUtils.isDeviceChangeAvailable('output')
  555. && stream.getAudioTracks
  556. && stream.getAudioTracks().length) {
  557. element.setSinkId(RTCUtils.getAudioOutputDevice())
  558. .catch(function (ex) {
  559. var err = new JitsiTrackError(ex, null, ['audiooutput']);
  560. GlobalOnErrorHandler.callUnhandledRejectionHandler(
  561. {promise: this, reason: err});
  562. logger.warn('Failed to set audio output device for the ' +
  563. 'element. Default audio output device will be used ' +
  564. 'instead',
  565. element, err);
  566. });
  567. }
  568. return res;
  569. }
  570. }
  571. /**
  572. * Represents a default implementation of {@link RTCUtils#getVideoSrc} which
  573. * tries to be browser-agnostic through feature checking. Note though that it
  574. * was not completely clear from the predating browser-specific implementations
  575. * what &quot;videoSrc&quot; was because one implementation would return
  576. * <tt>MediaStream</tt> (e.g. Firefox), another a <tt>string</tt> representation
  577. * of the <tt>URL</tt> of the <tt>MediaStream</tt> (e.g. Chrome) and the return
  578. * value was only used by {@link RTCUIHelper#getVideoId} which itself did not
  579. * appear to be used anywhere. Generally, the implementation will try to follow
  580. * the related standards i.e. work with the <tt>srcObject</tt> and <tt>src</tt>
  581. * properties of the specified <tt>element</tt> taking into account vender
  582. * prefixes.
  583. *
  584. * @param element the element to get the associated video source/src of
  585. * @return the video source/src of the specified <tt>element</tt>
  586. */
  587. function defaultGetVideoSrc(element) {
  588. // https://www.w3.org/TR/mediacapture-streams/
  589. //
  590. // User Agents that support this specification must support the srcObject
  591. // attribute of the HTMLMediaElement interface defined in [HTML51].
  592. // https://www.w3.org/TR/2015/WD-html51-20150506/semantics.html#dom-media-srcobject
  593. //
  594. // There are three ways to specify a media resource: the srcObject IDL
  595. // attribute, the src content attribute, and source elements. The IDL
  596. // attribute takes priority, followed by the content attribute, followed by
  597. // the elements.
  598. // srcObject
  599. var srcObject = element.srcObject || element.mozSrcObject;
  600. if (srcObject) {
  601. // Try the optimized path to the URL of a MediaStream.
  602. var url = srcObject.jitsiObjectURL;
  603. if (url) {
  604. return url.toString();
  605. }
  606. // Go via the unoptimized path to the URL of a MediaStream then.
  607. var URL = (window.URL || webkitURL);
  608. if (URL) {
  609. url = URL.createObjectURL(srcObject);
  610. try {
  611. return url.toString();
  612. } finally {
  613. URL.revokeObjectURL(url);
  614. }
  615. }
  616. }
  617. // src
  618. return element.src;
  619. }
  620. /**
  621. * Represents a default implementation of setting a <tt>MediaStream</tt> as the
  622. * source of a video element that tries to be browser-agnostic through feature
  623. * checking. Note though that it was not completely clear from the predating
  624. * browser-specific implementations what &quot;videoSrc&quot; was because one
  625. * implementation of {@link RTCUtils#getVideoSrc} would return
  626. * <tt>MediaStream</tt> (e.g. Firefox), another a <tt>string</tt> representation
  627. * of the <tt>URL</tt> of the <tt>MediaStream</tt> (e.g. Chrome) and the return
  628. * value was only used by {@link RTCUIHelper#getVideoId} which itself did not
  629. * appear to be used anywhere. Generally, the implementation will try to follow
  630. * the related standards i.e. work with the <tt>srcObject</tt> and <tt>src</tt>
  631. * properties of the specified <tt>element</tt> taking into account vender
  632. * prefixes.
  633. *
  634. * @param element the element whose video source/src is to be set to the
  635. * specified <tt>stream</tt>
  636. * @param {MediaStream} stream the <tt>MediaStream</tt> to set as the video
  637. * source/src of <tt>element</tt>
  638. */
  639. function defaultSetVideoSrc(element, stream) {
  640. // srcObject
  641. var srcObjectPropertyName = 'srcObject';
  642. if (!(srcObjectPropertyName in element)) {
  643. srcObjectPropertyName = 'mozSrcObject';
  644. if (!(srcObjectPropertyName in element)) {
  645. srcObjectPropertyName = null;
  646. }
  647. }
  648. if (srcObjectPropertyName) {
  649. element[srcObjectPropertyName] = stream;
  650. return;
  651. }
  652. // src
  653. var src;
  654. if (stream) {
  655. src = stream.jitsiObjectURL;
  656. // Save the created URL for stream so we can reuse it and not keep
  657. // creating URLs.
  658. if (!src) {
  659. stream.jitsiObjectURL
  660. = src
  661. = (URL || webkitURL).createObjectURL(stream);
  662. }
  663. }
  664. element.src = src || '';
  665. }
  666. //Options parameter is to pass config options. Currently uses only "useIPv6".
  667. var RTCUtils = {
  668. init: function (options) {
  669. if (typeof(options.disableAEC) === "boolean") {
  670. disableAEC = options.disableAEC;
  671. logger.info("Disable AEC: " + disableAEC);
  672. }
  673. if (typeof(options.disableNS) === "boolean") {
  674. disableNS = options.disableNS;
  675. logger.info("Disable NS: " + disableNS);
  676. }
  677. return new Promise(function(resolve, reject) {
  678. if (RTCBrowserType.isFirefox()) {
  679. var FFversion = RTCBrowserType.getFirefoxVersion();
  680. if (FFversion < 40) {
  681. logger.error(
  682. "Firefox version too old: " + FFversion +
  683. ". Required >= 40.");
  684. reject(new Error("Firefox version too old: " + FFversion +
  685. ". Required >= 40."));
  686. return;
  687. }
  688. this.peerconnection = mozRTCPeerConnection;
  689. this.getUserMedia = wrapGetUserMedia(navigator.mozGetUserMedia.bind(navigator));
  690. this.enumerateDevices = wrapEnumerateDevices(
  691. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
  692. );
  693. this.pc_constraints = {};
  694. this.attachMediaStream = wrapAttachMediaStream(function (element, stream) {
  695. // srcObject is being standardized and FF will eventually
  696. // support that unprefixed. FF also supports the
  697. // "element.src = URL.createObjectURL(...)" combo, but that
  698. // will be deprecated in favour of srcObject.
  699. //
  700. // https://groups.google.com/forum/#!topic/mozilla.dev.media/pKOiioXonJg
  701. // https://github.com/webrtc/samples/issues/302
  702. if (element) {
  703. defaultSetVideoSrc(element, stream);
  704. if (stream)
  705. element.play();
  706. }
  707. return element;
  708. });
  709. this.getStreamID = function (stream) {
  710. var id = stream.id;
  711. if (!id) {
  712. var tracks = stream.getVideoTracks();
  713. if (!tracks || tracks.length === 0) {
  714. tracks = stream.getAudioTracks();
  715. }
  716. id = tracks[0].id;
  717. }
  718. return SDPUtil.filter_special_chars(id);
  719. };
  720. this.getVideoSrc = defaultGetVideoSrc;
  721. RTCSessionDescription = mozRTCSessionDescription;
  722. RTCIceCandidate = mozRTCIceCandidate;
  723. } else if (RTCBrowserType.isChrome() ||
  724. RTCBrowserType.isOpera() ||
  725. RTCBrowserType.isNWJS() ||
  726. RTCBrowserType.isReactNative()) {
  727. this.peerconnection = webkitRTCPeerConnection;
  728. var getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  729. if (navigator.mediaDevices) {
  730. this.getUserMedia = wrapGetUserMedia(getUserMedia);
  731. this.enumerateDevices = wrapEnumerateDevices(
  732. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
  733. );
  734. } else {
  735. this.getUserMedia = getUserMedia;
  736. this.enumerateDevices = enumerateDevicesThroughMediaStreamTrack;
  737. }
  738. this.attachMediaStream = wrapAttachMediaStream(function (element, stream) {
  739. defaultSetVideoSrc(element, stream);
  740. return element;
  741. });
  742. this.getStreamID = function (stream) {
  743. // A. MediaStreams from FF endpoints have the characters '{'
  744. // and '}' that make jQuery choke.
  745. // B. The react-native-webrtc implementation that we use on
  746. // React Native at the time of this writing returns a number
  747. // for the id of MediaStream. Let's just say that a number
  748. // contains no special characters.
  749. var id = stream.id;
  750. // XXX The return statement is affected by automatic
  751. // semicolon insertion (ASI). No line terminator is allowed
  752. // between the return keyword and the expression.
  753. return (
  754. (typeof id === 'number')
  755. ? id
  756. : SDPUtil.filter_special_chars(id));
  757. };
  758. this.getVideoSrc = defaultGetVideoSrc;
  759. // DTLS should now be enabled by default but..
  760. this.pc_constraints = {'optional': [
  761. {'DtlsSrtpKeyAgreement': 'true'}
  762. ]};
  763. if (options.useIPv6) {
  764. // https://code.google.com/p/webrtc/issues/detail?id=2828
  765. this.pc_constraints.optional.push({googIPv6: true});
  766. }
  767. if (RTCBrowserType.isAndroid()) {
  768. this.pc_constraints = {}; // disable DTLS on Android
  769. }
  770. if (!webkitMediaStream.prototype.getVideoTracks) {
  771. webkitMediaStream.prototype.getVideoTracks = function () {
  772. return this.videoTracks;
  773. };
  774. }
  775. if (!webkitMediaStream.prototype.getAudioTracks) {
  776. webkitMediaStream.prototype.getAudioTracks = function () {
  777. return this.audioTracks;
  778. };
  779. }
  780. }
  781. // Detect IE/Safari
  782. else if (RTCBrowserType.isTemasysPluginUsed()) {
  783. //AdapterJS.WebRTCPlugin.setLogLevel(
  784. // AdapterJS.WebRTCPlugin.PLUGIN_LOG_LEVELS.VERBOSE);
  785. var self = this;
  786. AdapterJS.webRTCReady(function (isPlugin) {
  787. self.peerconnection = RTCPeerConnection;
  788. self.getUserMedia = window.getUserMedia;
  789. self.enumerateDevices = enumerateDevicesThroughMediaStreamTrack;
  790. self.attachMediaStream = wrapAttachMediaStream(function (element, stream) {
  791. if (stream) {
  792. if (stream.id === "dummyAudio"
  793. || stream.id === "dummyVideo") {
  794. return;
  795. }
  796. // The container must be visible in order to play or
  797. // attach the stream when Temasys plugin is in use
  798. var containerSel = $(element);
  799. if (RTCBrowserType.isTemasysPluginUsed()
  800. && !containerSel.is(':visible')) {
  801. containerSel.show();
  802. }
  803. var video = !!stream.getVideoTracks().length;
  804. if (video && !$(element).is(':visible')) {
  805. throw new Error(
  806. 'video element must be visible to attach'
  807. + ' video stream');
  808. }
  809. }
  810. return attachMediaStream(element, stream);
  811. });
  812. self.getStreamID = function (stream) {
  813. return SDPUtil.filter_special_chars(stream.label);
  814. };
  815. self.getVideoSrc = function (element) {
  816. // There's nothing standard about getVideoSrc in the
  817. // case of Temasys so there's no point to try to
  818. // generalize it through defaultGetVideoSrc.
  819. if (!element) {
  820. logger.warn(
  821. "Attempt to get video SRC of null element");
  822. return null;
  823. }
  824. var children = element.children;
  825. for (var i = 0; i !== children.length; ++i) {
  826. if (children[i].name === 'streamId') {
  827. return children[i].value;
  828. }
  829. }
  830. //logger.info(element.id + " SRC: " + src);
  831. return null;
  832. };
  833. onReady(options, self.getUserMediaWithConstraints);
  834. resolve();
  835. });
  836. } else {
  837. var errmsg = 'Browser does not appear to be WebRTC-capable';
  838. try {
  839. logger.error(errmsg);
  840. } catch (e) {
  841. }
  842. reject(new Error(errmsg));
  843. return;
  844. }
  845. // Call onReady() if Temasys plugin is not used
  846. if (!RTCBrowserType.isTemasysPluginUsed()) {
  847. onReady(options, this.getUserMediaWithConstraints);
  848. resolve();
  849. }
  850. }.bind(this));
  851. },
  852. /**
  853. * @param {string[]} um required user media types
  854. * @param {function} success_callback
  855. * @param {Function} failure_callback
  856. * @param {Object} [options] optional parameters
  857. * @param {string} options.resolution
  858. * @param {number} options.bandwidth
  859. * @param {number} options.fps
  860. * @param {string} options.desktopStream
  861. * @param {string} options.cameraDeviceId
  862. * @param {string} options.micDeviceId
  863. **/
  864. getUserMediaWithConstraints: function ( um, success_callback, failure_callback, options) {
  865. options = options || {};
  866. var resolution = options.resolution;
  867. var constraints = getConstraints(um, options);
  868. logger.info("Get media constraints", constraints);
  869. try {
  870. this.getUserMedia(constraints,
  871. function (stream) {
  872. logger.log('onUserMediaSuccess');
  873. setAvailableDevices(um, true);
  874. success_callback(stream);
  875. },
  876. function (error) {
  877. setAvailableDevices(um, false);
  878. logger.warn('Failed to get access to local media. Error ',
  879. error, constraints);
  880. if (failure_callback) {
  881. failure_callback(
  882. new JitsiTrackError(error, constraints, um));
  883. }
  884. });
  885. } catch (e) {
  886. logger.error('GUM failed: ', e);
  887. if (failure_callback) {
  888. failure_callback(new JitsiTrackError(e, constraints, um));
  889. }
  890. }
  891. },
  892. /**
  893. * Creates the local MediaStreams.
  894. * @param {Object} [options] optional parameters
  895. * @param {Array} options.devices the devices that will be requested
  896. * @param {string} options.resolution resolution constraints
  897. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  898. * type: "audio" or "video", videoType: "camera" or "desktop"}
  899. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  900. * @param {string} options.cameraDeviceId
  901. * @param {string} options.micDeviceId
  902. * @returns {*} Promise object that will receive the new JitsiTracks
  903. */
  904. obtainAudioAndVideoPermissions: function (options) {
  905. var self = this;
  906. options = options || {};
  907. return new Promise(function (resolve, reject) {
  908. var successCallback = function (stream) {
  909. resolve(handleLocalStream(stream, options.resolution));
  910. };
  911. options.devices = options.devices || ['audio', 'video'];
  912. if(!screenObtainer.isSupported()
  913. && options.devices.indexOf("desktop") !== -1){
  914. reject(new Error("Desktop sharing is not supported!"));
  915. }
  916. if (RTCBrowserType.isFirefox()
  917. // XXX The react-native-webrtc implementation that we
  918. // utilize on React Native at the time of this writing does
  919. // not support the MediaStream constructors defined by
  920. // https://www.w3.org/TR/mediacapture-streams/#constructors
  921. // and instead has a single constructor which expects (an
  922. // NSNumber as) a MediaStream ID.
  923. || RTCBrowserType.isReactNative()
  924. || RTCBrowserType.isTemasysPluginUsed()) {
  925. var GUM = function (device, s, e) {
  926. this.getUserMediaWithConstraints(device, s, e, options);
  927. };
  928. var deviceGUM = {
  929. "audio": GUM.bind(self, ["audio"]),
  930. "video": GUM.bind(self, ["video"])
  931. };
  932. if(screenObtainer.isSupported()){
  933. deviceGUM["desktop"] = screenObtainer.obtainStream.bind(
  934. screenObtainer);
  935. }
  936. // With FF/IE we can't split the stream into audio and video because FF
  937. // doesn't support media stream constructors. So, we need to get the
  938. // audio stream separately from the video stream using two distinct GUM
  939. // calls. Not very user friendly :-( but we don't have many other
  940. // options neither.
  941. //
  942. // Note that we pack those 2 streams in a single object and pass it to
  943. // the successCallback method.
  944. obtainDevices({
  945. devices: options.devices,
  946. streams: [],
  947. successCallback: successCallback,
  948. errorCallback: reject,
  949. deviceGUM: deviceGUM
  950. });
  951. } else {
  952. var hasDesktop = options.devices.indexOf('desktop') > -1;
  953. if (hasDesktop) {
  954. options.devices.splice(options.devices.indexOf("desktop"), 1);
  955. }
  956. options.resolution = options.resolution || '360';
  957. if(options.devices.length) {
  958. this.getUserMediaWithConstraints(
  959. options.devices,
  960. function (stream) {
  961. var audioDeviceRequested = options.devices.indexOf("audio") !== -1;
  962. var videoDeviceRequested = options.devices.indexOf("video") !== -1;
  963. var audioTracksReceived = !!stream.getAudioTracks().length;
  964. var videoTracksReceived = !!stream.getVideoTracks().length;
  965. if((audioDeviceRequested && !audioTracksReceived) ||
  966. (videoDeviceRequested && !videoTracksReceived))
  967. {
  968. self.stopMediaStream(stream);
  969. // We are getting here in case if we requested
  970. // 'audio' or 'video' devices or both, but
  971. // didn't get corresponding MediaStreamTrack in
  972. // response stream. We don't know the reason why
  973. // this happened, so reject with general error.
  974. var devices = [];
  975. if (audioDeviceRequested && !audioTracksReceived) {
  976. devices.push("audio");
  977. }
  978. if (videoDeviceRequested && !videoTracksReceived) {
  979. devices.push("video");
  980. }
  981. reject(new JitsiTrackError(
  982. { name: "UnknownError" },
  983. getConstraints(options.devices, options),
  984. devices)
  985. );
  986. return;
  987. }
  988. if(hasDesktop) {
  989. screenObtainer.obtainStream(
  990. function (desktopStream) {
  991. successCallback({audioVideo: stream,
  992. desktopStream: desktopStream});
  993. }, function (error) {
  994. self.stopMediaStream(stream);
  995. reject(error);
  996. });
  997. } else {
  998. successCallback({audioVideo: stream});
  999. }
  1000. },
  1001. function (error) {
  1002. reject(error);
  1003. },
  1004. options);
  1005. } else if (hasDesktop) {
  1006. screenObtainer.obtainStream(
  1007. function (stream) {
  1008. successCallback({desktopStream: stream});
  1009. }, function (error) {
  1010. reject(error);
  1011. });
  1012. }
  1013. }
  1014. }.bind(this));
  1015. },
  1016. addListener: function (eventType, listener) {
  1017. eventEmitter.on(eventType, listener);
  1018. },
  1019. removeListener: function (eventType, listener) {
  1020. eventEmitter.removeListener(eventType, listener);
  1021. },
  1022. getDeviceAvailability: function () {
  1023. return devices;
  1024. },
  1025. isRTCReady: function () {
  1026. return rtcReady;
  1027. },
  1028. /**
  1029. * Checks if its possible to enumerate available cameras/micropones.
  1030. * @returns {boolean} true if available, false otherwise.
  1031. */
  1032. isDeviceListAvailable: function () {
  1033. var isEnumerateDevicesAvailable
  1034. = navigator.mediaDevices && navigator.mediaDevices.enumerateDevices;
  1035. if (isEnumerateDevicesAvailable) {
  1036. return true;
  1037. }
  1038. return (MediaStreamTrack && MediaStreamTrack.getSources)? true : false;
  1039. },
  1040. /**
  1041. * Returns true if changing the input (camera / microphone) or output
  1042. * (audio) device is supported and false if not.
  1043. * @params {string} [deviceType] - type of device to change. Default is
  1044. * undefined or 'input', 'output' - for audio output device change.
  1045. * @returns {boolean} true if available, false otherwise.
  1046. */
  1047. isDeviceChangeAvailable: function (deviceType) {
  1048. return deviceType === 'output' || deviceType === 'audiooutput'
  1049. ? isAudioOutputDeviceChangeAvailable
  1050. : RTCBrowserType.isChrome() ||
  1051. RTCBrowserType.isFirefox() ||
  1052. RTCBrowserType.isOpera() ||
  1053. RTCBrowserType.isTemasysPluginUsed()||
  1054. RTCBrowserType.isNWJS();
  1055. },
  1056. /**
  1057. * A method to handle stopping of the stream.
  1058. * One point to handle the differences in various implementations.
  1059. * @param mediaStream MediaStream object to stop.
  1060. */
  1061. stopMediaStream: function (mediaStream) {
  1062. mediaStream.getTracks().forEach(function (track) {
  1063. // stop() not supported with IE
  1064. if (!RTCBrowserType.isTemasysPluginUsed() && track.stop) {
  1065. track.stop();
  1066. }
  1067. });
  1068. // leave stop for implementation still using it
  1069. if (mediaStream.stop) {
  1070. mediaStream.stop();
  1071. }
  1072. // if we have done createObjectURL, lets clean it
  1073. var url = mediaStream.jitsiObjectURL;
  1074. if (url) {
  1075. delete mediaStream.jitsiObjectURL;
  1076. (URL || webkitURL).revokeObjectURL(url);
  1077. }
  1078. },
  1079. /**
  1080. * Returns whether the desktop sharing is enabled or not.
  1081. * @returns {boolean}
  1082. */
  1083. isDesktopSharingEnabled: function () {
  1084. return screenObtainer.isSupported();
  1085. },
  1086. /**
  1087. * Sets current audio output device.
  1088. * @param {string} deviceId - id of 'audiooutput' device from
  1089. * navigator.mediaDevices.enumerateDevices(), 'default' for default
  1090. * device
  1091. * @returns {Promise} - resolves when audio output is changed, is rejected
  1092. * otherwise
  1093. */
  1094. setAudioOutputDevice: function (deviceId) {
  1095. if (!this.isDeviceChangeAvailable('output')) {
  1096. Promise.reject(
  1097. new Error('Audio output device change is not supported'));
  1098. }
  1099. return featureDetectionAudioEl.setSinkId(deviceId)
  1100. .then(function() {
  1101. audioOutputDeviceId = deviceId;
  1102. logger.log('Audio output device set to ' + deviceId);
  1103. eventEmitter.emit(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  1104. deviceId);
  1105. });
  1106. },
  1107. /**
  1108. * Returns currently used audio output device id, '' stands for default
  1109. * device
  1110. * @returns {string}
  1111. */
  1112. getAudioOutputDevice: function () {
  1113. return audioOutputDeviceId;
  1114. }
  1115. };
  1116. module.exports = RTCUtils;