aboutsummaryrefslogtreecommitdiff
path: root/src/Video.cpp
blob: 7b6dceec62d80094a6329589ce42752f0b9d1fcc (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "../include/Video.hpp"
#include <mpv/client.h>
#include <mpv/opengl_cb.h>
#include <clocale>

#include <SFML/Config.hpp>

#if defined(SFML_SYSTEM_WINDOWS)
    #ifdef _MSC_VER
        #include <windows.h>
    #endif
    #include <GL/gl.h>
    #include <GL/glx.h>
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD)
    #if defined(SFML_OPENGL_ES)
        #include <GLES/gl.h>
        #include <GLES/glext.h>
    #else
        #include <GL/gl.h>
    #endif
    #include <GL/glx.h>
    #define glGetProcAddress glXGetProcAddress
#elif defined(SFML_SYSTEM_MACOS)
    #include <OpenGL/gl.h>
#elif defined (SFML_SYSTEM_IOS)
    #include <OpenGLES/ES1/gl.h>
    #include <OpenGLES/ES1/glext.h>
#elif defined (SFML_SYSTEM_ANDROID)
    #include <GLES/gl.h>
    #include <GLES/glext.h>
    // We're not using OpenGL ES 2+ yet, but we can use the sRGB extension
    #include <GLES2/gl2ext.h>
#endif

using namespace std;

namespace dchat
{
    void* getProcAddressMpv(void *funcContext, const char *name)
    {
        return (void*)glGetProcAddress((const GLubyte*)name);
    }
    
    void onMpvRedraw(void *rawVideo)
    {
        Video *video = (Video*)rawVideo;
        ++video->redrawCounter;
    }
    
    Video::Video(unsigned int width, unsigned int height, const char *file, bool loop) : 
        redrawCounter(0),
        context(sf::ContextSettings(), width, height),
        mpv(nullptr),
        mpvGl(nullptr),
        textureBuffer(new sf::Uint8[width * height * 4]) // 4 = red, green, blue and alpha
    {
        context.setActive(true);
        
        if(!texture.create(width, height))
            throw VideoInitializationException("Failed to create texture for video");
        texture.setSmooth(true);
        
        // mpv_create requires LC_NUMERIC to be set to "C" for some reason, see mpv_create documentation
        std::setlocale(LC_NUMERIC, "C");
        mpv = mpv_create();
        if(!mpv)
            throw VideoInitializationException("Failed to create mpv handle");
        
        if(mpv_initialize(mpv) < 0)
            throw VideoInitializationException("Failed to initialize mpv");
        
        mpv_set_option_string(mpv, "vo", "opengl-cb");
        mpv_set_option_string(mpv, "hwdec", "auto");
        if(loop)
            mpv_set_option_string(mpv, "loop", "inf");
        mpvGl = (mpv_opengl_cb_context*)mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB);
        if(!mpvGl)
            throw VideoInitializationException("Failed to initialize mpv opengl render context");
        
        mpv_opengl_cb_set_update_callback(mpvGl, onMpvRedraw, this);
        if(mpv_opengl_cb_init_gl(mpvGl, nullptr, getProcAddressMpv, nullptr) < 0)
            throw VideoInitializationException("Failed to initialize mpv gl callback func");
        
        renderThread = thread([this, width, height]()
        {
            context.setActive(true);
            while(true)
            {
                while(true)
                {
                    mpv_event *mpvEvent = mpv_wait_event(mpv, 0.0);
                    if(mpvEvent->event_id == MPV_EVENT_NONE)
                        break;
                    else if(mpvEvent->event_id == MPV_EVENT_SHUTDOWN)
                        break;
                }
                
                if(redrawCounter > 0)
                {
                    --redrawCounter;
                    context.setActive(true);
                    renderMutex.lock();
                    //mpv_render_context_render(mpvGl, params);
                    mpv_opengl_cb_draw(mpvGl, 0, width, height);
                    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, textureBuffer);
                    texture.update(textureBuffer);
                    sprite.setTexture(texture, true);
                    mpv_opengl_cb_report_flip(mpvGl, 0);
                    renderMutex.unlock();
                }
                this_thread::sleep_for(chrono::milliseconds(10));
            }
        });
        renderThread.detach();
        
        const char *cmd[] = { "loadfile", file, nullptr };
        mpv_command(mpv, cmd);
        context.setActive(false);
    }
    
    Video::~Video()
    {
        lock_guard<mutex> lock(renderMutex);
        context.setActive(true);
        if(mpvGl)
            mpv_opengl_cb_set_update_callback(mpvGl, nullptr, nullptr);
        
        delete[] textureBuffer;
        mpv_opengl_cb_uninit_gl(mpvGl);
        mpv_detach_destroy(mpv);
        if(renderThread.joinable())
            renderThread.join();
    }
    
    void Video::setPosition(float x, float y)
    {
        sprite.setPosition(x, y);
    }
    
    void Video::draw(sf::RenderWindow &window)
    {
        lock_guard<mutex> lock(renderMutex);
        window.draw(sprite);
    }
}