From 977547ff7f0b609291da56df32e5642d10c530cd Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 14 Jun 2021 04:33:39 +0200 Subject: Properly close the previous video when navigating to another video, for example when doing ctrl+i in a comment and then playing a video --- include/QuickMedia.hpp | 2 ++ src/Body.cpp | 2 +- src/QuickMedia.cpp | 25 ++++++++++++++----------- src/plugins/Youtube.cpp | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/include/QuickMedia.hpp b/include/QuickMedia.hpp index 9e24f3c..8be6fa5 100644 --- a/include/QuickMedia.hpp +++ b/include/QuickMedia.hpp @@ -31,6 +31,7 @@ namespace QuickMedia { class MatrixChatPage; class VideoPage; class Tabs; + class VideoPlayer; enum class ImageViewMode { SINGLE, @@ -205,5 +206,6 @@ namespace QuickMedia { XEvent xev; std::filesystem::path file_manager_start_dir; std::string youtube_url; + std::unique_ptr video_player; }; } \ No newline at end of file diff --git a/src/Body.cpp b/src/Body.cpp index b3ec7e1..4ab90f3 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -958,7 +958,7 @@ namespace QuickMedia { item_background.draw(window); } - int index; + int index = -1; if(attach_side == AttachSide::TOP) { if(page_scroll > 0.0) page_scroll = 0.0; diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 47e2530..c81d59d 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -2388,7 +2388,7 @@ namespace QuickMedia { const bool is_matrix = strcmp(plugin_name, "matrix") == 0; idle_active_handler(); - std::unique_ptr video_player; + video_player.reset(); BodyItems related_videos; if(video_page->autoplay_next_item() && play_index + 1 >= 0 && play_index + 1 < (int)next_play_items.size()) @@ -2406,7 +2406,7 @@ namespace QuickMedia { std::function video_event_callback; bool go_to_previous_page = false; - auto load_video_error_check = [this, &video_title, &video_tasks, &video_player, &channel_url, previous_page, &go_to_previous_page, &time_watched_timer, &video_loaded, video_page, &video_event_callback, &on_window_create, &video_player_window, is_youtube, is_matrix, download_if_streaming_fails](bool resume_video) mutable { + auto load_video_error_check = [this, &video_title, &video_tasks, &channel_url, previous_page, &go_to_previous_page, &time_watched_timer, &video_loaded, video_page, &video_event_callback, &on_window_create, &video_player_window, is_youtube, is_matrix, download_if_streaming_fails](bool resume_video) mutable { video_player.reset(); channel_url.clear(); video_loaded = false; @@ -2580,7 +2580,7 @@ namespace QuickMedia { } }; - video_event_callback = [&video_player, &time_watched_timer, &video_loaded](const char *event_name) mutable { + video_event_callback = [this, &time_watched_timer, &video_loaded](const char *event_name) mutable { if(strcmp(event_name, "pause") == 0) { //double time_remaining = 9999.0; //if(video_player->get_time_remaining(&time_remaining) == VideoPlayer::Error::OK && time_remaining <= 1.0) @@ -2610,7 +2610,7 @@ namespace QuickMedia { bool cursor_visible = true; sf::Clock cursor_hide_timer; - auto save_video_url_to_clipboard = [video_page, &video_player]() { + auto save_video_url_to_clipboard = [this, video_page]() { std::string url = video_page->get_url(); if(video_url_supports_timestamp(url)) { // TODO: Remove timestamp (&t= or ?t=) from video_url @@ -2726,7 +2726,7 @@ namespace QuickMedia { } bool page_changed = false; - page_loop(tabs, 1, [this, &video_player, &page_changed](const std::vector &new_tabs) { + page_loop(tabs, 1, [this, &page_changed](const std::vector &new_tabs) { if(!page_changed && new_tabs.size() == 1 && new_tabs[0].page->get_type() == PageTypez::VIDEO) { window.setMouseCursorVisible(true); if(video_player) { @@ -2743,10 +2743,12 @@ namespace QuickMedia { } }); - if(!window.isOpen() || current_page == PageType::EXIT) + if(!window.isOpen() || current_page == PageType::EXIT) { + video_player.reset(); return; + } - if(page_changed) { + if(!video_player) { current_page = PageType::VIDEO_CONTENT; load_video_error_check(true); } else { @@ -2794,7 +2796,7 @@ namespace QuickMedia { if(task_result == TaskResult::CANCEL || task_result == TaskResult::FALSE) { current_page = previous_page; go_to_previous_page = true; - return; + break; } } @@ -2833,7 +2835,7 @@ namespace QuickMedia { if(load_next_page_result == TaskResult::CANCEL) { current_page = previous_page; go_to_previous_page = true; - continue; + break; } } @@ -2853,12 +2855,12 @@ namespace QuickMedia { if(get_playable_url_result == TaskResult::CANCEL) { current_page = previous_page; go_to_previous_page = true; - return; + break; } else if(get_playable_url_result == TaskResult::FALSE) { show_notification("QuickMedia", "Failed to get playable url", Urgency::CRITICAL); current_page = previous_page; go_to_previous_page = true; - return; + break; } load_video_error_check(false); @@ -2901,6 +2903,7 @@ namespace QuickMedia { std::this_thread::sleep_for(std::chrono::milliseconds(50)); } + video_player.reset(); window.setMouseCursorVisible(true); window_set_fullscreen(disp, window.getSystemHandle(), WindowFullscreenState::UNSET); diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index ce7e8fa..49d7b83 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -1867,7 +1867,7 @@ namespace QuickMedia { const YoutubeVideoFormat *best_mp4 = get_highest_resolution_mp4_non_av1(video_formats, max_height); const YoutubeVideoFormat *best_non_mp4 = get_highest_resolution_non_mp4(video_formats, max_height); // We prefer mp4 (h264) because it has the best hardware decoding support - if(best_mp4 && (!best_non_mp4 || best_mp4->height >= best_non_mp4->height)) { + if(best_mp4 && (!best_non_mp4 || (best_mp4->height >= best_non_mp4->height && best_mp4->fps >= best_non_mp4->fps))) { print_chosen_format(*best_mp4); has_embedded_audio = best_mp4->has_embedded_audio; return best_mp4->base.url; -- cgit v1.2.3