|
| 1 | +package app.revanced.extension.music.patches.misc; |
| 2 | + |
| 3 | +import static app.revanced.extension.music.shared.VideoInformation.parameterIsAgeRestricted; |
| 4 | +import static app.revanced.extension.music.shared.VideoInformation.parameterIsSample; |
| 5 | + |
| 6 | +import androidx.annotation.GuardedBy; |
| 7 | +import androidx.annotation.NonNull; |
| 8 | +import androidx.annotation.Nullable; |
| 9 | + |
| 10 | +import org.apache.commons.lang3.BooleanUtils; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.LinkedHashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import app.revanced.extension.music.settings.Settings; |
| 17 | +import app.revanced.extension.music.shared.VideoInformation; |
| 18 | +import app.revanced.extension.shared.utils.Logger; |
| 19 | + |
| 20 | +@SuppressWarnings("unused") |
| 21 | +public class SpoofPlayerParameterPatch { |
| 22 | + /** |
| 23 | + * Used in YouTube Music. |
| 24 | + */ |
| 25 | + private static final boolean SPOOF_PLAYER_PARAMETER = Settings.SPOOF_PLAYER_PARAMETER.get(); |
| 26 | + |
| 27 | + /** |
| 28 | + * Parameter to fix playback issues. |
| 29 | + * Used in YouTube Music Samples. |
| 30 | + */ |
| 31 | + private static final String PLAYER_PARAMETER_SAMPLES = |
| 32 | + "8AEB2AUBogYVAUY4C8W9wrM-FdhjSW4MnCgH44uhkAcI"; |
| 33 | + |
| 34 | + /** |
| 35 | + * Parameter to fix playback issues. |
| 36 | + * Used in YouTube Shorts. |
| 37 | + */ |
| 38 | + private static final String PLAYER_PARAMETER_SHORTS = |
| 39 | + "8AEByAMkuAQ0ogYVAePzwRN3uesV1sPI2x4-GkDYlvqUkAcC"; |
| 40 | + |
| 41 | + /** |
| 42 | + * On app first start, the first video played usually contains a single non-default window setting value |
| 43 | + * and all other subtitle settings for the video are (incorrect) default Samples window settings. |
| 44 | + * For this situation, the Samples settings must be replaced. |
| 45 | + * <p> |
| 46 | + * But some videos use multiple text positions on screen (such as youtu.be/3hW1rMNC89o), |
| 47 | + * and by chance many of the subtitles uses window positions that match a default Samples position. |
| 48 | + * To handle these videos, selectively allowing the Samples specific window settings to 'pass thru' unchanged, |
| 49 | + * but only if the video contains multiple non-default subtitle window positions. |
| 50 | + * <p> |
| 51 | + * Do not enable 'pass thru mode' until this many non default subtitle settings are observed for a single video. |
| 52 | + */ |
| 53 | + private static final int NUMBER_OF_NON_DEFAULT_SUBTITLES_BEFORE_ENABLING_PASSTHRU = 2; |
| 54 | + |
| 55 | + /** |
| 56 | + * The number of non default subtitle settings encountered for the current video. |
| 57 | + */ |
| 58 | + private static int numberOfNonDefaultSettingsObserved; |
| 59 | + |
| 60 | + @GuardedBy("itself") |
| 61 | + private static final Map<String, Boolean> lastVideoIds = new LinkedHashMap<>() { |
| 62 | + private static final int NUMBER_OF_LAST_VIDEO_IDS_TO_TRACK = 5; |
| 63 | + |
| 64 | + @Override |
| 65 | + protected boolean removeEldestEntry(Entry eldest) { |
| 66 | + return size() > NUMBER_OF_LAST_VIDEO_IDS_TO_TRACK; |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + /** |
| 71 | + * Injection point. |
| 72 | + */ |
| 73 | + public static String spoofParameter(@NonNull String videoId, @Nullable String parameter) { |
| 74 | + if (SPOOF_PLAYER_PARAMETER) { |
| 75 | + synchronized (lastVideoIds) { |
| 76 | + Boolean isSamples = parameterIsSample(parameter); |
| 77 | + if (lastVideoIds.put(videoId, isSamples) == null) { |
| 78 | + Logger.printDebug(() -> "New video loaded (videoId: " + videoId + ", isSamples: " + isSamples + ")"); |
| 79 | + } |
| 80 | + } |
| 81 | + return parameterIsAgeRestricted(parameter) |
| 82 | + ? PLAYER_PARAMETER_SHORTS |
| 83 | + : PLAYER_PARAMETER_SAMPLES; |
| 84 | + } |
| 85 | + return parameter; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Injection point. Overrides values passed into SubtitleWindowSettings constructor. |
| 90 | + * |
| 91 | + * @param ap anchor position. A bitmask with 6 bit fields, that appears to indicate the layout position on screen |
| 92 | + * @param ah anchor horizontal. A percentage [0, 100], that appears to be a horizontal text anchor point |
| 93 | + * @param av anchor vertical. A percentage [0, 100], that appears to be a vertical text anchor point |
| 94 | + * @param vs appears to indicate if subtitles exist, and the value is always true. |
| 95 | + * @param sd function is not entirely clear |
| 96 | + */ |
| 97 | + public static int[] fixSubtitleWindowPosition(int ap, int ah, int av, boolean vs, boolean sd) { |
| 98 | + // Videos with custom captions that specify screen positions appear to always have correct screen positions (even with spoofing). |
| 99 | + // But for auto generated and most other captions, the spoof incorrectly gives various default Samples caption settings. |
| 100 | + // Check for these known default Samples captions parameters, and replace with the known correct values. |
| 101 | + // |
| 102 | + // If a regular video uses a custom subtitle setting that match a default Samples setting, |
| 103 | + // then this will incorrectly replace the setting. |
| 104 | + // But, if the video uses multiple subtitles in different screen locations, then detect the non-default values |
| 105 | + // and do not replace any window settings for the video (regardless if they match a Samples default). |
| 106 | + if (SPOOF_PLAYER_PARAMETER && |
| 107 | + numberOfNonDefaultSettingsObserved < NUMBER_OF_NON_DEFAULT_SUBTITLES_BEFORE_ENABLING_PASSTHRU) { |
| 108 | + synchronized (lastVideoIds) { |
| 109 | + String videoId = VideoInformation.getVideoId(); |
| 110 | + Boolean isSample = lastVideoIds.get(videoId); |
| 111 | + if (BooleanUtils.isFalse(isSample)) { |
| 112 | + for (SubtitleWindowReplacementSettings setting : SubtitleWindowReplacementSettings.values()) { |
| 113 | + if (setting.match(ap, ah, av, vs, sd)) { |
| 114 | + return setting.replacementSetting(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + numberOfNonDefaultSettingsObserved++; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return new int[]{ap, ah, av}; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Injection point. |
| 128 | + * <p> |
| 129 | + * Return false to force disable age restricted playback feature flag. |
| 130 | + */ |
| 131 | + public static boolean forceDisableAgeRestrictedPlaybackFeatureFlag(boolean original) { |
| 132 | + if (SPOOF_PLAYER_PARAMETER) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + return original; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Known incorrect default Samples subtitle parameters, and the corresponding correct (non-Samples) values. |
| 140 | + */ |
| 141 | + private enum SubtitleWindowReplacementSettings { |
| 142 | + DEFAULT_SAMPLES_PARAMETERS_1(10, 50, 0, true, false, |
| 143 | + 34, 50, 95), |
| 144 | + DEFAULT_SAMPLES_PARAMETERS_2(9, 20, 0, true, false, |
| 145 | + 34, 50, 90), |
| 146 | + DEFAULT_SAMPLES_PARAMETERS_3(9, 20, 0, true, true, |
| 147 | + 33, 20, 100); |
| 148 | + |
| 149 | + // original values |
| 150 | + final int ap, ah, av; |
| 151 | + final boolean vs, sd; |
| 152 | + |
| 153 | + // replacement int values |
| 154 | + final int[] replacement; |
| 155 | + |
| 156 | + SubtitleWindowReplacementSettings(int ap, int ah, int av, boolean vs, boolean sd, |
| 157 | + int replacementAp, int replacementAh, int replacementAv) { |
| 158 | + this.ap = ap; |
| 159 | + this.ah = ah; |
| 160 | + this.av = av; |
| 161 | + this.vs = vs; |
| 162 | + this.sd = sd; |
| 163 | + this.replacement = new int[]{replacementAp, replacementAh, replacementAv}; |
| 164 | + } |
| 165 | + |
| 166 | + boolean match(int ap, int ah, int av, boolean vs, boolean sd) { |
| 167 | + return this.ap == ap && this.ah == ah && this.av == av && this.vs == vs && this.sd == sd; |
| 168 | + } |
| 169 | + |
| 170 | + int[] replacementSetting() { |
| 171 | + return replacement; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments