VideoReceiver.h 5.65 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 24
#include "VideoSurface.h"

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

29 30
Q_DECLARE_LOGGING_CATEGORY(VideoReceiverLog)

31 32
class VideoSettings;

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

44
#if defined(QGC_GST_STREAMING)
45
    Q_PROPERTY(bool             recording           READ    recording           NOTIFY recordingChanged)
46
#endif
47
    Q_PROPERTY(VideoSurface*    videoSurface        READ    videoSurface        CONSTANT)
Gus Grubba's avatar
Gus Grubba committed
48 49
    Q_PROPERTY(bool             videoRunning        READ    videoRunning        NOTIFY  videoRunningChanged)
    Q_PROPERTY(QString          imageFile           READ    imageFile           NOTIFY  imageFileChanged)
50
    Q_PROPERTY(QString          videoFile           READ    videoFile           NOTIFY  videoFileChanged)
Gus Grubba's avatar
Gus Grubba committed
51
    Q_PROPERTY(bool             showFullScreen      READ    showFullScreen      WRITE   setShowFullScreen     NOTIFY showFullScreenChanged)
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
#if defined(QGC_GST_STREAMING)
57
    virtual bool            recording       () { return _recording; }
58 59
#endif

60 61 62 63 64
    virtual VideoSurface*   videoSurface    () { return _videoSurface; }
    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
65

66
    virtual void            grabImage       (QString imageFile);
67

68
    virtual void        setShowFullScreen   (bool show) { _showFullScreen = show; emit showFullScreenChanged(); }
69

70 71
    void                  setVideoDecoder   (VideoEncoding encoding);

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

85
public slots:
86 87 88 89 90 91 92 93
    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               ();
94
#if defined(QGC_GST_STREAMING)
95
    virtual void _restart_timeout           ();
96 97 98
    virtual void _tcp_timeout               ();
    virtual void _connected                 ();
    virtual void _socketError               (QAbstractSocket::SocketError socketError);
99 100 101
    virtual void _handleError               ();
    virtual void _handleEOS                 ();
    virtual void _handleStateChanged        ();
102
#endif
Gus Grubba's avatar
Gus Grubba committed
103

104
protected:
105
#if defined(QGC_GST_STREAMING)
106

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

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

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

    virtual void                _detachRecordingBranch  (GstPadProbeInfo* info);
    virtual void                _shutdownRecordingBranch();
    virtual void                _shutdownPipeline       ();
    virtual void                _cleanupOldVideos       ();
    virtual void                _setVideoSink           (GstElement* sink);
135

136 137 138
    GstElement*     _pipeline;
    GstElement*     _pipelineStopRec;
    GstElement*     _videoSink;
Gus Grubba's avatar
Gus Grubba committed
139

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

149 150
    //-- RTSP UDP reconnect timeout
    uint64_t        _udpReconnect_us;
151 152
#endif

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