getusermedia.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2016 The WebRTC 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.shimGetUserMedia = shimGetUserMedia;
  14. var utils = _interopRequireWildcard(require("../utils"));
  15. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  16. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  17. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  18. function shimGetUserMedia(window, browserDetails) {
  19. var navigator = window && window.navigator;
  20. var MediaStreamTrack = window && window.MediaStreamTrack;
  21. navigator.getUserMedia = function (constraints, onSuccess, onError) {
  22. // Replace Firefox 44+'s deprecation warning with unprefixed version.
  23. utils.deprecated('navigator.getUserMedia', 'navigator.mediaDevices.getUserMedia');
  24. navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError);
  25. };
  26. if (!(browserDetails.version > 55 && 'autoGainControl' in navigator.mediaDevices.getSupportedConstraints())) {
  27. var remap = function remap(obj, a, b) {
  28. if (a in obj && !(b in obj)) {
  29. obj[b] = obj[a];
  30. delete obj[a];
  31. }
  32. };
  33. var nativeGetUserMedia = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);
  34. navigator.mediaDevices.getUserMedia = function (c) {
  35. if (_typeof(c) === 'object' && _typeof(c.audio) === 'object') {
  36. c = JSON.parse(JSON.stringify(c));
  37. remap(c.audio, 'autoGainControl', 'mozAutoGainControl');
  38. remap(c.audio, 'noiseSuppression', 'mozNoiseSuppression');
  39. }
  40. return nativeGetUserMedia(c);
  41. };
  42. if (MediaStreamTrack && MediaStreamTrack.prototype.getSettings) {
  43. var nativeGetSettings = MediaStreamTrack.prototype.getSettings;
  44. MediaStreamTrack.prototype.getSettings = function () {
  45. var obj = nativeGetSettings.apply(this, arguments);
  46. remap(obj, 'mozAutoGainControl', 'autoGainControl');
  47. remap(obj, 'mozNoiseSuppression', 'noiseSuppression');
  48. return obj;
  49. };
  50. }
  51. if (MediaStreamTrack && MediaStreamTrack.prototype.applyConstraints) {
  52. var nativeApplyConstraints = MediaStreamTrack.prototype.applyConstraints;
  53. MediaStreamTrack.prototype.applyConstraints = function (c) {
  54. if (this.kind === 'audio' && _typeof(c) === 'object') {
  55. c = JSON.parse(JSON.stringify(c));
  56. remap(c, 'autoGainControl', 'mozAutoGainControl');
  57. remap(c, 'noiseSuppression', 'mozNoiseSuppression');
  58. }
  59. return nativeApplyConstraints.apply(this, [c]);
  60. };
  61. }
  62. }
  63. }