refactor(youtube/video-information): include video speed (#345)

This commit is contained in:
LisoUseInAIKyrios 2023-04-04 11:00:16 +04:00 committed by oSumAtrIX
parent 22e453706d
commit d4de3f6819
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
3 changed files with 50 additions and 42 deletions

View File

@ -5,6 +5,7 @@ import androidx.annotation.NonNull;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import app.revanced.integrations.patches.playback.speed.RememberPlaybackSpeedPatch;
import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils; import app.revanced.integrations.utils.ReVancedUtils;
@ -12,6 +13,7 @@ import app.revanced.integrations.utils.ReVancedUtils;
* Hooking class for the current playing video. * Hooking class for the current playing video.
*/ */
public final class VideoInformation { public final class VideoInformation {
private static final float DEFAULT_YOUTUBE_PLAYBACK_SPEED = 1.0f;
private static final String SEEK_METHOD_NAME = "seekTo"; private static final String SEEK_METHOD_NAME = "seekTo";
private static WeakReference<Object> playerController; private static WeakReference<Object> playerController;
@ -21,6 +23,10 @@ public final class VideoInformation {
private static String videoId = ""; private static String videoId = "";
private static long videoLength = 0; 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 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. * Injection point.
@ -50,6 +56,30 @@ public final class VideoInformation {
if (!videoId.equals(newlyLoadedVideoId)) { if (!videoId.equals(newlyLoadedVideoId)) {
LogHelper.printDebug(() -> "New video id: " + newlyLoadedVideoId); LogHelper.printDebug(() -> "New video id: " + newlyLoadedVideoId);
videoId = 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.
*
* <b> Used exclusively by {@link RememberPlaybackSpeedPatch} </b>
*/
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 videoId;
} }
/**
* @return The current playback speed.
*/
public static float getCurrentPlaybackSpeed() {
return playbackSpeed;
}
/** /**
* Length of the current video playing. * Length of the current video playing.
* Includes Shorts playback. * Includes Shorts playback.

View File

@ -3,34 +3,15 @@ package app.revanced.integrations.patches.playback.speed;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import app.revanced.integrations.patches.VideoInformation;
import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils; import app.revanced.integrations.utils.ReVancedUtils;
public final class RememberPlaybackSpeedPatch { 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 @Nullable
private static String currentVideoId; 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. * Injection point.
* Called when a new video loads. * Called when a new video loads.
@ -39,41 +20,32 @@ public final class RememberPlaybackSpeedPatch {
if (videoId.equals(currentVideoId)) { if (videoId.equals(currentVideoId)) {
return; return;
} }
currentVideoId = videoId; currentVideoId = videoId;
currentPlaybackSpeed = getLastRememberedPlaybackSpeed(); VideoInformation.overridePlaybackSpeed(SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.getFloat());
} }
/** /**
* Injection point. * 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) { public static void userSelectedPlaybackSpeed(float playbackSpeed) {
LogHelper.printDebug(() -> "Playback speed changed to: " + playbackSpeed); if (SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED.getBoolean()) {
SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.saveValue(playbackSpeed);
currentPlaybackSpeed = playbackSpeed;
if (rememberLastSelectedPlaybackSpeed()) {
rememberPlaybackSpeed();
// TODO: extract these strings into localized file
ReVancedUtils.showToastLong("Remembering playback speed: " + playbackSpeed + "x"); ReVancedUtils.showToastLong("Remembering playback speed: " + playbackSpeed + "x");
} else { } else if (playbackSpeed != (float) SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.getDefaultValue()) {
if (getLastRememberedPlaybackSpeed() == DEFAULT_PLAYBACK_SPEED) return;
ReVancedUtils.showToastLong("Applying playback speed: " + playbackSpeed + "x"); ReVancedUtils.showToastLong("Applying playback speed: " + playbackSpeed + "x");
} }
} }
/** /**
* Injection point. * Injection point.
* Called when playback first starts, * Overrides the video speed. Called after video loads, and immediately after user selects a different playback speed
* and also called immediately after the user selects a new video speed.
*
* @return The currently set playback speed.
*/ */
public static float getCurrentPlaybackSpeed() { public static float getPlaybackSpeedOverride() {
return currentPlaybackSpeed; return VideoInformation.getCurrentPlaybackSpeed();
} }
} }

View File

@ -14,7 +14,6 @@ import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import app.revanced.integrations.patches.VideoInformation; import app.revanced.integrations.patches.VideoInformation;
import app.revanced.integrations.patches.playback.speed.RememberPlaybackSpeedPatch;
import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.shared.PlayerType; import app.revanced.integrations.shared.PlayerType;
import app.revanced.integrations.sponsorblock.objects.CategoryBehaviour; 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) // 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 // 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 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); final long startTimerLookAheadThreshold = millis + (long)(playbackSpeed * lookAheadMilliseconds);
SponsorSegment foundCurrentSegment = null; SponsorSegment foundCurrentSegment = null;