Skip to content

Commit

Permalink
feat(video-information): hook video time
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Nov 5, 2022
1 parent 98eaf9c commit 6aa0ca9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import app.revanced.integrations.utils.LogHelper;

/**
* Hooking class for the player controller.
* Hooking class for the current playing video.
*/
public final class PlayerControllerPatch {
public final class VideoInformation {
private static final String SEEK_METHOD_NAME = "seekTo";

private static WeakReference<Object> playerController;
private static Method seekMethod;

private static long videoLength = 1;
private static long videoTime = -1;


/**
* Hook into PlayerController.onCreate() method.
Expand All @@ -26,26 +29,36 @@ public final class PlayerControllerPatch {
public static void playerController_onCreateHook(final Object thisRef) {
playerController = new WeakReference<>(thisRef);
videoLength = 1;
videoTime = -1;

try {
seekMethod = thisRef.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE);
seekMethod.setAccessible(true);
} catch (NoSuchMethodException ex) {
LogHelper.debug(PlayerControllerPatch.class, "Failed to initialize: " + ex.getMessage());
LogHelper.debug(VideoInformation.class, "Failed to initialize: " + ex.getMessage());
}
}

/**
* Set the current video length.
* Set the video length.
*
* @param length The length of the video in milliseconds.
*/
public static void setCurrentVideoLength(final long length) {
LogHelper.debug(PlayerControllerPatch.class, "Setting current video length to " + length);

public static void setVideoLength(final long length) {
LogHelper.debug(VideoInformation.class, "Setting current video length to " + length);
videoLength = length;
}

/**
* Set the video time.
*
* @param time The time of the video in milliseconds.
*/
public static void setVideoTime(final long time) {
LogHelper.debug(VideoInformation.class, "Current video time " + time);
videoTime = time;
}

/**
* Seek on the current video.
*
Expand All @@ -54,25 +67,34 @@ public static void setCurrentVideoLength(final long length) {
public static void seekTo(final long millisecond) {
new Handler(Looper.getMainLooper()).post(() -> {
if (seekMethod == null) {
LogHelper.debug(PlayerControllerPatch.class, "seekMethod was null");
LogHelper.debug(VideoInformation.class, "seekMethod was null");
return;
}

try {
LogHelper.debug(PlayerControllerPatch.class, "Seeking to " + millisecond);
LogHelper.debug(VideoInformation.class, "Seeking to " + millisecond);
seekMethod.invoke(playerController.get(), millisecond);
} catch (Exception ex) {
LogHelper.debug(PlayerControllerPatch.class, "Failed to seek: " + ex.getMessage());
LogHelper.debug(VideoInformation.class, "Failed to seek: " + ex.getMessage());
}
});
}

/**
* Get the length of the current video playing.
*
* @return The length of the video in milliseconds.
* @return The length of the video in milliseconds. 1 if not set yet.
*/
public static long getCurrentVideoLength() {
return videoLength;
return videoLength;
}

/**
* Get the time of the current video playing.
*
* @return The time of the video in milliseconds. -1 if not set yet.
*/
public static long getVideoTime() {
return videoTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Timer;
import java.util.TimerTask;

import app.revanced.integrations.patches.PlayerControllerPatch;
import app.revanced.integrations.patches.VideoInformation;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.sponsorblock.objects.SponsorSegment;
import app.revanced.integrations.sponsorblock.requests.SBRequester;
Expand Down Expand Up @@ -102,20 +102,18 @@ public static void executeDownloadSegments(String videoId) {
}

sponsorSegmentsOfCurrentVideo = segments;
// new Handler(Looper.getMainLooper()).post(findAndSkipSegmentRunnable);
// new Handler(Looper.getMainLooper()).post(findAndSkipSegmentRunnable);
}

/**
* Called when it's time to update the UI with new second, about once per second, only when playing, also in background
*/
public static void setCurrentVideoTime(long millis) {

public static void setVideoTime(long millis) {
LogHelper.debug(PlayerController.class, "setCurrentVideoTime: current video time: " + millis);
if (!SettingsEnum.SB_ENABLED.getBoolean()) return;
lastKnownVideoTime = millis;
if (millis <= 0) return;
//findAndSkipSegment(false);

if (millis == PlayerControllerPatch.getCurrentVideoLength()) {
if (millis == VideoInformation.getCurrentVideoLength()) {
SponsorBlockUtils.hideShieldButton();
SponsorBlockUtils.hideVoteButton();
return;
Expand Down Expand Up @@ -187,22 +185,19 @@ private static void sendViewRequestAsync(final long millis, final SponsorSegment
}).start();
}

/**
* Called very high frequency (once every about 100ms), also in background. It sometimes triggers when a video is paused (couple times in the row with the same value)
*/
public static void setCurrentVideoTimeHighPrecision(final long millis) {
if ((millis < lastKnownVideoTime && lastKnownVideoTime >= PlayerControllerPatch.getCurrentVideoLength()) || millis == 0) {
public static void setHighPrecisionVideoTime(final long millis) {
if ((millis < lastKnownVideoTime && lastKnownVideoTime >= VideoInformation.getCurrentVideoLength()) || millis == 0) {
SponsorBlockUtils.showShieldButton(); // skipping from end to the video will show the buttons again
SponsorBlockUtils.showVoteButton();
}
if (lastKnownVideoTime > 0) {
lastKnownVideoTime = millis;
} else
setCurrentVideoTime(millis);
setVideoTime(millis);
}

public static long getCurrentVideoLength() {
return PlayerControllerPatch.getCurrentVideoLength();
return VideoInformation.getCurrentVideoLength();
}

public static long getLastKnownVideoTime() {
Expand Down Expand Up @@ -296,7 +291,7 @@ public static void drawSponsorTimeBars(final Canvas canvas, final float posY) {
final float absoluteLeft = sponsorBarLeft;
final float absoluteRight = sponsorBarRight;

final float tmp1 = 1f / (float) PlayerControllerPatch.getCurrentVideoLength() * (absoluteRight - absoluteLeft);
final float tmp1 = 1f / (float) VideoInformation.getCurrentVideoLength() * (absoluteRight - absoluteLeft);
for (SponsorSegment segment : sponsorSegmentsOfCurrentVideo) {
float left = segment.start * tmp1 + absoluteLeft;
float right = segment.end * tmp1 + absoluteLeft;
Expand Down Expand Up @@ -330,7 +325,7 @@ public static boolean skipToMillisecond(long millisecond) {
try {
LogHelper.debug(PlayerController.class, "Skipping to millis=" + finalMillisecond);
lastKnownVideoTime = finalMillisecond;
PlayerControllerPatch.seekTo(finalMillisecond);
VideoInformation.seekTo(finalMillisecond);
} catch (Exception e) {
LogHelper.printException(PlayerController.class, "Cannot skip to millisecond", e);
}
Expand Down

0 comments on commit 6aa0ca9

Please sign in to comment.