VideoManager.cc 7.92 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 15

#ifndef QGC_DISABLE_UVC
16
#include <QCameraInfo>
17
#endif
18 19 20 21 22

#include <VideoItem.h>

#include "ScreenToolsController.h"
#include "VideoManager.h"
23 24 25
#include "QGCToolbox.h"
#include "QGCCorePlugin.h"
#include "QGCOptions.h"
26 27

static const char* kVideoSourceKey  = "VideoSource";
28 29
static const char* kVideoUDPPortKey = "VideoUDPPort";
static const char* kVideoRTSPUrlKey = "VideoRTSPUrl";
30
#if defined(QGC_GST_STREAMING)
31
static const char* kUDPStream       = "UDP Video Stream";
32
static const char* kRTSPStream      = "RTSP Video Stream";
33 34
#endif
static const char* kNoVideo         = "No Video Available";
35 36 37 38 39 40

QGC_LOGGING_CATEGORY(VideoManagerLog, "VideoManagerLog")

//-----------------------------------------------------------------------------
VideoManager::VideoManager(QGCApplication* app)
    : QGCTool(app)
41 42
    , _videoSurface(NULL)
    , _videoReceiver(NULL)
43
    , _videoRunning(false)
44 45
    , _udpPort(5600) //-- Defalut Port 5600 == Solo UDP Port
    , _init(false)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
}

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

}

//-----------------------------------------------------------------------------
void
VideoManager::setToolbox(QGCToolbox *toolbox)
{
   QGCTool::setToolbox(toolbox);
   QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
   qmlRegisterUncreatableType<VideoManager>("QGroundControl.VideoManager", 1, 0, "VideoManager", "Reference only");
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
   //-- Get saved settings
#if defined(QGC_GST_STREAMING)
   QSettings settings;
#if defined(NO_UDP_VIDEO)
   setVideoSource(settings.value(kVideoSourceKey, kRTSPStream).toString());
#else
   setVideoSource(settings.value(kVideoSourceKey, kUDPStream).toString());
#endif
   //-- Check if core plugin defines its own video requirements
   if(qgcApp()->toolbox()->corePlugin()->options()->definesVideo()) {
       if(qgcApp()->toolbox()->corePlugin()->options()->videoUDPPort()) {
           setUdpPort(qgcApp()->toolbox()->corePlugin()->options()->videoUDPPort());
           setVideoSource(kUDPStream);
       } else {
           setVideoSource(kRTSPStream);
           setRtspURL(qgcApp()->toolbox()->corePlugin()->options()->videoRSTPUrl());
       }
   } else {
       setUdpPort(settings.value(kVideoUDPPortKey, 5600).toUInt());
       setRtspURL(settings.value(kVideoRTSPUrlKey, "rtsp://192.168.42.1:554/live").toString()); //-- Example RTSP URL
   }
#endif
   _init = true;
#if defined(QGC_GST_STREAMING)
   _updateVideo();
   connect(&_frameTimer, &QTimer::timeout, this, &VideoManager::_updateTimer);
   _frameTimer.start(1000);
#endif
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
{
#if defined(QGC_GST_STREAMING)
    return true;
#endif
    return !_videoSource.isEmpty();
}

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
107
    return _videoSource == kUDPStream || _videoSource == kRTSPStream;
108 109 110 111 112
#else
    return false;
#endif
}

113 114 115 116 117 118 119 120 121
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

122 123 124 125
//-----------------------------------------------------------------------------
void
VideoManager::setVideoSource(QString vSource)
{
126 127
    if(vSource == kNoVideo)
        return;
128 129 130 131
    _videoSource = vSource;
    QSettings settings;
    settings.setValue(kVideoSourceKey, vSource);
    emit videoSourceChanged();
132
#ifndef QGC_DISABLE_UVC
133 134 135 136 137
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach (const QCameraInfo &cameraInfo, cameras) {
        if(cameraInfo.description() == vSource) {
            _videoSourceID = cameraInfo.deviceName();
            emit videoSourceIDChanged();
138
            qCDebug(VideoManagerLog) << "Found USB source:" << _videoSourceID << " Name:" << _videoSource;
139 140 141
            break;
        }
    }
142
#endif
143 144
    emit isGStreamerChanged();
    qCDebug(VideoManagerLog) << "New Video Source:" << vSource;
145
    /*
146
     * Not working. Requires restart for now. (Undef KRTSP/kUDP above when enabling this)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    if(isGStreamer())
        _updateVideo();
    */
    if(_videoReceiver) {
        if(isGStreamer()) {
            _videoReceiver->start();
        } else {
            _videoReceiver->stop();
        }
    }
}

