diff --git a/crates/voelin-ui/src/app.rs b/crates/voelin-ui/src/app.rs index f6aab9a..d0d26b3 100644 --- a/crates/voelin-ui/src/app.rs +++ b/crates/voelin-ui/src/app.rs @@ -735,6 +735,9 @@ impl App { self.playback = self.prefs.get(&CLIENT_PLAYBACK); } else if appearance_keys().iter().any(|k| k.name() == key) { self.apply_appearance(); + } else if key == voelin_core::settings::STREAM_AUDIO_SOURCES.name() { + // A running share mixes the new sources (streams.rs). + self.share_audio_changed(); } } diff --git a/crates/voelin-ui/src/dev.rs b/crates/voelin-ui/src/dev.rs index d51be49..6bbc6af 100644 --- a/crates/voelin-ui/src/dev.rs +++ b/crates/voelin-ui/src/dev.rs @@ -152,7 +152,13 @@ pub(crate) fn start(ui: &MainWindow, switches: &Switches) -> Running { "server" => nav.invoke_show(Page::Server), "settings" => nav.invoke_open_settings(section(arg)), "about" => nav.invoke_open_about(), - "share" => nav.invoke_open_share(), + "share" => { + nav.invoke_open_share(); + // The sample share of the test pattern, live at once. + if arg == "live" && switches.demo_ui { + with_app(|app| app.demo_share()); + } + } "bookmark" => nav.invoke_add_server(), "emoji" => nav.set_emoji_open(true), "panel" => nav.set_right_panel_open(true), diff --git a/crates/voelin-ui/src/streams.rs b/crates/voelin-ui/src/streams.rs index 19460f6..8e3bca3 100644 --- a/crates/voelin-ui/src/streams.rs +++ b/crates/voelin-ui/src/streams.rs @@ -2,16 +2,22 @@ //! viewer. The engine runs the stream sessions; `video.rs` captures, //! encodes and decodes. +use std::time::Duration; + use slint::{ComponentHandle, Model}; use tracing::warn; -use voelin_core::media::audio_source_specs; -use voelin_core::settings::{STREAM_AUDIO_SOURCES, STREAM_BITRATE_KBPS, STREAM_FPS}; +use voelin_core::media::{Streamer, audio_source_specs}; +use voelin_core::settings::{ + AudioSourceSetting, STREAM_AUDIO_SOURCES, STREAM_BITRATE_KBPS, STREAM_FPS, +}; use voelin_core::stream::{ EndReason, LayerId, LayerSpec, LeaveReason, StreamKind, StreamSetup, ViewerInfo, ViewerState, }; use voelin_core::{Command, Event, StreamState, WatchState}; -use crate::app::{App, Bridge, ShareForm, SourceItem, StreamItem, ViewerItem, later, model}; +use crate::app::{ + App, Bridge, ShareForm, SourceItem, StreamItem, ViewerItem, later, model, with_app, +}; use crate::settings::{ BITRATE_CHOICES, FPS_CHOICES, ShareDefaults, nearest_choice, parse_positive, }; @@ -27,6 +33,46 @@ pub(crate) struct Share { viewers: Vec, /// The capture ended by itself and the stream is being stopped. stopping: bool, + /// The audio mixer's meters (the studio's rows), about 15 times a second. + _meters: slint::Timer, +} + +/// What the share dialog asks of the capture: the audio mixer's `sources` +/// (`stream.audio_sources`) while "Share system audio" is on; off, or with +/// no sources, no audio at all. +fn capture_request( + fps: u32, + bitrate_kbps: u32, + audio: bool, + sources: &[AudioSourceSetting], + restore_token: Option, +) -> CaptureRequest { + let audio_sources = if audio { audio_source_specs(sources) } else { Vec::new() }; + CaptureRequest { + fps, + bitrate_kbps, + audio: !audio_sources.is_empty(), + audio_sources, + restore_token, + } +} + +/// The viewers of the sample share (VOELIN_DEMO_UI): two watch, one asks. +fn demo_viewers() -> Vec { + let viewer = |id, state, message: &str| ViewerInfo { + client: tsclientlib::ClientId(id), + state, + message: message.into(), + layer: None, + estimate: None, + srtp_profile: None, + codec: None, + }; + vec![ + viewer(3, ViewerState::Connected, ""), + viewer(5, ViewerState::Connected, ""), + viewer(4, ViewerState::Requested, "Can I watch?"), + ] } /// The stream in the viewer. @@ -171,6 +217,7 @@ impl App { } else { share_end_text(&reason) }); + self.mixer_refresh(); } } self.refresh_streams(); @@ -287,6 +334,7 @@ impl App { bridge.set_share_state(state.into()); bridge.set_share_busy(self.share_busy); bridge.set_share_error(self.share_error.clone().into()); + bridge.set_share_audio(share.is_some_and(|s| s.capture.has_audio())); let viewers: Vec = share .map(|s| { s.viewers @@ -416,6 +464,8 @@ impl App { if self.share.is_some() { self.refresh_streams(); } + // The share dialog's audio mixer (studio.rs). + self.mixer_tick(); if self.watch.is_some() { self.refresh_viewer(); } @@ -449,6 +499,8 @@ impl App { ui.global::().set_share_sources(model(items)); } self.refresh_streams(); + // Its audio: the Stream Studio's mixer rows and picker. + self.mixer_show(); let defaults = &self.settings.share; let nickname = self.current.and_then(|id| self.bookmark(id)).map(|b| b.nickname.clone()); let (fps, bitrate) = (self.prefs.get(&STREAM_FPS), self.prefs.get(&STREAM_BITRATE_KBPS)); @@ -467,21 +519,34 @@ impl App { } } + /// The share dialog's form with the test pattern chosen. + fn test_pattern_form(&mut self) -> Option { + let mut form = self.open_share(); + let ui = self.ui.upgrade()?; + let sources = ui.global::().get_share_sources(); + let pattern = (0..sources.row_count()) + .find(|&i| sources.row_data(i).is_some_and(|s| s.name == "Test pattern"))?; + form.source = pattern as i32; + Some(form) + } + /// Development switch `VOELIN_AUTOSHARE`: share the test pattern once. pub(crate) fn autoshare(&mut self) { if !self.autoshare || !self.view().is_some_and(|v| v.streams_available()) { return; } self.autoshare = false; - let mut form = self.open_share(); - let Some(ui) = self.ui.upgrade() else { return }; - let sources = ui.global::().get_share_sources(); - let pattern = (0..sources.row_count()) - .find(|&i| sources.row_data(i).is_some_and(|s| s.name == "Test pattern")); - let Some(index) = pattern else { return }; - form.source = index as i32; - form.auto_accept = true; - self.start_share(form); + if let Some(form) = self.test_pattern_form() { + self.start_share(ShareForm { auto_accept: true, ..form }); + } + } + + /// `VOELIN_OPEN=share:live` with sample data: the test pattern shared + /// with the sample mixer's sources (live at once, `capture_ready`). + pub(crate) fn demo_share(&mut self) { + if let Some(form) = self.test_pattern_form() { + self.start_share(ShareForm { audio: true, ..form }); + } } pub(crate) fn start_share(&mut self, form: ShareForm) { @@ -515,15 +580,10 @@ impl App { audio: form.audio, auto_accept: form.auto_accept, }; - // No sources configured: no audio. - let audio_sources = audio_source_specs(&self.prefs.get(&STREAM_AUDIO_SOURCES)); - let request = CaptureRequest { - fps, - bitrate_kbps: bitrate, - audio: form.audio && !audio_sources.is_empty(), - audio_sources, - restore_token: self.settings.portal_restore_token.clone(), - }; + // The mixer's sources (the sample studio's with VOELIN_DEMO_UI). + let sources = self.studio_settings().get(&STREAM_AUDIO_SOURCES); + let token = self.settings.portal_restore_token.clone(); + let request = capture_request(fps, bitrate, form.audio, &sources, token); self.settings.share = defaults; self.store_settings(); self.share_busy = true; @@ -532,7 +592,7 @@ impl App { let setup = StreamSetup { name: form.name.trim().to_owned(), bitrate, - audio: form.audio, + audio: request.audio, ..StreamSetup::default() }; let auto_accept = form.auto_accept; @@ -568,30 +628,64 @@ impl App { let reason = capture.audio_error().unwrap_or_default(); self.set_status(format!("Sharing without sound: {reason}")); } - self.engine.send(Command::StartStream { - session: session as u64, - setup, - auto_accept, + // Sample data has no server: live at once, with viewers. + let (live, viewers) = if self.demo_ui { + (true, demo_viewers()) + } else { + let session = session as u64; + self.engine.send(Command::StartStream { session, setup, auto_accept }); + (false, Vec::new()) + }; + let meters = slint::Timer::default(); + meters.start(slint::TimerMode::Repeated, Duration::from_millis(66), || { + with_app(|app| app.studio_meters()); }); self.share = Some(Share { session, capture, - live: false, - viewers: Vec::new(), + live, + viewers, stopping: false, + _meters: meters, }); + // The mixer's rows follow this capture's sources. + self.mixer_refresh(); } } self.refresh_streams(); } pub(crate) fn stop_share(&mut self) { - if let Some(share) = &mut self.share { - share.stopping = true; - self.engine.send(Command::StopStream { session: share.session as u64 }); + let Some(share) = &mut self.share else { return }; + share.stopping = true; + let session = share.session; + if self.demo_ui { + self.share_state(session, StreamState::Ended(EndReason::Local)); + } else { + self.engine.send(Command::StopStream { session: session as u64 }); } } + /// The running share's streamer (the mixer's rows, studio.rs). + pub(crate) fn share_streamer(&self) -> Option<&Streamer> { + self.share.as_ref().map(|s| s.capture.streamer()) + } + + /// `stream.audio_sources` changed (the mixer, the settings, another + /// window): a running share mixes the new sources. + pub(crate) fn share_audio_changed(&mut self) { + if self.share.is_none() { + return; + } + let sources = audio_source_specs(&self.studio_settings().get(&STREAM_AUDIO_SOURCES)); + let codecs = self.video.codecs(); + let Some(share) = &self.share else { return }; + if let Err(e) = share.capture.follow_audio(&codecs, sources) { + self.set_status(format!("Stream audio: {e}")); + } + self.mixer_refresh(); + } + pub(crate) fn respond_viewer(&mut self, viewer: u16, accept: bool) { if let Some(share) = &self.share { let session = share.session as u64; @@ -802,4 +896,34 @@ mod tests { assert_eq!(super::bitrate_text(8000), "8 Mbit/s"); assert_eq!(super::bitrate_text(4608), "4.6 Mbit/s"); } + + /// What the share dialog's mixer holds goes to the capture, with each + /// source's gain and mute; "Share system audio" off (or nothing picked) + /// shares no audio at all. + #[test] + fn the_share_carries_the_picked_audio() { + use voelin_core::media::{AppMatch, AudioSourceKind}; + use voelin_core::settings::{AudioSourceKindSetting, AudioSourceSetting}; + let app = AudioSourceKindSetting::App { name: Some("firefox".into()), pid: None }; + let picked = [ + AudioSourceSetting { gain: 0.5, ..AudioSourceSetting::new(app) }, + AudioSourceSetting { + muted: true, + ..AudioSourceSetting::new(AudioSourceKindSetting::Microphone) + }, + ]; + let request = super::capture_request(30, 4608, true, &picked, None); + assert!(request.audio); + let kinds: Vec<_> = request.audio_sources.iter().map(|s| s.kind.clone()).collect(); + let firefox = AudioSourceKind::App(AppMatch::Name("firefox".into())); + assert_eq!(kinds, [firefox, AudioSourceKind::Microphone]); + let mix: Vec<_> = request.audio_sources.iter().map(|s| (s.gain, s.muted)).collect(); + assert_eq!(mix, [(0.5, false), (1.0, true)]); + assert_eq!((request.fps, request.bitrate_kbps), (30, 4608)); + + let off = super::capture_request(30, 4608, false, &picked, None); + assert!(!off.audio && off.audio_sources.is_empty()); + let none = super::capture_request(30, 4608, true, &[], None); + assert!(!none.audio && none.audio_sources.is_empty()); + } } diff --git a/crates/voelin-ui/src/studio.rs b/crates/voelin-ui/src/studio.rs index a7e5a6a..cf3cee1 100644 --- a/crates/voelin-ui/src/studio.rs +++ b/crates/voelin-ui/src/studio.rs @@ -28,8 +28,8 @@ use voelin_core::media::voelin_media::handoff::Handoff; use voelin_core::media::voelin_media::mix::SourceHandle; use voelin_core::media::voelin_media::{VideoFrame, convert}; use voelin_core::media::{ - AudioApps, AudioSourceSpec, Latest, Streamer, StreamerConfigUpdate, StreamerStats, audio_apps, - audio_source_specs, screen_sources, + AudioApps, AudioSourceSpec, AudioSourceStats, Latest, Streamer, StreamerConfigUpdate, + StreamerStats, audio_apps, audio_source_specs, screen_sources, }; use voelin_core::settings::{ AudioSourceKindSetting, AudioSourceSetting, Key, Kind, STREAM_AUDIO_SOURCES, @@ -421,8 +421,9 @@ fn file_name(what: &str) -> String { } impl App { - /// The studio's settings (made once). - fn studio_settings(&mut self) -> Settings { + /// The studio's settings (made once): its mixer's `stream.audio_sources` + /// are the share dialog's too. + pub(crate) fn studio_settings(&mut self) -> Settings { let demo = self.demo_ui; let prefs = &self.prefs; self.studio @@ -478,14 +479,19 @@ impl App { || self.ui.upgrade().is_some_and(|ui| ui.global::