Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <button>Select 2nd Camera</button><hr>
- <video controls autoplay style="width:100%;"></video>
- <script>
- var MediaDevices = [];
- if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
- // Firefox 38+ seems having support of enumerateDevices
- // Thanks @xdumaine/enumerateDevices
- navigator.enumerateDevices = function(callback) {
- navigator.mediaDevices.enumerateDevices().then(callback);
- };
- }
- // ---------- Media Devices detection
- var canEnumerate = false;
- /*global MediaStreamTrack:true */
- if(typeof MediaStreamTrack !== 'undefined' && 'getSources' in MediaStreamTrack) {
- canEnumerate = true;
- }
- else if(navigator.mediaDevices && !!navigator.mediaDevices.enumerateDevices) {
- canEnumerate = true;
- }
- var hasMicrophone = false;
- var hasSpeakers = false;
- var hasWebcam = false;
- var isWebsiteHasMicrophonePermissions = false;
- var isWebsiteHasWebcamPermissions = false;
- // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediadevices
- // todo: switch to enumerateDevices when landed in canary.
- function checkDeviceSupport(callback) {
- if(!canEnumerate) {
- return;
- }
- // This method is useful only for Chrome!
- if (!navigator.enumerateDevices && window.MediaStreamTrack && window.MediaStreamTrack.getSources) {
- navigator.enumerateDevices = window.MediaStreamTrack.getSources.bind(window.MediaStreamTrack);
- }
- if (!navigator.enumerateDevices && navigator.enumerateDevices) {
- navigator.enumerateDevices = navigator.enumerateDevices.bind(navigator);
- }
- if (!navigator.enumerateDevices) {
- if (callback) {
- callback();
- }
- return;
- }
- MediaDevices = [];
- navigator.enumerateDevices(function(devices) {
- devices.forEach(function(_device) {
- var device = {};
- for (var d in _device) {
- device[d] = _device[d];
- }
- // if it is MediaStreamTrack.getSources
- if (device.kind === 'audio') {
- device.kind = 'audioinput';
- }
- if (device.kind === 'video') {
- device.kind = 'videoinput';
- }
- var skip;
- MediaDevices.forEach(function(d) {
- if (d.id === device.id && d.kind === device.kind) {
- skip = true;
- }
- });
- if (skip) {
- return;
- }
- if (!device.deviceId) {
- device.deviceId = device.id;
- }
- if (!device.id) {
- device.id = device.deviceId;
- }
- if (!device.label) {
- device.label = 'Please invoke getUserMedia once.';
- if(location.protocol !== 'https:') {
- device.label = 'HTTPs is required to get label of this ' + device.kind + ' device.';
- }
- } else {
- if (device.kind === 'videoinput' && !isWebsiteHasWebcamPermissions) {
- isWebsiteHasWebcamPermissions = true;
- }
- if (device.kind === 'audioinput' && !isWebsiteHasMicrophonePermissions) {
- isWebsiteHasMicrophonePermissions = true;
- }
- }
- if (device.kind === 'audioinput') {
- hasMicrophone = true;
- }
- if (device.kind === 'audiooutput') {
- hasSpeakers = true;
- }
- if (device.kind === 'videoinput') {
- hasWebcam = true;
- }
- // there is no 'videoouput' in the spec.
- MediaDevices.push(device);
- });
- if (callback) {
- callback();
- }
- });
- }
- function select2ndCamera() {
- this.disabled = true;
- // link: https://cdn.webrtc-experiment.com/DetectRTC/checkDevicesSupport.js
- checkDeviceSupport(function() {
- var videoDevices = [];
- MediaDevices.forEach(function(device) {
- if(device.kind === 'videoinput') {
- videoDevices.push(device);
- }
- });
- var secondDevice = videoDevices[1];
- if(!secondDevice) return alert('No secondary webcam available.');
- var videoConstraints = {
- deviceId: secondDevice.deviceId
- };
- if(!!navigator.webkitGetUserMedia) {
- videoConstraints = {
- mandatory: {},
- optional: [{
- sourceId: secondDevice.deviceId
- }]
- }
- }
- navigator.getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
- navigator.getUserMedia({ video: videoConstraints }, function(stream) {
- document.querySelector('video').src = URL.createObjectURL(stream);
- }, function(error) {
- alert(JSON.stringify(error));
- });
- });
- }
- document.querySelector('button').onclick = select2ndCamera;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement