VideoStreaming.cc 6.3 KB
Newer Older
1
/****************************************************************************
2
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10 11 12 13


/**
 * @file
 *   @brief QGC Video Streaming Initialization
Gus Grubba's avatar
Gus Grubba committed
14
 *   @author Gus Grubba <gus@auterion.com>
15 16 17 18 19 20 21
 */

#include <QtQml>
#include <QDebug>

#if defined(QGC_GST_STREAMING)
#include <gst/gst.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#if defined(__android__)
#include <android/log.h>

static void gst_android_log(GstDebugCategory * category,
                            GstDebugLevel      level,
                            const gchar      * file,
                            const gchar      * function,
                            gint               line,
                            GObject          * object,
                            GstDebugMessage  * message,
                            gpointer           data)
{
    if (level <= gst_debug_category_get_threshold (category)) {
        __android_log_print(ANDROID_LOG_ERROR, "GST", "%s, %s: %s", file, function, gst_debug_message_get(message));
    }
}
#elif defined(__ios__)
39 40
#include "gst_ios_init.h"
#endif
41 42
#else
#include "GLVideoItemStub.h"
43 44 45 46 47 48 49
#endif

#include "VideoStreaming.h"

#if defined(QGC_GST_STREAMING)
    G_BEGIN_DECLS
    // The static plugins we use
50
#if defined(__android__) || defined(__ios__)
51
    GST_PLUGIN_STATIC_DECLARE(coreelements);
52
    GST_PLUGIN_STATIC_DECLARE(playback);
53 54
    GST_PLUGIN_STATIC_DECLARE(libav);
    GST_PLUGIN_STATIC_DECLARE(rtp);
55
    GST_PLUGIN_STATIC_DECLARE(rtsp);
56 57 58
    GST_PLUGIN_STATIC_DECLARE(udp);
    GST_PLUGIN_STATIC_DECLARE(videoparsersbad);
    GST_PLUGIN_STATIC_DECLARE(x264);
59
    GST_PLUGIN_STATIC_DECLARE(rtpmanager);
60 61
    GST_PLUGIN_STATIC_DECLARE(isomp4);
    GST_PLUGIN_STATIC_DECLARE(matroska);
62
    GST_PLUGIN_STATIC_DECLARE(opengl);
63 64
#if defined(__android__)
    GST_PLUGIN_STATIC_DECLARE(androidmedia);
65 66 67
#elif defined(__ios__)
    GST_PLUGIN_STATIC_DECLARE(applemedia);
#endif
68
#endif
69
    GST_PLUGIN_STATIC_DECLARE(qmlgl);
70 71 72
    G_END_DECLS
#endif

dogmaphobic's avatar
dogmaphobic committed
73
#if defined(QGC_GST_STREAMING)
74
#if (defined(Q_OS_MAC) && defined(QGC_INSTALL_RELEASE)) || defined(Q_OS_WIN)
dogmaphobic's avatar
dogmaphobic committed
75 76 77 78 79 80
static void qgcputenv(const QString& key, const QString& root, const QString& path)
{
    QString value = root + path;
    qputenv(key.toStdString().c_str(), QByteArray(value.toStdString().c_str()));
}
#endif
Don Gagne's avatar
Don Gagne committed
81
#endif
dogmaphobic's avatar
dogmaphobic committed
82

