media-polyfill.js 1.2 KB

12345678910111213141516171819202122232425262728
  1. // Older browsers might not implement mediaDevices at all, so we set an empty object first
  2. if (navigator.mediaDevices === undefined) {
  3. navigator.mediaDevices = {};
  4. }
  5. // Some browsers partially implement mediaDevices. We can't just assign an object
  6. // with getUserMedia as it would overwrite existing properties.
  7. // Here, we will just add the getUserMedia property if it's missing.
  8. if (navigator.mediaDevices.getUserMedia === undefined) {
  9. navigator.mediaDevices.getUserMedia = function(constraints) {
  10. // First get ahold of the legacy getUserMedia, if present
  11. var getUserMedia =
  12. navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  13. // Some browsers just don't implement it - return a rejected promise with an error
  14. // to keep a consistent interface
  15. if (!getUserMedia) {
  16. return Promise.reject(
  17. new Error("getUserMedia is not implemented in this browser")
  18. );
  19. }
  20. // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
  21. return new Promise(function(resolve, reject) {
  22. getUserMedia.call(navigator, constraints, resolve, reject);
  23. });
  24. };
  25. }