getdisplaymedia.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2018 The adapter.js project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree.
  7. */
  8. /* eslint-env node */
  9. 'use strict';
  10. Object.defineProperty(exports, "__esModule", {
  11. value: true
  12. });
  13. exports.shimGetDisplayMedia = shimGetDisplayMedia;
  14. function shimGetDisplayMedia(window, getSourceId) {
  15. if (window.navigator.mediaDevices && 'getDisplayMedia' in window.navigator.mediaDevices) {
  16. return;
  17. }
  18. if (!window.navigator.mediaDevices) {
  19. return;
  20. }
  21. // getSourceId is a function that returns a promise resolving with
  22. // the sourceId of the screen/window/tab to be shared.
  23. if (typeof getSourceId !== 'function') {
  24. console.error('shimGetDisplayMedia: getSourceId argument is not ' + 'a function');
  25. return;
  26. }
  27. window.navigator.mediaDevices.getDisplayMedia = function getDisplayMedia(constraints) {
  28. return getSourceId(constraints).then(function (sourceId) {
  29. var widthSpecified = constraints.video && constraints.video.width;
  30. var heightSpecified = constraints.video && constraints.video.height;
  31. var frameRateSpecified = constraints.video && constraints.video.frameRate;
  32. constraints.video = {
  33. mandatory: {
  34. chromeMediaSource: 'desktop',
  35. chromeMediaSourceId: sourceId,
  36. maxFrameRate: frameRateSpecified || 3
  37. }
  38. };
  39. if (widthSpecified) {
  40. constraints.video.mandatory.maxWidth = widthSpecified;
  41. }
  42. if (heightSpecified) {
  43. constraints.video.mandatory.maxHeight = heightSpecified;
  44. }
  45. return window.navigator.mediaDevices.getUserMedia(constraints);
  46. });
  47. };
  48. }