VideoReceiver.h 5.34 KB
Newer Older
1 2
/****************************************************************************
 *
3
 *   (c) 2009-2019 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 13 14 15

/**
 * @file
 *   @brief QGC Video Receiver
 *   @author Gus Grubba <mavlink@grubba.com>
 */

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 _handleError               ();
    virtual void _handleEOS                 ();
    virtual void _handleStateChanged        ();
99
#endif
Gus Grubba's avatar
Gus Grubba committed
100

101
protected:
102
#if defined(QGC_GST_STREAMING)
103

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

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

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

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

133 134 135
    GstElement*     _pipeline;
    GstElement*     _pipelineStopRec;
    GstElement*     _videoSink;
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
    //-- RTSP UDP reconnect timeout
    uint64_t        _udpReconnect_us;
144 145
#endif

146 147
    QString         _uri;
    QString         _imageFile;
148
    QString         _videoFile;
149 150 151
    VideoSurface*   _videoSurface;
    bool            _videoRunning;
    bool            _showFullScreen;
152
    VideoSettings*  _videoSettings;
153 154
    const char*     _depayName;
    const char*     _parserName;
155 156 157
    bool            _tryWithHardwareDecoding = true;
    const char*     _hwDecoderName;
    const char*     _swDecoderName;
Gus Grubba's avatar
Gus Grubba committed
158 159
};