Skip to content

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

android: phone as speaker stays busy while the PC has it on

Beea696a48f9baf
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 d96d932..1b62be2 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,7 +328,11 @@ 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(),
- pcSpeakerPlayer.playing.map { it }.distinctUntilChanged(),
+ // Phone as speaker: frames arriving, or the PC still has it on
+ // (frames stop while the PC is silent, for example a paused song).
+ combine(pcSpeakerPlayer.playing, _state.map { it.pcSpeakerRelayOn }) { playing, relayOn ->
+ playing || relayOn
+ }.distinctUntilChanged(),
) { hasTransfers: Boolean, isMirroring: Boolean, isWebcam: Boolean, isMic: Boolean, isSpeaker: Boolean ->
hasTransfers || isMirroring || isWebcam || isMic || isSpeaker
}.stateIn(scope, SharingStarted.Eagerly, false)
@@ -2221,16 +2225,16 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener
val current = s.device(id)?.deckState
if (current != null) {
val updatedOutputs = when (volume) {
- 201 -> if (current.outputDevices.any { it.id == "nectarlink:phone_speaker" }) {
+ 201 -> if (current.outputDevices.any { it.id == PHONE_SPEAKER_OUTPUT }) {
current.outputDevices
} else {
current.outputDevices + app.nectarlink.core.AudioOutputDevice(
- id = "nectarlink:phone_speaker",
+ id = PHONE_SPEAKER_OUTPUT,
name = "Phone speaker (NectarLink)",
isDefault = false,
)
}
- 200 -> current.outputDevices.filterNot { it.id == "nectarlink:phone_speaker" }
+ 200 -> current.outputDevices.filterNot { it.id == PHONE_SPEAKER_OUTPUT }
else -> current.outputDevices
}
s.reduce(
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt
index 159bb8e..65bd554 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt
@@ -164,6 +164,9 @@ data class Device(
}
}
+/** The output a PC's deck state lists while it plays its sound on this phone. */
+const val PHONE_SPEAKER_OUTPUT = "nectarlink:phone_speaker"
+
private val deviceOrder: Comparator<Device> =
compareByDescending<Device> { it.online }.thenBy { it.pairedAt }
@@ -250,6 +253,16 @@ data class CoreState(
) {
fun device(id: String): Device? = devices.firstOrNull { it.id == id }
+ /**
+ * Whether a connected PC has "Phone as speaker" on: its deck state lists
+ * this phone as an output. The PC stops sending audio during silence, so
+ * this stays true across pauses where no frames arrive.
+ */
+ val pcSpeakerRelayOn: Boolean
+ get() = devices.any { d ->
+ d.online && d.deckState?.outputDevices?.any { it.id == PHONE_SPEAKER_OUTPUT } == true
+ }
+
fun nameOf(id: String): String? =
device(id)?.name
?: discovered.firstOrNull { it.id == id }?.name
diff --git a/android/app/src/test/kotlin/app/nectarlink/android/core/CoreStateTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/core/CoreStateTest.kt
index 5d324a5..4a96d28 100644
--- a/android/app/src/test/kotlin/app/nectarlink/android/core/CoreStateTest.kt
+++ b/android/app/src/test/kotlin/app/nectarlink/android/core/CoreStateTest.kt
@@ -27,6 +27,25 @@ class CoreStateTest {
canWake = canWake,
)
+ @Test
+ fun phoneAsSpeakerCountsWhileAConnectedPcHasItOn() {
+ val speaker = app.nectarlink.core.AudioOutputDevice(PHONE_SPEAKER_OUTPUT, "Phone speaker (NectarLink)", false)
+ val headphones = app.nectarlink.core.AudioOutputDevice("hp", "Headphones", true)
+ fun deck(vararg outputs: app.nectarlink.core.AudioOutputDevice) =
+ app.nectarlink.core.DeckState(playing = false, volume = 50u, muted = false, micMuted = null, outputDevices = outputs.toList())
+ val online = CoreState()
+ .reduce(Event.DeviceAdded(pc("a", "Desk", 10)))
+ .reduce(Event.LinkChanged("a", Link.Online(relayed = false, usb = false, rttMs = 5u)))
+ fun withDeck(state: CoreState, deck: app.nectarlink.core.DeckState) =
+ state.copy(devices = state.devices.map { it.copy(deckState = deck) })
+
+ assertTrue("relay on, nothing playing", withDeck(online, deck(headphones, speaker)).pcSpeakerRelayOn)
+ assertFalse("relay off", withDeck(online, deck(headphones)).pcSpeakerRelayOn)
+ assertFalse("no deck state yet", online.pcSpeakerRelayOn)
+ val offline = online.reduce(Event.LinkChanged("a", Link.Offline(null)))
+ assertFalse("a PC that is offline doesn't keep the phone busy", withDeck(offline, deck(speaker)).pcSpeakerRelayOn)
+ }
+
@Test
fun devicesFollowEventsInPairingOrder() {
var state = CoreState()