From e53a1f90b08350cbe0543dc1d133a702c3052b26 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 18 Oct 2020 20:05:09 -0500 Subject: [PATCH] Correct some callers of time functions that expect monotonic clocks --- .../binding/input/virtual_controller/AnalogStick.java | 7 ++++--- .../binding/video/MediaCodecDecoderRenderer.java | 11 ++++++----- .../java/com/limelight/binding/video/VideoStats.java | 4 +++- .../limelight/computers/ComputerManagerService.java | 5 +++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/limelight/binding/input/virtual_controller/AnalogStick.java b/app/src/main/java/com/limelight/binding/input/virtual_controller/AnalogStick.java index 8c8fbea1..a54c4b1f 100644 --- a/app/src/main/java/com/limelight/binding/input/virtual_controller/AnalogStick.java +++ b/app/src/main/java/com/limelight/binding/input/virtual_controller/AnalogStick.java @@ -8,6 +8,7 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.os.SystemClock; import android.view.MotionEvent; import java.util.ArrayList; @@ -270,7 +271,7 @@ public class AnalogStick extends VirtualControllerElement { // We also release the deadzone if the user keeps the stick pressed for a bit to allow // them to make precise movements. stick_state = (stick_state == STICK_STATE.MOVED_ACTIVE || - System.currentTimeMillis() - timeLastClick > timeoutDeadzone || + SystemClock.uptimeMillis() - timeLastClick > timeoutDeadzone || movement_radius > radius_dead_zone) ? STICK_STATE.MOVED_ACTIVE : STICK_STATE.MOVED_IN_DEAD_ZONE; @@ -311,7 +312,7 @@ public class AnalogStick extends VirtualControllerElement { stick_state = STICK_STATE.MOVED_IN_DEAD_ZONE; // check for double click if (lastClickState == CLICK_STATE.SINGLE && - timeLastClick + timeoutDoubleClick > System.currentTimeMillis()) { + timeLastClick + timeoutDoubleClick > SystemClock.uptimeMillis()) { click_state = CLICK_STATE.DOUBLE; notifyOnDoubleClick(); } else { @@ -319,7 +320,7 @@ public class AnalogStick extends VirtualControllerElement { notifyOnClick(); } // reset last click timestamp - timeLastClick = System.currentTimeMillis(); + timeLastClick = SystemClock.uptimeMillis(); // set item pressed and update setPressed(true); break; diff --git a/app/src/main/java/com/limelight/binding/video/MediaCodecDecoderRenderer.java b/app/src/main/java/com/limelight/binding/video/MediaCodecDecoderRenderer.java index 554e5cfd..4d68ab22 100644 --- a/app/src/main/java/com/limelight/binding/video/MediaCodecDecoderRenderer.java +++ b/app/src/main/java/com/limelight/binding/video/MediaCodecDecoderRenderer.java @@ -20,6 +20,7 @@ import android.media.MediaFormat; import android.media.MediaCodec.BufferInfo; import android.media.MediaCodec.CodecException; import android.os.Build; +import android.os.SystemClock; import android.util.Range; import android.view.SurfaceHolder; @@ -410,7 +411,7 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer { // if (initialException != null) { // This isn't the first time we've had an exception processing video - if (System.currentTimeMillis() - initialExceptionTimestamp >= EXCEPTION_REPORT_DELAY_MS) { + if (SystemClock.uptimeMillis() - initialExceptionTimestamp >= EXCEPTION_REPORT_DELAY_MS) { // It's been over 3 seconds and we're still getting exceptions. Throw the original now. if (!reportedCrash) { reportedCrash = true; @@ -427,7 +428,7 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer { else { initialException = new RendererException(this, e); } - initialExceptionTimestamp = System.currentTimeMillis(); + initialExceptionTimestamp = SystemClock.uptimeMillis(); } } } @@ -637,7 +638,7 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer { } if (lastFrameNumber == 0) { - activeWindowVideoStats.measurementStartTimestamp = System.currentTimeMillis(); + activeWindowVideoStats.measurementStartTimestamp = SystemClock.uptimeMillis(); } else if (frameNumber != lastFrameNumber && frameNumber != lastFrameNumber + 1) { // We can receive the same "frame" multiple times if it's an IDR frame. // In that case, each frame start NALU is submitted independently. @@ -649,7 +650,7 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer { lastFrameNumber = frameNumber; // Flip stats windows roughly every second - if (System.currentTimeMillis() >= activeWindowVideoStats.measurementStartTimestamp + 1000) { + if (SystemClock.uptimeMillis() >= activeWindowVideoStats.measurementStartTimestamp + 1000) { if (prefs.enablePerfOverlay) { VideoStats lastTwo = new VideoStats(); lastTwo.add(lastWindowVideoStats); @@ -682,7 +683,7 @@ public class MediaCodecDecoderRenderer extends VideoDecoderRenderer { globalVideoStats.add(activeWindowVideoStats); lastWindowVideoStats.copy(activeWindowVideoStats); activeWindowVideoStats.clear(); - activeWindowVideoStats.measurementStartTimestamp = System.currentTimeMillis(); + activeWindowVideoStats.measurementStartTimestamp = SystemClock.uptimeMillis(); } activeWindowVideoStats.totalFramesReceived++; diff --git a/app/src/main/java/com/limelight/binding/video/VideoStats.java b/app/src/main/java/com/limelight/binding/video/VideoStats.java index 317714bf..baebb043 100644 --- a/app/src/main/java/com/limelight/binding/video/VideoStats.java +++ b/app/src/main/java/com/limelight/binding/video/VideoStats.java @@ -1,5 +1,7 @@ package com.limelight.binding.video; +import android.os.SystemClock; + class VideoStats { long decoderTimeMs; @@ -50,7 +52,7 @@ class VideoStats { } VideoStatsFps getFps() { - float elapsed = (System.currentTimeMillis() - this.measurementStartTimestamp) / (float) 1000; + float elapsed = (SystemClock.uptimeMillis() - this.measurementStartTimestamp) / (float) 1000; VideoStatsFps fps = new VideoStatsFps(); if (elapsed > 0) { diff --git a/app/src/main/java/com/limelight/computers/ComputerManagerService.java b/app/src/main/java/com/limelight/computers/ComputerManagerService.java index d23dbd05..6ba21320 100644 --- a/app/src/main/java/com/limelight/computers/ComputerManagerService.java +++ b/app/src/main/java/com/limelight/computers/ComputerManagerService.java @@ -40,6 +40,7 @@ import android.net.NetworkCapabilities; import android.os.Binder; import android.os.Build; import android.os.IBinder; +import android.os.SystemClock; import org.xmlpull.v1.XmlPullParserException; @@ -176,7 +177,7 @@ public class ComputerManagerService extends Service { LimeLog.warning(tuple.computer.name + " is offline (try " + offlineCount + ")"); offlineCount++; } else { - tuple.lastSuccessfulPollMs = System.currentTimeMillis(); + tuple.lastSuccessfulPollMs = SystemClock.elapsedRealtime(); offlineCount = 0; } } @@ -207,7 +208,7 @@ public class ComputerManagerService extends Service { synchronized (pollingTuples) { for (PollingTuple tuple : pollingTuples) { // Enforce the poll data TTL - if (System.currentTimeMillis() - tuple.lastSuccessfulPollMs > POLL_DATA_TTL_MS) { + if (SystemClock.elapsedRealtime() - tuple.lastSuccessfulPollMs > POLL_DATA_TTL_MS) { LimeLog.info("Timing out polled state for "+tuple.computer.name); tuple.computer.state = ComputerDetails.State.UNKNOWN; }