1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00

bugfixes / more code cleanup

This commit is contained in:
NoobishSVK
2024-03-21 22:30:18 +01:00
parent 0f10e04bcd
commit 8f4c96ce12
12 changed files with 166 additions and 81 deletions

View File

@@ -268,10 +268,15 @@ function handleData(ws, receivedData) {
}
break;
case receivedLine.startsWith('T'):
modifiedData = receivedLine.substring(1).split(",")[0];
if((modifiedData / 1000).toFixed(3) == dataToSend.freq) {
return; // Prevent tune spamming using scrollwheel
}
resetToDefault(dataToSend);
dataToSend.af.length = 0;
rdsparser.clear(rds);
modifiedData = receivedLine.substring(1);
parsedValue = parseFloat(modifiedData);
if (!isNaN(parsedValue)) {
@@ -293,17 +298,20 @@ function handleData(ws, receivedData) {
dataToSend.ims = mapping.ims;
}
break;
case receivedData.startsWith('Sm'):
processSignal(receivedData, false, false);
case receivedLine.startsWith('W'):
console.log(receivedLine);
break;
case receivedData.startsWith('Ss'):
processSignal(receivedData, true, false);
case receivedLine.startsWith('Sm'):
processSignal(receivedLine, false, false);
break;
case receivedData.startsWith('SS'):
processSignal(receivedData, true, true);
case receivedLine.startsWith('Ss'):
processSignal(receivedLine, true, false);
break;
case receivedData.startsWith('SM'):
processSignal(receivedData, false, true);
case receivedLine.startsWith('SS'):
processSignal(receivedLine, true, true);
break;
case receivedLine.startsWith('SM'):
processSignal(receivedLine, false, true);
break;
case receivedLine.startsWith('R'):
modifiedData = receivedLine.slice(1);

View File

@@ -25,7 +25,6 @@ const { logDebug, logError, logInfo, logWarn } = require('./console');
const storage = require('./storage');
const { configName, serverConfig, configUpdate, configSave } = require('./server_config');
const pjson = require('../package.json');
require('./stream/index');
console.log(`\x1b[32m
_____ __ __ ______ __ __ __ _
@@ -37,6 +36,9 @@ console.log(`\x1b[32m
console.log('\x1b[0mFM-DX-Webserver', pjson.version);
console.log('\x1b[90m======================================================');
// Start ffmpeg
require('./stream/index');
// Create a WebSocket proxy instance
const proxy = httpProxy.createProxyServer({
target: 'ws://localhost:' + (Number(serverConfig.webserver.webserverPort) + 10), // WebSocket httpServer's address
@@ -67,8 +69,8 @@ function connectToSerial() {
serialport.on('open', () => {
logInfo('Using COM device: ' + serverConfig.xdrd.comPort);
serialport.write('x\n');
serialport.write('W0\n');
serialport.write('M0\n');
serialport.write('Y100\n');
serialport.write('D0\n');
@@ -232,6 +234,7 @@ app.use('/', endpoints);
* WEBSOCKET BLOCK
*/
wss.on('connection', (ws, request) => {
const output = serverConfig.xdrd.wirelessConnection ? client : serialport;
const clientIp = request.headers['x-forwarded-for'] || request.connection.remoteAddress;
currentUsers++;
dataHandler.showOnlineUsers(currentUsers);
@@ -310,14 +313,22 @@ wss.on('connection', (ws, request) => {
}
}
const { isAdminAuthenticated, isTuneAuthenticated } = request.session || {};
const { wirelessConnection } = serverConfig.xdrd;
const { isAdminAuthenticated, isTuneAuthenticated } = request.session || {};
if ((serverConfig.publicTuner || (isTuneAuthenticated && wirelessConnection)) &&
(!serverConfig.lockToAdmin || isAdminAuthenticated)) {
const output = serverConfig.xdrd.wirelessConnection ? client : serialport;
output.write(`${command}\n`);
if (serverConfig.publicTuner && !serverConfig.lockToAdmin) {
output.write(`${command}\n`);
} else {
if (serverConfig.lockToAdmin) {
if(isAdminAuthenticated) {
output.write(`${command}\n`);
}
} else {
if(isTuneAuthenticated) {
output.write(`${command}\n`);
}
}
}
});
ws.on('close', (code, reason) => {
@@ -333,9 +344,10 @@ wss.on('connection', (ws, request) => {
if (currentUsers === 0 && serverConfig.enableDefaultFreq === true && serverConfig.autoShutdown !== true && serverConfig.xdrd.wirelessConnection === true) {
setTimeout(function() {
if(currentUsers === 0) {
client.write('T' + Math.round(serverConfig.defaultFreq * 1000) +'\n');
output.write('T' + Math.round(serverConfig.defaultFreq * 1000) +'\n');
dataHandler.resetToDefault(dataHandler.dataToSend);
dataHandler.dataToSend.freq = Number(serverConfig.defaultFreq).toFixed(3);
dataHandler.initialData.freq = Number(serverConfig.defaultFreq).toFixed(3);
}
}, 10000)
}

View File

@@ -1,16 +1,7 @@
"use strict";
var fs = require('fs');
let serverConfig = {
audio: {
audioBitrate: "128k"
},
};
if(fs.existsSync('./config.json')) {
const configFileContents = fs.readFileSync('./config.json', 'utf8');
serverConfig = JSON.parse(configFileContents);
}
const ffmpegStaticPath = require('ffmpeg-static');
const {serverConfig} = require('../server_config')
/*
Stdin streamer is part of 3LAS (Low Latency Live Audio Streaming)
@@ -50,12 +41,7 @@ const child_process_1 = require("child_process");
const ws = __importStar(require("ws"));
const wrtc = require('wrtc');
const Settings = JSON.parse((0, fs_1.readFileSync)('server/stream/settings.json', 'utf-8'));
const FFmpeg_command = (() => {
if (process.platform === 'win32')
return Settings.FallbackFFmpegPath;
else if (process.platform === 'linux')
return "ffmpeg";
})();
const FFmpeg_command = ffmpegStaticPath;
class RtcProvider {
constructor() {
this.RtcDistributePeer = new wrtc.RTCPeerConnection(Settings.RtcConfig);

View File

@@ -1,29 +1,18 @@
const { spawn } = require('child_process');
const consoleCmd = require('../console.js');
const ffmpeg = require('ffmpeg-static');
const { configName, serverConfig, configUpdate, configSave } = require('../server_config');
const { logDebug, logError, logInfo, logWarn } = require('../console');
const commandExists = require('command-exists-promise');
// Check if FFmpeg is installed
commandExists('ffmpeg')
.then(exists => {
if (exists) {
logInfo("An existing installation of ffmpeg found, enabling audio stream.");
enableAudioStream();
} else {
logError("No ffmpeg installation found. Audio stream won't be available.");
}
})
const { logDebug, logError, logInfo, logWarn, logFfmpeg } = require('../console');
function enableAudioStream() {
var ffmpegCommand;
serverConfig.webserver.webserverPort = Number(serverConfig.webserver.webserverPort);
// Specify the command and its arguments
const command = 'ffmpeg';
const command = ffmpeg.replace(/\\/g, '\\\\');
const flags = `-fflags +nobuffer+flush_packets -flags low_delay -rtbufsize 6192 -probesize 32`;
const codec = `-acodec pcm_s16le -ar 48000 -ac ${serverConfig.audio.audioChannels}`;
const output = `-f s16le -fflags +nobuffer+flush_packets -packetsize 384 -flush_packets 1 -bufsize 960`;
// Combine all the settings for the ffmpeg command
if (process.platform === 'win32') {
// Windows
ffmpegCommand = `${flags} -f dshow -audio_buffer_size 50 -i audio="${serverConfig.audio.audioDevice}" ${codec} ${output} pipe:1 | node server/stream/3las.server.js -port ${serverConfig.webserver.webserverPort + 10} -samplerate 48000 -channels ${serverConfig.audio.audioChannels}`;
@@ -34,28 +23,27 @@ function enableAudioStream() {
consoleCmd.logInfo("Using audio device: " + serverConfig.audio.audioDevice);
consoleCmd.logInfo(`Launching audio stream on internal port ${serverConfig.webserver.webserverPort + 10}.`);
// Spawn the child process
// If an audio device is configured, start the stream
if(serverConfig.audio.audioDevice.length > 2) {
const childProcess = spawn(command, [ffmpegCommand], { shell: true });
// Handle the output of the child process (optional)
childProcess.stdout.on('data', (data) => {
consoleCmd.logFfmpeg(`stdout: ${data}`);
logFfmpeg(`stdout: ${data}`);
});
childProcess.stderr.on('data', (data) => {
consoleCmd.logFfmpeg(`stderr: ${data}`);
logFfmpeg(`stderr: ${data}`);
});
// Handle the child process exit event
childProcess.on('close', (code) => {
consoleCmd.logFfmpeg(`Child process exited with code ${code}`);
logFfmpeg(`Child process exited with code ${code}`);
});
// You can also listen for the 'error' event in case the process fails to start
childProcess.on('error', (err) => {
consoleCmd.logFfmpeg(`Error starting child process: ${err}`);
logFfmpeg(`Error starting child process: ${err}`);
});
}
}
}
enableAudioStream();

View File

@@ -2,6 +2,7 @@
const exec = require('child_process').exec;
const fs = require('fs');
const ffmpeg = require('ffmpeg-static');
const filePath = '/proc/asound/cards';
const platform = process.platform;
@@ -15,7 +16,7 @@ function parseAudioDevice(options, callback) {
options = null;
}
options = options || {};
const ffmpegPath = options.ffmpegPath || 'ffmpeg';
const ffmpegPath = ffmpeg.replace(/\\/g, '\\\\');
const callbackExists = typeof callback === 'function';
let inputDevice, prefix, audioSeparator, alternativeName, deviceParams;