VideoManager.cc 17.8 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
 *
 * 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

#include "ScreenToolsController.h"
#include "VideoManager.h"
23 24 25
#include "QGCToolbox.h"
#include "QGCCorePlugin.h"
#include "QGCOptions.h"
26
#include "MultiVehicleManager.h"
27
#include "Settings/SettingsManager.h"
28
#include "Vehicle.h"
29
#include "QGCCameraManager.h"
30 31 32 33

QGC_LOGGING_CATEGORY(VideoManagerLog, "VideoManagerLog")

//-----------------------------------------------------------------------------
34 35
VideoManager::VideoManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
36 37 38 39 40 41
{
}

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

//-----------------------------------------------------------------------------
void
VideoManager::setToolbox(QGCToolbox *toolbox)
{
   QGCTool::setToolbox(toolbox);
   QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
56 57
   qmlRegisterUncreatableType<VideoManager> ("QGroundControl.VideoManager", 1, 0, "VideoManager", "Reference only");
   qmlRegisterUncreatableType<VideoReceiver>("QGroundControl",              1, 0, "VideoReceiver","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
   connect(_videoSettings->aspectRatio(),   &Fact::rawValueChanged, this, &VideoManager::_aspectRatioChanged);
65 66
   MultiVehicleManager *pVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
   connect(pVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &VideoManager::_setActiveVehicle);
67

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

    emit isGStreamerChanged();
    qCDebug(VideoManagerLog) << "New Video Source:" << videoSource;
76
    _videoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
77
    _thermalVideoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
78 79
    _updateSettings();
    if(isGStreamer()) {
80
        startVideo();
81
        _subtitleWriter.setVideoReceiver(_videoReceiver);
82
    } else {
83
        stopVideo();
84 85
    }

86
#endif
87 88
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
//-----------------------------------------------------------------------------
void
VideoManager::startVideo()
{
    if(_videoReceiver) _videoReceiver->start();
    if(_thermalVideoReceiver) _thermalVideoReceiver->start();
}

//-----------------------------------------------------------------------------
void
VideoManager::stopVideo()
{
    if(_videoReceiver) _videoReceiver->stop();
    if(_thermalVideoReceiver) _thermalVideoReceiver->stop();
}

105 106 107
//-----------------------------------------------------------------------------
double VideoManager::aspectRatio()
{
108
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
109
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
110 111 112 113 114 115 116 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
        if(pInfo) {
            qCDebug(VideoManagerLog) << "Primary AR: " << pInfo->aspectRatio();
            return pInfo->aspectRatio();
        }
    }
    return _videoSettings->aspectRatio()->rawValue().toDouble();
}

//-----------------------------------------------------------------------------
double VideoManager::thermalAspectRatio()
{
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->thermalStreamInstance();
        if(pInfo) {
            qCDebug(VideoManagerLog) << "Thermal AR: " << pInfo->aspectRatio();
            return pInfo->aspectRatio();
        }
    }
    return 1.0;
}

//-----------------------------------------------------------------------------
double VideoManager::hfov()
{
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
        if(pInfo) {
            return pInfo->hfov();
        }
    }
    return 1.0;
}

//-----------------------------------------------------------------------------
double VideoManager::thermalHfov()
{
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->thermalStreamInstance();
148 149 150 151 152 153 154
        if(pInfo) {
            return pInfo->aspectRatio();
        }
    }
    return _videoSettings->aspectRatio()->rawValue().toDouble();
}

155 156 157 158 159 160 161 162 163 164 165 166 167
//-----------------------------------------------------------------------------
bool
VideoManager::hasThermal()
{
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->thermalStreamInstance();
        if(pInfo) {
            return true;
        }
    }
    return false;
}

168 169 170 171
//-----------------------------------------------------------------------------
bool
VideoManager::autoStreamConfigured()
{
Gus Grubba's avatar
Gus Grubba committed
172
#if defined(QGC_GST_STREAMING)
173
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
174 175 176
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
        if(pInfo) {
            return !pInfo->uri().isEmpty();
177 178
        }
    }
