Pick several audio sources at once, with the apps' own icons

The phone had no way to choose which apps a stream carries: the
studio's audio picker added one source and closed, and Android's app
list had names only. The picker now marks what is mixed in already
(stream.audio_sources) and switches a source on or off with a tap, so
several apps (and the microphone, desktop audio, test tones) are picked
in one go; Done closes it. Android's launchable apps come with their
icons: Kotlin draws each app's icon once per installed version into a
96 px PNG in the cache and hands over its path (AudioApp::icon), which
the picker shows. The shared window's audio is not offered on Android,
which shares the whole screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-03 18:30:04 +03:00
1 parent 1eabbb4704
commit ecb0e11668
7 files changed
+144 -47

No files matched your search

@@ -4,7 +4,11 @@ import android.Manifest
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Build import android.os.Build
import java.io.File
/** /**
* What the Rust library (crates/voelin-android/src/bridge.rs) calls, from any * What the Rust library (crates/voelin-android/src/bridge.rs) calls, from any
@@ -88,7 +92,10 @@ object Bridge {
ScreenCaptureService.stopInput(id) ScreenCaptureService.stopInput(id)
} }
/** Launchable apps other than us, as "label<TAB>package" lines sorted by label. */ /**
* Launchable apps other than us, as "label<TAB>package<TAB>icon" lines
* sorted by label; the icon is a PNG in our cache (empty if it has none).
*/
@JvmStatic @JvmStatic
fun launchableApps(): String { fun launchableApps(): String {
val pm = app.packageManager val pm = app.packageManager
@@ -100,12 +107,37 @@ object Bridge {
pm.queryIntentActivities(launcher, 0) pm.queryIntentActivities(launcher, 0)
} }
val separators = Regex("[\t\n]") val separators = Regex("[\t\n]")
val icons = File(app.cacheDir, "app-icons").apply { mkdirs() }
return activities return activities
.map { it.activityInfo.packageName to it.loadLabel(pm).toString() } .filter { it.activityInfo.packageName != app.packageName }
.filter { it.first != app.packageName } .distinctBy { it.activityInfo.packageName }
.distinctBy { it.first } .map { Triple(it, it.activityInfo.packageName, it.loadLabel(pm).toString()) }
.sortedBy { it.second.lowercase() } .sortedBy { it.third.lowercase() }
.joinToString("\n") { (packageName, label) -> label.replace(separators, " ") + "\t" + packageName } .joinToString("\n") { (info, packageName, label) ->
val icon = appIcon(pm, info, packageName, icons)?.path.orEmpty()
label.replace(separators, " ") + "\t" + packageName + "\t" + icon
}
}
private const val ICON_SIZE = 96
/** The app's icon as a PNG in `dir`, drawn once per installed version. */
private fun appIcon(pm: PackageManager, info: ResolveInfo, packageName: String, dir: File): File? {
val version = info.activityInfo.applicationInfo.sourceDir?.let { File(it).lastModified() } ?: 0L
val file = File(dir, "$packageName-$version.png")
if (file.exists()) return file
return try {
val bitmap = Bitmap.createBitmap(ICON_SIZE, ICON_SIZE, Bitmap.Config.ARGB_8888)
info.loadIcon(pm).apply { setBounds(0, 0, ICON_SIZE, ICON_SIZE) }.draw(Canvas(bitmap))
file.outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) }
bitmap.recycle()
// Icons of earlier versions.
dir.listFiles { f -> f.name.startsWith("$packageName-") && f.name != file.name }?.forEach { it.delete() }
file
} catch (e: Exception) {
file.delete()
null
}
} }
/** The cameras, as CameraCapture.list describes them. */ /** The cameras, as CameraCapture.list describes them. */
+11 -8
View File
@@ -181,8 +181,9 @@ pub fn stop_audio_input(id: u64) -> Result<()> {
}) })
} }
/// Launchable apps other than ours, as `(label, package)`, sorted by label. /// Launchable apps other than ours, as `(label, package, icon)`, sorted by
pub fn launchable_apps() -> Result<Vec<(String, String)>> { /// label; `icon` is the path of a PNG in the app's cache, or empty.
pub fn launchable_apps() -> Result<Vec<(String, String, String)>> {
let lines = with_bridge(|env, class| { let lines = with_bridge(|env, class| {
let value = env let value = env
.call_static_method( .call_static_method(
@@ -201,15 +202,17 @@ pub fn launchable_apps() -> Result<Vec<(String, String)>> {
Ok(parse_apps(&lines)) Ok(parse_apps(&lines))
} }
/// `label<TAB>package` lines. /// `label<TAB>package<TAB>icon` lines (the icon may be missing).
fn parse_apps(lines: &str) -> Vec<(String, String)> { fn parse_apps(lines: &str) -> Vec<(String, String, String)> {
lines lines
.lines() .lines()
.filter_map(|line| { .filter_map(|line| {
let (label, package) = line.split_once('\t')?; let mut parts = line.split('\t');
let package = package.trim(); let label = parts.next()?.trim();
let label = if label.trim().is_empty() { package } else { label.trim() }; let package = parts.next()?.trim();
(!package.is_empty()).then(|| (label.to_owned(), package.to_owned())) let icon = parts.next().unwrap_or_default().trim();
let label = if label.is_empty() { package } else { label };
(!package.is_empty()).then(|| (label.to_owned(), package.to_owned(), icon.to_owned()))
}) })
.collect() .collect()
} }
+2 -1
View File
@@ -196,9 +196,10 @@ impl AudioProvider for PlaybackAudio {
match bridge::launchable_apps() { match bridge::launchable_apps() {
Ok(apps) => apps Ok(apps) => apps
.into_iter() .into_iter()
.map(|(name, package)| AudioApp { .map(|(name, package, icon)| AudioApp {
name, name,
binary: Some(package), binary: Some(package),
icon: (!icon.is_empty()).then_some(icon),
..AudioApp::default() ..AudioApp::default()
}) })
.collect(), .collect(),
+2 -2
View File
@@ -73,8 +73,8 @@ pub(crate) fn wire(bridge: &StudioBridge) {
bridge.on_list_audio(|| { bridge.on_list_audio(|| {
with_app(|app| app.studio_list_audio()); with_app(|app| app.studio_list_audio());
}); });
bridge.on_add_audio(|i| { bridge.on_toggle_audio(|i| {
with_app(|app| app.studio_add_audio(index(i))); with_app(|app| app.studio_toggle_audio(index(i)));
}); });
bridge.on_set_gain(|i, db, done| { bridge.on_set_gain(|i, db, done| {
with_app(|app| app.studio_set_gain(index(i), db, done)); with_app(|app| app.studio_set_gain(index(i), db, done));
+45 -14
View File
@@ -12,7 +12,7 @@
//! VecModels) and get the same values; their callbacks come here //! VecModels) and get the same values; their callbacks come here
//! (bind/studio.rs). Commands go to the studio through one task, in order. //! (bind/studio.rs). Commands go to the studio through one task, in order.
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::rc::Rc; use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
@@ -831,7 +831,12 @@ impl App {
.iter() .iter()
.map(|(id, _)| { .map(|(id, _)| {
let (channel, server) = self.studio_destination_parts(*id); let (channel, server) = self.studio_destination_parts(*id);
StudioPick { name: channel.into(), detail: server.into(), kind: "server".into() } StudioPick {
name: channel.into(),
detail: server.into(),
kind: "server".into(),
..Default::default()
}
}) })
.collect(); .collect();
vm::list::sync(&self.studio.models.destinations, &rows); vm::list::sync(&self.studio.models.destinations, &rows);
@@ -1453,6 +1458,7 @@ impl App {
name: name.into(), name: name.into(),
detail: detail.into(), detail: detail.into(),
kind: icon.into(), kind: icon.into(),
..Default::default()
}; };
let mut picks: Vec<(Pick, StudioPick)> = Vec::new(); let mut picks: Vec<(Pick, StudioPick)> = Vec::new();
let source = |name: &str, kind: SourceKind| Pick::Source { name: name.into(), kind }; let source = |name: &str, kind: SourceKind| Pick::Source { name: name.into(), kind };
@@ -1611,11 +1617,24 @@ impl App {
let settings = self.studio_settings(); let settings = self.studio_settings();
let current = settings.get(&STREAM_AUDIO_SOURCES); let current = settings.get(&STREAM_AUDIO_SOURCES);
let mut picks: Vec<(Pick, StudioPick)> = Vec::new(); let mut picks: Vec<(Pick, StudioPick)> = Vec::new();
let mut add = |setting: AudioSourceSetting, name: String, detail: String| { let mut add =
let (_, _, kind) = vm::studio::audio_label(&setting.kind); |setting: AudioSourceSetting, name: String, detail: String, icon: Option<&str>| {
let row = StudioPick { name: name.into(), detail: detail.into(), kind: kind.into() }; let (_, _, kind) = vm::studio::audio_label(&setting.kind);
picks.push((Pick::Audio(setting), row)); let row = StudioPick {
}; name: name.into(),
detail: detail.into(),
kind: kind.into(),
// An application's own icon, when it gave a file (Android
// does; a desktop icon theme name is left out).
icon: icon
.map(Path::new)
.filter(|p| p.is_absolute())
.map(crate::images::file)
.unwrap_or_default(),
selected: current.iter().any(|s| s.kind == setting.kind),
};
picks.push((Pick::Audio(setting), row));
};
let fixed = [ let fixed = [
(AudioSourceKindSetting::Microphone, "Microphone", "What you say in voice, cleaned up"), (AudioSourceKindSetting::Microphone, "Microphone", "What you say in voice, cleaned up"),
( (
@@ -1630,14 +1649,17 @@ impl App {
), ),
]; ];
for (kind, name, detail) in fixed { for (kind, name, detail) in fixed {
if !current.iter().any(|s| s.kind == kind) { // A phone shares the whole screen: there is no window's app.
add(AudioSourceSetting::new(kind), name.into(), detail.into()); if cfg!(target_os = "android") && kind == AudioSourceKindSetting::Window {
continue;
} }
add(AudioSourceSetting::new(kind), name.into(), detail.into(), None);
} }
if self.demo_ui { if self.demo_ui {
for frequency in [330, 550] { for frequency in [440, 660, 330] {
let kind = AudioSourceKindSetting::Synthetic { frequency }; let kind = AudioSourceKindSetting::Synthetic { frequency };
add(AudioSourceSetting::new(kind), "Test tone".into(), format!("{frequency} Hz")); let detail = format!("{frequency} Hz");
add(AudioSourceSetting::new(kind), "Test tone".into(), detail, None);
} }
} else { } else {
if self.studio.apps.is_none() { if self.studio.apps.is_none() {
@@ -1654,7 +1676,7 @@ impl App {
.media .media
.clone() .clone()
.unwrap_or_else(|| if app.playing { "playing".into() } else { String::new() }); .unwrap_or_else(|| if app.playing { "playing".into() } else { String::new() });
add(AudioSourceSetting::new(kind), app.name, detail); add(AudioSourceSetting::new(kind), app.name, detail, app.icon.as_deref());
} }
} }
self.studio_set_picks(picks); self.studio_set_picks(picks);
@@ -1670,10 +1692,19 @@ impl App {
self.studio_refresh_audio(); self.studio_refresh_audio();
} }
pub(crate) fn studio_add_audio(&mut self, index: usize) { /// Mix a picked source in, or take it out if it is (the picker
/// chooses several at once), and mark the picks again.
pub(crate) fn studio_toggle_audio(&mut self, index: usize) {
let Some(Pick::Audio(setting)) = self.studio.picks.get(index) else { return }; let Some(Pick::Audio(setting)) = self.studio.picks.get(index) else { return };
let setting = setting.clone(); let setting = setting.clone();
self.studio_audio_sources(|sources| sources.push(setting)); self.studio_audio_sources(|sources| {
if sources.iter().any(|s| s.kind == setting.kind) {
sources.retain(|s| s.kind != setting.kind);
} else {
sources.push(setting);
}
});
self.studio_list_audio();
} }
/// While dragging the gain goes to the mixer directly; at the end it is /// While dragging the gain goes to the mixer directly; at the end it is
+39 -14
View File
@@ -1926,8 +1926,8 @@ component SourceDialog inherits Modal {
component PickDialog inherits Modal { component PickDialog inherits Modal {
in property <string> kind; in property <string> kind;
callback done(); callback done();
title: kind == "screen" ? @tr("Share a screen") : kind == "window" ? @tr("Share a window") : kind == "camera" ? @tr("Add a camera") : @tr("Add an audio source"); title: kind == "screen" ? @tr("Share a screen") : kind == "window" ? @tr("Share a window") : kind == "camera" ? @tr("Add a camera") : @tr("Audio sources");
subtitle: kind == "audio" ? @tr("Mixed into the stream's audio.") : @tr("Added to the live scene."); subtitle: kind == "audio" ? @tr("Mixed into the stream's audio. Pick as many as you like.") : @tr("Added to the live scene.");
icon: StudioKinds.icon(kind == "audio" ? "microphone" : kind); icon: StudioKinds.icon(kind == "audio" ? "microphone" : kind);
card-width: 520px; card-width: 520px;
card-height: 540px; card-height: 540px;
@@ -1948,24 +1948,22 @@ component PickDialog inherits Modal {
accessible-role: AccessibleRole.button; accessible-role: AccessibleRole.button;
accessible-label: pick.name; accessible-label: pick.name;
accessible-description: pick.detail; accessible-description: pick.detail;
accessible-action-default => { accessible-checkable: root.kind == "audio";
if root.kind == "audio" { accessible-checked: pick.selected;
StudioBridge.add-audio(i); accessible-action-default => { pick-touch.pick(); }
} else {
StudioBridge.add-picked(root.kind, i);
}
root.done();
}
pick-touch := TouchArea { pick-touch := TouchArea {
mouse-cursor: MouseCursor.pointer; mouse-cursor: MouseCursor.pointer;
clicked => { // Audio sources are switched on and off (several at once);
// the rest is added and the picker closes.
function pick() {
if root.kind == "audio" { if root.kind == "audio" {
StudioBridge.add-audio(i); StudioBridge.toggle-audio(i);
} else { } else {
StudioBridge.add-picked(root.kind, i); StudioBridge.add-picked(root.kind, i);
root.done();
} }
root.done();
} }
clicked => { self.pick(); }
} }
HorizontalLayout { HorizontalLayout {
padding-left: Theme.space-3; padding-left: Theme.space-3;
@@ -1977,10 +1975,15 @@ component PickDialog inherits Modal {
y: (parent.height - self.height) / 2; y: (parent.height - self.height) / 2;
border-radius: Theme.radius-md; border-radius: Theme.radius-md;
background: Theme.surface-3; background: Theme.surface-3;
Icon { if pick.icon.width == 0: Icon {
source: StudioKinds.icon(pick.kind); source: StudioKinds.icon(pick.kind);
size: 18px; size: 18px;
} }
if pick.icon.width > 0: Image {
width: 28px;
height: 28px;
source: pick.icon;
}
} }
VerticalLayout { VerticalLayout {
alignment: center; alignment: center;
@@ -1997,9 +2000,31 @@ component PickDialog inherits Modal {
overflow: elide; overflow: elide;
} }
} }
if root.kind == "audio": Rectangle {
width: 24px;
height: 24px;
y: (parent.height - self.height) / 2;
border-radius: 12px;
background: pick.selected ? Theme.accent : transparent;
border-width: pick.selected ? 0 : 1.5px;
border-color: Theme.border-strong;
if pick.selected: Icon {
source: Icons.check;
size: 16px;
tint: Theme.on-accent;
}
}
} }
} }
} }
if root.kind == "audio": HorizontalLayout {
alignment: end;
Button {
kind: ButtonKind.primary;
text: @tr("Done");
clicked => { root.done(); }
}
}
} }
// Rename a scene. // Rename a scene.
+7 -2
View File
@@ -78,6 +78,10 @@ export struct StudioPick {
detail: string, detail: string,
// For the icon: monitor, window, portal, camera, microphone, desktop, app, tone // For the icon: monitor, window, portal, camera, microphone, desktop, app, tone
kind: string, kind: string,
// The application's own icon (audio), if it has one.
icon: image,
// Already mixed in (audio): picking it again takes it out.
selected: bool,
} }
// The header, the preview's badges and the bottom bar. // The header, the preview's badges and the bottom bar.
@@ -223,9 +227,10 @@ export global StudioBridge {
// The phone layout's source cards: screen, camera, window. // The phone layout's source cards: screen, camera, window.
callback main-source(string); callback main-source(string);
// Audio: fill `picks` with what can be added; add, change, remove. // Audio: fill `picks` with what can be mixed in (marking what is);
// mix one in or take it out, change, remove.
callback list-audio(); callback list-audio();
callback add-audio(int); callback toggle-audio(int);
// While dragging (`done` false) and at the end. // While dragging (`done` false) and at the end.
callback set-gain(int, float, bool); callback set-gain(int, float, bool);
callback toggle-mute(int); callback toggle-mute(int);