Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
fix(YouTube - SponsorBlock): Skip segments when casting (#655)
Browse files Browse the repository at this point in the history
Co-authored-by: oSumAtrIX <[email protected]>
Co-authored-by: Hoàng Gia Bảo <[email protected]>
Co-authored-by: LisoUseInAIKyrios <[email protected]>
fix(YouTube - SponsorBlock): Skip segments when casting #655
  • Loading branch information
thedroidgeek authored Jul 10, 2024
1 parent 7aec046 commit 5ce16ee
Showing 1 changed file with 44 additions and 2 deletions.
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,22 @@ 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);
} catch (Exception ex) {
Logger.printException(() -> "Failed to initialize MDX", ex);
}
}

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

Logger.printDebug(() -> "Seeking to " + adjustedSeekTime);
//noinspection DataFlowIssue
return (Boolean) seekMethod.invoke(playerControllerRef.get(), adjustedSeekTime);

try {
//noinspection DataFlowIssue
if ((Boolean) seekMethod.invoke(playerControllerRef.get(), adjustedSeekTime)) {
return true;
} // Else the video is loading or changing videos, or video is casting to a different device.
} catch (Exception ex) {
Logger.printInfo(() -> "seekTo method call failed", ex);
}

// Try calling the seekTo method of the MDX player director (called when casting).
// The difference has to be a different second mark in order to avoid infinite skip loops
// as the Lounge API only supports seconds.
if ((adjustedSeekTime / 1000) == (videoTime / 1000)) {
Logger.printDebug(() -> "Skipping seekTo for MDX because seek time is too small ("
+ (adjustedSeekTime - videoTime) + "ms)");
return false;
}
try {
//noinspection DataFlowIssue
return (Boolean) mdxSeekMethod.invoke(mdxPlayerDirectorRef.get(), adjustedSeekTime);
} catch (Exception ex) {
Logger.printInfo(() -> "seekTo (MDX) method call failed", ex);
return false;
}

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

0 comments on commit 5ce16ee

Please sign in to comment.