aboutsummaryrefslogtreecommitdiff
path: root/include/Video.hpp
blob: 9d6663ec1ef5bb07a927d20b029476df79f8cf8d (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
#pragma once

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Window/Context.hpp>
#include <thread>
#include <mutex>
#include <atomic>
#include <stdexcept>

class mpv_handle;
class mpv_opengl_cb_context;

namespace dchat
{
    class VideoInitializationException : public std::runtime_error
    {
    public:
        VideoInitializationException(const std::string &errMsg) : std::runtime_error(errMsg) {}
    };
    
    class Video
    {
    public:
        // Throws VideoInitializationException on error
        Video(unsigned int width, unsigned int height, const char *file, bool loop = false);
        ~Video();
        
        void setPosition(float x, float y);
        void draw(sf::RenderWindow &window);
        
        // This counter is incremented when mpv wants to redraw content
        std::atomic_int redrawCounter;
    private:
        sf::Context context;
        mpv_handle *mpv;
        mpv_opengl_cb_context *mpvGl;
        std::thread renderThread;
        std::mutex renderMutex;
        sf::Sprite sprite;
        sf::Texture texture;
        sf::Uint8 *textureBuffer;
        bool alive;
    };
}