From bb80d168af2c5e36903dd61473fdbd3840846d7f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 27 Oct 2021 15:20:30 +0200 Subject: Stop download of thumbnail if the thumbnail is no longer visible on the screen --- TODO | 4 +++- include/AsyncImageLoader.hpp | 20 +++++++++++--------- src/AsyncImageLoader.cpp | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/TODO b/TODO index 08ae2ee..35594ab 100644 --- a/TODO +++ b/TODO @@ -201,4 +201,6 @@ Add keybindings for image control for 4chan. Fix youtube videos that are age restricted AND do not allow embedding. Is that even possible? Use local lbry instead of odysee when I figure out the incorrect instructions of to use lighthouse locally. ffmpeg (and mpv) is very slow at playing streams (mostly affects lbry and certain peertube videos) for some reason. -Allow specifying start/end range for video/music downloads. \ No newline at end of file +Allow specifying start/end range for video/music downloads. +Limit text input length for 4chan posts to the server limit. +Allow creating a new thread on 4chan. \ No newline at end of file diff --git a/include/AsyncImageLoader.hpp b/include/AsyncImageLoader.hpp index 77fd5b4..94b225d 100644 --- a/include/AsyncImageLoader.hpp +++ b/include/AsyncImageLoader.hpp @@ -54,6 +54,15 @@ namespace QuickMedia { // Note: this method is not thread-safe void update(); private: + struct Download { + ReadProgram read_program; + int64_t download_start = 0; + Path thumbnail_path; + std::shared_ptr thumbnail_data; + sf::Vector2i resize_target_size; + std::string url; + }; + AsyncImageLoader(); ~AsyncImageLoader(); AsyncImageLoader(AsyncImageLoader &other) = delete; @@ -65,16 +74,9 @@ namespace QuickMedia { // Returns -1 if all threads are busy int get_free_load_index() const; - private: - struct Download { - ReadProgram read_program; - int64_t download_start = 0; - Path thumbnail_path; - std::shared_ptr thumbnail_data; - sf::Vector2i resize_target_size; - std::string url; - }; + void reset_download(Download &download); + private: std::mutex download_mutex; // TODO: Use curl single-threaded multi-download feature instead Download downloads[NUM_IMAGE_LOAD_PARALLEL]; diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index f926a8d..762408c 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -236,14 +236,7 @@ namespace QuickMedia { } else { fprintf(stderr, "Thumbnail download failed for %s\n", download.url.c_str()); } - - std::lock_guard lock(download_mutex); - close(download.read_program.read_fd); - download.read_program.pid = -1; - download.read_program.read_fd = -1; - download.thumbnail_path.data.clear(); - download.url.c_str(); - download.thumbnail_data = nullptr; + reset_download(download); } } @@ -336,6 +329,19 @@ namespace QuickMedia { bool loaded_textures_changed = false; for(auto it = thumbnails.begin(); it != thumbnails.end();) { if(it->second->counter != counter) { + { + for(int i = 0; i < NUM_IMAGE_LOAD_PARALLEL; ++i) { + Download &download = downloads[i]; + if(download.read_program.pid == -1) + continue; + + if(download.url == it->first) { + reset_download(download); + break; + } + } + } + image_load_queue.erase_if([&it](ThumbnailLoadData &load_data) { return load_data.path.data == it->first; }); @@ -358,4 +364,19 @@ namespace QuickMedia { } return -1; } + + void AsyncImageLoader::reset_download(Download &download) { + std::lock_guard lock(download_mutex); + if(download.read_program.pid != -1) { + kill(download.read_program.pid, SIGTERM); + download.read_program.pid = -1; + } + if(download.read_program.read_fd != -1) { + close(download.read_program.read_fd); + download.read_program.read_fd = -1; + } + download.thumbnail_path.data.clear(); + download.url.c_str(); + download.thumbnail_data = nullptr; + } } -- cgit v1.2.3