VideoManager.cc 5.99 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

QGC_LOGGING_CATEGORY(VideoManagerLog, "VideoManagerLog")

//-----------------------------------------------------------------------------
33 34
VideoManager::VideoManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
Gus Grubba's avatar
Gus Grubba committed
35 36
    , _videoReceiver(nullptr)
    , _videoSettings(nullptr)
37
    , _fullScreen(false)
38 39 40 41 42 43
{
}

//-----------------------------------------------------------------------------
VideoManager::~VideoManager()
{
44 45 46
    if(_videoReceiver) {
        delete _videoReceiver;
    }
47 48 49 50 51 52 53 54
}

//-----------------------------------------------------------------------------
void
VideoManager::setToolbox(QGCToolbox *toolbox)
{
   QGCTool::setToolbox(toolbox);
   QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
55 56 57
   qmlRegisterUncreatableType<VideoManager> ("QGroundControl.VideoManager", 1, 0, "VideoManager", "Reference only");
   qmlRegisterUncreatableType<VideoReceiver>("QGroundControl",              1, 0, "VideoReceiver","Reference only");
   qmlRegisterUncreatableType<VideoSurface> ("QGroundControl",              1, 0, "VideoSurface", "Reference only");
58 59
   _videoSettings = toolbox->settingsManager()->videoSettings();
   QString videoSource = _videoSettings->videoSource()->rawValue().toString();
60 61 62
   connect(_videoSettings->videoSource(),   &Fact::rawValueChanged, this, &VideoManager::_videoSourceChanged);
   connect(_videoSettings->udpPort(),       &Fact::rawValueChanged, this, &VideoManager::_udpPortChanged);
   connect(_videoSettings->rtspUrl(),       &Fact::rawValueChanged, this, &VideoManager::_rtspUrlChanged);
63
   connect(_videoSettings->tcpUrl(),        &Fact::rawValueChanged, this, &VideoManager::_tcpUrlChanged);
64

65
#if defined(QGC_GST_STREAMING)
66 67
#ifndef QGC_DISABLE_UVC
   // If we are using a UVC camera setup the device name
Gus Grubba's avatar
Gus Grubba committed
68
   _updateUVC();
69
#endif
70 71 72

    emit isGStreamerChanged();
    qCDebug(VideoManagerLog) << "New Video Source:" << videoSource;
73
    _videoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
74 75 76 77 78
    _updateSettings();
    if(isGStreamer()) {
        _videoReceiver->start();
    } else {
        _videoReceiver->stop();
79 80
    }

81
#endif
82 83
}

Gus Grubba's avatar
Gus Grubba committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
//-----------------------------------------------------------------------------
void
VideoManager::_updateUVC()
{
#ifndef QGC_DISABLE_UVC
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    for (const QCameraInfo &cameraInfo: cameras) {
        if(cameraInfo.description() == videoSource) {
            _videoSourceID = cameraInfo.deviceName();
            emit videoSourceIDChanged();
            qCDebug(VideoManagerLog) << "Found USB source:" << _videoSourceID << " Name:" << videoSource;
            break;
        }
    }
#endif
}

102 103 104
//-----------------------------------------------------------------------------
void
VideoManager::_videoSourceChanged()
105
{
Gus Grubba's avatar
Gus Grubba committed
106
    _updateUVC();
107 108
    emit hasVideoChanged();
    emit isGStreamerChanged();
109 110 111
    _restartVideo();
}

112 113 114
//-----------------------------------------------------------------------------
void
VideoManager::_udpPortChanged()
115 116
{
    _restartVideo();
117 118
}

119 120
//-----------------------------------------------------------------------------
void
121
VideoManager::_rtspUrlChanged()
122
{
123
    _restartVideo();
124 125
}

126 127 128 129 130 131 132
//-----------------------------------------------------------------------------
void
VideoManager::_tcpUrlChanged()
{
    _restartVideo();
}

133 134 135 136
//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
{
137
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
138
    return !videoSource.isEmpty() && videoSource != VideoSettings::videoSourceNoVideo && videoSource != VideoSettings::videoDisabled;
139 140 141 142 143 144 145
}

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
146
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
147
    return videoSource == VideoSettings::videoSourceUDP || videoSource == VideoSettings::videoSourceRTSP || videoSource == VideoSettings::videoSourceTCP;
148 149 150 151 152
#else
    return false;
#endif
}

153 154 155 156 157 158 159 160 161
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

162
//-----------------------------------------------------------------------------
163 164
void
VideoManager::_updateSettings()
165 166 167 168 169
{
    if(!_videoSettings || !_videoReceiver)
        return;
    if (_videoSettings->videoSource()->rawValue().toString() == VideoSettings::videoSourceUDP)
        _videoReceiver->setUri(QStringLiteral("udp://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
170
    else if (_videoSettings->videoSource()->rawValue().toString() == VideoSettings::videoSourceRTSP)
171
        _videoReceiver->setUri(_videoSettings->rtspUrl()->rawValue().toString());
172 173
    else if (_videoSettings->videoSource()->rawValue().toString() == VideoSettings::videoSourceTCP)
        _videoReceiver->setUri(QStringLiteral("tcp://%1").arg(_videoSettings->tcpUrl()->rawValue().toString()));
174 175
}

176
//-----------------------------------------------------------------------------
177 178
void
VideoManager::_restartVideo()
179
{
180
#if defined(QGC_GST_STREAMING)
181 182 183 184 185 186 187
    if(!_videoReceiver)
        return;
    _videoReceiver->stop();
    _updateSettings();
    _videoReceiver->start();
#endif
}