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

fix(YouTube - SponsorBlock): Skip segments when casting #655

Merged
merged 6 commits into from
Jul 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public final class VideoInformation {
private static final String SHORTS_PLAYER_PARAMETERS = "8AEB";

private static WeakReference<Object> playerControllerRef;
private static WeakReference<Object> mdxPlayerDirectorRef;
private static Method seekMethod;
private static Method mdxSeekMethod;

@NonNull
private static String videoId = "";
Expand Down Expand Up @@ -59,6 +61,23 @@ public static void initialize(@NonNull Object playerController) {
}
}

/**
* Injection point.
*
* @param mdxPlayerDirector MDX player director object (casting mode).
*/
public static void initializeMdx(@NonNull Object mdxPlayerDirector) {
try {
mdxPlayerDirectorRef = new WeakReference<>(Objects.requireNonNull(mdxPlayerDirector));

mdxSeekMethod = mdxPlayerDirector.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE);
mdxSeekMethod.setAccessible(true);

LisoUseInAIKyrios marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception ex) {
Logger.printException(() -> "Failed to initialize", ex);
LisoUseInAIKyrios marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Injection point.
*
Expand Down Expand Up @@ -178,8 +197,22 @@ public static boolean seekTo(final long seekTime) {
}

Logger.printDebug(() -> "Seeking to " + adjustedSeekTime);

// Try calling the seekTo method of the MDX player director first (called when casting to a big screen device).
// The difference has to be at least 1000ms to avoid infinite skip loops (the seekTo Lounge API command only supports seconds).
if (adjustedSeekTime - videoTime >= 1000) {
LisoUseInAIKyrios marked this conversation as resolved.
Show resolved Hide resolved
try {
mdxSeekMethod.invoke(mdxPlayerDirectorRef.get(), adjustedSeekTime);
LisoUseInAIKyrios marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception ex) {
Logger.printInfo(() -> "Failed to seek (MDX)", ex);
}
} else {
Logger.printDebug(() -> "Skipping seekTo for MDX because of a small relative value (" + (seekTime - videoTime) + "ms).");
}

LisoUseInAIKyrios marked this conversation as resolved.
Show resolved Hide resolved
//noinspection DataFlowIssue
return (Boolean) seekMethod.invoke(playerControllerRef.get(), adjustedSeekTime);

} catch (Exception ex) {
Logger.printException(() -> "Failed to seek", ex);
return false;
Expand Down
Loading