From eb89e4048642a4ebf73635730e2a0aa960e7ecdd Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 7 Aug 2021 16:21:05 +0200 Subject: Fix seeking in youtube videos larger than 2gb, launch launcher when running qm without any args --- src/Downloader.cpp | 2 +- src/QuickMedia.cpp | 9 ++++++--- src/plugins/youtube/Signature.cpp | 2 +- src/plugins/youtube/YoutubeMediaProxy.cpp | 27 +++++++++++++++------------ 4 files changed, 23 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/Downloader.cpp b/src/Downloader.cpp index 6f6d709..450bff3 100644 --- a/src/Downloader.cpp +++ b/src/Downloader.cpp @@ -61,7 +61,7 @@ namespace QuickMedia { if(!content_length_str.empty()) { errno = 0; char *endptr; - const long content_length_tmp = strtoll(content_length_str.c_str(), &endptr, 10); + const int64_t content_length_tmp = strtoll(content_length_str.c_str(), &endptr, 10); if(endptr != content_length_str.c_str() && errno == 0) { std::lock_guard lock(content_length_mutex); content_length = content_length_tmp; diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index bb92484..86177dc 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -347,7 +347,7 @@ namespace QuickMedia { } static void usage() { - fprintf(stderr, "usage: quickmedia [--no-video] [--use-system-mpv-config] [--dir ] [-e ] [youtube-url]\n"); + fprintf(stderr, "usage: quickmedia [plugin] [--no-video] [--use-system-mpv-config] [--dir ] [-e ] [youtube-url]\n"); fprintf(stderr, "OPTIONS:\n"); fprintf(stderr, " plugin The plugin to use. Should be either launcher, 4chan, manga, manganelo, manganelos, mangatown, mangakatana, mangadex, readm, onimanga, youtube, soundcloud, nyaa.si, matrix, saucenao, hotexamples, file-manager, stdin, pornhub, spankbang, xvideos or xhamster\n"); fprintf(stderr, " --no-video Only play audio when playing a video. Disabled by default\n"); @@ -357,7 +357,7 @@ namespace QuickMedia { fprintf(stderr, " --dir Set the start directory when using file-manager. Default is the the users home directory\n"); fprintf(stderr, " -e Embed QuickMedia into another window\n"); fprintf(stderr, "EXAMPLES:\n"); - fprintf(stderr, " quickmedia launcher\n"); + fprintf(stderr, " quickmedia\n"); fprintf(stderr, " quickmedia --upscale-images-always manganelo\n"); fprintf(stderr, " quickmedia https://www.youtube.com/watch?v=jHg91NVHh3s\n"); fprintf(stderr, " echo -e \"hello\\nworld\" | quickmedia stdin\n"); @@ -381,11 +381,14 @@ namespace QuickMedia { } int Program::run(int argc, char **argv) { - if(argc < 2) { + if(argc < 1) { usage(); return -1; } + if(argc == 1) + plugin_name = "launcher"; + Window parent_window = None; std::vector tabs; const char *url = nullptr; diff --git a/src/plugins/youtube/Signature.cpp b/src/plugins/youtube/Signature.cpp index 394abf0..7e5e835 100644 --- a/src/plugins/youtube/Signature.cpp +++ b/src/plugins/youtube/Signature.cpp @@ -77,7 +77,7 @@ namespace QuickMedia { errno = 0; char *endptr; - const long value_int = strtoll(value_args.c_str(), &endptr, 10); + const int64_t value_int = strtoll(value_args.c_str(), &endptr, 10); if(endptr != value_args.c_str() && errno == 0) new_func_calls.push_back({ std::move(func_name), value_int }); else diff --git a/src/plugins/youtube/YoutubeMediaProxy.cpp b/src/plugins/youtube/YoutubeMediaProxy.cpp index e67a920..90dedbf 100644 --- a/src/plugins/youtube/YoutubeMediaProxy.cpp +++ b/src/plugins/youtube/YoutubeMediaProxy.cpp @@ -18,7 +18,7 @@ namespace QuickMedia { static const int MAX_BUFFER_SIZE = 65536; - static const int RANGE = 524287; + static const int64_t RANGE = 524287; static const int64_t THROTTLED_DOWNLOAD_LIMIT_KB = 80; // TODO: What about people with really slow internet? What if the video player cache is not working and download is stuck, leading to false download speed calculation? static const int64_t THROTTLED_DURATION_SECS = 2; static const char download_error_response_msg[] = @@ -34,7 +34,7 @@ namespace QuickMedia { // TODO: Restrict range end to remote file size (content-length which we have saved). // TODO: Check if custom youtube redirect code is needed - bool YoutubeMediaProxy::start_download(const std::string &media_url, ReadProgram &read_program, int range_start, bool include_header, bool is_livestream, int livestream_sequence) { + bool YoutubeMediaProxy::start_download(const std::string &media_url, ReadProgram &read_program, int64_t range_start, bool include_header, bool is_livestream, int livestream_sequence) { std::string r = std::to_string(range_start) + "-" + std::to_string(range_start + RANGE); std::string url = media_url + "&rn=" + std::to_string(rn) + "&rbuf=" + std::to_string(rbuf); @@ -214,13 +214,13 @@ namespace QuickMedia { } // Returns 0 if start range is not found - static int header_extract_start_range(const std::string &header) { + static int64_t header_extract_start_range(const std::string &header) { std::string range = header_extract_value(header, "range"); if(range.empty()) return 0; - int start_range = 0; - if(sscanf(range.c_str(), " bytes=%d", &start_range) != 1) + int64_t start_range = 0; + if(sscanf(range.c_str(), " bytes=%lld", &start_range) != 1) return 0; return start_range; @@ -259,9 +259,7 @@ namespace QuickMedia { download_started = false; throttle_started = false; - int new_start_range = header_extract_start_range(client_request_buffer); - //fprintf(stderr, "got new range from client: %d\n", new_start_range); - //if(new_start_range >= 0) { + const int64_t new_start_range = header_extract_start_range(client_request_buffer); if(downloader_read_program.pid != -1) { kill(downloader_read_program.pid, SIGTERM); wait_program(downloader_read_program.pid); @@ -269,7 +267,6 @@ namespace QuickMedia { } clear_download_state(); return update_download_program_status(false, new_start_range, true); - //} } else { if(client_request_buffer.size() > MAX_BUFFER_SIZE) { client_request_finished = true; @@ -345,6 +342,12 @@ namespace QuickMedia { if(!restart_download) return Error::OK; + if(new_range_start == -1) { + download_header_finished = true; + download_header_sent = true; + download_header_remaining_sent = true; + } + const bool start_download_success = start_download(youtube_media_url, downloader_read_program, current_download_range, new_range_start != -1); if(!start_download_success) { fprintf(stderr, "YoutubeStaticMediaProxy::update_download_program_status: failed to start download\n"); @@ -461,7 +464,7 @@ namespace QuickMedia { throttle_started = false; struct timespec tp; - clock_gettime(CLOCK_BOOTTIME, &tp); + clock_gettime(CLOCK_MONOTONIC, &tp); download_start_time = tp.tv_sec; } total_downloaded_bytes += downloader_num_read_bytes; @@ -470,7 +473,7 @@ namespace QuickMedia { if(download_started) { struct timespec tp; - clock_gettime(CLOCK_BOOTTIME, &tp); + clock_gettime(CLOCK_MONOTONIC, &tp); int64_t time_elapsed = tp.tv_sec - download_start_time; int64_t download_speed_kb_sec = 0; @@ -780,4 +783,4 @@ namespace QuickMedia { address = "fd://" + std::to_string(fd[0]); return true; } -} \ No newline at end of file +} -- cgit v1.2.3