VideoReceiver.cc 19.2 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


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

#include "VideoReceiver.h"
18 19 20
#include "SettingsManager.h"
#include "QGCApplication.h"

Gus Grubba's avatar
Gus Grubba committed
21
#include <QDebug>
22
#include <QUrl>
23 24
#include <QDir>
#include <QDateTime>
25
#include <QSysInfo>
26

27 28
QGC_LOGGING_CATEGORY(VideoReceiverLog, "VideoReceiverLog")

Gus Grubba's avatar
Gus Grubba committed
29 30
VideoReceiver::VideoReceiver(QObject* parent)
    : QObject(parent)
31
#if defined(QGC_GST_STREAMING)
32
    , _running(false)
33
    , _recording(false)
34
    , _streaming(false)
35 36
    , _starting(false)
    , _stopping(false)
37 38 39
    , _sink(NULL)
    , _tee(NULL)
    , _pipeline(NULL)
40
    , _pipelineStopRec(NULL)
Gus Grubba's avatar
Gus Grubba committed
41
    , _videoSink(NULL)
42 43
    , _socket(NULL)
    , _serverPresent(false)
44
#endif
Gus Grubba's avatar
Gus Grubba committed
45
{
46 47 48
#if defined(QGC_GST_STREAMING)
    _timer.setSingleShot(true);
    connect(&_timer, &QTimer::timeout, this, &VideoReceiver::_timeout);
49 50 51
    connect(this, &VideoReceiver::msgErrorReceived, this, &VideoReceiver::_handleError);
    connect(this, &VideoReceiver::msgEOSReceived, this, &VideoReceiver::_handleEOS);
    connect(this, &VideoReceiver::msgStateChangedReceived, this, &VideoReceiver::_handleStateChanged);
52
#endif
Gus Grubba's avatar
Gus Grubba committed
53 54 55 56
}

VideoReceiver::~VideoReceiver()
{
57
#if defined(QGC_GST_STREAMING)
58 59 60 61
    stop();
    if(_socket) {
        delete _socket;
    }
62
#endif
Gus Grubba's avatar
Gus Grubba committed
63 64
}

65
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
66 67 68 69 70 71 72 73 74 75 76
void VideoReceiver::setVideoSink(GstElement* sink)
{
    if (_videoSink) {
        gst_object_unref(_videoSink);
        _videoSink = NULL;
    }
    if (sink) {
        _videoSink = sink;
        gst_object_ref_sink(_videoSink);
    }
}
77
#endif
Gus Grubba's avatar
Gus Grubba committed
78

79
#if defined(QGC_GST_STREAMING)
80
static void newPadCB(GstElement* element, GstPad* pad, gpointer data)
81
{
82
    gchar* name;
83 84
    name = gst_pad_get_name(pad);
    g_print("A new pad %s was created\n", name);
85 86
    GstCaps* p_caps = gst_pad_get_pad_template_caps (pad);
    gchar* description = gst_caps_to_string(p_caps);
87
    qCDebug(VideoReceiverLog) << p_caps << ", " << description;
88
    g_free(description);
89
    GstElement* p_rtph264depay = GST_ELEMENT(data);
90 91 92 93
    if(gst_element_link_pads(element, name, p_rtph264depay, "sink") == false)
        qCritical() << "newPadCB : failed to link elements\n";
    g_free(name);
}
94
#endif
95

96 97 98 99 100
#if defined(QGC_GST_STREAMING)
void VideoReceiver::_connected()
{
    //-- Server showed up. Now we start the stream.
    _timer.stop();
101
    _socket->deleteLater();
102 103 104 105 106 107 108 109 110 111
    _socket = NULL;
    _serverPresent = true;
    start();
}
#endif

#if defined(QGC_GST_STREAMING)
void VideoReceiver::_socketError(QAbstractSocket::SocketError socketError)
{
    Q_UNUSED(socketError);
112
    _socket->deleteLater();
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    _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);
135
    //qCDebug(VideoReceiverLog) << "Trying to connect to:" << url.host() << url.port();
136 137 138 139 140
    _socket->connectToHost(url.host(), url.port());
    _timer.start(5000);
}
#endif

