VideoReceiver.h 7.26 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
#if defined(QGC_GST_STREAMING)
37
    Q_PROPERTY(bool             recording           READ    recording           NOTIFY recordingChanged)
38
#endif
Gus Grubba's avatar
Gus Grubba committed
39 40
    Q_PROPERTY(bool             videoRunning        READ    videoRunning        NOTIFY  videoRunningChanged)
    Q_PROPERTY(QString          imageFile           READ    imageFile           NOTIFY  imageFileChanged)
41
    Q_PROPERTY(QString          videoFile           READ    videoFile           NOTIFY  videoFileChanged)
42 43 44
    Q_PROPERTY(QString          imagePath           READ    imagePath           NOTIFY  imagePathChanged)
    Q_PROPERTY(QString          videoPath           READ    videoPath           NOTIFY  videoPathChanged)

Gus Grubba's avatar
Gus Grubba committed
45
    Q_PROPERTY(bool             showFullScreen      READ    showFullScreen      WRITE   setShowFullScreen     NOTIFY showFullScreenChanged)
46 47 48 49 50 51
    Q_PROPERTY(bool             streamEnabled       READ    streamEnabled       WRITE   setStreamEnabled      NOTIFY streamEnabledChanged)
    Q_PROPERTY(bool             streamConfigured    READ    streamConfigured    WRITE   setStreamConfigured   NOTIFY streamConfiguredChanged)
    Q_PROPERTY(bool             isTaisync           READ    isTaisync           WRITE   setIsTaysinc          NOTIFY isTaisyncChanged)

    Q_PROPERTY(int              recordingFormatId   READ    recordingFormatId   WRITE   setRecordingFormatId  NOTIFY recordingFormatIdChanged)
    Q_PROPERTY(int              rtspTimeout         READ    rtspTimeout         WRITE   setRtspTimeout        NOTIFY rtspTimeoutChanged)
52

Gus Grubba's avatar
Gus Grubba committed
53
    explicit VideoReceiver(QObject* parent = nullptr);
Gus Grubba's avatar
Gus Grubba committed
54 55
    ~VideoReceiver();

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    bool streamEnabled() const;
    Q_SLOT void setStreamEnabled(bool enabled);
    Q_SIGNAL void streamEnabledChanged();

    bool streamConfigured() const;
    Q_SLOT void setStreamConfigured(bool enabled);
    Q_SIGNAL void streamConfiguredChanged();

    bool isTaisync() const;
    Q_SLOT void setIsTaysinc(bool value);
    Q_SIGNAL void isTaisyncChanged();

    QString videoPath() const;
    Q_SLOT void setVideoPath(const QString& path);
    Q_SIGNAL void videoPathChanged();

    QString imagePath() const;
    Q_SLOT void setImagePath(const QString& path);
    Q_SIGNAL void imagePathChanged();

    int recordingFormatId() const;
    Q_SLOT void setRecordingFormatId(int value);
    Q_SIGNAL void recordingFormatIdChanged();

    int rtspTimeout() const;
    Q_SLOT void setRtspTimeout(int value);
    Q_SIGNAL void rtspTimeoutChanged();

    Q_SIGNAL void restartTimeout();
    Q_SIGNAL void sendMessage(const QString& message);

87 88
    // Emitted before recording starts.
    Q_SIGNAL void beforeRecording();
89
    void setUnittestMode(bool runUnitTests);
90
#if defined(QGC_GST_STREAMING)
91
    virtual bool            recording       () { return _recording; }
92 93
#endif

94 95 96 97
    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
98

99
    virtual void            grabImage       (QString imageFile);
100

101
    virtual void        setShowFullScreen   (bool show) { _showFullScreen = show; emit showFullScreenChanged(); }
102

103 104 105
#if defined(QGC_GST_STREAMING)
    void                  setVideoSink      (GstElement* videoSink);
#endif
106

107
signals:
108 109 110 111
    void videoRunningChanged                ();
    void imageFileChanged                   ();
    void videoFileChanged                   ();
    void showFullScreenChanged              ();
112
#if defined(QGC_GST_STREAMING)
113 114 115 116
    void recordingChanged                   ();
    void msgErrorReceived                   ();
    void msgEOSReceived                     ();
    void msgStateChangedReceived            ();
117
    void gotFirstRecordingKeyFrame          ();
118
#endif
119

120
public slots:
121 122 123 124 125 126 127 128
    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               ();
129
#if defined(QGC_GST_STREAMING)
130
    GstElement*  _makeSource                (const QString& uri);
131
    GstElement*  _makeFileSink              (const QString& videoFile, unsigned format);
132 133 134
    virtual void _handleError               ();
    virtual void _handleEOS                 ();
    virtual void _handleStateChanged        ();
135
#endif
Gus Grubba's avatar
Gus Grubba committed
136

137
protected:
138
#if defined(QGC_GST_STREAMING)
139

140 141
    typedef struct
    {
142 143 144 145
        GstPad*         teepad;
        GstElement*     queue;
        GstElement*     filesink;
        gboolean        removing;
146 147
    } Sink;

148
    bool                _running;
149
    bool                _recording;
150
    bool                _streaming;
151 152
    bool                _starting;
    bool                _stopping;
153
    bool                _stop;
154 155
    Sink*               _sink;
    GstElement*         _tee;
156

157 158
    void _noteVideoSinkFrame                            ();

159 160
    static gboolean             _onBusMessage           (GstBus* bus, GstMessage* message, gpointer user_data);
    static GstPadProbeReturn    _unlinkCallBack         (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
161
    static GstPadProbeReturn    _videoSinkProbe         (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
162
    static GstPadProbeReturn    _keyframeWatch          (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
163

164
    virtual void                _unlinkRecordingBranch  (GstPadProbeInfo* info);
165 166
    virtual void                _shutdownRecordingBranch();
    virtual void                _shutdownPipeline       ();
167

168 169
    GstElement*     _pipeline;
    GstElement*     _videoSink;
170
    guint64         _lastFrameId;
171
    qint64          _lastFrameTime;
Gus Grubba's avatar
Gus Grubba committed
172

173 174
    //-- Wait for Video Server to show up before starting
    QTimer          _frameTimer;
175 176
    QTimer          _restart_timer;
    int             _restart_time_ms;
177

178 179
    //-- RTSP UDP reconnect timeout
    uint64_t        _udpReconnect_us;
180 181
#endif

182 183
    QString         _uri;
    QString         _imageFile;
184
    QString         _videoFile;
185 186 187
    QString         _videoPath;
    QString         _imagePath;

188 189
    bool            _videoRunning;
    bool            _showFullScreen;
190 191 192 193 194 195 196 197
    bool            _streamEnabled;
    bool            _streamConfigured;
    bool            _storageLimit;
    bool            _unittTestMode;
    bool            _isTaisync;
    int            _recordingFormatId; // 0 - 2, defined in VideoReceiver.cc / kVideoExtensions. TODO: use a better representation.
    int            _rtspTimeout;

Gus Grubba's avatar
Gus Grubba committed
198 199
};