Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix]: properly clean up MPV on quit, use pid for socket #627

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions src/main/features/core/player/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import console from 'console';
import { rm } from 'fs/promises';
import { pid } from 'node:process';
import { app, ipcMain } from 'electron';
import uniq from 'lodash/uniq';
import MpvAPI from 'node-mpv';
Expand All @@ -18,6 +20,7 @@ declare module 'node-mpv';
// }

let mpvInstance: MpvAPI | null = null;
const socketPath = isWindows() ? `\\\\.\\pipe\\mpvserver-${pid}` : `/tmp/node-mpv-${pid}.sock`;

const NodeMpvErrorCode = {
0: 'Unable to load file or stream',
Expand Down Expand Up @@ -62,7 +65,6 @@ const mpvLog = (
};

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',
Expand All @@ -89,14 +91,12 @@ const createMpv = async (data: {

const params = uniq([...DEFAULT_MPV_PARAMETERS(extraParameters), ...(extraParameters || [])]);

const extra = isDevelopment ? '-dev' : '';

const mpv = new MpvAPI(
{
audio_only: true,
auto_restart: false,
binary: binaryPath || MPV_BINARY_PATH || undefined,
socket: isWindows() ? `\\\\.\\pipe\\mpvserver${extra}` : `/tmp/node-mpv${extra}.sock`,
socket: socketPath,
time_update: 1,
},
params,
Expand Down Expand Up @@ -149,6 +149,16 @@ export const getMpvInstance = () => {
return mpvInstance;
};

const quit = async () => {
const instance = getMpvInstance();
if (instance) {
await instance.quit();
if (!isWindows()) {
await rm(socketPath);
}
}
};

const setAudioPlayerFallback = (isError: boolean) => {
getMainWindow()?.webContents.send('renderer-player-fallback', isError);
};
Expand Down Expand Up @@ -216,7 +226,7 @@ ipcMain.handle(
ipcMain.on('player-quit', async () => {
try {
await getMpvInstance()?.stop();
await getMpvInstance()?.quit();
await quit();
} catch (err: NodeMpvError | any) {
mpvLog({ action: 'Failed to quit mpv' }, err);
} finally {
Expand Down Expand Up @@ -412,19 +422,34 @@ ipcMain.handle('player-get-time', async (): Promise<number | undefined> => {
}
});

app.on('before-quit', async () => {
try {
await getMpvInstance()?.stop();
await getMpvInstance()?.quit();
} catch (err: NodeMpvError | any) {
mpvLog({ action: `Failed to cleanly before-quit` }, err);
}
});
enum MpvState {
STARTED,
IN_PROGRESS,
DONE,
}

app.on('window-all-closed', async () => {
try {
await getMpvInstance()?.quit();
} catch (err: NodeMpvError | any) {
mpvLog({ action: `Failed to cleanly exit` }, err);
let mpvState = MpvState.STARTED;

app.on('before-quit', async (event) => {
switch (mpvState) {
case MpvState.DONE:
return;
case MpvState.IN_PROGRESS:
event.preventDefault();
break;
case MpvState.STARTED: {
try {
mpvState = MpvState.IN_PROGRESS;
event.preventDefault();
await getMpvInstance()?.stop();
await quit();
} catch (err: NodeMpvError | any) {
mpvLog({ action: `Failed to cleanly before-quit` }, err);
} finally {
mpvState = MpvState.DONE;
app.quit();
}
break;
}
}
});
Loading