141 142 143 144 145 146 147 148 149
// When we finish our pipeline will look like this:
//
//                                   +-->queue-->decoder-->_videosink
//                                   |
//    datasource-->demux-->parser-->tee
//
//                                   ^
//                                   |
//                                   +-Here we will later link elements for recording
Gus Grubba's avatar
Gus Grubba committed
150 151
void VideoReceiver::start()
{
152
#if defined(QGC_GST_STREAMING)
153 154
    qCDebug(VideoReceiverLog) << "start()";

Gus Grubba's avatar
Gus Grubba committed
155 156 157 158 159 160 161 162
    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;
    }
163 164 165 166
    if(_running) {
        qCDebug(VideoReceiverLog) << "Already running!";
        return;
    }
Gus Grubba's avatar
Gus Grubba committed
167

168
    _starting = true;
169

170
    bool isUdp = _uri.contains("udp://");
Gus Grubba's avatar
Gus Grubba committed
171

172 173 174 175 176 177
    //-- For RTSP, check to see if server is there first
    if(!_serverPresent && !isUdp) {
        _timer.start(100);
        return;
    }

Gus Grubba's avatar
Gus Grubba committed
178 179 180 181 182 183
    bool running = false;

    GstElement*     dataSource  = NULL;
    GstCaps*        caps        = NULL;
    GstElement*     demux       = NULL;
    GstElement*     parser      = NULL;
184
    GstElement*     queue       = NULL;
Gus Grubba's avatar
Gus Grubba committed
185
    GstElement*     decoder     = NULL;
186

Gus Grubba's avatar
Gus Grubba committed
187 188
    do {
        if ((_pipeline = gst_pipeline_new("receiver")) == NULL) {
189
            qCritical() << "VideoReceiver::start() failed. Error with gst_pipeline_new()";
Gus Grubba's avatar
Gus Grubba committed
190 191 192
            break;
        }

193 194 195 196
        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
197 198
        }

199 200
        if (!dataSource) {
            qCritical() << "VideoReceiver::start() failed. Error with data source for gst_element_factory_make()";
Gus Grubba's avatar
Gus Grubba committed
201 202 203
            break;
        }

204 205 206 207 208 209 210
        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 {
211
            g_object_set(G_OBJECT(dataSource), "location", qPrintable(_uri), "latency", 17, "udp-reconnect", 1, "timeout", static_cast<guint64>(5000000), NULL);
212
        }
Gus Grubba's avatar
Gus Grubba committed
213 214

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

219 220 221 222
        if(!isUdp) {
            g_signal_connect(dataSource, "pad-added", G_CALLBACK(newPadCB), demux);
        }

Gus Grubba's avatar
Gus Grubba committed
223
        if ((parser = gst_element_factory_make("h264parse", "h264-parser")) == NULL) {
224
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('h264parse')";
Gus Grubba's avatar
Gus Grubba committed
225 226 227 228
            break;
        }

        if ((decoder = gst_element_factory_make("avdec_h264", "h264-decoder")) == NULL) {
229
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('avdec_h264')";
Gus Grubba's avatar
Gus Grubba committed
230 231 232
            break;
        }

233
        if((_tee = gst_element_factory_make("tee", NULL)) == NULL)  {
234 235 236
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('tee')";
            break;
        }
Gus Grubba's avatar
Gus Grubba committed
237

238
        if((queue = gst_element_factory_make("queue", NULL)) == NULL)  {
239
            qCritical() << "VideoReceiver::start() failed. Error with gst_element_factory_make('queue')";
240 241
            break;
        }
242

243
        gst_bin_add_many(GST_BIN(_pipeline), dataSource, demux, parser, _tee, queue, decoder, _videoSink, NULL);
244

245 246
        if(isUdp) {
            // Link the pipeline in front of the tee
247 248
            if(!gst_element_link_many(dataSource, demux, parser, _tee, queue, decoder, _videoSink, NULL)) {
                qCritical() << "Unable to link elements.";
249 250 251
                break;
            }
        } else {
252 253
            if(!gst_element_link_many(demux, parser, _tee, queue, decoder, _videoSink, NULL)) {
                qCritical() << "Unable to link elements.";
254 255
                break;
            }
256 257
        }

258
        dataSource = demux = parser = queue = decoder = NULL;
Gus Grubba's avatar
Gus Grubba committed
259

260
        GstBus* bus = NULL;
Gus Grubba's avatar
Gus Grubba committed
261

