aboutsummaryrefslogtreecommitdiff
path: root/include/VideoPlayer.hpp
blob: c5d9cfe5143f55d1558cadaa58f926840f9958fa (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once

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

#include <sys/un.h>
#include <X11/Xlib.h>

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

    // 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,
            FAIL_TO_FIND_WINDOW,
            FAIL_TO_FIND_WINDOW_TIMEOUT,
            UNEXPECTED_WINDOW_ERROR,
            FAIL_TO_READ,
            READ_TIMEOUT,
            READ_RESPONSE_ERROR,
            READ_INCORRECT_TYPE,
            INIT_FAILED,
            EXITED
        };

        // @event_callback is called from another thread
        VideoPlayer(bool use_tor, bool use_system_mpv_config, EventCallbackFunc event_callback, VideoPlayerWindowCreateCallback window_create_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();

        // Progress is in range [0..1]
        Error get_progress(double *result);
        Error get_time_remaining(double *result);
        Error set_paused(bool paused);

        // Progress is in range [0..1]
        Error set_progress(double progress);

        Error is_seekable(bool *result);

        bool is_connected() const { return connected_to_ipc; }
    private:
        Error set_property(const std::string &property_name, const Json::Value &value);
        Error get_property(const std::string &property_name, Json::Value *result, Json::ValueType result_type);
        Error send_command(const char *cmd, size_t size);
        Error launch_video_process(const char *path, sf::WindowHandle parent_window);
        VideoPlayer::Error read_ipc_func();
    private:
        bool use_tor;
        bool use_system_mpv_config;
        pid_t video_process_id;
        int ipc_socket;
        bool connected_to_ipc;
        sf::Clock retry_timer;
        int connect_tries;
        int find_window_tries;
        struct sockaddr_un ipc_addr;
        char ipc_server_path[L_tmpnam];
        EventCallbackFunc event_callback;
        VideoPlayerWindowCreateCallback window_create_callback;
        sf::WindowHandle window_handle;
        sf::WindowHandle parent_window;
        Display *display;
        unsigned int request_id;
        unsigned int expected_request_id;
        Json::Value request_response_data;

        enum ResponseDataStatus {
            NONE,
            OK,
            ERROR
        };
        ResponseDataStatus response_data_status;
    };
}