VideoManager.cc 6.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/****************************************************************************
 *
 *   (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>
#include <QCameraInfo>

#include <VideoItem.h>

#include "ScreenToolsController.h"
#include "VideoManager.h"

static const char* kVideoSourceKey  = "VideoSource";
22 23 24 25
static const char* kVideoUDPPortKey = "VideoUDPPort";
static const char* kVideoRTSPUrlKey = "VideoRTSPUrl";
static const char* kUDPStream       = "UDP Video Stream";
static const char* kRTSPStream      = "RTSP Video Stream";
26 27 28 29 30 31

QGC_LOGGING_CATEGORY(VideoManagerLog, "VideoManagerLog")

//-----------------------------------------------------------------------------
VideoManager::VideoManager(QGCApplication* app)
    : QGCTool(app)
32 33
    , _videoSurface(NULL)
    , _videoReceiver(NULL)
34
    , _videoRunning(false)
35 36
    , _udpPort(5600) //-- Defalut Port 5600 == Solo UDP Port
    , _init(false)
37
{
38 39 40 41 42 43
    //-- Get saved settings
    QSettings settings;
    setVideoSource(settings.value(kVideoSourceKey, kUDPStream).toString());
    setUdpPort(settings.value(kVideoUDPPortKey, 5600).toUInt());
    setRtspURL(settings.value(kVideoRTSPUrlKey, "rtsp://192.168.42.1:554/live").toString()); //-- Example RTSP URL
    _init = true;
44
#if defined(QGC_GST_STREAMING)
45
    _updateVideo();
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    connect(&_frameTimer, &QTimer::timeout, this, &VideoManager::_updateTimer);
    _frameTimer.start(1000);
#endif
}

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

}

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

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

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
81
    return _videoSource == kUDPStream || _videoSource == kRTSPStream;
82 83 84 85 86
#else
    return false;
#endif
}

87 88 89 90 91 92 93 94 95
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

96 97 98 99 100 101 102 103
//-----------------------------------------------------------------------------
void
VideoManager::setVideoSource(QString vSource)
{
    _videoSource = vSource;
    QSettings settings;
    settings.setValue(kVideoSourceKey, vSource);
    emit videoSourceChanged();
104
#ifndef QGC_DISABLE_UVC
105 106 107 108 109
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach (const QCameraInfo &cameraInfo, cameras) {
        if(cameraInfo.description() == vSource) {
            _videoSourceID = cameraInfo.deviceName();
            emit videoSourceIDChanged();
110
            qCDebug(VideoManagerLog) << "Found USB source:" << _videoSourceID << " Name:" << _videoSource;
111 112 113
            break;
        }
    }
114
#endif
115 116
    emit isGStreamerChanged();
    qCDebug(VideoManagerLog) << "New Video Source:" << vSource;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    /*
     * Not working. Requires restart for now
    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();
    /*
     * Not working. Requires restart for now
    if(_videoSource == kUDPStream)
        _updateVideo();
    */
}

//-----------------------------------------------------------------------------
void
VideoManager::setRtspURL(QString url)
{
    _rtspURL = url;
    QSettings settings;
    settings.setValue(kVideoRTSPUrlKey, url);
    emit rtspURLChanged();
    /*
     * Not working. Requires restart for now
    if(_videoSource == kRTSPStream)
        _updateVideo();
    */
159 160 161 162 163 164 165 166
}

//-----------------------------------------------------------------------------
QStringList
VideoManager::videoSourceList()
{
    _videoSourceList.clear();
#if defined(QGC_GST_STREAMING)
167 168
    _videoSourceList.append(kUDPStream);
    _videoSourceList.append(kRTSPStream);
169
#endif
170
#ifndef QGC_DISABLE_UVC
171 172 173 174 175
    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());
    }
176
#endif
177 178 179 180
    return _videoSourceList;
}

//-----------------------------------------------------------------------------
181
void VideoManager::_updateTimer()
182
{
183
#if defined(QGC_GST_STREAMING)
184 185 186 187 188 189 190
    if(_videoRunning)
    {
        time_t elapsed = 0;
        if(_videoSurface)
        {
            elapsed = time(0) - _videoSurface->lastFrame();
        }
191
        if(elapsed > 2 && _videoSurface)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        {
            _videoRunning = false;
            _videoSurface->setLastFrame(0);
            emit videoRunningChanged();
        }
    }
    else
    {
        if(_videoSurface && _videoSurface->lastFrame()) {
            if(!_videoRunning)
            {
                _videoRunning = true;
                emit videoRunningChanged();
            }
        }
    }
#endif
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

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