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

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