#pragma once #include "MediaChapter.hpp" #include #include #include #include #include #include namespace QuickMedia { using EventCallbackFunc = std::function; using VideoPlayerWindowCreateCallback = std::function; // Currently this video player launches mpv and embeds it into the QuickMedia window class VideoPlayer { public: enum class Error { OK, FAIL_TO_GENERATE_IPC_FILENAME, FAIL_TO_LAUNCH_PROCESS, FAIL_TO_CREATE_SOCKET, FAIL_TO_CONNECT_TIMEOUT, FAIL_NOT_CONNECTED, FAIL_TO_SEND, FAIL_TO_FIND_WINDOW, FAIL_TO_FIND_WINDOW_TIMEOUT, UNEXPECTED_WINDOW_ERROR, FAIL_TO_READ, READ_TIMEOUT, READ_RESPONSE_ERROR, READ_INCORRECT_TYPE, INIT_FAILED, EXITED }; // @event_callback is called from another thread VideoPlayer(bool no_video, bool use_system_mpv_config, bool keep_open, EventCallbackFunc event_callback, VideoPlayerWindowCreateCallback window_create_callback, const std::string &resource_root, int monitor_height, std::string plugin_name); ~VideoPlayer(); VideoPlayer(const VideoPlayer&) = delete; VideoPlayer& operator=(const VideoPlayer&) = delete; // |audio_path| is only set when video and audio are separate files/urls. Error load_video(const char *path, const char *audio_path, mgl::WindowHandle parent_window, bool use_youtube_dl, const std::string &title, const std::string &start_time = "", const std::vector &chapters = {}); // Should be called every update frame Error update(); // Returns time in seconds Error get_time_in_file(double *result); Error set_property(const std::string &property_name, const Json::Value &value); Error get_property(const std::string &property_name, Json::Value *result, Json::ValueType result_type); Error add_subtitle(const std::string &url, const std::string &title, const std::string &lang); int exit_status; private: Error send_command(const char *cmd, size_t size); Error launch_video_process(const char *path, const char *audio_path, mgl::WindowHandle parent_window, const std::string &title, const std::string &start_time); VideoPlayer::Error read_ipc_func(); private: std::string plugin_name; bool no_video; bool use_system_mpv_config; bool keep_open; bool use_youtube_dl; pid_t video_process_id; bool connected_to_ipc; mgl::Clock retry_timer; int connect_tries; int find_window_tries; int monitor_height; int ipc_socket = -1; struct sockaddr_un ipc_addr; char ipc_server_path[L_tmpnam]; EventCallbackFunc event_callback; VideoPlayerWindowCreateCallback window_create_callback; mgl::WindowHandle window_handle; mgl::WindowHandle parent_window; Display *display; unsigned int request_id; unsigned int expected_request_id; Json::Value request_response_data; enum ResponseDataStatus { NONE, OK, ERROR }; ResponseDataStatus response_data_status; std::string resource_root; char tmp_chapters_filepath[27]; }; }