Share audio picks/excludes per app instance (name+pid, media names shown, pid-specific choices not persisted), Pick uses link routing so every stream of the instance is captured
Browse files at this commit19bdbbf0a68d7cbc3006488a65f53e8b9473f6fcParent: b38a99e- feat
- share audio picks/excludes per app instance (name+pid, media names shown, pid-specific choices not persisted), Pick uses link routing so every stream of the instance is captured
- source list refreshes every 2 s while shown
- Windows names sessions by process
- tile name chip white with user-tinted border
7 changed files +263 -89
- crates/client/src/gui.rs +4 -0
- crates/client/src/gui/camera_view.rs +4 -2
- crates/client/src/gui/share_view.rs +119 -50
- crates/client/src/settings.rs +23 -5
- crates/client/src/share/audio.rs +26 -1
- crates/client/src/share/audio_capture.rs +62 -23
- crates/client/src/share/audio_capture_windows.rs +25 -8
crates/client/src/gui.rsmodified+4 -0
| @@ -630,6 +630,8 @@ pub struct Yumush { | |||
| 630 | backups: Vec<BackupInfo>, | 630 | backups: Vec<BackupInfo>, |
| 631 | pending_backup_delete: Option<String>, | 631 | pending_backup_delete: Option<String>, |
| 632 | share_apps: Vec<crate::share::audio::AudioApp>, | 632 | share_apps: Vec<crate::share::audio::AudioApp>, |
| 633 | + share_apps_seen: std::cell::Cell<Instant>, | ||
| 634 | + share_apps_task: Option<gpui_kit::Task<()>>, | ||
| 633 | share_tab: common::share::Kind, | 635 | share_tab: common::share::Kind, |
| 634 | camera_modes: Vec<common::share::Mode>, | 636 | camera_modes: Vec<common::share::Mode>, |
| 635 | fullscreen: bool, | 637 | fullscreen: bool, |
| @@ -694,6 +696,8 @@ impl Yumush { | |||
| 694 | backups: Vec::new(), | 696 | backups: Vec::new(), |
| 695 | pending_backup_delete: None, | 697 | pending_backup_delete: None, |
| 696 | share_apps: Vec::new(), | 698 | share_apps: Vec::new(), |
| 699 | + share_apps_seen: std::cell::Cell::new(Instant::now()), | ||
| 700 | + share_apps_task: None, | ||
| 697 | share_tab: common::share::Kind::Screen, | 701 | share_tab: common::share::Kind::Screen, |
| 698 | camera_modes: Vec::new(), | 702 | camera_modes: Vec::new(), |
| 699 | fullscreen: false, | 703 | fullscreen: false, |
crates/client/src/gui/camera_view.rsmodified+4 -2
| @@ -510,6 +510,8 @@ pub(super) fn tile_label(yumush: &Yumush, palette: Palette, (user_id, kind): (Id | |||
| 510 | .px(px(8.)) | 510 | .px(px(8.)) |
| 511 | .rounded(px(12.)) | 511 | .rounded(px(12.)) |
| 512 | .bg(gpui_kit::black().opacity(0.72)) | 512 | .bg(gpui_kit::black().opacity(0.72)) |
| 513 | + .border_1() | ||
| 514 | + .border_color(palette.tinted(user_id).opacity(0.7)) | ||
| 513 | .flex() | 515 | .flex() |
| 514 | .items_center() | 516 | .items_center() |
| 515 | .gap(px(6.)) | 517 | .gap(px(6.)) |
| @@ -522,8 +524,8 @@ pub(super) fn tile_label(yumush: &Yumush, palette: Palette, (user_id, kind): (Id | |||
| 522 | })) | 524 | })) |
| 523 | .child( | 525 | .child( |
| 524 | div() | 526 | div() |
| 525 | - .font_weight(FontWeight::SEMIBOLD) | 527 | + .font_weight(FontWeight::MEDIUM) |
| 526 | - .text_color(palette.tinted(user_id)) | 528 | + .text_color(gpui_kit::white().opacity(0.95)) |
| 527 | .child(name.to_string()), | 529 | .child(name.to_string()), |
| 528 | ) | 530 | ) |
| 529 | .children(wave.map(|history| mini_wave(history, 10., palette.ok))) | 531 | .children(wave.map(|history| mini_wave(history, 10., palette.ok))) |
crates/client/src/gui/share_view.rsmodified+119 -50
| @@ -42,13 +42,15 @@ use super::{ | |||
| 42 | use crate::{ | 42 | use crate::{ |
| 43 | settings::{self, ShareAudio, ShareCodec, ShareSettings}, | 43 | settings::{self, ShareAudio, ShareCodec, ShareSettings}, |
| 44 | share::{ | 44 | share::{ |
| 45 | - audio::{AudioApp, AudioKind}, | 45 | + audio::{AudioApp, AudioKind, is_excluded, is_picked}, |
| 46 | encode::capabilities, | 46 | encode::capabilities, |
| 47 | session::WatchQuality, | 47 | session::WatchQuality, |
| 48 | }, | 48 | }, |
| 49 | }; | 49 | }; |
| 50 | 50 | ||
| 51 | const BAR_TTL: Duration = Duration::from_millis(2500); | 51 | const BAR_TTL: Duration = Duration::from_millis(2500); |
| 52 | +const APPS_REFRESH: Duration = Duration::from_secs(2); | ||
| 53 | +const APPS_IDLE: Duration = Duration::from_secs(5); | ||
| 52 | 54 | ||
| 53 | const RESOLUTIONS: [(Resolution, &str); 6] = [ | 55 | const RESOLUTIONS: [(Resolution, &str); 6] = [ |
| 54 | (Resolution::P360, "360"), | 56 | (Resolution::P360, "360"), |
| @@ -85,11 +87,31 @@ impl AudioMode { | |||
| 85 | match audio { | 87 | match audio { |
| 86 | ShareAudio::Off => Self::Off, | 88 | ShareAudio::Off => Self::Off, |
| 87 | ShareAudio::Everything => Self::Everything, | 89 | ShareAudio::Everything => Self::Everything, |
| 88 | - ShareAudio::Source(_) => Self::App, | 90 | + ShareAudio::App { .. } | ShareAudio::Device(_) => Self::App, |
| 89 | } | 91 | } |
| 90 | } | 92 | } |
| 91 | } | 93 | } |
| 92 | 94 | ||
| 95 | +fn pick(app: &AudioApp) -> ShareAudio { | ||
| 96 | + match app.kind { | ||
| 97 | + AudioKind::App => ShareAudio::App { | ||
| 98 | + name: app.name.clone(), | ||
| 99 | + pid: app.pid, | ||
| 100 | + }, | ||
| 101 | + AudioKind::Output | AudioKind::Input => ShareAudio::Device(app.node.clone()), | ||
| 102 | + } | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +fn chosen(audio: &ShareAudio, app: &AudioApp) -> bool { | ||
| 106 | + match audio { | ||
| 107 | + ShareAudio::App { name, pid } => { | ||
| 108 | + app.kind == AudioKind::App && is_picked((name, *pid), &app.name, app.pid) | ||
| 109 | + } | ||
| 110 | + ShareAudio::Device(node) => app.kind != AudioKind::App && *node == app.node, | ||
| 111 | + _ => false, | ||
| 112 | + } | ||
| 113 | +} | ||
| 114 | + | ||
| 93 | pub(super) struct Viewer { | 115 | pub(super) struct Viewer { |
| 94 | user_id: Id, | 116 | user_id: Id, |
| 95 | kind: Kind, | 117 | kind: Kind, |
| @@ -166,20 +188,33 @@ impl Yumush { | |||
| 166 | cx.notify(); | 188 | cx.notify(); |
| 167 | } | 189 | } |
| 168 | 190 | ||
| 169 | - pub(super) fn refresh_share_apps(&self, cx: &mut Context<Self>) { | 191 | + pub(super) fn refresh_share_apps(&mut self, cx: &mut Context<Self>) { |
| 192 | + self.share_apps_seen.set(Instant::now()); | ||
| 193 | + if self.share_apps_task.is_some() { | ||
| 194 | + return; | ||
| 195 | + } | ||
| 170 | let network = self.network.clone(); | 196 | let network = self.network.clone(); |
| 171 | - | 197 | + self.share_apps_task = Some(cx.spawn(async move |this, cx| { |
| 172 | - cx.spawn(async move |this, cx| { | 198 | + loop { |
| 173 | - let apps = cx | 199 | + let network = network.clone(); |
| 174 | - .background_executor() | 200 | + let apps = cx |
| 175 | - .spawn(async move { network.audio_apps() }) | 201 | + .background_executor() |
| 176 | - .await; | 202 | + .spawn(async move { network.audio_apps() }) |
| 177 | - let _ = this.update(cx, |yumush, cx| { | 203 | + .await; |
| 178 | - yumush.share_apps = apps; | 204 | + let shown = this |
| 179 | - cx.notify(); | 205 | + .update(cx, |yumush, cx| { |
| 180 | - }); | 206 | + yumush.share_apps = apps; |
| 181 | - }) | 207 | + cx.notify(); |
| 182 | - .detach(); | 208 | + yumush.share_apps_seen.get().elapsed() < APPS_IDLE |
| 209 | + }) | ||
| 210 | + .unwrap_or(false); | ||
| 211 | + if !shown { | ||
| 212 | + let _ = this.update(cx, |yumush, _| yumush.share_apps_task = None); | ||
| 213 | + return; | ||
| 214 | + } | ||
| 215 | + cx.background_executor().timer(APPS_REFRESH).await; | ||
| 216 | + } | ||
| 217 | + })); | ||
| 183 | } | 218 | } |
| 184 | 219 | ||
| 185 | fn refresh_camera_modes(&self, cx: &mut Context<Self>) { | 220 | fn refresh_camera_modes(&self, cx: &mut Context<Self>) { |
| @@ -224,26 +259,46 @@ impl Yumush { | |||
| 224 | let audio = match (mode, self.share_apps.first()) { | 259 | let audio = match (mode, self.share_apps.first()) { |
| 225 | (AudioMode::Off, _) => ShareAudio::Off, | 260 | (AudioMode::Off, _) => ShareAudio::Off, |
| 226 | (AudioMode::Everything, _) => ShareAudio::Everything, | 261 | (AudioMode::Everything, _) => ShareAudio::Everything, |
| 227 | - (AudioMode::App, Some(app)) => ShareAudio::Source(app.node.clone()), | 262 | + (AudioMode::App, Some(app)) => pick(app), |
| 228 | (AudioMode::App, None) => return, | 263 | (AudioMode::App, None) => return, |
| 229 | }; | 264 | }; |
| 230 | self.update_share(cx, |settings| settings.audio = audio); | 265 | self.update_share(cx, |settings| settings.audio = audio); |
| 231 | } | 266 | } |
| 232 | 267 | ||
| 233 | - pub(super) fn set_share_source(&mut self, node: String, cx: &mut Context<Self>) { | 268 | + pub(super) fn set_share_source(&mut self, app: &AudioApp, cx: &mut Context<Self>) { |
| 234 | - self.update_share(cx, |settings| settings.audio = ShareAudio::Source(node)); | 269 | + let audio = pick(app); |
| 270 | + self.update_share(cx, |settings| settings.audio = audio); | ||
| 235 | } | 271 | } |
| 236 | 272 | ||
| 237 | - pub(super) fn set_share_excluded( | 273 | + pub(super) fn set_share_included( |
| 238 | &mut self, | 274 | &mut self, |
| 239 | - name: String, | 275 | + app: &AudioApp, |
| 240 | - excluded: bool, | 276 | + included: bool, |
| 241 | cx: &mut Context<Self>, | 277 | cx: &mut Context<Self>, |
| 242 | ) { | 278 | ) { |
| 279 | + let siblings: Vec<String> = self | ||
| 280 | + .share_apps | ||
| 281 | + .iter() | ||
| 282 | + .filter(|other| other.kind == AudioKind::App && other.name == app.name) | ||
| 283 | + .map(AudioApp::key) | ||
| 284 | + .collect(); | ||
| 285 | + let name = app.name.clone(); | ||
| 286 | + let key = app.key(); | ||
| 243 | self.update_share(cx, |settings| { | 287 | self.update_share(cx, |settings| { |
| 244 | - settings.excluded.retain(|app| *app != name); | 288 | + let excluded = &mut settings.excluded; |
| 245 | - if excluded { | 289 | + let app_off = excluded.contains(&name); |
| 246 | - settings.excluded.push(name); | 290 | + excluded.retain(|entry| *entry != name && *entry != key); |
| 291 | + match (included, siblings.len() > 1) { | ||
| 292 | + (false, false) => excluded.push(name), | ||
| 293 | + (false, true) => excluded.push(key), | ||
| 294 | + (true, true) if app_off => { | ||
| 295 | + let others: Vec<String> = siblings | ||
| 296 | + .into_iter() | ||
| 297 | + .filter(|sibling| *sibling != key && !excluded.contains(sibling)) | ||
| 298 | + .collect(); | ||
| 299 | + excluded.extend(others); | ||
| 300 | + } | ||
| 301 | + (true, _) => {} | ||
| 247 | } | 302 | } |
| 248 | }); | 303 | }); |
| 249 | } | 304 | } |
| @@ -747,10 +802,12 @@ fn share_modes(yumush: &Yumush, kind: Kind) -> Vec<Mode> { | |||
| 747 | } | 802 | } |
| 748 | 803 | ||
| 749 | fn audio_list(entity: &Entity<Yumush>, yumush: &Yumush, palette: Palette) -> Option<AnyElement> { | 804 | fn audio_list(entity: &Entity<Yumush>, yumush: &Yumush, palette: Palette) -> Option<AnyElement> { |
| 805 | + yumush.share_apps_seen.set(Instant::now()); | ||
| 750 | let settings = &yumush.settings.share; | 806 | let settings = &yumush.settings.share; |
| 751 | 807 | ||
| 752 | match &settings.audio { | 808 | match &settings.audio { |
| 753 | ShareAudio::Off => None, | 809 | ShareAudio::Off => None, |
| 810 | + ShareAudio::Everything if cfg!(target_os = "windows") => None, | ||
| 754 | ShareAudio::Everything => { | 811 | ShareAudio::Everything => { |
| 755 | let apps: Vec<&AudioApp> = yumush | 812 | let apps: Vec<&AudioApp> = yumush |
| 756 | .share_apps | 813 | .share_apps |
| @@ -760,20 +817,14 @@ fn audio_list(entity: &Entity<Yumush>, yumush: &Yumush, palette: Palette) -> Opt | |||
| 760 | (!apps.is_empty()).then(|| { | 817 | (!apps.is_empty()).then(|| { |
| 761 | let rows = div().flex().flex_col().gap(px(2.)).children( | 818 | let rows = div().flex().flex_col().gap(px(2.)).children( |
| 762 | apps.into_iter() | 819 | apps.into_iter() |
| 763 | - .map(|app| exclude_row(entity, settings, app)), | 820 | + .map(|app| include_row(entity, settings, app, palette)), |
| 764 | ); | 821 | ); |
| 765 | field("Include", app_list(rows), palette).into_any_element() | 822 | field("Include", app_list(rows), palette).into_any_element() |
| 766 | }) | 823 | }) |
| 767 | } | 824 | } |
| 768 | - ShareAudio::Source(selected) => Some( | 825 | + audio => { |
| 769 | - app_list(pick_list( | 826 | + Some(app_list(pick_list(entity, audio, &yumush.share_apps, palette)).into_any_element()) |
| 770 | - entity, | 827 | + } |
| 771 | - selected.as_str(), | ||
| 772 | - &yumush.share_apps, | ||
| 773 | - palette, | ||
| 774 | - )) | ||
| 775 | - .into_any_element(), | ||
| 776 | - ), | ||
| 777 | } | 828 | } |
| 778 | } | 829 | } |
| 779 | 830 | ||
| @@ -785,7 +836,12 @@ fn app_list(rows: impl IntoElement) -> impl IntoElement { | |||
| 785 | .child(rows) | 836 | .child(rows) |
| 786 | } | 837 | } |
| 787 | 838 | ||
| 788 | -fn pick_list(entity: &Entity<Yumush>, selected: &str, apps: &[AudioApp], palette: Palette) -> Div { | 839 | +fn pick_list( |
| 840 | + entity: &Entity<Yumush>, | ||
| 841 | + audio: &ShareAudio, | ||
| 842 | + apps: &[AudioApp], | ||
| 843 | + palette: Palette, | ||
| 844 | +) -> Div { | ||
| 789 | let groups = [ | 845 | let groups = [ |
| 790 | (AudioKind::App, "Apps"), | 846 | (AudioKind::App, "Apps"), |
| 791 | (AudioKind::Output, "Outputs"), | 847 | (AudioKind::Output, "Outputs"), |
| @@ -800,7 +856,7 @@ fn pick_list(entity: &Entity<Yumush>, selected: &str, apps: &[AudioApp], palette | |||
| 800 | let rows: Vec<_> = apps | 856 | let rows: Vec<_> = apps |
| 801 | .iter() | 857 | .iter() |
| 802 | .filter(|app| app.kind == kind) | 858 | .filter(|app| app.kind == kind) |
| 803 | - .map(|app| pick_row(entity, selected == app.node, app, palette)) | 859 | + .map(|app| pick_row(entity, chosen(audio, app), app, palette)) |
| 804 | .collect(); | 860 | .collect(); |
| 805 | (!rows.is_empty()).then(|| group(label, rows, palette)) | 861 | (!rows.is_empty()).then(|| group(label, rows, palette)) |
| 806 | })) | 862 | })) |
| @@ -844,28 +900,42 @@ fn row_style<E: Styled>(item: E) -> E { | |||
| 844 | .rounded(px(6.)) | 900 | .rounded(px(6.)) |
| 845 | } | 901 | } |
| 846 | 902 | ||
| 847 | -fn name_cell(name: String) -> Div { | 903 | +fn name_cell(app: &AudioApp, palette: Palette) -> Div { |
| 848 | - div().flex_1().min_w_0().truncate().child(name) | 904 | + div() |
| 905 | + .flex_1() | ||
| 906 | + .min_w_0() | ||
| 907 | + .flex() | ||
| 908 | + .items_baseline() | ||
| 909 | + .gap(px(6.)) | ||
| 910 | + .child(div().flex_none().child(app.name.clone())) | ||
| 911 | + .child( | ||
| 912 | + div() | ||
| 913 | + .min_w_0() | ||
| 914 | + .truncate() | ||
| 915 | + .text_size(text(11.)) | ||
| 916 | + .text_color(palette.muted) | ||
| 917 | + .child(app.detail.clone()), | ||
| 918 | + ) | ||
| 849 | } | 919 | } |
| 850 | 920 | ||
| 851 | -fn exclude_row( | 921 | +fn include_row( |
| 852 | entity: &Entity<Yumush>, | 922 | entity: &Entity<Yumush>, |
| 853 | settings: &ShareSettings, | 923 | settings: &ShareSettings, |
| 854 | app: &AudioApp, | 924 | app: &AudioApp, |
| 925 | + palette: Palette, | ||
| 855 | ) -> impl IntoElement { | 926 | ) -> impl IntoElement { |
| 856 | let entity = entity.clone(); | 927 | let entity = entity.clone(); |
| 857 | - let name = app.name.clone(); | 928 | + let included = !is_excluded(&settings.excluded, &app.name, app.pid); |
| 858 | - let included = !settings.excluded.contains(&app.name); | 929 | + let target = app.clone(); |
| 859 | 930 | ||
| 860 | - row_style(div()).child(name_cell(app.name.clone())).child( | 931 | + row_style(div()).child(name_cell(app, palette)).child( |
| 861 | div().flex_shrink_0().child( | 932 | div().flex_shrink_0().child( |
| 862 | - Switch::new(format!("share_app_{}", app.node)) | 933 | + Switch::new(format!("share_app_{}", app.key())) |
| 863 | .checked(included) | 934 | .checked(included) |
| 864 | .on_change(move |checked, _, cx| { | 935 | .on_change(move |checked, _, cx| { |
| 865 | - let name = name.clone(); | ||
| 866 | let included = *checked; | 936 | let included = *checked; |
| 867 | entity.update(cx, |yumush, cx| { | 937 | entity.update(cx, |yumush, cx| { |
| 868 | - yumush.set_share_excluded(name, !included, cx) | 938 | + yumush.set_share_included(&target, included, cx) |
| 869 | }); | 939 | }); |
| 870 | }), | 940 | }), |
| 871 | ), | 941 | ), |
| @@ -879,17 +949,16 @@ fn pick_row( | |||
| 879 | palette: Palette, | 949 | palette: Palette, |
| 880 | ) -> impl IntoElement { | 950 | ) -> impl IntoElement { |
| 881 | let entity = entity.clone(); | 951 | let entity = entity.clone(); |
| 882 | - let node = app.node.clone(); | 952 | + let target = app.clone(); |
| 883 | 953 | ||
| 884 | row_style(clickable( | 954 | row_style(clickable( |
| 885 | - format!("share_app_{}", app.node), | 955 | + format!("share_app_{}", app.key()), |
| 886 | palette, | 956 | palette, |
| 887 | move |_, cx| { | 957 | move |_, cx| { |
| 888 | - let node = node.clone(); | 958 | + entity.update(cx, |yumush, cx| yumush.set_share_source(&target, cx)); |
| 889 | - entity.update(cx, |yumush, cx| yumush.set_share_source(node, cx)); | ||
| 890 | }, | 959 | }, |
| 891 | )) | 960 | )) |
| 892 | - .child(name_cell(app.name.clone())) | 961 | + .child(name_cell(app, palette)) |
| 893 | .children(chosen.then(|| { | 962 | .children(chosen.then(|| { |
| 894 | Icon::new(IconName::Check) | 963 | Icon::new(IconName::Check) |
| 895 | .w(px(14.)) | 964 | .w(px(14.)) |
crates/client/src/settings.rsmodified+23 -5
| @@ -135,7 +135,8 @@ impl Default for VoiceSettings { | |||
| 135 | pub enum ShareAudio { | 135 | pub enum ShareAudio { |
| 136 | Off, | 136 | Off, |
| 137 | Everything, | 137 | Everything, |
| 138 | - Source(String), | 138 | + App { name: String, pid: Option<u32> }, |
| 139 | + Device(String), | ||
| 139 | } | 140 | } |
| 140 | 141 | ||
| 141 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 142 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -197,7 +198,11 @@ impl ShareSettings { | |||
| 197 | ShareAudio::Everything => Some(AudioMode::Everything { | 198 | ShareAudio::Everything => Some(AudioMode::Everything { |
| 198 | excluded: self.excluded.clone(), | 199 | excluded: self.excluded.clone(), |
| 199 | }), | 200 | }), |
| 200 | - ShareAudio::Source(name) => Some(AudioMode::Source(name.clone())), | 201 | + ShareAudio::App { name, pid } => Some(AudioMode::App { |
| 202 | + name: name.clone(), | ||
| 203 | + pid: *pid, | ||
| 204 | + }), | ||
| 205 | + ShareAudio::Device(node) => Some(AudioMode::Device(node.clone())), | ||
| 201 | } | 206 | } |
| 202 | } | 207 | } |
| 203 | } | 208 | } |
| @@ -301,8 +306,11 @@ pub fn load() -> Settings { | |||
| 301 | .map_or(ShareCodec::Auto, ShareCodec::Only), | 306 | .map_or(ShareCodec::Auto, ShareCodec::Only), |
| 302 | audio: match text("share.audio").as_deref() { | 307 | audio: match text("share.audio").as_deref() { |
| 303 | Some("off") => ShareAudio::Off, | 308 | Some("off") => ShareAudio::Off, |
| 309 | + Some("app") => text("share.audio_source").map_or(ShareAudio::Everything, |name| { | ||
| 310 | + ShareAudio::App { name, pid: None } | ||
| 311 | + }), | ||
| 304 | Some("source") => { | 312 | Some("source") => { |
| 305 | - text("share.audio_source").map_or(ShareAudio::Everything, ShareAudio::Source) | 313 | + text("share.audio_source").map_or(ShareAudio::Everything, ShareAudio::Device) |
| 306 | } | 314 | } |
| 307 | _ => ShareAudio::Everything, | 315 | _ => ShareAudio::Everything, |
| 308 | }, | 316 | }, |
| @@ -336,7 +344,8 @@ pub fn save(settings: &Settings) { | |||
| 336 | let (audio, source) = match &share.audio { | 344 | let (audio, source) = match &share.audio { |
| 337 | ShareAudio::Off => ("off", ""), | 345 | ShareAudio::Off => ("off", ""), |
| 338 | ShareAudio::Everything => ("everything", ""), | 346 | ShareAudio::Everything => ("everything", ""), |
| 339 | - ShareAudio::Source(name) => ("source", name.as_str()), | 347 | + ShareAudio::App { name, .. } => ("app", name.as_str()), |
| 348 | + ShareAudio::Device(node) => ("source", node.as_str()), | ||
| 340 | }; | 349 | }; |
| 341 | let content = [ | 350 | let content = [ |
| 342 | line("theme", settings.theme.as_str()), | 351 | line("theme", settings.theme.as_str()), |
| @@ -376,7 +385,16 @@ pub fn save(settings: &Settings) { | |||
| 376 | ), | 385 | ), |
| 377 | line("share.audio", audio), | 386 | line("share.audio", audio), |
| 378 | line("share.audio_source", source), | 387 | line("share.audio_source", source), |
| 379 | - line("share.excluded", share.excluded.join("\t")), | 388 | + line( |
| 389 | + "share.excluded", | ||
| 390 | + share | ||
| 391 | + .excluded | ||
| 392 | + .iter() | ||
| 393 | + .filter(|entry| !entry.contains('#')) | ||
| 394 | + .cloned() | ||
| 395 | + .collect::<Vec<_>>() | ||
| 396 | + .join("\t"), | ||
| 397 | + ), | ||
| 380 | line("share.volume", share.volume), | 398 | line("share.volume", share.volume), |
| 381 | ] | 399 | ] |
| 382 | .concat(); | 400 | .concat(); |
crates/client/src/share/audio.rsmodified+26 -1
| @@ -1,7 +1,8 @@ | |||
| 1 | #[derive(Debug, Clone, PartialEq, Eq)] | 1 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 2 | pub enum AudioMode { | 2 | pub enum AudioMode { |
| 3 | Everything { excluded: Vec<String> }, | 3 | Everything { excluded: Vec<String> }, |
| 4 | - Source(String), | 4 | + App { name: String, pid: Option<u32> }, |
| 5 | + Device(String), | ||
| 5 | } | 6 | } |
| 6 | 7 | ||
| 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -14,6 +15,30 @@ pub enum AudioKind { | |||
| 14 | #[derive(Debug, Clone)] | 15 | #[derive(Debug, Clone)] |
| 15 | pub struct AudioApp { | 16 | pub struct AudioApp { |
| 16 | pub name: String, | 17 | pub name: String, |
| 18 | + pub detail: String, | ||
| 17 | pub node: String, | 19 | pub node: String, |
| 20 | + pub pid: Option<u32>, | ||
| 18 | pub kind: AudioKind, | 21 | pub kind: AudioKind, |
| 19 | } | 22 | } |
| 23 | + | ||
| 24 | +impl AudioApp { | ||
| 25 | + pub fn key(&self) -> String { | ||
| 26 | + instance_key(&self.name, self.pid) | ||
| 27 | + } | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +pub fn instance_key(name: &str, pid: Option<u32>) -> String { | ||
| 31 | + match pid { | ||
| 32 | + Some(pid) => format!("{name}#{pid}"), | ||
| 33 | + None => name.to_string(), | ||
| 34 | + } | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +pub fn is_excluded(excluded: &[String], name: &str, pid: Option<u32>) -> bool { | ||
| 38 | + excluded.iter().any(|entry| *entry == name) | ||
| 39 | + || pid.is_some_and(|pid| excluded.contains(&instance_key(name, Some(pid)))) | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +pub fn is_picked(picked: (&str, Option<u32>), name: &str, pid: Option<u32>) -> bool { | ||
| 43 | + picked.0 == name && picked.1.is_none_or(|picked| Some(picked) == pid) | ||
| 44 | +} | ||
crates/client/src/share/audio_capture.rsmodified+62 -23
| @@ -1,6 +1,6 @@ | |||
| 1 | use std::{ | 1 | use std::{ |
| 2 | cell::{Cell, RefCell}, | 2 | cell::{Cell, RefCell}, |
| 3 | - collections::{HashMap, HashSet}, | 3 | + collections::{BTreeMap, HashMap, HashSet}, |
| 4 | error::Error, | 4 | error::Error, |
| 5 | process, | 5 | process, |
| 6 | rc::Rc, | 6 | rc::Rc, |
| @@ -23,7 +23,7 @@ use pw::{ | |||
| 23 | use spa::{pod::Pod, utils::dict::DictRef}; | 23 | use spa::{pod::Pod, utils::dict::DictRef}; |
| 24 | use tracing::warn; | 24 | use tracing::warn; |
| 25 | 25 | ||
| 26 | -use super::audio::{AudioApp, AudioKind, AudioMode}; | 26 | +use super::audio::{AudioApp, AudioKind, AudioMode, is_excluded, is_picked}; |
| 27 | use crate::voice::audio::{Worker, supervise}; | 27 | use crate::voice::audio::{Worker, supervise}; |
| 28 | 28 | ||
| 29 | const STOP_CHECK: Duration = Duration::from_millis(100); | 29 | const STOP_CHECK: Duration = Duration::from_millis(100); |
| @@ -52,6 +52,13 @@ struct Node { | |||
| 52 | role: String, | 52 | role: String, |
| 53 | process: String, | 53 | process: String, |
| 54 | app: String, | 54 | app: String, |
| 55 | + media: String, | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +impl Node { | ||
| 59 | + fn pid(&self) -> Option<u32> { | ||
| 60 | + self.process.parse().ok() | ||
| 61 | + } | ||
| 55 | } | 62 | } |
| 56 | 63 | ||
| 57 | struct Port { | 64 | struct Port { |
| @@ -69,20 +76,43 @@ struct Routing { | |||
| 69 | } | 76 | } |
| 70 | 77 | ||
| 71 | pub fn apps() -> Vec<AudioApp> { | 78 | pub fn apps() -> Vec<AudioApp> { |
| 72 | - let mut apps: Vec<AudioApp> = nodes() | 79 | + let nodes = nodes().unwrap_or_default(); |
| 73 | - .unwrap_or_default() | 80 | + let own = process::id().to_string(); |
| 81 | + let mut instances: BTreeMap<(String, Option<u32>), Vec<String>> = BTreeMap::new(); | ||
| 82 | + let mut devices = Vec::new(); | ||
| 83 | + for node in nodes | ||
| 74 | .into_iter() | 84 | .into_iter() |
| 75 | - .filter(|node| !node.name.starts_with(OWN_PREFIX)) | 85 | + .filter(|node| !node.name.starts_with(OWN_PREFIX) && node.process != own) |
| 76 | - .filter_map(|node| { | 86 | + { |
| 77 | - Some(AudioApp { | 87 | + match AudioKind::of(&node.class) { |
| 78 | - kind: AudioKind::of(&node.class)?, | 88 | + Some(AudioKind::App) => { |
| 89 | + let media = instances.entry((node.app.clone(), node.pid())).or_default(); | ||
| 90 | + if !node.media.is_empty() && !media.contains(&node.media) { | ||
| 91 | + media.push(node.media); | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | + Some(kind) => devices.push(AudioApp { | ||
| 79 | name: node.app, | 95 | name: node.app, |
| 96 | + detail: String::new(), | ||
| 80 | node: node.name, | 97 | node: node.name, |
| 81 | - }) | 98 | + pid: None, |
| 99 | + kind, | ||
| 100 | + }), | ||
| 101 | + None => {} | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + let mut apps: Vec<AudioApp> = instances | ||
| 105 | + .into_iter() | ||
| 106 | + .map(|((name, pid), media)| AudioApp { | ||
| 107 | + detail: media.join(", "), | ||
| 108 | + node: name.clone(), | ||
| 109 | + name, | ||
| 110 | + pid, | ||
| 111 | + kind: AudioKind::App, | ||
| 82 | }) | 112 | }) |
| 83 | .collect(); | 113 | .collect(); |
| 84 | - apps.sort_by(|a, b| (a.kind as u8, &a.name).cmp(&(b.kind as u8, &b.name))); | 114 | + devices.sort_by(|a, b| (a.kind as u8, &a.name).cmp(&(b.kind as u8, &b.name))); |
| 85 | - apps.dedup_by(|a, b| a.kind == AudioKind::App && a.kind == b.kind && a.name == b.name); | 115 | + apps.extend(devices); |
| 86 | apps | 116 | apps |
| 87 | } | 117 | } |
| 88 | 118 | ||
| @@ -90,11 +120,11 @@ pub fn start(mode: AudioMode, output: rtrb::Producer<f32>) -> AudioCapture { | |||
| 90 | Worker::spawn("share-audio", move |stop| { | 120 | Worker::spawn("share-audio", move |stop| { |
| 91 | let output = Rc::new(RefCell::new(output)); | 121 | let output = Rc::new(RefCell::new(output)); |
| 92 | let is_sink = match &mode { | 122 | let is_sink = match &mode { |
| 93 | - AudioMode::Everything { .. } => false, | 123 | + AudioMode::Device(name) => nodes() |
| 94 | - AudioMode::Source(name) => nodes() | ||
| 95 | .unwrap_or_default() | 124 | .unwrap_or_default() |
| 96 | .iter() | 125 | .iter() |
| 97 | .any(|node| &node.name == name && node.class == "Audio/Sink"), | 126 | .any(|node| &node.name == name && node.class == "Audio/Sink"), |
| 127 | + _ => false, | ||
| 98 | }; | 128 | }; |
| 99 | supervise("share audio", stop, || run(&mode, &output, is_sink, stop)); | 129 | supervise("share audio", stop, || run(&mode, &output, is_sink, stop)); |
| 100 | }) | 130 | }) |
| @@ -185,6 +215,10 @@ fn node_of(global: &GlobalObject<&DictRef>) -> Option<Node> { | |||
| 185 | .get(*pw::keys::APP_NAME) | 215 | .get(*pw::keys::APP_NAME) |
| 186 | .unwrap_or(description) | 216 | .unwrap_or(description) |
| 187 | .to_string(), | 217 | .to_string(), |
| 218 | + media: props | ||
| 219 | + .get(*pw::keys::MEDIA_NAME) | ||
| 220 | + .unwrap_or_default() | ||
| 221 | + .to_string(), | ||
| 188 | }) | 222 | }) |
| 189 | } | 223 | } |
| 190 | 224 | ||
| @@ -215,7 +249,7 @@ impl Routing { | |||
| 215 | self.ports.remove(&id); | 249 | self.ports.remove(&id); |
| 216 | } | 250 | } |
| 217 | 251 | ||
| 218 | - fn sync(&mut self, own: u32, excluded: &[String], core: &CoreRc) { | 252 | + fn sync(&mut self, own: u32, mode: &AudioMode, core: &CoreRc) { |
| 219 | self.links.retain(|(output, input), _| { | 253 | self.links.retain(|(output, input), _| { |
| 220 | self.ports.contains_key(output) && self.ports.contains_key(input) | 254 | self.ports.contains_key(output) && self.ports.contains_key(input) |
| 221 | }); | 255 | }); |
| @@ -225,7 +259,7 @@ impl Routing { | |||
| 225 | let eligible = self | 259 | let eligible = self |
| 226 | .streams | 260 | .streams |
| 227 | .get(&output.node) | 261 | .get(&output.node) |
| 228 | - .is_some_and(|node| eligible(node, excluded)); | 262 | + .is_some_and(|node| eligible(node, mode)); |
| 229 | if !eligible { | 263 | if !eligible { |
| 230 | continue; | 264 | continue; |
| 231 | } | 265 | } |
| @@ -258,13 +292,18 @@ impl Routing { | |||
| 258 | } | 292 | } |
| 259 | } | 293 | } |
| 260 | 294 | ||
| 261 | -fn eligible(node: &Node, excluded: &[String]) -> bool { | 295 | +fn eligible(node: &Node, mode: &AudioMode) -> bool { |
| 262 | let own_process = process::id().to_string(); | 296 | let own_process = process::id().to_string(); |
| 263 | - !(SKIPPED_ROLES.contains(&node.role.as_str()) | 297 | + let foreign = !(SKIPPED_ROLES.contains(&node.role.as_str()) |
| 264 | || node.process == own_process | 298 | || node.process == own_process |
| 265 | || node.name.starts_with(OWN_PREFIX) | 299 | || node.name.starts_with(OWN_PREFIX) |
| 266 | - || node.name.contains("echo-cancel") | 300 | + || node.name.contains("echo-cancel")); |
| 267 | - || excluded.contains(&node.app)) | 301 | + foreign |
| 302 | + && match mode { | ||
| 303 | + AudioMode::Everything { excluded } => !is_excluded(excluded, &node.app, node.pid()), | ||
| 304 | + AudioMode::App { name, pid } => is_picked((name, *pid), &node.app, node.pid()), | ||
| 305 | + AudioMode::Device(_) => false, | ||
| 306 | + } | ||
| 268 | } | 307 | } |
| 269 | 308 | ||
| 270 | fn matches_channel(output: &Port, input: &Port) -> bool { | 309 | fn matches_channel(output: &Port, input: &Port) -> bool { |
| @@ -354,8 +393,8 @@ fn run( | |||
| 354 | .register(); | 393 | .register(); |
| 355 | 394 | ||
| 356 | let target = match mode { | 395 | let target = match mode { |
| 357 | - AudioMode::Everything { .. } => None, | 396 | + AudioMode::Device(name) => Some(name.as_str()), |
| 358 | - AudioMode::Source(name) => Some(name.as_str()), | 397 | + _ => None, |
| 359 | }; | 398 | }; |
| 360 | 399 | ||
| 361 | let mut props = properties! { | 400 | let mut props = properties! { |
| @@ -411,8 +450,8 @@ fn run( | |||
| 411 | if failed.get() { | 450 | if failed.get() { |
| 412 | return Err("pipewire connection lost".into()); | 451 | return Err("pipewire connection lost".into()); |
| 413 | } | 452 | } |
| 414 | - if let AudioMode::Everything { excluded } = mode { | 453 | + if target.is_none() { |
| 415 | - routing.borrow_mut().sync(stream.node_id(), excluded, &core); | 454 | + routing.borrow_mut().sync(stream.node_id(), mode, &core); |
| 416 | } | 455 | } |
| 417 | mainloop.loop_().iterate(Timeout::Finite(STOP_CHECK)); | 456 | mainloop.loop_().iterate(Timeout::Finite(STOP_CHECK)); |
| 418 | } | 457 | } |
crates/client/src/share/audio_capture_windows.rsmodified+25 -8
| @@ -14,7 +14,7 @@ use wasapi::{ | |||
| 14 | StreamMode, WaveFormat, deinitialize, initialize_mta, | 14 | StreamMode, WaveFormat, deinitialize, initialize_mta, |
| 15 | }; | 15 | }; |
| 16 | 16 | ||
| 17 | -use super::audio::{AudioApp, AudioKind, AudioMode}; | 17 | +use super::audio::{AudioApp, AudioKind, AudioMode, is_picked}; |
| 18 | use crate::voice::audio::{Worker, supervise}; | 18 | use crate::voice::audio::{Worker, supervise}; |
| 19 | 19 | ||
| 20 | const STOP_CHECK: Duration = Duration::from_millis(100); | 20 | const STOP_CHECK: Duration = Duration::from_millis(100); |
| @@ -64,7 +64,7 @@ fn sessions() -> Result<Vec<AudioApp>, Box<dyn Error>> { | |||
| 64 | } | 64 | } |
| 65 | let name = session.get_display_name().unwrap_or_default(); | 65 | let name = session.get_display_name().unwrap_or_default(); |
| 66 | let name = if name.is_empty() { | 66 | let name = if name.is_empty() { |
| 67 | - format!("pid {pid}") | 67 | + process_name(pid).unwrap_or_else(|| format!("pid {pid}")) |
| 68 | } else { | 68 | } else { |
| 69 | name | 69 | name |
| 70 | }; | 70 | }; |
| @@ -74,14 +74,28 @@ fn sessions() -> Result<Vec<AudioApp>, Box<dyn Error>> { | |||
| 74 | .into_iter() | 74 | .into_iter() |
| 75 | .map(|(pid, name)| AudioApp { | 75 | .map(|(pid, name)| AudioApp { |
| 76 | name, | 76 | name, |
| 77 | + detail: format!("pid {pid}"), | ||
| 77 | node: pid.to_string(), | 78 | node: pid.to_string(), |
| 79 | + pid: Some(pid), | ||
| 78 | kind: AudioKind::App, | 80 | kind: AudioKind::App, |
| 79 | }) | 81 | }) |
| 80 | .collect(); | 82 | .collect(); |
| 81 | - apps.sort_by(|a, b| a.name.cmp(&b.name)); | 83 | + apps.sort_by(|a, b| (&a.name, a.pid).cmp(&(&b.name, b.pid))); |
| 82 | Ok(apps) | 84 | Ok(apps) |
| 83 | } | 85 | } |
| 84 | 86 | ||
| 87 | +fn process_name(pid: u32) -> Option<String> { | ||
| 88 | + let pid = sysinfo::Pid::from_u32(pid); | ||
| 89 | + let mut system = sysinfo::System::new().ok()?; | ||
| 90 | + system.refresh_processes_specifics( | ||
| 91 | + sysinfo::ProcessesToUpdate::Some(&[pid]), | ||
| 92 | + false, | ||
| 93 | + sysinfo::ProcessRefreshKind::nothing(), | ||
| 94 | + ); | ||
| 95 | + let name = system.process(pid)?.name().to_string_lossy().into_owned(); | ||
| 96 | + Some(name.trim_end_matches(".exe").to_string()) | ||
| 97 | +} | ||
| 98 | + | ||
| 85 | pub fn start(mode: AudioMode, output: Producer<f32>) -> AudioCapture { | 99 | pub fn start(mode: AudioMode, output: Producer<f32>) -> AudioCapture { |
| 86 | Worker::spawn("share-audio", move |stop| { | 100 | Worker::spawn("share-audio", move |stop| { |
| 87 | with_com(|| { | 101 | with_com(|| { |
| @@ -111,12 +125,15 @@ fn run( | |||
| 111 | AudioMode::Everything { .. } => { | 125 | AudioMode::Everything { .. } => { |
| 112 | AudioClient::new_application_loopback_client(process::id(), false)? | 126 | AudioClient::new_application_loopback_client(process::id(), false)? |
| 113 | } | 127 | } |
| 114 | - AudioMode::Source(node) => { | 128 | + AudioMode::App { name, pid } => { |
| 115 | - if !sessions()?.iter().any(|app| &app.node == node) { | 129 | + let target = sessions()? |
| 116 | - return Err("share audio app is not running".into()); | 130 | + .into_iter() |
| 117 | - } | 131 | + .find(|app| is_picked((name, *pid), &app.name, app.pid)) |
| 118 | - AudioClient::new_application_loopback_client(node.parse()?, true)? | 132 | + .and_then(|app| app.pid) |
| 133 | + .ok_or("share audio app is not running")?; | ||
| 134 | + AudioClient::new_application_loopback_client(target, true)? | ||
| 119 | } | 135 | } |
| 136 | + AudioMode::Device(_) => return Err("share audio device capture is not supported".into()), | ||
| 120 | }; | 137 | }; |
| 121 | client.initialize_client( | 138 | client.initialize_client( |
| 122 | &wave_format(), | 139 | &wave_format(), |