VideoReceiver.h 5.39 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)
Gus Grubba's avatar
Gus Grubba committed
42
    Q_PROPERTY(bool             showFullScreen      READ    showFullScreen      WRITE   setShowFullScreen     NOTIFY showFullScreenChanged)
43

Gus Grubba's avatar
Gus Grubba committed
44
    explicit VideoReceiver(QObject* parent = nullptr);
Gus Grubba's avatar
Gus Grubba committed
45 46
    ~VideoReceiver();

47
#if defined(QGC_GST_STREAMING)
48
    virtual bool            recording       () { return _recording; }
49 50
#endif

51 52 53 54
    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
55

56
    virtual void            grabImage       (QString imageFile);
57

58
    virtual void        setShowFullScreen   (bool show) { _showFullScreen = show; emit showFullScreenChanged(); }
59

60 61 62
#if defined(QGC_GST_STREAMING)
    void                  setVideoSink      (GstElement* videoSink);
#endif
63

64
signals:
65 66 67 68
    void videoRunningChanged                ();
    void imageFileChanged                   ();
    void videoFileChanged                   ();
    void showFullScreenChanged              ();
69
#if defined(QGC_GST_STREAMING)
70 71 72 73
    void recordingChanged                   ();
    void msgErrorReceived                   ();
    void msgEOSReceived                     ();
    void msgStateChangedReceived            ();
74
    void gotFirstRecordingKeyFrame          ();
75
#endif
76

77
public slots:
78 79 80 81 82 83 84 85
    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               ();
86
#if defined(QGC_GST_STREAMING)
87
    GstElement*  _makeSource                (const QString& uri);
88
    GstElement*  _makeFileSink              (const QString& videoFile, unsigned format);
89
    virtual void _restart_timeout           ();
90 91 92
    virtual void _tcp_timeout               ();
    virtual void _connected                 ();
    virtual void _socketError               (QAbstractSocket::SocketError socketError);
93 94 95
    virtual void _handleError               ();
    virtual void _handleEOS                 ();
    virtual void _handleStateChanged        ();
96
#endif
Gus Grubba's avatar
Gus Grubba committed
97

98
protected:
99
#if defined(QGC_GST_STREAMING)
100

101 102
    typedef struct
    {
103 104 105 106
        GstPad*         teepad;
        GstElement*     queue;
        GstElement*     filesink;
        gboolean        removing;
107 108
    } Sink;

109
    bool                _running;
110
    bool                _recording;
111
    bool                _streaming;
112 113
    bool                _starting;
    bool                _stopping;
114
    bool                _stop;
115 116
    Sink*               _sink;
    GstElement*         _tee;
117

118 119
    void _noteVideoSinkFrame                            ();

120 121
    static gboolean             _onBusMessage           (GstBus* bus, GstMessage* message, gpointer user_data);
    static GstPadProbeReturn    _unlinkCallBack         (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
122
    static GstPadProbeReturn    _videoSinkProbe         (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
123
    static GstPadProbeReturn    _keyframeWatch          (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
124

125
    virtual void                _unlinkRecordingBranch  (GstPadProbeInfo* info);
126 127 128
    virtual void                _shutdownRecordingBranch();
    virtual void                _shutdownPipeline       ();
    virtual void                _cleanupOldVideos       ();
129

130 131
    GstElement*     _pipeline;
    GstElement*     _videoSink;
132
    guint64         _lastFrameId;
133
    qint64          _lastFrameTime;
Gus Grubba's avatar
Gus Grubba committed
134

135 136
    //-- Wait for Video Server to show up before starting
    QTimer          _frameTimer;
137 138
    QTimer          _restart_timer;
    int             _restart_time_ms;
139 140 141 142
    QTimer          _tcp_timer;
    QTcpSocket*     _socket;
    bool            _serverPresent;
    int             _tcpTestInterval_ms;
143

144 145
    //-- RTSP UDP reconnect timeout
    uint64_t        _udpReconnect_us;
146 147
#endif

148 149
    QString         _uri;
    QString         _imageFile;
150
    QString         _videoFile;
151 152
    bool            _videoRunning;
    bool            _showFullScreen;
153
    VideoSettings*  _videoSettings;
Gus Grubba's avatar
Gus Grubba committed
154 155
};