262 263 264 265 266 267
        if ((bus = gst_pipeline_get_bus(GST_PIPELINE(_pipeline))) != NULL) {
            gst_bus_enable_sync_message_emission(bus);
            g_signal_connect(bus, "sync-message", G_CALLBACK(_onBusMessage), this);
            gst_object_unref(bus);
            bus = NULL;
        }
Gus Grubba's avatar
Gus Grubba committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

        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;
        }

301 302
        if (_tee != NULL) {
            gst_object_unref(_tee);
303 304 305
            dataSource = NULL;
        }

306 307
        if (queue != NULL) {
            gst_object_unref(queue);
308 309 310
            dataSource = NULL;
        }

Gus Grubba's avatar
Gus Grubba committed
311 312 313 314
        if (_pipeline != NULL) {
            gst_object_unref(_pipeline);
            _pipeline = NULL;
        }
315 316 317 318 319

        _running = false;
    } else {
        _running = true;
        qCDebug(VideoReceiverLog) << "Running";
Gus Grubba's avatar
Gus Grubba committed
320
    }
321
    _starting = false;
322
#endif
Gus Grubba's avatar
Gus Grubba committed
323 324 325 326
}

void VideoReceiver::stop()
{
327
#if defined(QGC_GST_STREAMING)
328
    qCDebug(VideoReceiverLog) << "stop()";
329 330 331
    if(!_streaming) {
        _shutdownPipeline();
    } else if (_pipeline != NULL && !_stopping) {
332 333 334 335
        qCDebug(VideoReceiverLog) << "Stopping _pipeline";
        gst_element_send_event(_pipeline, gst_event_new_eos());
        _stopping = true;
        GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(_pipeline));
336
        GstMessage* message = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_EOS|GST_MESSAGE_ERROR));
337
        gst_object_unref(bus);
338 339 340 341 342 343
        if(GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
            _shutdownPipeline();
            qCritical() << "Error stopping pipeline!";
        } else if(GST_MESSAGE_TYPE(message) == GST_MESSAGE_EOS) {
            _handleEOS();
        }
344
        gst_message_unref(message);
Gus Grubba's avatar
Gus Grubba committed
345
    }
346
#endif
Gus Grubba's avatar
Gus Grubba committed
347 348 349 350 351 352 353
}

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

354
#if defined(QGC_GST_STREAMING)
355
void VideoReceiver::_shutdownPipeline() {
356 357 358 359
    if(!_pipeline) {
        qCDebug(VideoReceiverLog) << "No pipeline";
        return;
    }
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    GstBus* bus = NULL;
    if ((bus = gst_pipeline_get_bus(GST_PIPELINE(_pipeline))) != NULL) {
        gst_bus_disable_sync_message_emission(bus);
        gst_object_unref(bus);
        bus = NULL;
    }
    gst_element_set_state(_pipeline, GST_STATE_NULL);
    gst_bin_remove(GST_BIN(_pipeline), _videoSink);
    gst_object_unref(_pipeline);
    _pipeline = NULL;
    delete _sink;
    _sink = NULL;
    _serverPresent = false;
    _streaming = false;
    _recording = false;
    _stopping = false;
    _running = false;
    emit recordingChanged();
}
379
#endif
380

381
#if defined(QGC_GST_STREAMING)
382 383 384 385 386 387 388 389 390 391
void VideoReceiver::_handleError() {
    qCDebug(VideoReceiverLog) << "Gstreamer error!";
    _shutdownPipeline();
}
#endif

#if defined(QGC_GST_STREAMING)
void VideoReceiver::_handleEOS() {
    if(_stopping) {
        _shutdownPipeline();
392
        qCDebug(VideoReceiverLog) << "Stopped";
393 394 395 396 397
    } else if(_recording && _sink->removing) {
        _shutdownRecordingBranch();
    } else {
        qCritical() << "VideoReceiver: Unexpected EOS!";
        _shutdownPipeline();
Gus Grubba's avatar
Gus Grubba committed
398 399
    }
}
400
#endif
Gus Grubba's avatar
Gus Grubba committed
401

402 403 404 405 406 407 408
#if defined(QGC_GST_STREAMING)
void VideoReceiver::_handleStateChanged() {
    _streaming = GST_STATE(_pipeline) == GST_STATE_PLAYING;
    qCDebug(VideoReceiverLog) << "State changed, _streaming:" << _streaming;
}
#endif