Gus Grubba's avatar
Gus Grubba committed
179
#endif
180
    return false;
181 182
}

Gus Grubba's avatar
Gus Grubba committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
//-----------------------------------------------------------------------------
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
}

201 202 203
//-----------------------------------------------------------------------------
void
VideoManager::_videoSourceChanged()
204
{
Gus Grubba's avatar
Gus Grubba committed
205
    _updateUVC();
206 207
    emit hasVideoChanged();
    emit isGStreamerChanged();
208
    emit isAutoStreamChanged();
209
    restartVideo();
210 211
}

212 213 214
//-----------------------------------------------------------------------------
void
VideoManager::_udpPortChanged()
215
{
216
    restartVideo();
217 218
}

219 220
//-----------------------------------------------------------------------------
void
221
VideoManager::_rtspUrlChanged()
222
{
223
    restartVideo();
224 225
}

226 227 228 229
//-----------------------------------------------------------------------------
void
VideoManager::_tcpUrlChanged()
{
230
    restartVideo();
231 232
}

233 234 235 236
//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
{
237 238 239
    if(autoStreamConfigured()) {
        return true;
    }
240
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
241
    return !videoSource.isEmpty() && videoSource != VideoSettings::videoSourceNoVideo && videoSource != VideoSettings::videoDisabled;
242 243 244 245 246 247 248
}

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
249
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
250
    return
Gus Grubba's avatar
Gus Grubba committed
251 252
        videoSource == VideoSettings::videoSourceUDPH264 ||
        videoSource == VideoSettings::videoSourceUDPH265 ||
253
        videoSource == VideoSettings::videoSourceRTSP ||
254
        videoSource == VideoSettings::videoSourceTCP ||
255 256
        videoSource == VideoSettings::videoSourceMPEGTS ||
        autoStreamConfigured();
257 258 259 260 261
#else
    return false;
#endif
}

262 263 264 265 266 267 268 269 270
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

