1
0
Fork 0

Re add direct support and reenable preview.

This commit is contained in:
namedkitten 2020-04-12 13:37:44 +01:00
parent 5197bfe9f2
commit f1c8b4fd7a
5 changed files with 70 additions and 33 deletions

View file

@ -8,10 +8,10 @@
#include <QQuickWindow>
#include <QSequentialIterable>
#include <clocale>
#include <iostream>
#include <math.h>
#include <stdbool.h>
#include <stdexcept>
#include <iostream>
#include <QX11Info>
#include <QtX11Extras/QX11Info>
@ -21,6 +21,8 @@
auto mpvLogger = initLogger("mpv");
bool usedirect = false;
namespace {
void
@ -39,7 +41,8 @@ on_mpv_redraw(void* ctx)
static void*
get_proc_address_mpv(void* ctx, const char* name)
{
return reinterpret_cast<void*>(reinterpret_cast<QOpenGLContext*>(ctx)->getProcAddress(QByteArray(name)));
return reinterpret_cast<void*>(
reinterpret_cast<QOpenGLContext*>(ctx)->getProcAddress(QByteArray(name)));
}
} // namespace
@ -51,17 +54,26 @@ class MpvRenderer : public QQuickFramebufferObject::Renderer
public:
MpvRenderer(MPVBackend* new_obj)
: obj{ new_obj }
{}
{
if (usedirect) {
int r =
mpv_opengl_cb_init_gl(obj->mpv_gl_cb, NULL, get_proc_address_mpv, QOpenGLContext::currentContext());
if (r < 0) {
std::cout << "No." << std::endl;
throw std::runtime_error("failed to initialize mpv GL context");
}
}
}
virtual ~MpvRenderer() {}
// This function is called when a new FBO is needed.
// This happens on the initial frame.
QOpenGLFramebufferObject* createFramebufferObject(const QSize& size)
{
// init mpv_gl:
if (!obj->mpv_gl) {
if (!obj->mpv_gl && !usedirect) {
mpv_opengl_init_params gl_init_params{ get_proc_address_mpv,
QOpenGLContext::currentContext(),
nullptr };
@ -76,18 +88,21 @@ public:
if (QGuiApplication::platformName().contains("xcb")) {
params[2].type = MPV_RENDER_PARAM_X11_DISPLAY;
params[2].data = QX11Info::display();
} /* else if (QGuiApplication::platformName().contains("wayland")) {
} else if (QGuiApplication::platformName().contains("wayland")) {
params[2].type = MPV_RENDER_PARAM_WL_DISPLAY;
auto *native = QGuiApplication::platformNativeInterface();
params[2].data = native->nativeResourceForWindow("display", NULL);
} */
}
#endif
if (mpv_render_context_create(&obj->mpv_gl, obj->mpv, params) < 0)
if (mpv_render_context_create(&obj->mpv_gl, obj->mpv, params) < 0) {
std::cout << "Failed to use render API, try setting Backend/direct to true in settings." << std::endl;
throw std::runtime_error("failed to initialize mpv GL context");
mpv_render_context_set_update_callback(obj->mpv_gl, on_mpv_redraw, obj);
QMetaObject::invokeMethod(obj, "startPlayer");
}
mpv_render_context_set_update_callback(obj->mpv_gl, on_mpv_redraw, obj);
}
QMetaObject::invokeMethod(obj, "startPlayer");
return QQuickFramebufferObject::Renderer::createFramebufferObject(size);
}
@ -95,8 +110,10 @@ public:
void render()
{
obj->window()->resetOpenGLState();
QOpenGLFramebufferObject* fbo = framebufferObject();
if (usedirect) {
mpv_opengl_cb_draw(obj->mpv_gl_cb, fbo->handle(), fbo->width(), fbo->height());
} else {
mpv_opengl_fbo mpfbo{ .fbo = static_cast<int>(fbo->handle()),
.w = fbo->width(),
.h = fbo->height(),
@ -106,6 +123,8 @@ public:
{ MPV_RENDER_PARAM_FLIP_Y, &flip_y },
{ MPV_RENDER_PARAM_INVALID, nullptr } };
mpv_render_context_render(obj->mpv_gl, params);
}
obj->window()->resetOpenGLState();
}
};
@ -114,16 +133,19 @@ MPVBackend::MPVBackend(QQuickItem* parent)
: QQuickFramebufferObject(parent)
, mpv{ mpv_create() }
, mpv_gl(nullptr)
, mpv_gl_cb(nullptr)
{
if (!mpv)
throw std::runtime_error("could not create mpv context");
QSettings settings;
usedirect = settings.value("Backend/direct", false).toBool();
mpv_set_option_string(mpv, "terminal", "off");
mpv_set_option_string(mpv, "terminal", "on");
mpv_set_option_string(mpv, "msg-level", "all=v");
// Fix?
mpv_set_option_string(mpv, "ytdl", "yes");
mpv_set_option_string(mpv, "vo", "libmpv");
mpv_set_option_string(mpv, "slang", "en");
@ -146,13 +168,23 @@ MPVBackend::MPVBackend(QQuickItem* parent)
mpv_observe_property(mpv, 0, "playlist", MPV_FORMAT_NODE);
mpv_observe_property(mpv, 0, "speed", MPV_FORMAT_DOUBLE);
mpv_request_log_messages(mpv, "trace");
mpv_request_log_messages(mpv, "verbose");
mpv_set_wakeup_callback(mpv, wakeup, this);
if (mpv_initialize(mpv) < 0)
throw std::runtime_error("could not initialize mpv context");
if (usedirect) {
mpv_set_option_string(mpv, "vo", "libmpv");
mpv_gl_cb = (mpv_opengl_cb_context*)mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB);
if (!mpv_gl_cb)
throw std::runtime_error("OpenGL not compiled in");
mpv_opengl_cb_set_update_callback(mpv_gl_cb, on_mpv_redraw, (void*)this);
} else {
mpv_set_option_string(mpv, "vo", "libmpv");
}
connect(this,
&MPVBackend::onUpdate,
this,
@ -176,8 +208,10 @@ MPVBackend::~MPVBackend()
Utils::SetDPMS(true);
command("write-watch-later-config");
if (mpv_gl) {
if (usedirect) {
mpv_render_context_free(mpv_gl);
} else {
mpv_opengl_cb_set_update_callback(mpv_gl_cb, NULL, NULL);
}
mpv_terminate_destroy(mpv);
@ -556,10 +590,12 @@ MPVBackend::handle_mpv_event(mpv_event* event)
(struct mpv_event_log_message*)event->data;
QString logMsg = "[" + QString(msg->prefix) + "] " + QString(msg->text);
QString msgLevel = QString(msg->level);
if (msgLevel.startsWith("d")) {
mpvLogger->debug("{}", logMsg.toStdString());
if (msgLevel.startsWith("d") || msgLevel.startsWith("t")) {
mpvLogger->info("{}", logMsg.toStdString());
} else if (msgLevel.startsWith("v") || msgLevel.startsWith("i")) {
mpvLogger->info("{}", logMsg.toStdString());
} else {
mpvLogger->debug("{}", logMsg.toStdString());
}
}

View file

@ -2,8 +2,8 @@
#define MPVBackend_H
#include <mpv/client.h>
#include <mpv/opengl_cb.h>
#include <mpv/qthelper.hpp>
#include <mpv/render_gl.h>
#include <QObject>
@ -28,6 +28,8 @@ class MPVBackend
mpv_handle* mpv;
mpv_render_context* mpv_gl;
mpv_opengl_cb_context* mpv_gl_cb;
QSettings settings;
bool onTop = false;
bool m_logging = true;

View file

@ -130,7 +130,6 @@ main(int argc, char* argv[])
qmlRegisterType<UtilsClass>("player", 1, 0, "Utils");
qmlRegisterType<MPVBackend>("player", 1, 0, "PlayerBackend");
setlocale(LC_NUMERIC, "C");
launcherLogger->info("Loading player...");

View file

@ -86,8 +86,8 @@ Slider {
shouldSeek = true
}
if (shouldSeek) {
/*progressBarTimePreview.playerCommand(
Enums.Commands.SeekAbsolute, a)*/
progressBarTimePreview.playerCommand(
Enums.Commands.SeekAbsolute, a)
} else {
hoverProgressLabel.text = utils.createTimestamp(a)
}

View file

@ -203,7 +203,7 @@ Window {
z: 1
logging: loggingSettings.logBackend
/*onPlaylistChanged: function (playlist) {
onPlaylistChanged: function (playlist) {
for (var thing in playlist) {
var item = playlist[thing]
if (playlist[thing]["current"]) {
@ -213,7 +213,7 @@ Window {
}
progressBarTimePreview.playerCommand(Enums.Commands.ForcePause)
}
}*/
}
Action {
onTriggered: {
@ -492,7 +492,7 @@ Window {
}
}
/*PlayerBackend {
PlayerBackend {
z: 90
id: progressBarTimePreview
height: parent.height
@ -513,7 +513,7 @@ Window {
"worstvideo[height<=?" + String(
height) + "]/worst")
}
}*/
}
}
}
}