From 2030684b16004a4f6c60f499584366ae5ad57bc9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 15 Feb 2022 21:52:40 +0100 Subject: Finish video player --- src/QuickMedia.cpp | 27 +--- src/VideoPlayer.cpp | 234 ++++++++++++------------------ src/plugins/LocalManga.cpp | 48 +++--- src/plugins/Youtube.cpp | 2 +- src/plugins/youtube/YoutubeMediaProxy.cpp | 34 +---- 5 files changed, 118 insertions(+), 227 deletions(-) (limited to 'src') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index aa86bf8..35efd8d 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -2700,18 +2700,11 @@ namespace QuickMedia { } mgl::WindowHandle video_player_window = None; - auto on_window_create = [this, &video_player_window, &video_loaded, &video_page](mgl::WindowHandle _video_player_window) mutable { + auto on_window_create = [this, &video_player_window, &video_page](mgl::WindowHandle _video_player_window) mutable { video_player_window = _video_player_window; XSelectInput(disp, video_player_window, KeyPressMask | PointerMotionMask); XSync(disp, False); - // Hack to detect video playing if the video starts playing before we have connected to the ipc. - // TODO: This is an issue just because of ubuntu shit that uses old mpv that doesn't support ipc over file descriptors. - double time_in_file = 0.0; - video_player->get_time_in_file(&time_in_file); - if(time_in_file > 0.00001) - video_loaded = true; - SubtitleData subtitle_data; video_page->get_subtitles(subtitle_data); if(!subtitle_data.url.empty()) @@ -2736,11 +2729,6 @@ namespace QuickMedia { std::string prev_start_time; std::vector media_chapters; - bool throttled = false; - auto throttle_handler = [&throttled] { - throttled = true; - }; - auto load_video_error_check = [&](std::string start_time = "", bool reuse_media_source = false) mutable { video_player.reset(); channel_url.clear(); @@ -2852,7 +2840,6 @@ namespace QuickMedia { youtube_downloader_task.cancel(); youtube_video_media_proxy.reset(); youtube_audio_media_proxy.reset(); - throttled = false; struct MediaProxyMetadata { std::unique_ptr *media_proxy; @@ -2869,7 +2856,7 @@ namespace QuickMedia { if(media_proxies[i].url->empty() || youtube_url_is_live_stream(*media_proxies[i].url)) continue; - *media_proxies[i].media_proxy = std::make_unique(throttle_handler); + *media_proxies[i].media_proxy = std::make_unique(); if(!(*media_proxies[i].media_proxy)->start(*media_proxies[i].url, media_proxies[i].content_length)) { show_notification("QuickMedia", "Failed to load start youtube media proxy", Urgency::CRITICAL); current_page = previous_page; @@ -2963,6 +2950,8 @@ namespace QuickMedia { // end_of_file = true; } else if(strcmp(event_name, "playback-restart") == 0) { //video_player->set_paused(false); + } else if(strcmp(event_name, "start-file") == 0) { + video_loaded = true; } else if(strcmp(event_name, "file-loaded") == 0) { video_loaded = true; } else if(strcmp(event_name, "video-reconfig") == 0 || strcmp(event_name, "audio-reconfig") == 0) { @@ -3129,14 +3118,6 @@ namespace QuickMedia { cursor_visible = true; } - if(is_youtube && video_player && throttled) { - throttled = false; - fprintf(stderr, "Throttled media download detected, reconnecting...\n"); - double resume_start_time = 0.0; - video_player->get_time_in_file(&resume_start_time); - load_video_error_check(std::to_string((int)resume_start_time)); - } - VideoPlayer::Error update_err = video_player ? video_player->update() : VideoPlayer::Error::OK; if(update_err == VideoPlayer::Error::FAIL_TO_CONNECT_TIMEOUT) { show_notification("QuickMedia", "Failed to connect to mpv ipc after 10 seconds", Urgency::CRITICAL); diff --git a/src/VideoPlayer.cpp b/src/VideoPlayer.cpp index 75871da..76fa82a 100644 --- a/src/VideoPlayer.cpp +++ b/src/VideoPlayer.cpp @@ -45,7 +45,7 @@ static ssize_t write_all(int fd, const void *buffer, size_t size) { namespace QuickMedia { static const double RETRY_TIME_SEC = 0.5; static const int MAX_RETRIES_CONNECT = 1000; - static const double READ_TIMEOUT_SEC = 0.2; + static const double READ_TIMEOUT_SEC = 3.0; static std::string media_chapters_to_ffmetadata_chapters(const std::vector &chapters) { std::string result = ";FFMETADATA1\n\n"; @@ -98,7 +98,6 @@ namespace QuickMedia { keep_open(keep_open), use_youtube_dl(true), video_process_id(-1), - connected_to_ipc(false), connect_tries(0), find_window_tries(0), monitor_height(monitor_height), @@ -107,7 +106,7 @@ namespace QuickMedia { window_handle(0), parent_window(0), display(nullptr), - request_id(1), + request_id_counter(1), expected_request_id(0), request_response_data(Json::nullValue), response_data_status(ResponseDataStatus::NONE), @@ -120,7 +119,6 @@ namespace QuickMedia { abort(); } fprintf(stderr, "Video max height: %d\n", monitor_height); - ipc_server_path[0] = '\0'; } VideoPlayer::~VideoPlayer() { @@ -131,9 +129,6 @@ namespace QuickMedia { if(ipc_socket != -1) close(ipc_socket); - - if(video_process_id != -1 && ipc_server_path[0] != '\0') - remove(ipc_server_path); if(display) XCloseDisplay(display); @@ -155,31 +150,43 @@ namespace QuickMedia { VideoPlayer::Error VideoPlayer::launch_video_process(const char *path, const char *audio_path, mgl::WindowHandle _parent_window, const std::string &title, const std::string &start_time) { parent_window = _parent_window; - if(!tmpnam(ipc_server_path)) { - perror("Failed to generate ipc file name"); - return Error::FAIL_TO_GENERATE_IPC_FILENAME; + int fd[2]; + if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) { + perror("Failed to create socketpair for video player"); + return Error::FAIL_TO_CREATE_SOCKET; } + ipc_socket = fd[0]; - const std::string parent_window_str = std::to_string(parent_window); - std::vector args; - - std::string input_ipc_server_arg = "--input-ipc-server="; - input_ipc_server_arg += ipc_server_path; - std::string wid_arg = "--wid="; - wid_arg += parent_window_str; + int flags = fcntl(ipc_socket, F_GETFL, 0); + if(flags != -1) + fcntl(ipc_socket, F_SETFL, flags | O_NONBLOCK); + const std::string ipc_fd = std::to_string(fd[1]); std::string input_conf = "--input-conf=" + resource_root + "input.conf"; std::string cache_dir = "--cache-dir=" + std::move(get_cache_dir().join("media").data); + std::string wid_arg = "--wid="; + wid_arg += std::to_string(parent_window); + + std::string video_player_filepath = resource_root + "/video_player/sibs-build/linux_x86_64/" +#ifdef NDEBUG + "release/" +#else + "debug/" +#endif + "quickmedia-video-player"; + + if(get_file_type(video_player_filepath.c_str()) != FileType::REGULAR) + video_player_filepath = "/usr/bin/quickmedia-video-player"; + + std::vector args; // TODO: Resume playback if the last video played matches the first video played next time QuickMedia is launched args.insert(args.end(), { - "mpv", - input_ipc_server_arg.c_str(), - "--cursor-autohide=no", /* "--no-input-default-bindings", "--input-vo-keyboard=no", "--no-input-cursor", */ - "--no-terminal", + video_player_filepath.c_str(), + "--cursor-autohide=no", "--save-position-on-quit=no", "--profile=pseudo-gui", // For gui when playing audio, requires a version of mpv that isn't ancient - "--no-resume-playback", + "--resume-playback=no", // TODO: Disable hr seek on low power devices? "--hr-seek=yes", "--force-seekable=yes", @@ -189,14 +196,19 @@ namespace QuickMedia { "--cache-on-disk=yes", "--cache-secs=86400", // 24 hours "--sub-font-size=60", - "--sub-margin-y=45", + "--sub-margin-y=60", "--sub-border-size=1.95", + "--input-default-bindings=yes", + "--input-vo-keyboard=yes", + "--osc=yes", //"--force_all_formats=no", cache_dir.c_str(), input_conf.c_str(), - wid_arg.c_str() + wid_arg.c_str(), + "--ipc-fd", + ipc_fd.c_str() }); - + if(is_running_wayland()) { args.push_back("--gpu-context=x11egl"); fprintf(stderr, "Wayland detected. Launching mpv in x11egl mode\n"); @@ -212,18 +224,18 @@ namespace QuickMedia { ytdl_format = "--ytdl-format=bestvideo[height<=?" + std::to_string(monitor_height) + "]+bestaudio/best"; if(!use_youtube_dl) - args.push_back("--no-ytdl"); + args.push_back("--ytdl=no"); else args.push_back(ytdl_format.c_str()); std::string mpris_arg; Path mpris_path = get_config_dir_xdg().join("mpv").join("scripts").join("mpris.so"); if(get_file_type(mpris_path) == FileType::REGULAR) - mpris_arg = "--script=" + mpris_path.data; + mpris_arg = "--scripts=" + mpris_path.data; if(!use_system_mpv_config) { args.insert(args.end(), { - "--no-config", + "--config=no", "--profile=gpu-hq", "--vo=gpu,vdpau,x11", "--hwdec=auto" @@ -240,7 +252,7 @@ namespace QuickMedia { } if(no_video) - args.push_back("--no-video"); + args.push_back("--video=no"); std::string chapters_file_arg; if(tmp_chapters_filepath[0] != '\0') { @@ -248,10 +260,9 @@ namespace QuickMedia { args.push_back(chapters_file_arg.c_str()); } - std::string audio_file_arg; if(audio_path && audio_path[0] != '\0') { - audio_file_arg = std::string("--audio-file=") + audio_path; - args.push_back(audio_file_arg.c_str()); + args.push_back("--audio-file"); + args.push_back(audio_path); } std::string start_time_arg; @@ -262,26 +273,14 @@ namespace QuickMedia { args.insert(args.end(), { "--", path, nullptr }); - fprintf(stderr, "mpv input ipc server: %s\n", ipc_server_path); - - if((ipc_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { - perror("Failed to create socket for video player"); - return Error::FAIL_TO_CREATE_SOCKET; - } - - ipc_addr.sun_family = AF_UNIX; - strcpy(ipc_addr.sun_path, ipc_server_path); - - int flags = fcntl(ipc_socket, F_GETFL, 0); - if(flags != -1) - fcntl(ipc_socket, F_SETFL, flags | O_NONBLOCK); - if(exec_program_async(args.data(), &video_process_id) != 0) { + close(fd[1]); close(ipc_socket); ipc_socket = -1; return Error::FAIL_TO_LAUNCH_PROCESS; } + close(fd[1]); return Error::OK; } @@ -298,18 +297,9 @@ namespace QuickMedia { if(video_process_id == -1) return launch_video_process(path, audio_path, _parent_window, title, start_time); - // TODO: When these are used, add audio_path, title and start_time. Also handle is_youtube - Json::Value command_data(Json::arrayValue); - command_data.append("loadfile"); - command_data.append(path); - Json::Value command(Json::objectValue); - command["command"] = command_data; - - Json::StreamWriterBuilder builder; - builder["commentStyle"] = "None"; - builder["indentation"] = ""; - const std::string cmd_str = Json::writeString(builder, command) + "\n"; - return send_command(cmd_str.c_str(), cmd_str.size()); + fprintf(stderr, "TODO: Implement VideoPlayer::load_video without restarting the video player\n"); + abort(); + return VideoPlayer::Error::INIT_FAILED; } static std::vector get_child_window(Display *display, Window window) { @@ -333,12 +323,9 @@ namespace QuickMedia { if(wait_program_non_blocking(video_process_id, &exit_status)) { fprintf(stderr, "The video player exited!, status: %d\n", exit_status); close(ipc_socket); - remove(ipc_server_path); - ipc_server_path[0] = '\0'; video_process_id = -1; ipc_socket = -1; window_handle = None; - connected_to_ipc = false; return Error::EXITED; } } @@ -352,20 +339,7 @@ namespace QuickMedia { if(find_window_tries == max_retries_find_window) return Error::FAIL_TO_FIND_WINDOW; - if(!connected_to_ipc && retry_timer.get_elapsed_time_seconds() >= RETRY_TIME_SEC) { - retry_timer.restart(); - if(connect(ipc_socket, (struct sockaddr*)&ipc_addr, sizeof(ipc_addr)) == -1) { - ++connect_tries; - if(connect_tries == MAX_RETRIES_CONNECT) { - fprintf(stderr, "Failed to connect to mpv ipc after %d seconds, last error: %s\n", (int)(RETRY_TIME_SEC * MAX_RETRIES_CONNECT), strerror(errno)); - return Error::FAIL_TO_CONNECT_TIMEOUT; - } - } else { - connected_to_ipc = true; - } - } - - if(connected_to_ipc && window_handle == 0 && retry_timer.get_elapsed_time_seconds() >= RETRY_TIME_SEC) { + if(window_handle == 0 && retry_timer.get_elapsed_time_seconds() >= RETRY_TIME_SEC) { retry_timer.restart(); std::vector child_windows = get_child_window(display, parent_window); size_t num_children = child_windows.size(); @@ -385,7 +359,7 @@ namespace QuickMedia { } } - if(connected_to_ipc && window_handle && event_callback) { + if(window_handle && event_callback) { Error err = read_ipc_func(); if(err != Error::OK) return err; @@ -395,7 +369,6 @@ namespace QuickMedia { } VideoPlayer::Error VideoPlayer::read_ipc_func() { - assert(connected_to_ipc); Json::Value json_root; Json::CharReaderBuilder json_builder; std::unique_ptr json_reader(json_builder.newCharReader()); @@ -424,7 +397,8 @@ namespace QuickMedia { } if(expected_request_id != 0 && request_id_json.isNumeric() && request_id_json.asUInt() == expected_request_id) { - if(json_root["error"].isNull()) + const Json::Value &status_json = json_root["status"]; + if(!status_json.isString() || strcmp(status_json.asCString(), "error") == 0) response_data_status = ResponseDataStatus::ERROR; else response_data_status = ResponseDataStatus::OK; @@ -441,8 +415,11 @@ namespace QuickMedia { } VideoPlayer::Error VideoPlayer::get_time_in_file(double *result) { + Json::Value json_root(Json::objectValue); + json_root["command"] = "time-pos"; + Json::Value time_pos_json; - Error err = get_property("time-pos", &time_pos_json, Json::realValue); + Error err = send_command(json_root, &time_pos_json, Json::ValueType::realValue); if(err != Error::OK) return err; @@ -450,46 +427,51 @@ namespace QuickMedia { return err; } - VideoPlayer::Error VideoPlayer::set_property(const std::string &property_name, const Json::Value &value) { - Json::Value command_data(Json::arrayValue); - command_data.append("set_property"); - command_data.append(property_name); - command_data.append(value); - Json::Value command(Json::objectValue); - command["command"] = std::move(command_data); + VideoPlayer::Error VideoPlayer::add_subtitle(const std::string &url, const std::string &title, const std::string &lang) { + Json::Value data_json(Json::objectValue); + data_json["file"] = url; + if(!title.empty()) { + data_json["title"] = title; + if(!lang.empty()) + data_json["language"] = title; + } - Json::StreamWriterBuilder builder; - builder["commentStyle"] = "None"; - builder["indentation"] = ""; - const std::string cmd_str = Json::writeString(builder, command) + "\n"; - return send_command(cmd_str.c_str(), cmd_str.size()); + Json::Value json_root(Json::objectValue); + json_root["command"] = "sub-add"; + json_root["data"] = std::move(data_json); + + Json::Value result; + return send_command(json_root, &result, Json::ValueType::nullValue); } - VideoPlayer::Error VideoPlayer::get_property(const std::string &property_name, Json::Value *result, Json::ValueType result_type) { - unsigned int cmd_request_id = request_id; - ++request_id; + uint32_t VideoPlayer::get_next_request_id() { + unsigned int cmd_request_id = request_id_counter; + ++request_id_counter; + // Overflow check. 0 is defined as no request, 1 is the first valid one - if(request_id == 0) - request_id = 1; + if(request_id_counter == 0) + request_id_counter = 1; + + return cmd_request_id; + } - Json::Value command_data(Json::arrayValue); - command_data.append("get_property"); - command_data.append(property_name); - Json::Value command(Json::objectValue); - command["command"] = std::move(command_data); - command["request_id"] = cmd_request_id; + VideoPlayer::Error VideoPlayer::send_command(Json::Value &json_root, Json::Value *result, Json::ValueType result_type) { + const uint32_t request_id = get_next_request_id(); + json_root["request_id"] = request_id; Json::StreamWriterBuilder builder; builder["commentStyle"] = "None"; builder["indentation"] = ""; - const std::string cmd_str = Json::writeString(builder, command) + "\n"; + const std::string cmd_str = Json::writeString(builder, json_root) + "\n"; - Error err = send_command(cmd_str.c_str(), cmd_str.size()); - if(err != Error::OK) - return err; + if(write_all(ipc_socket, cmd_str.data(), cmd_str.size()) == -1) { + fprintf(stderr, "Failed to send to ipc socket, error: %s, command: %.*s\n", strerror(errno), (int)cmd_str.size(), cmd_str.c_str()); + return Error::FAIL_TO_SEND; + } + VideoPlayer::Error err; mgl::Clock read_timer; - expected_request_id = cmd_request_id; + expected_request_id = request_id; do { err = read_ipc_func(); if(err != Error::OK) @@ -505,6 +487,12 @@ namespace QuickMedia { else err = Error::READ_INCORRECT_TYPE; } else if(response_data_status == ResponseDataStatus::ERROR) { + const char *err_msg = "Unknown"; + const Json::Value &message_json = request_response_data["message"]; + if(message_json.isString()) + err_msg = message_json.asCString(); + + fprintf(stderr, "VideoPlayer::send_command failed, error from video player: %s\n", err_msg); err = Error::READ_RESPONSE_ERROR; goto cleanup; } else { @@ -518,36 +506,4 @@ namespace QuickMedia { request_response_data = Json::Value(Json::nullValue); return err; } - - VideoPlayer::Error VideoPlayer::add_subtitle(const std::string &url, const std::string &title, const std::string &lang) { - Json::Value command_data(Json::arrayValue); - command_data.append("sub-add"); - command_data.append(url); - command_data.append("auto"); - if(!title.empty()) { - command_data.append(title); - if(!lang.empty()) - command_data.append(lang); - } - Json::Value command(Json::objectValue); - command["command"] = std::move(command_data); - - Json::StreamWriterBuilder builder; - builder["commentStyle"] = "None"; - builder["indentation"] = ""; - const std::string cmd_str = Json::writeString(builder, command) + "\n"; - return send_command(cmd_str.c_str(), cmd_str.size()); - } - - VideoPlayer::Error VideoPlayer::send_command(const char *cmd, size_t size) { - if(!connected_to_ipc) - return Error::FAIL_NOT_CONNECTED; - - if(write_all(ipc_socket, cmd, size) == -1) { - fprintf(stderr, "Failed to send to ipc socket, error: %s, command: %.*s\n", strerror(errno), (int)size, cmd); - return Error::FAIL_TO_SEND; - } - - return Error::OK; - } } diff --git a/src/plugins/LocalManga.cpp b/src/plugins/LocalManga.cpp index 83dd983..8ab8cd1 100644 --- a/src/plugins/LocalManga.cpp +++ b/src/plugins/LocalManga.cpp @@ -163,13 +163,7 @@ namespace QuickMedia { return manga_list; } - enum class ReadStatus { - READ, - UNREAD - }; - - // Returns the new read status - static bool toggle_read_save_to_file(Program *program, const std::string &manga_name, const std::string &thumbnail_url, ReadStatus &read_status) { + static bool validate_local_manga_dir_config_is_set() { if(get_config().local_manga_directory.empty()) { show_notification("QuickMedia", "local_manga_directory config is not set", Urgency::CRITICAL); return false; @@ -180,6 +174,19 @@ namespace QuickMedia { return false; } + return true; + } + + enum class ReadStatus { + READ, + UNREAD + }; + + // Returns the new read status + static bool toggle_read_save_to_file(Program *program, const std::string &manga_name, const std::string &thumbnail_url, ReadStatus &read_status) { + if(!validate_local_manga_dir_config_is_set()) + return false; + Path manga_url = Path(get_config().local_manga_directory).join(manga_name); std::vector chapters = get_chapters_in_manga(manga_name, manga_url, true, true); if(chapters.empty() || chapters.front().pages.empty()) @@ -327,15 +334,8 @@ namespace QuickMedia { } PluginResult LocalMangaSearchPage::submit(const SubmitArgs &args, std::vector &result_tabs) { - if(get_config().local_manga_directory.empty()) { - show_notification("QuickMedia", "local_manga_directory config is not set", Urgency::CRITICAL); + if(!validate_local_manga_dir_config_is_set()) return PluginResult::OK; - } - - if(get_file_type(get_config().local_manga_directory) != FileType::DIRECTORY) { - show_notification("QuickMedia", "local_manga_directory config is not set to a valid directory", Urgency::CRITICAL); - return PluginResult::OK; - } Path manga_url = Path(get_config().local_manga_directory).join(args.url); std::vector chapters = get_chapters_in_manga(args.url, manga_url, false, false); @@ -371,15 +371,8 @@ namespace QuickMedia { manga_list.clear(); finished_reading_manga.clear(); - if(get_config().local_manga_directory.empty()) { - show_notification("QuickMedia", "local_manga_directory config is not set", Urgency::CRITICAL); + if(!validate_local_manga_dir_config_is_set()) return PluginResult::OK; - } - - if(get_file_type(get_config().local_manga_directory) != FileType::DIRECTORY) { - show_notification("QuickMedia", "local_manga_directory config is not set to a valid directory", Urgency::CRITICAL); - return PluginResult::OK; - } manga_list = get_manga_in_directory(get_config().local_manga_directory, true); @@ -467,15 +460,8 @@ namespace QuickMedia { } PluginResult LocalMangaChaptersPage::submit(const SubmitArgs &args, std::vector &result_tabs) { - if(get_config().local_manga_directory.empty()) { - show_notification("QuickMedia", "local_manga_directory config is not set", Urgency::CRITICAL); - return PluginResult::OK; - } - - if(get_file_type(get_config().local_manga_directory) != FileType::DIRECTORY) { - show_notification("QuickMedia", "local_manga_directory config is not set to a valid directory", Urgency::CRITICAL); + if(!validate_local_manga_dir_config_is_set()) return PluginResult::OK; - } result_tabs.push_back(Tab{nullptr, std::make_unique(program, content_title, args.url, args.url, thumbnail_url), nullptr}); return PluginResult::OK; diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index d6035f2..a4212ae 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -2606,7 +2606,7 @@ R"END( std::string response; DownloadResult download_result = download_to_string(playback_url + "&ver=2&cpn=" + cpn + "&gl=US&hl=en", response, std::move(additional_args), true); if(download_result != DownloadResult::OK) { - fprintf(stderr, "Failed to mark video as watched because http request failed\n"); + fprintf(stderr, "Failed to mark video as watched because the http request failed\n"); return; } } diff --git a/src/plugins/youtube/YoutubeMediaProxy.cpp b/src/plugins/youtube/YoutubeMediaProxy.cpp index b33c122..9efd7ac 100644 --- a/src/plugins/youtube/YoutubeMediaProxy.cpp +++ b/src/plugins/youtube/YoutubeMediaProxy.cpp @@ -47,8 +47,6 @@ static ssize_t write_all(int fd, const void *buffer, size_t size) { namespace QuickMedia { static const int MAX_BUFFER_SIZE = 65536; static const int64_t RANGE = 5242870; - 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 = 3; static const char download_error_response_msg[] = "HTTP/1.1 500 Internal Server Error\r\n" "Content-Length: 0\r\n\r\n"; @@ -101,7 +99,7 @@ namespace QuickMedia { return true; } - YoutubeStaticMediaProxy::YoutubeStaticMediaProxy(ThrottleHandler throttle_handler) : throttle_handler(std::move(throttle_handler)) { + YoutubeStaticMediaProxy::YoutubeStaticMediaProxy() { } @@ -226,7 +224,6 @@ namespace QuickMedia { client_request_buffer.clear(); client_request_finished = false; download_started = false; - throttle_started = false; if(client_fd != -1) { close(client_fd); @@ -283,7 +280,6 @@ namespace QuickMedia { client_request_buffer.erase(header_end + 4); client_request_finished = true; download_started = false; - throttle_started = false; download_read_buffer_offset = 0; const int64_t new_start_range = header_extract_start_range(client_request_buffer); @@ -489,40 +485,12 @@ namespace QuickMedia { if(!download_started) { total_downloaded_bytes = 0; download_started = true; - throttle_started = false; download_start_time = get_boottime_milliseconds(); } total_downloaded_bytes += downloader_num_read_bytes; } } -#if 0 - if(download_started) { - const int64_t time_elapsed_sec = (get_boottime_milliseconds() - download_start_time) / 1000; - int64_t download_speed_kb_sec = 0; - if(time_elapsed > 0) - download_speed_kb_sec = (total_downloaded_bytes / time_elapsed) / 1024; - - if(download_speed_kb_sec < THROTTLED_DOWNLOAD_LIMIT_KB) { - if(throttle_started) { - if(tp.tv_sec - throttle_start_time >= THROTTLED_DURATION_SECS && !throttle_callback_called) { - total_downloaded_bytes = 0; - download_started = false; - throttle_started = false; - throttle_callback_called = true; - if(throttle_handler) - throttle_handler(); - } - } else { - throttle_started = true; - throttle_start_time = tp.tv_sec; - } - } else { - throttle_started = false; - } - } -#endif - // TODO: Remove this code and instead create the header ourselves and send it to the client. Then pipe the curl output directly to the client input. if(!download_header_finished) { download_header.append(download_read_buffer, downloader_num_read_bytes); -- cgit v1.2.3