From 5ce16eedc6e27560b97ab982408ac697146105e9 Mon Sep 17 00:00:00 2001 From: Sami Alaoui <4ndroidgeek@gmail.com> Date: Wed, 10 Jul 2024 23:11:55 +0100 Subject: [PATCH] fix(YouTube - SponsorBlock): Skip segments when casting (#655) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: oSumAtrIX Co-authored-by: Hoàng Gia Bảo <70064328+YT-Advanced@users.noreply.github.com> Co-authored-by: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> fix(YouTube - SponsorBlock): Skip segments when casting #655 --- .../youtube/patches/VideoInformation.java | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/VideoInformation.java b/app/src/main/java/app/revanced/integrations/youtube/patches/VideoInformation.java index 695af326..21aafa66 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/VideoInformation.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/VideoInformation.java @@ -23,7 +23,9 @@ public final class VideoInformation { private static final String SHORTS_PLAYER_PARAMETERS = "8AEB"; private static WeakReference playerControllerRef; + private static WeakReference mdxPlayerDirectorRef; private static Method seekMethod; + private static Method mdxSeekMethod; @NonNull private static String videoId = ""; @@ -59,6 +61,22 @@ public final class VideoInformation { } } + /** + * 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. * @@ -178,8 +196,32 @@ public final class VideoInformation { } 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;