diff --git a/app/src/main/java/app/revanced/integrations/patches/VideoInformation.java b/app/src/main/java/app/revanced/integrations/patches/VideoInformation.java index 21299163..ccdde90d 100644 --- a/app/src/main/java/app/revanced/integrations/patches/VideoInformation.java +++ b/app/src/main/java/app/revanced/integrations/patches/VideoInformation.java @@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import java.lang.ref.WeakReference; import java.lang.reflect.Method; +import app.revanced.integrations.patches.playback.speed.RememberPlaybackSpeedPatch; import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; @@ -12,6 +13,7 @@ import app.revanced.integrations.utils.ReVancedUtils; * Hooking class for the current playing video. */ public final class VideoInformation { + private static final float DEFAULT_YOUTUBE_PLAYBACK_SPEED = 1.0f; private static final String SEEK_METHOD_NAME = "seekTo"; private static WeakReference playerController; @@ -21,6 +23,10 @@ public final class VideoInformation { private static String videoId = ""; private static long videoLength = 0; private static volatile long videoTime = -1; // must be volatile. Value is set off main thread from high precision patch hook + /** + * The current playback speed + */ + private static float playbackSpeed = DEFAULT_YOUTUBE_PLAYBACK_SPEED; /** * Injection point. @@ -50,6 +56,30 @@ public final class VideoInformation { if (!videoId.equals(newlyLoadedVideoId)) { LogHelper.printDebug(() -> "New video id: " + newlyLoadedVideoId); videoId = newlyLoadedVideoId; + playbackSpeed = DEFAULT_YOUTUBE_PLAYBACK_SPEED; + } + } + + /** + * Injection point. + * Called when user selects a playback speed. + * + * @param userSelectedPlaybackSpeed The playback speed the user selected + */ + public static void userSelectedPlaybackSpeed(float userSelectedPlaybackSpeed) { + LogHelper.printDebug(() -> "User selected playback speed: " + userSelectedPlaybackSpeed); + playbackSpeed = userSelectedPlaybackSpeed; + } + + /** + * Overrides the current playback speed. + * + * Used exclusively by {@link RememberPlaybackSpeedPatch} + */ + public static void overridePlaybackSpeed(float speedOverride) { + if (playbackSpeed != speedOverride) { + LogHelper.printDebug(() -> "Overriding playback speed to: " + speedOverride); + playbackSpeed = speedOverride; } } @@ -115,6 +145,13 @@ public final class VideoInformation { return videoId; } + /** + * @return The current playback speed. + */ + public static float getCurrentPlaybackSpeed() { + return playbackSpeed; + } + /** * Length of the current video playing. * Includes Shorts playback. diff --git a/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java b/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java index 8b4b2114..babbc291 100644 --- a/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java +++ b/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java @@ -3,34 +3,15 @@ package app.revanced.integrations.patches.playback.speed; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import app.revanced.integrations.patches.VideoInformation; import app.revanced.integrations.settings.SettingsEnum; -import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; public final class RememberPlaybackSpeedPatch { - /** - * The current playback speed - */ - private static float currentPlaybackSpeed = getLastRememberedPlaybackSpeed(); - - private final static float DEFAULT_PLAYBACK_SPEED = (float) SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.getDefaultValue(); - @Nullable private static String currentVideoId; - private static float getLastRememberedPlaybackSpeed() { - return SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.getFloat(); - } - - private static void rememberPlaybackSpeed() { - SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.saveValue(currentPlaybackSpeed); - } - - private static boolean rememberLastSelectedPlaybackSpeed() { - return SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED.getBoolean(); - } - /** * Injection point. * Called when a new video loads. @@ -39,41 +20,32 @@ public final class RememberPlaybackSpeedPatch { if (videoId.equals(currentVideoId)) { return; } - currentVideoId = videoId; - currentPlaybackSpeed = getLastRememberedPlaybackSpeed(); + VideoInformation.overridePlaybackSpeed(SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.getFloat()); } /** * Injection point. - * Called when a playback speed is selected. + * Called when user selects a playback speed. * - * @param playbackSpeed The playback speed to set. + * @param playbackSpeed The playback speed the user selected */ - public static void setPlaybackSpeed(final float playbackSpeed) { - LogHelper.printDebug(() -> "Playback speed changed to: " + playbackSpeed); - - currentPlaybackSpeed = playbackSpeed; - - if (rememberLastSelectedPlaybackSpeed()) { - rememberPlaybackSpeed(); + public static void userSelectedPlaybackSpeed(float playbackSpeed) { + if (SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED.getBoolean()) { + SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.saveValue(playbackSpeed); + // TODO: extract these strings into localized file ReVancedUtils.showToastLong("Remembering playback speed: " + playbackSpeed + "x"); - } else { - if (getLastRememberedPlaybackSpeed() == DEFAULT_PLAYBACK_SPEED) return; - + } else if (playbackSpeed != (float) SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.getDefaultValue()) { ReVancedUtils.showToastLong("Applying playback speed: " + playbackSpeed + "x"); } } /** * Injection point. - * Called when playback first starts, - * and also called immediately after the user selects a new video speed. - * - * @return The currently set playback speed. + * Overrides the video speed. Called after video loads, and immediately after user selects a different playback speed */ - public static float getCurrentPlaybackSpeed() { - return currentPlaybackSpeed; + public static float getPlaybackSpeedOverride() { + return VideoInformation.getCurrentPlaybackSpeed(); } } diff --git a/app/src/main/java/app/revanced/integrations/sponsorblock/SegmentPlaybackController.java b/app/src/main/java/app/revanced/integrations/sponsorblock/SegmentPlaybackController.java index 5435bfe0..d2cea53d 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/SegmentPlaybackController.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/SegmentPlaybackController.java @@ -14,7 +14,6 @@ import java.util.Arrays; import java.util.Objects; import app.revanced.integrations.patches.VideoInformation; -import app.revanced.integrations.patches.playback.speed.RememberPlaybackSpeedPatch; import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.shared.PlayerType; import app.revanced.integrations.sponsorblock.objects.CategoryBehaviour; @@ -189,7 +188,7 @@ public class SegmentPlaybackController { // to debug the timing logic, set this to a very large value (5000 or more) // then try manually seeking just playback reaches a skip/hide of different segments final long lookAheadMilliseconds = 1500; // must be larger than the average time between calls to this method - final float playbackSpeed = RememberPlaybackSpeedPatch.getCurrentPlaybackSpeed(); + final float playbackSpeed = VideoInformation.getCurrentPlaybackSpeed(); final long startTimerLookAheadThreshold = millis + (long)(playbackSpeed * lookAheadMilliseconds); SponsorSegment foundCurrentSegment = null;