271 272 273 274 275 276 277 278 279 280 281 282 283 284
//-----------------------------------------------------------------------------
void
VideoManager::setfullScreen(bool f)
{
    if(f) {
        //-- No can do if no vehicle or connection lost
        if(!_activeVehicle || _activeVehicle->connectionLost()) {
            f = false;
        }
    }
    _fullScreen = f;
    emit fullScreenChanged();
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
//-----------------------------------------------------------------------------
#if defined(QGC_GST_STREAMING)
GstElement*
VideoManager::_makeVideoSink(const QString& widgetName)
{
    GstElement* glupload        = nullptr;
    GstElement* glcolorconvert  = nullptr;
    GstElement* qmlglsink       = nullptr;
    GstElement* bin             = nullptr;
    GstElement* sink            = nullptr;

    do {
        QQuickItem* root = qgcApp()->mainRootWindow();

        if (root == nullptr) {
            qCDebug(VideoManagerLog) << "VideoManager::_makeVideoSink() failed. No root window";
            break;
        }

        QQuickItem* widget = root->findChild<QQuickItem*>(widgetName);

        if (widget == nullptr) {
            qCDebug(VideoManagerLog) << "VideoManager::_makeVideoSink() failed. Widget \'" << widgetName << "\' not found";
            break;
        }

        if ((glupload = gst_element_factory_make("glupload", nullptr)) == nullptr) {
            qCritical() << "VideoManager::_makeVideoSink() failed. Error with gst_element_factory_make('glupload')";
            break;
        }

        if ((glcolorconvert = gst_element_factory_make("glcolorconvert", nullptr)) == nullptr) {
            qCritical() << "VideoManager::_makeVideoSink() failed. Error with gst_element_factory_make('glcolorconvert')";
            break;
        }

        if ((qmlglsink = gst_element_factory_make("qmlglsink", nullptr)) == nullptr) {
            qCritical() << "VideoManager::_makeVideoSink() failed. Error with gst_element_factory_make('qmlglsink')";
            break;
        }

        g_object_set(qmlglsink, "widget", widget, NULL);

        if ((bin = gst_bin_new("videosink")) == nullptr) {
            qCritical() << "VideoManager::_makeVideoSink() failed. Error with gst_bin_new('videosink')";
            break;
        }

        GstPad* pad;

        if ((pad = gst_element_get_static_pad(glupload, "sink")) == nullptr) {
            qCritical() << "VideoManager::_makeVideoSink() failed. Error with gst_element_get_static_pad(glupload, 'sink')";
            break;
        }

        gst_bin_add_many(GST_BIN(bin), glupload, glcolorconvert, qmlglsink, nullptr);
        gboolean ret = gst_element_link_many(glupload, glcolorconvert, qmlglsink, nullptr);

        qmlglsink = glcolorconvert = glupload = nullptr;

        if (!ret) {
            qCritical() << "VideoManager::_makeVideoSink() failed. Error with gst_element_link_many()";
            break;
        }

        gst_element_add_pad(bin, gst_ghost_pad_new("sink", pad));
        gst_object_unref(pad);
        pad = nullptr;

        sink = bin;
        bin = nullptr;
    } while(0);

    if (bin != nullptr) {
        gst_object_unref(bin);
        bin = nullptr;
    }

    if (qmlglsink != nullptr) {
        gst_object_unref(qmlglsink);
        qmlglsink = nullptr;
    }

    if (glcolorconvert != nullptr) {
        gst_object_unref(glcolorconvert);
        glcolorconvert = nullptr;
    }

    if (glupload != nullptr) {
        gst_object_unref(glupload);
        glupload = nullptr;
    }

    return sink;
}
#endif

//-----------------------------------------------------------------------------
void
VideoManager::_initVideo()
{
#if defined(QGC_GST_STREAMING)
    _videoReceiver->setVideoSink(_makeVideoSink("videoContent"));
    _thermalVideoReceiver->setVideoSink(_makeVideoSink("thermalVideo"));
#endif
}

392
//-----------------------------------------------------------------------------
393 394
void
VideoManager::_updateSettings()
395 396 397
{
    if(!_videoSettings || !_videoReceiver)
        return;
398
    //-- Auto discovery
399
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
400
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
401
        if(pInfo) {
402
            qCDebug(VideoManagerLog) << "Configure primary stream: " << pInfo->uri();
403 404
            switch(pInfo->type()) {
                case VIDEO_STREAM_TYPE_RTSP:
405 406 407
                    _videoReceiver->setUri(pInfo->uri());
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceRTSP);
                    break;
408 409
                case VIDEO_STREAM_TYPE_TCP_MPEG:
                    _videoReceiver->setUri(pInfo->uri());
410
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceTCP);
411 412 413
                    break;
                case VIDEO_STREAM_TYPE_RTPUDP:
                    _videoReceiver->setUri(QStringLiteral("udp://0.0.0.0:%1").arg(pInfo->uri()));
414
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceUDPH264);
415 416 417
                    break;
                case VIDEO_STREAM_TYPE_MPEG_TS_H264:
                    _videoReceiver->setUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(pInfo->uri()));
418
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceMPEGTS);
419 420 421 422 423
                    break;
                default:
                    _videoReceiver->setUri(pInfo->uri());
                    break;
            }
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
            //-- Thermal stream (if any)
            QGCVideoStreamInfo* pTinfo = _activeVehicle->dynamicCameras()->thermalStreamInstance();
            if(pTinfo) {
                qCDebug(VideoManagerLog) << "Configure secondary stream: " << pTinfo->uri();
                switch(pTinfo->type()) {
                    case VIDEO_STREAM_TYPE_RTSP:
                    case VIDEO_STREAM_TYPE_TCP_MPEG:
                        _thermalVideoReceiver->setUri(pTinfo->uri());
                        break;
                    case VIDEO_STREAM_TYPE_RTPUDP:
                        _thermalVideoReceiver->setUri(QStringLiteral("udp://0.0.0.0:%1").arg(pTinfo->uri()));
                        break;
                    case VIDEO_STREAM_TYPE_MPEG_TS_H264:
                        _thermalVideoReceiver->setUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(pTinfo->uri()));
                        break;
                    default:
                        _thermalVideoReceiver->setUri(pTinfo->uri());
                        break;
                }
            }
