VideoReceiver.h 5.7 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 21 22
#include <QTimer>
#include <QTcpSocket>

23
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
24
#include <gst/gst.h>
25
#endif
Gus Grubba's avatar
Gus Grubba committed
26

27 28
Q_DECLARE_LOGGING_CATEGORY(VideoReceiverLog)

29 30
class VideoSettings;

Gus Grubba's avatar
Gus Grubba committed
31 32 33 34
class VideoReceiver : public QObject
{
    Q_OBJECT
public:
35 36 37 38 39 40 41
    enum VideoEncoding {
        H264_SW = 1,
        H264_HW = 2,
        H265_SW = 3,
        H265_HW = 4
    };

42
#if defined(QGC_GST_STREAMING)
43
    Q_PROPERTY(bool             recording           READ    recording           NOTIFY recordingChanged)
44
#endif
Gus Grubba's avatar
Gus Grubba committed
45 46
    Q_PROPERTY(bool             videoRunning        READ    videoRunning        NOTIFY  videoRunningChanged)
    Q_PROPERTY(QString          imageFile           READ    imageFile           NOTIFY  imageFileChanged)
47
    Q_PROPERTY(QString          videoFile           READ    videoFile           NOTIFY  videoFileChanged)
Gus Grubba's avatar
Gus Grubba committed
48
    Q_PROPERTY(bool             showFullScreen      READ    showFullScreen      WRITE   setShowFullScreen     NOTIFY showFullScreenChanged)
49

Gus Grubba's avatar
Gus Grubba committed
50
    explicit VideoReceiver(QObject* parent = nullptr);
Gus Grubba's avatar
Gus Grubba committed
51 52
    ~VideoReceiver();

53
#if defined(QGC_GST_STREAMING)
54
    virtual bool            recording       () { return _recording; }
55 56
#endif

57 58 59 60
    virtual bool            videoRunning    () { return _videoRunning; }
    virtual QString         imageFile       () { return _imageFile; }
    virtual QString         videoFile       () { return _videoFile; }
    virtual bool            showFullScreen  () { return _showFullScreen; }
Gus Grubba's avatar
Gus Grubba committed
61

62
    virtual void            grabImage       (QString imageFile);
63

64
    virtual void        setShowFullScreen   (bool show) { _showFullScreen = show; emit showFullScreenChanged(); }
65

66
    void                  setVideoDecoder   (VideoEncoding encoding);
67 68 69
#if defined(QGC_GST_STREAMING)
    void                  setVideoSink      (GstElement* videoSink);
#endif
70

71
signals:
72 73 74 75
    void videoRunningChanged                ();
    void imageFileChanged                   ();
    void videoFileChanged                   ();
    void showFullScreenChanged              ();
76
#if defined(QGC_GST_STREAMING)
77 78 79 80
    void recordingChanged                   ();
    void msgErrorReceived                   ();
    void msgEOSReceived                     ();
    void msgStateChangedReceived            ();
81
    void gotFirstRecordingKeyFrame          ();
82
#endif
83

84
public slots:
85 86 87 88 89 90 91 92
    virtual void start                      ();
    virtual void stop                       ();
    virtual void setUri                     (const QString& uri);
    virtual void stopRecording              ();
    virtual void startRecording             (const QString& videoFile = QString());

protected slots:
    virtual void _updateTimer               ();
93
#if defined(QGC_GST_STREAMING)
94
    virtual void _restart_timeout           ();
95 96 97
    virtual void _tcp_timeout               ();
    virtual void _connected                 ();
    virtual void _socketError               (QAbstractSocket::SocketError socketError);
98 99 100
    virtual void _handleError               ();
    virtual void _handleEOS                 ();
    virtual void _handleStateChanged        ();
101
#endif
Gus Grubba's avatar
Gus Grubba committed
102

103
protected:
104
#if defined(QGC_GST_STREAMING)
105

106 107
    typedef struct
    {
108 109 110 111
        GstPad*         teepad;
        GstElement*     queue;
        GstElement*     mux;
        GstElement*     filesink;
112
        GstElement*     parse;
113
        gboolean        removing;
114 115
    } Sink;

116
    bool                _running;
117
    bool                _recording;
118
    bool                _streaming;
119 120
    bool                _starting;
    bool                _stopping;
121
    bool                _stop;
122 123
    Sink*               _sink;
    GstElement*         _tee;
124

125 126
    void _noteVideoSinkFrame                            ();

127 128
    static gboolean             _onBusMessage           (GstBus* bus, GstMessage* message, gpointer user_data);
    static GstPadProbeReturn    _unlinkCallBack         (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
129
    static GstPadProbeReturn    _videoSinkProbe         (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
130
    static GstPadProbeReturn    _keyframeWatch          (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
131 132 133 134 135

    virtual void                _detachRecordingBranch  (GstPadProbeInfo* info);
    virtual void                _shutdownRecordingBranch();
    virtual void                _shutdownPipeline       ();
    virtual void                _cleanupOldVideos       ();
136

137 138 139
    GstElement*     _pipeline;
    GstElement*     _pipelineStopRec;
    GstElement*     _videoSink;
140
    guint64         _lastFrameId;
141
    qint64          _lastFrameTime;
Gus Grubba's avatar
Gus Grubba committed
142

143 144
    //-- Wait for Video Server to show up before starting
    QTimer          _frameTimer;
145 146
    QTimer          _restart_timer;
    int             _restart_time_ms;
147 148 149 150
    QTimer          _tcp_timer;
    QTcpSocket*     _socket;
    bool            _serverPresent;
    int             _tcpTestInterval_ms;
151

152 153
    //-- RTSP UDP reconnect timeout
    uint64_t        _udpReconnect_us;
154 155
#endif

156 157
    QString         _uri;
    QString         _imageFile;
158
    QString         _videoFile;
159 160
    bool            _videoRunning;
    bool            _showFullScreen;
161
    VideoSettings*  _videoSettings;
162 163
    const char*     _depayName;
    const char*     _parserName;
164 165 166
    bool            _tryWithHardwareDecoding = true;
    const char*     _hwDecoderName;
    const char*     _swDecoderName;
Gus Grubba's avatar
Gus Grubba committed
167 168
};