about summary refs log tree commit diff stats
path: root/src/watch
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/watch/events.rs40
-rw-r--r--src/watch/mod.rs7
2 files changed, 34 insertions, 13 deletions
diff --git a/src/watch/events.rs b/src/watch/events.rs
index c9f8373..0873bc2 100644
--- a/src/watch/events.rs
+++ b/src/watch/events.rs
@@ -8,12 +8,12 @@
 // You should have received a copy of the License along with this program.
 // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
 
-use std::{env::current_exe, mem, usize};
+use std::{env::current_exe, mem, time::Duration, usize};
 
 use anyhow::{bail, Result};
 use libmpv2::{events::Event, EndFileReason, Mpv};
-use log::{debug, info};
-use tokio::process::Command;
+use log::{debug, error, info, warn};
+use tokio::{process::Command, time};
 
 use crate::{
     app::App,
@@ -42,11 +42,19 @@ impl MpvEventHandler {
     }
 
     /// Checks, whether new videos are ready to be played
-    pub async fn possibly_add_new_videos(&mut self, app: &App, mpv: &Mpv) -> Result<usize> {
+    pub async fn possibly_add_new_videos(
+        &mut self,
+        app: &App,
+        mpv: &Mpv,
+        force_message: bool,
+    ) -> Result<usize> {
         let play_things = get_videos(app, &[VideoStatus::Cached], Some(false)).await?;
 
         // There is nothing to watch
         if play_things.len() == 0 {
+            if force_message {
+                Self::message(&mpv, "No new videos available to add", "3000")?;
+            }
             return Ok(0);
         }
 
@@ -76,6 +84,9 @@ impl MpvEventHandler {
             self.current_playlist.push(play_thing.extractor_hash);
         }
 
+        if force_message || num > 0 {
+            Self::message(&mpv, format!("Added {} videos", num).as_str(), "3000")?;
+        }
         Ok(num)
     }
 
@@ -95,6 +106,7 @@ impl MpvEventHandler {
             let video_hash = self.current_playlist[(index) as usize].clone();
             self.mark_video_watched(app, &video_hash).await?;
         }
+        error!("Expected a current video, but found none (while trying to mark it watched)");
         Ok(())
     }
 
@@ -104,6 +116,7 @@ impl MpvEventHandler {
             self.currently_playing_index = None;
             set_state_change(&app, video_hash, false).await?;
         }
+        error!("Expected a current video, but found none (while trying to mark it inactive)");
         Ok(())
     }
     async fn mark_video_active(&mut self, app: &App, playlist_index: usize) -> Result<()> {
@@ -118,10 +131,22 @@ impl MpvEventHandler {
         let options = get_video_mpv_opts(app, hash).await?;
 
         mpv.set_property("speed", options.playback_speed)?;
-
         Ok(())
     }
 
+    /// Check if the playback queue is empty
+    pub async fn check_idle(&mut self, app: &App, mpv: &Mpv) -> Result<bool> {
+        if self.current_playlist.is_empty() {
+            warn!("There is nothing to watch yet. Will idle, until something is available");
+            self.possibly_add_new_videos(app, mpv, false).await?;
+
+            time::sleep(Duration::from_secs(10)).await;
+            Ok(true)
+        } else {
+            Ok(false)
+        }
+    }
+
     /// This will return [`true`], if the event handling should be stopped
     pub async fn handle_mpv_event<'a>(
         &mut self,
@@ -162,7 +187,7 @@ impl MpvEventHandler {
                 }
             },
             Event::StartFile(playlist_index) => {
-                self.possibly_add_new_videos(app, &mpv).await?;
+                self.possibly_add_new_videos(app, &mpv, false).await?;
 
                 self.mark_video_active(app, (playlist_index - 1) as usize)
                     .await?;
@@ -243,8 +268,7 @@ impl MpvEventHandler {
                         Self::message(&mpv, "Marked the video watched", "3000")?;
                     }
                     &["yt-check-new-videos"] => {
-                        let num = self.possibly_add_new_videos(app, mpv).await?;
-                        Self::message(&mpv, format!("Added {} videos", num).as_str(), "3000")?;
+                        self.possibly_add_new_videos(app, mpv, true).await?;
                     }
                     other => {
                         debug!("Unknown message: {}", other.join(" "))
diff --git a/src/watch/mod.rs b/src/watch/mod.rs
index 815e208..9eb1c18 100644
--- a/src/watch/mod.rs
+++ b/src/watch/mod.rs
@@ -77,11 +77,6 @@ pub async fn watch(app: &App) -> Result<()> {
         play_things.len()
     );
 
-    // There is nothing to watch
-    if play_things.len() == 0 {
-        return Ok(());
-    }
-
     let mut playlist_cache: Vec<ExtractorHash> = Vec::with_capacity(play_things.len());
 
     for play_thing in play_things {
@@ -100,6 +95,8 @@ pub async fn watch(app: &App) -> Result<()> {
 
     let mut mpv_event_handler = MpvEventHandler::from_playlist(playlist_cache);
     loop {
+        while mpv_event_handler.check_idle(&app, &mpv).await? {}
+
         if let Some(ev) = ev_ctx.wait_event(600.) {
             match ev {
                 Ok(event) => {