From 2e156d80e4e3d379849bb1e127e4c69a8f34cea4 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 10 Jul 2020 04:55:11 +0200 Subject: Fallback to invidio.us only if mpv exits (faster fallback) --- src/QuickMedia.cpp | 61 +++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 2ccb9d6..df5a1e8 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -692,54 +692,34 @@ namespace QuickMedia { return false; } - static bool youtube_dl_can_download_video(const std::string &video_url) { - const char *args[] = { "youtube-dl", "--skip-download", "--", video_url.c_str(), nullptr }; - return exec_program(args, nullptr, nullptr) == 0; - } - void Program::video_content_page() { search_bar->onTextUpdateCallback = nullptr; search_bar->onTextSubmitCallback = nullptr; Page previous_page = pop_page_stack(); - bool seekable = false; - std::unique_ptr video_player; sf::WindowHandle video_player_window = None; - auto on_window_create = [this, &video_player, &seekable, &video_player_window](sf::WindowHandle _video_player_window) mutable { + auto on_window_create = [this, &video_player_window](sf::WindowHandle _video_player_window) mutable { video_player_window = _video_player_window; XSelectInput(disp, video_player_window, KeyPressMask); XSync(disp, False); - video_player->is_seekable(&seekable); }; auto load_video_error_check = [this, &video_player, previous_page]() mutable { watched_videos.insert(content_url); - std::string video_url = content_url; - - std::string youtube_video_id; - if(youtube_url_extract_id(video_url, youtube_video_id)) { - // This can fail for example if the video requires age vertification... - // TODO: Remove this when youtube-dl is modified to work with videos that require age verification. - if(!youtube_dl_can_download_video(video_url)) { - // Im sorry invidio.us server... this will be removed later and it only happens when youtube-dl fails! - video_url = "https://www.invidio.us/latest_version?id=" + youtube_video_id + "&itag=22"; - } - } - - VideoPlayer::Error err = video_player->load_video(video_url.c_str(), window.getSystemHandle()); + VideoPlayer::Error err = video_player->load_video(content_url.c_str(), window.getSystemHandle()); if(err != VideoPlayer::Error::OK) { std::string err_msg = "Failed to play url: "; - err_msg += video_url; + err_msg += content_url; show_notification("Video player", err_msg.c_str(), Urgency::CRITICAL); current_page = previous_page; } }; bool has_video_started = true; - video_player = std::make_unique(current_plugin->use_tor, use_system_mpv_config, [this, &video_player, &seekable, &load_video_error_check, previous_page, &has_video_started](const char *event_name) mutable { + auto video_event_callback = [this, &video_player, &load_video_error_check, previous_page, &has_video_started](const char *event_name) mutable { bool end_of_file = false; if(strcmp(event_name, "pause") == 0) { double time_remaining = 9999.0; @@ -747,7 +727,6 @@ namespace QuickMedia { end_of_file = true; } else if(strcmp(event_name, "playback-restart") == 0) { video_player->set_paused(false); - video_player->is_seekable(&seekable); } else if(strcmp(event_name, "start-file") == 0) { has_video_started = true; } @@ -774,9 +753,10 @@ namespace QuickMedia { content_url = std::move(new_video_url); load_video_error_check(); } - }, on_window_create); - load_video_error_check(); + }; + video_player = std::make_unique(current_plugin->use_tor, use_system_mpv_config, video_event_callback, on_window_create); + load_video_error_check(); sf::Event event; @@ -806,8 +786,33 @@ namespace QuickMedia { show_notification("Video player", "Failed to connect to mpv ipc after 5 seconds", Urgency::CRITICAL); current_page = previous_page; return; + } else if(update_err == VideoPlayer::Error::EXITED) { + std::string youtube_video_id; + if(youtube_url_extract_id(content_url, youtube_video_id)) { + fprintf(stderr, "Failed to play youtube video (maybe because of age verification?), trying with invidio.us...\n"); + // This can fail for example if the video requires age vertification... + // Im sorry invidio.us server... this will be removed later and it only happens when youtube-dl fails! + // TODO: Remove this when youtube-dl is modified to work with videos that require age verification. + video_player_window = None; + has_video_started = true; + video_player.reset(); + video_player = std::make_unique(current_plugin->use_tor, use_system_mpv_config, video_event_callback, on_window_create); + + std::string new_video_url = "https://www.invidio.us/latest_version?id=" + youtube_video_id + "&itag=22"; + VideoPlayer::Error err = video_player->load_video(new_video_url.c_str(), window.getSystemHandle()); + if(err != VideoPlayer::Error::OK) { + std::string err_msg = "Failed to play url: "; + err_msg += content_url; + show_notification("Video player", err_msg.c_str(), Urgency::CRITICAL); + current_page = previous_page; + } + } else { + show_notification("Video player", "The video player failed to play the video", Urgency::CRITICAL); + current_page = previous_page; + return; + } } else if(update_err != VideoPlayer::Error::OK) { - show_notification("Video player", "Unexpected error while updating", Urgency::CRITICAL); + show_notification("Video player", "The video player failed to play the video", Urgency::CRITICAL); current_page = previous_page; return; } -- cgit v1.2.3