VideoReceiver.h 5.4 KB
Newer Older
1 2
/****************************************************************************
 *
3
 *   (c) 2009-2018 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 16


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

17
#pragma once
Gus Grubba's avatar
Gus Grubba committed
18

19
#include "QGCLoggingCategory.h"
Gus Grubba's avatar
Gus Grubba committed
20
#include <QObject>
21 22 23
#include <QTimer>
#include <QTcpSocket>

24 25
#include "VideoSurface.h"

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

30 31
Q_DECLARE_LOGGING_CATEGORY(VideoReceiverLog)

32 33
class VideoSettings;

Gus Grubba's avatar
Gus Grubba committed
34 35 36 37
class VideoReceiver : public QObject
{
    Q_OBJECT
public:
38
#if defined(QGC_GST_STREAMING)
39
    Q_PROPERTY(bool             recording           READ    recording           NOTIFY recordingChanged)
40
#endif
41
    Q_PROPERTY(VideoSurface*    videoSurface        READ    videoSurface        CONSTANT)
Gus Grubba's avatar
Gus Grubba committed
42 43
    Q_PROPERTY(bool             videoRunning        READ    videoRunning        NOTIFY  videoRunningChanged)
    Q_PROPERTY(QString          imageFile           READ    imageFile           NOTIFY  imageFileChanged)
44
    Q_PROPERTY(QString          videoFile           READ    videoFile           NOTIFY  videoFileChanged)
Gus Grubba's avatar
Gus Grubba committed
45
    Q_PROPERTY(bool             showFullScreen      READ    showFullScreen      WRITE   setShowFullScreen     NOTIFY showFullScreenChanged)
46

Gus Grubba's avatar
Gus Grubba committed
47
    explicit VideoReceiver(QObject* parent = nullptr);
Gus Grubba's avatar
Gus Grubba committed
48 49
    ~VideoReceiver();

50
#if defined(QGC_GST_STREAMING)
51 52 53 54 55
    virtual bool            running         () { return _running;   }
    virtual bool            recording       () { return _recording; }
    virtual bool            streaming       () { return _streaming; }
    virtual bool            starting        () { return _starting;  }
    virtual bool            stopping        () { return _stopping;  }
56 57
#endif

58 59 60 61 62
    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
63

64
    virtual void            grabImage       (QString imageFile);
65

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

signals:
69 70 71 72
    void videoRunningChanged                ();
    void imageFileChanged                   ();
    void videoFileChanged                   ();
    void showFullScreenChanged              ();
73
#if defined(QGC_GST_STREAMING)
74 75 76 77
    void recordingChanged                   ();
    void msgErrorReceived                   ();
    void msgEOSReceived                     ();
    void msgStateChangedReceived            ();
78
#endif
79

80
public slots:
81 82 83 84 85 86 87 88
    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               ();
89
#if defined(QGC_GST_STREAMING)
90 91 92 93 94 95
    virtual void _timeout                   ();
    virtual void _connected                 ();
    virtual void _socketError               (QAbstractSocket::SocketError socketError);
    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*     mux;
        GstElement*     filesink;
107
        GstElement*     parse;
108
        gboolean        removing;
109 110
    } Sink;

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

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    _keyframeWatch          (GstPad* pad, GstPadProbeInfo* info, gpointer user_data);
123 124 125 126 127 128

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

130 131 132
    GstElement*     _pipeline;
    GstElement*     _pipelineStopRec;
    GstElement*     _videoSink;
Gus Grubba's avatar
Gus Grubba committed
133

134 135 136 137 138
    //-- Wait for Video Server to show up before starting
    QTimer          _frameTimer;
    QTimer          _timer;
    QTcpSocket*     _socket;
    bool            _serverPresent;
139
    int             _rtspTestInterval_ms;
140

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;
Gus Grubba's avatar
Gus Grubba committed
153 154
};