mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-01 18:33:33 +00:00
Add logger functions and move player functions to feature
This commit is contained in:
parent
adf5fc348a
commit
69f82a9427
3 changed files with 197 additions and 143 deletions
|
|
@ -1,7 +1,11 @@
|
|||
import console from 'console';
|
||||
import { ipcMain } from 'electron';
|
||||
import { getMpvInstance } from '../../../main';
|
||||
import { app, ipcMain } from 'electron';
|
||||
import uniq from 'lodash/uniq';
|
||||
import MpvAPI from 'node-mpv';
|
||||
import { getMainWindow, sendToastToRenderer } from '../../../main';
|
||||
import { PlayerData } from '/@/renderer/store';
|
||||
import { isWindows } from '../../../utils';
|
||||
import { store } from '../settings';
|
||||
|
||||
declare module 'node-mpv';
|
||||
|
||||
|
|
@ -13,6 +17,150 @@ declare module 'node-mpv';
|
|||
// });
|
||||
// }
|
||||
|
||||
let mpvInstance: MpvAPI | null = null;
|
||||
|
||||
const MPV_BINARY_PATH = store.get('mpv_path') as string | undefined;
|
||||
const isDevelopment = process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';
|
||||
|
||||
const prefetchPlaylistParams = [
|
||||
'--prefetch-playlist=no',
|
||||
'--prefetch-playlist=yes',
|
||||
'--prefetch-playlist',
|
||||
];
|
||||
|
||||
const DEFAULT_MPV_PARAMETERS = (extraParameters?: string[]) => {
|
||||
const parameters = ['--idle=yes', '--no-config', '--load-scripts=no'];
|
||||
|
||||
if (!extraParameters?.some((param) => prefetchPlaylistParams.includes(param))) {
|
||||
parameters.push('--prefetch-playlist=yes');
|
||||
}
|
||||
|
||||
return parameters;
|
||||
};
|
||||
|
||||
const createMpv = async (data: {
|
||||
extraParameters?: string[];
|
||||
properties?: Record<string, any>;
|
||||
}): Promise<MpvAPI> => {
|
||||
const { extraParameters, properties } = data;
|
||||
|
||||
const params = uniq([...DEFAULT_MPV_PARAMETERS(extraParameters), ...(extraParameters || [])]);
|
||||
console.log('Setting mpv params: ', params);
|
||||
|
||||
const extra = isDevelopment ? '-dev' : '';
|
||||
|
||||
const mpv = new MpvAPI(
|
||||
{
|
||||
audio_only: true,
|
||||
auto_restart: false,
|
||||
binary: MPV_BINARY_PATH || undefined,
|
||||
socket: isWindows() ? `\\\\.\\pipe\\mpvserver${extra}` : `/tmp/node-mpv${extra}.sock`,
|
||||
time_update: 1,
|
||||
},
|
||||
params,
|
||||
);
|
||||
|
||||
try {
|
||||
await mpv.start();
|
||||
} catch (error: { message: string; stack: any } | any) {
|
||||
console.log('MPV failed to start', error);
|
||||
} finally {
|
||||
console.log('Setting MPV properties: ', properties);
|
||||
await mpv.setMultipleProperties(properties || {});
|
||||
}
|
||||
|
||||
mpv.on('status', (status, ...rest) => {
|
||||
console.log('MPV Event: status', status.property, status.value, rest);
|
||||
if (status.property === 'playlist-pos') {
|
||||
if (status.value === -1) {
|
||||
mpv?.stop();
|
||||
}
|
||||
|
||||
if (status.value !== 0) {
|
||||
getMainWindow()?.webContents.send('renderer-player-auto-next');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is playing
|
||||
mpv.on('resumed', () => {
|
||||
console.log('MPV Event: resumed');
|
||||
getMainWindow()?.webContents.send('renderer-player-play');
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is stopped
|
||||
mpv.on('stopped', () => {
|
||||
console.log('MPV Event: stopped');
|
||||
getMainWindow()?.webContents.send('renderer-player-stop');
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is paused
|
||||
mpv.on('paused', () => {
|
||||
console.log('MPV Event: paused');
|
||||
getMainWindow()?.webContents.send('renderer-player-pause');
|
||||
});
|
||||
|
||||
// Event output every interval set by time_update, used to update the current time
|
||||
mpv.on('timeposition', (time: number) => {
|
||||
getMainWindow()?.webContents.send('renderer-player-current-time', time);
|
||||
});
|
||||
|
||||
mpv.on('quit', () => {
|
||||
console.log('MPV Event: quit');
|
||||
});
|
||||
|
||||
return mpv;
|
||||
};
|
||||
|
||||
export const getMpvInstance = () => {
|
||||
return mpvInstance;
|
||||
};
|
||||
|
||||
ipcMain.on('player-set-properties', async (_event, data: Record<string, any>) => {
|
||||
if (data.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.length === 1) {
|
||||
getMpvInstance()?.setProperty(Object.keys(data)[0], Object.values(data)[0]);
|
||||
} else {
|
||||
getMpvInstance()?.setMultipleProperties(data);
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on(
|
||||
'player-restart',
|
||||
async (_event, data: { extraParameters?: string[]; properties?: Record<string, any> }) => {
|
||||
console.log('Initializing MPV with data: ', data);
|
||||
mpvInstance?.quit();
|
||||
try {
|
||||
mpvInstance = await createMpv(data);
|
||||
} catch (err) {
|
||||
console.log('init error', err);
|
||||
sendToastToRenderer({ message: 'Initialization error', type: 'error' });
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
ipcMain.handle(
|
||||
'player-initialize',
|
||||
async (_event, data: { extraParameters?: string[]; properties?: Record<string, any> }) => {
|
||||
console.log('Initializing MPV with data: ', data);
|
||||
try {
|
||||
mpvInstance = await createMpv(data);
|
||||
} catch (err) {
|
||||
console.log('init error', err);
|
||||
sendToastToRenderer({ message: 'Initialization error', type: 'error' });
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
ipcMain.on('player-quit', async () => {
|
||||
mpvInstance?.stop();
|
||||
mpvInstance?.quit();
|
||||
mpvInstance = null;
|
||||
});
|
||||
|
||||
ipcMain.handle('player-is-running', async () => {
|
||||
return getMpvInstance()?.isRunning();
|
||||
});
|
||||
|
|
@ -203,3 +351,12 @@ ipcMain.on('player-mute', async (_event, mute: boolean) => {
|
|||
ipcMain.handle('player-get-time', async (): Promise<number | undefined> => {
|
||||
return getMpvInstance()?.getTimePosition();
|
||||
});
|
||||
|
||||
app.on('before-quit', () => {
|
||||
getMpvInstance()?.stop();
|
||||
getMpvInstance()?.quit();
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
getMpvInstance()?.quit();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue