some changes

This commit is contained in:
2026-07-10 22:02:51 +02:00
parent 7bd28794d6
commit 6e6217e1e7
33 changed files with 757 additions and 2305 deletions
-18
View File
@@ -1,18 +0,0 @@
const { spawn } = require('child_process');
module.exports = function() {
return new Promise((resolve, reject) => {
const checkFFmpegProcess = spawn('ffmpeg', ['-version'], {
stdio: ['ignore', 'ignore', 'ignore'],
});
checkFFmpegProcess.on('error', () => {
resolve(require('ffmpeg-static'));
});
checkFFmpegProcess.on('exit', (code) => {
if (code === 0) resolve('ffmpeg');
else resolve(require('ffmpeg-static'));
});
});
}
+18 -6
View File
@@ -7,7 +7,23 @@ if (!configExists() || !serverConfig.audio.audioDevice) return;
const { spawn } = require('child_process');
const { logDebug, logError, logInfo, logWarn } = require('../console');
const checkFFmpeg = require('./checkFFmpeg');
const checkFFmpeg = function() {
return new Promise((resolve, reject) => {
const checkFFmpegProcess = spawn('ffmpeg', ['-version'], {
stdio: ['ignore', 'ignore', 'ignore'],
});
checkFFmpegProcess.on('error', () => {
resolve(require('ffmpeg-static'));
});
checkFFmpegProcess.on('exit', (code) => {
if (code === 0) resolve('ffmpeg');
else resolve(require('ffmpeg-static'));
});
});
}
const consoleLogTitle = '[Audio Stream]';
@@ -24,8 +40,7 @@ checkFFmpeg().then((ffmpegPath) => {
logInfo(`${consoleLogTitle} Using ${ffmpegPath === 'ffmpeg' ? 'system-installed FFmpeg' : 'ffmpeg-static'}`);
logInfo(`${consoleLogTitle} Starting audio stream on device: \x1b[35m${serverConfig.audio.audioDevice}\x1b[0m`);
const sampleRate = Number(serverConfig.audio.sampleRate || 44100); // Maybe even do 32 khz, we do not need higher than 15 khz precision
const sampleRate = Number(serverConfig.audio.sampleRate || 32000);
const channels = Number(serverConfig.audio.audioChannels || 2);
let ffmpeg = null;
@@ -80,8 +95,6 @@ checkFFmpeg().then((ffmpegPath) => {
ffmpeg.stdout.pipe(audio_pipe, { end: false });
connectMessage(`${consoleLogTitle} Connected FFmpeg → MP3 → audioWss`);
ffmpeg.stderr.on('data', (data) => {
const msg = data.toString();
@@ -136,7 +149,6 @@ checkFFmpeg().then((ffmpegPath) => {
});
launchFFmpeg();
}).catch((err) => {
logError(`${consoleLogTitle} Error: ${err.message}`);
});
-110
View File
@@ -1,110 +0,0 @@
'use strict';
const exec = require('child_process').exec;
const fs = require('fs').promises; // Use the Promise-based fs API
const ffmpeg = require('ffmpeg-static');
const filePath = '/proc/asound/cards';
const platform = process.platform;
function parseAudioDevice(options, callback) {
let videoDevices = [];
let audioDevices = [];
let isVideo = true;
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
const ffmpegPath = `"${ffmpeg.replace(/\\/g, '\\\\')}"`;
const callbackExists = typeof callback === 'function';
const execute = async (fulfill, reject) => {
try {
if (platform === 'linux') {
try {
const data = await fs.readFile(filePath, 'utf8');
const regex = /\[([^\]]+)\]/g;
const matches = (data.match(regex) || []).map(match => 'hw:' + match.replace(/\s+/g, '').slice(1, -1));
matches.forEach(match => {
if (typeof match === 'string') audioDevices.push({ name: match });
});
} catch (err) {
console.error(`Error reading file: ${err.message}`);
}
// Linux doesn't support the `-list_devices` ffmpeg command like macOS/Windows,
// so skip the ffmpeg exec for Linux
const result = { videoDevices: [], audioDevices };
if (callbackExists) return callback(result);
return fulfill(result);
}
let inputDevice, prefix, audioSeparator, alternativeName, deviceParams;
switch (platform) {
case 'win32':
inputDevice = 'dshow';
prefix = /\[dshow/;
audioSeparator = /DirectShow\saudio\sdevices/;
alternativeName = /Alternative\sname\s*?"(.*?)"/;
deviceParams = /"(.*?)"/;
break;
case 'darwin':
inputDevice = 'avfoundation';
prefix = /^\[AVFoundation/;
audioSeparator = /AVFoundation\saudio\sdevices/;
deviceParams = /^\[AVFoundation.*?]\s\[(\d+)]\s(.*)$/;
break;
}
exec(`${ffmpegPath} -f ${inputDevice} -list_devices true -i ""`, (err, stdout, stderr) => {
stderr.split("\n")
.filter(line => line.search(prefix) > -1)
.forEach(line => {
const deviceList = isVideo ? videoDevices : audioDevices;
if (line.search(audioSeparator) > -1) {
isVideo = false;
return;
}
if (platform === 'win32' && line.search(/Alternative\sname/) > -1) {
const lastDevice = deviceList[deviceList.length - 1];
const alt = line.match(alternativeName);
if (lastDevice && alt) lastDevice.alternativeName = alt[1];
return;
}
const params = line.match(deviceParams);
if (params) {
let device;
switch (platform) {
case 'win32':
device = { name: params[1] };
break;
case 'darwin':
device = { id: parseInt(params[1]), name: params[2] };
break;
}
deviceList.push(device);
}
});
audioDevices = audioDevices.filter(device => device.name !== undefined);
const result = { videoDevices, audioDevices };
if (callbackExists) return callback(result);
return fulfill(result);
});
} catch (err) {
console.error('Unexpected error:', err);
if (callbackExists) callback({ videoDevices: [], audioDevices: [] });
else reject(err);
}
};
if (callbackExists) execute();
else return new Promise(execute);
}
module.exports = parseAudioDevice;