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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. if (compareAvailableMediaDevices(devices)) {
  300. onMediaDevicesListChanged(devices);
  301. }
  302. window.setTimeout(pollForAvailableMediaDevices,
  303. AVAILABLE_DEVICES_POLL_INTERVAL_TIME);
  304. });
  305. }
  306. }
  307. /**
  308. * Event handler for the 'devicechange' event.
  309. * @param {MediaDeviceInfo[]} devices - list of media devices.
  310. * @emits RTCEvents.DEVICE_LIST_CHANGED
  311. */
  312. function onMediaDevicesListChanged(devices) {
  313. currentlyAvailableMediaDevices = devices.slice(0);
  314. logger.info('list of media devices has changed:', currentlyAvailableMediaDevices);
  315. var videoInputDevices = currentlyAvailableMediaDevices.filter(function (d) {
  316. return d.kind === 'videoinput';
  317. }),
  318. audioInputDevices = currentlyAvailableMediaDevices.filter(function (d) {
  319. return d.kind === 'audioinput';
  320. }),
  321. videoInputDevicesWithEmptyLabels = videoInputDevices.filter(
  322. function (d) {
  323. return d.label === '';
  324. }),
  325. audioInputDevicesWithEmptyLabels = audioInputDevices.filter(
  326. function (d) {
  327. return d.label === '';
  328. });
  329. if (videoInputDevices.length &&
  330. videoInputDevices.length === videoInputDevicesWithEmptyLabels.length) {
  331. setAvailableDevices(['video'], false);
  332. }
  333. if (audioInputDevices.length &&
  334. audioInputDevices.length === audioInputDevicesWithEmptyLabels.length) {
  335. setAvailableDevices(['audio'], false);
  336. }
  337. eventEmitter.emit(RTCEvents.DEVICE_LIST_CHANGED, devices);
  338. }
  339. // In case of IE we continue from 'onReady' callback
  340. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  341. // once it is initialized.
  342. function onReady (options, GUM) {
  343. rtcReady = true;
  344. eventEmitter.emit(RTCEvents.RTC_READY, true);
  345. screenObtainer.init(options, GUM);
  346. if (isDeviceChangeEventSupported && RTCUtils.isDeviceListAvailable()) {
  347. navigator.mediaDevices.addEventListener('devicechange', function () {
  348. RTCUtils.enumerateDevices(onMediaDevicesListChanged);
  349. });
  350. } else if (RTCUtils.isDeviceListAvailable()) {
  351. pollForAvailableMediaDevices();
  352. }
  353. }
  354. /**
  355. * Apply function with arguments if function exists.
  356. * Do nothing if function not provided.
  357. * @param {function} [fn] function to apply
  358. * @param {Array} [args=[]] arguments for function
  359. */
  360. function maybeApply(fn, args) {
  361. if (fn) {
  362. fn.apply(null, args || []);
  363. }
  364. }
  365. var getUserMediaStatus = {
  366. initialized: false,
  367. callbacks: []
  368. };
  369. /**
  370. * Wrap `getUserMedia` to allow others to know if it was executed at least
  371. * once or not. Wrapper function uses `getUserMediaStatus` object.
  372. * @param {Function} getUserMedia native function
  373. * @returns {Function} wrapped function
  374. */
  375. function wrapGetUserMedia(getUserMedia) {
  376. return function (constraints, successCallback, errorCallback) {
  377. getUserMedia(constraints, function (stream) {
  378. maybeApply(successCallback, [stream]);
  379. if (!getUserMediaStatus.initialized) {
  380. getUserMediaStatus.initialized = true;
  381. getUserMediaStatus.callbacks.forEach(function (callback) {
  382. callback();
  383. });
  384. getUserMediaStatus.callbacks.length = 0;
  385. }
  386. }, function (error) {
  387. maybeApply(errorCallback, [error]);
  388. });
  389. };
  390. }
  391. /**
  392. * Execute function after getUserMedia was executed at least once.
  393. * @param {Function} callback function to execute after getUserMedia
  394. */
  395. function afterUserMediaInitialized(callback) {
  396. if (getUserMediaStatus.initialized) {
  397. callback();
  398. } else {
  399. getUserMediaStatus.callbacks.push(callback);
  400. }
  401. }
  402. /**
  403. * Wrapper function which makes enumerateDevices to wait
  404. * until someone executes getUserMedia first time.
  405. * @param {Function} enumerateDevices native function
  406. * @returns {Funtion} wrapped function
  407. */
  408. function wrapEnumerateDevices(enumerateDevices) {
  409. return function (callback) {
  410. // enumerate devices only after initial getUserMedia
  411. afterUserMediaInitialized(function () {
  412. enumerateDevices().then(callback, function (err) {
  413. logger.error('cannot enumerate devices: ', err);
  414. callback([]);
  415. });
  416. });
  417. };
  418. }
  419. /**
  420. * Use old MediaStreamTrack to get devices list and
  421. * convert it to enumerateDevices format.
  422. * @param {Function} callback function to call when received devices list.
  423. */
  424. function enumerateDevicesThroughMediaStreamTrack (callback) {
  425. MediaStreamTrack.getSources(function (sources) {
  426. callback(sources.map(convertMediaStreamTrackSource));
  427. });
  428. }
  429. /**
  430. * Converts MediaStreamTrack Source to enumerateDevices format.
  431. * @param {Object} source
  432. */
  433. function convertMediaStreamTrackSource(source) {
  434. var kind = (source.kind || '').toLowerCase();
  435. return {
  436. facing: source.facing || null,
  437. label: source.label,
  438. // theoretically deprecated MediaStreamTrack.getSources should
  439. // not return 'audiooutput' devices but let's handle it in any
  440. // case
  441. kind: kind
  442. ? (kind === 'audiooutput' ? kind : kind + 'input')
  443. : null,
  444. deviceId: source.id,
  445. groupId: source.groupId || null
  446. };
  447. }
  448. function obtainDevices(options) {
  449. if(!options.devices || options.devices.length === 0) {
  450. return options.successCallback(options.streams || {});
  451. }
  452. var device = options.devices.splice(0, 1);
  453. var devices = [];
  454. devices.push(device);
  455. options.deviceGUM[device](function (stream) {
  456. options.streams = options.streams || {};
  457. options.streams[device] = stream;
  458. obtainDevices(options);
  459. },
  460. function (error) {
  461. Object.keys(options.streams).forEach(function(device) {
  462. RTCUtils.stopMediaStream(options.streams[device]);
  463. });
  464. logger.error(
  465. "failed to obtain " + device + " stream - stop", error);
  466. options.errorCallback(error);
  467. });
  468. }
  469. /**
  470. * Handles the newly created Media Streams.
  471. * @param streams the new Media Streams
  472. * @param resolution the resolution of the video streams
  473. * @returns {*[]} object that describes the new streams
  474. */
  475. function handleLocalStream(streams, resolution) {
  476. var audioStream, videoStream, desktopStream, res = [];
  477. // XXX The function obtainAudioAndVideoPermissions has examined the type of
  478. // the browser, its capabilities, etc. and has taken the decision whether to
  479. // invoke getUserMedia per device (e.g. Firefox) or once for both audio and
  480. // video (e.g. Chrome). In order to not duplicate the logic here, examine
  481. // the specified streams and figure out what we've received based on
  482. // obtainAudioAndVideoPermissions' decision.
  483. if (streams) {
  484. // As mentioned above, certian types of browser (e.g. Chrome) support
  485. // (with a result which meets our requirements expressed bellow) calling
  486. // getUserMedia once for both audio and video.
  487. var audioVideo = streams.audioVideo;
  488. if (audioVideo) {
  489. var audioTracks = audioVideo.getAudioTracks();
  490. if (audioTracks.length) {
  491. audioStream = new webkitMediaStream();
  492. for (var i = 0; i < audioTracks.length; i++) {
  493. audioStream.addTrack(audioTracks[i]);
  494. }
  495. }
  496. var videoTracks = audioVideo.getVideoTracks();
  497. if (videoTracks.length) {
  498. videoStream = new webkitMediaStream();
  499. for (var j = 0; j < videoTracks.length; j++) {
  500. videoStream.addTrack(videoTracks[j]);
  501. }
  502. }
  503. } else {
  504. // On other types of browser (e.g. Firefox) we choose (namely,
  505. // obtainAudioAndVideoPermissions) to call getUsermedia per device
  506. // (type).
  507. audioStream = streams.audio;
  508. videoStream = streams.video;
  509. }
  510. // Again, different choices on different types of browser.
  511. desktopStream = streams.desktopStream || streams.desktop;
  512. }
  513. if (desktopStream) {
  514. res.push({
  515. stream: desktopStream,
  516. track: desktopStream.getVideoTracks()[0],
  517. mediaType: MediaType.VIDEO,
  518. videoType: VideoType.DESKTOP
  519. });
  520. }
  521. if (audioStream) {
  522. res.push({
  523. stream: audioStream,
  524. track: audioStream.getAudioTracks()[0],
  525. mediaType: MediaType.AUDIO,
  526. videoType: null
  527. });
  528. }
  529. if (videoStream) {
  530. res.push({
  531. stream: videoStream,
  532. track: videoStream.getVideoTracks()[0],
  533. mediaType: MediaType.VIDEO,
  534. videoType: VideoType.CAMERA,
  535. resolution: resolution
  536. });
  537. }
  538. return res;
  539. }
  540. /**
  541. * Wraps original attachMediaStream function to set current audio output device
  542. * if this is supported.
  543. * @param {Function} origAttachMediaStream
  544. * @returns {Function}
  545. */
  546. function wrapAttachMediaStream(origAttachMediaStream) {
  547. return function(element, stream) {
  548. var res = origAttachMediaStream.apply(RTCUtils, arguments);
  549. if (stream
  550. && RTCUtils.isDeviceChangeAvailable('output')
  551. && stream.getAudioTracks
  552. && stream.getAudioTracks().length) {
  553. element.setSinkId(RTCUtils.getAudioOutputDevice())
  554. .catch(function (ex) {
  555. var err = new JitsiTrackError(ex, null, ['audiooutput']);
  556. GlobalOnErrorHandler.callUnhandledRejectionHandler(
  557. {promise: this, reason: err});
  558. logger.warn('Failed to set audio output device for the ' +
  559. 'element. Default audio output device will be used ' +
  560. 'instead',
  561. element, err);
  562. });
  563. }
  564. return res;
  565. }
  566. }
  567. /**
  568. * Represents a default implementation of {@link RTCUtils#getVideoSrc} which
  569. * tries to be browser-agnostic through feature checking. Note though that it
  570. * was not completely clear from the predating browser-specific implementations
  571. * what &quot;videoSrc&quot; was because one implementation would return
  572. * <tt>MediaStream</tt> (e.g. Firefox), another a <tt>string</tt> representation
  573. * of the <tt>URL</tt> of the <tt>MediaStream</tt> (e.g. Chrome) and the return
  574. * value was only used by {@link RTCUIHelper#getVideoId} which itself did not
  575. * appear to be used anywhere. Generally, the implementation will try to follow
  576. * the related standards i.e. work with the <tt>srcObject</tt> and <tt>src</tt>
  577. * properties of the specified <tt>element</tt> taking into account vender
  578. * prefixes.
  579. *
  580. * @param element the element to get the associated video source/src of
  581. * @return the video source/src of the specified <tt>element</tt>
  582. */
  583. function defaultGetVideoSrc(element) {
  584. // https://www.w3.org/TR/mediacapture-streams/
  585. //
  586. // User Agents that support this specification must support the srcObject
  587. // attribute of the HTMLMediaElement interface defined in [HTML51].
  588. // https://www.w3.org/TR/2015/WD-html51-20150506/semantics.html#dom-media-srcobject
  589. //
  590. // There are three ways to specify a media resource: the srcObject IDL
  591. // attribute, the src content attribute, and source elements. The IDL
  592. // attribute takes priority, followed by the content attribute, followed by
  593. // the elements.
  594. // srcObject
  595. var srcObject = element.srcObject || element.mozSrcObject;
  596. if (srcObject) {
  597. // Try the optimized path to the URL of a MediaStream.
  598. var url = srcObject.jitsiObjectURL;
  599. if (url) {
  600. return url.toString();
  601. }
  602. // Go via the unoptimized path to the URL of a MediaStream then.
  603. var URL = (window.URL || webkitURL);
  604. if (URL) {
  605. url = URL.createObjectURL(srcObject);
  606. try {
  607. return url.toString();
  608. } finally {
  609. URL.revokeObjectURL(url);
  610. }
  611. }
  612. }
  613. // src
  614. return element.src;
  615. }
  616. /**
  617. * Represents a default implementation of setting a <tt>MediaStream</tt> as the
  618. * source of a video element that tries to be browser-agnostic through feature
  619. * checking. Note though that it was not completely clear from the predating
  620. * browser-specific implementations what &quot;videoSrc&quot; was because one
  621. * implementation of {@link RTCUtils#getVideoSrc} would return
  622. * <tt>MediaStream</tt> (e.g. Firefox), another a <tt>string</tt> representation
  623. * of the <tt>URL</tt> of the <tt>MediaStream</tt> (e.g. Chrome) and the return
  624. * value was only used by {@link RTCUIHelper#getVideoId} which itself did not
  625. * appear to be used anywhere. Generally, the implementation will try to follow
  626. * the related standards i.e. work with the <tt>srcObject</tt> and <tt>src</tt>
  627. * properties of the specified <tt>element</tt> taking into account vender
  628. * prefixes.
  629. *
  630. * @param element the element whose video source/src is to be set to the
  631. * specified <tt>stream</tt>
  632. * @param {MediaStream} stream the <tt>MediaStream</tt> to set as the video
  633. * source/src of <tt>element</tt>
  634. */
  635. function defaultSetVideoSrc(element, stream) {
  636. // srcObject
  637. var srcObjectPropertyName = 'srcObject';
  638. if (!(srcObjectPropertyName in element)) {
  639. srcObjectPropertyName = 'mozSrcObject';
  640. if (!(srcObjectPropertyName in element)) {
  641. srcObjectPropertyName = null;
  642. }
  643. }
  644. if (srcObjectPropertyName) {
  645. element[srcObjectPropertyName] = stream;
  646. return;
  647. }
  648. // src
  649. var src;
  650. if (stream) {
  651. src = stream.jitsiObjectURL;
  652. // Save the created URL for stream so we can reuse it and not keep
  653. // creating URLs.
  654. if (!src) {
  655. stream.jitsiObjectURL
  656. = src
  657. = (URL || webkitURL).createObjectURL(stream);
  658. }
  659. }
  660. element.src = src || '';
  661. }
  662. //Options parameter is to pass config options. Currently uses only "useIPv6".
  663. var RTCUtils = {
  664. init: function (options) {
  665. if (typeof(options.disableAEC) === "boolean") {
  666. disableAEC = options.disableAEC;
  667. logger.info("Disable AEC: " + disableAEC);
  668. }
  669. if (typeof(options.disableNS) === "boolean") {
  670. disableNS = options.disableNS;
  671. logger.info("Disable NS: " + disableNS);
  672. }
  673. return new Promise(function(resolve, reject) {
  674. if (RTCBrowserType.isFirefox()) {
  675. var FFversion = RTCBrowserType.getFirefoxVersion();
  676. if (FFversion < 40) {
  677. logger.error(
  678. "Firefox version too old: " + FFversion +
  679. ". Required >= 40.");
  680. reject(new Error("Firefox version too old: " + FFversion +
  681. ". Required >= 40."));
  682. return;
  683. }
  684. this.peerconnection = mozRTCPeerConnection;
  685. this.getUserMedia = wrapGetUserMedia(navigator.mozGetUserMedia.bind(navigator));
  686. this.enumerateDevices = wrapEnumerateDevices(
  687. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
  688. );
  689. this.pc_constraints = {};
  690. this.attachMediaStream = wrapAttachMediaStream(function (element, stream) {
  691. // srcObject is being standardized and FF will eventually
  692. // support that unprefixed. FF also supports the
  693. // "element.src = URL.createObjectURL(...)" combo, but that
  694. // will be deprecated in favour of srcObject.
  695. //
  696. // https://groups.google.com/forum/#!topic/mozilla.dev.media/pKOiioXonJg
  697. // https://github.com/webrtc/samples/issues/302
  698. if (element) {
  699. defaultSetVideoSrc(element, stream);
  700. if (stream)
  701. element.play();
  702. }
  703. return element;
  704. });
  705. this.getStreamID = function (stream) {
  706. var id = stream.id;
  707. if (!id) {
  708. var tracks = stream.getVideoTracks();
  709. if (!tracks || tracks.length === 0) {
  710. tracks = stream.getAudioTracks();
  711. }
  712. id = tracks[0].id;
  713. }
  714. return SDPUtil.filter_special_chars(id);
  715. };
  716. this.getVideoSrc = defaultGetVideoSrc;
  717. RTCSessionDescription = mozRTCSessionDescription;
  718. RTCIceCandidate = mozRTCIceCandidate;
  719. } else if (RTCBrowserType.isChrome() ||
  720. RTCBrowserType.isOpera() ||
  721. RTCBrowserType.isNWJS() ||
  722. RTCBrowserType.isReactNative()) {
  723. this.peerconnection = webkitRTCPeerConnection;
  724. var getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  725. if (navigator.mediaDevices) {
  726. this.getUserMedia = wrapGetUserMedia(getUserMedia);
  727. this.enumerateDevices = wrapEnumerateDevices(
  728. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
  729. );
  730. } else {
  731. this.getUserMedia = getUserMedia;
  732. this.enumerateDevices = enumerateDevicesThroughMediaStreamTrack;
  733. }
  734. this.attachMediaStream = wrapAttachMediaStream(function (element, stream) {
  735. defaultSetVideoSrc(element, stream);
  736. return element;
  737. });
  738. this.getStreamID = function (stream) {
  739. // A. MediaStreams from FF endpoints have the characters '{'
  740. // and '}' that make jQuery choke.
  741. // B. The react-native-webrtc implementation that we use on
  742. // React Native at the time of this writing returns a number
  743. // for the id of MediaStream. Let's just say that a number
  744. // contains no special characters.
  745. var id = stream.id;
  746. // XXX The return statement is affected by automatic
  747. // semicolon insertion (ASI). No line terminator is allowed
  748. // between the return keyword and the expression.
  749. return (
  750. (typeof id === 'number')
  751. ? id
  752. : SDPUtil.filter_special_chars(id));
  753. };
  754. this.getVideoSrc = defaultGetVideoSrc;
  755. // DTLS should now be enabled by default but..
  756. this.pc_constraints = {'optional': [
  757. {'DtlsSrtpKeyAgreement': 'true'}
  758. ]};
  759. if (options.useIPv6) {
  760. // https://code.google.com/p/webrtc/issues/detail?id=2828
  761. this.pc_constraints.optional.push({googIPv6: true});
  762. }
  763. if (RTCBrowserType.isAndroid()) {
  764. this.pc_constraints = {}; // disable DTLS on Android
  765. }
  766. if (!webkitMediaStream.prototype.getVideoTracks) {
  767. webkitMediaStream.prototype.getVideoTracks = function () {
  768. return this.videoTracks;
  769. };
  770. }
  771. if (!webkitMediaStream.prototype.getAudioTracks) {
  772. webkitMediaStream.prototype.getAudioTracks = function () {
  773. return this.audioTracks;
  774. };
  775. }
  776. }
  777. // Detect IE/Safari
  778. else if (RTCBrowserType.isTemasysPluginUsed()) {
  779. //AdapterJS.WebRTCPlugin.setLogLevel(
  780. // AdapterJS.WebRTCPlugin.PLUGIN_LOG_LEVELS.VERBOSE);
  781. var self = this;
  782. AdapterJS.webRTCReady(function (isPlugin) {
  783. self.peerconnection = RTCPeerConnection;
  784. self.getUserMedia = window.getUserMedia;
  785. self.enumerateDevices = enumerateDevicesThroughMediaStreamTrack;
  786. self.attachMediaStream = wrapAttachMediaStream(function (element, stream) {
  787. if (stream) {
  788. if (stream.id === "dummyAudio"
  789. || stream.id === "dummyVideo") {
  790. return;
  791. }
  792. // The container must be visible in order to play or
  793. // attach the stream when Temasys plugin is in use
  794. var containerSel = $(element);
  795. if (RTCBrowserType.isTemasysPluginUsed()
  796. && !containerSel.is(':visible')) {
  797. containerSel.show();
  798. }
  799. var video = !!stream.getVideoTracks().length;
  800. if (video && !$(element).is(':visible')) {
  801. throw new Error(
  802. 'video element must be visible to attach'
  803. + ' video stream');
  804. }
  805. }
  806. return attachMediaStream(element, stream);
  807. });
  808. self.getStreamID = function (stream) {
  809. return SDPUtil.filter_special_chars(stream.label);
  810. };
  811. self.getVideoSrc = function (element) {
  812. // There's nothing standard about getVideoSrc in the
  813. // case of Temasys so there's no point to try to
  814. // generalize it through defaultGetVideoSrc.
  815. if (!element) {
  816. logger.warn(
  817. "Attempt to get video SRC of null element");
  818. return null;
  819. }
  820. var children = element.children;
  821. for (var i = 0; i !== children.length; ++i) {
  822. if (children[i].name === 'streamId') {
  823. return children[i].value;
  824. }
  825. }
  826. //logger.info(element.id + " SRC: " + src);
  827. return null;
  828. };
  829. onReady(options, self.getUserMediaWithConstraints);
  830. resolve();
  831. });
  832. } else {
  833. var errmsg = 'Browser does not appear to be WebRTC-capable';
  834. try {
  835. logger.error(errmsg);
  836. } catch (e) {
  837. }
  838. reject(new Error(errmsg));
  839. return;
  840. }
  841. // Call onReady() if Temasys plugin is not used
  842. if (!RTCBrowserType.isTemasysPluginUsed()) {
  843. onReady(options, this.getUserMediaWithConstraints);
  844. resolve();
  845. }
  846. }.bind(this));
  847. },
  848. /**
  849. * @param {string[]} um required user media types
  850. * @param {function} success_callback
  851. * @param {Function} failure_callback
  852. * @param {Object} [options] optional parameters
  853. * @param {string} options.resolution
  854. * @param {number} options.bandwidth
  855. * @param {number} options.fps
  856. * @param {string} options.desktopStream
  857. * @param {string} options.cameraDeviceId
  858. * @param {string} options.micDeviceId
  859. **/
  860. getUserMediaWithConstraints: function ( um, success_callback, failure_callback, options) {
  861. options = options || {};
  862. var resolution = options.resolution;
  863. var constraints = getConstraints(um, options);
  864. logger.info("Get media constraints", constraints);
  865. try {
  866. this.getUserMedia(constraints,
  867. function (stream) {
  868. logger.log('onUserMediaSuccess');
  869. setAvailableDevices(um, true);
  870. success_callback(stream);
  871. },
  872. function (error) {
  873. setAvailableDevices(um, false);
  874. logger.warn('Failed to get access to local media. Error ',
  875. error, constraints);
  876. if (failure_callback) {
  877. failure_callback(
  878. new JitsiTrackError(error, constraints, um));
  879. }
  880. });
  881. } catch (e) {
  882. logger.error('GUM failed: ', e);
  883. if (failure_callback) {
  884. failure_callback(new JitsiTrackError(e, constraints, um));
  885. }
  886. }
  887. },
  888. /**
  889. * Creates the local MediaStreams.
  890. * @param {Object} [options] optional parameters
  891. * @param {Array} options.devices the devices that will be requested
  892. * @param {string} options.resolution resolution constraints
  893. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  894. * type: "audio" or "video", videoType: "camera" or "desktop"}
  895. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  896. * @param {string} options.cameraDeviceId
  897. * @param {string} options.micDeviceId
  898. * @returns {*} Promise object that will receive the new JitsiTracks
  899. */
  900. obtainAudioAndVideoPermissions: function (options) {
  901. var self = this;
  902. options = options || {};
  903. return new Promise(function (resolve, reject) {
  904. var successCallback = function (stream) {
  905. resolve(handleLocalStream(stream, options.resolution));
  906. };
  907. options.devices = options.devices || ['audio', 'video'];
  908. if(!screenObtainer.isSupported()
  909. && options.devices.indexOf("desktop") !== -1){
  910. reject(new Error("Desktop sharing is not supported!"));
  911. }
  912. if (RTCBrowserType.isFirefox()
  913. // XXX The react-native-webrtc implementation that we
  914. // utilize on React Native at the time of this writing does
  915. // not support the MediaStream constructors defined by
  916. // https://www.w3.org/TR/mediacapture-streams/#constructors
  917. // and instead has a single constructor which expects (an
  918. // NSNumber as) a MediaStream ID.
  919. || RTCBrowserType.isReactNative()
  920. || RTCBrowserType.isTemasysPluginUsed()) {
  921. var GUM = function (device, s, e) {
  922. this.getUserMediaWithConstraints(device, s, e, options);
  923. };
  924. var deviceGUM = {
  925. "audio": GUM.bind(self, ["audio"]),
  926. "video": GUM.bind(self, ["video"])
  927. };
  928. if(screenObtainer.isSupported()){
  929. deviceGUM["desktop"] = screenObtainer.obtainStream.bind(
  930. screenObtainer);
  931. }
  932. // With FF/IE we can't split the stream into audio and video because FF
  933. // doesn't support media stream constructors. So, we need to get the
  934. // audio stream separately from the video stream using two distinct GUM
  935. // calls. Not very user friendly :-( but we don't have many other
  936. // options neither.
  937. //
  938. // Note that we pack those 2 streams in a single object and pass it to
  939. // the successCallback method.
  940. obtainDevices({
  941. devices: options.devices,
  942. streams: [],
  943. successCallback: successCallback,
  944. errorCallback: reject,
  945. deviceGUM: deviceGUM
  946. });
  947. } else {
  948. var hasDesktop = options.devices.indexOf('desktop') > -1;
  949. if (hasDesktop) {
  950. options.devices.splice(options.devices.indexOf("desktop"), 1);
  951. }
  952. options.resolution = options.resolution || '360';
  953. if(options.devices.length) {
  954. this.getUserMediaWithConstraints(
  955. options.devices,
  956. function (stream) {
  957. var audioDeviceRequested = options.devices.indexOf("audio") !== -1;
  958. var videoDeviceRequested = options.devices.indexOf("video") !== -1;
  959. var audioTracksReceived = !!stream.getAudioTracks().length;
  960. var videoTracksReceived = !!stream.getVideoTracks().length;
  961. if((audioDeviceRequested && !audioTracksReceived) ||
  962. (videoDeviceRequested && !videoTracksReceived))
  963. {
  964. self.stopMediaStream(stream);
  965. // We are getting here in case if we requested
  966. // 'audio' or 'video' devices or both, but
  967. // didn't get corresponding MediaStreamTrack in
  968. // response stream. We don't know the reason why
  969. // this happened, so reject with general error.
  970. var devices = [];
  971. if (audioDeviceRequested && !audioTracksReceived) {
  972. devices.push("audio");
  973. }
  974. if (videoDeviceRequested && !videoTracksReceived) {
  975. devices.push("video");
  976. }
  977. reject(new JitsiTrackError(
  978. { name: "UnknownError" },
  979. getConstraints(options.devices, options),
  980. devices)
  981. );
  982. return;
  983. }
  984. if(hasDesktop) {
  985. screenObtainer.obtainStream(
  986. function (desktopStream) {
  987. successCallback({audioVideo: stream,
  988. desktopStream: desktopStream});
  989. }, function (error) {
  990. self.stopMediaStream(stream);
  991. reject(error);
  992. });
  993. } else {
  994. successCallback({audioVideo: stream});
  995. }
  996. },
  997. function (error) {
  998. reject(error);
  999. },
  1000. options);
  1001. } else if (hasDesktop) {
  1002. screenObtainer.obtainStream(
  1003. function (stream) {
  1004. successCallback({desktopStream: stream});
  1005. }, function (error) {
  1006. reject(error);
  1007. });
  1008. }
  1009. }
  1010. }.bind(this));
  1011. },
  1012. addListener: function (eventType, listener) {
  1013. eventEmitter.on(eventType, listener);
  1014. },
  1015. removeListener: function (eventType, listener) {
  1016. eventEmitter.removeListener(eventType, listener);
  1017. },
  1018. getDeviceAvailability: function () {
  1019. return devices;
  1020. },
  1021. isRTCReady: function () {
  1022. return rtcReady;
  1023. },
  1024. /**
  1025. * Checks if its possible to enumerate available cameras/micropones.
  1026. * @returns {boolean} true if available, false otherwise.
  1027. */
  1028. isDeviceListAvailable: function () {
  1029. var isEnumerateDevicesAvailable
  1030. = navigator.mediaDevices && navigator.mediaDevices.enumerateDevices;
  1031. if (isEnumerateDevicesAvailable) {
  1032. return true;
  1033. }
  1034. return (MediaStreamTrack && MediaStreamTrack.getSources)? true : false;
  1035. },
  1036. /**
  1037. * Returns true if changing the input (camera / microphone) or output
  1038. * (audio) device is supported and false if not.
  1039. * @params {string} [deviceType] - type of device to change. Default is
  1040. * undefined or 'input', 'output' - for audio output device change.
  1041. * @returns {boolean} true if available, false otherwise.
  1042. */
  1043. isDeviceChangeAvailable: function (deviceType) {
  1044. return deviceType === 'output' || deviceType === 'audiooutput'
  1045. ? isAudioOutputDeviceChangeAvailable
  1046. : RTCBrowserType.isChrome() ||
  1047. RTCBrowserType.isFirefox() ||
  1048. RTCBrowserType.isOpera() ||
  1049. RTCBrowserType.isTemasysPluginUsed()||
  1050. RTCBrowserType.isNWJS();
  1051. },
  1052. /**
  1053. * A method to handle stopping of the stream.
  1054. * One point to handle the differences in various implementations.
  1055. * @param mediaStream MediaStream object to stop.
  1056. */
  1057. stopMediaStream: function (mediaStream) {
  1058. mediaStream.getTracks().forEach(function (track) {
  1059. // stop() not supported with IE
  1060. if (!RTCBrowserType.isTemasysPluginUsed() && track.stop) {
  1061. track.stop();
  1062. }
  1063. });
  1064. // leave stop for implementation still using it
  1065. if (mediaStream.stop) {
  1066. mediaStream.stop();
  1067. }
  1068. // if we have done createObjectURL, lets clean it
  1069. var url = mediaStream.jitsiObjectURL;
  1070. if (url) {
  1071. delete mediaStream.jitsiObjectURL;
  1072. (URL || webkitURL).revokeObjectURL(url);
  1073. }
  1074. },
  1075. /**
  1076. * Returns whether the desktop sharing is enabled or not.
  1077. * @returns {boolean}
  1078. */
  1079. isDesktopSharingEnabled: function () {
  1080. return screenObtainer.isSupported();
  1081. },
  1082. /**
  1083. * Sets current audio output device.
  1084. * @param {string} deviceId - id of 'audiooutput' device from
  1085. * navigator.mediaDevices.enumerateDevices(), 'default' for default
  1086. * device
  1087. * @returns {Promise} - resolves when audio output is changed, is rejected
  1088. * otherwise
  1089. */
  1090. setAudioOutputDevice: function (deviceId) {
  1091. if (!this.isDeviceChangeAvailable('output')) {
  1092. Promise.reject(
  1093. new Error('Audio output device change is not supported'));
  1094. }
  1095. return featureDetectionAudioEl.setSinkId(deviceId)
  1096. .then(function() {
  1097. audioOutputDeviceId = deviceId;
  1098. logger.log('Audio output device set to ' + deviceId);
  1099. eventEmitter.emit(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  1100. deviceId);
  1101. });
  1102. },
  1103. /**
  1104. * Returns currently used audio output device id, '' stands for default
  1105. * device
  1106. * @returns {string}
  1107. */
  1108. getAudioOutputDevice: function () {
  1109. return audioOutputDeviceId;
  1110. }
  1111. };
  1112. module.exports = RTCUtils;