| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/mediaplayer/libmpv/libmpvwidget.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | |
| 7 | #include <mpv/client.h> |
| 8 | |
| 9 | #if defined(MEDIAPLAYER_LIBMPV_OPENGL) |
| 10 | #include <QOpenGLContext> |
| 11 | #endif |
| 12 | |
| 13 | static void wakeup(void* ctx) { |
| 14 | // This callback is invoked from any mpv thread (but possibly also |
| 15 | // recursively from a thread that is calling the mpv API). Just notify |
| 16 | // the Qt GUI thread to wake up (so that it can process events with |
| 17 | // mpv_wait_event()), and return as quickly as possible. |
| 18 | LibMpvWidget* backend = (LibMpvWidget*)ctx; |
| 19 | emit backend->launchMpvEvents(); |
| 20 | } |
| 21 | |
| 22 | LibMpvWidget::LibMpvWidget(mpv_handle* mpv_handle, QWidget* parent) |
| 23 | : BASE_WIDGET(parent), m_mpvHandle(mpv_handle) |
| 24 | #if defined(MEDIAPLAYER_LIBMPV_OPENGL) |
| 25 | , |
| 26 | m_mpvGL(nullptr) |
| 27 | #endif |
| 28 | { |
| 29 | #if !defined(MEDIAPLAYER_LIBMPV_OPENGL) |
| 30 | setAttribute(Qt::WidgetAttribute::WA_DontCreateNativeAncestors); |
| 31 | setAttribute(Qt::WidgetAttribute::WA_NativeWindow); |
| 32 | #endif |
| 33 | |
| 34 | setMouseTracking(true); |
| 35 | } |
| 36 | |
| 37 | LibMpvWidget::~LibMpvWidget() { |
| 38 | destroyHandle(); |
| 39 | } |
| 40 | |
| 41 | void LibMpvWidget::bind() { |
| 42 | #if !defined(MEDIAPLAYER_LIBMPV_OPENGL) |
| 43 | auto raw_wid = winId(); |
| 44 | |
| 45 | #if defined(Q_OS_WIN) |
| 46 | // Truncate to 32-bit, as all Windows handles are. This also ensures |
| 47 | // it doesn't go negative. |
| 48 | int64_t wid = static_cast<uint32_t>(raw_wid); |
| 49 | #else |
| 50 | int64_t wid = raw_wid; |
| 51 | #endif |
| 52 | |
| 53 | mpv_set_option(m_mpvHandle, "wid", MPV_FORMAT_INT64, &wid); |
| 54 | #endif |
| 55 | |
| 56 | mpv_set_wakeup_callback(m_mpvHandle, wakeup, this); |
| 57 | } |
| 58 | |
| 59 | void LibMpvWidget::destroyHandle() { |
| 60 | #if defined(MEDIAPLAYER_LIBMPV_OPENGL) |
| 61 | makeCurrent(); |
| 62 | |
| 63 | if (m_mpvGL != nullptr) { |
| 64 | mpv_render_context_free(m_mpvGL); |
| 65 | m_mpvGL = nullptr; |
| 66 | } |
| 67 | |
| 68 | doneCurrent(); |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | #if defined(MEDIAPLAYER_LIBMPV_OPENGL) |
| 73 | static void* get_proc_address(void* ctx, const char* name) { |
| 74 | Q_UNUSED(ctx); |
| 75 | |
| 76 | QOpenGLContext* glctx = QOpenGLContext::currentContext(); |
| 77 | |
| 78 | if (!glctx) { |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | return reinterpret_cast<void*>(glctx->getProcAddress(QByteArray(name))); |
| 83 | } |
| 84 | |
| 85 | void LibMpvWidget::maybeUpdate() { |
| 86 | // If the Qt window is not visible, Qt's update() will just skip rendering. |
| 87 | // This confuses mpv's render API, and may lead to small occasional |
| 88 | // freezes due to video rendering timing out. |
| 89 | // Handle this by manually redrawing. |
| 90 | // Note: Qt doesn't seem to provide a way to query whether update() will |
| 91 | // be skipped, and the following code still fails when e.g. switching |
| 92 | // to a different workspace with a reparenting window manager. |
| 93 | if (window()->isMinimized()) { |
| 94 | makeCurrent(); |
| 95 | paintGL(); |
| 96 | context()->swapBuffers(context()->surface()); |
| 97 | doneCurrent(); |
| 98 | } |
| 99 | else { |
| 100 | update(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void LibMpvWidget::onMpvRedraw(void* ctx) { |
| 105 | QMetaObject::invokeMethod((LibMpvWidget*)ctx, &LibMpvWidget::maybeUpdate, Qt::ConnectionType::QueuedConnection); |
| 106 | } |
| 107 | |
| 108 | void LibMpvWidget::initializeGL() { |
| 109 | mpv_opengl_init_params gl_init_params[1] = {get_proc_address, nullptr}; |
| 110 | mpv_render_param display{MPV_RENDER_PARAM_INVALID, nullptr}; |
| 111 | |
| 112 | #if QT_VERSION_MAJOR == 6 && defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) && !defined(Q_OS_ANDROID) |
| 113 | #if defined(QT_FEATURE_xcb) && QT_CONFIG(xcb) |
| 114 | if (QGuiApplication::platformName() == QStringLiteral("xcb")) { |
| 115 | display.type = MPV_RENDER_PARAM_X11_DISPLAY; |
| 116 | display.data = qApp->nativeInterface<QNativeInterface::QX11Application>()->display(); |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | #if defined(QT_FEATURE_wayland) && QT_CONFIG(wayland) |
| 121 | if (qApp->isWayland()) { |
| 122 | display.type = MPV_RENDER_PARAM_WL_DISPLAY; |
| 123 | display.data = qApp->nativeInterface<QNativeInterface::QWaylandApplication>()->display(); |
| 124 | } |
| 125 | #endif |
| 126 | #endif |
| 127 | |
| 128 | mpv_render_param params[]{{MPV_RENDER_PARAM_API_TYPE, const_cast<char*>(MPV_RENDER_API_TYPE_OPENGL)}, |
| 129 | {MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &gl_init_params}, |
| 130 | display, |
| 131 | {MPV_RENDER_PARAM_INVALID, nullptr}}; |
| 132 | |
| 133 | if (mpv_render_context_create(&m_mpvGL, m_mpvHandle, params) < 0) { |
| 134 | qFatal("failed to initialize mpv GL context"); |
| 135 | } |
| 136 | |
| 137 | mpv_render_context_set_update_callback(m_mpvGL, LibMpvWidget::onMpvRedraw, reinterpret_cast<void*>(this)); |
| 138 | } |
| 139 | |
| 140 | void LibMpvWidget::paintGL() { |
| 141 | qreal scaling_ratio = window()->devicePixelRatio(); |
| 142 | mpv_opengl_fbo mpfbo{static_cast<int>(defaultFramebufferObject()), |
| 143 | int(width() * scaling_ratio), |
| 144 | int(height() * scaling_ratio), |
| 145 | 0}; |
| 146 | int flip_y{1}; |
| 147 | |
| 148 | mpv_render_param params[] = {{MPV_RENDER_PARAM_OPENGL_FBO, &mpfbo}, |
| 149 | {MPV_RENDER_PARAM_FLIP_Y, &flip_y}, |
| 150 | {MPV_RENDER_PARAM_INVALID, nullptr}}; |
| 151 | |
| 152 | mpv_render_context_render(m_mpvGL, params); |
| 153 | } |
| 154 | #endif |