VideoReceiver.h 5.46 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
    virtual void _restart_timeout           ();
89 90 91
    virtual void _tcp_timeout               ();
    virtual void _connected                 ();
    virtual void _socketError               (QAbstractSocket::SocketError socketError);
92 93 94
    virtual void _handleError               ();
    virtual void _handleEOS                 ();
    virtual void _handleStateChanged        ();
95
#endif
Gus Grubba's avatar
Gus Grubba committed
96

97
protected:
98
#if defined(QGC_GST_STREAMING)
99

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

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

119 120
    void _noteVideoSinkFrame                            ();

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

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

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

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

146 147
    //-- RTSP UDP reconnect timeout
    uint64_t        _udpReconnect_us;
148 149
#endif

150 151
    QString         _uri;
    QString         _imageFile;
152
    QString         _videoFile;
153 154
    bool            _videoRunning;
    bool            _showFullScreen;
155
    VideoSettings*  _videoSettings;
156 157
    const char*     _depayName;
    const char*     _parserName;
Gus Grubba's avatar
Gus Grubba committed
158 159
};