444 445 446
            return;
        }
    }
447
    QString source = _videoSettings->videoSource()->rawValue().toString();
Gus Grubba's avatar
Gus Grubba committed
448
    if (source == VideoSettings::videoSourceUDPH264)
449
        _videoReceiver->setUri(QStringLiteral("udp://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
Gus Grubba's avatar
Gus Grubba committed
450 451
    else if (source == VideoSettings::videoSourceUDPH265)
        _videoReceiver->setUri(QStringLiteral("udp265://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
452 453 454
    else if (source == VideoSettings::videoSourceMPEGTS)
        _videoReceiver->setUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
    else if (source == VideoSettings::videoSourceRTSP)
455
        _videoReceiver->setUri(_videoSettings->rtspUrl()->rawValue().toString());
456
    else if (source == VideoSettings::videoSourceTCP)
457
        _videoReceiver->setUri(QStringLiteral("tcp://%1").arg(_videoSettings->tcpUrl()->rawValue().toString()));
458 459
}

460
//-----------------------------------------------------------------------------
461
void
462
VideoManager::restartVideo()
463
{
464
#if defined(QGC_GST_STREAMING)
465
    qCDebug(VideoManagerLog) << "Restart video streaming";
466
    stopVideo();
467
    _updateSettings();
468
    startVideo();
469
    emit aspectRatioChanged();
470 471
#endif
}
472 473 474 475 476 477

//----------------------------------------------------------------------------------------
void
VideoManager::_setActiveVehicle(Vehicle* vehicle)
{
    if(_activeVehicle) {
478
        disconnect(_activeVehicle, &Vehicle::connectionLostChanged, this, &VideoManager::_connectionLostChanged);
479 480 481 482 483
        if(_activeVehicle->dynamicCameras()) {
            QGCCameraControl* pCamera = _activeVehicle->dynamicCameras()->currentCameraInstance();
            if(pCamera) {
                pCamera->stopStream();
            }
484
            disconnect(_activeVehicle->dynamicCameras(), &QGCCameraManager::streamChanged, this, &VideoManager::restartVideo);
485
        }
486 487 488
    }
    _activeVehicle = vehicle;
    if(_activeVehicle) {
489
        connect(_activeVehicle, &Vehicle::connectionLostChanged, this, &VideoManager::_connectionLostChanged);
490
        if(_activeVehicle->dynamicCameras()) {
491
            connect(_activeVehicle->dynamicCameras(), &QGCCameraManager::streamChanged, this, &VideoManager::restartVideo);
492 493 494 495
            QGCCameraControl* pCamera = _activeVehicle->dynamicCameras()->currentCameraInstance();
            if(pCamera) {
                pCamera->resumeStream();
            }
496
        }
497 498 499
    } else {
        //-- Disable full screen video if vehicle is gone
        setfullScreen(false);
500
    }
501
    emit autoStreamConfiguredChanged();
502
    restartVideo();
503 504
}

505 506 507 508 509 510 511 512 513 514
//----------------------------------------------------------------------------------------
void
VideoManager::_connectionLostChanged(bool connectionLost)
{
    if(connectionLost) {
        //-- Disable full screen video if connection is lost
        setfullScreen(false);
    }
}

515 516
//----------------------------------------------------------------------------------------
void
517
VideoManager::_aspectRatioChanged()
518
{
519
    emit aspectRatioChanged();
520
}