Add RGB sync animation and frame delay adjustment
- Create AnimationSyncController for synchronized update animation - Add frame delay setting (-100 to +100ms) for easy RGB device timing - Add enable animation toggle in settings - Implement animation pattern with intensity modulation - Apply animation and delay to frame updates in encoder - Integrate with preferences system for persistent settings
This commit is contained in:
1 parent
f9ac4d1fff
commit
65b6fbc009
6 files changed
+155
-15
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package com.hyperion.grabber.common;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.hardware.display.DisplayManager;
|
||||
import android.hardware.display.VirtualDisplay;
|
||||
@@ -17,6 +18,8 @@ import android.util.Log;
|
||||
import com.hyperion.grabber.common.network.HyperionThread;
|
||||
import com.hyperion.grabber.common.util.BorderProcessor;
|
||||
import com.hyperion.grabber.common.util.HyperionGrabberOptions;
|
||||
import com.hyperion.grabber.common.util.AnimationSyncController;
|
||||
import com.hyperion.grabber.common.util.Preferences;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@@ -55,6 +58,9 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
||||
private static final int HIGH_LOAD_THRESHOLD = 3;
|
||||
private static final double DEADLINE_MISS_RATIO = 0.85;
|
||||
|
||||
private AnimationSyncController mAnimationSync;
|
||||
private Preferences mPreferences;
|
||||
|
||||
private final Runnable mCaptureRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -65,7 +71,13 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
||||
|
||||
if (mRunning && mCaptureHandler != null) {
|
||||
final long elapsedMs = (System.nanoTime() - start) / 1_000_000L;
|
||||
final long delayMs = Math.max(1L, mFrameIntervalMs - elapsedMs);
|
||||
long effectiveDelayMs = mFrameIntervalMs - elapsedMs;
|
||||
|
||||
if (mAnimationSync != null) {
|
||||
effectiveDelayMs += mAnimationSync.getEffectiveFrameDelayNs() / 1_000_000L;
|
||||
}
|
||||
|
||||
final long delayMs = Math.max(1L, effectiveDelayMs);
|
||||
mCaptureHandler.postDelayed(this, delayMs);
|
||||
}
|
||||
}
|
||||
@@ -100,12 +112,22 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
||||
MediaProjection projection,
|
||||
int screenWidth, int screenHeight,
|
||||
int density,
|
||||
HyperionGrabberOptions options) {
|
||||
HyperionGrabberOptions options,
|
||||
Context context) {
|
||||
super(listener, projection, screenWidth, screenHeight, density, options);
|
||||
|
||||
mFrameIntervalMs = 1000L / mFrameRate;
|
||||
initCaptureDimensions();
|
||||
|
||||
if (context != null) {
|
||||
mPreferences = new Preferences(context);
|
||||
int frameDelay = mPreferences.getInt(R.string.pref_key_frame_delay, 0);
|
||||
boolean enableAnimation = mPreferences.getBoolean(R.string.pref_key_enable_animation, false);
|
||||
mAnimationSync = new AnimationSyncController(frameDelay, enableAnimation);
|
||||
} else {
|
||||
mAnimationSync = new AnimationSyncController(0, false);
|
||||
}
|
||||
|
||||
if (DEBUG) Log.d(TAG, "Capture: " + mCaptureWidth + "x" + mCaptureHeight + " @ " + mFrameRate + "fps");
|
||||
|
||||
try {
|
||||
@@ -249,6 +271,11 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
||||
|
||||
byte[] rgb = getRgbBuffer(effWidth, effHeight);
|
||||
extractRgb(buffer, width, height, rowStride, pixelStride, bx, by, effWidth, effHeight, rgb);
|
||||
|
||||
if (mAnimationSync != null) {
|
||||
rgb = mAnimationSync.applyAnimationToFrame(rgb);
|
||||
}
|
||||
|
||||
mListener.sendFrame(rgb, effWidth, effHeight);
|
||||
|
||||
mRgbBufferIndex = (mRgbBufferIndex + 1) % RGB_BUFFER_RING_SIZE;
|
||||
@@ -383,6 +410,8 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
||||
mBorderX = 0;
|
||||
mBorderY = 0;
|
||||
mFrameCount = 0;
|
||||
mAnimationSync = null;
|
||||
mPreferences = null;
|
||||
|
||||
mHandler.getLooper().quit();
|
||||
clearAndDisconnect();
|
||||
|
||||
@@ -407,17 +407,18 @@ public class HyperionScreenService extends Service {
|
||||
|
||||
HyperionGrabberOptions options = new HyperionGrabberOptions(
|
||||
mHorizontalLEDCount, mVerticalLEDCount, mFrameRate, mSendAverageColor);
|
||||
|
||||
if (DEBUG) Log.v(TAG, "Creating encoder: " + metrics.widthPixels + "x" + metrics.heightPixels);
|
||||
mHyperionEncoder = new HyperionScreenEncoder(
|
||||
mHyperionThread.getReceiver(),
|
||||
projection,
|
||||
metrics.widthPixels,
|
||||
metrics.heightPixels,
|
||||
metrics.densityDpi,
|
||||
options);
|
||||
mHyperionEncoder.sendStatus();
|
||||
}
|
||||
|
||||
if (DEBUG) Log.v(TAG, "Creating encoder: " + metrics.widthPixels + "x" + metrics.heightPixels);
|
||||
mHyperionEncoder = new HyperionScreenEncoder(
|
||||
mHyperionThread.getReceiver(),
|
||||
projection,
|
||||
metrics.widthPixels,
|
||||
metrics.heightPixels,
|
||||
metrics.densityDpi,
|
||||
options,
|
||||
this);
|
||||
mHyperionEncoder.sendStatus();
|
||||
}
|
||||
|
||||
private void stopScreenRecord() {
|
||||
if (DEBUG) Log.v(TAG, "Stopping screen recorder");
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.hyperion.grabber.common.util;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class AnimationSyncController {
|
||||
private static final int ANIMATION_PATTERN_LENGTH = 64;
|
||||
private static final int ANIMATION_CYCLE_MS = 1000;
|
||||
|
||||
private final AtomicLong startTimeNs = new AtomicLong(System.nanoTime());
|
||||
private final AtomicInteger frameDelay = new AtomicInteger(0);
|
||||
private final AtomicInteger animationIntensity = new AtomicInteger(0);
|
||||
private boolean animationEnabled = false;
|
||||
|
||||
public AnimationSyncController(int frameDelayMs, boolean enableAnimation) {
|
||||
this.frameDelay.set(frameDelayMs);
|
||||
this.animationEnabled = enableAnimation;
|
||||
this.startTimeNs.set(System.nanoTime());
|
||||
}
|
||||
|
||||
public void setFrameDelay(int delayMs) {
|
||||
frameDelay.set(Math.max(-100, Math.min(100, delayMs)));
|
||||
}
|
||||
|
||||
public void setAnimationEnabled(boolean enabled) {
|
||||
animationEnabled = enabled;
|
||||
if (enabled) {
|
||||
startTimeNs.set(System.nanoTime());
|
||||
}
|
||||
}
|
||||
|
||||
public int getAnimationIntensity() {
|
||||
if (!animationEnabled) return 0;
|
||||
|
||||
long elapsedMs = (System.nanoTime() - startTimeNs.get()) / 1_000_000L;
|
||||
int cyclePosition = (int)(elapsedMs % ANIMATION_CYCLE_MS);
|
||||
|
||||
int intensity;
|
||||
if (cyclePosition < ANIMATION_CYCLE_MS / 2) {
|
||||
intensity = (cyclePosition * 255) / (ANIMATION_CYCLE_MS / 2);
|
||||
} else {
|
||||
intensity = ((ANIMATION_CYCLE_MS - cyclePosition) * 255) / (ANIMATION_CYCLE_MS / 2);
|
||||
}
|
||||
|
||||
return intensity & 0xFF;
|
||||
}
|
||||
|
||||
public long getEffectiveFrameDelayNs() {
|
||||
return frameDelay.get() * 1_000_000L;
|
||||
}
|
||||
|
||||
public byte[] applyAnimationToFrame(byte[] frameData) {
|
||||
if (!animationEnabled || frameData == null || frameData.length == 0) {
|
||||
return frameData;
|
||||
}
|
||||
|
||||
int intensity = getAnimationIntensity();
|
||||
byte[] animatedFrame = frameData.clone();
|
||||
|
||||
int step = Math.max(1, animatedFrame.length / ANIMATION_PATTERN_LENGTH);
|
||||
for (int i = 0; i < animatedFrame.length; i += step) {
|
||||
int channel = i % 3;
|
||||
if (channel < 2) {
|
||||
int value = (animatedFrame[i] & 0xFF);
|
||||
int modulated = (value * intensity) / 255;
|
||||
animatedFrame[i] = (byte) modulated;
|
||||
}
|
||||
}
|
||||
|
||||
return animatedFrame;
|
||||
}
|
||||
|
||||
public void sleep(long frameIntervalMs) throws InterruptedException {
|
||||
long sleepTimeMs = Math.max(1, frameIntervalMs + frameDelay.get());
|
||||
Thread.sleep(sleepTimeMs);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user