Skip to content

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

fix: phone as speaker needs the media permission and stays on that phone

Beefabfba700f48
diff --git a/core/nectarlink-core/src/deck.rs b/core/nectarlink-core/src/deck.rs
index 5ac60fb..7ebcf76 100644
--- a/core/nectarlink-core/src/deck.rs
+++ b/core/nectarlink-core/src/deck.rs
@@ -696,6 +696,14 @@ async fn run_pc_audio_set(shared: &Arc<Shared>, peer: &DeviceId, req: PcAudioSet
}
let is_phone_speaker_toggle =
req.volume.is_some_and(|v| v == PHONE_SPEAKER_OFF || v == PHONE_SPEAKER_ON) && req.muted.is_none();
+ // Playing this PC's sound on a phone shares what the PC plays, so it
+ // needs that phone's media permission. Stopping is always allowed.
+ if is_phone_speaker_toggle
+ && req.volume == Some(PHONE_SPEAKER_ON)
+ && !shared.toggle_on(peer, crate::media::TOGGLE)
+ {
+ return Envelope::error(ErrorCode::Denied, "media is turned off for this device");
+ }
if !is_phone_speaker_toggle
&& !shared.toggle_on(peer, crate::actions::POWER_TOGGLE)
&& !shared.toggle_on(peer, INPUT_TOGGLE)
diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rs
index e557605..5c2cf39 100644
--- a/core/nectarlink-core/tests/two_devices.rs
+++ b/core/nectarlink-core/tests/two_devices.rs
@@ -3556,6 +3556,26 @@ async fn pc_audio_volume_mute_output_devices_and_smart_clipboard_context_chips_f
.await
.unwrap();
+ // Phone as speaker needs the phone's `media` permission to start; stopping is always allowed.
+ pc.node.set_device_toggle(phone_id, "media", false).unwrap();
+ assert!(matches!(
+ phone.node.set_pc_audio(pc_id, Some(nectarlink_core::deck::PHONE_SPEAKER_ON), None).await,
+ Err(Error::Denied)
+ ));
+ with_timeout(
+ "phone speaker off while media is off",
+ phone.node.set_pc_audio(pc_id, Some(nectarlink_core::deck::PHONE_SPEAKER_OFF), None),
+ )
+ .await
+ .unwrap();
+ pc.node.set_device_toggle(phone_id, "media", true).unwrap();
+ with_timeout(
+ "phone speaker on",
+ phone.node.set_pc_audio(pc_id, Some(nectarlink_core::deck::PHONE_SPEAKER_ON), None),
+ )
+ .await
+ .unwrap();
+
// Smart clipboard context chips: phone sends different text clips to PC,
// and PC classifies each into at most 1 context chip (and None for OTP).
let cases = [
diff --git a/desktop/app/src/pc_audio_stream.rs b/desktop/app/src/pc_audio_stream.rs
index 5c1210e..0b94d72 100644
--- a/desktop/app/src/pc_audio_stream.rs
+++ b/desktop/app/src/pc_audio_stream.rs
@@ -4,6 +4,35 @@
//! 32-bit float stereo samples to 16-bit signed little-endian PCM, and streams
//! them to connected phones over NectarLink's low-latency audio stream.
+/// Where the PC's sound goes: only to the phone that asked for it, waiting
+/// while it's away (never to another phone, which may be someone else's), or
+/// to the first online phone when the PC itself turned the speaker on.
+#[cfg_attr(not(any(windows, target_os = "linux")), allow(dead_code))]
+fn pick_target(
+ wanted: Option<nectarlink_core::DeviceId>,
+ online: &[nectarlink_core::DeviceId],
+) -> Option<nectarlink_core::DeviceId> {
+ match wanted {
+ Some(phone) => online.contains(&phone).then_some(phone),
+ None => online.first().copied(),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::pick_target;
+ use nectarlink_core::DeviceId;
+
+ #[test]
+ fn sound_never_moves_to_a_phone_that_did_not_ask() {
+ let (a, b) = (DeviceId([1; 32]), DeviceId([2; 32]));
+ assert_eq!(pick_target(Some(a), &[b, a]), Some(a));
+ assert_eq!(pick_target(Some(a), &[b]), None);
+ assert_eq!(pick_target(None, &[b, a]), Some(b));
+ assert_eq!(pick_target(None, &[]), None);
+ }
+}
+
#[cfg(any(windows, target_os = "linux"))]
mod imp {
@@ -31,8 +60,8 @@ mod imp {
static ENABLED: AtomicBool = AtomicBool::new(false);
static WORKER_RUNNING: AtomicBool = AtomicBool::new(false);
- /// The phone that last turned "Phone speaker" on. Audio goes there while it's online;
- /// when the PC turned it on (or that phone is away), to the first phone that is.
+ /// The phone that last turned "Phone speaker" on. Audio goes only there; when
+ /// the PC turned it on, to the first phone that is online.
static TARGET: std::sync::Mutex<Option<nectarlink_core::DeviceId>> = std::sync::Mutex::new(None);
/// The phone the sound goes to first, if one asked.
@@ -297,7 +326,7 @@ mod imp {
.map(|d| d.id)
.collect();
let wanted = *TARGET.lock().unwrap_or_else(|e| e.into_inner());
- let Some(&peer) = wanted.as_ref().filter(|w| peers.contains(w)).or_else(|| peers.first()) else {
+ let Some(peer) = super::pick_target(wanted, &peers) else {
std::thread::sleep(Duration::from_millis(500));
return Ok(());
};
@@ -412,7 +441,7 @@ mod imp {
.map(|d| d.id)
.collect();
let wanted = *TARGET.lock().unwrap_or_else(|e| e.into_inner());
- let Some(&peer) = wanted.as_ref().filter(|w| peers.contains(w)).or_else(|| peers.first()) else {
+ let Some(peer) = super::pick_target(wanted, &peers) else {
std::thread::sleep(Duration::from_millis(500));
return Ok(());
};