feat(youtube): move video settings to Video settings category (#2010)

Co-authored-by: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com>
Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
johnconner122
2023-05-13 01:32:41 +02:00
committed by GitHub
co-authored by LisoUseInAIKyrios oSumAtrIX
parent 79e36e2b74
commit 1d707b07b4
40 changed files with 87 additions and 115 deletions
@@ -0,0 +1,24 @@
package app.revanced.patches.youtube.video.hdrbrightness.annotations
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package(
"com.google.android.youtube", arrayOf(
"17.49.37",
"18.03.36",
"18.03.42",
"18.04.35",
"18.04.41",
"18.05.32",
"18.05.35",
"18.05.40",
"18.08.37",
"18.15.40",
"18.16.37"
)
)]
)
@Target(AnnotationTarget.CLASS)
internal annotation class HDRBrightnessCompatibility
@@ -0,0 +1,10 @@
package app.revanced.patches.youtube.video.hdrbrightness.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object HDRBrightnessFingerprint : MethodFingerprint(
"V",
opcodes = listOf(Opcode.CMPL_FLOAT),
strings = listOf("c.SettingNotFound;", "screen_brightness", "android.mediaview"),
)
@@ -0,0 +1,64 @@
package app.revanced.patches.youtube.video.hdrbrightness.patch
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.youtube.video.hdrbrightness.annotations.HDRBrightnessCompatibility
import app.revanced.patches.youtube.video.hdrbrightness.fingerprints.HDRBrightnessFingerprint
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.SwitchPreference
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction
import org.jf.dexlib2.iface.reference.FieldReference
@Patch
@Name("hdr-auto-brightness")
@Description("Makes the brightness of HDR videos follow the system default.")
@HDRBrightnessCompatibility
@Version("0.0.2")
@DependsOn([IntegrationsPatch::class, SettingsPatch::class])
class HDRBrightnessPatch : BytecodePatch(
listOf(HDRBrightnessFingerprint)
) {
override fun execute(context: BytecodeContext): PatchResult {
SettingsPatch.PreferenceScreen.VIDEO.addPreferences(
SwitchPreference(
"revanced_pref_hdr_autobrightness",
StringResource("revanced_hdr_autobrightness_enabled_title", "Enable auto HDR brightness"),
true,
StringResource("revanced_hdr_autobrightness_summary_on", "Auto HDR brightness is enabled"),
StringResource("revanced_hdr_autobrightness_summary_off", "Auto HDR brightness is disabled")
)
)
val method = HDRBrightnessFingerprint.result!!.mutableMethod
method.implementation!!.instructions.filter { instruction ->
val fieldReference = (instruction as? ReferenceInstruction)?.reference as? FieldReference
fieldReference?.let { it.name == "screenBrightness" } == true
}.forEach { instruction ->
val brightnessRegisterIndex = method.implementation!!.instructions.indexOf(instruction)
val register = (instruction as TwoRegisterInstruction).registerA
val insertIndex = brightnessRegisterIndex + 1
method.addInstructions(
insertIndex,
"""
invoke-static {v$register}, Lapp/revanced/integrations/patches/HDRAutoBrightnessPatch;->getHDRBrightness(F)F
move-result v$register
"""
)
}
return PatchResultSuccess()
}
}
@@ -0,0 +1,10 @@
package app.revanced.patches.youtube.video.information.annotation
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package("com.google.android.youtube", arrayOf("18.08.37", "18.15.40", "18.16.37"))]
)
@Target(AnnotationTarget.CLASS)
internal annotation class VideoInformationCompatibility
@@ -0,0 +1,9 @@
package app.revanced.patches.youtube.video.information.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object CreateVideoPlayerSeekbarFingerprint : MethodFingerprint(
"V",
strings = listOf("timed_markers_width")
)
@@ -0,0 +1,14 @@
package app.revanced.patches.youtube.video.information.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object OnPlaybackSpeedItemClickFingerprint : MethodFingerprint(
customFingerprint = { it.name == "onItemClick" },
opcodes = listOf(
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_VIRTUAL,
Opcode.INVOKE_VIRTUAL,
Opcode.RETURN_VOID
)
)
@@ -0,0 +1,11 @@
package app.revanced.patches.youtube.video.information.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object PlayerControllerSetTimeReferenceFingerprint : MethodFingerprint(
opcodes = listOf(Opcode.INVOKE_DIRECT_RANGE, Opcode.IGET_OBJECT),
strings = listOf("Media progress reported outside media playback: ")
)
@@ -0,0 +1,10 @@
package app.revanced.patches.youtube.video.information.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object PlayerInitFingerprint : MethodFingerprint(
strings = listOf(
"playVideo called on player response with no videoStreamingData."
),
)
@@ -0,0 +1,8 @@
package app.revanced.patches.youtube.video.information.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object SeekFingerprint : MethodFingerprint(
strings = listOf("Attempting to seek during an ad")
)
@@ -0,0 +1,23 @@
package app.revanced.patches.youtube.video.information.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object VideoLengthFingerprint : MethodFingerprint(
opcodes = listOf(
Opcode.MOVE_RESULT_WIDE,
Opcode.CMP_LONG,
Opcode.IF_LEZ,
Opcode.IGET_OBJECT,
Opcode.CHECK_CAST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_WIDE,
Opcode.GOTO,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_WIDE,
Opcode.CONST_4,
Opcode.INVOKE_VIRTUAL
)
)
@@ -0,0 +1,8 @@
package app.revanced.patches.youtube.video.information.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object VideoTimeFingerprint : MethodFingerprint(
strings = listOf("MedialibPlayerTimeInfo{currentPositionMillis=")
)
@@ -0,0 +1,245 @@
package app.revanced.patches.youtube.video.information.patch
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.data.toMethodWalker
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.video.information.annotation.VideoInformationCompatibility
import app.revanced.patches.youtube.video.information.fingerprints.*
import app.revanced.patches.youtube.video.speed.remember.patch.RememberPlaybackSpeedPatch
import app.revanced.patches.youtube.video.videoid.patch.VideoIdPatch
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.builder.BuilderInstruction
import org.jf.dexlib2.builder.MutableMethodImplementation
import org.jf.dexlib2.iface.instruction.FiveRegisterInstruction
import org.jf.dexlib2.iface.instruction.Instruction
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.immutable.ImmutableMethod
import org.jf.dexlib2.immutable.ImmutableMethodParameter
import org.jf.dexlib2.util.MethodUtil
@Name("video-information")
@Description("Hooks YouTube to get information about the current playing video.")
@VideoInformationCompatibility
@Version("0.0.1")
@DependsOn([IntegrationsPatch::class, VideoIdPatch::class])
class VideoInformationPatch : BytecodePatch(
listOf(
PlayerInitFingerprint,
CreateVideoPlayerSeekbarFingerprint,
PlayerControllerSetTimeReferenceFingerprint,
VideoTimeFingerprint,
OnPlaybackSpeedItemClickFingerprint,
)
) {
override fun execute(context: BytecodeContext): PatchResult {
with(PlayerInitFingerprint.result!!) {
playerInitMethod = mutableClass.methods.first { MethodUtil.isConstructor(it) }
// hook the player controller for use through integrations
onCreateHook(INTEGRATIONS_CLASS_DESCRIPTOR, "initialize")
// seek method
val seekFingerprintResultMethod = SeekFingerprint.also { it.resolve(context, classDef) }.result!!.method
// create helper method
val seekHelperMethod = ImmutableMethod(
seekFingerprintResultMethod.definingClass,
"seekTo",
listOf(ImmutableMethodParameter("J", null, "time")),
"Z",
AccessFlags.PUBLIC or AccessFlags.FINAL,
null, null,
MutableMethodImplementation(4)
).toMutable()
// get enum type for the seek helper method
val seekSourceEnumType = seekFingerprintResultMethod.parameterTypes[1].toString()
// insert helper method instructions
seekHelperMethod.addInstructions(
0,
"""
sget-object v0, $seekSourceEnumType->a:$seekSourceEnumType
invoke-virtual {p0, p1, p2, v0}, ${seekFingerprintResultMethod.definingClass}->${seekFingerprintResultMethod.name}(J$seekSourceEnumType)Z
move-result p1
return p1
"""
)
// add the seekTo method to the class for the integrations to call
mutableClass.methods.add(seekHelperMethod)
}
with(CreateVideoPlayerSeekbarFingerprint.result!!) {
val videoLengthMethodResult = VideoLengthFingerprint.also { it.resolve(context, classDef) }.result!!
with(videoLengthMethodResult.mutableMethod) {
val videoLengthRegisterIndex = videoLengthMethodResult.scanResult.patternScanResult!!.endIndex - 2
val videoLengthRegister = instruction<OneRegisterInstruction>(videoLengthRegisterIndex).registerA
val dummyRegisterForLong = videoLengthRegister + 1 // required for long values since they are wide
addInstruction(
videoLengthMethodResult.scanResult.patternScanResult!!.endIndex,
"invoke-static {v$videoLengthRegister, v$dummyRegisterForLong}, $INTEGRATIONS_CLASS_DESCRIPTOR->setVideoLength(J)V"
)
}
}
/*
* Inject call for video id
*/
val videoIdMethodDescriptor = "$INTEGRATIONS_CLASS_DESCRIPTOR->setVideoId(Ljava/lang/String;)V"
VideoIdPatch.injectCall(videoIdMethodDescriptor)
VideoIdPatch.injectCallBackgroundPlay(videoIdMethodDescriptor)
/*
* Set the video time method
*/
with(PlayerControllerSetTimeReferenceFingerprint.result!!) {
timeMethod = context.toMethodWalker(method)
.nextMethod(scanResult.patternScanResult!!.startIndex, true)
.getMethod() as MutableMethod
}
/*
* Set the high precision video time method
*/
highPrecisionTimeMethod =
(object : MethodFingerprint("V", null, listOf("J", "J", "J", "J", "I", "L"), null) {}).also {
it.resolve(context, VideoTimeFingerprint.result!!.classDef)
}.result!!.mutableMethod
/*
* Hook the methods which set the time
*/
highPrecisionTimeHook(INTEGRATIONS_CLASS_DESCRIPTOR, "setVideoTimeHighPrecision")
/*
* Hook the user playback speed selection
*/
OnPlaybackSpeedItemClickFingerprint.result?.apply {
speedSelectionInsertMethod = mutableMethod
speedSelectionInsertIndex = scanResult.patternScanResult!!.startIndex - 3
speedSelectionValueRegister =
mutableMethod.instruction<FiveRegisterInstruction>(speedSelectionInsertIndex).registerD
val speedSelectionMethodInstructions = mutableMethod.implementation!!.instructions
setPlaybackSpeedContainerClassFieldReference =
getReference(speedSelectionMethodInstructions, -1, Opcode.IF_EQZ)
setPlaybackSpeedClassFieldReference =
getReference(speedSelectionMethodInstructions, 1, Opcode.IGET)
setPlaybackSpeedMethodReference =
getReference(speedSelectionMethodInstructions, 2, Opcode.IGET)
} ?: return OnPlaybackSpeedItemClickFingerprint.toErrorResult()
userSelectedPlaybackSpeedHook(INTEGRATIONS_CLASS_DESCRIPTOR, "userSelectedPlaybackSpeed")
return PatchResultSuccess()
}
companion object {
private const val INTEGRATIONS_CLASS_DESCRIPTOR = "Lapp/revanced/integrations/patches/VideoInformation;"
private lateinit var playerInitMethod: MutableMethod
private var playerInitInsertIndex = 4
private lateinit var timeMethod: MutableMethod
private var timeInitInsertIndex = 2
private lateinit var highPrecisionTimeMethod: MutableMethod
private var highPrecisionInsertIndex = 0
private fun MutableMethod.insert(insertIndex: Int, register: String, descriptor: String) =
addInstruction(insertIndex, "invoke-static { $register }, $descriptor")
private fun MutableMethod.insertTimeHook(insertIndex: Int, descriptor: String) =
insert(insertIndex, "p1, p2", descriptor)
/**
* Hook the player controller. Called when a video is opened or the current video is changed.
*
* Note: This hook is called very early and is called before the video id, video time, video length,
* and many other data fields are set.
*
* @param targetMethodClass The descriptor for the class to invoke when the player controller is created.
* @param targetMethodName The name of the static method to invoke when the player controller is created.
*/
internal fun onCreateHook(targetMethodClass: String, targetMethodName: String) =
playerInitMethod.insert(
playerInitInsertIndex++,
"v0",
"$targetMethodClass->$targetMethodName(Ljava/lang/Object;)V"
)
/**
* Hook the video time.
* The hook is usually called once per second.
*
* @param targetMethodClass The descriptor for the static method to invoke when the player controller is created.
* @param targetMethodName The name of the static method to invoke when the player controller is created.
*/
internal fun videoTimeHook(targetMethodClass: String, targetMethodName: String) =
timeMethod.insertTimeHook(
timeInitInsertIndex++,
"$targetMethodClass->$targetMethodName(J)V"
)
/**
* Hook the high precision video time.
* The hooks is called extremely often (10 to 15 times a seconds), so use with caution.
* Note: the hook is usually called _off_ the main thread
*
* @param targetMethodClass The descriptor for the static method to invoke when the player controller is created.
* @param targetMethodName The name of the static method to invoke when the player controller is created.
*/
internal fun highPrecisionTimeHook(targetMethodClass: String, targetMethodName: String) =
highPrecisionTimeMethod.insertTimeHook(
highPrecisionInsertIndex++,
"$targetMethodClass->$targetMethodName(J)V"
)
private fun getReference(instructions: List<BuilderInstruction>, offset: Int, opcode: Opcode) =
instructions[instructions.indexOfFirst { it.opcode == opcode } + offset].reference
val Instruction.reference get() = (this as ReferenceInstruction).reference.toString()
private lateinit var speedSelectionInsertMethod: MutableMethod
private var speedSelectionInsertIndex = 0
private var speedSelectionValueRegister = 0
/**
* Hook the video speed selected by the user.
*/
internal fun userSelectedPlaybackSpeedHook(targetMethodClass: String, targetMethodName: String) =
speedSelectionInsertMethod.addInstruction(
speedSelectionInsertIndex++,
"invoke-static {v$speedSelectionValueRegister}, $targetMethodClass->$targetMethodName(F)V"
)
/**
* Used by [RememberPlaybackSpeedPatch]
*/
internal lateinit var setPlaybackSpeedContainerClassFieldReference: String
internal lateinit var setPlaybackSpeedClassFieldReference: String
internal lateinit var setPlaybackSpeedMethodReference: String
}
}
@@ -0,0 +1,24 @@
package app.revanced.patches.youtube.video.oldqualitylayout.annotations
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package(
"com.google.android.youtube", arrayOf(
"17.49.37",
"18.03.36",
"18.03.42",
"18.04.35",
"18.04.41",
"18.05.32",
"18.05.35",
"18.05.40",
"18.08.37",
"18.15.40",
"18.16.37"
)
)]
)
@Target(AnnotationTarget.CLASS)
internal annotation class OldQualityLayoutCompatibility
@@ -0,0 +1,31 @@
package app.revanced.patches.youtube.video.oldqualitylayout.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object QualityMenuViewInflateFingerprint : MethodFingerprint(
"L", AccessFlags.FINAL or AccessFlags.PUBLIC, listOf("L", "L", "L"), listOf(
Opcode.INVOKE_SUPER,
Opcode.CONST,
Opcode.CONST_4,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CONST_16,
Opcode.INVOKE_VIRTUAL,
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.IGET_OBJECT,
Opcode.IGET_OBJECT,
Opcode.CONST_STRING,
)
)
@@ -0,0 +1,61 @@
package app.revanced.patches.youtube.video.oldqualitylayout.patch
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.SwitchPreference
import app.revanced.patches.youtube.video.oldqualitylayout.annotations.OldQualityLayoutCompatibility
import app.revanced.patches.youtube.video.oldqualitylayout.fingerprints.QualityMenuViewInflateFingerprint
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import org.jf.dexlib2.iface.instruction.FiveRegisterInstruction
@Patch
@DependsOn([IntegrationsPatch::class, SettingsPatch::class])
@Name("old-quality-layout")
@Description("Enables the original video quality flyout in the video player settings")
@OldQualityLayoutCompatibility
@Version("0.0.1")
// new ReVanced users have no idea what it means to use the "old quality layout menu"
// maybe rename this patch to better describe what it provides (ie: user-selectable-video-resolution )
class OldQualityLayoutPatch : BytecodePatch(
listOf(QualityMenuViewInflateFingerprint)
) {
override fun execute(context: BytecodeContext): PatchResult {
SettingsPatch.PreferenceScreen.VIDEO.addPreferences(
SwitchPreference(
"revanced_use_old_style_quality_settings",
StringResource("revanced_old_style_quality_settings_enabled_title", "Use old video quality player menu"),
true,
StringResource("revanced_old_style_quality_settings_summary_on", "Old video quality menu is used"),
StringResource("revanced_old_style_quality_settings_summary_off", "Old video quality menu is not used")
)
)
val inflateFingerprintResult = QualityMenuViewInflateFingerprint.result!!
val method = inflateFingerprintResult.mutableMethod
val instructions = method.implementation!!.instructions
// at this index the listener is added to the list view
val listenerInvokeRegister = instructions.size - 1 - 1
// get the register which stores the quality menu list view
val onItemClickViewRegister = (instructions[listenerInvokeRegister] as FiveRegisterInstruction).registerC
// insert the integrations method
method.addInstruction(
listenerInvokeRegister, // insert the integrations instructions right before the listener
"invoke-static { v$onItemClickViewRegister }, Lapp/revanced/integrations/patches/playback/quality/OldQualityLayoutPatch;->showOldQualityMenu(Landroid/widget/ListView;)V"
)
return PatchResultSuccess()
}
}
@@ -0,0 +1,10 @@
package app.revanced.patches.youtube.video.quality.annotations
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package("com.google.android.youtube", arrayOf("18.08.37", "18.15.40", "18.16.37"))]
)
@Target(AnnotationTarget.CLASS)
internal annotation class RememberVideoQualityCompatibility
@@ -0,0 +1,14 @@
package app.revanced.patches.youtube.video.quality.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object SetQualityByIndexMethodClassFieldReferenceFingerprint : MethodFingerprint(
returnType = "V",
parameters = listOf("L"),
opcodes = listOf(
Opcode.IGET_OBJECT,
Opcode.INVOKE_INTERFACE,
Opcode.RETURN_VOID
)
)
@@ -0,0 +1,9 @@
package app.revanced.patches.youtube.video.quality.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object VideoQualityItemOnClickParentFingerprint : MethodFingerprint(
"V",
strings = listOf("VIDEO_QUALITIES_MENU_BOTTOM_SHEET_FRAGMENT")
)
@@ -0,0 +1,21 @@
package app.revanced.patches.youtube.video.quality.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object VideoQualitySetterFingerprint : MethodFingerprint(
"V",
AccessFlags.PUBLIC or AccessFlags.FINAL,
listOf("[L", "I", "Z"),
listOf(
Opcode.IF_EQZ,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_VIRTUAL,
Opcode.IPUT_BOOLEAN,
),
strings = listOf("menu_item_video_quality"),
)
@@ -0,0 +1,183 @@
package app.revanced.patches.youtube.video.quality.patch
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultError
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.shared.settings.preference.impl.ArrayResource
import app.revanced.patches.shared.settings.preference.impl.ListPreference
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.SwitchPreference
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.patches.youtube.video.information.patch.VideoInformationPatch
import app.revanced.patches.youtube.video.quality.annotations.RememberVideoQualityCompatibility
import app.revanced.patches.youtube.video.quality.fingerprints.SetQualityByIndexMethodClassFieldReferenceFingerprint
import app.revanced.patches.youtube.video.quality.fingerprints.VideoQualityItemOnClickParentFingerprint
import app.revanced.patches.youtube.video.quality.fingerprints.VideoQualitySetterFingerprint
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.reference.FieldReference
@Patch
@DependsOn([IntegrationsPatch::class, VideoInformationPatch::class, SettingsPatch::class])
@Name("remember-video-quality")
@Description("Adds the ability to remember the video quality you chose in the video quality flyout.")
@RememberVideoQualityCompatibility
@Version("0.0.1")
class RememberVideoQualityPatch : BytecodePatch(
listOf(
VideoQualitySetterFingerprint,
VideoQualityItemOnClickParentFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
// This is bloated as each value has it's own String key/value
// ideally the entries would be raw values (and not a key to a String resource)
val entries = listOf(
StringResource("revanced_default_quality_entry_1", "Automatic quality"),
StringResource("revanced_default_quality_entry_2", "2160p"),
StringResource("revanced_default_quality_entry_3", "1440p"),
StringResource("revanced_default_quality_entry_4", "1080p"),
StringResource("revanced_default_quality_entry_5", "720p"),
StringResource("revanced_default_quality_entry_6", "480p"),
StringResource("revanced_default_quality_entry_7", "360p"),
StringResource("revanced_default_quality_entry_8", "280p"),
StringResource("revanced_default_quality_entry_9", "144p"),
)
val entryValues = listOf(
StringResource("revanced_default_quality_entry_value_1", "-2"),
StringResource("revanced_default_quality_entry_value_2", "2160"),
StringResource("revanced_default_quality_entry_value_3", "1440"),
StringResource("revanced_default_quality_entry_value_4", "1080"),
StringResource("revanced_default_quality_entry_value_5", "720"),
StringResource("revanced_default_quality_entry_value_6", "480"),
StringResource("revanced_default_quality_entry_value_7", "360"),
StringResource("revanced_default_quality_entry_value_8", "280"),
StringResource("revanced_default_quality_entry_value_9", "144"),
)
SettingsPatch.PreferenceScreen.VIDEO.addPreferences(
SwitchPreference(
"revanced_remember_video_quality_last_selected",
StringResource(
"revanced_remember_video_quality_last_selected_title",
"Remember video quality changes"
),
false,
StringResource(
"revanced_remember_video_quality_last_selected_summary_on",
"Quality changes apply to all videos"
),
StringResource(
"revanced_remember_video_quality_last_selected_summary_off",
"Quality changes only apply to the current video"
)
),
ListPreference(
"revanced_default_video_quality_wifi",
StringResource(
"revanced_default_video_quality_wifi_title",
"Default video quality on Wi-Fi network"
),
ArrayResource("revanced_video_quality_wifi_entry", entries),
ArrayResource("revanced_video_quality_wifi_entry_values", entryValues)
// default value and summary are set by integrations after loading
),
ListPreference(
"revanced_default_video_quality_mobile",
StringResource(
"revanced_default_video_quality_mobile_title",
"Default video quality on mobile network"
),
ArrayResource("revanced_video_quality_mobile_entries", entries),
ArrayResource("revanced_video_quality_mobile_entry_values", entryValues)
)
)
/*
* The following code works by hooking the method which is called when the user selects a video quality
* to remember the last selected video quality.
*
* It also hooks the method which is called when the video quality to set is determined.
* Conveniently, at this point the video quality is overridden to the remembered playback speed.
*/
VideoInformationPatch.onCreateHook(INTEGRATIONS_CLASS_DESCRIPTOR, "newVideoStarted")
// Inject a call to set the remembered quality once a video loads.
VideoQualitySetterFingerprint.result?.also {
if (!SetQualityByIndexMethodClassFieldReferenceFingerprint.resolve(context, it.classDef))
return PatchResultError("Could not resolve fingerprint to find setQualityByIndex method")
}?.let {
// This instruction refers to the field with the type that contains the setQualityByIndex method.
val instructions = SetQualityByIndexMethodClassFieldReferenceFingerprint.result!!
.method.implementation!!.instructions
val getOnItemClickListenerClassReference =
(instructions.elementAt(0) as ReferenceInstruction).reference
val getSetQualityByIndexMethodClassFieldReference =
(instructions.elementAt(1) as ReferenceInstruction).reference
val setQualityByIndexMethodClassFieldReference =
getSetQualityByIndexMethodClassFieldReference as FieldReference
val setQualityByIndexMethodClass = context.classes
.find { classDef -> classDef.type == setQualityByIndexMethodClassFieldReference.type }!!
// Get the name of the setQualityByIndex method.
val setQualityByIndexMethod = setQualityByIndexMethodClass.methods
.find { method -> method.parameterTypes.first() == "I" }
?: return PatchResultError("Could not find setQualityByIndex method")
it.mutableMethod.addInstructions(
0,
"""
# Get the object instance to invoke the setQualityByIndex method on.
iget-object v0, p0, $getOnItemClickListenerClassReference
iget-object v0, v0, $getSetQualityByIndexMethodClassFieldReference
# Get the method name.
const-string v1, "${setQualityByIndexMethod.name}"
# Set the quality.
# The first parameter is the array list of video qualities.
# The second parameter is the index of the selected quality.
# The register v0 stores the object instance to invoke the setQualityByIndex method on.
# The register v1 stores the name of the setQualityByIndex method.
invoke-static {p1, p2, v0, v1}, $INTEGRATIONS_CLASS_DESCRIPTOR->setVideoQuality([Ljava/lang/Object;ILjava/lang/Object;Ljava/lang/String;)I
move-result p2
""",
)
} ?: return VideoQualitySetterFingerprint.toErrorResult()
// Inject a call to remember the selected quality.
VideoQualityItemOnClickParentFingerprint.result?.let {
val onItemClickMethod = it.mutableClass.methods.find { method -> method.name == "onItemClick" }
onItemClickMethod?.apply {
val listItemIndexParameter = 3
addInstruction(
0,
"invoke-static {p$listItemIndexParameter}, $INTEGRATIONS_CLASS_DESCRIPTOR->userChangedQuality(I)V"
)
} ?: return PatchResultError("Failed to find onItemClick method")
} ?: return VideoQualityItemOnClickParentFingerprint.toErrorResult()
return PatchResultSuccess()
}
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR =
"Lapp/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch;"
}
}
@@ -0,0 +1,24 @@
package app.revanced.patches.youtube.video.speed.custom.annotations
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package(
"com.google.android.youtube", arrayOf(
"17.49.37",
"18.03.36",
"18.03.42",
"18.04.35",
"18.04.41",
"18.05.32",
"18.05.35",
"18.05.40",
"18.08.37",
"18.15.40",
"18.16.37"
)
)]
)
@Target(AnnotationTarget.CLASS)
internal annotation class CustomPlaybackSpeedCompatibility
@@ -0,0 +1,22 @@
package app.revanced.patches.youtube.video.speed.custom.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
@FuzzyPatternScanMethod(2)
object SpeedArrayGeneratorFingerprint : MethodFingerprint(
"[L",
AccessFlags.PUBLIC or AccessFlags.STATIC,
opcodes = listOf(
Opcode.IF_NEZ,
Opcode.SGET_OBJECT,
Opcode.GOTO,
Opcode.INVOKE_INTERFACE,
Opcode.MOVE_RESULT_OBJECT,
Opcode.IGET_OBJECT,
),
strings = listOf("0.0#")
)
@@ -0,0 +1,22 @@
package app.revanced.patches.youtube.video.speed.custom.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object SpeedLimiterFingerprint : MethodFingerprint(
"V",
AccessFlags.PUBLIC or AccessFlags.FINAL,
listOf("F"),
listOf(
Opcode.INVOKE_STATIC,
Opcode.MOVE_RESULT,
Opcode.IF_EQZ,
Opcode.CONST_HIGH16,
Opcode.GOTO,
Opcode.CONST_HIGH16,
Opcode.CONST_HIGH16,
Opcode.INVOKE_STATIC,
),
)
@@ -0,0 +1,122 @@
package app.revanced.patches.youtube.video.speed.custom.patch
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.replaceInstruction
import app.revanced.patcher.patch.*
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patches.shared.settings.preference.impl.InputType
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.TextPreference
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.patches.youtube.video.speed.custom.annotations.CustomPlaybackSpeedCompatibility
import app.revanced.patches.youtube.video.speed.custom.fingerprints.SpeedArrayGeneratorFingerprint
import app.revanced.patches.youtube.video.speed.custom.fingerprints.SpeedLimiterFingerprint
import org.jf.dexlib2.iface.instruction.NarrowLiteralInstruction
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.reference.FieldReference
import org.jf.dexlib2.iface.reference.MethodReference
@Name("custom-video-speed")
@Description("Adds custom video speed options.")
@DependsOn([IntegrationsPatch::class])
@CustomPlaybackSpeedCompatibility
@Version("0.0.1")
class CustomVideoSpeedPatch : BytecodePatch(
listOf(
SpeedArrayGeneratorFingerprint, SpeedLimiterFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
SettingsPatch.PreferenceScreen.MISC.addPreferences(
TextPreference(
key = "revanced_custom_video_speeds",
title = StringResource(
"revanced_custom_video_speeds_title",
"Custom playback speeds"
),
inputType = InputType.TEXT_MULTI_LINE,
summary = StringResource(
"revanced_custom_video_speeds_summary",
"Add or change the video speeds available"
),
default = "0.25\n0.5\n0.75\n0.9\n0.95\n1.0\n1.05\n1.1\n1.25\n1.5\n1.75\n2.0\n3.0\n4.0\n5.0"
)
)
val arrayGenMethod = SpeedArrayGeneratorFingerprint.result?.mutableMethod!!
val arrayGenMethodImpl = arrayGenMethod.implementation!!
val sizeCallIndex = arrayGenMethodImpl.instructions
.indexOfFirst { ((it as? ReferenceInstruction)?.reference as? MethodReference)?.name == "size" }
if (sizeCallIndex == -1) return PatchResultError("Couldn't find call to size()")
val sizeCallResultRegister =
(arrayGenMethodImpl.instructions.elementAt(sizeCallIndex + 1) as OneRegisterInstruction).registerA
arrayGenMethod.replaceInstruction(
sizeCallIndex + 1,
"const/4 v$sizeCallResultRegister, 0x0"
)
val (arrayLengthConstIndex, arrayLengthConst) = arrayGenMethodImpl.instructions.withIndex()
.first { (it.value as? NarrowLiteralInstruction)?.narrowLiteral == 7 }
val arrayLengthConstDestination = (arrayLengthConst as OneRegisterInstruction).registerA
val videoSpeedsArrayType = "Lapp/revanced/integrations/patches/playback/speed/CustomVideoSpeedPatch;->customVideoSpeeds:[F"
arrayGenMethod.addInstructions(
arrayLengthConstIndex + 1,
"""
sget-object v$arrayLengthConstDestination, $videoSpeedsArrayType
array-length v$arrayLengthConstDestination, v$arrayLengthConstDestination
"""
)
val (originalArrayFetchIndex, originalArrayFetch) = arrayGenMethodImpl.instructions.withIndex()
.first {
val reference = ((it.value as? ReferenceInstruction)?.reference as? FieldReference)
reference?.definingClass?.contains("PlayerConfigModel") ?: false &&
reference?.type == "[F"
}
val originalArrayFetchDestination = (originalArrayFetch as OneRegisterInstruction).registerA
arrayGenMethod.replaceInstruction(
originalArrayFetchIndex,
"sget-object v$originalArrayFetchDestination, $videoSpeedsArrayType"
)
val limiterMethod = SpeedLimiterFingerprint.result?.mutableMethod!!
val limiterMethodImpl = limiterMethod.implementation!!
val (limiterMinConstIndex, limiterMinConst) = limiterMethodImpl.instructions.withIndex()
.first { (it.value as? NarrowLiteralInstruction)?.narrowLiteral == 0.25f.toRawBits() }
val (limiterMaxConstIndex, limiterMaxConst) = limiterMethodImpl.instructions.withIndex()
.first { (it.value as? NarrowLiteralInstruction)?.narrowLiteral == 2.0f.toRawBits() }
val limiterMinConstDestination = (limiterMinConst as OneRegisterInstruction).registerA
val limiterMaxConstDestination = (limiterMaxConst as OneRegisterInstruction).registerA
// edit: alternatively this might work by overriding with fixed values such as 0.1x and 10x
limiterMethod.replaceInstruction(
limiterMinConstIndex,
"sget v$limiterMinConstDestination, Lapp/revanced/integrations/patches/playback/speed/CustomVideoSpeedPatch;->minVideoSpeed:F"
)
limiterMethod.replaceInstruction(
limiterMaxConstIndex,
"sget v$limiterMaxConstDestination, Lapp/revanced/integrations/patches/playback/speed/CustomVideoSpeedPatch;->maxVideoSpeed:F"
)
return PatchResultSuccess()
}
}
@@ -0,0 +1,10 @@
package app.revanced.patches.youtube.video.speed.remember.annotation
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package("com.google.android.youtube", arrayOf("18.08.37", "18.15.40", "18.16.37"))]
)
@Target(AnnotationTarget.CLASS)
internal annotation class RememberPlaybackSpeedCompatibility
@@ -0,0 +1,8 @@
package app.revanced.patches.youtube.video.speed.remember.fingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
object InitializePlaybackSpeedValuesFingerprint : MethodFingerprint(
parameters = listOf("[L", "I"),
strings = listOf("menu_item_playback_speed")
)
@@ -0,0 +1,122 @@
package app.revanced.patches.youtube.video.speed.remember.patch
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.shared.settings.preference.impl.ArrayResource
import app.revanced.patches.shared.settings.preference.impl.ListPreference
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.SwitchPreference
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.patches.youtube.video.information.patch.VideoInformationPatch
import app.revanced.patches.youtube.video.information.patch.VideoInformationPatch.Companion.reference
import app.revanced.patches.youtube.video.speed.custom.patch.CustomVideoSpeedPatch
import app.revanced.patches.youtube.video.speed.remember.annotation.RememberPlaybackSpeedCompatibility
import app.revanced.patches.youtube.video.speed.remember.fingerprint.InitializePlaybackSpeedValuesFingerprint
@Name("remember-playback-speed")
@Description("Adds the ability to remember the playback speed you chose in the video playback speed flyout.")
@DependsOn([IntegrationsPatch::class, SettingsPatch::class, VideoInformationPatch::class, CustomVideoSpeedPatch::class])
@RememberPlaybackSpeedCompatibility
@Version("0.0.1")
class RememberPlaybackSpeedPatch : BytecodePatch(
listOf(
InitializePlaybackSpeedValuesFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
SettingsPatch.PreferenceScreen.VIDEO.addPreferences(
SwitchPreference(
"revanced_remember_playback_speed_last_selected",
StringResource(
"revanced_remember_playback_speed_last_selected_title",
"Remember playback speed changes"
),
true,
StringResource(
"revanced_remember_playback_speed_last_selected_summary_on",
"Playback speed changes apply to all videos"
),
StringResource(
"revanced_remember_playback_speed_last_selected_summary_off",
"Playback speed changes only apply to the current video"
)
),
ListPreference(
"revanced_default_playback_speed",
StringResource(
"revanced_default_playback_speed_title",
"Default playback speed"
),
// Dummy data:
// Entries and values are set by Integrations code based on the actual speeds available,
// and the values set here are ignored and do nothing.
ArrayResource(
"revanced_default_playback_speed_entries",
listOf(StringResource("revanced_default_playback_speed_entry", "1.0x"))
),
ArrayResource(
"revanced_default_playback_speed_entry_values",
listOf(StringResource("revanced_default_playback_speed_entry_value", "1.0"))
)
)
)
VideoInformationPatch.onCreateHook(INTEGRATIONS_CLASS_DESCRIPTOR, "newVideoStarted")
VideoInformationPatch.userSelectedPlaybackSpeedHook(
INTEGRATIONS_CLASS_DESCRIPTOR, "userSelectedPlaybackSpeed")
/*
* Hook the code that is called when the playback speeds are initialized, and sets the playback speed
*/
InitializePlaybackSpeedValuesFingerprint.result?.apply {
// Infer everything necessary for calling the method setPlaybackSpeed().
val onItemClickListenerClassFieldReference = mutableMethod.instruction(0).reference
// Registers are not used at index 0, so they can be freely used.
mutableMethod.addInstructions(
0,
"""
invoke-static { }, $INTEGRATIONS_CLASS_DESCRIPTOR->getPlaybackSpeedOverride()F
move-result v0
# Check if the playback speed is not 1.0x.
const/high16 v1, 0x3f800000 # 1.0f
cmpg-float v1, v0, v1
if-eqz v1, :do_not_override
# Get the instance of the class which has the container class field below.
iget-object v1, p0, $onItemClickListenerClassFieldReference
# Get the container class field.
iget-object v1, v1, ${VideoInformationPatch.setPlaybackSpeedContainerClassFieldReference}
# Get the field from its class.
iget-object v2, v1, ${VideoInformationPatch.setPlaybackSpeedClassFieldReference}
# Invoke setPlaybackSpeed on that class.
invoke-virtual {v2, v0}, ${VideoInformationPatch.setPlaybackSpeedMethodReference}
""".trimIndent(),
listOf(ExternalLabel("do_not_override", mutableMethod.instruction(0)))
)
} ?: return InitializePlaybackSpeedValuesFingerprint.toErrorResult()
return PatchResultSuccess()
}
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR =
"Lapp/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch;"
}
}
@@ -0,0 +1,24 @@
package app.revanced.patches.youtube.video.videoid.annotation
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package(
"com.google.android.youtube", arrayOf(
"17.49.37",
"18.03.36",
"18.03.42",
"18.04.35",
"18.04.41",
"18.05.32",
"18.05.35",
"18.05.40",
"18.08.37",
"18.15.40",
"18.16.37"
)
)]
)
@Target(AnnotationTarget.CLASS)
internal annotation class VideoIdCompatibility
@@ -0,0 +1,31 @@
package app.revanced.patches.youtube.video.videoid.fingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
@FuzzyPatternScanMethod(2)
object VideoIdFingerprint : MethodFingerprint(
returnType = "V",
access = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("L"),
opcodes = listOf(
Opcode.IF_EQZ,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_INTERFACE,
Opcode.MOVE_RESULT_OBJECT,
Opcode.IF_EQZ,
Opcode.IGET_OBJECT,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_INTERFACE,
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_INTERFACE,
Opcode.MOVE_RESULT_OBJECT
),
null,
{ it.definingClass.endsWith("SubtitlesOverlayPresenter;") }
)
@@ -0,0 +1,16 @@
package app.revanced.patches.youtube.video.videoid.fingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object VideoIdFingerprintBackgroundPlay : MethodFingerprint(
returnType = "V",
access = AccessFlags.DECLARED_SYNCHRONIZED or AccessFlags.FINAL or AccessFlags.PUBLIC,
parameters = listOf("L"),
opcodes = listOf(Opcode.INVOKE_INTERFACE),
customFingerprint = {
it.definingClass.endsWith("PlaybackLifecycleMonitor;")
}
)
@@ -0,0 +1,102 @@
package app.revanced.patches.youtube.video.videoid.patch
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.video.videoid.annotation.VideoIdCompatibility
import app.revanced.patches.youtube.video.videoid.fingerprint.VideoIdFingerprint
import app.revanced.patches.youtube.video.videoid.fingerprint.VideoIdFingerprintBackgroundPlay
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
@Name("video-id-hook")
@Description("Hooks to detect when the video id changes")
@VideoIdCompatibility
@Version("0.0.1")
@DependsOn([IntegrationsPatch::class])
class VideoIdPatch : BytecodePatch(
listOf(VideoIdFingerprint, VideoIdFingerprintBackgroundPlay)
) {
override fun execute(context: BytecodeContext): PatchResult {
VideoIdFingerprint.result?.let { result ->
val videoIdRegisterInstructionIndex = result.scanResult.patternScanResult!!.endIndex
result.mutableMethod.also {
insertMethod = it
}.apply {
videoIdRegister = instruction<OneRegisterInstruction>(videoIdRegisterInstructionIndex).registerA
insertIndex = videoIdRegisterInstructionIndex + 1
}
} ?: return VideoIdFingerprint.toErrorResult()
VideoIdFingerprintBackgroundPlay.result?.let { result ->
val endIndex = result.scanResult.patternScanResult!!.endIndex
result.mutableMethod.also {
backgroundPlaybackMethod = it
}.apply {
backgroundPlaybackVideoIdRegister = instruction<OneRegisterInstruction>(endIndex + 1).registerA
backgroundPlaybackInsertIndex = endIndex + 2
}
} ?: return VideoIdFingerprintBackgroundPlay.toErrorResult()
return PatchResultSuccess()
}
companion object {
private var videoIdRegister = 0
private var insertIndex = 0
private lateinit var insertMethod: MutableMethod
private var backgroundPlaybackVideoIdRegister = 0
private var backgroundPlaybackInsertIndex = 0
private lateinit var backgroundPlaybackMethod: MutableMethod
/**
* Adds an invoke-static instruction, called with the new id when the video changes.
*
* Supports all videos (regular videos, Shorts and Stories).
*
* _Does not function if playing in the background with no video visible_.
*
* Be aware, this can be called multiple times for the same video id.
*
* @param methodDescriptor which method to call. Params have to be `Ljava/lang/String;`
*/
fun injectCall(
methodDescriptor: String
) = insertMethod.addInstructions(
// Keep injection calls in the order they're added:
// Increment index. So if additional injection calls are added, those calls run after this injection call.
insertIndex++,
"invoke-static {v$videoIdRegister}, $methodDescriptor"
)
/**
* Alternate hook that supports only regular videos, but hook supports changing to new video
* during background play when no video is visible.
*
* _Does not support Shorts or Stories_.
*
* Be aware, the hook can be called multiple times for the same video id.
*
* @param methodDescriptor which method to call. Params have to be `Ljava/lang/String;`
*/
fun injectCallBackgroundPlay(
methodDescriptor: String
) = backgroundPlaybackMethod.addInstructions(
backgroundPlaybackInsertIndex++, // move-result-object offset
"invoke-static {v$backgroundPlaybackVideoIdRegister}, $methodDescriptor"
)
}
}