fixed bug in waitForConnection when it will not exit loop after successful connection

This commit is contained in:
mykola2312 2024-04-29 11:50:37 +03:00
parent 5d5c47d259
commit b80f8f4b29

View file

@ -84,14 +84,21 @@ public class MPV implements TaskProcess {
private MPVReader reader; private MPVReader reader;
private Thread readerThread; private Thread readerThread;
// around 10 seconds
private static final int MAX_STALLED_RETRIES = 10;
private Float lastPlaybackTime = null; private Float lastPlaybackTime = null;
private int stalledRetries = 0;
private static final Path MPV_SOCKET_PATH = Path.of("/tmp/mptv-mpv.sock"); private static final Path MPV_SOCKET_PATH = Path.of("/tmp/mptv-mpv.sock");
private static final long WAIT_MILLIS = 250; private static final long WAIT_MILLIS = 550;
private static final int WAIT_ATTEMPTS = 5; private static final int WAIT_ATTEMPTS = 5;
private void waitForConnection(Path socketPath) throws MPVSocketFailure { private void waitForConnection(Path socketPath) throws MPVSocketFailure {
for (int i = 0; i < WAIT_ATTEMPTS; i++) { int attempt = 0;
// I have to make it with while loop just to remind me
// exit loop after socket successfuly connected
while ((socket == null || !socket.isConnected()) && attempt++ < WAIT_ATTEMPTS) {
try { try {
Thread.sleep(WAIT_MILLIS); Thread.sleep(WAIT_MILLIS);
@ -130,6 +137,10 @@ public class MPV implements TaskProcess {
}); });
waitForConnection(MPV_SOCKET_PATH); waitForConnection(MPV_SOCKET_PATH);
// reset it here just to be sure
lastPlaybackTime = null;
stalledRetries = 0;
} catch (IOException e) { } catch (IOException e) {
return false; return false;
} catch (MPVSocketFailure e) { } catch (MPVSocketFailure e) {
@ -139,6 +150,8 @@ public class MPV implements TaskProcess {
return process.isAlive(); return process.isAlive();
} }
private static final float PLAYBACK_TIME_DELAY = 2.5f;
private boolean checkPlayback() { private boolean checkPlayback() {
try { try {
// get playback // get playback
@ -157,10 +170,23 @@ public class MPV implements TaskProcess {
// if we have previous playback - compare them, // if we have previous playback - compare them,
// if not changed, then player stuck // if not changed, then player stuck
if (lastPlaybackTime != null) { if (lastPlaybackTime != null) {
boolean playbackChanged = (playbackTime - lastPlaybackTime) > 0.1; // we need to check this because MPV loves
// returning playback time even before video loads
if (playbackTime == 0.0) {
// we're loading video, give it a try
stalledRetries++;
if (stalledRetries > MAX_STALLED_RETRIES) {
stalledRetries = 0;
return false; // we're stuck, therefore die
}
return true;
} else {
boolean playbackChanged = (playbackTime - lastPlaybackTime) > PLAYBACK_TIME_DELAY;
lastPlaybackTime = playbackTime; lastPlaybackTime = playbackTime;
return playbackChanged; return playbackChanged;
}
} else { // just set first playback } else { // just set first playback
lastPlaybackTime = playbackTime; lastPlaybackTime = playbackTime;
return true; return true;
@ -168,6 +194,9 @@ public class MPV implements TaskProcess {
} catch (MPVCommandTimeout e) { } catch (MPVCommandTimeout e) {
logger.warn("mpv ipc timeout bruh"); logger.warn("mpv ipc timeout bruh");
return false; return false;
} catch (MPVSocketFailure e) {
logger.warn("since socket failure we must trigger ProcessService for restart");
return false;
} }
} }
@ -201,7 +230,7 @@ public class MPV implements TaskProcess {
private static final long COMMAND_TIMEOUT = 2000L; private static final long COMMAND_TIMEOUT = 2000L;
public MPVCommandResult executeCommand(MPVCommand command) { public MPVCommandResult executeCommand(MPVCommand command) throws MPVCommandTimeout, MPVSocketFailure {
try { try {
commandRequestId = command.setRequestId(requestIdCounter++); commandRequestId = command.setRequestId(requestIdCounter++);
@ -223,16 +252,12 @@ public class MPV implements TaskProcess {
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new RuntimeException("failed to serialize command", e); throw new RuntimeException("failed to serialize command", e);
} catch (SocketClosedException e) { } catch (SocketClosedException e) {
logger.warn("socket's closed. lets try re-open ipc connection and request again"); logger.warn("socket's closed");
// so, for some reason socket got closed. we're gonna re-open it
// (hopefully) and try executing command again closeConnection();
try { socket = null;
waitForConnection(MPV_SOCKET_PATH);
throw new MPVSocketFailure(e);
return executeCommand(command);
} catch (MPVSocketFailure e1) {
throw new MPVCommandTimeout();
}
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("io exception", e); throw new RuntimeException("io exception", e);
} }