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


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

#include "VideoReceiver.h"
#include <QDebug>
19
#include <QUrl>
Gus Grubba's avatar
Gus Grubba committed
20 21 22

VideoReceiver::VideoReceiver(QObject* parent)
    : QObject(parent)
23
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
24 25
    , _pipeline(NULL)
    , _videoSink(NULL)
26 27
    , _socket(NULL)
    , _serverPresent(false)
28
#endif
Gus Grubba's avatar
Gus Grubba committed
29
{
30 31 32 33
#if defined(QGC_GST_STREAMING)
    _timer.setSingleShot(true);
    connect(&_timer, &QTimer::timeout, this, &VideoReceiver::_timeout);
#endif
Gus Grubba's avatar
Gus Grubba committed
34 35 36 37
}

VideoReceiver::~VideoReceiver()
{
38
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
39 40
    stop();
    setVideoSink(NULL);
41 42 43
    if(_socket) {
        delete _socket;
    }
44
#endif
Gus Grubba's avatar
Gus Grubba committed
45 46
}

47
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
48 49 50 51 52 53 54 55 56 57 58
void VideoReceiver::setVideoSink(GstElement* sink)
{
    if (_videoSink) {
        gst_object_unref(_videoSink);
        _videoSink = NULL;
    }
    if (sink) {
        _videoSink = sink;
        gst_object_ref_sink(_videoSink);
    }
}
59
#endif
Gus Grubba's avatar
Gus Grubba committed
60

61
#if defined(QGC_GST_STREAMING)
62 63 64 65 66 67 68 69 70 71 72 73 74 75
static void newPadCB(GstElement * element, GstPad* pad, gpointer data)
{
    gchar *name;
    name = gst_pad_get_name(pad);
    g_print("A new pad %s was created\n", name);
    GstCaps * p_caps = gst_pad_get_pad_template_caps (pad);
    gchar * description = gst_caps_to_string(p_caps);
    qDebug() << p_caps << ", " << description;
    g_free(description);
    GstElement * p_rtph264depay = GST_ELEMENT(data);
    if(gst_element_link_pads(element, name, p_rtph264depay, "sink") == false)
        qCritical() << "newPadCB : failed to link elements\n";
    g_free(name);
}
76
#endif
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#if defined(QGC_GST_STREAMING)
void VideoReceiver::_connected()
{
    //-- Server showed up. Now we start the stream.
    _timer.stop();
    delete _socket;
    _socket = NULL;
    _serverPresent = true;
    start();
}
#endif

#if defined(QGC_GST_STREAMING)
void VideoReceiver::_socketError(QAbstractSocket::SocketError socketError)
{
    Q_UNUSED(socketError);
    delete _socket;
    _socket = NULL;
    //-- Try again in 5 seconds
    _timer.start(5000);
}
#endif

#if defined(QGC_GST_STREAMING)
void VideoReceiver::_timeout()
{
    //-- If socket is live, we got no connection nor a socket error
    if(_socket) {
        delete _socket;
        _socket = NULL;
    }
    //-- RTSP will try to connect to the server. If it cannot connect,
    //   it will simply give up and never try again. Instead, we keep
    //   attempting a connection on this timer. Once a connection is
    //   found to be working, only then we actually start the stream.
    QUrl url(_uri);
    _socket = new QTcpSocket;
    connect(_socket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QTcpSocket::error), this, &VideoReceiver::_socketError);
    connect(_socket, &QTcpSocket::connected, this, &VideoReceiver::_connected);
    //qDebug() << "Trying to connect to:" << url.host() << url.port();
    _socket->connectToHost(url.host(), url.port());
    _timer.start(5000);
}
#endif

Gus Grubba's avatar
Gus Grubba committed
123 124
void VideoReceiver::start()
{
125
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
126 127 128 129 130 131 132 133 134
    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;
    }

135 136
    bool isUdp = _uri.contains("udp://");

Gus Grubba's avatar
Gus Grubba committed
137 138
    stop();

139 140 141 142 143 144
    //-- For RTSP, check to see if server is there first
    if(!_serverPresent && !isUdp) {
        _timer.start(100);
        return;
    }

Gus Grubba's avatar
Gus Grubba committed
145 146 147 148 149 150 151 152 153 154
    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) {
155
            qCritical() << "VideoReceiver::start() failed. Error with gst_pipeline_new()";
Gus Grubba's avatar
Gus Grubba committed
156 157 158
            break;
        }

159 160 161 162
        if(isUdp) {
            dataSource = gst_element_factory_make("udpsrc", "udp-source");
        } else {
            dataSource = gst_element_factory_make("rtspsrc", "rtsp-source");
Gus Grubba's avatar
Gus Grubba committed
163 164
        }

165 166
        if (!dataSource) {
            qCritical() << "VideoReceiver::start() failed. Error with data source for gst_element_factory_make()";
Gus Grubba's avatar
Gus Grubba committed
167 168 169
            break;
        }

170 171 172 173 174 175 176
        if(isUdp) {
            if ((caps = gst_caps_from_string("application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264")) == NULL) {
                qCritical() << "VideoReceiver::start() failed. Error with gst_caps_from_string()";
                break;
            }
            g_object_set(G_OBJECT(dataSource), "uri", qPrintable(_uri), "caps", caps, NULL);
        } else {
177
            g_object_set(G_OBJECT(dataSource), "location", qPrintable(_uri), "latency", 0, "udp-reconnect", 1, "timeout", 5000000, NULL);
178
        }
Gus Grubba's avatar
Gus Grubba committed
179 180

        if ((demux = gst_element_factory_make("rtph264depay", "rtp-h264-depacketizer")) == NULL) {
181
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('rtph264depay')";
Gus Grubba's avatar
Gus Grubba committed
182 183 184
            break;
        }

185 186 187 188
        if(!isUdp) {
            g_signal_connect(dataSource, "pad-added", G_CALLBACK(newPadCB), demux);
        }

Gus Grubba's avatar
Gus Grubba committed
189
        if ((parser = gst_element_factory_make("h264parse", "h264-parser")) == NULL) {
190
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('h264parse')";
Gus Grubba's avatar
Gus Grubba committed
191 192 193 194
            break;
        }

        if ((decoder = gst_element_factory_make("avdec_h264", "h264-decoder")) == NULL) {
195
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('avdec_h264')";
Gus Grubba's avatar
Gus Grubba committed
196 197 198 199 200
            break;
        }

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

201 202 203 204 205 206 207 208 209
        gboolean res = FALSE;

        if(isUdp) {
            res = gst_element_link_many(dataSource, demux, parser, decoder, _videoSink, NULL);
        } else {
            res = gst_element_link_many(demux, parser, decoder, _videoSink, NULL);
        }

        if (!res) {
210
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_link_many()";
Gus Grubba's avatar
Gus Grubba committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            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;
        }
    }
261
#endif
Gus Grubba's avatar
Gus Grubba committed
262 263 264 265
}

void VideoReceiver::stop()
{
266
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
267 268 269 270
    if (_pipeline != NULL) {
        gst_element_set_state(_pipeline, GST_STATE_NULL);
        gst_object_unref(_pipeline);
        _pipeline = NULL;
271
        _serverPresent = false;
Gus Grubba's avatar
Gus Grubba committed
272
    }
273
#endif
Gus Grubba's avatar
Gus Grubba committed
274 275 276 277 278 279 280 281
}

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

282
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
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;
    }
}
304
#endif
Gus Grubba's avatar
Gus Grubba committed
305

306
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
307 308 309 310 311 312 313 314
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;
}
315
#endif