83
void initializeVideoStreaming(int &argc, char* argv[], char* logpath, char* debuglevel)
84 85
{
#if defined(QGC_GST_STREAMING)
86
    #ifdef Q_OS_MAC
87 88 89 90 91 92 93 94 95 96
        #ifdef QGC_INSTALL_RELEASE
            QString currentDir = QCoreApplication::applicationDirPath();
            qgcputenv("GST_PLUGIN_SCANNER",           currentDir, "/../Frameworks/GStreamer.framework/Versions/1.0/libexec/gstreamer-1.0/gst-plugin-scanner");
            qgcputenv("GTK_PATH",                     currentDir, "/../Frameworks/GStreamer.framework/Versions/Current");
            qgcputenv("GIO_EXTRA_MODULES",            currentDir, "/../Frameworks/GStreamer.framework/Versions/Current/lib/gio/modules");
            qgcputenv("GST_PLUGIN_SYSTEM_PATH_1_0",   currentDir, "/../Frameworks/GStreamer.framework/Versions/Current/lib/gstreamer-1.0");
            qgcputenv("GST_PLUGIN_SYSTEM_PATH",       currentDir, "/../Frameworks/GStreamer.framework/Versions/Current/lib/gstreamer-1.0");
            qgcputenv("GST_PLUGIN_PATH_1_0",          currentDir, "/../Frameworks/GStreamer.framework/Versions/Current/lib/gstreamer-1.0");
            qgcputenv("GST_PLUGIN_PATH",              currentDir, "/../Frameworks/GStreamer.framework/Versions/Current/lib/gstreamer-1.0");
        #endif
Don Gagne's avatar
Don Gagne committed
97 98 99
    #elif defined(Q_OS_WIN)
        QString currentDir = QCoreApplication::applicationDirPath();
        qgcputenv("GST_PLUGIN_PATH", currentDir, "/gstreamer-plugins");
100
    #endif
101

102 103 104 105 106 107
    //-- Generic initialization
    if (qgetenv("GST_DEBUG").isEmpty() && logpath) {
        QString gstDebugFile = QString("%1/%2").arg(logpath).arg("gstreamer-log.txt");
        qDebug() << "GStreamer debug output:" << gstDebugFile;
        if (debuglevel) {
            qputenv("GST_DEBUG", debuglevel);
108
        }
109 110 111 112 113 114 115 116 117 118 119 120
        qputenv("GST_DEBUG_NO_COLOR", "1");
        qputenv("GST_DEBUG_FILE", gstDebugFile.toUtf8());
        qputenv("GST_DEBUG_DUMP_DOT_DIR", logpath);
    }

    // Initialize GStreamer
#if defined(__android__)
    gst_debug_add_log_function(gst_android_log, nullptr, nullptr);
#elif defined(__ios__)
    //-- iOS specific initialization
    gst_ios_pre_init();
#endif
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    GError* error = nullptr;
    if (!gst_init_check(&argc, &argv, &error)) {
        qCritical() << "gst_init_check() failed: " << error->message;
        g_error_free(error);
    }

    // The static plugins we use
#if defined(__android__) || defined(__ios__)
    GST_PLUGIN_STATIC_REGISTER(coreelements);
    GST_PLUGIN_STATIC_REGISTER(playback);
    GST_PLUGIN_STATIC_REGISTER(libav);
    GST_PLUGIN_STATIC_REGISTER(rtp);
    GST_PLUGIN_STATIC_REGISTER(rtsp);
    GST_PLUGIN_STATIC_REGISTER(udp);
    GST_PLUGIN_STATIC_REGISTER(videoparsersbad);
    GST_PLUGIN_STATIC_REGISTER(x264);
    GST_PLUGIN_STATIC_REGISTER(rtpmanager);
    GST_PLUGIN_STATIC_REGISTER(isomp4);
    GST_PLUGIN_STATIC_REGISTER(matroska);
    GST_PLUGIN_STATIC_REGISTER(opengl);

#if defined(__android__)
    GST_PLUGIN_STATIC_REGISTER(androidmedia);
#elif defined(__ios__)
    GST_PLUGIN_STATIC_REGISTER(applemedia);
#endif
#endif

#if defined(__ios__)
    gst_ios_post_init();
152
#endif
153

154 155 156 157
    /* the plugin must be loaded before loading the qml file to register the
     * GstGLVideoItem qml item
     * FIXME Add a QQmlExtensionPlugin into qmlglsink to register GstGLVideoItem
     * with the QML engine, then remove this */
158 159 160 161 162 163 164 165
    GstElement *sink = gst_element_factory_make("qmlglsink", nullptr);

    if (sink == nullptr) {
        GST_PLUGIN_STATIC_REGISTER(qmlgl);
        sink = gst_element_factory_make("qmlglsink", nullptr);
    }

    if (sink != nullptr) {
166
        gst_object_unref(sink);
167
        sink = nullptr;
168 169 170
    } else {
        qCritical() << "unable to find qmlglsink - you need to build it yourself and add to GST_PLUGIN_PATH";
    }
171
#else
172
    qmlRegisterType<GLVideoItemStub>("org.freedesktop.gstreamer.GLVideoItem", 1, 0, "GstGLVideoItem");
173 174 175 176
    Q_UNUSED(argc)
    Q_UNUSED(argv)
    Q_UNUSED(logpath)
    Q_UNUSED(debuglevel)
177
#endif
178
}