var target = ""; var hosts = []; var pairingCert; var myUniqueid; var api; // Called by the common.js module. function attachListeners() { $('#selectResolution').on('change', saveResolution); $('#selectFramerate').on('change', saveFramerate); $('#bitrateSlider').on('input', updateBitrateField); // input occurs every notch you slide $('#bitrateSlider').on('change', saveBitrate); // change occurs once the mouse lets go. $('#pairButton').on('click', pairPushed); $('#cancelPairingDialog').on('click', pairingPopupCanceled); $('#showAppsButton').on('click', showAppsPushed); $('#selectGame').on('change', gameSelectUpdated); $('#startGameButton').on('click', startSelectedGame); $('#cancelReplaceApp').on('click', cancelReplaceApp); $('#continueReplaceApp').on('click', continueReplaceApp); $('#quitGameButton').on('click', stopGame); $(window).resize(fullscreenNaclModule); } function snackbarLog(givenMessage) { console.log(givenMessage); var data = { message: givenMessage, timeout: 5000 }; document.querySelector('#snackbar').MaterialSnackbar.showSnackbar(data); } function updateBitrateField() { $('#bitrateField').html($('#bitrateSlider').val() + " Mbps"); } function moduleDidLoad() { if(!myUniqueid) { console.log("Failed to get uniqueId. Generating new one"); myUniqueid = uniqueid(); storeData('uniqueid', myUniqueid, null); } if(!pairingCert) { // we couldn't load a cert. Make one. console.log("Failed to load local cert. Generating new one"); sendMessage('makeCert', []).then(function (cert) { storeData('cert', cert, null); pairingCert = cert; console.log("Generated new cert."); }).then(function (ret) { sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]); }); } else { sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) { snackbarLog('Initialization complete.'); }); } } // because the user can change the target host at any time, we continually have to check function updateTarget() { target = $('#GFEHostIPField').val(); if (target == null || target == "") { target = $("#selectHost option:selected").val(); } if(api && api.address != target) { api = new NvHTTP(target, myUniqueid); } } // we want the user to progress through the streaming process // but to save from the PITA of inter-chrome-app-page JS message passing, // I'm opting to do it in a single page, and keep the data around. function hideAllWorkflowDivs() { $('#streamSettings').css('display', 'inline-block'); $('#hostSettings').css('display', 'inline-block'); $('#gameSelection').css('display', 'none'); // do NOT hide the nacl module. you can't interact with it then } // pair button was pushed. pass what the user entered into the GFEHostIPField. function pairPushed() { updateTarget(); if(!pairingCert) { snackbarLog('ERROR: cert has not been generated yet. Is NaCL initialized?'); console.log("User wants to pair, and we still have no cert. Problem = very yes."); return; } if(!api) { api = new NvHTTP(target, myUniqueid); } if(api.paired) { return; } $('#pairButton').html('Pairing...'); snackbarLog('Attempting pair to: ' + target); var randomNumber = String("0000" + (Math.random()*10000|0)).slice(-4); var pairingDialog = document.querySelector('#pairingDialog'); $('#pairingDialogText').html('Please enter the number ' + randomNumber + ' on the GFE dialog on the computer. This dialog will be dismissed once complete'); pairingDialog.showModal(); console.log('sending pairing request to ' + target + ' with random number ' + randomNumber); api.pair(randomNumber).then(function (paired) { if (!paired) { snackbarLog('Pairing failed'); $('#pairButton').html('Pairing Failed'); $('#pairingDialogText').html('Error: Pairing failed'); if (api.currentGame != 0) snackbarLog(target + ' is already in game. Cannot pair!'); return; } $('#pairButton').html('Paired'); snackbarLog('Pairing successful'); pairingDialog.close(); var hostSelect = $('#selectHost')[0]; for(var i = 0; i < hostSelect.length; i++) { // check if we already have the host. if (hostSelect.options[i].value == target) return; } var opt = document.createElement('option'); opt.appendChild(document.createTextNode(target)); opt.value = target; $('#selectHost')[0].appendChild(opt); hosts.push(target); saveHosts(); showAppsPushed(); }); } function pairingPopupCanceled() { document.querySelector('#pairingDialog').close(); } // someone pushed the "show apps" button. // if they entered something in the GFEHostIPField, use that. // otherwise, we assume they selected from the host history dropdown. function showAppsPushed() { updateTarget(); if(!api || !api.paired) { return; } api.getAppList().then(function (appList) { if ($('#selectGame').has('option').length > 0 ) { // there was already things in the dropdown. Clear it, then add the new ones. // Most likely, the user just hit the 'retrieve app list' button again $('#selectGame').empty(); } appList.forEach(function (app) { $('#selectGame').append($('