Skip to content

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

Phone as microphone: a declined request reaches the PC

Beec44a42100a63
diff --git a/core/nectarlink-core/src/mic.rs b/core/nectarlink-core/src/mic.rs
index 42118f6..7de09c3 100644
--- a/core/nectarlink-core/src/mic.rs
+++ b/core/nectarlink-core/src/mic.rs
@@ -134,8 +134,12 @@ pub(crate) async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: &E
session.send(reply.reply_to(env.id)).await?;
}
types::MIC_STOP => {
- // The PC's side: stop listening; the phone's: stop recording.
- shared.stop_mic(&peer);
+ // The PC's side: stop listening; the phone's: stop recording. A phone that
+ // declines before any sound came has no stream to stop, but the PC still
+ // hears that the microphone is off, so it stops waiting.
+ if !shared.stop_mic(&peer) {
+ shared.emit(NodeEvent::Mic { device: peer, on: false });
+ }
tokio::task::spawn_blocking(move || platform.mic_stop_requested(&peer));
if env.id.is_some() {
session.send(Envelope::empty(types::OK).reply_to(env.id)).await?;
@@ -194,10 +198,13 @@ impl Shared {
}
}
- pub(crate) fn stop_mic(&self, peer: &DeviceId) {
- if let Some(signal) = self.mic_stops.lock().unwrap_or_else(|e| e.into_inner()).remove(peer) {
- signal.notify_one();
- self.emit(NodeEvent::Mic { device: *peer, on: false });
- }
+ /// Stops the stream from `peer`, if one is open; whether one was.
+ pub(crate) fn stop_mic(&self, peer: &DeviceId) -> bool {
+ let Some(signal) = self.mic_stops.lock().unwrap_or_else(|e| e.into_inner()).remove(peer) else {
+ return false;
+ };
+ signal.notify_one();
+ self.emit(NodeEvent::Mic { device: *peer, on: false });
+ true
}
}
diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rs
index 93983c4..acc4a2b 100644
--- a/core/nectarlink-core/tests/two_devices.rs
+++ b/core/nectarlink-core/tests/two_devices.rs
@@ -5956,6 +5956,29 @@ async fn a_phone_streams_its_microphone_when_its_user_agrees() {
phone.node.shutdown().await;
}
+#[tokio::test(flavor = "multi_thread")]
+async fn a_pc_hears_when_the_phone_declines_its_microphone_request() {
+ use nectarlink_core::{MIC_LISTEN, MIC_STREAM};
+
+ let mut pc = device_with("Desktop", DeviceKind::Desktop, &[MIC_LISTEN]).await;
+ let mut phone = device_with("Pixel", DeviceKind::Phone, &[MIC_STREAM]).await;
+ pair_qr(&mut pc, &mut phone).await;
+ let (pc_id, phone_id) = (pc.node.device_id(), phone.node.device_id());
+
+ with_timeout("mic_start", pc.node.mic_start(phone_id)).await.unwrap();
+ // The user taps Not now: no sound ever came, and the PC stops waiting.
+ phone.node.mic_stop(pc_id).await;
+ let device = wait_for(&mut pc, "mic off", |e| match e {
+ NodeEvent::Mic { device, on: false } => Some(*device),
+ _ => None,
+ })
+ .await;
+ assert_eq!(device, phone_id);
+
+ pc.node.shutdown().await;
+ phone.node.shutdown().await;
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn a_pc_without_mic_listen_refuses_a_phones_microphone() {
use nectarlink_core::{MIC_STREAM, MirrorAudioConfig, PacketKind};
diff --git a/docs/protocol/mic.md b/docs/protocol/mic.md
index 3e7c037..0c56ff9 100644
--- a/docs/protocol/mic.md
+++ b/docs/protocol/mic.md
@@ -44,7 +44,9 @@ taps Start. Errors: `DENIED` when the microphone is off for the PC,
**`mic.stop`** ends it: from the PC, the phone stops recording; from the
phone (its user tapped Stop), the PC stops listening. Ending the stream
-itself (either side) means the same.
+itself (either side) means the same. A phone whose user declines the
+request (Not now, or dismissing it) sends `mic.stop` too, before any
+sound, so the PC stops waiting.
## 3. The sound stream