From 803e579ff584a88bd3525a123fdbeace76fb0ee4 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 9 Jan 2015 15:48:35 +0100 Subject: DOCS/client_api_examples: qml: reduce number of files This is annoying. --- DOCS/client_api_examples/qml/main.cpp | 115 +++++++++++++++++++++++++- DOCS/client_api_examples/qml/main.h | 36 +++++++++ DOCS/client_api_examples/qml/mpvrenderer.cpp | 116 --------------------------- DOCS/client_api_examples/qml/mpvrenderer.h | 36 --------- DOCS/client_api_examples/qml/mpvtest.pro | 4 +- 5 files changed, 152 insertions(+), 155 deletions(-) create mode 100644 DOCS/client_api_examples/qml/main.h delete mode 100644 DOCS/client_api_examples/qml/mpvrenderer.cpp delete mode 100644 DOCS/client_api_examples/qml/mpvrenderer.h (limited to 'DOCS') diff --git a/DOCS/client_api_examples/qml/main.cpp b/DOCS/client_api_examples/qml/main.cpp index cc86302be6..2794d87c1a 100644 --- a/DOCS/client_api_examples/qml/main.cpp +++ b/DOCS/client_api_examples/qml/main.cpp @@ -1,8 +1,121 @@ +#include "main.h" + +#include + +#include +#include +#include #include +#include + +#include #include -#include "mpvrenderer.h" +class MpvRenderer : public QQuickFramebufferObject::Renderer +{ + static void *get_proc_address(void *ctx, const char *name) { + (void)ctx; + QOpenGLContext *glctx = QOpenGLContext::currentContext(); + if (!glctx) + return NULL; + return (void *)glctx->getProcAddress(QByteArray(name)); + } + + mpv::qt::Handle mpv; + QQuickWindow *window; + mpv_opengl_cb_context *mpv_gl; +public: + MpvRenderer(const MpvObject *obj) + : mpv(obj->mpv), window(obj->window()), mpv_gl(obj->mpv_gl) + { + int r = mpv_opengl_cb_init_gl(mpv_gl, NULL, get_proc_address, NULL); + if (r < 0) + throw std::runtime_error("could not initialize OpenGL"); + } + + virtual ~MpvRenderer() + { + // Until this call is done, we need to make sure the player remains + // alive. This is done implicitly with the mpv::qt::Handle instance + // in this class. + mpv_opengl_cb_uninit_gl(mpv_gl); + } + + void render() + { + QOpenGLFramebufferObject *fbo = framebufferObject(); + int vp[4] = {0, 0, fbo->width(), fbo->height()}; + window->resetOpenGLState(); + mpv_opengl_cb_render(mpv_gl, fbo->handle(), vp); + window->resetOpenGLState(); + } +}; + +MpvObject::MpvObject(QQuickItem * parent) + : QQuickFramebufferObject(parent), mpv_gl(0) +{ + mpv = mpv::qt::Handle::FromRawHandle(mpv_create()); + if (!mpv) + throw std::runtime_error("could not create mpv context"); + + mpv_set_option_string(mpv, "terminal", "yes"); + mpv_set_option_string(mpv, "msg-level", "all=v"); + + if (mpv_initialize(mpv) < 0) + throw std::runtime_error("could not initialize mpv context"); + + // Make use of the MPV_SUB_API_OPENGL_CB API. + mpv::qt::set_option_variant(mpv, "vo", "opengl-cb"); + + // Request hw decoding, just for testing. + mpv::qt::set_option_variant(mpv, "hwdec", "auto"); + + // Setup the callback that will make QtQuick update and redraw if there + // is a new video frame. Use a queued connection: this makes sure the + // doUpdate() function is run on the GUI thread. + mpv_gl = (mpv_opengl_cb_context *)mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB); + if (!mpv_gl) + throw std::runtime_error("OpenGL not compiled in"); + mpv_opengl_cb_set_update_callback(mpv_gl, MpvObject::on_update, (void *)this); + connect(this, &MpvObject::onUpdate, this, &MpvObject::doUpdate, + Qt::QueuedConnection); +} + +MpvObject::~MpvObject() +{ + if (mpv_gl) + mpv_opengl_cb_set_update_callback(mpv_gl, NULL, NULL); +} + +void MpvObject::on_update(void *ctx) +{ + MpvObject *self = (MpvObject *)ctx; + emit self->onUpdate(); +} + +// connected to onUpdate(); signal makes sure it runs on the GUI thread +void MpvObject::doUpdate() +{ + update(); +} + +void MpvObject::command(const QVariant& params) +{ + mpv::qt::command_variant(mpv, params); +} + +void MpvObject::setProperty(const QString& name, const QVariant& value) +{ + mpv::qt::set_property_variant(mpv, name, value); +} + +QQuickFramebufferObject::Renderer *MpvObject::createRenderer() const +{ + window()->setPersistentOpenGLContext(true); + window()->setPersistentSceneGraph(true); + return new MpvRenderer(this); +} int main(int argc, char **argv) { diff --git a/DOCS/client_api_examples/qml/main.h b/DOCS/client_api_examples/qml/main.h new file mode 100644 index 0000000000..9a65ae5350 --- /dev/null +++ b/DOCS/client_api_examples/qml/main.h @@ -0,0 +1,36 @@ +#ifndef MPVRENDERER_H_ +#define MPVRENDERER_H_ + +#include + +#include +#include +#include + +class MpvRenderer; + +class MpvObject : public QQuickFramebufferObject +{ + Q_OBJECT + + mpv::qt::Handle mpv; + mpv_opengl_cb_context *mpv_gl; + + friend class MpvRenderer; + +public: + MpvObject(QQuickItem * parent = 0); + virtual ~MpvObject(); + virtual Renderer *createRenderer() const; +public slots: + void command(const QVariant& params); + void setProperty(const QString& name, const QVariant& value); +signals: + void onUpdate(); +private slots: + void doUpdate(); +private: + static void on_update(void *ctx); +}; + +#endif diff --git a/DOCS/client_api_examples/qml/mpvrenderer.cpp b/DOCS/client_api_examples/qml/mpvrenderer.cpp deleted file mode 100644 index f24ae42ed3..0000000000 --- a/DOCS/client_api_examples/qml/mpvrenderer.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include "mpvrenderer.h" - -#include - -#include -#include -#include - -#include - -#include - -class MpvRenderer : public QQuickFramebufferObject::Renderer -{ - static void *get_proc_address(void *ctx, const char *name) { - (void)ctx; - QOpenGLContext *glctx = QOpenGLContext::currentContext(); - if (!glctx) - return NULL; - return (void *)glctx->getProcAddress(QByteArray(name)); - } - - mpv::qt::Handle mpv; - QQuickWindow *window; - mpv_opengl_cb_context *mpv_gl; -public: - MpvRenderer(const MpvObject *obj) - : mpv(obj->mpv), window(obj->window()), mpv_gl(obj->mpv_gl) - { - int r = mpv_opengl_cb_init_gl(mpv_gl, NULL, get_proc_address, NULL); - if (r < 0) - throw std::runtime_error("could not initialize OpenGL"); - } - - virtual ~MpvRenderer() - { - // Until this call is done, we need to make sure the player remains - // alive. This is done implicitly with the mpv::qt::Handle instance - // in this class. - mpv_opengl_cb_uninit_gl(mpv_gl); - } - - void render() - { - QOpenGLFramebufferObject *fbo = framebufferObject(); - int vp[4] = {0, 0, fbo->width(), fbo->height()}; - window->resetOpenGLState(); - mpv_opengl_cb_render(mpv_gl, fbo->handle(), vp); - window->resetOpenGLState(); - } -}; - -MpvObject::MpvObject(QQuickItem * parent) - : QQuickFramebufferObject(parent), mpv_gl(0) -{ - mpv = mpv::qt::Handle::FromRawHandle(mpv_create()); - if (!mpv) - throw std::runtime_error("could not create mpv context"); - - mpv_set_option_string(mpv, "terminal", "yes"); - mpv_set_option_string(mpv, "msg-level", "all=v"); - - if (mpv_initialize(mpv) < 0) - throw std::runtime_error("could not initialize mpv context"); - - // Make use of the MPV_SUB_API_OPENGL_CB API. - mpv::qt::set_option_variant(mpv, "vo", "opengl-cb"); - - // Request hw decoding, just for testing. - mpv::qt::set_option_variant(mpv, "hwdec", "auto"); - - // Setup the callback that will make QtQuick update and redraw if there - // is a new video frame. Use a queued connection: this makes sure the - // doUpdate() function is run on the GUI thread. - mpv_gl = (mpv_opengl_cb_context *)mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB); - if (!mpv_gl) - throw std::runtime_error("OpenGL not compiled in"); - mpv_opengl_cb_set_update_callback(mpv_gl, MpvObject::on_update, (void *)this); - connect(this, &MpvObject::onUpdate, this, &MpvObject::doUpdate, - Qt::QueuedConnection); -} - -MpvObject::~MpvObject() -{ - if (mpv_gl) - mpv_opengl_cb_set_update_callback(mpv_gl, NULL, NULL); -} - -void MpvObject::on_update(void *ctx) -{ - MpvObject *self = (MpvObject *)ctx; - emit self->onUpdate(); -} - -// connected to onUpdate(); signal makes sure it runs on the GUI thread -void MpvObject::doUpdate() -{ - update(); -} - -void MpvObject::command(const QVariant& params) -{ - mpv::qt::command_variant(mpv, params); -} - -void MpvObject::setProperty(const QString& name, const QVariant& value) -{ - mpv::qt::set_property_variant(mpv, name, value); -} - -QQuickFramebufferObject::Renderer *MpvObject::createRenderer() const -{ - window()->setPersistentOpenGLContext(true); - window()->setPersistentSceneGraph(true); - return new MpvRenderer(this); -} diff --git a/DOCS/client_api_examples/qml/mpvrenderer.h b/DOCS/client_api_examples/qml/mpvrenderer.h deleted file mode 100644 index 9a65ae5350..0000000000 --- a/DOCS/client_api_examples/qml/mpvrenderer.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef MPVRENDERER_H_ -#define MPVRENDERER_H_ - -#include - -#include -#include -#include - -class MpvRenderer; - -class MpvObject : public QQuickFramebufferObject -{ - Q_OBJECT - - mpv::qt::Handle mpv; - mpv_opengl_cb_context *mpv_gl; - - friend class MpvRenderer; - -public: - MpvObject(QQuickItem * parent = 0); - virtual ~MpvObject(); - virtual Renderer *createRenderer() const; -public slots: - void command(const QVariant& params); - void setProperty(const QString& name, const QVariant& value); -signals: - void onUpdate(); -private slots: - void doUpdate(); -private: - static void on_update(void *ctx); -}; - -#endif diff --git a/DOCS/client_api_examples/qml/mpvtest.pro b/DOCS/client_api_examples/qml/mpvtest.pro index df497e4dba..323b8bef71 100644 --- a/DOCS/client_api_examples/qml/mpvtest.pro +++ b/DOCS/client_api_examples/qml/mpvtest.pro @@ -1,7 +1,7 @@ QT += qml quick -HEADERS += mpvrenderer.h -SOURCES += mpvrenderer.cpp main.cpp +HEADERS += main.h +SOURCES += main.cpp CONFIG += link_pkgconfig debug PKGCONFIG += mpv -- cgit v1.2.3