From 5ec1811fa7499386f324f13a3a49dbe7d8ea6741 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 19 Jun 2021 13:51:37 +0200 Subject: Fix youtube sometimes needing a redirect for media url --- src/QuickMedia.cpp | 77 +++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 35 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index a4ea258..c02b9ab 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -2295,10 +2295,13 @@ namespace QuickMedia { TaskResult Program::run_task_with_loading_screen(std::function callback) { assert(std::this_thread::get_id() == main_thread_id); + if(running_task_with_loading_screen) + return callback() ? TaskResult::TRUE : TaskResult::FALSE; + running_task_with_loading_screen = true; idle_active_handler(); - AsyncTask task = callback; + TaskResult task_result = TaskResult::TRUE; window_size.x = window.getSize().x; window_size.y = window.getSize().y; @@ -2312,20 +2315,20 @@ namespace QuickMedia { window.setView(sf::View(visible_area)); } else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) { task.cancel(); - return TaskResult::CANCEL; + task_result = TaskResult::CANCEL; + goto task_end; } } if(handle_window_close()) { task.cancel(); - return TaskResult::CANCEL; + task_result = TaskResult::CANCEL; + goto task_end; } if(task.ready()) { - if(task.get()) - return TaskResult::TRUE; - else - return TaskResult::FALSE; + task_result = task.get() ? TaskResult::TRUE : TaskResult::FALSE; + goto task_end; } window.clear(back_color); @@ -2335,7 +2338,9 @@ namespace QuickMedia { window.display(); } - return TaskResult::TRUE; + task_end: + running_task_with_loading_screen = false; + return task_result; } static bool video_url_supports_timestamp(const std::string &url) { @@ -2507,8 +2512,35 @@ namespace QuickMedia { if(!reuse_media_source) { std::string new_title; - TaskResult load_result = run_task_with_loading_screen([video_page, &new_title, &channel_url, &media_chapters]() { - return video_page->load(new_title, channel_url, media_chapters) == PluginResult::OK; + video_url.clear(); + audio_url.clear(); + + TaskResult load_result = run_task_with_loading_screen([this, video_page, &new_title, &channel_url, &media_chapters, largest_monitor_height, &has_embedded_audio, &video_url, &audio_url, &is_audio_only, &previous_page, is_youtube, download_if_streaming_fails]() { + if(video_page->load(new_title, channel_url, media_chapters) != PluginResult::OK) + return false; + + if(!no_video) + video_url = video_page->get_video_url(largest_monitor_height, has_embedded_audio); + + if(video_url.empty() || no_video) { + video_url = video_page->get_audio_url(); + if(video_url.empty()) { + video_url = video_page->get_url(); + has_embedded_audio = true; + } else { + is_audio_only = true; + has_embedded_audio = false; + } + } else if(!has_embedded_audio) { + audio_url = video_page->get_audio_url(); + } + + if(!is_youtube && download_if_streaming_fails) { + if(!video_download_if_non_streamable(video_url, audio_url, is_audio_only, has_embedded_audio, previous_page)) + return false; + } + + return true; }); if(!new_title.empty()) @@ -2524,31 +2556,6 @@ namespace QuickMedia { go_to_previous_page = true; return; } - - video_url.clear(); - audio_url.clear(); - if(!no_video) - video_url = video_page->get_video_url(largest_monitor_height, has_embedded_audio); - - if(video_url.empty() || no_video) { - video_url = video_page->get_audio_url(); - if(video_url.empty()) { - video_url = video_page->get_url(); - has_embedded_audio = true; - } else { - is_audio_only = true; - has_embedded_audio = false; - } - } else if(!has_embedded_audio) { - audio_url = video_page->get_audio_url(); - } - - if(!is_youtube && download_if_streaming_fails) { - if(!video_download_if_non_streamable(video_url, audio_url, is_audio_only, has_embedded_audio, previous_page)) { - go_to_previous_page = true; - return; - } - } } const bool is_resume_go_back = !start_time.empty(); -- cgit v1.2.3