mirror of
https://github.com/Avaneesh-Inamdar/Stream-Tube-Desktop.git
synced 2026-12-03 21:16:36 +00:00
Fix autoplay, SponsorBlock mute, mini player and lower playback load
- Autoplay goes on to the next video while the window is minimized, in the tray or behind another window (#316, #349): it used the active window, which is None then. - SponsorBlock mute segments unmute again (#322): mpv's mute echo was taken for the user muting. - The mini player keeps the video picked from "Up next" instead of going back to the first one (#321). - 1440p/4K streams render above 1080p when the player is that large (#370). - Ambient colour changes no longer re-render the whole watch page several times a second, and not at all when the glow isn't shown (#307, #332). - With the window minimized or in the tray, video decoding stops while the audio goes on (Windows).
This commit is contained in:
parent
7cd4595048
commit
c16445a404
5 changed files with 87 additions and 23 deletions
|
|
@ -54,6 +54,8 @@ pub struct Player {
|
|||
engine_generation: u64,
|
||||
foreground: bool,
|
||||
in_pip: bool,
|
||||
/// the main window is minimized or hidden in the tray (see `set_window_hidden`)
|
||||
window_hidden: bool,
|
||||
pub init_error: Option<String>,
|
||||
pub video: Option<VideoItem>,
|
||||
pub streams: Option<Arc<StreamInfo>>,
|
||||
|
|
@ -85,6 +87,8 @@ pub struct Player {
|
|||
skipped: HashSet<String>,
|
||||
pub sb_prompt: Option<Segment>,
|
||||
muted_by_sb: bool,
|
||||
/// when the ambient colour last changed (see `MpvEvent::Frame`)
|
||||
ambient_at: std::time::Instant,
|
||||
pub caption: Option<usize>,
|
||||
pub queue: Vec<VideoItem>,
|
||||
pub queue_pos: Option<usize>,
|
||||
|
|
@ -206,6 +210,7 @@ impl Player {
|
|||
engine_generation: 0,
|
||||
foreground: true,
|
||||
in_pip: false,
|
||||
window_hidden: false,
|
||||
init_error: None,
|
||||
video: None,
|
||||
streams: None,
|
||||
|
|
@ -237,6 +242,7 @@ impl Player {
|
|||
skipped: HashSet::new(),
|
||||
sb_prompt: None,
|
||||
muted_by_sb: false,
|
||||
ambient_at: std::time::Instant::now(),
|
||||
caption: None,
|
||||
queue: Vec::new(),
|
||||
queue_pos: None,
|
||||
|
|
@ -310,10 +316,25 @@ impl Player {
|
|||
self.mpv.is_some()
|
||||
}
|
||||
|
||||
/// The picture is on screen somewhere: decoding and converting frames nobody
|
||||
/// sees (a background tab, a minimized or tray-hidden window) only costs CPU and GPU.
|
||||
fn video_visible(&self) -> bool {
|
||||
(self.foreground && !self.window_hidden) || self.in_pip
|
||||
}
|
||||
|
||||
/// Background play with the window minimized or in the tray: audio goes on,
|
||||
/// video decoding stops until the window is back.
|
||||
pub(crate) fn set_window_hidden(&mut self, hidden: bool, cx: &mut Context<Self>) {
|
||||
if self.window_hidden != hidden {
|
||||
self.window_hidden = hidden;
|
||||
self.update_video_output(cx);
|
||||
}
|
||||
}
|
||||
|
||||
fn update_video_output(&mut self, cx: &mut Context<Self>) {
|
||||
let visible = self.foreground || self.in_pip;
|
||||
let visible = self.video_visible();
|
||||
if let Some(mpv) = &self.mpv {
|
||||
mpv.set("vid", if visible { "auto" } else { "no" });
|
||||
mpv.set("vid", if visible && !self.audio_only { "auto" } else { "no" });
|
||||
}
|
||||
if !visible {
|
||||
if let Some(frame) = self.frame.take() {
|
||||
|
|
@ -345,7 +366,7 @@ impl Player {
|
|||
let hw = if s.hwdec { "auto-copy" } else { "no" };
|
||||
let opts: Vec<(&str, &str)> = vec![
|
||||
("vo", "libmpv"),
|
||||
("vid", if self.foreground || self.in_pip { "auto" } else { "no" }),
|
||||
("vid", if self.video_visible() { "auto" } else { "no" }),
|
||||
("hwdec", hw),
|
||||
("keep-open", "yes"),
|
||||
("idle", "yes"),
|
||||
|
|
@ -497,10 +518,14 @@ impl Player {
|
|||
}
|
||||
self.hold_picture = None;
|
||||
self.ambient_tick += 1;
|
||||
if self.ambient_tick % 12 == 1 && store::settings_snapshot().ambient_mode {
|
||||
// Each change re-renders the whole watch page around the glow: follow
|
||||
// the picture's colours about once a second, and only clear changes
|
||||
let ambient_due = self.ambient.is_none() || self.ambient_at.elapsed() >= std::time::Duration::from_millis(900);
|
||||
if ambient_due && self.ambient_tick % 12 == 1 && store::settings_snapshot().ambient_mode {
|
||||
if let Some(c) = average_color(&f.data, f.width, f.height) {
|
||||
let changed = self.ambient.map(|o| (o.r - c.r).abs() + (o.g - c.g).abs() + (o.b - c.b).abs() > 0.04).unwrap_or(true);
|
||||
let changed = self.ambient.map(|o| (o.r - c.r).abs() + (o.g - c.g).abs() + (o.b - c.b).abs() > 0.08).unwrap_or(true);
|
||||
if changed {
|
||||
self.ambient_at = std::time::Instant::now();
|
||||
self.ambient = Some(c);
|
||||
cx.emit(PlayerEvent::Ambient);
|
||||
}
|
||||
|
|
@ -565,10 +590,10 @@ impl Player {
|
|||
MpvEvent::Prop(name, val) => {
|
||||
// While frames are flowing, the frame event already redraws the player;
|
||||
// only redraw for clock/cache updates when no video frames arrive.
|
||||
// A hidden window shows no clock either: a couple of updates a second do.
|
||||
let quiet = matches!(name, "time-pos" | "demuxer-cache-time" | "demuxer-cache-state")
|
||||
&& !self.paused
|
||||
&& !self.audio_only
|
||||
&& self.frame.is_some()
|
||||
&& ((!self.audio_only && self.frame.is_some()) || self.window_hidden)
|
||||
&& self.last_prop_notify.elapsed() < std::time::Duration::from_millis(500);
|
||||
match (name, val) {
|
||||
("time-pos", PropValue::Double(t)) => {
|
||||
|
|
@ -619,7 +644,9 @@ impl Player {
|
|||
}
|
||||
("speed", PropValue::Double(s)) => self.speed = s,
|
||||
("volume", PropValue::Double(v)) => self.volume = v,
|
||||
("mute", PropValue::Flag(m)) => self.muted = m,
|
||||
// a SponsorBlock mute segment isn't the user muting: keep `muted` the
|
||||
// user's own choice, or leaving the segment never unmutes (#322)
|
||||
("mute", PropValue::Flag(m)) if !self.muted_by_sb => self.muted = m,
|
||||
("dwidth", PropValue::Double(w)) => self.video_size.0 = w,
|
||||
("dheight", PropValue::Double(h)) => self.video_size.1 = h,
|
||||
_ => {}
|
||||
|
|
@ -814,7 +841,7 @@ impl Player {
|
|||
/// without an EndFile event. Compare rendered pictures with the media clock.
|
||||
fn recover_stalled_picture(&mut self, cx: &mut Context<Self>) -> bool {
|
||||
let (Some(mpv), Some(info)) = (&self.mpv, &self.streams) else { return false };
|
||||
if !(self.foreground || self.in_pip) || self.audio_only || self.loading || self.paused || self.buffering || self.seeking || self.eof || self.error.is_some()
|
||||
if !self.video_visible() || self.audio_only || self.loading || self.paused || self.buffering || self.seeking || self.eof || self.error.is_some()
|
||||
|| info.is_live || (!info.hls_only && info.video.is_empty() && info.muxed.is_empty())
|
||||
|| (self.duration > 0.0 && self.duration - self.time < 2.0)
|
||||
{
|
||||
|
|
@ -876,6 +903,8 @@ impl Player {
|
|||
// SponsorBlock
|
||||
let s = store::settings_snapshot();
|
||||
if !s.sponsorblock || self.segments.is_empty() {
|
||||
// a new video without segments (or SponsorBlock turned off) mid-segment
|
||||
self.set_muted_by_sb(false);
|
||||
return;
|
||||
}
|
||||
let mut prompt = None;
|
||||
|
|
@ -940,13 +969,17 @@ impl Player {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
if in_mute != self.muted_by_sb {
|
||||
self.muted_by_sb = in_mute;
|
||||
self.set_muted_by_sb(in_mute);
|
||||
self.sb_prompt = prompt;
|
||||
}
|
||||
|
||||
fn set_muted_by_sb(&mut self, on: bool) {
|
||||
if on != self.muted_by_sb {
|
||||
self.muted_by_sb = on;
|
||||
if let Some(m) = &self.mpv {
|
||||
m.set_flag("mute", in_mute || self.muted);
|
||||
m.set_flag("mute", on || self.muted);
|
||||
}
|
||||
}
|
||||
self.sb_prompt = prompt;
|
||||
}
|
||||
|
||||
pub(crate) fn save_progress(&mut self, force: bool) {
|
||||
|
|
@ -1309,7 +1342,7 @@ impl Player {
|
|||
if let Some(v) = &self.video {
|
||||
opts.push(mpv_media_title_option(&v.title));
|
||||
}
|
||||
mpv.set("vid", if self.audio_only { "no" } else { "auto" });
|
||||
mpv.set("vid", if self.audio_only || !self.video_visible() { "no" } else { "auto" });
|
||||
self.loading = true;
|
||||
// mpv 0.38 added an index argument; older libmpv (Ubuntu 22.04's 0.34) takes options third
|
||||
let joined = opts.join(",");
|
||||
|
|
@ -1755,7 +1788,7 @@ impl Player {
|
|||
pub fn toggle_mute(&mut self) {
|
||||
self.muted = !self.muted;
|
||||
if let Some(m) = &self.mpv {
|
||||
m.set_flag("mute", self.muted);
|
||||
m.set_flag("mute", self.muted || self.muted_by_sb);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -318,13 +318,14 @@ impl Render for PlayerView {
|
|||
canvas(
|
||||
move |bounds, _, _| {
|
||||
if let Some(m) = &mpv {
|
||||
// Render at the video's own resolution (capped at 1080p), or
|
||||
// smaller when the player shows it smaller: every frame is
|
||||
// converted, copied and uploaded, so pixels nobody sees cost
|
||||
// CPU, memory and GPU time. The GPU scales the image for display.
|
||||
// Render at the video's own resolution, or smaller when the
|
||||
// player shows it smaller: every frame is converted, copied and
|
||||
// uploaded, so pixels nobody sees cost CPU, memory and GPU time.
|
||||
// The GPU scales the image for display. A 1440p/4K stream only
|
||||
// renders above 1080p when the player is that large (#370).
|
||||
let (vw, vh) = (vsize.0 as f32, vsize.1 as f32);
|
||||
if vw > 0.0 && vh > 0.0 {
|
||||
let cap = (1920.0 / vw).min(1080.0 / vh).min(1.0);
|
||||
let cap = (3840.0 / vw).min(2160.0 / vh).min(1.0);
|
||||
let aw = f32::from(bounds.size.width) * scale;
|
||||
let ah = f32::from(bounds.size.height) * scale;
|
||||
// enough to cover the player; cropping modes enlarge a part of the frame
|
||||
|
|
|
|||
|
|
@ -218,6 +218,11 @@ pub fn go_back_keep_playing(window: &mut Window, cx: &mut App) {
|
|||
exit_fullscreen(window, cx);
|
||||
if let Some(app) = cx.try_global::<AppHandle>().and_then(|h| h.0.upgrade()) {
|
||||
app.update(cx, |a, cx| {
|
||||
// skip earlier watch pages (videos picked from "Up next"): going back to one
|
||||
// would load that video instead of keeping this one in the mini player (#321)
|
||||
while matches!(a.history.last(), Some((Route::Watch { .. } | Route::Shorts(_), _))) {
|
||||
a.history.pop();
|
||||
}
|
||||
if a.history.is_empty() {
|
||||
a.go(Route::Home, window, cx);
|
||||
} else {
|
||||
|
|
@ -941,6 +946,13 @@ impl AppView {
|
|||
subs.push(window.observe_window_visibility({
|
||||
let paused_by_us = std::rc::Rc::new(std::cell::RefCell::new(Vec::<WeakEntity<Player>>::new()));
|
||||
move |vis, window, cx| {
|
||||
// minimized or in the tray, nobody sees the video: stop decoding it (audio
|
||||
// and a picture-in-picture window go on). Only when Windows says minimized:
|
||||
// elsewhere "not visible" can also mean covered by another window.
|
||||
let hidden = cfg!(windows) && !vis.is_visible() && window_minimized(window);
|
||||
for player in tab_players(cx) {
|
||||
player.update(cx, |p, cx| p.set_window_hidden(hidden, cx));
|
||||
}
|
||||
if !vis.is_visible() {
|
||||
if store::settings_snapshot().background_play || !window_minimized(window) {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -106,7 +106,15 @@ impl WatchPage {
|
|||
let sub = cx.subscribe(&pl, |this: &mut Self, _, ev: &PlayerEvent, cx| {
|
||||
match ev {
|
||||
PlayerEvent::Ended => this.on_ended(cx),
|
||||
PlayerEvent::Ambient | PlayerEvent::QueueChanged => cx.notify(),
|
||||
PlayerEvent::QueueChanged => cx.notify(),
|
||||
// Re-rendering the page lays out every comment, queue row and related
|
||||
// video again, so only do it when the glow is actually shown (#307, #332)
|
||||
PlayerEvent::Ambient => {
|
||||
let s = store::settings_snapshot();
|
||||
if s.ambient_mode && !s.theater_mode && !cx.global::<UiState>().fullscreen && !crate::ui::music::on(cx) {
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
|
@ -535,7 +543,7 @@ impl WatchPage {
|
|||
let next = playing.autoplay_next.clone().filter(|_| s.autoplay_next);
|
||||
let looping = playing.loop_video;
|
||||
if next.is_none() && !looping && s.exit_fullscreen_at_end && cx.global::<UiState>().fullscreen {
|
||||
if let Some(w) = cx.active_window() {
|
||||
if let Some(w) = crate::ui::pip::main_window(cx) {
|
||||
let _ = w.update(cx, |_, window, cx| crate::ui::app::exit_fullscreen(window, cx));
|
||||
}
|
||||
}
|
||||
|
|
@ -553,7 +561,9 @@ impl WatchPage {
|
|||
let p = playback.read(cx);
|
||||
let still_ended = player(cx) == playback && p.eof && p.video.as_ref().is_some_and(|video| video.id == finished);
|
||||
if still_ended {
|
||||
if let Some(w) = cx.active_window() {
|
||||
// not `active_window`: that is None while the app is minimized, in the
|
||||
// tray or behind another window, and autoplay must still go on (#316)
|
||||
if let Some(w) = crate::ui::pip::main_window(cx) {
|
||||
let _ = w.update(cx, |_, window, cx| navigate(Route::Watch { id, playlist: None, start: None }, window, cx));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,14 @@ pub fn handle(cx: &App) -> Option<AnyWindowHandle> {
|
|||
cx.try_global::<PipWindow>().and_then(|p| p.0.as_ref().map(|(handle, _)| *handle))
|
||||
}
|
||||
|
||||
/// The main app window: any open window that isn't the picture-in-picture one.
|
||||
/// Works while the app is minimized, hidden to the tray or behind another app,
|
||||
/// when `cx.active_window()` is `None`.
|
||||
pub fn main_window(cx: &App) -> Option<AnyWindowHandle> {
|
||||
let pip = handle(cx);
|
||||
cx.windows().into_iter().find(|w| Some(*w) != pip)
|
||||
}
|
||||
|
||||
pub(crate) fn owner(cx: &App) -> Option<Entity<Player>> {
|
||||
cx.try_global::<PipWindow>().and_then(|p| p.0.as_ref().map(|(_, player)| player.clone()))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue