| 1 | /* GStreamer |
| 2 | * Copyright (C) 2023 Seungha Yang <[email protected]> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <QApplication> |
| 21 | #include <QQmlApplicationEngine> |
| 22 | #include <QQuickWindow> |
| 23 | #include <QQuickItem> |
| 24 | #include <QTimer> |
| 25 | #include <gst/gst.h> |
| 26 | |
| 27 | static int |
| 28 | run_application (int argc, char ** argv, GstElement * pipeline) |
| 29 | { |
| 30 | QGuiApplication app(argc, argv); |
| 31 | int ret; |
| 32 | |
| 33 | /* NOTE: Default API on Windows is D3D11 already */ |
| 34 | QQuickWindow::setGraphicsApi(QSGRendererInterface::Direct3D11); |
| 35 | |
| 36 | QQmlApplicationEngine engine; |
| 37 | engine.load (QUrl (QStringLiteral ("qrc:/qml6d3d11sink-dyn-add.qml"))); |
| 38 | |
| 39 | QQuickWindow *rootObject; |
| 40 | |
| 41 | rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first()); |
| 42 | |
| 43 | gst_element_set_state (pipeline, GST_STATE_PLAYING); |
| 44 | |
| 45 | GstElement *tee = gst_bin_get_by_name (GST_BIN (pipeline), "t"); |
| 46 | /* Add the qml6d3d11sink element on timer callback */ |
| 47 | QTimer::singleShot(5000, [&]() |
| 48 | { |
| 49 | GstElement *queue, *sink; |
| 50 | QQuickItem *videoItem = rootObject->findChild<QQuickItem *> ("videoItem"); |
| 51 | |
| 52 | queue = gst_element_factory_make ("queue", nullptr); |
| 53 | sink = gst_element_factory_make ("qml6d3d11sink", nullptr); |
| 54 | |
| 55 | g_object_set (sink, "widget", videoItem, nullptr); |
| 56 | |
| 57 | gst_println ("Adding new qml6d3d11sink to pipeline"); |
| 58 | |
| 59 | gst_bin_add_many (GST_BIN (pipeline), queue, sink, nullptr); |
| 60 | |
| 61 | gst_element_sync_state_with_parent (sink); |
| 62 | gst_element_sync_state_with_parent (queue); |
| 63 | |
| 64 | gst_element_link_many (tee, queue, sink, nullptr); |
| 65 | |
| 66 | gst_object_unref (tee); |
| 67 | }); |
| 68 | |
| 69 | ret = app.exec(); |
| 70 | |
| 71 | gst_element_set_state (pipeline, GST_STATE_NULL); |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | main(int argc, char *argv[]) |
| 78 | { |
| 79 | GstElement *pipeline = nullptr; |
| 80 | GstElement *sink = nullptr; |
| 81 | int exit_code; |
| 82 | |
| 83 | gst_init (&argc, &argv); |
| 84 | |
| 85 | pipeline = gst_parse_launch ("d3d11testsrc ! " |
| 86 | "video/x-raw(memory:D3D11Memory),format=RGBA ! " |
| 87 | "tee name=t allow-not-linked=true ! queue ! fakesink sync=true", nullptr); |
| 88 | |
| 89 | /* the plugin must be loaded before loading the qml file to register the |
| 90 | * GstD3D11Qt6VideoItem */ |
| 91 | sink = gst_element_factory_make ("qml6d3d11sink", nullptr); |
| 92 | gst_object_unref (sink); |
| 93 | |
| 94 | exit_code = run_application (argc, argv, pipeline); |
| 95 | |
| 96 | gst_object_unref (pipeline); |
| 97 | |
| 98 | gst_deinit (); |
| 99 | |
| 100 | return exit_code; |
| 101 | } |