| 1 |
// SPDX-License-Identifier: BSD-2-Clause |
| 2 |
// |
| 3 |
// Copyright (C) 2022, Matthew Waters <[email protected]> |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are met: |
| 7 |
// |
| 8 |
// a) Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions and the following disclaimer. |
| 10 |
// |
| 11 |
// b) Redistributions in binary form must reproduce the above copyright |
| 12 |
// notice, this list of conditions and the following disclaimer in |
| 13 |
// the documentation and/or other materials provided with the distribution. |
| 14 |
// |
| 15 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 |
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 19 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 |
// THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
|
| 27 |
#include <QApplication> |
| 28 |
#include <QQmlApplicationEngine> |
| 29 |
#include <QQuickWindow> |
| 30 |
#include <QQuickItem> |
| 31 |
#include <QRunnable> |
| 32 |
#include <gst/gst.h> |
| 33 |
|
| 34 |
class SetPlaying : public QRunnable |
| 35 |
{ |
| 36 |
public: |
| 37 |
SetPlaying(GstElement *); |
| 38 |
~SetPlaying(); |
| 39 |
|
| 40 |
void run (); |
| 41 |
|
| 42 |
private: |
| 43 |
GstElement * pipeline_; |
| 44 |
}; |
| 45 |
|
| 46 |
SetPlaying::SetPlaying (GstElement * pipeline) |
| 47 |
{ |
| 48 |
this->pipeline_ = pipeline ? static_cast<GstElement *> (gst_object_ref (pipeline)) : NULL; |
| 49 |
} |
| 50 |
|
| 51 |
SetPlaying::~SetPlaying () |
| 52 |
{ |
| 53 |
if (this->pipeline_) |
| 54 |
gst_object_unref (this->pipeline_); |
| 55 |
} |
| 56 |
|
| 57 |
void |
| 58 |
SetPlaying::run () |
| 59 |
{ |
| 60 |
if (this->pipeline_) |
| 61 |
gst_element_set_state (this->pipeline_, GST_STATE_PLAYING); |
| 62 |
} |
| 63 |
|
| 64 |
int main(int argc, char *argv[]) |
| 65 |
{ |
| 66 |
int ret; |
| 67 |
|
| 68 |
gst_init (&argc, &argv); |
| 69 |
|
| 70 |
{ |
| 71 |
QGuiApplication app(argc, argv); |
| 72 |
|
| 73 |
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL); |
| 74 |
|
| 75 |
GstElement *pipeline = gst_pipeline_new (NULL); |
| 76 |
GstElement *src = gst_element_factory_make ("videotestsrc", NULL); |
| 77 |
GstElement *capsfilter = gst_element_factory_make ("capsfilter", NULL); |
| 78 |
GstCaps *caps = gst_caps_from_string ("video/x-raw,format=YV12"); |
| 79 |
g_object_set (capsfilter, "caps", caps, NULL); |
| 80 |
gst_clear_caps (&caps); |
| 81 |
GstElement *glupload = gst_element_factory_make ("glupload", NULL); |
| 82 |
/* the plugin must be loaded before loading the qml file to register the |
| 83 |
* GstGLVideoItem qml item */ |
| 84 |
GstElement *sink = gst_element_factory_make ("qml6glsink", NULL); |
| 85 |
|
| 86 |
g_assert (src && glupload && sink); |
| 87 |
|
| 88 |
gst_bin_add_many (GST_BIN (pipeline), src, capsfilter, glupload, sink, NULL); |
| 89 |
gst_element_link_many (src, capsfilter, glupload, sink, NULL); |
| 90 |
|
| 91 |
QQmlApplicationEngine engine; |
| 92 |
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); |
| 93 |
|
| 94 |
QQuickItem *videoItem; |
| 95 |
QQuickWindow *rootObject; |
| 96 |
|
| 97 |
/* find and set the videoItem on the sink */ |
| 98 |
rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first()); |
| 99 |
videoItem = rootObject->findChild<QQuickItem *> ("videoItem"); |
| 100 |
g_assert (videoItem); |
| 101 |
g_object_set(sink, "widget", videoItem, NULL); |
| 102 |
|
| 103 |
rootObject->scheduleRenderJob (new SetPlaying (pipeline), |
| 104 |
QQuickWindow::BeforeSynchronizingStage); |
| 105 |
|
| 106 |
ret = app.exec(); |
| 107 |
|
| 108 |
gst_element_set_state (pipeline, GST_STATE_NULL); |
| 109 |
gst_object_unref (pipeline); |
| 110 |
} |
| 111 |
|
| 112 |
gst_deinit (); |
| 113 |
|
| 114 |
return ret; |
| 115 |
} |