aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-08-08 22:12:09 +0200
committerdec05eba <dec05eba@protonmail.com>2019-08-08 22:12:12 +0200
commitf26534ca8d7107b14fdd5a02cbadd56505d159de (patch)
treee8f91f264126665a175709046bd92c9b0973323a /include
parentc9c2621accb68634685a14703491cacdd7ed2bb1 (diff)
Switch from libmpv to mpv process with window embed
Diffstat (limited to 'include')
-rw-r--r--include/Program.h20
-rw-r--r--include/VideoPlayer.hpp85
2 files changed, 59 insertions, 46 deletions
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 <sys/types.h>
+
#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 <SFML/Graphics/RenderWindow.hpp>
-#include <SFML/Window/Event.hpp>
-#include <SFML/Graphics/Texture.hpp>
-#include <SFML/Graphics/Sprite.hpp>
-#include <SFML/Graphics/RectangleShape.hpp>
-#include <SFML/Window/Context.hpp>
-#include <thread>
-#include <mutex>
-#include <atomic>
-#include <stdexcept>
+#include <SFML/Window/WindowHandle.hpp>
+#include <SFML/System/Clock.hpp>
+#include <stdio.h>
#include <functional>
+#include <thread>
-class mpv_handle;
-class mpv_render_context;
+#include <sys/un.h>
namespace QuickMedia {
- class VideoInitializationException : public std::runtime_error {
- public:
- VideoInitializationException(const std::string &errMsg) : std::runtime_error(errMsg) {}
- };
+ using EventCallbackFunc = std::function<void(const char *event_name)>;
- using PlaybackEndedCallback = std::function<void()>;
-
+ // 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<sf::Context> 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;
};
}