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/Program.h | 20 ++++++++++++ include/VideoPlayer.hpp | 85 +++++++++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 46 deletions(-) (limited to 'include') diff --git a/include/Program.h b/include/Program.h index ba7e523..69ee564 100644 --- a/include/Program.h +++ b/include/Program.h @@ -1,10 +1,18 @@ #ifndef QUICKMEDIA_PROGRAM_H #define QUICKMEDIA_PROGRAM_H +#include + #ifdef __cplusplus extern "C" { #endif +typedef struct { + pid_t pid; + int read_fd; + int write_fd; +} ProgramPipe; + /* Return 0 if you want to continue reading. @data is null-terminated */ typedef int (*ProgramOutputCallback)(char *data, int size, void *userdata); @@ -14,6 +22,18 @@ typedef int (*ProgramOutputCallback)(char *data, int size, void *userdata); */ int exec_program(const char **args, ProgramOutputCallback output_callback, void *userdata); +/* + @args need to have at least 2 arguments. The first which is the program name + and the last which is NULL, which indicates end of args +*/ +int exec_program_async(const char **args, pid_t *result_process_id); +#if 0 + +int program_pipe_write(ProgramPipe *self, const char *data, size_t size); +int program_pipe_read(ProgramPipe *self, ProgramOutputCallback output_callback, void *userdata); +void program_pipe_close(ProgramPipe *self); +#endif + #ifdef __cplusplus } #endif 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