Advertisement
krot

rtc select device

Jun 18th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <button>Select 2nd Camera</button><hr>
  2. <video controls autoplay style="width:100%;"></video>
  3.  
  4. <script>
  5. var MediaDevices = [];
  6.  
  7. if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
  8.     // Firefox 38+ seems having support of enumerateDevices
  9.     // Thanks @xdumaine/enumerateDevices
  10.     navigator.enumerateDevices = function(callback) {
  11.         navigator.mediaDevices.enumerateDevices().then(callback);
  12.     };
  13. }
  14.  
  15. // ---------- Media Devices detection
  16. var canEnumerate = false;
  17.  
  18. /*global MediaStreamTrack:true */
  19. if(typeof MediaStreamTrack !== 'undefined' && 'getSources' in MediaStreamTrack) {
  20.     canEnumerate = true;
  21. }
  22.  
  23. else if(navigator.mediaDevices && !!navigator.mediaDevices.enumerateDevices) {
  24.     canEnumerate = true;
  25. }
  26.  
  27. var hasMicrophone = false;
  28. var hasSpeakers = false;
  29. var hasWebcam = false;
  30.  
  31. var isWebsiteHasMicrophonePermissions = false;
  32. var isWebsiteHasWebcamPermissions = false;
  33.  
  34. // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediadevices
  35. // todo: switch to enumerateDevices when landed in canary.
  36. function checkDeviceSupport(callback) {
  37.     if(!canEnumerate) {
  38.         return;
  39.     }
  40.  
  41.     // This method is useful only for Chrome!
  42.  
  43.     if (!navigator.enumerateDevices && window.MediaStreamTrack && window.MediaStreamTrack.getSources) {
  44.         navigator.enumerateDevices = window.MediaStreamTrack.getSources.bind(window.MediaStreamTrack);
  45.     }
  46.  
  47.     if (!navigator.enumerateDevices && navigator.enumerateDevices) {
  48.         navigator.enumerateDevices = navigator.enumerateDevices.bind(navigator);
  49.     }
  50.  
  51.     if (!navigator.enumerateDevices) {
  52.         if (callback) {
  53.             callback();
  54.         }
  55.         return;
  56.     }
  57.  
  58.     MediaDevices = [];
  59.     navigator.enumerateDevices(function(devices) {
  60.         devices.forEach(function(_device) {
  61.             var device = {};
  62.             for (var d in _device) {
  63.                 device[d] = _device[d];
  64.             }
  65.  
  66.             // if it is MediaStreamTrack.getSources
  67.             if (device.kind === 'audio') {
  68.                 device.kind = 'audioinput';
  69.             }
  70.  
  71.             if (device.kind === 'video') {
  72.                 device.kind = 'videoinput';
  73.             }
  74.  
  75.             var skip;
  76.             MediaDevices.forEach(function(d) {
  77.                 if (d.id === device.id && d.kind === device.kind) {
  78.                     skip = true;
  79.                 }
  80.             });
  81.  
  82.             if (skip) {
  83.                 return;
  84.             }
  85.  
  86.             if (!device.deviceId) {
  87.                 device.deviceId = device.id;
  88.             }
  89.  
  90.             if (!device.id) {
  91.                 device.id = device.deviceId;
  92.             }
  93.  
  94.             if (!device.label) {
  95.                 device.label = 'Please invoke getUserMedia once.';
  96.                 if(location.protocol !== 'https:') {
  97.                     device.label = 'HTTPs is required to get label of this ' + device.kind + ' device.';
  98.                 }
  99.             } else {
  100.                 if (device.kind === 'videoinput' && !isWebsiteHasWebcamPermissions) {
  101.                     isWebsiteHasWebcamPermissions = true;
  102.                 }
  103.  
  104.                 if (device.kind === 'audioinput' && !isWebsiteHasMicrophonePermissions) {
  105.                     isWebsiteHasMicrophonePermissions = true;
  106.                 }
  107.             }
  108.  
  109.             if (device.kind === 'audioinput') {
  110.                 hasMicrophone = true;
  111.             }
  112.  
  113.             if (device.kind === 'audiooutput') {
  114.                 hasSpeakers = true;
  115.             }
  116.  
  117.             if (device.kind === 'videoinput') {
  118.                 hasWebcam = true;
  119.             }
  120.  
  121.             // there is no 'videoouput' in the spec.
  122.  
  123.             MediaDevices.push(device);
  124.         });
  125.  
  126.         if (callback) {
  127.             callback();
  128.         }
  129.     });
  130. }
  131.  
  132. function select2ndCamera() {
  133.     this.disabled = true;
  134.    
  135.     // link: https://cdn.webrtc-experiment.com/DetectRTC/checkDevicesSupport.js
  136.  
  137.     checkDeviceSupport(function() {
  138.         var videoDevices = [];
  139.         MediaDevices.forEach(function(device) {
  140.             if(device.kind === 'videoinput') {
  141.                 videoDevices.push(device);
  142.             }
  143.         });
  144.  
  145.         var secondDevice = videoDevices[1];
  146.         if(!secondDevice) return alert('No secondary webcam available.');
  147.  
  148.         var videoConstraints = {
  149.             deviceId: secondDevice.deviceId
  150.         };
  151.  
  152.         if(!!navigator.webkitGetUserMedia) {
  153.             videoConstraints = {
  154.                 mandatory: {},
  155.                 optional: [{
  156.                     sourceId: secondDevice.deviceId
  157.                 }]
  158.             }
  159.         }
  160.  
  161.         navigator.getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
  162.         navigator.getUserMedia({ video: videoConstraints }, function(stream) {
  163.             document.querySelector('video').src = URL.createObjectURL(stream);
  164.         }, function(error) {
  165.             alert(JSON.stringify(error));
  166.         });
  167.     });
  168. }
  169.  
  170. document.querySelector('button').onclick = select2ndCamera;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement