From e4792f46d545263d16db21bd0caf71345a69b63f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 22 Jun 2021 16:07:20 +0200 Subject: Only do youtube redirect on failure to load video --- src/QuickMedia.cpp | 67 +++++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 38 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index e155fb3..7305788 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -2308,10 +2308,10 @@ 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; + assert(std::this_thread::get_id() == main_thread_id); idle_active_handler(); AsyncTask task = callback; @@ -2497,12 +2497,13 @@ namespace QuickMedia { }; std::string channel_url; - AsyncTask video_tasks; + AsyncTask video_tasks; std::function video_event_callback; bool go_to_previous_page = false; std::string video_url; std::string audio_url; + bool has_embedded_audio = true; bool in_seeking = false; sf::Clock seeking_start_timer; @@ -2513,7 +2514,7 @@ namespace QuickMedia { std::string prev_start_time; std::vector media_chapters; - auto load_video_error_check = [this, &prev_start_time, &media_chapters, &in_seeking, &video_url, &audio_url, &video_title, &video_tasks, &channel_url, previous_page, &go_to_previous_page, &video_loaded, video_page, &video_event_callback, &on_window_create, &video_player_window, is_youtube, is_matrix, download_if_streaming_fails](std::string start_time = "", bool reuse_media_source = false) mutable { + auto load_video_error_check = [this, &prev_start_time, &media_chapters, &in_seeking, &video_url, &audio_url, &has_embedded_audio, &video_title, &video_tasks, &channel_url, previous_page, &go_to_previous_page, &video_loaded, video_page, &video_event_callback, &on_window_create, &video_player_window, is_youtube, is_matrix, download_if_streaming_fails](std::string start_time = "", bool reuse_media_source = false) mutable { video_player.reset(); channel_url.clear(); video_loaded = false; @@ -2521,13 +2522,13 @@ namespace QuickMedia { video_player_window = None; bool is_audio_only = no_video; - bool has_embedded_audio = true; const int largest_monitor_height = get_largest_monitor_height(disp); if(!reuse_media_source) { std::string new_title; video_url.clear(); audio_url.clear(); + has_embedded_audio = true; 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) @@ -2579,7 +2580,7 @@ namespace QuickMedia { watched_videos.insert(video_page->get_url()); - video_player = std::make_unique(is_audio_only, use_system_mpv_config, is_matrix && !is_youtube, video_event_callback, on_window_create, resources_root, largest_monitor_height); + video_player = std::make_unique(is_audio_only, use_system_mpv_config, is_matrix && !is_youtube, video_event_callback, on_window_create, resources_root, largest_monitor_height, plugin_name); VideoPlayer::Error err = video_player->load_video(video_url.c_str(), audio_url.c_str(), window.getSystemHandle(), is_youtube, video_title, start_time, media_chapters); if(err != VideoPlayer::Error::OK) { std::string err_msg = "Failed to play url: "; @@ -2593,10 +2594,8 @@ namespace QuickMedia { if(!is_resume_go_back) { std::string url = video_page->get_url(); - video_tasks = AsyncTask([video_page, url]() { - BodyItems related_videos = video_page->get_related_media(url); + video_tasks = AsyncTask([video_page, url]() { video_page->mark_watched(); - return related_videos; }); } @@ -2744,23 +2743,15 @@ namespace QuickMedia { XUnmapWindow(disp, video_player_window); XSync(disp, False); - TaskResult task_result = run_task_with_loading_screen([&video_tasks, &related_videos]() { - while(true) { - if(program_is_dead_in_current_thread()) - return false; - - if(video_tasks.ready()) { - related_videos = video_tasks.get(); - return true; - } - std::this_thread::sleep_for(std::chrono::milliseconds(50)); - } + TaskResult task_result = run_task_with_loading_screen([video_page, &related_videos]() { + related_videos = video_page->get_related_media(video_page->get_url()); + return true; }); XMapWindow(disp, video_player_window); XSync(disp, False); - if(task_result == TaskResult::CANCEL || task_result == TaskResult::FALSE) + if(task_result == TaskResult::CANCEL) cancelled = true; } @@ -2852,20 +2843,12 @@ namespace QuickMedia { std::string new_video_url; if(video_tasks.valid()) { - TaskResult task_result = run_task_with_loading_screen([&video_tasks, &related_videos]() { - while(true) { - if(program_is_dead_in_current_thread()) - return false; - - if(video_tasks.ready()) { - related_videos = video_tasks.get(); - return true; - } - std::this_thread::sleep_for(std::chrono::milliseconds(50)); - } + TaskResult task_result = run_task_with_loading_screen([video_page, &related_videos]() { + related_videos = video_page->get_related_media(video_page->get_url()); + return true; }); - if(task_result == TaskResult::CANCEL || task_result == TaskResult::FALSE) { + if(task_result == TaskResult::CANCEL) { current_page = previous_page; go_to_previous_page = true; break; @@ -2943,14 +2926,22 @@ namespace QuickMedia { load_video_error_check(); } else if(update_err != VideoPlayer::Error::OK) { ++load_try; - if(load_try < num_load_tries_max) { + if(load_try == 1 && num_load_tries_max > 1 && is_youtube) { fprintf(stderr, "Failed to play the media, retrying (try %d out of %d)\n", 1 + load_try, num_load_tries_max); - load_video_error_check(prev_start_time); + std::string prev_video_url = video_url; + std::string prev_audio_url = audio_url; + youtube_custom_redirect(video_url, audio_url); + load_video_error_check(prev_start_time, video_url != prev_video_url || audio_url != prev_audio_url); } else { - show_notification("QuickMedia", "Failed to play the video (error code " + std::to_string((int)update_err) + ")", Urgency::CRITICAL); - current_page = previous_page; - go_to_previous_page = true; - break; + if(load_try < num_load_tries_max) { + fprintf(stderr, "Failed to play the media, retrying (try %d out of %d)\n", 1 + load_try, num_load_tries_max); + load_video_error_check(prev_start_time); + } else { + show_notification("QuickMedia", "Failed to play the video (error code " + std::to_string((int)update_err) + ")", Urgency::CRITICAL); + current_page = previous_page; + go_to_previous_page = true; + break; + } } } -- cgit v1.2.3