var srcwid=wid; var srchei=hei; var fUpdatedSizeH=1; var fUpdatedSizeV=1; var myvideo = document.querySelector('video'); var mycanvas = document.getElementById('canvas'); var mycontext = canvas.getContext('2d', {willReadFrequently: true }); var canvas2 = document.getElementById('canvasbuf');//ダブルバッファ var context2 = canvas2.getContext('2d', {willReadFrequently: true }); var isErr=false; var selectSource = document.querySelector('select#videoSource'); selectSource.onchange = camChanged; var videoSource = undefined; //----change size function sizeChanged(obj){ let aSize = obj.options[obj.selectedIndex].value.split(','); window.location.href='?cmd=setsize&relcmd=' + cmd + '&w=' + aSize[0] + '&h=' + aSize[1]; } //----change camera function camChanged(){ cameraid = selectSource.value; document.cookie = 'cameraid=' + selectSource.value + ';max-age=86400'; //alert('setcookie' + cameraid); removeChildren(selectSource); streamStart(); } //---reset device select function removeChildren(x){ if (x.hasChildNodes()) { while (x.childNodes.length > 0) { x.removeChild(x.firstChild) } } } //------STREAM function streamStart(){ if (!navigator.mediaDevices) { isErr=true; videoStatus('MediaDevices is not supported. Please consider switching to a different web browser.'); return; } streamStartInit().then(deviceGet).then(deviceEnum); } function streamStartInit() { //--Stereamがあれば停止 if (window.stream) { window.stream.getTracks().forEach(track => { track.stop(); }); } //---パラメータ作成 var constraints = { audio: true, video: true } videoSource = streamGetCookie(); if(videoSource!='' && videoSource!='undefined'){ var constraints = {video: {deviceId: {exact: videoSource}}}; } return navigator.mediaDevices.getUserMedia(constraints).then(streamStartSet).catch(streamError); } function streamStartSet(stream) { window.stream = stream; selectSource.selectedIndex = [...selectSource.options].findIndex(option => option.text === stream.getVideoTracks()[0].label); myvideo.srcObject = stream; videoSetup(); if(!isErr) videoStatus('Setup is done.'); return true; } function streamError(error) { isErr=true; videoStatus('Webcam Error: ' + error + ' Check your webcam and try again.'); //alert('Webcam Error!!: ' + error + ' Check your webcam and try again.'); //videoStatus('Webcam Error!: ' + error + ' Please reload.'); document.cookie = 'cameraid=;max-age=86400'; } function streamGetCookie(){ if(cameraid != '')return cameraid; var cookies = document.cookie; var cookiesArray = cookies.split(';'); var videoSource = selectSource.value; for(var c of cookiesArray){ var cArray = c.split('='); if( cArray[0] == 'cameraid'){ return cArray[1]; break; } } return undefined; } //------DEVICES function deviceGet() { return navigator.mediaDevices.enumerateDevices(); } function deviceEnum(deviceInfos) { window.deviceInfos = deviceInfos; for (const deviceInfo of deviceInfos) { const option = document.createElement('option'); option.value = deviceInfo.deviceId; if (deviceInfo.kind === 'videoinput') { option.text = deviceInfo.label; selectSource.appendChild(option); } } //----選択し直す if(videoSource!=''){ var myindex=0; const options = selectSource.options; Array.from(options).forEach(function(option) { if(option.value == videoSource){ option.selected = true; } }); } } //------BUF function videoSetup(){ videoSetupEx(); videoLoop(); } function videoLoop() { requestAnimationFrame(videoLoop); videoLoopEx(); } //------MISC function videoStatus(s){ $('#dResult').html(s); } window.addEventListener('load', streamStart);