409
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
410 411 412 413 414
gboolean VideoReceiver::_onBusMessage(GstBus* bus, GstMessage* msg, gpointer data)
{
    Q_UNUSED(bus)
    Q_ASSERT(msg != NULL && data != NULL);
    VideoReceiver* pThis = (VideoReceiver*)data;
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

    switch(GST_MESSAGE_TYPE(msg)) {
    case(GST_MESSAGE_ERROR): {
        gchar* debug;
        GError* error;
        gst_message_parse_error(msg, &error, &debug);
        g_free(debug);
        qCritical() << error->message;
        g_error_free(error);
        pThis->msgErrorReceived();
    }
        break;
    case(GST_MESSAGE_EOS):
        pThis->msgEOSReceived();
        break;
    case(GST_MESSAGE_STATE_CHANGED):
        pThis->msgStateChangedReceived();
        break;
    default:
        break;
    }

Gus Grubba's avatar
Gus Grubba committed
437 438
    return TRUE;
}
439
#endif
440 441 442 443 444 445 446 447 448 449 450 451 452 453

// When we finish our pipeline will look like this:
//
//                                   +-->queue-->decoder-->_videosink
//                                   |
//    datasource-->demux-->parser-->tee
//                                   |
//                                   |    +--------------_sink-------------------+
//                                   |    |                                      |
//   we are adding these elements->  +->teepad-->queue-->matroskamux-->_filesink |
//                                        |                                      |
//                                        +--------------------------------------+
void VideoReceiver::startRecording(void)
{
454 455
#if defined(QGC_GST_STREAMING) && defined(QGC_ENABLE_VIDEORECORDING)

456 457 458 459 460 461 462
    qCDebug(VideoReceiverLog) << "startRecording()";
    // exit immediately if we are already recording
    if(_pipeline == NULL || _recording) {
        qCDebug(VideoReceiverLog) << "Already recording!";
        return;
    }

463 464 465
    QString savePath = qgcApp()->toolbox()->settingsManager()->videoSettings()->videoSavePath()->rawValue().toString();
    if(savePath.isEmpty()) {
        qgcApp()->showMessage("Unabled to record video. Video save path must be specified in Settings.");
466 467 468 469
        return;
    }

    _sink           = new Sink();
470 471 472 473 474 475 476 477 478 479 480
    _sink->teepad   = gst_element_get_request_pad(_tee, "src_%u");
    _sink->queue    = gst_element_factory_make("queue", NULL);
    _sink->mux      = gst_element_factory_make("matroskamux", NULL);
    _sink->filesink = gst_element_factory_make("filesink", NULL);
    _sink->removing = false;

    if(!_sink->teepad || !_sink->queue || !_sink->mux || !_sink->filesink) {
        qCritical() << "VideoReceiver::startRecording() failed to make _sink elements";
        return;
    }

481
    QString videoFile;
482
    videoFile = savePath + "/QGC-" + QDateTime::currentDateTime().toString("yyyy-MM-dd_hh.mm.ss") + ".mkv";
483

484 485
    g_object_set(G_OBJECT(_sink->filesink), "location", qPrintable(videoFile), NULL);
    qCDebug(VideoReceiverLog) << "New video file:" << videoFile;
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

    gst_object_ref(_sink->queue);
    gst_object_ref(_sink->mux);
    gst_object_ref(_sink->filesink);

    gst_bin_add_many(GST_BIN(_pipeline), _sink->queue, _sink->mux, _sink->filesink, NULL);
    gst_element_link_many(_sink->queue, _sink->mux, _sink->filesink, NULL);

    gst_element_sync_state_with_parent(_sink->queue);
    gst_element_sync_state_with_parent(_sink->mux);
    gst_element_sync_state_with_parent(_sink->filesink);

    GstPad* sinkpad = gst_element_get_static_pad(_sink->queue, "sink");
    gst_pad_link(_sink->teepad, sinkpad);
    gst_object_unref(sinkpad);

    _recording = true;
    emit recordingChanged();
    qCDebug(VideoReceiverLog) << "Recording started";
#endif
}

void VideoReceiver::stopRecording(void)
{
510
#if defined(QGC_GST_STREAMING) && defined(QGC_ENABLE_VIDEORECORDING)
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    qCDebug(VideoReceiverLog) << "stopRecording()";
    // exit immediately if we are not recording
    if(_pipeline == NULL || !_recording) {
        qCDebug(VideoReceiverLog) << "Not recording!";
        return;
    }
    // Wait for data block before unlinking
    gst_pad_add_probe(_sink->teepad, GST_PAD_PROBE_TYPE_IDLE, _unlinkCallBack, this, NULL);
#endif
}

