VideoReceiver.h 5.64 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.
 *
 ****************************************************************************/
Gus Grubba's avatar
Gus Grubba committed
9 10 11 12

/**
 * @file
 *   @brief QGC Video Receiver
Gus Grubba's avatar
Gus Grubba committed
13
 *   @author Gus Grubba <gus@auterion.com>
Gus Grubba's avatar
Gus Grubba committed
14 15
 */

16
#pragma once
Gus Grubba's avatar
Gus Grubba committed
17

18
#include "QGCLoggingCategory.h"
Gus Grubba's avatar
Gus Grubba committed
19
#include <QObject>
20
#include <QSize>
21 22
#include <QTimer>
#include <QTcpSocket>
23 24 25 26
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
#include <QQueue>
27

28
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
29
#include <gst/gst.h>
30 31 32
typedef GstElement VideoSink;
#else
typedef void VideoSink;
33
#endif
Gus Grubba's avatar
Gus Grubba committed
34

35 36
Q_DECLARE_LOGGING_CATEGORY(VideoReceiverLog)

37
class VideoReceiver : public QThread
Gus Grubba's avatar
Gus Grubba committed
38 39
{
    Q_OBJECT
40

41
public:
Gus Grubba's avatar
Gus Grubba committed
42
    explicit VideoReceiver(QObject* parent = nullptr);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    ~VideoReceiver(void);

    typedef enum {
        FILE_FORMAT_MIN = 0,
        FILE_FORMAT_MKV = FILE_FORMAT_MIN,
        FILE_FORMAT_MOV,
        FILE_FORMAT_MP4,
        FILE_FORMAT_MAX
    } FILE_FORMAT;

    Q_PROPERTY(bool     streaming   READ    streaming   NOTIFY  streamingChanged)
    Q_PROPERTY(bool     decoding    READ    decoding    NOTIFY  decodingChanged)
    Q_PROPERTY(bool     recording   READ    recording   NOTIFY  recordingChanged)
    Q_PROPERTY(QSize    videoSize   READ    videoSize   NOTIFY  videoSizeChanged)

    bool streaming(void) {
        return _streaming;
    }

    bool decoding(void) {
        return _decoding;
    }

    bool recording(void) {
        return _recording;
    }

    QSize videoSize(void) {
        const quint32 size = _videoSize;
        return QSize((size >> 16) & 0xFFFF, size & 0xFFFF);
    }
74

75
signals:
76 77 78 79 80 81 82
    void timeout(void);
    void streamingChanged(void);
    void decodingChanged(void);
    void recordingChanged(void);
    void recordingStarted(void);
    void videoSizeChanged(void);
    void screenshotComplete(void);
83

84
public slots:
85 86 87 88 89 90 91
    virtual void start(const QString& uri, unsigned timeout);
    virtual void stop(void);
    virtual void startDecoding(VideoSink* videoSink);
    virtual void stopDecoding(void);
    virtual void startRecording(const QString& videoFile, FILE_FORMAT format);
    virtual void stopRecording(void);
    virtual void takeScreenshot(const QString& imageFile);
92

93
#if defined(QGC_GST_STREAMING)
94 95 96
protected slots:
    virtual void _watchdog(void);
    virtual void _handleEOS(void);
Gus Grubba's avatar
Gus Grubba committed
97

98
protected:
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    void _setVideoSize(const QSize& size) {
        _videoSize = ((quint32)size.width() << 16) | (quint32)size.height();
        emit videoSizeChanged();
    }

    virtual GstElement* _makeSource(const QString& uri);
    virtual GstElement* _makeDecoder(GstCaps* caps, GstElement* videoSink);
    virtual GstElement* _makeFileSink(const QString& videoFile, FILE_FORMAT format);

    virtual void _onNewSourcePad(GstPad* pad);
    virtual void _onNewDecoderPad(GstPad* pad);
    virtual bool _addDecoder(GstElement* src);
    virtual bool _addVideoSink(GstPad* pad);
    virtual void _noteTeeFrame(void);
    virtual void _noteVideoSinkFrame(void);
    virtual void _noteEndOfStream(void);
    virtual void _unlinkBranch(GstElement* from);
    virtual void _shutdownDecodingBranch (void);
    virtual void _shutdownRecordingBranch(void);

    typedef std::function<void(void)> Task;
    bool _isOurThread(void);
    void _post(Task t);
    void run(void);

private:
    static gboolean _onBusMessage(GstBus* bus, GstMessage* message, gpointer user_data);
    static void _onNewPad(GstElement* element, GstPad* pad, gpointer data);
    static void _wrapWithGhostPad(GstElement* element, GstPad* pad, gpointer data);
    static void _linkPadWithOptionalBuffer(GstElement* element, GstPad* pad, gpointer data);
    static gboolean _padProbe(GstElement* element, GstPad* pad, gpointer user_data);
    static gboolean _autoplugQueryCaps(GstElement* bin, GstPad* pad, GstElement* element, GstQuery* query, gpointer data);
    static gboolean _autoplugQueryContext(GstElement* bin, GstPad* pad, GstElement* element, GstQuery* query, gpointer data);
    static gboolean _autoplugQuery(GstElement* bin, GstPad* pad, GstElement* element, GstQuery* query, gpointer data);
    static GstPadProbeReturn _teeProbe(GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
    static GstPadProbeReturn _videoSinkProbe(GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
    static GstPadProbeReturn _eosProbe(GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
    static GstPadProbeReturn _keyframeWatch(GstPad* pad, GstPadProbeInfo* info, gpointer user_data);

    bool                _removingDecoder;
    bool                _removingRecorder;
    GstElement*         _source;
141
    GstElement*         _tee;
142 143 144 145 146 147
    GstElement*         _decoderValve;
    GstElement*         _recorderValve;
    GstElement*         _decoder;
    GstElement*         _videoSink;
    GstElement*         _fileSink;
    GstElement*         _pipeline;
148

149 150 151 152
    qint64              _lastSourceFrameTime;
    qint64              _lastVideoFrameTime;
    bool                _resetVideoSink;
    gulong              _videoSinkProbeId;
153

154
    QTimer              _watchdogTimer;
155

156 157
    //-- RTSP UDP reconnect timeout
    uint64_t            _udpReconnect_us;
158

159
    unsigned            _timeout;
Gus Grubba's avatar
Gus Grubba committed
160

161 162 163 164
    QWaitCondition      _taskQueueUpdate;
    QMutex              _taskQueueSync;
    QQueue<Task>        _taskQueue;
    bool                _shutdown;
165

166 167 168
    static const char*  _kFileMux[FILE_FORMAT_MAX - FILE_FORMAT_MIN];
#else
private:
169 170
#endif

171 172 173 174
    std::atomic<bool>   _streaming;
    std::atomic<bool>   _decoding;
    std::atomic<bool>   _recording;
    std::atomic<quint32>_videoSize;
Gus Grubba's avatar
Gus Grubba committed
175
};