VideoManager.cc 5.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/


#include <QQmlContext>
#include <QQmlEngine>
#include <QSettings>
14
#include <QUrl>
15
#include <QDir>
16 17

#ifndef QGC_DISABLE_UVC
18
#include <QCameraInfo>
19
#endif
20 21 22 23 24

#include <VideoItem.h>

#include "ScreenToolsController.h"
#include "VideoManager.h"
25 26 27
#include "QGCToolbox.h"
#include "QGCCorePlugin.h"
#include "QGCOptions.h"
28
#include "Settings/SettingsManager.h"
29 30 31 32 33 34

QGC_LOGGING_CATEGORY(VideoManagerLog, "VideoManagerLog")

//-----------------------------------------------------------------------------
VideoManager::VideoManager(QGCApplication* app)
    : QGCTool(app)
35 36
    , _videoSurface(NULL)
    , _videoReceiver(NULL)
37
    , _videoRunning(false)
38
    , _init(false)
39
    , _videoSettings(NULL)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
{
}

//-----------------------------------------------------------------------------
VideoManager::~VideoManager()
{

}

//-----------------------------------------------------------------------------
void
VideoManager::setToolbox(QGCToolbox *toolbox)
{
   QGCTool::setToolbox(toolbox);
   QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
   qmlRegisterUncreatableType<VideoManager>("QGroundControl.VideoManager", 1, 0, "VideoManager", "Reference only");
56 57 58

   _videoSettings = toolbox->settingsManager()->videoSettings();
   QString videoSource = _videoSettings->videoSource()->rawValue().toString();
59
   connect(_videoSettings->videoSource(), &Fact::rawValueChanged, this, &VideoManager::_videoSourceChanged);
60

61
#if defined(QGC_GST_STREAMING)
62 63 64 65 66 67 68 69 70 71 72
#ifndef QGC_DISABLE_UVC
   // If we are using a UVC camera setup the device name
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach (const QCameraInfo &cameraInfo, cameras) {
        if(cameraInfo.description() == videoSource) {
            _videoSourceID = cameraInfo.deviceName();
            emit videoSourceIDChanged();
            qCDebug(VideoManagerLog) << "Found USB source:" << _videoSourceID << " Name:" << videoSource;
            break;
        }
    }
73
#endif
74 75 76 77 78 79 80 81 82 83 84

    emit isGStreamerChanged();
    qCDebug(VideoManagerLog) << "New Video Source:" << videoSource;

    if(_videoReceiver) {
        if(isGStreamer()) {
            _videoReceiver->start();
        } else {
            _videoReceiver->stop();
        }
    }
85
#endif
86

87 88 89 90 91 92
   _init = true;
#if defined(QGC_GST_STREAMING)
   _updateVideo();
   connect(&_frameTimer, &QTimer::timeout, this, &VideoManager::_updateTimer);
   _frameTimer.start(1000);
#endif
93 94
}

95 96 97 98 99 100
void VideoManager::_videoSourceChanged(void)
{
    emit hasVideoChanged();
    emit isGStreamerChanged();
}

101 102 103 104 105 106 107
//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
{
#if defined(QGC_GST_STREAMING)
    return true;
#endif
108 109
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
    return !videoSource.isEmpty() && videoSource != VideoSettings::videoSourceNoVideo;
110 111 112 113 114 115 116
}

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
117 118
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
    return videoSource == VideoSettings::videoSourceUDP || videoSource == VideoSettings::videoSourceRTSP;
119 120 121 122 123
#else
    return false;
#endif
}

124 125 126 127 128 129 130 131 132
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

133
//-----------------------------------------------------------------------------
134
void VideoManager::_updateTimer()
135
{
136
#if defined(QGC_GST_STREAMING)
137 138 139
    if(_videoReceiver && _videoSurface) {
        if(_videoReceiver->stopping() || _videoReceiver->starting()) {
            return;
140
        }
141 142 143 144

        if(_videoReceiver->streaming()) {
            if(!_videoRunning) {
                _videoSurface->setLastFrame(0);
145 146 147
                _videoRunning = true;
                emit videoRunningChanged();
            }
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        } else {
            if(_videoRunning) {
                _videoRunning = false;
                emit videoRunningChanged();
            }
        }

        if(_videoRunning) {
            time_t elapsed = 0;
            time_t lastFrame = _videoSurface->lastFrame();
            if(lastFrame != 0) {
                elapsed = time(0) - _videoSurface->lastFrame();
            }
            if(elapsed > 2 && _videoSurface) {
                _videoReceiver->stop();
            }
        } else {
            if(!_videoReceiver->running()) {
                _videoReceiver->start();
            }
168 169 170
        }
    }
#endif
171 172 173 174 175 176 177 178 179 180 181 182
}

//-----------------------------------------------------------------------------
void VideoManager::_updateVideo()
{
    if(_init) {
        if(_videoReceiver)
            delete _videoReceiver;
        if(_videoSurface)
            delete _videoSurface;
        _videoSurface  = new VideoSurface;
        _videoReceiver = new VideoReceiver(this);
183
#if defined(QGC_GST_STREAMING)
184
        _videoReceiver->setVideoSink(_videoSurface->videoSink());
185 186 187
        QString videoSource = _videoSettings->videoSource()->rawValue().toString();
        if (_videoSettings->videoSource()->rawValue().toString() == VideoSettings::videoSourceUDP)
            _videoReceiver->setUri(QStringLiteral("udp://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
188
        else
189
            _videoReceiver->setUri(_videoSettings->rtspUrl()->rawValue().toString());
190
#endif
191 192 193
        _videoReceiver->start();
    }
}