//-----------------------------------------------------------------------------
void
VideoManager::setUdpPort(quint16 port)
{
    _udpPort = port;
    QSettings settings;
    settings.setValue(kVideoUDPPortKey, port);
    emit udpPortChanged();
    /*
168
     * Not working. Requires restart for now. (Undef KRTSP/kUDP above when enabling this)
169 170 171 172 173 174 175 176 177 178 179 180 181 182
    if(_videoSource == kUDPStream)
        _updateVideo();
    */
}

//-----------------------------------------------------------------------------
void
VideoManager::setRtspURL(QString url)
{
    _rtspURL = url;
    QSettings settings;
    settings.setValue(kVideoRTSPUrlKey, url);
    emit rtspURLChanged();
    /*
183
     * Not working. Requires restart for now. (Undef KRTSP/kUDP above when enabling this)
184 185 186
    if(_videoSource == kRTSPStream)
        _updateVideo();
    */
187 188 189 190 191 192 193 194
}

//-----------------------------------------------------------------------------
QStringList
VideoManager::videoSourceList()
{
    _videoSourceList.clear();
#if defined(QGC_GST_STREAMING)
195 196
    _videoSourceList.append(kUDPStream);
    _videoSourceList.append(kRTSPStream);
197
#endif
198
#ifndef QGC_DISABLE_UVC
199 200 201 202 203
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach (const QCameraInfo &cameraInfo, cameras) {
        qCDebug(VideoManagerLog) << "UVC Video source ID:" << cameraInfo.deviceName() << " Name:" << cameraInfo.description();
        _videoSourceList.append(cameraInfo.description());
    }
204
#endif
205 206
    if(_videoSourceList.count() == 0)
        _videoSourceList.append(kNoVideo);
207 208 209 210
    return _videoSourceList;
}

//-----------------------------------------------------------------------------
211
void VideoManager::_updateTimer()
212
{
213
#if defined(QGC_GST_STREAMING)
214 215 216 217 218 219 220
    if(_videoRunning)
    {
        time_t elapsed = 0;
        if(_videoSurface)
        {
            elapsed = time(0) - _videoSurface->lastFrame();
        }
221
        if(elapsed > 2 && _videoSurface)
222 223 224 225
        {
            _videoRunning = false;
            _videoSurface->setLastFrame(0);
            emit videoRunningChanged();
226 227 228 229 230 231 232 233 234
            if(_videoReceiver) {
                if(isGStreamer()) {
                    //-- Stop it
                    _videoReceiver->stop();
                    QThread::msleep(100);
                    //-- And start over
                    _videoReceiver->start();
                }
            }
235 236 237 238 239 240 241 242 243 244 245 246 247
        }
    }
    else
    {
        if(_videoSurface && _videoSurface->lastFrame()) {
            if(!_videoRunning)
            {
                _videoRunning = true;
                emit videoRunningChanged();
            }
        }
    }
#endif
248 249 250 251 252 253 254 255 256 257 258 259 260
}

//-----------------------------------------------------------------------------
void VideoManager::_updateVideo()
{
    if(_init) {
        if(_videoReceiver)
            delete _videoReceiver;
        if(_videoSurface)
            delete _videoSurface;
        _videoSurface  = new VideoSurface;
        _videoReceiver = new VideoReceiver(this);
        #if defined(QGC_GST_STREAMING)
261
        _videoReceiver->setVideoSink(_videoSurface->videoSink());
262 263 264 265 266 267 268 269
        if(_videoSource == kUDPStream)
            _videoReceiver->setUri(QStringLiteral("udp://0.0.0.0:%1").arg(_udpPort));
        else
            _videoReceiver->setUri(_rtspURL);
        #endif
        _videoReceiver->start();
    }
}