aboutsummaryrefslogtreecommitdiff
path: root/include/VideoPlayer.hpp
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/VideoPlayer.hpp
parentc9c2621accb68634685a14703491cacdd7ed2bb1 (diff)
Switch from libmpv to mpv process with window embed
Diffstat (limited to 'include/VideoPlayer.hpp')
-rw-r--r--include/VideoPlayer.hpp85
1 files changed, 39 insertions, 46 deletions
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;
};
}