fix(youtube/remember-video-quality): fix default video quality/speed being applied when resuming app (#392)

This commit is contained in:
LisoUseInAIKyrios 2023-05-09 19:50:56 +04:00 committed by GitHub
parent 9688934610
commit f0a575f09e
4 changed files with 19 additions and 46 deletions

View File

@ -4,6 +4,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 java.util.Objects;
import app.revanced.integrations.patches.playback.speed.RememberPlaybackSpeedPatch; import app.revanced.integrations.patches.playback.speed.RememberPlaybackSpeedPatch;
import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.LogHelper;
@ -16,7 +17,7 @@ public final class VideoInformation {
private static final float DEFAULT_YOUTUBE_PLAYBACK_SPEED = 1.0f; 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> playerControllerRef;
private static Method seekMethod; private static Method seekMethod;
@NonNull @NonNull
@ -30,17 +31,17 @@ public final class VideoInformation {
/** /**
* Injection point. * Injection point.
* Sets a reference to the YouTube playback controller.
* *
* @param thisRef Reference to the player controller object. * @param playerController player controller object.
*/ */
public static void playerController_onCreateHook(final Object thisRef) { public static void initialize(@NonNull Object playerController) {
playerController = new WeakReference<>(thisRef);
videoLength = 0;
videoTime = -1;
try { try {
seekMethod = thisRef.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE); playerControllerRef = new WeakReference<>(Objects.requireNonNull(playerController));
videoTime = -1;
videoLength = 0;
playbackSpeed = DEFAULT_YOUTUBE_PLAYBACK_SPEED;
seekMethod = playerController.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE);
seekMethod.setAccessible(true); seekMethod.setAccessible(true);
} catch (Exception ex) { } catch (Exception ex) {
LogHelper.printException(() -> "Failed to initialize", ex); LogHelper.printException(() -> "Failed to initialize", ex);
@ -56,7 +57,6 @@ 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;
} }
} }
@ -124,7 +124,7 @@ public final class VideoInformation {
try { try {
LogHelper.printDebug(() -> "Seeking to " + millisecond); LogHelper.printDebug(() -> "Seeking to " + millisecond);
return (Boolean) seekMethod.invoke(playerController.get(), millisecond); return (Boolean) seekMethod.invoke(playerControllerRef.get(), millisecond);
} catch (Exception ex) { } catch (Exception ex) {
LogHelper.printException(() -> "Failed to seek", ex); LogHelper.printException(() -> "Failed to seek", ex);
return false; return false;

View File

@ -2,7 +2,6 @@ package app.revanced.integrations.patches.playback.quality;
import static app.revanced.integrations.utils.ReVancedUtils.NetworkType; import static app.revanced.integrations.utils.ReVancedUtils.NetworkType;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -20,8 +19,6 @@ public class RememberVideoQualityPatch {
private static final SettingsEnum mobileQualitySetting = SettingsEnum.VIDEO_QUALITY_DEFAULT_MOBILE; private static final SettingsEnum mobileQualitySetting = SettingsEnum.VIDEO_QUALITY_DEFAULT_MOBILE;
private static boolean qualityNeedsUpdating; private static boolean qualityNeedsUpdating;
@Nullable
private static String currentVideoId;
/** /**
* If the user selected a new quality from the flyout menu, * If the user selected a new quality from the flyout menu,
@ -91,7 +88,7 @@ public class RememberVideoQualityPatch {
} }
} }
} }
LogHelper.printDebug(() -> "VideoId: " + currentVideoId + " videoQualities: " + videoQualities); LogHelper.printDebug(() -> "videoQualities: " + videoQualities);
} }
if (userChangedDefaultQuality) { if (userChangedDefaultQuality) {
@ -144,25 +141,9 @@ public class RememberVideoQualityPatch {
/** /**
* Injection point. * Injection point.
*/ */
public static void newVideoStarted(@NonNull String videoId) { public static void newVideoStarted(Object ignoredPlayerController) {
// The same videoId can be passed in multiple times for a single video playback. LogHelper.printDebug(() -> "newVideoStarted");
// Such as closing and opening the app, and sometimes when turning off/on the device screen.
//
// Known limitation, if:
// 1. a default video quality exists, and remember quality is turned off
// 2. user opens a video
// 3. user changes the video quality
// 4. user turns off then on the device screen (or does anything else that triggers the video id hook)
// result: the video quality of the current video will revert back to the saved default
//
// qualityNeedsUpdating could be set only when the videoId changes
// but then if the user closes and re-opens the same video the default video quality will not be applied.
LogHelper.printDebug(() -> "newVideoStarted: " + videoId);
qualityNeedsUpdating = true; qualityNeedsUpdating = true;
if (!videoId.equals(currentVideoId)) {
currentVideoId = videoId;
videoQualities = null; videoQualities = null;
} }
}
} }

View File

@ -2,11 +2,9 @@ package app.revanced.integrations.patches.playback.speed;
import android.preference.ListPreference; import android.preference.ListPreference;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import app.revanced.integrations.patches.VideoInformation; 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 {
@ -16,18 +14,12 @@ public final class RememberPlaybackSpeedPatch {
*/ */
private static String[] preferenceListEntries, preferenceListEntryValues; private static String[] preferenceListEntries, preferenceListEntryValues;
@Nullable
private static String currentVideoId;
/** /**
* Injection point. * Injection point.
* Called when a new video loads.
*/ */
public static void newVideoLoaded(@NonNull String videoId) { public static void newVideoStarted(Object ignoredPlayerController) {
if (videoId.equals(currentVideoId)) { LogHelper.printDebug(() -> "newVideoStarted");
return;
}
currentVideoId = videoId;
VideoInformation.overridePlaybackSpeed(SettingsEnum.PLAYBACK_SPEED_DEFAULT.getFloat()); VideoInformation.overridePlaybackSpeed(SettingsEnum.PLAYBACK_SPEED_DEFAULT.getFloat());
} }

View File

@ -177,7 +177,7 @@ public class SegmentPlaybackController {
* Injection point. * Injection point.
* Initializes SponsorBlock when the video player starts playing a new video. * Initializes SponsorBlock when the video player starts playing a new video.
*/ */
public static void initialize(Object _o) { public static void initialize(Object ignoredPlayerController) {
try { try {
ReVancedUtils.verifyOnMainThread(); ReVancedUtils.verifyOnMainThread();
SponsorBlockSettings.initialize(); SponsorBlockSettings.initialize();