Skip to content

Instantly share code, notes, and snippets.

@uggedal
Created April 11, 2021 11:37
Show Gist options
  • Save uggedal/2611bd0a31c4a14f38e91fc13150a693 to your computer and use it in GitHub Desktop.
Save uggedal/2611bd0a31c4a14f38e91fc13150a693 to your computer and use it in GitHub Desktop.
diff --git a/src/player/PlayerComponent.cpp b/src/player/PlayerComponent.cpp
index c020bb2ef24d..434d80f840ff 100644
--- a/src/player/PlayerComponent.cpp
+++ b/src/player/PlayerComponent.cpp
@@ -261,7 +261,7 @@ void PlayerComponent::setQtQuickWindow(QQuickWindow* window)
///////////////////////////////////////////////////////////////////////////////////////////////////
void PlayerComponent::setWindow(QQuickWindow* window)
{
- QString vo = "libmpv";
+ QString vo = "opengl-cb";
#ifdef TARGET_RPI
window->setFlags(Qt::FramelessWindowHint);
@@ -278,7 +278,7 @@ void PlayerComponent::setWindow(QQuickWindow* window)
mpv::qt::set_property(m_mpv, "vo", vo);
- if (vo == "libmpv")
+ if (vo == "opengl-cb")
setQtQuickWindow(window);
}
diff --git a/src/player/PlayerQuickItem.cpp b/src/player/PlayerQuickItem.cpp
index 0acae27d9c75..6f5a46bbaaf2 100644
--- a/src/player/PlayerQuickItem.cpp
+++ b/src/player/PlayerQuickItem.cpp
@@ -11,8 +11,6 @@
#include <QtQuick/QQuickWindow>
#include <QOpenGLFunctions>
-#include <mpv/render_gl.h>
-
#include "QsLog.h"
#include "utils/Utils.h"
@@ -25,6 +23,12 @@
#ifdef USE_X11EXTRAS
#include <QX11Info>
+static void* MPGetNativeDisplay(const char* name)
+{
+ if (strcmp(name, "x11") == 0)
+ return QX11Info::display();
+ return nullptr;
+}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -37,6 +41,14 @@ static void* get_proc_address(void* ctx, const char* name)
return nullptr;
void *res = (void *)glctx->getProcAddress(QByteArray(name));
+ if (strcmp(name, "glMPGetNativeDisplay") == 0)
+ {
+#ifdef USE_X11EXTRAS
+ return (void *)&MPGetNativeDisplay;
+#else
+ return nullptr;
+#endif
+ }
#ifdef Q_OS_WIN32
// wglGetProcAddress(), which is used by Qt, does not always resolve all
// builtin functions with all drivers (only extensions). Qt compensates this
@@ -81,6 +93,7 @@ private:
PlayerRenderer::PlayerRenderer(mpv::qt::Handle mpv, QQuickWindow* window)
: m_mpv(mpv), m_mpvGL(nullptr), m_window(window), m_size(), m_hAvrtHandle(nullptr), m_videoRectangle(-1, -1, -1, -1), m_fbo(0)
{
+ m_mpvGL = (mpv_opengl_cb_context *)mpv_get_sub_api(m_mpv, MPV_SUB_API_OPENGL_CB);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -91,31 +104,11 @@ bool PlayerRenderer::init()
DwmEnableMMCSS(TRUE);
#endif
-mpv_opengl_init_params opengl_params = {
-#ifdef Q_OS_WIN32
- get_proc_address,
- NULL,
-#else
- .get_proc_address = get_proc_address,
- .get_proc_address_ctx = NULL,
-#endif
-};
-
- mpv_render_param params[] = {
- {MPV_RENDER_PARAM_API_TYPE, (void*)MPV_RENDER_API_TYPE_OPENGL},
- {MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &opengl_params},
-#ifdef USE_X11EXTRAS
- {MPV_RENDER_PARAM_X11_DISPLAY, QX11Info::display()},
-#endif
- {MPV_RENDER_PARAM_INVALID},
- };
- int err = mpv_render_context_create(&m_mpvGL, m_mpv, params);
+ mpv_opengl_cb_set_update_callback(m_mpvGL, on_update, (void *)this);
- if (err >= 0) {
- mpv_render_context_set_update_callback(m_mpvGL, on_update, (void *)this);
- return true;
- }
- return false;
+ // Signals presence of MPGetNativeDisplay().
+ const char *extensions = "GL_MP_MPGetNativeDisplay";
+ return mpv_opengl_cb_init_gl(m_mpvGL, extensions, get_proc_address, nullptr) >= 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -123,8 +116,7 @@ PlayerRenderer::~PlayerRenderer()
{
// Keep in mind that the m_mpv handle must be held until this is done.
if (m_mpvGL)
- mpv_render_context_free(m_mpvGL);
- m_mpvGL = nullptr;
+ mpv_opengl_cb_uninit_gl(m_mpvGL);
delete m_fbo;
}
@@ -166,24 +158,9 @@ void PlayerRenderer::render()
}
}
- mpv_opengl_fbo mpv_fbo = {
-#ifdef Q_OS_WIN32
- fbo,
- fboSize.width(),
- fboSize.height(),
-#else
- .fbo = fbo,
- .w = fboSize.width(),
- .h = fboSize.height(),
-#endif
- };
- int mpv_flip = flip ? -1 : 0;
- mpv_render_param params[] = {
- {MPV_RENDER_PARAM_OPENGL_FBO, &mpv_fbo},
- {MPV_RENDER_PARAM_FLIP_Y, &mpv_flip},
- {MPV_RENDER_PARAM_INVALID}
- };
- mpv_render_context_render(m_mpvGL, params);
+ // The negative height signals to mpv that the video should be flipped
+ // (according to the flipped OpenGL coordinate system).
+ mpv_opengl_cb_draw(m_mpvGL, fbo, fboSize.width(), (flip ? -1 : 1) * fboSize.height());
m_window->resetOpenGLState();
@@ -200,8 +177,7 @@ void PlayerRenderer::render()
///////////////////////////////////////////////////////////////////////////////////////////////////
void PlayerRenderer::swap()
{
- if (m_mpvGL)
- mpv_render_context_report_swap(m_mpvGL);
+ mpv_opengl_cb_report_flip(m_mpvGL, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -248,7 +224,7 @@ PlayerQuickItem::PlayerQuickItem(QQuickItem* parent)
PlayerQuickItem::~PlayerQuickItem()
{
if (m_mpvGL)
- mpv_render_context_set_update_callback(m_mpvGL, nullptr, nullptr);
+ mpv_opengl_cb_set_update_callback(m_mpvGL, nullptr, nullptr);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -322,6 +298,10 @@ void PlayerQuickItem::initMpv(PlayerComponent* player)
{
m_mpv = player->getMpvHandle();
+ m_mpvGL = (mpv_opengl_cb_context *)mpv_get_sub_api(m_mpv, MPV_SUB_API_OPENGL_CB);
+ if (!m_mpvGL)
+ throw FatalException(tr("OpenGL not enabled in libmpv."));
+
connect(player, &PlayerComponent::windowVisible, this, &QQuickItem::setVisible);
window()->update();
}
diff --git a/src/player/PlayerQuickItem.h b/src/player/PlayerQuickItem.h
index 644a62e0f7fd..b706b8927474 100644
--- a/src/player/PlayerQuickItem.h
+++ b/src/player/PlayerQuickItem.h
@@ -6,7 +6,7 @@
#include <QOpenGLFramebufferObject>
#include <mpv/client.h>
-#include <mpv/render.h>
+#include <mpv/opengl_cb.h>
#ifdef Q_OS_WIN32
#include <windows.h>
@@ -34,7 +34,7 @@ public slots:
private:
static void on_update(void *ctx);
mpv::qt::Handle m_mpv;
- mpv_render_context* m_mpvGL;
+ mpv_opengl_cb_context* m_mpvGL;
QQuickWindow* m_window;
QSize m_size;
HANDLE m_hAvrtHandle;
@@ -64,7 +64,7 @@ private slots:
private:
mpv::qt::Handle m_mpv;
- mpv_render_context* m_mpvGL;
+ mpv_opengl_cb_context* m_mpvGL;
PlayerRenderer* m_renderer;
QString m_debugInfo;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment