/* * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. Copyright (c) 2014, The WebRTC project authors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Google nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 'use strict'; // Put variables in global scope to make them available to the browser console. const constraints = window.constraints = { audio: true, video: true }; const vconstraints = window.constraints = { audio: false, video: true }; const aconstraints = window.constraints = { audio: true, video: false }; function handleSuccess(stream) { handleVSuccess(stream); handleASuccess(stream); } function handleVSuccess(stream) { const video = document.querySelector('video'); const videoTracks = stream.getVideoTracks(); stream.onremovetrack = function() { console.log('Stream ended'); }; console.log('Got video stream with constraints:', vconstraints); console.log(`Using video device: ${videoTracks[0].label} out of ${videoTracks.length} video tracks`); window.stream += stream; // make variable available to browser console video.srcObject = stream; video.play(); } function handleASuccess(stream) { const audio = document.querySelector('audio'); const audioTracks = stream.getAudioTracks(); console.log('Got audio stream with constraints:', aconstraints); console.log(`Using audio device: ${audioTracks[0].label} out of ${audioTracks.length} audio tracks`); window.stream += stream; // make variable available to browser console audio.srcObject = stream; audio.play(); } function handleError(error) { if (error.name === 'ConstraintNotSatisfiedError') { const v = constraints.video; errorMsg(`The resolution ${v.width.exact}x${v.height.exact} px is not supported by your device.`); } else if (error.name === 'PermissionDeniedError') { errorMsg('Permissions have not been granted to use your camera and ' + 'microphone, you need to allow the page access to your devices in ' + 'order for the demo to work.'); } errorMsg(`getUserMedia error: ${error.name}`, error); } function errorMsg(msg, error) { const errorElement = document.querySelector('#errorMsg'); errorElement.innerHTML += `

${msg}

`; if (typeof error !== 'undefined') { console.error(error); } } async function init(e) { try { const stream = await navigator.mediaDevices.getUserMedia(constraints); handleSuccess(stream); e.target.disabled = true; document.getElementById('showVideo').disabled = true; document.getElementById('showAudio').disabled = true; } catch (e) { handleError(e); } } async function ainit(e) { try { const astream = await navigator.mediaDevices.getUserMedia(aconstraints); handleASuccess(astream); e.target.disabled = true; } catch (e) { handleError(e); } } async function vinit(e) { try { const vstream = await navigator.mediaDevices.getUserMedia(vconstraints); handleVSuccess(vstream); e.target.disabled = true; } catch (e) { handleError(e); } } async function ainit(e) { try { const astream = await navigator.mediaDevices.getUserMedia(aconstraints); handleASuccess(astream); e.target.disabled = true; } catch (e) { handleError(e); } } document.querySelector('#showVideo').addEventListener('click', e => vinit(e)); document.querySelector('#showAudio').addEventListener('click', e => ainit(e)); document.querySelector('#showBoth').addEventListener('click', e => init(e));