VideoReceiver.cc 6.15 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief QGC Video Receiver
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include "VideoReceiver.h"
#include <QDebug>

VideoReceiver::VideoReceiver(QObject* parent)
    : QObject(parent)
35
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
36 37
    , _pipeline(NULL)
    , _videoSink(NULL)
38
#endif
Gus Grubba's avatar
Gus Grubba committed
39 40 41 42 43 44
{

}

VideoReceiver::~VideoReceiver()
{
45
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
46 47
    stop();
    setVideoSink(NULL);
48
#endif
Gus Grubba's avatar
Gus Grubba committed
49 50
}

51
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
52 53 54 55 56 57 58 59 60 61 62
void VideoReceiver::setVideoSink(GstElement* sink)
{
    if (_videoSink) {
        gst_object_unref(_videoSink);
        _videoSink = NULL;
    }
    if (sink) {
        _videoSink = sink;
        gst_object_ref_sink(_videoSink);
    }
}
63
#endif
Gus Grubba's avatar
Gus Grubba committed
64 65 66

void VideoReceiver::start()
{
67
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    if (_uri.isEmpty()) {
        qCritical() << "VideoReceiver::start() failed because URI is not specified";
        return;
    }

    if (_videoSink == NULL) {
        qCritical() << "VideoReceiver::start() failed because video sink is not set";
        return;
    }

    stop();

    bool running = false;

    GstElement*     dataSource  = NULL;
    GstCaps*        caps        = NULL;
    GstElement*     demux       = NULL;
    GstElement*     parser      = NULL;
    GstElement*     decoder     = NULL;

    do {
        if ((_pipeline = gst_pipeline_new("receiver")) == NULL) {
90
            qCritical() << "VideoReceiver::start() failed. Error with gst_pipeline_new()";
Gus Grubba's avatar
Gus Grubba committed
91 92 93 94
            break;
        }

        if ((dataSource = gst_element_factory_make("udpsrc", "udp-source")) == NULL) {
95
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('udpsrc')";
Gus Grubba's avatar
Gus Grubba committed
96 97 98 99
            break;
        }

        if ((caps = gst_caps_from_string("application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264")) == NULL) {
100
            qCritical() << "VideoReceiver::start() failed. Error with gst_caps_from_string()";
Gus Grubba's avatar
Gus Grubba committed
101 102 103 104 105 106
            break;
        }

        g_object_set(G_OBJECT(dataSource), "uri", qPrintable(_uri), "caps", caps, NULL);

        if ((demux = gst_element_factory_make("rtph264depay", "rtp-h264-depacketizer")) == NULL) {
107
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('rtph264depay')";
Gus Grubba's avatar
Gus Grubba committed
108 109 110 111
            break;
        }

        if ((parser = gst_element_factory_make("h264parse", "h264-parser")) == NULL) {
112
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('h264parse')";
Gus Grubba's avatar
Gus Grubba committed
113 114 115 116
            break;
        }

        if ((decoder = gst_element_factory_make("avdec_h264", "h264-decoder")) == NULL) {
117
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('avdec_h264')";
Gus Grubba's avatar
Gus Grubba committed
118 119 120 121 122
            break;
        }

        gst_bin_add_many(GST_BIN(_pipeline), dataSource, demux, parser, decoder, _videoSink, NULL);

Gus Grubba's avatar
Gus Grubba committed
123
        if (gst_element_link_many(dataSource, demux, parser, decoder, _videoSink, NULL) != (gboolean)TRUE) {
124
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_link_many()";
Gus Grubba's avatar
Gus Grubba committed
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            break;
        }

        dataSource = demux = parser = decoder = NULL;

        GstBus* bus = NULL;

        if ((bus = gst_pipeline_get_bus(GST_PIPELINE(_pipeline))) != NULL) {
            gst_bus_add_watch(bus, _onBusMessage, this);
            gst_object_unref(bus);
            bus = NULL;
        }

        running = gst_element_set_state(_pipeline, GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE;

    } while(0);

    if (caps != NULL) {
        gst_caps_unref(caps);
        caps = NULL;
    }

    if (!running) {
        qCritical() << "VideoReceiver::start() failed";

        if (decoder != NULL) {
            gst_object_unref(decoder);
            decoder = NULL;
        }

        if (parser != NULL) {
            gst_object_unref(parser);
            parser = NULL;
        }

        if (demux != NULL) {
            gst_object_unref(demux);
            demux = NULL;
        }

        if (dataSource != NULL) {
            gst_object_unref(dataSource);
            dataSource = NULL;
        }

        if (_pipeline != NULL) {
            gst_object_unref(_pipeline);
            _pipeline = NULL;
        }
    }
175
#endif
Gus Grubba's avatar
Gus Grubba committed
176 177 178 179
}

void VideoReceiver::stop()
{
180
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
181 182 183 184 185
    if (_pipeline != NULL) {
        gst_element_set_state(_pipeline, GST_STATE_NULL);
        gst_object_unref(_pipeline);
        _pipeline = NULL;
    }
186
#endif
Gus Grubba's avatar
Gus Grubba committed
187 188 189 190 191 192 193 194
}

void VideoReceiver::setUri(const QString & uri)
{
    stop();
    _uri = uri;
}

195
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
void VideoReceiver::_onBusMessage(GstMessage* msg)
{
    switch (GST_MESSAGE_TYPE(msg)) {
    case GST_MESSAGE_EOS:
        stop();
        break;
    case GST_MESSAGE_ERROR:
        do {
            gchar* debug;
            GError* error;
            gst_message_parse_error(msg, &error, &debug);
            g_free(debug);
            qCritical() << error->message;
            g_error_free(error);
        } while(0);
        stop();
        break;
    default:
        break;
    }
}
217
#endif
Gus Grubba's avatar
Gus Grubba committed
218

219
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
220 221 222 223 224 225 226 227
gboolean VideoReceiver::_onBusMessage(GstBus* bus, GstMessage* msg, gpointer data)
{
    Q_UNUSED(bus)
    Q_ASSERT(msg != NULL && data != NULL);
    VideoReceiver* pThis = (VideoReceiver*)data;
    pThis->_onBusMessage(msg);
    return TRUE;
}
228
#endif