aboutsummaryrefslogtreecommitdiff
path: root/include/VideoPlayer.hpp
blob: 9d30dc55c607e13fbfc92e2cab23c5162bf146cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once

#include <SFML/Window/WindowHandle.hpp>
#include <SFML/System/Clock.hpp>
#include <stdio.h>
#include <functional>
#include <thread>

#include <sys/un.h>

namespace QuickMedia {
    using EventCallbackFunc = std::function<void(const char *event_name)>;

    // Currently this video player launches mpv and embeds it into the QuickMedia window
    class VideoPlayer {
    public:
        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;

        // @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:
        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:
        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;
    };
}