-
-
Notifications
You must be signed in to change notification settings - Fork 416
feat(Prime Video): Add Skip ads
patch
#4824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dependencies { | ||
compileOnly(project(":extensions:primevideo:stub")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest/> |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||||||||||||||||||||||||||||||||||||||
package app.revanced.extension.primevideo.ads; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
import com.amazon.avod.fsm.SimpleTrigger; | ||||||||||||||||||||||||||||||||||||||||||
import com.amazon.avod.media.ads.AdBreak; | ||||||||||||||||||||||||||||||||||||||||||
import com.amazon.avod.media.ads.internal.state.AdBreakTrigger; | ||||||||||||||||||||||||||||||||||||||||||
import com.amazon.avod.media.ads.internal.state.AdEnabledPlayerTriggerType; | ||||||||||||||||||||||||||||||||||||||||||
import com.amazon.avod.media.playback.VideoPlayer; | ||||||||||||||||||||||||||||||||||||||||||
import com.amazon.avod.media.ads.internal.state.ServerInsertedAdBreakState; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
@SuppressWarnings("unused") | ||||||||||||||||||||||||||||||||||||||||||
public final class SkipAdsPatch { | ||||||||||||||||||||||||||||||||||||||||||
public static void ServerInsertedAdBreakState_enter(ServerInsertedAdBreakState state, AdBreakTrigger trigger, VideoPlayer player) { | ||||||||||||||||||||||||||||||||||||||||||
AdBreak adBreak = trigger.getBreak(); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
if (trigger.getSeekStartPosition() != null) | ||||||||||||||||||||||||||||||||||||||||||
// if scrubbing over ad, seek straight to it | ||||||||||||||||||||||||||||||||||||||||||
player.seekTo(trigger.getSeekTarget().getTotalMilliseconds()); | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the point of this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a user manually seeks to a spot in the video that skips over an ad, logic in the app will force the video player timestamp to the start of an ad. After watching the ad, the player will resume to where you were seeking. In this scenario, when we enter this method, we are at the beginning of the ad and |
||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||
// if naturally entering ad, seek to end of ad break | ||||||||||||||||||||||||||||||||||||||||||
player.seekTo(player.getCurrentPosition() + adBreak.getDurationExcludingAux().getTotalMilliseconds()); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// send "end of ads" trigger to state machine so everything doesn't get whacky | ||||||||||||||||||||||||||||||||||||||||||
state.doTrigger(new SimpleTrigger(AdEnabledPlayerTriggerType.NO_MORE_ADS_SKIP_TRANSITION)); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+15
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
plugins { | ||
id(libs.plugins.android.library.get().pluginId) | ||
} | ||
|
||
android { | ||
namespace = "app.revanced.extension" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this set already by the patches Gradle plugin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so. This could be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Build error when removing
|
||
compileSdk = 34 | ||
|
||
defaultConfig { | ||
minSdk = 21 | ||
} | ||
Comment on lines
+7
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this also set already by the patches Gradle plugin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compile sdk is already set and can be removed here. But minSdk for Prime video is Android 5.0 and not the default Android 6.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Build error when removing
|
||
|
||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest/> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.amazon.avod.fsm; | ||
|
||
public final class SimpleTrigger<T> implements Trigger<T> { | ||
public SimpleTrigger(T triggerType) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.amazon.avod.fsm; | ||
|
||
public abstract class StateBase<S, T> { | ||
// This method orginally has protected access (modified in patch code). | ||
public void doTrigger(Trigger<T> trigger) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.amazon.avod.fsm; | ||
|
||
public interface Trigger<T> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.amazon.avod.media; | ||
|
||
public final class TimeSpan { | ||
public long getTotalMilliseconds() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.amazon.avod.media.ads; | ||
|
||
import com.amazon.avod.media.TimeSpan; | ||
|
||
public interface AdBreak { | ||
TimeSpan getDurationExcludingAux(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.amazon.avod.media.ads.internal.state; | ||
|
||
public abstract class AdBreakState extends AdEnabledPlaybackState { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.amazon.avod.media.ads.internal.state; | ||
|
||
import com.amazon.avod.media.ads.AdBreak; | ||
import com.amazon.avod.media.TimeSpan; | ||
|
||
public class AdBreakTrigger { | ||
public AdBreak getBreak() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public TimeSpan getSeekTarget() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public TimeSpan getSeekStartPosition() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.amazon.avod.media.ads.internal.state; | ||
|
||
import com.amazon.avod.fsm.StateBase; | ||
import com.amazon.avod.media.playback.state.PlayerStateType; | ||
import com.amazon.avod.media.playback.state.trigger.PlayerTriggerType; | ||
|
||
public class AdEnabledPlaybackState extends StateBase<PlayerStateType, PlayerTriggerType> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.amazon.avod.media.ads.internal.state; | ||
|
||
public enum AdEnabledPlayerTriggerType { | ||
NO_MORE_ADS_SKIP_TRANSITION | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.amazon.avod.media.ads.internal.state; | ||
|
||
public class ServerInsertedAdBreakState extends AdBreakState { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.amazon.avod.media.playback; | ||
|
||
public interface VideoPlayer { | ||
long getCurrentPosition(); | ||
|
||
void seekTo(long positionMs); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.amazon.avod.media.playback.state; | ||
|
||
public interface PlayerStateType { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.amazon.avod.media.playback.state.trigger; | ||
|
||
public interface PlayerTriggerType { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package app.revanced.patches.primevideo.ads | ||
|
||
import app.revanced.patcher.fingerprint | ||
import com.android.tools.smali.dexlib2.AccessFlags | ||
import com.android.tools.smali.dexlib2.Opcode | ||
|
||
internal val enterServerInsertedAdBreakStateFingerprint = fingerprint { | ||
accessFlags(AccessFlags.PUBLIC) | ||
parameters("Lcom/amazon/avod/fsm/Trigger;") | ||
returns("V") | ||
opcodes( | ||
Opcode.INVOKE_VIRTUAL, | ||
Opcode.MOVE_RESULT_OBJECT, | ||
Opcode.CONST_4, | ||
Opcode.CONST_4 | ||
) | ||
custom { method, classDef -> | ||
method.name == "enter" && classDef.type == "Lcom/amazon/avod/media/ads/internal/state/ServerInsertedAdBreakState;" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is ".type" needed here? ClassDef extends CharSequence There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed since it's not using any String methods (such as endsWith). but not sure if using == will show a false warning about comparing different types since ClassDef is a CharSequence not a String. I guess it could be with or without .type, but I find it slightly more convoluted comparing the non-String ClassDef object directly to a static String. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing |
||
} | ||
} | ||
|
||
internal val doTriggerFingerprint = fingerprint { | ||
accessFlags(AccessFlags.PROTECTED) | ||
returns("V") | ||
opcodes( | ||
Opcode.IGET_OBJECT, | ||
Opcode.INVOKE_INTERFACE, | ||
Opcode.RETURN_VOID | ||
) | ||
custom { method, classDef -> | ||
method.name == "doTrigger" && classDef.type == "Lcom/amazon/avod/fsm/StateBase;" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,45 @@ | ||||||||||
package app.revanced.patches.primevideo.ads | ||||||||||
|
||||||||||
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions | ||||||||||
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction | ||||||||||
import app.revanced.patcher.patch.bytecodePatch | ||||||||||
import app.revanced.patches.shared.misc.extension.sharedExtensionPatch | ||||||||||
import com.android.tools.smali.dexlib2.AccessFlags | ||||||||||
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction | ||||||||||
|
||||||||||
@Suppress("unused") | ||||||||||
val skipAdsPatch = bytecodePatch( | ||||||||||
name = "Skip ads", | ||||||||||
description = "Automatically skip video stream ads.", | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
) { | ||||||||||
compatibleWith("com.amazon.avod.thirdpartyclient"("3.0.403.257")) | ||||||||||
|
||||||||||
dependsOn(sharedExtensionPatch("primevideo")) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the shared extension create a val sharedExtensionPatch = sharedExtensionPatch("primevideo") just like its done for the other apps for consistency. |
||||||||||
|
||||||||||
// We're going to skip all the logic in ServerInsertedAdBreakState.enter(), which plays all the ad clips in this | ||||||||||
// ad break. Instead, we'll force the video player to seek over the entire break and reset the state machine. | ||||||||||
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
execute { | ||||||||||
// Force doTrigger() access to public so we can call it from our extension. | ||||||||||
doTriggerFingerprint.method.accessFlags = AccessFlags.PUBLIC.value; | ||||||||||
|
||||||||||
val getPlayerIndex = enterServerInsertedAdBreakStateFingerprint.patternMatch!!.startIndex | ||||||||||
enterServerInsertedAdBreakStateFingerprint.method.apply { | ||||||||||
// Get register that stores VideoPlayer: | ||||||||||
// invoke-virtual ->getPrimaryPlayer() | ||||||||||
// move-result-object {vx} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
val playerRegister = getInstruction<OneRegisterInstruction>(getPlayerIndex + 1).registerA | ||||||||||
|
||||||||||
// Reuse the params from the original method: | ||||||||||
// p0 = ServerInsertedAdBreakState | ||||||||||
// p1 = AdBreakTrigger | ||||||||||
addInstructions( | ||||||||||
getPlayerIndex + 2, | ||||||||||
""" | ||||||||||
invoke-static {p0, p1, v${playerRegister}}, Lapp/revanced/extension/primevideo/ads/SkipAdsPatch;->ServerInsertedAdBreakState_enter(Lcom/amazon/avod/media/ads/internal/state/ServerInsertedAdBreakState;Lcom/amazon/avod/media/ads/internal/state/AdBreakTrigger;Lcom/amazon/avod/media/playback/VideoPlayer;)V | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
return-void | ||||||||||
""" | ||||||||||
) | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the naming convention we use for extensions is respected here. Please match the style. Something like "enterServerInsertedAdBreakState"