VideoManager.h 7.93 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 9 10 11 12 13 14 15
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/


#ifndef VideoManager_H
#define VideoManager_H

#include <QObject>
#include <QTimer>
16
#include <QTime>
17
#include <QUrl>
18

19
#include "QGCMAVLink.h"
20 21 22
#include "QGCLoggingCategory.h"
#include "VideoReceiver.h"
#include "QGCToolbox.h"
23
#include "SubtitleWriter.h"
24 25 26

Q_DECLARE_LOGGING_CATEGORY(VideoManagerLog)

27
class VideoSettings;
28
class Vehicle;
29
class Joystick;
30

31 32 33 34 35
class VideoManager : public QGCTool
{
    Q_OBJECT

public:
36
    VideoManager    (QGCApplication* app, QGCToolbox* toolbox);
37
    virtual ~VideoManager   ();
38

39 40 41 42 43 44 45
    Q_PROPERTY(bool             hasVideo                READ    hasVideo                                    NOTIFY hasVideoChanged)
    Q_PROPERTY(bool             isGStreamer             READ    isGStreamer                                 NOTIFY isGStreamerChanged)
    Q_PROPERTY(bool             isTaisync               READ    isTaisync       WRITE   setIsTaisync        NOTIFY isTaisyncChanged)
    Q_PROPERTY(QString          videoSourceID           READ    videoSourceID                               NOTIFY videoSourceIDChanged)
    Q_PROPERTY(bool             uvcEnabled              READ    uvcEnabled                                  CONSTANT)
    Q_PROPERTY(bool             fullScreen              READ    fullScreen      WRITE   setfullScreen       NOTIFY fullScreenChanged)
    Q_PROPERTY(VideoReceiver*   videoReceiver           READ    videoReceiver                               CONSTANT)
46
    Q_PROPERTY(VideoReceiver*   thermalVideoReceiver    READ    thermalVideoReceiver                        CONSTANT)
47
    Q_PROPERTY(double           aspectRatio             READ    aspectRatio                                 NOTIFY aspectRatioChanged)
48 49 50
    Q_PROPERTY(double           thermalAspectRatio      READ    thermalAspectRatio                          NOTIFY aspectRatioChanged)
    Q_PROPERTY(double           hfov                    READ    hfov                                        NOTIFY aspectRatioChanged)
    Q_PROPERTY(double           thermalHfov             READ    thermalHfov                                 NOTIFY aspectRatioChanged)
51
    Q_PROPERTY(bool             autoStreamConfigured    READ    autoStreamConfigured                        NOTIFY autoStreamConfiguredChanged)
52
    Q_PROPERTY(bool             hasThermal              READ    hasThermal                                  NOTIFY decodingChanged)
53
    Q_PROPERTY(QString          imageFile               READ    imageFile                                   NOTIFY imageFileChanged)
54 55 56 57
    Q_PROPERTY(bool             streaming               READ    streaming                                   NOTIFY streamingChanged)
    Q_PROPERTY(bool             decoding                READ    decoding                                    NOTIFY decodingChanged)
    Q_PROPERTY(bool             recording               READ    recording                                   NOTIFY recordingChanged)
    Q_PROPERTY(QSize            videoSize               READ    videoSize                                   NOTIFY videoSizeChanged)
58

59 60 61 62 63 64 65 66 67 68 69
    virtual bool        hasVideo            ();
    virtual bool        isGStreamer         ();
    virtual bool        isTaisync           () { return _isTaisync; }
    virtual bool        fullScreen          () { return _fullScreen; }
    virtual QString     videoSourceID       () { return _videoSourceID; }
    virtual double      aspectRatio         ();
    virtual double      thermalAspectRatio  ();
    virtual double      hfov                ();
    virtual double      thermalHfov         ();
    virtual bool        autoStreamConfigured();
    virtual bool        hasThermal          ();
70 71
    virtual QString     imageFile           ();

72 73 74
    bool streaming(void) {
        return _streaming;
    }
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    bool decoding(void) {
        return _decoding;
    }

    bool recording(void) {
        return _recording;
    }

