generated from ReVanced/revanced-patches-template
-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c5355c3
feat(Prime Video): Initial commit of Skip Ads patch
hoo-dles d3348bb
Code cleanup after PR review
hoo-dles 43d93ed
Code cleanup after PR review
hoo-dles 925d33c
Merge remote-tracking branch 'upstream/dev' into primevideo-ads
hoo-dles 73607d8
Adding application hook for shared extension patch
hoo-dles 573c1d8
Wrap extension in try block, and additional comments
hoo-dles bf30030
Forgot an import and dependency
hoo-dles ca4c320
Moving extension code toe separate package
hoo-dles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dependencies { | ||
compileOnly(project(":extensions:shared:library")) | ||
compileOnly(project(":extensions:primevideo:stub")) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest/> |
36 changes: 36 additions & 0 deletions
36
extensions/primevideo/src/main/java/app/revanced/extension/primevideo/ads/SkipAdsPatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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; | ||
|
||
import app.revanced.extension.shared.Logger; | ||
|
||
@SuppressWarnings("unused") | ||
public final class SkipAdsPatch { | ||
public static void enterServerInsertedAdBreakState(ServerInsertedAdBreakState state, AdBreakTrigger trigger, VideoPlayer player) { | ||
try { | ||
AdBreak adBreak = trigger.getBreak(); | ||
|
||
// There are two scenarios when entering the original method: | ||
// 1. Player naturally entered an ad break while watching a video. | ||
// 2. User is skipped/scrubbed to a position on the timeline. If seek position is past an ad break, | ||
// user is forced to watch an ad before continuing. | ||
// | ||
// Scenario 2 is indicated by trigger.getSeekStartPosition() != null, so skip directly to the scrubbing | ||
// target. Otherwise, just calculate when the ad break should end and skip to there. | ||
if (trigger.getSeekStartPosition() != null) | ||
player.seekTo(trigger.getSeekTarget().getTotalMilliseconds()); | ||
else | ||
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)); | ||
} catch (Exception ex) { | ||
Logger.printException(() -> "Failed skipping ads", ex); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
compileSdk = 34 | ||
|
||
defaultConfig { | ||
minSdk = 21 | ||
} | ||
LisoUseInAIKyrios marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest/> |
6 changes: 6 additions & 0 deletions
6
extensions/primevideo/stub/src/main/java/com/amazon/avod/fsm/SimpleTrigger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
extensions/primevideo/stub/src/main/java/com/amazon/avod/fsm/StateBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
extensions/primevideo/stub/src/main/java/com/amazon/avod/fsm/Trigger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.amazon.avod.fsm; | ||
|
||
public interface Trigger<T> { | ||
} |
7 changes: 7 additions & 0 deletions
7
extensions/primevideo/stub/src/main/java/com/amazon/avod/media/TimeSpan.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
extensions/primevideo/stub/src/main/java/com/amazon/avod/media/ads/AdBreak.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
4 changes: 4 additions & 0 deletions
4
.../primevideo/stub/src/main/java/com/amazon/avod/media/ads/internal/state/AdBreakState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
18 changes: 18 additions & 0 deletions
18
...rimevideo/stub/src/main/java/com/amazon/avod/media/ads/internal/state/AdBreakTrigger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...o/stub/src/main/java/com/amazon/avod/media/ads/internal/state/AdEnabledPlaybackState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
5 changes: 5 additions & 0 deletions
5
...ub/src/main/java/com/amazon/avod/media/ads/internal/state/AdEnabledPlayerTriggerType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
4 changes: 4 additions & 0 deletions
4
...ub/src/main/java/com/amazon/avod/media/ads/internal/state/ServerInsertedAdBreakState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
7 changes: 7 additions & 0 deletions
7
extensions/primevideo/stub/src/main/java/com/amazon/avod/media/playback/VideoPlayer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
4 changes: 4 additions & 0 deletions
4
...s/primevideo/stub/src/main/java/com/amazon/avod/media/playback/state/PlayerStateType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.amazon.avod.media.playback.state; | ||
|
||
public interface PlayerStateType { | ||
} |
4 changes: 4 additions & 0 deletions
4
...eo/stub/src/main/java/com/amazon/avod/media/playback/state/trigger/PlayerTriggerType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
patches/src/main/kotlin/app/revanced/patches/primevideo/ads/Fingerprints.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;" | ||
LisoUseInAIKyrios marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
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;" | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
patches/src/main/kotlin/app/revanced/patches/primevideo/ads/SkipAdsPatch.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.primevideo.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 skips video stream ads.", | ||
) { | ||
compatibleWith("com.amazon.avod.thirdpartyclient"("3.0.403.257")) | ||
|
||
dependsOn(sharedExtensionPatch) | ||
|
||
// Skip all the logic in ServerInsertedAdBreakState.enter(), which plays all the ad clips in this | ||
// ad break. Instead, force the video player to seek over the entire break and reset the state machine. | ||
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 { playerRegister } | ||
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;->enterServerInsertedAdBreakState(Lcom/amazon/avod/media/ads/internal/state/ServerInsertedAdBreakState;Lcom/amazon/avod/media/ads/internal/state/AdBreakTrigger;Lcom/amazon/avod/media/playback/VideoPlayer;)V | ||
return-void | ||
""" | ||
) | ||
} | ||
} | ||
} | ||
|
5 changes: 5 additions & 0 deletions
5
patches/src/main/kotlin/app/revanced/patches/primevideo/misc/extension/ExtensionPatch.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package app.revanced.patches.primevideo.misc.extension | ||
|
||
import app.revanced.patches.shared.misc.extension.sharedExtensionPatch | ||
|
||
val sharedExtensionPatch = sharedExtensionPatch("primevideo", applicationInitHook) |
9 changes: 9 additions & 0 deletions
9
patches/src/main/kotlin/app/revanced/patches/primevideo/misc/extension/Hooks.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package app.revanced.patches.primevideo.misc.extension | ||
|
||
import app.revanced.patches.shared.misc.extension.extensionHook | ||
|
||
internal val applicationInitHook = extensionHook { | ||
custom { method, classDef -> | ||
method.name == "onCreate" && classDef.endsWith("/SplashScreenActivity;") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.