feat: 实现功能&播放器内核&实现页面

- AMLL MeshGradient背景
- 全屏播放页初始化
- 纯C音频播放器
  - FFmpeg解码
  - 编译FFmpeg静态库
  - wasapi shared
  - IPC通信
  - FFTW实时频谱计算
  - 低频响度实时计算
  - PCM缓存
  - 数据缓存&解码缓存
 - 弃用mpv,改用qzplayer
This commit is contained in:
lqtmcstudio
2026-02-05 18:52:58 +08:00
parent 8eab16cbf5
commit 6a94931cbd
20 changed files with 2791 additions and 373 deletions

View File

@@ -3,7 +3,7 @@ import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import fs from 'node:fs'
import { MpvController } from './mpvController'
import { QzpController } from './qzpController.ts'
import { startProxyServer, cleanupCache, getCacheDir, getCacheSize, setPersistCache, clearCacheNow } from './proxyServer'
import { PluginSystem } from '../src/main/pluginSystem.ts'
import { loadSettings, saveSettings, getSetting, AppSettings } from './settingsStore'
@@ -20,7 +20,7 @@ export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist')
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL ? path.join(process.env.APP_ROOT, 'public') : RENDERER_DIST
let win: BrowserWindow | null
let mpv: MpvController | null
let qzplayer: QzpController | null
// === Electron 窗口逻辑 ===
@@ -59,21 +59,21 @@ ipcMain.on('window-maximize', () => win?.isMaximized() ? win.unmaximize() : win?
ipcMain.on('window-close', () => win?.close())
ipcMain.handle('window-is-maximized', () => win?.isMaximized() || false)
// --- MPV IPC Handlers ---
ipcMain.handle('mpv-command', async (_, command: any[]) => {
if (mpv) {
mpv.send(command)
// --- qzplayer IPC Handlers ---
ipcMain.handle('qzplayer-command', async (_, command: any[]) => {
if (qzplayer) {
qzplayer.send(command)
}
})
// Quick Helpers
ipcMain.handle('mpv-load', (_, url) => mpv?.load(url))
ipcMain.handle('mpv-play', () => mpv?.play())
ipcMain.handle('mpv-pause', () => mpv?.pause())
ipcMain.handle('mpv-toggle-pause', () => mpv?.togglePause())
ipcMain.handle('mpv-stop', () => mpv?.stop())
ipcMain.handle('mpv-set-volume', (_, vol) => mpv?.setVolume(vol))
ipcMain.handle('mpv-seek', (_, time) => mpv?.seek(time))
ipcMain.handle('qzplayer-load', (_, url) => qzplayer?.load(url))
ipcMain.handle('qzplayer-play', () => qzplayer?.play())
ipcMain.handle('qzplayer-pause', () => qzplayer?.pause())
ipcMain.handle('qzplayer-toggle-pause', () => qzplayer?.togglePause())
ipcMain.handle('qzplayer-stop', () => qzplayer?.stop())
ipcMain.handle('qzplayer-set-volume', (_, vol) => qzplayer?.setVolume(vol))
ipcMain.handle('qzplayer-seek', (_, time) => qzplayer?.seek(time))
// PluginSystem
ipcMain.handle(
@@ -150,8 +150,8 @@ app.on('window-all-closed', () => {
app.on('will-quit', () => {
cleanupCache()
if (mpv) {
mpv.destroy()
if (qzplayer) {
qzplayer.destroy()
}
})
@@ -219,14 +219,14 @@ module.exports = {
// Start Proxy Server
startProxyServer()
// Start MPV
mpv = new MpvController()
mpv.start()
// Start qzplayer
qzplayer = new QzpController()
qzplayer.start()
mpv.on('event', (data) => {
// Forward MPV events to Render Process
qzplayer.on('event', (data) => {
// Forward qzplayer events to Render Process
if (win && !win.isDestroyed()) {
win.webContents.send('mpv-event', data)
win.webContents.send('qzplayer-event', data)
}
})
})

View File

@@ -7,16 +7,16 @@ contextBridge.exposeInMainWorld('electronAPI', {
closeWindow: () => ipcRenderer.send('window-close'),
isMaximized: () => ipcRenderer.invoke('window-is-maximized'),
// MPV Control
mpv: {
load: (url: string) => ipcRenderer.invoke('mpv-load', url),
play: () => ipcRenderer.invoke('mpv-play'),
pause: () => ipcRenderer.invoke('mpv-pause'),
togglePause: () => ipcRenderer.invoke('mpv-toggle-pause'),
stop: () => ipcRenderer.invoke('mpv-stop'),
setVolume: (vol: number) => ipcRenderer.invoke('mpv-set-volume', vol),
seek: (time: number) => ipcRenderer.invoke('mpv-seek', time),
onEvent: (callback: (event: any, data: any) => void) => ipcRenderer.on('mpv-event', callback)
// qzplayer Control
qzplayer: {
load: (url: string) => ipcRenderer.invoke('qzplayer-load', url),
play: () => ipcRenderer.invoke('qzplayer-play'),
pause: () => ipcRenderer.invoke('qzplayer-pause'),
togglePause: () => ipcRenderer.invoke('qzplayer-toggle-pause'),
stop: () => ipcRenderer.invoke('qzplayer-stop'),
setVolume: (vol: number) => ipcRenderer.invoke('qzplayer-set-volume', vol),
seek: (time: number) => ipcRenderer.invoke('qzplayer-seek', time),
onEvent: (callback: (event: any, data: any) => void) => ipcRenderer.on('qzplayer-event', callback)
},
// Plugin System

View File

@@ -1,5 +1,5 @@
import http from 'http';
import { Readable, PassThrough } from 'stream';
import { Readable } from 'stream';
import fs from 'fs';
import path from 'path';
import { app } from 'electron';
@@ -42,6 +42,8 @@ interface DownloadTask {
contentType: string;
abortController: AbortController | null;
promise: Promise<void> | null;
// 新增:用于通知等待者的回调列表
waiters: Array<{ start: number; end: number; resolve: () => void; reject: (err: Error) => void }>;
}
// Memory cache for resolved URLs
@@ -212,7 +214,6 @@ async function proxyRangeDirect(
throw { statusCode: response.status, statusText: response.statusText };
}
// Forward response headers
const responseContentType = response.headers.get('content-type') || contentType || 'audio/mpeg';
const contentLength = response.headers.get('content-length');
const contentRange = response.headers.get('content-range');
@@ -269,9 +270,18 @@ function serveFromPartialCache(
const { start, end } = range;
// 检查请求的范围是否已下载
if (end >= task.currentSize) {
return false; // 还没下载到这里
// 检查请求的范围是否已下载,使用实际文件大小而不是 task.currentSize
// 因为文件写入可能有延迟
let actualSize: number;
try {
actualSize = fs.statSync(tempPath).size;
} catch {
return false;
}
// 确保请求的范围完全在已下载的部分内
if (end >= actualSize) {
return false;
}
const chunksize = (end - start) + 1;
@@ -286,6 +296,10 @@ function serveFromPartialCache(
const file = fs.createReadStream(tempPath, { start, end });
file.pipe(res);
file.on('error', (err) => {
console.error('[Proxy] Error reading partial cache:', err);
});
return true;
} catch (e) {
console.error('[Proxy] Error serving from partial cache:', e);
@@ -295,6 +309,7 @@ function serveFromPartialCache(
/**
* 启动后台下载任务(不阻塞当前请求)
* 修复版本:确保下载任务独立运行,不受客户端连接影响
*/
function startBackgroundDownload(
targetUrl: string,
@@ -302,7 +317,8 @@ function startBackgroundDownload(
cacheKey: string
): DownloadTask {
const existingTask = downloadTasks.get(cacheFilePath);
if (existingTask) {
if (existingTask && existingTask.promise) {
console.log(`[Proxy] Reusing existing download task for ${cacheFilePath}`);
return existingTask;
}
@@ -313,6 +329,7 @@ function startBackgroundDownload(
contentType: 'audio/mpeg',
abortController,
promise: null,
waiters: [],
};
downloadTasks.set(cacheFilePath, task);
@@ -365,12 +382,20 @@ function startBackgroundDownload(
await new Promise<void>((resolve, reject) => {
nodeStream.on('data', (chunk: Buffer) => {
task.currentSize += chunk.length;
// 通知等待下载进度的请求
notifyWaiters(task);
// 每 10% 输出一次进度
if (task.currentSize % Math.floor(contentLength / 10) < chunk.length) {
const percent = Math.floor((task.currentSize / contentLength) * 100);
console.log(`[Proxy] Download progress: ${percent}% (${task.currentSize}/${contentLength})`);
}
});
nodeStream.pipe(fileStream);
fileStream.on('finish', () => {
// 验证并移动文件
try {
const stat = fs.statSync(tempPath);
if (stat.size === contentLength) {
@@ -412,6 +437,12 @@ function startBackgroundDownload(
console.error('[Proxy] Background download failed:', e);
}
try { fs.unlinkSync(tempPath); } catch { }
// 通知所有等待者下载失败
for (const waiter of task.waiters) {
waiter.reject(new Error('Download failed'));
}
task.waiters = [];
} finally {
downloadTasks.delete(cacheFilePath);
}
@@ -420,8 +451,30 @@ function startBackgroundDownload(
return task;
}
/**
* 通知等待下载进度的请求
*/
function notifyWaiters(task: DownloadTask) {
const resolvedWaiters: number[] = [];
for (let i = 0; i < task.waiters.length; i++) {
const waiter = task.waiters[i];
// 当下载进度超过请求的结束位置时,通知等待者
if (task.currentSize > waiter.end) {
waiter.resolve();
resolvedWaiters.push(i);
}
}
// 移除已解决的等待者
for (let i = resolvedWaiters.length - 1; i >= 0; i--) {
task.waiters.splice(resolvedWaiters[i], 1);
}
}
/**
* 主代理函数 - 智能处理缓存和 Range 请求
* 修复版本:正确处理流的复制,确保下载和响应同时进行
*/
async function proxyAndCache(
req: http.IncomingMessage,
@@ -439,24 +492,29 @@ async function proxyAndCache(
if (task) {
console.log(`[Proxy] Download in progress: ${task.currentSize}/${task.totalSize}`);
// 尝试从部分缓存中读取
if (requestedRange && task.totalSize > 0) {
const range = parseRangeHeader(requestedRange, task.totalSize);
if (range) {
// 检查请求的数据是否已下载
// 给一些缓冲区间(已下载部分的 95%
const safeEnd = Math.floor(task.currentSize * 0.95);
// 获取临时文件的实际大小
const tempPath = getTempPath(cacheFilePath);
let actualSize = 0;
try {
if (fs.existsSync(tempPath)) {
actualSize = fs.statSync(tempPath).size;
}
} catch { }
if (range.end < safeEnd && range.start < safeEnd) {
console.log(`[Proxy] Serving range ${range.start}-${range.end} from partial cache (downloaded: ${task.currentSize})`);
// 如果请求的范围已经下载完成,从缓存读取
if (range.end < actualSize) {
console.log(`[Proxy] Serving range ${range.start}-${range.end} from partial cache (downloaded: ${actualSize})`);
if (serveFromPartialCache(req, res, cacheFilePath, task)) {
return;
}
}
// 请求的数据还没下载到,直接代理这个 Range
console.log(`[Proxy] Range ${range.start}-${range.end} not cached yet (downloaded: ${task.currentSize}), proxying directly`);
// 请求的数据还没下载到,直接代理
console.log(`[Proxy] Range ${range.start}-${range.end} not cached yet (downloaded: ${actualSize}), proxying directly`);
await proxyRangeDirect(req, res, targetUrl, task.totalSize, task.contentType);
return;
}
@@ -492,8 +550,21 @@ async function proxyAndCache(
return;
}
// 从头开始的请求:启动主下载任务并流式返回
// 从头开始的请求:下载并同时流式返回
await streamAndCache(req, res, targetUrl, cacheFilePath, cacheKey);
}
/**
* 新增:边下载边返回给客户端,同时写入缓存
* 使用 PassThrough 流正确复制数据
*/
async function streamAndCache(
req: http.IncomingMessage,
res: http.ServerResponse,
targetUrl: string,
cacheFilePath: string,
cacheKey: string
): Promise<void> {
const headers: Record<string, string> = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
};
@@ -527,7 +598,6 @@ async function proxyAndCache(
return;
}
// 设置下载任务
const tempPath = getTempPath(cacheFilePath);
// 清理旧文件
@@ -538,12 +608,14 @@ async function proxyAndCache(
const fileStream = fs.createWriteStream(tempPath);
task = {
// 创建下载任务用于跟踪进度
const task: DownloadTask = {
currentSize: 0,
totalSize: contentLength,
contentType: contentType,
abortController: null,
promise: null,
waiters: [],
};
downloadTasks.set(cacheFilePath, task);
@@ -564,27 +636,63 @@ async function proxyAndCache(
// @ts-ignore
const nodeStream = Readable.fromWeb(response.body);
const passThrough = new PassThrough();
// 跟踪下载进度
passThrough.on('data', (chunk: Buffer) => {
if (task) {
task.currentSize += chunk.length;
}
// 关键修复:使用正确的方式将流复制到多个目标
// 手动读取数据并写入多个目标
let clientConnected = true;
let downloadComplete = false;
res.on('close', () => {
clientConnected = false;
console.log('[Proxy] Client disconnected');
});
// 流到客户端
passThrough.pipe(res);
return new Promise<void>((resolve, reject) => {
nodeStream.on('data', (chunk: Buffer) => {
task.currentSize += chunk.length;
// 流到文件
nodeStream.pipe(passThrough);
nodeStream.pipe(fileStream);
// 写入文件(始终进行)
fileStream.write(chunk);
// 处理完成
const cleanup = (success: boolean, error?: Error) => {
downloadTasks.delete(cacheFilePath);
// 写入响应(如果客户端还连接着)
if (clientConnected && !res.writableEnded) {
res.write(chunk);
}
if (success && fs.existsSync(tempPath)) {
// 通知等待者
notifyWaiters(task);
});
nodeStream.on('end', () => {
downloadComplete = true;
// 结束文件流
fileStream.end();
// 结束响应(如果客户端还连接着)
if (clientConnected && !res.writableEnded) {
res.end();
}
});
nodeStream.on('error', (err: Error) => {
console.error('[Proxy] Stream error:', err);
fileStream.destroy();
if (clientConnected && !res.headersSent) {
res.writeHead(502);
res.end('Proxy stream error');
}
downloadTasks.delete(cacheFilePath);
try { fs.unlinkSync(tempPath); } catch { }
reject(err);
});
fileStream.on('finish', () => {
downloadTasks.delete(cacheFilePath);
// 验证并移动文件
try {
const stat = fs.statSync(tempPath);
if (stat.size === contentLength) {
@@ -595,7 +703,7 @@ async function proxyAndCache(
complete: true,
createdAt: Date.now()
});
console.log(`[Proxy] Cache complete: ${cacheFilePath}`);
console.log(`[Proxy] Cache complete: ${cacheFilePath} (${contentLength} bytes)`);
} else {
console.warn(`[Proxy] Incomplete download: ${stat.size}/${contentLength}`);
try { fs.unlinkSync(tempPath); } catch { }
@@ -604,27 +712,16 @@ async function proxyAndCache(
console.error('[Proxy] Failed to finalize cache:', e);
try { fs.unlinkSync(tempPath); } catch { }
}
} else if (!success) {
console.error('[Proxy] Download failed:', error);
// 不删除临时文件,让后续请求可以继续
}
};
fileStream.on('finish', () => cleanup(true));
fileStream.on('error', (err) => cleanup(false, err));
nodeStream.on('error', (err: Error) => {
cleanup(false, err);
if (!res.headersSent) {
res.writeHead(502);
res.end('Proxy stream error');
}
});
resolve();
});
// 客户端断开时不终止下载
res.on('close', () => {
if (!res.writableEnded) {
console.log('[Proxy] Client disconnected, download continues in background');
}
fileStream.on('error', (err) => {
console.error('[Proxy] File write error:', err);
downloadTasks.delete(cacheFilePath);
try { fs.unlinkSync(tempPath); } catch { }
reject(err);
});
});
}
@@ -749,7 +846,7 @@ export function startProxyServer(persistCache: boolean = true) {
console.log(`[Proxy] Persist cache: ${persistCache}`);
// 定期清理临时文件
setInterval(cleanupTempFiles, 30 * 60 * 1000); // 每 30 分钟
setInterval(cleanupTempFiles, 30 * 60 * 1000);
const server = http.createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
@@ -838,11 +935,8 @@ export function startProxyServer(persistCache: boolean = true) {
if (currentAttempt < maxAttempts) {
console.log('[Proxy] Error occurred, refreshing URL from plugin...');
// 清除 URL 缓存
urlCache.delete(cacheKey);
// 获取新 URL
const newUrl = await fetchFreshUrl(source, id, quality);
if (newUrl) {
playUrl = newUrl;
@@ -852,7 +946,6 @@ export function startProxyServer(persistCache: boolean = true) {
}
}
// 发送错误响应
if (!res.headersSent) {
res.writeHead(lastError?.statusCode || 502);
res.end('Proxy Error');
@@ -872,7 +965,7 @@ export function startProxyServer(persistCache: boolean = true) {
server.listen(PORT, '127.0.0.1', () => {
ensureCacheDir();
cleanupTempFiles(); // 启动时清理
cleanupTempFiles();
console.log(`[Proxy] Server running at http://127.0.0.1:${PORT}/music`);
console.log(`[Proxy] Cache dir: ${CACHE_DIR}`);
});

View File

@@ -1,9 +1,10 @@
//@ts-ignore
import { spawn, ChildProcess } from 'child_process';
import { Socket } from 'net';
import { EventEmitter } from 'events';
import path from 'path';
export class MpvController extends EventEmitter {
export class QzpController extends EventEmitter {
private process: ChildProcess | null = null;
private socket: Socket | null = null;
private ipcPath: string;
@@ -16,45 +17,33 @@ export class MpvController extends EventEmitter {
private getIpcPath(): string {
if (process.platform === 'win32') {
return '\\\\.\\pipe\\qzmusic_mpv_socket';
return '\\\\.\\pipe\\qzplayer';
}
return '/tmp/qzmusic_mpv_socket';
}
private getMpvPath(): string {
private getCorePath(): string {
const appRoot = process.env.APP_ROOT || process.cwd();
if (process.platform === 'win32') {
return path.join(appRoot, 'core', 'mpv.exe');
return path.join(appRoot, 'core', 'qzplayer.exe');
}
// Darwin (Mac) or Linux
// For now, assuming global mpv or a specific path in future
return 'mpv';
return "qzplayer"
}
start() {
const mpvPath = this.getMpvPath();
console.log('Starting MPV from:', mpvPath);
const playerPath = this.getCorePath();
console.log('Starting QZPlayer from:', playerPath);
this.process = spawn(mpvPath, [
'--idle',
'--force-window=no',
'--no-media-controls',
`--input-ipc-server=${this.ipcPath}`,
'--no-terminal',
// Network cache for smooth playback (disk caching is handled by proxy)
'--cache=yes',
'--demuxer-max-bytes=50MiB',
'--demuxer-readahead-secs=30'
]);
this.process = spawn(playerPath);
this.process.on('error', (err) => {
console.error('Failed to start MPV:', err);
console.error('Failed to start QZPlayer:', err);
this.emit('error', err);
});
this.process.on('exit', (code, signal) => {
console.log(`MPV exited with code ${code} and signal ${signal}`);
console.log(`QZPlayer exited with code ${code} and signal ${signal}`);
this.emit('exit', { code, signal });
this.socket?.destroy();
});
@@ -64,7 +53,7 @@ export class MpvController extends EventEmitter {
private tryConnect(retries = 10) {
if (retries <= 0) {
console.error('Could not connect to MPV socket after multiple attempts.');
console.error('Could not connect to QZPlayer socket after multiple attempts.');
return;
}
@@ -72,7 +61,7 @@ export class MpvController extends EventEmitter {
this.socket = new Socket();
this.socket.on('connect', () => {
console.log('Connected to MPV IPC socket');
console.log('Connected to QZPlayer IPC socket');
this.emit('ready');
this.send(['observe_property', 1, 'pause']);
@@ -98,7 +87,6 @@ export class MpvController extends EventEmitter {
private handleData(data: Buffer) {
// Determine message boundaries by newline
const raw = data.toString();
// console.log('[MPV RX RAW]', raw.trim()); // Very noisy if enabled
this.messageBuffer += raw;
const messages = this.messageBuffer.split('\n');
@@ -118,19 +106,19 @@ export class MpvController extends EventEmitter {
this.emit('event', json);
}
} catch (e) {
console.error('Failed to parse MPV message:', msg);
console.error('Failed to parse QZPlayer message:', msg);
}
}
}
async send(command: any[]) {
if (!this.socket || this.socket.destroyed) {
console.warn('MPV socket not connected');
console.warn('QZPlayer socket not connected');
return;
}
const payload = JSON.stringify({ command });
console.log('[MPV TX]', payload); // User requested raw communication
console.log('[QZPlayer TX]', payload); // User requested raw communication
this.socket.write(payload + '\n');
}
@@ -165,7 +153,7 @@ export class MpvController extends EventEmitter {
destroy() {
if (this.process) {
console.log('Killing MPV process...');
console.log('Killing QZPlayer process...');
this.process.kill();
this.process = null;
}