Skip to content

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

Phone as microphone: start it from the phone, as the webcam can

Bee4ffd8c6ab3b8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 30535c3..a7d95b6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,7 +35,9 @@ change things that need a step from you, and its notes say so.
quiet voice where it can. The request also shows on the phone's Home,
with Start and Not now, so it's seen with notifications off; Not now
tells the PC, which stops waiting, and so does a request left unanswered
- for two minutes. While it's on, Home has a Stop button. Choosing another
+ for two minutes. It can also be started from the phone: **Use phone as
+microphone** sits under Use phone as speaker on Home. While it's on, Home
+has a Stop button. Choosing another
output on the PC moves the sound there right away, and **Use this phone
as a microphone** in the phone's Settings, for each PC, turns asking off
for that PC. Thanks to Jochen Kirstätter for finding the gaps.
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt
index 8180d09..5d378a8 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt
@@ -885,6 +885,13 @@ private fun App(
recorderSession = recorderSession,
micStreamingTo = micStreamingTo,
onDeclineMicRequest = core::declineMicRequest,
+ onStartMic = { pcId ->
+ // Asks for the microphone permission if needed, then starts.
+ context.startActivity(
+ android.content.Intent(context, app.nectarlink.android.mic.MicStartActivity::class.java)
+ .putExtra(app.nectarlink.android.mic.MicStartActivity.EXTRA_PC, pcId),
+ )
+ },
onStopMic = { app.nectarlink.android.mic.MicService.stopFromUser(context) },
onStopMirror = { MirrorService.stopCurrent() },
onOpenWebcam = { pcId ->
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt
index 9107c01..257a5ba 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt
@@ -202,6 +202,7 @@ fun HomeScreen(
onAcceptWebcamRequest: (app.nectarlink.android.webcam.WebcamRequest) -> Unit = {},
onDismissWebcamRequest: () -> Unit = {},
onDeclineMicRequest: (pcId: String) -> Unit = {},
+ onStartMic: (pcId: String) -> Unit = {},
onStopMic: () -> Unit = {},
onCopyClipboardHistory: (id: String) -> Unit = {},
onPinClipboardHistory: (id: String, pinned: Boolean) -> Unit = { _, _ -> },
@@ -618,6 +619,8 @@ fun HomeScreen(
onWebcam = onWebcam,
onSetPcAudio = onSetPcAudio,
onBrowsePcFiles = { browseFilesPcId = it },
+ micOn = micStreamingTo == device.id,
+ onToggleMic = { on -> if (on) onStartMic(device.id) else onStopMic() },
)
}
}
@@ -1347,6 +1350,9 @@ private fun PcCard(
onSetPcAudio: (String, Int?, Boolean?) -> Unit,
onBrowsePcFiles: (String) -> Unit = {},
onDesk: (String) -> Unit = {},
+ /** Whether this phone's microphone streams to this PC now. */
+ micOn: Boolean = false,
+ onToggleMic: (Boolean) -> Unit = {},
) {
val pickFiles = rememberLauncherForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) { uris ->
if (uris.isNotEmpty()) onSendFiles(device.id, uris)
@@ -1630,6 +1636,9 @@ private fun PcCard(
deckState = deckState,
ink = ink,
onSetPcAudio = { vol, muted -> onSetPcAudio(device.id, vol, muted) },
+ micAvailable = micOn || device.has("media.phone_mic"),
+ micOn = micOn,
+ onToggleMic = onToggleMic,
)
}
@@ -1748,6 +1757,9 @@ private fun PcAudioCardSection(
deckState: DeckState,
ink: Color,
onSetPcAudio: (volume: Int?, muted: Boolean?) -> Unit,
+ micAvailable: Boolean = false,
+ micOn: Boolean = false,
+ onToggleMic: (Boolean) -> Unit = {},
) {
val context = LocalContext.current
val pcMedia = remember(context) {
@@ -1893,6 +1905,49 @@ private fun PcAudioCardSection(
onCheckedChange = null,
)
}
+ if (micAvailable) {
+ Row(
+ modifier = Modifier
+ .fillMaxWidth()
+ .bentoWell(
+ cornerRadius = 16.dp,
+ fillColor = ink.copy(alpha = if (micOn) 0.14f else 0.06f),
+ )
+ .clickable(role = Role.Switch) { onToggleMic(!micOn) }
+ .padding(horizontal = 12.dp, vertical = 8.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ horizontalArrangement = Arrangement.spacedBy(10.dp),
+ ) {
+ Icon(
+ imageVector = MicIcon,
+ contentDescription = null,
+ tint = ink.copy(alpha = 0.9f),
+ modifier = Modifier.size(20.dp),
+ )
+ Column(modifier = Modifier.weight(1f)) {
+ Text(
+ text = stringResource(R.string.pc_audio_use_phone_mic),
+ style = MaterialTheme.typography.labelLarge,
+ color = ink,
+ maxLines = 1,
+ overflow = TextOverflow.Ellipsis,
+ )
+ Text(
+ text = stringResource(
+ if (micOn) R.string.pc_audio_use_phone_mic_on else R.string.pc_audio_use_phone_mic_sub,
+ ),
+ style = MaterialTheme.typography.labelSmall,
+ color = ink.copy(alpha = 0.78f),
+ maxLines = 1,
+ overflow = TextOverflow.Ellipsis,
+ )
+ }
+ Switch(
+ checked = micOn,
+ onCheckedChange = null,
+ )
+ }
+ }
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
diff --git a/android/app/src/main/res/values-zh-rCN/strings.xml b/android/app/src/main/res/values-zh-rCN/strings.xml
index 992ab96..f136e4c 100644
--- a/android/app/src/main/res/values-zh-rCN/strings.xml
+++ b/android/app/src/main/res/values-zh-rCN/strings.xml
@@ -420,6 +420,9 @@
<string name="pc_audio_use_phone_speaker">将手机用作扬声器</string>
<string name="pc_audio_use_phone_speaker_sub">在此手机上播放电脑音频</string>
<string name="pc_audio_use_phone_speaker_on">正在将电脑音频传输到此手机</string>
+ <string name="pc_audio_use_phone_mic">将手机用作麦克风</string>
+ <string name="pc_audio_use_phone_mic_sub">将此手机的麦克风发送到电脑</string>
+ <string name="pc_audio_use_phone_mic_on">电脑正在接收此手机的麦克风</string>
<string name="settings_storage">文件资源管理器中的存储</string>
<string name="storage_all_files_title">所有文件访问权限</string>
<string name="storage_all_files_on">已开启。你允许的电脑可在文件资源管理器中浏览、打开和复制此手机的文件。</string>
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index 88a7559..c9f49ac 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -438,6 +438,9 @@
<string name="pc_audio_use_phone_speaker">Use phone as speaker</string>
<string name="pc_audio_use_phone_speaker_sub">Play PC audio on this phone</string>
<string name="pc_audio_use_phone_speaker_on">Streaming PC audio to this phone</string>
+ <string name="pc_audio_use_phone_mic">Use phone as microphone</string>
+ <string name="pc_audio_use_phone_mic_sub">Send this phone\'s microphone to the PC</string>
+ <string name="pc_audio_use_phone_mic_on">The PC hears this phone\'s microphone</string>
<!-- Phone storage in File Explorer -->
<string name="settings_storage">Storage in File Explorer</string>
diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rs
index acc4a2b..01a410d 100644
--- a/core/nectarlink-core/tests/two_devices.rs
+++ b/core/nectarlink-core/tests/two_devices.rs
@@ -5956,6 +5956,35 @@ async fn a_phone_streams_its_microphone_when_its_user_agrees() {
phone.node.shutdown().await;
}
+#[tokio::test(flavor = "multi_thread")]
+async fn a_phone_can_start_its_microphone_for_a_pc_itself() {
+ use nectarlink_core::{MIC_LISTEN, MIC_STREAM, MirrorAudioConfig, MirrorSend, PacketKind};
+
+ 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());
+
+ // Started on the phone, as the webcam can be: the PC never asked.
+ let stream = Arc::new(with_timeout("mic_open", phone.node.mic_open(pc_id)).await.unwrap());
+ let config = MirrorAudioConfig { codec: nectarlink_core::MIRROR_PCM.into(), rate: 48_000, channels: 1 };
+ let sender = stream.clone();
+ let sent = tokio::task::spawn_blocking(move || sender.send(PacketKind::Config, 0, config.to_cbor()))
+ .await
+ .unwrap();
+ assert_eq!(sent, MirrorSend::Queued);
+ let device = wait_for(&mut pc, "mic on", |e| match e {
+ NodeEvent::Mic { device, on: true } => Some(*device),
+ _ => None,
+ })
+ .await;
+ assert_eq!(device, phone_id);
+ assert!(phone.platform.mic_asks.lock().unwrap().is_empty(), "nobody was asked");
+
+ pc.node.shutdown().await;
+ 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};
diff --git a/docs/protocol/mic.md b/docs/protocol/mic.md
index 0c56ff9..4975155 100644
--- a/docs/protocol/mic.md
+++ b/docs/protocol/mic.md
@@ -24,8 +24,10 @@ for a peer, `mic.start` is refused with `DENIED`, incoming `mic` streams are
reset with code `13`, and a running stream with that peer stops.
A phone **never** turns its microphone on silently: it streams only after
-its user taps Start on the PC's request, and shows an ongoing notification
-with a **Stop** button the whole time. Android only lets the microphone
+its user taps Start on the PC's request, or turns it on for a PC on the
+phone itself (as with the [webcam](webcam.md) §2), and shows an ongoing
+notification with a **Stop** button the whole time. A PC takes a stream
+it didn't ask for the same way, while `mic` is on for that phone. Android only lets the microphone
start from the foreground, which the tap provides.
## 2. Starting and stopping