From f26534ca8d7107b14fdd5a02cbadd56505d159de Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 8 Aug 2019 22:12:09 +0200 Subject: Switch from libmpv to mpv process with window embed --- include/VideoPlayer.hpp | 85 +++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 46 deletions(-) (limited to 'include/VideoPlayer.hpp') diff --git a/include/VideoPlayer.hpp b/include/VideoPlayer.hpp index a9b177c..9d30dc5 100644 --- a/include/VideoPlayer.hpp +++ b/include/VideoPlayer.hpp @@ -1,63 +1,56 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include #include +#include -class mpv_handle; -class mpv_render_context; +#include namespace QuickMedia { - class VideoInitializationException : public std::runtime_error { - public: - VideoInitializationException(const std::string &errMsg) : std::runtime_error(errMsg) {} - }; + using EventCallbackFunc = std::function; - using PlaybackEndedCallback = std::function; - + // Currently this video player launches mpv and embeds it into the QuickMedia window class VideoPlayer { public: - // Throws VideoInitializationException on error - VideoPlayer(sf::RenderWindow *window, unsigned int width, unsigned int height, const char *file, bool loop = false); - ~VideoPlayer(); + enum class Error { + OK, + FAIL_TO_LAUNCH_PROCESS, + FAIL_TO_CREATE_SOCKET, + FAIL_TO_GENERATE_IPC_FILENAME, + FAIL_TO_CONNECT_TIMEOUT, + FAIL_NOT_CONNECTED, + FAIL_TO_SEND, + INIT_FAILED + }; + // @event_callback is called from another thread + VideoPlayer(EventCallbackFunc event_callback); + ~VideoPlayer(); VideoPlayer(const VideoPlayer&) = delete; VideoPlayer& operator=(const VideoPlayer&) = delete; - - void handle_event(sf::Event &event); - void setPosition(float x, float y); - void resize(const sf::Vector2f &size); - void draw(sf::RenderWindow &window); - // @path can also be an url if youtube-dl is installed - void load_file(const std::string &path); - - // This is updated when mpv wants to render - std::atomic_bool redraw; - std::atomic_bool event_update; - // Important: Do not destroy the video player in this callback - PlaybackEndedCallback onPlaybackEndedCallback; + // @path can also be an url if youtube-dl is installed and accessible to mpv + Error load_video(const char *path, sf::WindowHandle parent_window); + // Should be called every update frame + Error update(); + + Error toggle_pause(); private: - void handle_mpv_events(); + Error send_command(const char *cmd, size_t size); + Error launch_video_process(const char *path, sf::WindowHandle parent_window); + void read_ipc_func(); private: - mpv_handle *mpv; - mpv_render_context *mpvGl; - std::unique_ptr context; - sf::Sprite sprite; - sf::Texture texture; - sf::Uint8 *textureBuffer; - sf::Vector2i video_size; - sf::Vector2f desired_size; - sf::RectangleShape seekbar; - sf::RectangleShape seekbar_background; - sf::Clock cursor_last_active_timer; + pid_t video_process_id; + int ipc_socket; + bool connected_to_ipc; + sf::Clock ipc_connect_retry_timer; + int connect_tries; + struct sockaddr_un ipc_addr; + char ipc_server_path[L_tmpnam]; + EventCallbackFunc event_callback; + std::thread event_read_thread; + bool alive; }; } -- cgit v1.2.3