// This is only installed on the transient _pipelineStopRec in order
// to finalize a video file. It is not used for the main _pipeline.
// -EOS has appeared on the bus of the temporary pipeline
// -At this point all of the recoring elements have been flushed, and the video file has been finalized
// -Now we can remove the temporary pipeline and its elements
#if defined(QGC_GST_STREAMING)
528
void VideoReceiver::_shutdownRecordingBranch()
529 530 531 532 533 534 535
{
    gst_bin_remove(GST_BIN(_pipelineStopRec), _sink->queue);
    gst_bin_remove(GST_BIN(_pipelineStopRec), _sink->mux);
    gst_bin_remove(GST_BIN(_pipelineStopRec), _sink->filesink);

    gst_element_set_state(_pipelineStopRec, GST_STATE_NULL);
    gst_object_unref(_pipelineStopRec);
536
    _pipelineStopRec = NULL;
537 538 539 540 541 542 543 544 545 546 547 548

    gst_element_set_state(_sink->filesink, GST_STATE_NULL);
    gst_element_set_state(_sink->mux, GST_STATE_NULL);
    gst_element_set_state(_sink->queue, GST_STATE_NULL);

    gst_object_unref(_sink->queue);
    gst_object_unref(_sink->mux);
    gst_object_unref(_sink->filesink);

    delete _sink;
    _sink = NULL;
    _recording = false;
549

550 551 552 553 554 555 556 557 558 559
    emit recordingChanged();
    qCDebug(VideoReceiverLog) << "Recording Stopped";
}
#endif

// -Unlink the recording branch from the tee in the main _pipeline
// -Create a second temporary pipeline, and place the recording branch elements into that pipeline
// -Setup watch and handler for EOS event on the temporary pipeline's bus
// -Send an EOS event at the beginning of that pipeline
#if defined(QGC_GST_STREAMING)
560
void VideoReceiver::_detachRecordingBranch(GstPadProbeInfo* info)
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
{
    Q_UNUSED(info)

    // Also unlinks and unrefs
    gst_bin_remove_many(GST_BIN(_pipeline), _sink->queue, _sink->mux, _sink->filesink, NULL);

    // Give tee its pad back
    gst_element_release_request_pad(_tee, _sink->teepad);
    gst_object_unref(_sink->teepad);

    // Create temporary pipeline
    _pipelineStopRec = gst_pipeline_new("pipeStopRec");

    // Put our elements from the recording branch into the temporary pipeline
    gst_bin_add_many(GST_BIN(_pipelineStopRec), _sink->queue, _sink->mux, _sink->filesink, NULL);
    gst_element_link_many(_sink->queue, _sink->mux, _sink->filesink, NULL);

578 579 580
    // Add handler for EOS event
    GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(_pipelineStopRec));
    gst_bus_enable_sync_message_emission(bus);
581
    g_signal_connect(bus, "sync-message", G_CALLBACK(_onBusMessage), this);
582
    gst_object_unref(bus);
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

    if(gst_element_set_state(_pipelineStopRec, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
        qCDebug(VideoReceiverLog) << "problem starting _pipelineStopRec";
    }

    // Send EOS at the beginning of the pipeline
    GstPad* sinkpad = gst_element_get_static_pad(_sink->queue, "sink");
    gst_pad_send_event(sinkpad, gst_event_new_eos());
    gst_object_unref(sinkpad);
    qCDebug(VideoReceiverLog) << "Recording branch unlinked";
}
#endif

#if defined(QGC_GST_STREAMING)
GstPadProbeReturn VideoReceiver::_unlinkCallBack(GstPad* pad, GstPadProbeInfo* info, gpointer user_data)
{
    Q_UNUSED(pad);
    Q_ASSERT(info != NULL && user_data != NULL);
    VideoReceiver* pThis = (VideoReceiver*)user_data;
602 603 604 605
    // We will only act once
    if(g_atomic_int_compare_and_exchange(&pThis->_sink->removing, FALSE, TRUE))
        pThis->_detachRecordingBranch(info);

606 607 608
    return GST_PAD_PROBE_REMOVE;
}
#endif