    QSize videoSize(void) {
        const quint32 size = _videoSize;
        return QSize((size >> 16) & 0xFFFF, size & 0xFFFF);
    }

// FIXME: AV: they should be removed after finishing multiple video stream support
// new arcitecture does not assume direct access to video receiver from QML side, even if it works for now
91 92
    virtual VideoReceiver*  videoReceiver           () { return _videoReceiver[0]; }
    virtual VideoReceiver*  thermalVideoReceiver    () { return _videoReceiver[1]; }
93

94
#if defined(QGC_DISABLE_UVC)
95
    virtual bool        uvcEnabled          () { return false; }
96
#else
97
    virtual bool        uvcEnabled          ();
98 99
#endif

100
    virtual void        setfullScreen       (bool f);
101
    virtual void        setIsTaisync        (bool t) { _isTaisync = t;  emit isTaisyncChanged(); }
102

103
    // Override from QGCTool
104
    virtual void        setToolbox          (QGCToolbox *toolbox);
105

106 107
    Q_INVOKABLE void startVideo     ();
    Q_INVOKABLE void stopVideo      ();
Patrick José Pereira's avatar
Patrick José Pereira committed
108

109 110 111 112
    Q_INVOKABLE void startRecording (const QString& videoFile = QString());
    Q_INVOKABLE void stopRecording  ();

    Q_INVOKABLE void grabImage(const QString& imageFile);
113

114
signals:
115 116 117
    void hasVideoChanged            ();
    void isGStreamerChanged         ();
    void videoSourceIDChanged       ();
118
    void fullScreenChanged          ();
119
    void isAutoStreamChanged        ();
120
    void isTaisyncChanged           ();
121 122
    void aspectRatioChanged         ();
    void autoStreamConfiguredChanged();
123
    void imageFileChanged           ();
124 125 126 127 128
    void streamingChanged           ();
    void decodingChanged            ();
    void recordingChanged           ();
    void recordingStarted           ();
    void videoSizeChanged           ();
129

130
protected slots:
131 132 133
    void _videoSourceChanged        ();
    void _udpPortChanged            ();
    void _rtspUrlChanged            ();
134
    void _tcpUrlChanged             ();
135
    void _lowLatencyModeChanged     ();
Gus Grubba's avatar
Gus Grubba committed
136
    void _updateUVC                 ();
137
    void _setActiveVehicle          (Vehicle* vehicle);
138
    void _aspectRatioChanged        ();
139
    void _connectionLostChanged     (bool connectionLost);
140

141
protected:
142
    friend class FinishVideoInitialization;
143

144
    void _initVideo                 ();
145 146
    bool _updateSettings            (unsigned id);
    bool _updateVideoUri            (unsigned id, const QString& uri);
147
    void _cleanupOldVideos          ();
148 149
    void _restartAllVideos          ();
    void _restartVideo              (unsigned id);
150 151
    void _startReceiver             (unsigned id);
    void _stopReceiver              (unsigned id);
152

153
protected:
154 155 156 157
    QString                 _videoFile;
    QString                 _imageFile;
    SubtitleWriter          _subtitleWriter;
    bool                    _isTaisync              = false;
158 159 160
    VideoReceiver*          _videoReceiver[2]       = { nullptr, nullptr };
    void*                   _videoSink[2]           = { nullptr, nullptr };
    QString                 _videoUri[2];
161 162 163 164 165
    // FIXME: AV: _videoStarted seems to be access from 3 different threads, from time to time
    // 1) Video Receiver thread
    // 2) Video Manager/main app thread
    // 3) Qt rendering thread (during video sink creation process which should happen in this thread)
    // It works for now but...
166
    bool                    _videoStarted[2]        = { false, false };
167
    bool                    _lowLatencyStreaming[2] = { false, false };
168 169 170 171 172 173 174 175
    QAtomicInteger<bool>    _streaming              = false;
    QAtomicInteger<bool>    _decoding               = false;
    QAtomicInteger<bool>    _recording              = false;
    QAtomicInteger<quint32> _videoSize              = 0;
    VideoSettings*          _videoSettings          = nullptr;
    QString                 _videoSourceID;
    bool                    _fullScreen             = false;
    Vehicle*                _activeVehicle          = nullptr;
176 177 178
};

#endif