Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

android: include phone as speaker in core busy signal

abinanthan-cgf890fcc46668
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/audio/ActivityWindow.kt b/android/app/src/main/kotlin/app/nectarlink/android/audio/ActivityWindow.kt
new file mode 100644
index 0000000..a377dfc
--- /dev/null
+++ b/android/app/src/main/kotlin/app/nectarlink/android/audio/ActivityWindow.kt
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.audio
+
+import android.os.SystemClock
+
+/**
+ * Tracks whether activity occurred within a trailing time window.
+ */
+class ActivityWindow(
+ private val windowMs: Long,
+ private val now: () -> Long = { SystemClock.elapsedRealtime() },
+) {
+ @Volatile
+ private var lastActiveTime: Long = 0L
+
+ fun markActive() {
+ lastActiveTime = now()
+ }
+
+ fun reset() {
+ lastActiveTime = 0L
+ }
+
+ fun isActive(): Boolean {
+ val last = lastActiveTime
+ if (last == 0L) return false
+ val elapsed = now() - last
+ return elapsed in 0 until windowMs
+ }
+}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/audio/PcSpeakerPlayer.kt b/android/app/src/main/kotlin/app/nectarlink/android/audio/PcSpeakerPlayer.kt
index 1e7e47f..cf7c469 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/audio/PcSpeakerPlayer.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/audio/PcSpeakerPlayer.kt
@@ -10,6 +10,9 @@ import android.util.Log
import java.util.concurrent.LinkedBlockingDeque
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
/**
* Plays live PCM-16LE audio frames streamed from a paired PC (`pc.audio.relay`)
@@ -18,7 +21,9 @@ import java.util.concurrent.atomic.AtomicBoolean
* dedicated `THREAD_PRIORITY_URGENT_AUDIO` worker maintains a tight ~35–45 ms
* jitter cushion without ever calling `pause()`/`flush()` mid-stream.
*/
-class PcSpeakerPlayer {
+class PcSpeakerPlayer(
+ private val activity: ActivityWindow = ActivityWindow(ACTIVE_WINDOW_MS),
+) {
private class AudioChunk(
val sampleRate: Int,
val channels: Int,
@@ -27,12 +32,26 @@ class PcSpeakerPlayer {
private val queue = LinkedBlockingDeque<AudioChunk>(MAX_QUEUE_CHUNKS)
private val workerRunning = AtomicBoolean(false)
+ private val watchdogRunning = AtomicBoolean(false)
+ @Volatile
+ private var watchdogThread: Thread? = null
+
+ private val _playing = MutableStateFlow(false)
+
+ /** True while audio frames arrived within the trailing [ACTIVE_WINDOW_MS]. */
+ val playing: StateFlow<Boolean> = _playing.asStateFlow()
@Volatile
private var queuedBytes: Int = 0
fun playFrame(sampleRate: Int, channels: Int, pcm: ByteArray) {
if (pcm.isEmpty() || sampleRate <= 0 || channels <= 0) return
+ activity.markActive()
+ if (!_playing.value) {
+ _playing.value = true
+ }
+ ensureWatchdog()
+
val safeRate = sampleRate.coerceIn(8_000, 96_000)
val safeChannels = if (channels >= 2) 2 else 1
val frameBytes = safeChannels * 2
@@ -59,6 +78,9 @@ class PcSpeakerPlayer {
}
fun stop() {
+ activity.reset()
+ _playing.value = false
+ watchdogThread?.interrupt()
synchronized(queue) {
queue.clear()
queuedBytes = 0
@@ -67,6 +89,41 @@ class PcSpeakerPlayer {
}
}
+ private fun ensureWatchdog() {
+ if (watchdogRunning.compareAndSet(false, true)) {
+ Thread(::runWatchdogLoop, "nectarlink-pc-speaker-watchdog").apply {
+ isDaemon = true
+ start()
+ }
+ }
+ }
+
+ private fun runWatchdogLoop() {
+ watchdogThread = Thread.currentThread()
+ try {
+ while (true) {
+ Thread.sleep(WATCHDOG_CHECK_INTERVAL_MS)
+ if (!activity.isActive()) {
+ _playing.value = false
+ break
+ }
+ }
+ } catch (_: InterruptedException) {
+ Thread.currentThread().interrupt()
+ _playing.value = false
+ } finally {
+ watchdogThread = null
+ watchdogRunning.set(false)
+ if (activity.isActive() && watchdogRunning.compareAndSet(false, true)) {
+ _playing.value = true
+ Thread(::runWatchdogLoop, "nectarlink-pc-speaker-watchdog").apply {
+ isDaemon = true
+ start()
+ }
+ }
+ }
+ }
+
private fun pollChunk(timeoutMs: Long): AudioChunk? {
val chunk = if (timeoutMs <= 0L) {
queue.pollFirst()
@@ -226,5 +283,7 @@ class PcSpeakerPlayer {
private const val TAG = "PcSpeakerPlayer"
private const val INACTIVITY_TIMEOUT_MS = 30_000L
private const val MAX_QUEUE_CHUNKS = 96
+ const val ACTIVE_WINDOW_MS = 3_000L
+ private const val WATCHDOG_CHECK_INTERVAL_MS = 1_000L
}
}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
index 662857c..d96d932 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt
@@ -328,8 +328,9 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener
MirrorService.session.map { it.active }.distinctUntilChanged(),
WebcamService.session.map { it.active || it.connecting }.distinctUntilChanged(),
MicService.streaming.map { it != null }.distinctUntilChanged(),
- ) { hasTransfers: Boolean, isMirroring: Boolean, isWebcam: Boolean, isMic: Boolean ->
- hasTransfers || isMirroring || isWebcam || isMic
+ pcSpeakerPlayer.playing.map { it }.distinctUntilChanged(),
+ ) { hasTransfers: Boolean, isMirroring: Boolean, isWebcam: Boolean, isMic: Boolean, isSpeaker: Boolean ->
+ hasTransfers || isMirroring || isWebcam || isMic || isSpeaker
}.stateIn(scope, SharingStarted.Eagerly, false)
/** Short messages for the user (a command failed). */
diff --git a/android/app/src/test/kotlin/app/nectarlink/android/audio/ActivityWindowTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/audio/ActivityWindowTest.kt
new file mode 100644
index 0000000..59472cd
--- /dev/null
+++ b/android/app/src/test/kotlin/app/nectarlink/android/audio/ActivityWindowTest.kt
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.audio
+
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class ActivityWindowTest {
+ @Test
+ fun tracksTrailingActivityWindow() {
+ var currentTime = 10_000L
+ val window = ActivityWindow(windowMs = 3_000L, now = { currentTime })
+
+ // Initially inactive before any activity
+ assertFalse(window.isActive())
+
+ // Active right after markActive()
+ window.markActive()
+ assertTrue(window.isActive())
+
+ // Still active just inside the window (2,999 ms later)
+ currentTime += 2_999L
+ assertTrue(window.isActive())
+
+ // Inactive just after the window expires (3,000 ms elapsed)
+ currentTime += 1L
+ assertFalse(window.isActive())
+
+ // Inactive further after the window
+ currentTime += 1_000L
+ assertFalse(window.isActive())
+
+ // Active again after another markActive()
+ window.markActive()
+ assertTrue(window.isActive())
+
+ // Inactive immediately after reset()
+ window.reset()
+ assertFalse(window.isActive())
+ }
+}
diff --git a/android/app/src/test/kotlin/app/nectarlink/android/audio/PcSpeakerPlayerTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/audio/PcSpeakerPlayerTest.kt
new file mode 100644
index 0000000..05c0336
--- /dev/null
+++ b/android/app/src/test/kotlin/app/nectarlink/android/audio/PcSpeakerPlayerTest.kt
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.audio
+
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class PcSpeakerPlayerTest {
+ @Test
+ fun invalidFramesDoNotSetPlaying() {
+ var currentTime = 1_000L
+ val window = ActivityWindow(windowMs = 3_000L, now = { currentTime })
+ val player = PcSpeakerPlayer(window)
+
+ assertFalse(player.playing.value)
+
+ // Empty PCM
+ player.playFrame(44_100, 2, ByteArray(0))
+ assertFalse(player.playing.value)
+
+ // Invalid sample rate
+ player.playFrame(0, 2, ByteArray(16))
+ assertFalse(player.playing.value)
+
+ // Invalid channels
+ player.playFrame(44_100, 0, ByteArray(16))
+ assertFalse(player.playing.value)
+ }
+
+ @Test
+ fun validFrameSetsPlayingAndStopResets() {
+ var currentTime = 1_000L
+ val window = ActivityWindow(windowMs = 3_000L, now = { currentTime })
+ val player = PcSpeakerPlayer(window)
+
+ assertFalse(player.playing.value)
+
+ player.playFrame(44_100, 2, ByteArray(16))
+ assertTrue(player.playing.value)
+
+ player.stop()
+ assertFalse(player.playing.value)
+ }
+}