`servers.rs`, `chat.rs`, `streams.rs` and `settings_page.rs` of voelin-ui are `servers/`, `chat/`, `streams/` and `settings_page/` since the split (the Slint side in `bind.rs`, the decisions in voelin-ui-vm), and `adopt_server_name` is in the view model's `events`. The comments of `events.rs`, `app.rs`, `studio/bind.rs` and `dev/bind.rs`, and the add-server section of docs/ui.md, named the old files. The "before" names in the table of the `.set_` counts stay: they are the files as they were. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1359 lines
43 KiB
Rust
1359 lines
43 KiB
Rust
//! The Stream Studio on the Slint windows: running the engine's studio (its
|
|
//! tasks, the preview, the encoder), putting the rows and values on both
|
|
//! windows' StudioBridge, and the commands the controls send. What is
|
|
//! decided is `voelin_ui_vm::studio::logic`.
|
|
|
|
use std::path::PathBuf;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::{Arc, Weak};
|
|
use std::time::Duration;
|
|
|
|
use slint::{ComponentHandle, Rgba8Pixel, SharedPixelBuffer};
|
|
use tokio::sync::broadcast::error::RecvError;
|
|
use tokio::sync::{broadcast, mpsc};
|
|
use tracing::warn;
|
|
use voelin_core::media::voelin_media::handoff::Handoff;
|
|
use voelin_core::media::voelin_media::{VideoFrame, convert};
|
|
use voelin_core::media::{
|
|
AudioApps, AudioSourceStats, Latest, Streamer, StreamerConfigUpdate, audio_apps,
|
|
audio_source_specs, screen_sources,
|
|
};
|
|
use voelin_core::settings::{
|
|
AudioSourceSetting, STREAM_AUDIO_SOURCES, STREAM_BITRATE_KBPS, STREAM_LAYERS,
|
|
STUDIO_REPLAY_SECONDS, STUDIO_SCENES, Settings, layer_specs,
|
|
};
|
|
use voelin_core::stream::{EndReason, LayerSpec, ViewerInfo, ViewerState};
|
|
use voelin_core::studio::scene::Source;
|
|
use voelin_core::studio::{self, SourceChange, Studio, camera};
|
|
use voelin_core::{Command, HistorySource, StreamState};
|
|
use voelin_model::ChatTarget;
|
|
|
|
use super::{Op, Picture, Running};
|
|
use crate::app::{App, Nav, Page, StudioBridge, StudioNav, StudioWindow, later, with_app};
|
|
use crate::video::CaptureRequest;
|
|
use crate::vm;
|
|
use crate::vm::bridge::ChatLine;
|
|
use crate::vm::studio::logic::{self, Outgoing, Pick, STUDIO_UI, StudioUi};
|
|
use crate::vm::studio_bridge::{StudioLayer, StudioPick, StudioSettingsForm, StudioSourceForm};
|
|
|
|
/// Forward the studio's events to the UI thread.
|
|
async fn forward_events(mut events: broadcast::Receiver<studio::Event>) {
|
|
loop {
|
|
match events.recv().await {
|
|
Ok(event) => later(move |app| app.studio_event(event)),
|
|
Err(RecvError::Lagged(_)) => {}
|
|
Err(RecvError::Closed) => break,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Apply the ops one after the other.
|
|
async fn run_ops(studio: Weak<Studio>, mut ops: mpsc::UnboundedReceiver<Op>) {
|
|
while let Some(op) = ops.recv().await {
|
|
let Some(studio) = studio.upgrade() else { break };
|
|
let result = match op {
|
|
Op::Apply(command) => studio.apply(command).await,
|
|
Op::Add(source) => {
|
|
let mut scenes = studio.scenes();
|
|
logic::add_to_live_scene(&mut scenes, source);
|
|
studio.apply(studio::Command::SetScenes(Box::new(scenes))).await
|
|
}
|
|
Op::Overlays(wanted) => {
|
|
let mut scenes = studio.scenes();
|
|
if vm::studio::apply_overlays(&mut scenes, &wanted) {
|
|
studio.apply(studio::Command::SetScenes(Box::new(scenes))).await
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
};
|
|
if let Err(e) = result {
|
|
later(move |app| app.set_status(format!("Stream Studio: {e}")));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Tell the UI thread when a key the studio follows changes elsewhere.
|
|
async fn watch_settings(settings: Settings) {
|
|
let mut audio = settings.watch(&STREAM_AUDIO_SOURCES);
|
|
let mut layers = settings.watch(&STREAM_LAYERS);
|
|
let mut bitrate = settings.watch(&STREAM_BITRATE_KBPS);
|
|
let mut ui = settings.watch(&STUDIO_UI);
|
|
loop {
|
|
tokio::select! {
|
|
Some(_) = audio.changed() => later(|app| app.studio_audio_changed()),
|
|
Some(_) = layers.changed() => later(|app| app.studio_encoding_changed()),
|
|
Some(_) = bitrate.changed() => later(|app| app.studio_encoding_changed()),
|
|
Some(_) = ui.changed() => later(|app| app.studio_ui_changed()),
|
|
else => break,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Turn preview frames into pictures on a thread of its own; the UI thread
|
|
/// takes the newest when it gets to it.
|
|
fn spawn_preview(
|
|
handoff: Arc<Handoff<VideoFrame>>,
|
|
pictures: Arc<Latest<Picture>>,
|
|
stop: Arc<AtomicBool>,
|
|
) {
|
|
let spawned =
|
|
std::thread::Builder::new().name("voelin-studio-preview".into()).spawn(move || {
|
|
while !stop.load(Ordering::Relaxed) && !handoff.is_closed() {
|
|
let Some(frame) = handoff.wait_timeout(Duration::from_millis(250)) else {
|
|
continue;
|
|
};
|
|
let mut picture = Picture::new(frame.width, frame.height);
|
|
let stride = frame.width as usize * 4;
|
|
if let Err(e) = convert::to_rgba(&frame, picture.make_mut_bytes(), stride) {
|
|
warn!("studio preview: {e}");
|
|
continue;
|
|
}
|
|
if pictures.put(picture) {
|
|
later(|app| app.studio_picture());
|
|
}
|
|
}
|
|
});
|
|
if let Err(e) = spawned {
|
|
warn!("cannot show the studio's preview: {e}");
|
|
}
|
|
}
|
|
|
|
impl App {
|
|
/// 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
|
|
.data
|
|
.settings
|
|
.get_or_insert_with(|| {
|
|
if demo {
|
|
let (settings, problems) = logic::demo_settings();
|
|
for problem in problems {
|
|
warn!("demo studio: {problem}");
|
|
}
|
|
settings
|
|
} else {
|
|
prefs.clone()
|
|
}
|
|
})
|
|
.clone()
|
|
}
|
|
|
|
fn studio_streamer(&self) -> Option<&Streamer> {
|
|
self.studio.run.as_ref()?.streamer.as_ref()
|
|
}
|
|
|
|
/// The stream's layers for the engine: `stream.layers`, or the single
|
|
/// layer at [`studio_bitrate_kbps`](Self::studio_bitrate_kbps).
|
|
fn studio_session_layers(&self, settings: &Settings) -> Vec<LayerSpec> {
|
|
self.studio.data.session_layers(settings)
|
|
}
|
|
|
|
/// `stream.bitrate_kbps`, an automatic one (0) as it comes to for the
|
|
/// output's size and frame rate.
|
|
fn studio_bitrate_kbps(&self, settings: &Settings) -> u32 {
|
|
self.studio.data.bitrate_kbps(settings)
|
|
}
|
|
|
|
fn studio_op(&self, op: Op) {
|
|
if let Some(run) = &self.studio.run {
|
|
let _ = run.ops.send(op);
|
|
}
|
|
}
|
|
|
|
/// `f` on the StudioBridge of the main window and of the studio window.
|
|
fn studio_each(&self, f: impl Fn(&StudioBridge)) {
|
|
if let Some(ui) = self.ui.upgrade() {
|
|
f(&ui.global::<StudioBridge>());
|
|
}
|
|
if let Some(window) = &self.studio.window {
|
|
f(&window.global::<StudioBridge>());
|
|
}
|
|
}
|
|
|
|
/// The StudioNav of the window the studio is in.
|
|
fn studio_nav(&self, f: impl FnOnce(&StudioNav)) {
|
|
match (&self.studio.window, self.ui.upgrade()) {
|
|
(Some(window), _) => f(&window.global::<StudioNav>()),
|
|
(None, Some(ui)) => f(&ui.global::<StudioNav>()),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn studio_attach(&self, bridge: &StudioBridge) {
|
|
let m = &self.studio.models;
|
|
bridge.set_scenes(m.scenes.model());
|
|
bridge.set_sources(m.sources.model());
|
|
bridge.set_audio(m.audio.model());
|
|
bridge.set_layers(m.layers.model());
|
|
bridge.set_viewers(m.viewers.model());
|
|
bridge.set_destinations(m.destinations.model());
|
|
bridge.set_picks(m.picks.model());
|
|
bridge.set_chat(m.chat.model());
|
|
}
|
|
|
|
/// Whether a studio page or window is on screen.
|
|
fn studio_shown(&self) -> bool {
|
|
self.studio.window.is_some()
|
|
|| self.ui.upgrade().is_some_and(|ui| ui.global::<Nav>().get_page() == Page::Studio)
|
|
}
|
|
|
|
/// The models in the main window's StudioBridge (once).
|
|
fn studio_attach_main(&mut self) {
|
|
if !self.studio.attached
|
|
&& let Some(ui) = self.ui.upgrade()
|
|
{
|
|
self.studio_attach(&ui.global::<StudioBridge>());
|
|
self.studio.attached = true;
|
|
}
|
|
}
|
|
|
|
/// The studio page or window appeared: show it and start the studio.
|
|
pub(crate) fn studio_open(&mut self) {
|
|
self.studio_attach_main();
|
|
if let Some(window) = &self.studio.window
|
|
&& let Err(e) = window.show()
|
|
{
|
|
warn!("cannot show the studio window: {e}");
|
|
}
|
|
self.studio.data.hidden = 0;
|
|
if self.studio.run.is_none() && !self.studio.starting {
|
|
self.studio_start();
|
|
}
|
|
self.studio_refresh();
|
|
}
|
|
|
|
fn studio_start(&mut self) {
|
|
let settings = self.studio_settings();
|
|
settings.register(&STUDIO_UI);
|
|
self.studio.data.ui = (*settings.get_arc(&STUDIO_UI)).clone();
|
|
self.studio.data.scenes = settings.get_arc(&STUDIO_SCENES);
|
|
self.studio.starting = true;
|
|
self.studio.data.error.clear();
|
|
self.engine.runtime().spawn(async move {
|
|
let result = studio::start(&settings).await.map_err(|e| e.to_string());
|
|
later(move |app| app.studio_started(result));
|
|
});
|
|
}
|
|
|
|
fn studio_started(&mut self, result: Result<Arc<Studio>, String>) {
|
|
self.studio.starting = false;
|
|
let studio = match result {
|
|
Ok(studio) => studio,
|
|
Err(e) => {
|
|
self.studio.data.error = format!("The studio did not start: {e}");
|
|
self.set_status(self.studio.data.error.clone());
|
|
self.studio_refresh_status();
|
|
return;
|
|
}
|
|
};
|
|
let settings = self.studio_settings();
|
|
let runtime = self.engine.runtime().clone();
|
|
let (ops, rx) = mpsc::unbounded_channel();
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let pictures = Arc::new(Latest::new());
|
|
let tasks = vec![
|
|
runtime.spawn(run_ops(Arc::downgrade(&studio), rx)),
|
|
runtime.spawn(forward_events(studio.events())),
|
|
runtime.spawn(watch_settings(settings.clone())),
|
|
];
|
|
spawn_preview(studio.preview(), pictures.clone(), stop.clone());
|
|
let meters = slint::Timer::default();
|
|
meters.start(slint::TimerMode::Repeated, Duration::from_millis(66), || {
|
|
with_app(|app| app.studio_meters());
|
|
});
|
|
self.studio.data.scenes = Arc::new(studio.scenes());
|
|
self.studio.data.status = studio.status();
|
|
self.studio.run = Some(Running {
|
|
studio: studio.clone(),
|
|
streamer: None,
|
|
ops,
|
|
pictures,
|
|
stop,
|
|
tasks,
|
|
_meters: meters,
|
|
});
|
|
self.studio_preview_size();
|
|
// The encoder: the stream's codec, layers and audio.
|
|
let mut sources = audio_source_specs(&settings.get(&STREAM_AUDIO_SOURCES));
|
|
if !self.studio.data.ui.audio {
|
|
sources.clear();
|
|
}
|
|
let request = CaptureRequest {
|
|
fps: self.studio.data.scenes.fps(),
|
|
bitrate_kbps: settings.get(&STREAM_BITRATE_KBPS),
|
|
audio: !sources.is_empty(),
|
|
audio_sources: sources,
|
|
restore_token: None,
|
|
};
|
|
let layers = layer_specs(&settings.get(&STREAM_LAYERS));
|
|
self.video.start_studio(&runtime, studio, request, layers, |result| {
|
|
later(move |app| app.studio_encoder(result));
|
|
});
|
|
self.studio_overlays(true);
|
|
self.studio_refresh();
|
|
}
|
|
|
|
fn studio_encoder(&mut self, result: Result<Streamer, String>) {
|
|
let Some(run) = &mut self.studio.run else {
|
|
// Stopped meanwhile.
|
|
if let Ok(streamer) = result {
|
|
std::thread::spawn(move || drop(streamer));
|
|
}
|
|
return;
|
|
};
|
|
let problem = match result {
|
|
Ok(streamer) => {
|
|
let warning = streamer.audio_error().map(|e| format!("Stream audio: {e}"));
|
|
run.streamer = Some(streamer);
|
|
warning
|
|
}
|
|
Err(e) => {
|
|
self.studio.data.error = format!("The stream encoder did not start: {e}");
|
|
Some(self.studio.data.error.clone())
|
|
}
|
|
};
|
|
if let Some(problem) = problem {
|
|
self.set_status(problem);
|
|
}
|
|
self.studio_map_meters();
|
|
self.studio_refresh_audio();
|
|
self.studio_refresh_status();
|
|
self.studio_dev_apply();
|
|
}
|
|
|
|
/// Stop the studio (it is not shown, live or recording).
|
|
fn studio_stop(&mut self) {
|
|
let Some(mut run) = self.studio.run.take() else { return };
|
|
let streamer = run.streamer.take();
|
|
let studio = run.studio.clone();
|
|
drop(run);
|
|
// Stopping joins threads: not on the UI thread.
|
|
std::thread::spawn(move || drop((streamer, studio)));
|
|
self.studio.apps = None;
|
|
self.studio.meters.clear();
|
|
self.studio.data.stats = None;
|
|
self.studio.data.encoder = None;
|
|
self.studio.has_preview = false;
|
|
self.studio_each(|b| {
|
|
b.set_has_preview(false);
|
|
b.set_preview(slint::Image::default());
|
|
});
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
/// The size and rate of the preview: its width, the output's shape.
|
|
fn studio_preview_size(&self) {
|
|
self.studio_op(Op::Apply(self.studio.data.preview_command()));
|
|
}
|
|
|
|
/// A preview picture is waiting.
|
|
fn studio_picture(&mut self) {
|
|
let Some(picture) = self.studio.run.as_ref().and_then(|r| r.pictures.take()) else {
|
|
return;
|
|
};
|
|
let image = slint::Image::from_rgba8(picture);
|
|
let first = !self.studio.has_preview;
|
|
self.studio.has_preview = true;
|
|
self.studio_each(|b| {
|
|
b.set_preview(image.clone());
|
|
if first {
|
|
b.set_has_preview(true);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// The studio's events.
|
|
pub(crate) fn studio_event(&mut self, event: studio::Event) {
|
|
match event {
|
|
studio::Event::State(status) => {
|
|
self.studio.data.status = status;
|
|
self.studio_refresh_status();
|
|
}
|
|
studio::Event::Stats(stats) => {
|
|
self.studio.data.status = stats.status.clone();
|
|
self.studio.data.stats = Some(stats);
|
|
self.studio_tick();
|
|
}
|
|
studio::Event::Scenes(scenes) => {
|
|
let (switched, resized) = self.studio.data.set_scenes(scenes);
|
|
self.studio_refresh_scenes();
|
|
if switched {
|
|
self.studio_overlays(true);
|
|
}
|
|
if resized {
|
|
self.studio_preview_size();
|
|
}
|
|
self.studio_refresh_status();
|
|
}
|
|
other => {
|
|
if let Some(message) = logic::event_message(&other) {
|
|
self.set_status(message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Once a second, with the studio's statistics.
|
|
fn studio_tick(&mut self) {
|
|
if !self.studio.data.keep_running(self.studio_shown()) {
|
|
self.studio_stop();
|
|
return;
|
|
}
|
|
self.studio.data.encoder = self.studio_streamer().map(Streamer::stats);
|
|
self.studio_map_meters();
|
|
self.studio_refresh_audio();
|
|
self.studio_refresh_destinations();
|
|
self.studio_refresh_scenes();
|
|
self.studio_overlays(false);
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
/// Everything the studio shows.
|
|
fn studio_refresh(&mut self) {
|
|
self.studio_refresh_scenes();
|
|
self.studio_refresh_audio();
|
|
self.studio_refresh_layers();
|
|
self.studio_refresh_destinations();
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
fn studio_refresh_scenes(&self) {
|
|
let m = &self.studio.models;
|
|
m.scenes.sync(&vm::studio::scenes(&self.studio.data.scenes));
|
|
let stats = self.studio.data.stats.as_ref().map_or(&[][..], |s| &s.sources[..]);
|
|
m.sources.sync(&vm::studio::sources(self.studio.data.scenes.active(), stats));
|
|
}
|
|
|
|
/// The streamer whose mixer the audio rows show: a quick share's while
|
|
/// one runs (what goes out; both mix `stream.audio_sources`), else the
|
|
/// studio's encoder.
|
|
fn mixer_streamer(&self) -> Option<&Streamer> {
|
|
self.share_streamer().or_else(|| self.studio_streamer())
|
|
}
|
|
|
|
/// What the mixer says about each of its sources now.
|
|
fn mixer_stats(&self) -> Vec<AudioSourceStats> {
|
|
self.mixer_streamer().map(|s| s.stats().audio_sources).unwrap_or_default()
|
|
}
|
|
|
|
/// The mixer source of each audio row, and why one captures nothing.
|
|
fn studio_map_meters(&mut self) {
|
|
let sources = self
|
|
.studio
|
|
.data
|
|
.settings
|
|
.as_ref()
|
|
.map(|s| s.get(&STREAM_AUDIO_SOURCES))
|
|
.unwrap_or_default();
|
|
let mixer = self.mixer_streamer().and_then(Streamer::audio_mixer);
|
|
let stats = self.mixer_stats();
|
|
self.studio.meters = logic::meter_ids(&sources, &stats)
|
|
.into_iter()
|
|
.map(|id| mixer.as_ref()?.source(id?))
|
|
.collect();
|
|
}
|
|
|
|
fn studio_refresh_audio(&self) {
|
|
let Some(settings) = &self.studio.data.settings else { return };
|
|
let sources = settings.get(&STREAM_AUDIO_SOURCES);
|
|
let stats = self.mixer_stats();
|
|
let levels: Vec<Option<f32>> = (0..sources.len())
|
|
.map(|i| Some(self.studio.meters.get(i).cloned().flatten()?.level().peak_db()))
|
|
.collect();
|
|
let live = logic::audio_live(&sources, &stats, &levels);
|
|
self.studio.models.audio.sync(&vm::studio::audio(&sources, &live));
|
|
}
|
|
|
|
/// The share dialog's audio: the studio's mixer rows and picker in the
|
|
/// main window, without starting the studio.
|
|
pub(crate) fn mixer_show(&mut self) {
|
|
self.studio_attach_main();
|
|
self.studio_settings();
|
|
self.mixer_refresh();
|
|
}
|
|
|
|
/// The audio rows again: which mixer source each meter reads, levels
|
|
/// and errors.
|
|
pub(crate) fn mixer_refresh(&mut self) {
|
|
self.studio_map_meters();
|
|
self.studio_refresh_audio();
|
|
}
|
|
|
|
/// Once a second while the studio does not run (it has its own tick):
|
|
/// the share's rows, and the picker's application list let go once the
|
|
/// share dialog is closed.
|
|
pub(crate) fn mixer_tick(&mut self) {
|
|
if self.studio.run.is_some() {
|
|
return;
|
|
}
|
|
if self.state.share.is_some() {
|
|
self.mixer_refresh();
|
|
}
|
|
if !self.ui.upgrade().is_some_and(|ui| ui.global::<Nav>().get_share_open()) {
|
|
self.studio.apps = None;
|
|
}
|
|
}
|
|
|
|
/// The meters, about 15 times a second (only rows that moved redraw).
|
|
pub(crate) fn studio_meters(&mut self) {
|
|
let model = &self.studio.models.audio;
|
|
for (i, meter) in self.studio.meters.iter().enumerate() {
|
|
let level = meter.as_ref().map_or(-100.0, |m| m.level().peak_db().max(-100.0));
|
|
if let Some(mut row) = model.get(i)
|
|
&& (row.level - level).abs() >= 0.5
|
|
{
|
|
row.level = level;
|
|
model.set(i, row);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn studio_refresh_layers(&mut self) {
|
|
let settings = self.studio_settings();
|
|
self.studio.models.layers.sync(&vm::studio::layers(&settings.get(&STREAM_LAYERS)));
|
|
}
|
|
|
|
fn studio_refresh_destinations(&mut self) {
|
|
let list = logic::destinations(&self.state);
|
|
self.studio.data.choose_destination(&list);
|
|
self.studio.models.destinations.sync(&logic::destination_rows(&self.state, &list));
|
|
self.studio_bind_chat();
|
|
self.studio_refresh_form();
|
|
}
|
|
|
|
/// The session and channel the stream (and its chat) goes to.
|
|
fn studio_chat_target(&self) -> Option<(i64, u64)> {
|
|
logic::chat_target(&self.state, self.studio.data.destination)
|
|
}
|
|
|
|
/// Show the destination channel's chat (its tab opened, with its
|
|
/// history, if need be).
|
|
fn studio_bind_chat(&mut self) {
|
|
let name = match self.studio_chat_target() {
|
|
Some((id, channel)) => {
|
|
let (name, new) = logic::chat_tab(&mut self.state, id, channel);
|
|
if new && !self.demo_ui {
|
|
let target = ChatTarget::Channel(channel);
|
|
self.engine.send(Command::OpenChat { session: id as u64, target });
|
|
}
|
|
name
|
|
}
|
|
None => String::new(),
|
|
};
|
|
self.studio_each(|b| b.set_chat_name(name.clone().into()));
|
|
self.studio_sync_chat();
|
|
}
|
|
|
|
/// The stream chat's lines: the destination tab's, made as the chat
|
|
/// view makes them. The main window keeps only its shown tab's lines up
|
|
/// to date, and the stream's channel need not be that tab.
|
|
fn studio_sync_chat(&self) {
|
|
let tab = self.studio_chat_target().and_then(|(id, channel)| {
|
|
let view = self.state.sessions.get(&id)?;
|
|
let tab = view.tabs.iter().find(|t| t.target == ChatTarget::Channel(channel))?;
|
|
Some((view, tab))
|
|
});
|
|
let lines: Vec<ChatLine> = match tab {
|
|
Some((view, tab)) if view.has_history() => self.lines_of(view, tab, true),
|
|
// Without stored history the tab's lines are pushed as they come.
|
|
Some((_, tab)) => tab.lines.rows().to_vec(),
|
|
None => Vec::new(),
|
|
};
|
|
self.studio.models.chat.sync(&lines);
|
|
}
|
|
|
|
/// A chat of session `id` changed (chat/): follow it if it is the
|
|
/// stream's.
|
|
pub(crate) fn studio_chat_changed(&mut self, id: i64, target: &ChatTarget) {
|
|
if self.studio.run.is_some()
|
|
&& let Some((session, channel)) = self.studio_chat_target()
|
|
&& session == id
|
|
&& *target == ChatTarget::Channel(channel)
|
|
{
|
|
self.studio_sync_chat();
|
|
self.studio_overlays(false);
|
|
}
|
|
}
|
|
|
|
fn studio_refresh_form(&self) {
|
|
let list = logic::destinations(&self.state);
|
|
let shown = crate::app::StudioForm::from(&self.studio.data.form(&list));
|
|
self.studio_each(|b| {
|
|
if b.get_form() != shown {
|
|
b.set_form(shown.clone());
|
|
}
|
|
});
|
|
}
|
|
|
|
/// The header, the preview's badges and the bottom bar.
|
|
fn studio_refresh_status(&self) {
|
|
let st = &self.studio;
|
|
let destination = st.data.destination.map(|id| logic::destination_name(&self.state, id));
|
|
let sharing = st
|
|
.data
|
|
.destination
|
|
.is_some_and(|d| self.state.share.as_ref().is_some_and(|s| s.session == d));
|
|
let session = st.data.stream.as_ref().map(|s| s.session);
|
|
let viewer_name = |v: &ViewerInfo| logic::viewer_name(&self.state, session, v.client);
|
|
let cx = logic::StatusContext {
|
|
running: st.run.is_some(),
|
|
has_encoder: st.run.as_ref().is_some_and(|r| r.streamer.is_some()),
|
|
detached: st.window.is_some(),
|
|
sharing,
|
|
destination,
|
|
viewer_name: &viewer_name,
|
|
};
|
|
let (status, requests) = st.data.status_view(&cx);
|
|
st.models.viewers.sync(&requests);
|
|
let shown = crate::app::StudioStatus::from(&status);
|
|
self.studio_each(|b| {
|
|
if b.get_status() != shown {
|
|
b.set_status(shown.clone());
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Keep the overlay sources as the switches say (`force`: also when
|
|
/// nothing changed, e.g. after a scene switch).
|
|
fn studio_overlays(&mut self, force: bool) {
|
|
let wanted = self.studio.data.wanted_overlays(|| self.studio_chat_tail(5));
|
|
if let Some(wanted) = self.studio.data.overlay_change(force, wanted) {
|
|
self.studio_op(Op::Overlays(wanted));
|
|
}
|
|
}
|
|
|
|
/// The last lines of the stream's chat, for the chat overlay.
|
|
fn studio_chat_tail(&self, lines: usize) -> Option<String> {
|
|
logic::chat_tail(&self.studio.models.chat.rows(), lines)
|
|
}
|
|
|
|
// Following the settings.
|
|
|
|
/// `stream.audio_sources` (or the stream audio switch) changed.
|
|
pub(crate) fn studio_audio_changed(&mut self) {
|
|
let settings = self.studio_settings();
|
|
let mut specs = audio_source_specs(&settings.get(&STREAM_AUDIO_SOURCES));
|
|
if !self.studio.data.ui.audio {
|
|
specs.clear();
|
|
}
|
|
let codecs = self.video.codecs();
|
|
let result =
|
|
self.studio_streamer().filter(|s| s.audio_sources() != specs).map(|streamer| {
|
|
let update =
|
|
StreamerConfigUpdate { audio_sources: Some(specs), ..Default::default() };
|
|
streamer.reconfigure(&codecs, update)
|
|
});
|
|
if result.is_some() {
|
|
self.studio.data.encoder = self.studio_streamer().map(Streamer::stats);
|
|
}
|
|
if let Some(Err(e)) = result {
|
|
self.set_status(format!("Stream audio: {e}"));
|
|
}
|
|
self.studio_map_meters();
|
|
self.studio_refresh_audio();
|
|
}
|
|
|
|
/// `stream.layers` or `stream.bitrate_kbps` changed.
|
|
pub(crate) fn studio_encoding_changed(&mut self) {
|
|
let settings = self.studio_settings();
|
|
if let Some(stream) = &self.studio.data.stream {
|
|
let session = stream.session as u64;
|
|
let layers = self.studio_session_layers(&settings);
|
|
self.engine.send(Command::SetStreamLayers { session, layers });
|
|
}
|
|
let update = StreamerConfigUpdate {
|
|
bitrate_kbps: Some(settings.get(&STREAM_BITRATE_KBPS)),
|
|
layers: Some(layer_specs(&settings.get(&STREAM_LAYERS))),
|
|
..Default::default()
|
|
};
|
|
let codecs = self.video.codecs();
|
|
let result = self.studio_streamer().map(|streamer| streamer.reconfigure(&codecs, update));
|
|
if let Some(Err(e)) = result {
|
|
self.set_status(format!("Stream quality: {e}"));
|
|
}
|
|
self.studio_refresh_layers();
|
|
self.studio_refresh_form();
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
/// `studio.ui` changed elsewhere (another window, `--set`).
|
|
pub(crate) fn studio_ui_changed(&mut self) {
|
|
let settings = self.studio_settings();
|
|
let ui = (*settings.get_arc(&STUDIO_UI)).clone();
|
|
if ui == self.studio.data.ui {
|
|
return;
|
|
}
|
|
let (audio, preview) = (
|
|
ui.audio != self.studio.data.ui.audio,
|
|
(ui.preview_width, ui.preview_fps)
|
|
!= (self.studio.data.ui.preview_width, self.studio.data.ui.preview_fps),
|
|
);
|
|
self.studio.data.ui = ui;
|
|
if audio {
|
|
self.studio_audio_changed();
|
|
}
|
|
if preview {
|
|
self.studio_preview_size();
|
|
}
|
|
self.studio_overlays(false);
|
|
self.studio_refresh_form();
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
fn studio_store_ui(&mut self, ui: StudioUi) {
|
|
let settings = self.studio_settings();
|
|
self.studio.data.ui = ui.clone();
|
|
if let Err(e) = settings.set(&STUDIO_UI, ui) {
|
|
self.set_status(e.to_string());
|
|
}
|
|
}
|
|
|
|
// The stream settings panel.
|
|
|
|
/// A field of the stream settings.
|
|
pub(crate) fn studio_edit(&mut self, key: &str, value: &str) {
|
|
match logic::edit(&self.studio.data.ui, key, value) {
|
|
logic::Edit::Bitrate(Some(kbps)) => {
|
|
let settings = self.studio_settings();
|
|
if let Err(e) = settings.set(&STREAM_BITRATE_KBPS, kbps) {
|
|
self.set_status(e.to_string());
|
|
}
|
|
}
|
|
logic::Edit::Bitrate(None) => self.set_status(logic::BITRATE_HINT),
|
|
logic::Edit::Destination(index) => {
|
|
let list = logic::destinations(&self.state);
|
|
if self.studio.data.stream.is_none()
|
|
&& let Some((id, _)) = index.and_then(|i| list.get(i))
|
|
{
|
|
self.studio.data.destination = Some(*id);
|
|
self.studio_bind_chat();
|
|
self.studio_refresh_form();
|
|
self.studio_overlays(false);
|
|
self.studio_refresh_status();
|
|
}
|
|
}
|
|
logic::Edit::Ui(ui) => {
|
|
let audio = ui.audio != self.studio.data.ui.audio;
|
|
self.studio_store_ui(ui);
|
|
if audio {
|
|
self.studio_audio_changed();
|
|
}
|
|
self.studio_overlays(false);
|
|
self.studio_refresh_form();
|
|
self.studio_refresh_status();
|
|
}
|
|
logic::Edit::Nothing => {}
|
|
}
|
|
}
|
|
|
|
/// The output's size and rate: presets or anything typed.
|
|
pub(crate) fn studio_set_output(&mut self, width: &str, height: &str, fps: &str) {
|
|
let Some((width, height, fps)) = logic::parse_output(width, height, fps) else {
|
|
self.set_status(logic::OUTPUT_HINT);
|
|
return;
|
|
};
|
|
self.studio_op(Op::Apply(studio::Command::SetOutput { width, height, fps }));
|
|
let codecs = self.video.codecs();
|
|
let update = StreamerConfigUpdate { fps: Some(fps), ..Default::default() };
|
|
let result = self.studio_streamer().map(|streamer| streamer.reconfigure(&codecs, update));
|
|
if let Some(Err(e)) = result {
|
|
self.set_status(format!("Stream quality: {e}"));
|
|
}
|
|
}
|
|
|
|
pub(crate) fn studio_set_layer(&mut self, index: usize, row: &StudioLayer) {
|
|
let settings = self.studio_settings();
|
|
let Some(messages) = logic::set_layer(&settings, index, row) else { return };
|
|
for message in messages {
|
|
self.set_status(message);
|
|
}
|
|
self.studio_refresh_layers();
|
|
}
|
|
|
|
pub(crate) fn studio_add_layer(&mut self) {
|
|
let settings = self.studio_settings();
|
|
let bitrate = self.studio_bitrate_kbps(&settings);
|
|
for message in logic::add_layer(&settings, bitrate) {
|
|
self.set_status(message);
|
|
}
|
|
self.studio_refresh_layers();
|
|
}
|
|
|
|
pub(crate) fn studio_remove_layer(&mut self, index: usize) {
|
|
let settings = self.studio_settings();
|
|
for message in logic::remove_layer(&settings, index) {
|
|
self.set_status(message);
|
|
}
|
|
self.studio_refresh_layers();
|
|
}
|
|
|
|
/// Settings → Streaming in the main window.
|
|
pub(crate) fn studio_advanced(&mut self) {
|
|
let weak = self.ui.clone();
|
|
// After this callback: opening the settings calls back into the app.
|
|
let _ = slint::invoke_from_event_loop(move || {
|
|
if let Some(ui) = weak.upgrade() {
|
|
ui.global::<Nav>().invoke_open_settings(crate::app::SettingsSection::Streaming);
|
|
let _ = ui.show();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Scenes and sources.
|
|
|
|
pub(crate) fn studio_scene_command(&mut self, command: studio::Command) {
|
|
self.studio_op(Op::Apply(command));
|
|
}
|
|
|
|
pub(crate) fn studio_add_scene(&mut self) {
|
|
let n = self.studio.data.scenes.scenes.len() + 1;
|
|
self.studio_op(Op::Apply(studio::Command::AddScene { name: format!("Scene {n}") }));
|
|
}
|
|
|
|
fn studio_live_source(&self, id: u64) -> Option<&Source> {
|
|
self.studio.data.live_source(id)
|
|
}
|
|
|
|
fn studio_live_scene(&self) -> Option<u64> {
|
|
self.studio.data.live_scene()
|
|
}
|
|
|
|
fn studio_update(&self, source: u64, change: SourceChange) {
|
|
let Some(scene) = self.studio_live_scene() else { return };
|
|
let command = studio::Command::UpdateSource { scene, source, change: Box::new(change) };
|
|
self.studio_op(Op::Apply(command));
|
|
}
|
|
|
|
pub(crate) fn studio_toggle_source(&mut self, id: u64) {
|
|
if let Some(source) = self.studio_live_source(id) {
|
|
let visible = Some(!source.visible);
|
|
self.studio_update(id, SourceChange { visible, ..Default::default() });
|
|
}
|
|
}
|
|
|
|
/// up, down, lock, remove, bg-keep, bg-blur.
|
|
pub(crate) fn studio_source_action(&mut self, id: u64, action: &str) {
|
|
let demo = self.demo_ui;
|
|
let cameras = || if demo { Vec::new() } else { camera::list() };
|
|
match logic::source_action(&self.studio.data.scenes, id, action, cameras) {
|
|
Some(logic::SourceStep::Apply(command)) => self.studio_op(Op::Apply(command)),
|
|
Some(logic::SourceStep::Update(change)) => self.studio_update(id, change),
|
|
Some(logic::SourceStep::Say(text)) => self.set_status(text),
|
|
None => {}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn studio_source_form(&self, id: u64) -> StudioSourceForm {
|
|
self.studio_live_source(id).map(vm::studio::form).unwrap_or_default()
|
|
}
|
|
|
|
pub(crate) fn studio_apply_source(&mut self, form: &StudioSourceForm) {
|
|
let Ok(id) = u64::try_from(form.id) else { return };
|
|
let Some(old) = self.studio_live_source(id) else { return };
|
|
match logic::source_change(form, old) {
|
|
Ok((change, notice)) => {
|
|
if let Some(notice) = notice {
|
|
self.set_status(notice);
|
|
}
|
|
self.studio_update(id, change);
|
|
}
|
|
Err(e) => self.set_status(e),
|
|
}
|
|
}
|
|
|
|
/// A text, colour or image source from the dialog.
|
|
pub(crate) fn studio_add_source(&mut self, form: &StudioSourceForm) {
|
|
match vm::studio::new_source(form, self.studio.data.scenes.size()) {
|
|
Ok(source) => self.studio_op(Op::Add(source)),
|
|
Err(e) => self.set_status(e),
|
|
}
|
|
}
|
|
|
|
fn studio_set_picks(&mut self, picks: Vec<(Pick, StudioPick)>) {
|
|
let rows: Vec<StudioPick> = picks.iter().map(|(_, row)| row.clone()).collect();
|
|
self.studio.data.picks = picks.into_iter().map(|(pick, _)| pick).collect();
|
|
self.studio.models.picks.sync(&rows);
|
|
}
|
|
|
|
/// What a screen, window or camera source can show. False when there is
|
|
/// nothing to pick from: the one choice (the desktop's own dialog) was
|
|
/// added at once.
|
|
pub(crate) fn studio_list_sources(&mut self, kind: &str) -> bool {
|
|
let picks = match kind {
|
|
"camera" if self.demo_ui => logic::demo_camera_picks(),
|
|
"camera" => logic::camera_picks(&camera::list()),
|
|
"screen" | "window" => {
|
|
let listed = if self.demo_ui { Ok(Vec::new()) } else { screen_sources() };
|
|
let list = listed.unwrap_or_else(|e| {
|
|
self.set_status(format!("Cannot list what to share: {e}"));
|
|
Vec::new()
|
|
});
|
|
logic::screen_picks(kind, list, self.demo_ui || cfg!(debug_assertions))
|
|
}
|
|
_ => Vec::new(),
|
|
};
|
|
if logic::only_portal(&picks) {
|
|
self.studio_set_picks(picks);
|
|
self.studio_add_picked(0);
|
|
return false;
|
|
}
|
|
self.studio_set_picks(picks);
|
|
true
|
|
}
|
|
|
|
pub(crate) fn studio_add_picked(&mut self, index: usize) {
|
|
let Some(pick) = self.studio.data.picks.get(index) else { return };
|
|
let Some(source) = logic::picked_source(pick, self.studio.data.scenes.size()) else {
|
|
return;
|
|
};
|
|
self.studio_op(Op::Add(source));
|
|
}
|
|
|
|
/// The phone's source cards: show a screen, a camera or a window (and
|
|
/// hide the other two kinds); add one if the live scene has none.
|
|
pub(crate) fn studio_main_source(&mut self, kind: &str) {
|
|
let (changes, has_kind) =
|
|
logic::main_source_changes(self.studio.data.scenes.active(), kind);
|
|
for (id, visible) in changes {
|
|
self.studio_update(id, SourceChange { visible: Some(visible), ..Default::default() });
|
|
}
|
|
if !has_kind && self.studio_list_sources(kind) {
|
|
if self.studio.data.picks.len() == 1 {
|
|
self.studio_add_picked(0);
|
|
} else {
|
|
self.studio_nav(|nav| {
|
|
nav.set_pick_kind(kind.into());
|
|
nav.set_dialog("pick".into());
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// The audio mixer.
|
|
|
|
/// What can be added to the mixer.
|
|
pub(crate) fn studio_list_audio(&mut self) {
|
|
let settings = self.studio_settings();
|
|
let current = settings.get(&STREAM_AUDIO_SOURCES);
|
|
let apps = if self.demo_ui {
|
|
Vec::new()
|
|
} else {
|
|
if self.studio.apps.is_none() {
|
|
match audio_apps() {
|
|
Ok(apps) => self.studio.apps = Some(apps),
|
|
Err(e) => warn!("no application list: {e}"),
|
|
}
|
|
}
|
|
self.studio.apps.as_mut().map(AudioApps::current).unwrap_or_default()
|
|
};
|
|
self.studio_set_picks(logic::audio_picks(¤t, apps, self.demo_ui));
|
|
}
|
|
|
|
fn studio_audio_sources(&mut self, f: impl FnOnce(&mut Vec<AudioSourceSetting>)) {
|
|
let settings = self.studio_settings();
|
|
let mut sources = settings.get(&STREAM_AUDIO_SOURCES);
|
|
f(&mut sources);
|
|
if let Err(e) = settings.set(&STREAM_AUDIO_SOURCES, sources) {
|
|
self.set_status(e.to_string());
|
|
}
|
|
// A quick share follows at once (the studio through its watch).
|
|
self.share_audio_changed();
|
|
self.studio_refresh_audio();
|
|
}
|
|
|
|
/// 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.data.picks.get(index) else { return };
|
|
let setting = setting.clone();
|
|
self.studio_audio_sources(|sources| logic::toggle_audio(sources, setting));
|
|
self.studio_list_audio();
|
|
}
|
|
|
|
/// While dragging the gain goes to the mixer directly; at the end it is
|
|
/// stored (and the streamer follows).
|
|
pub(crate) fn studio_set_gain(&mut self, index: usize, db: f32, done: bool) {
|
|
let gain = vm::studio::gain_linear(db);
|
|
if let Some(Some(meter)) = self.studio.meters.get(index) {
|
|
meter.set_gain(gain);
|
|
}
|
|
let model = &self.studio.models.audio;
|
|
if let Some(mut row) = model.get(index) {
|
|
row.gain_db = db;
|
|
model.set(index, row);
|
|
}
|
|
if done {
|
|
self.studio_audio_sources(|sources| logic::set_gain(sources, index, gain));
|
|
}
|
|
}
|
|
|
|
pub(crate) fn studio_toggle_mute(&mut self, index: usize) {
|
|
self.studio_audio_sources(|sources| logic::toggle_mute(sources, index));
|
|
}
|
|
|
|
pub(crate) fn studio_remove_audio(&mut self, index: usize) {
|
|
self.studio_audio_sources(|sources| logic::remove_audio(sources, index));
|
|
}
|
|
|
|
// Recording, clips, the studio settings.
|
|
|
|
fn studio_recordings(&mut self) -> Option<PathBuf> {
|
|
let dir = studio::recording_dir(&self.studio_settings());
|
|
match std::fs::create_dir_all(&dir) {
|
|
Ok(()) => Some(dir),
|
|
Err(e) => {
|
|
self.set_status(format!("Cannot use {}: {e}", dir.display()));
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn studio_save_clip(&mut self) {
|
|
if self.studio_settings().get(&STUDIO_REPLAY_SECONDS) == 0 {
|
|
self.set_status("The replay buffer is off: turn it on in the Studio Settings.");
|
|
return;
|
|
}
|
|
if let Some(dir) = self.studio_recordings() {
|
|
let path = dir.join(logic::file_name("Clip"));
|
|
self.studio_op(Op::Apply(studio::Command::SaveClip { path }));
|
|
}
|
|
}
|
|
|
|
pub(crate) fn studio_toggle_recording(&mut self) {
|
|
if self.studio.data.status.recording.is_some() {
|
|
self.studio_op(Op::Apply(studio::Command::StopRecording));
|
|
} else if let Some(dir) = self.studio_recordings() {
|
|
let path = dir.join(logic::file_name("Recording"));
|
|
self.studio_op(Op::Apply(studio::Command::StartRecording { path }));
|
|
}
|
|
}
|
|
|
|
pub(crate) fn studio_open_recordings(&mut self) {
|
|
if let Some(dir) = self.studio_recordings()
|
|
&& let Err(e) = crate::app::open_path(&dir)
|
|
{
|
|
self.set_status(format!("Cannot open {}: {e}", dir.display()));
|
|
}
|
|
}
|
|
|
|
pub(crate) fn studio_settings_form(&mut self) -> StudioSettingsForm {
|
|
let settings = self.studio_settings();
|
|
logic::settings_form(&settings, &self.studio.data.ui)
|
|
}
|
|
|
|
pub(crate) fn studio_apply_settings(&mut self, form: &StudioSettingsForm) {
|
|
let settings = self.studio_settings();
|
|
let done = logic::apply_settings_form(&settings, &self.studio.data.ui, form);
|
|
for note in done.notes {
|
|
self.set_status(note);
|
|
}
|
|
if let Some(ui) = done.ui {
|
|
self.studio_store_ui(ui);
|
|
self.studio_preview_size();
|
|
}
|
|
if let Some(refused) = done.refused {
|
|
self.set_status(refused);
|
|
}
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
// Going live.
|
|
|
|
pub(crate) fn studio_go_live(&mut self) {
|
|
let Some(session) = self.studio.data.destination else { return };
|
|
if self.studio.data.stream.is_some() || self.studio_streamer().is_none() {
|
|
return;
|
|
}
|
|
if self.state.share.as_ref().is_some_and(|s| s.session == session) {
|
|
self.set_status("Stop sharing your screen first.");
|
|
return;
|
|
}
|
|
self.studio.data.stream = Some(Outgoing { session, live: false, viewers: Vec::new() });
|
|
if self.demo_ui {
|
|
self.studio_demo_live();
|
|
return;
|
|
}
|
|
let settings = self.studio_settings();
|
|
let setup = self.studio.data.stream_setup(&settings);
|
|
let auto_accept = self.state.settings.share.auto_accept;
|
|
// The stream's layers are the encoder's, so viewers move between
|
|
// them (the session sends only layers it knows of).
|
|
let session = session as u64;
|
|
let layers = self.studio_session_layers(&settings);
|
|
self.engine.send(Command::SetStreamLayers { session, layers });
|
|
self.engine.send(Command::StartStream { session, setup, auto_accept });
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
/// Whether `session` is where the studio streams to (app.rs routes its
|
|
/// stream events here).
|
|
pub(crate) fn studio_streams_to(&self, session: i64) -> bool {
|
|
self.studio.data.streams_to(session)
|
|
}
|
|
|
|
pub(crate) fn studio_stream_state(&mut self, state: StreamState) {
|
|
match state {
|
|
StreamState::Starting => {}
|
|
StreamState::Live { sink, .. } => {
|
|
if let Some(streamer) = self.studio_streamer() {
|
|
streamer.attach(Arc::new(sink));
|
|
}
|
|
self.studio_now_live();
|
|
}
|
|
StreamState::Ended(reason) => self.studio_ended(Some(&reason)),
|
|
}
|
|
}
|
|
|
|
/// The stream is up: the studio's LIVE, the go-live message.
|
|
fn studio_now_live(&mut self) {
|
|
let Some(out) = &mut self.studio.data.stream else { return };
|
|
out.live = true;
|
|
self.studio_op(Op::Apply(studio::Command::GoLive));
|
|
let message = self.studio.data.ui.message.trim().to_owned();
|
|
if !message.is_empty() {
|
|
self.studio_send_chat(message);
|
|
}
|
|
self.set_status("You are live");
|
|
self.studio_overlays(false);
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
pub(crate) fn studio_viewers(&mut self, viewers: Vec<ViewerInfo>) {
|
|
if let Some(out) = &mut self.studio.data.stream {
|
|
out.viewers = viewers;
|
|
}
|
|
self.studio_overlays(false);
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
pub(crate) fn studio_end(&mut self) {
|
|
let Some(out) = &self.studio.data.stream else { return };
|
|
if self.demo_ui {
|
|
self.studio_demo_announce(false);
|
|
self.studio_ended(None);
|
|
} else {
|
|
self.engine.send(Command::StopStream { session: out.session as u64 });
|
|
}
|
|
}
|
|
|
|
fn studio_ended(&mut self, reason: Option<&EndReason>) {
|
|
if let Some(streamer) = self.studio_streamer() {
|
|
streamer.detach();
|
|
}
|
|
self.studio_op(Op::Apply(studio::Command::EndStream));
|
|
self.studio.data.stream = None;
|
|
self.set_status(logic::ended_message(reason));
|
|
self.studio_overlays(false);
|
|
self.studio_refresh_status();
|
|
}
|
|
|
|
pub(crate) fn studio_respond_viewer(&mut self, client: u16, accept: bool) {
|
|
let Some(out) = &mut self.studio.data.stream else { return };
|
|
if self.demo_ui {
|
|
for v in out.viewers.iter_mut().filter(|v| v.client == client) {
|
|
v.state = ViewerState::Connected;
|
|
}
|
|
out.viewers.retain(|v| accept || v.client != client);
|
|
self.studio_refresh_status();
|
|
return;
|
|
}
|
|
let session = out.session as u64;
|
|
self.engine.send(Command::AcceptViewer { session, viewer: client, accept });
|
|
}
|
|
|
|
pub(crate) fn studio_send_chat(&mut self, text: String) {
|
|
let Some((id, channel)) = self.studio_chat_target() else { return };
|
|
let target = ChatTarget::Channel(channel);
|
|
// Sample data has no engine to echo the message: shown as it would be.
|
|
if self.demo_ui {
|
|
let view = self.state.sessions.entry(id).or_default();
|
|
let (client, history) = (view.state.own_client, view.has_history());
|
|
let author = self.bookmark(id).map(|b| b.nickname.clone()).unwrap_or_default();
|
|
let ts_ms = chrono::Utc::now().timestamp_millis();
|
|
let message = logic::demo_message(target.clone(), author, client, text, ts_ms);
|
|
if history {
|
|
let stored = logic::demo_stored(message);
|
|
self.history_batch(id, &target, vec![stored], HistorySource::Live, false);
|
|
} else {
|
|
self.add_message(id, message);
|
|
}
|
|
} else {
|
|
self.engine.send(Command::SendChat { session: id as u64, target, text });
|
|
}
|
|
}
|
|
|
|
/// The destination's chat in the main window.
|
|
pub(crate) fn studio_open_chat(&mut self) {
|
|
let Some((id, channel)) = self.studio_chat_target() else { return };
|
|
if self.state.current != Some(id) {
|
|
self.select_server(id);
|
|
}
|
|
self.open_chat(ChatTarget::Channel(channel), true);
|
|
let weak = self.ui.clone();
|
|
let _ = slint::invoke_from_event_loop(move || {
|
|
if let Some(ui) = weak.upgrade() {
|
|
ui.global::<Nav>().invoke_show(Page::Server);
|
|
let _ = ui.show();
|
|
}
|
|
});
|
|
}
|
|
|
|
// The window of its own.
|
|
|
|
pub(crate) fn studio_detach(&mut self) {
|
|
// One window on Android.
|
|
if cfg!(target_os = "android") {
|
|
return;
|
|
}
|
|
if self.studio.window.is_none() {
|
|
let window = match StudioWindow::new() {
|
|
Ok(window) => window,
|
|
Err(e) => {
|
|
self.set_status(format!("Cannot open the studio window: {e}"));
|
|
return;
|
|
}
|
|
};
|
|
let bridge = window.global::<StudioBridge>();
|
|
crate::bind::studio::wire(&bridge);
|
|
self.studio_attach(&bridge);
|
|
window.window().on_close_requested(|| {
|
|
// After this event: the window goes away then.
|
|
later(|app| app.studio_dock());
|
|
slint::CloseRequestResponse::HideWindow
|
|
});
|
|
self.studio.window = Some(window);
|
|
self.theme_windows();
|
|
if let Some(ui) = self.ui.upgrade()
|
|
&& ui.global::<Nav>().get_page() == Page::Studio
|
|
{
|
|
ui.global::<Nav>().set_page(Page::Server);
|
|
}
|
|
}
|
|
// The next picture goes to both windows with the flag.
|
|
self.studio.has_preview = false;
|
|
self.studio_open();
|
|
self.studio_bind_chat();
|
|
}
|
|
|
|
/// The studio back into the main window.
|
|
pub(crate) fn studio_dock(&mut self) {
|
|
let Some(window) = self.studio.window.take() else { return };
|
|
let _ = window.hide();
|
|
self.studio_refresh_status();
|
|
let weak = self.ui.clone();
|
|
// Showing the page runs Nav code that calls back into the app.
|
|
let _ = slint::invoke_from_event_loop(move || {
|
|
if let Some(ui) = weak.upgrade() {
|
|
ui.global::<Nav>().invoke_show(Page::Studio);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// A picture of the studio window (screenshots, dev/).
|
|
pub(crate) fn studio_snapshot(&self) -> Option<SharedPixelBuffer<Rgba8Pixel>> {
|
|
self.studio.window.as_ref()?.window().take_snapshot().ok()
|
|
}
|
|
|
|
// Development switches and sample data (dev/).
|
|
|
|
/// `VOELIN_OPEN=studio:<what>`: `window` (the window of its own), `live`,
|
|
/// `record`, or a dialog: `source`, `audio`, `camera`, `scene`, `settings`.
|
|
pub(crate) fn studio_dev(&mut self, what: &str) {
|
|
match what {
|
|
"" => {}
|
|
"window" => self.studio_detach(),
|
|
// Started here: a screen opened after it (`studio:live,server`)
|
|
// leaves the studio page before it is drawn, which starts it.
|
|
"live" => {
|
|
self.studio.data.dev.push(what.into());
|
|
self.studio_open();
|
|
}
|
|
other => self.studio.data.dev.push(other.into()),
|
|
}
|
|
}
|
|
|
|
/// The development switches that wait for the studio and its encoder.
|
|
fn studio_dev_apply(&mut self) {
|
|
for what in std::mem::take(&mut self.studio.data.dev) {
|
|
match what.as_str() {
|
|
"live" => {
|
|
self.studio_refresh_destinations();
|
|
self.studio_go_live();
|
|
}
|
|
"record" => self.studio_toggle_recording(),
|
|
"source" => {
|
|
let front = self
|
|
.studio
|
|
.data
|
|
.scenes
|
|
.active()
|
|
.and_then(|s| s.sources.first())
|
|
.map(|s| s.id);
|
|
let form = front.map(|id| self.studio_source_form(id)).unwrap_or_default();
|
|
self.studio_nav(|nav| {
|
|
nav.set_source_form(form.into());
|
|
nav.set_dialog("source".into());
|
|
});
|
|
}
|
|
"audio" | "camera" => {
|
|
if what == "audio" {
|
|
self.studio_list_audio();
|
|
} else {
|
|
self.studio_list_sources("camera");
|
|
}
|
|
self.studio_nav(|nav| {
|
|
nav.set_pick_kind(what.as_str().into());
|
|
nav.set_dialog("pick".into());
|
|
});
|
|
}
|
|
"scene" => {
|
|
let scene =
|
|
self.studio.data.scenes.active().map(|s| (s.id as i32, s.name.clone()));
|
|
let (id, name) = scene.unwrap_or_default();
|
|
self.studio_nav(|nav| {
|
|
nav.set_scene_id(id);
|
|
nav.set_scene_name(name.into());
|
|
nav.set_dialog("scene".into());
|
|
});
|
|
}
|
|
"settings" => {
|
|
let form = self.studio_settings_form();
|
|
self.studio_nav(|nav| {
|
|
nav.set_settings_form(form.into());
|
|
nav.set_dialog("settings".into());
|
|
});
|
|
}
|
|
other => eprintln!("VOELIN_OPEN: unknown studio switch {other:?}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// VOELIN_DEMO_UI going live: no server, so the studio goes live at once
|
|
/// with sample viewers.
|
|
fn studio_demo_live(&mut self) {
|
|
if let Some(out) = &mut self.studio.data.stream {
|
|
out.viewers = logic::demo_viewers();
|
|
}
|
|
self.studio_demo_announce(true);
|
|
self.studio_now_live();
|
|
}
|
|
|
|
/// VOELIN_DEMO_UI: the sample server announces our stream, or its end,
|
|
/// as a server would (the voice view shows its card).
|
|
fn studio_demo_announce(&mut self, live: bool) {
|
|
let Some(session) = self.studio.data.stream.as_ref().map(|s| s.session) else { return };
|
|
let name = logic::stream_name(&self.studio.data.ui);
|
|
let audio = self.studio.data.ui.audio;
|
|
let Some(view) = self.state.sessions.get_mut(&session) else { return };
|
|
view.streams.retain(|s| s.id != logic::DEMO_STREAM_ID);
|
|
if live && let Some(own) = view.state.own_client {
|
|
view.streams.push(logic::demo_stream(own, name, audio));
|
|
}
|
|
self.refresh_streams();
|
|
}
|
|
}
|