Merge pull request #33 from danielvanpaass/fix-not-streaming-after-sleep

Fix: Streaming stops working after device sleep/wake
This commit is contained in:
haumlab authored and GitHub committed 2026-05-17 12:35:03 +01:00
commit 5c4c64fe37
4 files changed
+55 -10

No files matched your search

@@ -370,6 +370,16 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
} }
} }
@Override
public void pauseRecording() {
if (DEBUG) Log.i(TAG, "Pausing");
mRunning = false;
setCapturing(false);
if (mCaptureHandler != null) {
mCaptureHandler.removeCallbacksAndMessages(null);
}
}
@Override @Override
public void resumeRecording() { public void resumeRecording() {
if (DEBUG) Log.i(TAG, "Resuming"); if (DEBUG) Log.i(TAG, "Resuming");
@@ -111,6 +111,7 @@ abstract class HyperionScreenEncoderBase {
} }
public abstract void stopRecording(); public abstract void stopRecording();
public abstract void pauseRecording();
public abstract void resumeRecording(); public abstract void resumeRecording();
public abstract void setOrientation(int orientation); public abstract void setOrientation(int orientation);
} }
@@ -100,19 +100,13 @@ public class HyperionScreenService extends Service {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
switch (Objects.requireNonNull(intent.getAction())) { switch (Objects.requireNonNull(intent.getAction())) {
case Intent.ACTION_SCREEN_ON: case Intent.ACTION_SCREEN_ON:
if (DEBUG) Log.v(TAG, "ACTION_SCREEN_ON intent received"); if (mHyperionThread != null) mHyperionThread.resumeConnection();
if (mHyperionEncoder != null && !isCapturing()) { if (mHyperionEncoder != null) mHyperionEncoder.resumeRecording();
if (DEBUG) Log.v(TAG, "Encoder not grabbing, attempting to restart");
mHyperionEncoder.resumeRecording();
}
notifyActivity(); notifyActivity();
break; break;
case Intent.ACTION_SCREEN_OFF: case Intent.ACTION_SCREEN_OFF:
if (DEBUG) Log.v(TAG, "ACTION_SCREEN_OFF intent received"); if (mHyperionEncoder != null) mHyperionEncoder.pauseRecording();
if (mHyperionEncoder != null) { if (mHyperionThread != null) mHyperionThread.pauseConnection();
if (DEBUG) Log.v(TAG, "Clearing current light data");
mHyperionEncoder.clearLights();
}
break; break;
case Intent.ACTION_CONFIGURATION_CHANGED: case Intent.ACTION_CONFIGURATION_CHANGED:
if (DEBUG) Log.v(TAG, "ACTION_CONFIGURATION_CHANGED intent received"); if (DEBUG) Log.v(TAG, "ACTION_CONFIGURATION_CHANGED intent received");
@@ -113,6 +113,46 @@ class HyperionThread(
connect() connect()
} }
/**
* Closes the current socket without shutting down the executor.
* Call [resumeConnection] to reconnect (e.g. on screen wake).
*/
fun pauseConnection() {
val pending = pendingTask
if (pending != null) {
pending.cancel(false)
pendingTask = null
}
pendingFrame = null
val client = clientRef.getAndSet(null)
if (client != null) {
try { client.disconnect() } catch (ignored: IOException) {}
}
connected.set(false)
}
/**
* Opens a fresh socket on the executor thread.
* Call after [pauseConnection] when the screen wakes up.
*/
fun resumeConnection() {
if (executor.isShutdown) return
executor.submit {
try {
val client = createClient()
if (client.isConnected()) {
clientRef.set(client)
connected.set(true)
callback.onConnected()
} else {
callback.onConnectionError(0, "Failed to reconnect")
}
} catch (e: IOException) {
callback.onConnectionError(e.hashCode(), e.message ?: "Unknown error")
}
}
}
private fun createClient(): HyperionClient { private fun createClient(): HyperionClient {
return if (wledEnabled && wledIp != null) { return if (wledEnabled && wledIp != null) {
WledDdpClient(InetAddress.getByName(wledIp), 4048) WledDdpClient(InetAddress.getByName(wledIp), 4048)