VideoManager.cc 25.3 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 34 35 36
#if defined(QGC_GST_STREAMING)
#include "GStreamer.h"
#else
#include "GLVideoItemStub.h"
#endif

Andrew Voznytsa's avatar
Andrew Voznytsa committed
37 38 39 40
#ifdef QGC_GST_TAISYNC_ENABLED
#include "TaisyncHandler.h"
#endif

41 42
QGC_LOGGING_CATEGORY(VideoManagerLog, "VideoManagerLog")

43
#if defined(QGC_GST_STREAMING)
44 45 46 47 48
static const char* kFileExtension[VideoReceiver::FILE_FORMAT_MAX - VideoReceiver::FILE_FORMAT_MIN] = {
    "mkv",
    "mov",
    "mp4"
};
49
#endif
50

51
//-----------------------------------------------------------------------------
52 53
VideoManager::VideoManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
54
{
55 56 57 58 59 60 61
#if !defined(QGC_GST_STREAMING)
    static bool once = false;
    if (!once) {
        qmlRegisterType<GLVideoItemStub>("org.freedesktop.gstreamer.GLVideoItem", 1, 0, "GstGLVideoItem");
        once = true;
    }
#endif
62 63 64 65 66
}

//-----------------------------------------------------------------------------
VideoManager::~VideoManager()
{
67 68 69 70
    delete _videoReceiver;
    _videoReceiver = nullptr;
    delete _thermalVideoReceiver;
    _thermalVideoReceiver = nullptr;
71
#if defined(QGC_GST_STREAMING)
72 73 74 75
    GStreamer::releaseVideoSink(_thermalVideoSink);
    _thermalVideoSink = nullptr;
    GStreamer::releaseVideoSink(_videoSink);
    _videoSink = nullptr;
76
#endif
77 78 79 80 81 82 83 84
}

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

   // TODO: Those connections should be Per Video, not per VideoManager.
89 90
   _videoSettings = toolbox->settingsManager()->videoSettings();
   QString videoSource = _videoSettings->videoSource()->rawValue().toString();
91 92 93
   connect(_videoSettings->videoSource(),   &Fact::rawValueChanged, this, &VideoManager::_videoSourceChanged);
   connect(_videoSettings->udpPort(),       &Fact::rawValueChanged, this, &VideoManager::_udpPortChanged);
   connect(_videoSettings->rtspUrl(),       &Fact::rawValueChanged, this, &VideoManager::_rtspUrlChanged);
94
   connect(_videoSettings->tcpUrl(),        &Fact::rawValueChanged, this, &VideoManager::_tcpUrlChanged);
95
   connect(_videoSettings->aspectRatio(),   &Fact::rawValueChanged, this, &VideoManager::_aspectRatioChanged);
96
   connect(_videoSettings->lowLatencyMode(),&Fact::rawValueChanged, this, &VideoManager::_lowLatencyModeChanged);
97 98
   MultiVehicleManager *pVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
   connect(pVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &VideoManager::_setActiveVehicle);
99

100
#if defined(QGC_GST_STREAMING)
101 102
#ifndef QGC_DISABLE_UVC
   // If we are using a UVC camera setup the device name
Gus Grubba's avatar
Gus Grubba committed
103
   _updateUVC();
104
#endif
105 106 107

    emit isGStreamerChanged();
    qCDebug(VideoManagerLog) << "New Video Source:" << videoSource;
108
#if defined(QGC_GST_STREAMING)
109
    _videoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
110
    _thermalVideoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
111

112 113 114 115
    connect(_videoReceiver, &VideoReceiver::timeout, this, &VideoManager::_restartVideo);
    connect(_videoReceiver, &VideoReceiver::streamingChanged, this, &VideoManager::_streamingChanged);
    connect(_videoReceiver, &VideoReceiver::recordingStarted, this,  &VideoManager::_recordingStarted);
    connect(_videoReceiver, &VideoReceiver::recordingChanged, this,  &VideoManager::_recordingChanged);
116
    connect(_videoReceiver, &VideoReceiver::onTakeScreenshotComplete, this,  &VideoManager::_onTakeScreenshotComplete);
117

118 119
    // FIXME: AV: I believe _thermalVideoReceiver should be handled just like _videoReceiver in terms of event
    // and I expect that it will be changed during multiple video stream activity
120 121 122 123
    if (_thermalVideoReceiver != nullptr) {
        connect(_thermalVideoReceiver, &VideoReceiver::timeout, this, &VideoManager::_restartVideo);
        connect(_thermalVideoReceiver, &VideoReceiver::streamingChanged, this, &VideoManager::_streamingChanged);
    }
124
#endif
125 126
    _updateSettings();
    if(isGStreamer()) {
127
        startVideo();
128
    } else {
129
        stopVideo();
130 131
    }

132
#endif
133 134
}

135
void VideoManager::_cleanupOldVideos()
136 137 138 139 140 141 142 143 144 145 146 147
{
#if defined(QGC_GST_STREAMING)
    //-- Only perform cleanup if storage limit is enabled
    if(!_videoSettings->enableStorageLimit()->rawValue().toBool()) {
        return;
    }
    QString savePath = qgcApp()->toolbox()->settingsManager()->appSettings()->videoSavePath();
    QDir videoDir = QDir(savePath);
    videoDir.setFilter(QDir::Files | QDir::Readable | QDir::NoSymLinks | QDir::Writable);
    videoDir.setSorting(QDir::Time);

    QStringList nameFilters;
148 149 150

    for(size_t i = 0; i < sizeof(kFileExtension) / sizeof(kFileExtension[0]); i += 1) {
        nameFilters << QString("*.") + kFileExtension[i];
151
    }
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166
    videoDir.setNameFilters(nameFilters);
    //-- get the list of videos stored
    QFileInfoList vidList = videoDir.entryInfoList();
    if(!vidList.isEmpty()) {
        uint64_t total   = 0;
        //-- Settings are stored using MB
        uint64_t maxSize = _videoSettings->maxVideoSize()->rawValue().toUInt() * 1024 * 1024;
        //-- Compute total used storage
        for(int i = 0; i < vidList.size(); i++) {
            total += vidList[i].size();
        }
        //-- Remove old movies until max size is satisfied.
        while(total >= maxSize && !vidList.isEmpty()) {
            total -= vidList.last().size();
167
            qCDebug(VideoManagerLog) << "Removing old video file:" << vidList.last().filePath();
168 169 170 171 172 173 174 175
            QFile file (vidList.last().filePath());
            file.remove();
            vidList.removeLast();
        }
    }
#endif
}

176 177 178 179
//-----------------------------------------------------------------------------
void
VideoManager::startVideo()
{
180 181 182 183 184
    if (qgcApp()->runningUnitTests()) {
        return;
    }

    if(!_videoSettings->streamEnabled()->rawValue().toBool() || !_videoSettings->streamConfigured()) {
185
        qCDebug(VideoManagerLog) << "Stream not enabled/configured";
186 187 188 189 190 191 192
        return;
    }

#if defined(QGC_GST_STREAMING)
    const unsigned timeout = _videoSettings->rtspTimeout()->rawValue().toUInt();

    if(_videoReceiver != nullptr) {
193
        connect(_videoReceiver, &VideoReceiver::onStartComplete, this, &VideoManager::_onStartComplete);
194 195 196 197 198 199 200 201 202 203 204 205 206
        _videoReceiver->start(_videoUri, timeout);
        if (_videoSink != nullptr) {
            _videoReceiver->startDecoding(_videoSink);
        }
    }

    if(_thermalVideoReceiver != nullptr) {
        _thermalVideoReceiver->start(_thermalVideoUri, timeout);
        if (_thermalVideoSink != nullptr) {
            _thermalVideoReceiver->startDecoding(_thermalVideoSink);
        }
    }
#endif
207 208 209 210 211 212
}

//-----------------------------------------------------------------------------
void
VideoManager::stopVideo()
{
213 214 215
    if (qgcApp()->runningUnitTests()) {
        return;
    }
216 217 218 219

    disconnect(_videoReceiver, &VideoReceiver::timeout, this, &VideoManager::_restartVideo);
    disconnect(_videoReceiver, &VideoReceiver::streamingChanged, this, &VideoManager::_streamingChanged);

220
#if defined(QGC_GST_STREAMING)
221 222
    if(_videoReceiver) _videoReceiver->stop();
    if(_thermalVideoReceiver) _thermalVideoReceiver->stop();
223
#endif
224 225
}

226 227 228 229 230 231
void
VideoManager::startRecording(const QString& videoFile)
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }
232
#if defined(QGC_GST_STREAMING)
233
    if (!_videoReceiver) {
234
        qgcApp()->showAppMessage(tr("Video receiver is not ready."));
235 236 237 238 239 240
        return;
    }

    const VideoReceiver::FILE_FORMAT fileFormat = static_cast<VideoReceiver::FILE_FORMAT>(_videoSettings->recordingFormat()->rawValue().toInt());

    if(fileFormat < VideoReceiver::FILE_FORMAT_MIN || fileFormat >= VideoReceiver::FILE_FORMAT_MAX) {
241
        qgcApp()->showAppMessage(tr("Invalid video format defined."));
242 243 244 245 246 247 248 249 250
        return;
    }

    //-- Disk usage maintenance
    _cleanupOldVideos();

    QString savePath = qgcApp()->toolbox()->settingsManager()->appSettings()->videoSavePath();

    if(savePath.isEmpty()) {
251
        qgcApp()->showAppMessage(tr("Unabled to record video. Video save path must be specified in Settings."));
252 253 254 255 256 257 258 259
        return;
    }

    _videoFile = savePath + "/"
            + (videoFile.isEmpty() ? QDateTime::currentDateTime().toString("yyyy-MM-dd_hh.mm.ss") : videoFile)
            + "." + kFileExtension[fileFormat - VideoReceiver::FILE_FORMAT_MIN];

    _videoReceiver->startRecording(_videoFile, fileFormat);
260 261 262
#else
    Q_UNUSED(videoFile)
#endif
263 264 265 266 267 268 269 270
}

void
VideoManager::stopRecording()
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }
271
#if defined(QGC_GST_STREAMING)
272 273 274 275 276
    if (!_videoReceiver) {
        return;
    }

    _videoReceiver->stopRecording();
277
#endif
278 279 280 281 282 283 284 285
}

void
VideoManager::grabImage(const QString& imageFile)
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }
286
#if defined(QGC_GST_STREAMING)
287 288 289 290 291 292 293 294 295
    if (!_videoReceiver) {
        return;
    }

    _imageFile = imageFile;

    emit imageFileChanged();

    _videoReceiver->takeScreenshot(_imageFile);
296 297 298
#else
    Q_UNUSED(imageFile)
#endif
299 300
}

301 302 303
//-----------------------------------------------------------------------------
double VideoManager::aspectRatio()
{
304
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
305
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
306 307 308 309 310
        if(pInfo) {
            qCDebug(VideoManagerLog) << "Primary AR: " << pInfo->aspectRatio();
            return pInfo->aspectRatio();
        }
    }
311
    // FIXME: AV: use _videoReceiver->videoSize() to calculate AR (if AR is not specified in the settings?)
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
    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();
345 346 347 348 349 350 351
        if(pInfo) {
            return pInfo->aspectRatio();
        }
    }
    return _videoSettings->aspectRatio()->rawValue().toDouble();
}

352 353 354 355 356 357 358 359 360 361 362 363 364
//-----------------------------------------------------------------------------
bool
VideoManager::hasThermal()
{
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->thermalStreamInstance();
        if(pInfo) {
            return true;
        }
    }
    return false;
}

365 366 367 368 369 370 371
//-----------------------------------------------------------------------------
QString
VideoManager::imageFile()
{
    return _imageFile;
}

372 373 374 375
//-----------------------------------------------------------------------------
bool
VideoManager::autoStreamConfigured()
{
Gus Grubba's avatar
Gus Grubba committed
376
#if defined(QGC_GST_STREAMING)
377
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
378 379 380
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
        if(pInfo) {
            return !pInfo->uri().isEmpty();
381 382
        }
    }
Gus Grubba's avatar
Gus Grubba committed
383
#endif
384
    return false;
385 386
}

Gus Grubba's avatar
Gus Grubba committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
//-----------------------------------------------------------------------------
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
}

405 406 407
//-----------------------------------------------------------------------------
void
VideoManager::_videoSourceChanged()
408
{
Gus Grubba's avatar
Gus Grubba committed
409
    _updateUVC();
410 411
    emit hasVideoChanged();
    emit isGStreamerChanged();
412
    emit isAutoStreamChanged();
413
    _restartVideo();
414 415
}

416 417 418
//-----------------------------------------------------------------------------
void
VideoManager::_udpPortChanged()
419
{
420
    _restartVideo();
421 422
}

423 424
//-----------------------------------------------------------------------------
void
425
VideoManager::_rtspUrlChanged()
426
{
427
    _restartVideo();
428 429
}

430 431 432 433
//-----------------------------------------------------------------------------
void
VideoManager::_tcpUrlChanged()
{
434
    _restartVideo();
435 436
}

437 438 439 440
//-----------------------------------------------------------------------------
void
VideoManager::_lowLatencyModeChanged()
{
DoinLakeFlyer's avatar
DoinLakeFlyer committed
441
    //restartVideo();
442 443
}

444 445 446 447
//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
{
448 449 450
    if(autoStreamConfigured()) {
        return true;
    }
451
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
452
    return !videoSource.isEmpty() && videoSource != VideoSettings::videoSourceNoVideo && videoSource != VideoSettings::videoDisabled;
453 454 455 456 457 458 459
}

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
460
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
461
    return
Gus Grubba's avatar
Gus Grubba committed
462 463
        videoSource == VideoSettings::videoSourceUDPH264 ||
        videoSource == VideoSettings::videoSourceUDPH265 ||
464
        videoSource == VideoSettings::videoSourceRTSP ||
465
        videoSource == VideoSettings::videoSourceTCP ||
466 467
        videoSource == VideoSettings::videoSourceMPEGTS ||
        autoStreamConfigured();
468 469 470 471 472
#else
    return false;
#endif
}

473 474 475 476 477 478 479 480 481
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

482 483 484 485 486 487 488 489 490 491 492 493 494 495
//-----------------------------------------------------------------------------
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();
}

496 497 498 499
void
VideoManager::_initVideo()
{
#if defined(QGC_GST_STREAMING)
500 501 502
    QQuickItem* root = qgcApp()->mainRootWindow();

    if (root == nullptr) {
503
        qCDebug(VideoManagerLog) << "mainRootWindow() failed. No root window";
504 505 506 507 508
        return;
    }

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

509
    if (widget != nullptr && _videoReceiver != nullptr) {
510
        if ((_videoSink = qgcApp()->toolbox()->corePlugin()->createVideoSink(this, widget)) != nullptr) {
511 512
            _videoReceiver->startDecoding(_videoSink);
        } else {
513
            qCDebug(VideoManagerLog) << "createVideoSink() failed";
514
        }
515
    } else {
516
        qCDebug(VideoManagerLog) << "video receiver disabled";
517 518 519 520
    }

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

521
    if (widget != nullptr && _thermalVideoReceiver != nullptr) {
522
        if ((_thermalVideoSink = qgcApp()->toolbox()->corePlugin()->createVideoSink(this, widget)) != nullptr) {
523 524
            _thermalVideoReceiver->startDecoding(_thermalVideoSink);
        } else {
525
            qCDebug(VideoManagerLog) << "createVideoSink() failed";
526
        }
527
    } else {
528
        qCDebug(VideoManagerLog) << "thermal video receiver disabled";
529
    }
530 531 532
#endif
}

533
//-----------------------------------------------------------------------------
534
bool
535
VideoManager::_updateSettings()
536
{
537
    if(!_videoSettings)
538
        return false;
539
    //-- Auto discovery
540
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
541
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
542
        if(pInfo) {
543
            bool status = false;
544
            qCDebug(VideoManagerLog) << "Configure primary stream: " << pInfo->uri();
545 546
            switch(pInfo->type()) {
                case VIDEO_STREAM_TYPE_RTSP:
547 548 549
                    if (status = _updateVideoUri(pInfo->uri())) {
                        _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceRTSP);
                    }
550
                    break;
551
                case VIDEO_STREAM_TYPE_TCP_MPEG:
552 553 554
                    if (status = _updateVideoUri(pInfo->uri())) {
                        _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceTCP);
                    }
555 556
                    break;
                case VIDEO_STREAM_TYPE_RTPUDP:
557 558 559
                    if (status = _updateVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(pInfo->uri()))) {
                        _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceUDPH264);
                    }
560 561
                    break;
                case VIDEO_STREAM_TYPE_MPEG_TS_H264:
562 563 564
                    if (status = _updateVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(pInfo->uri()))) {
                        _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceMPEGTS);
                    }
565 566
                    break;
                default:
567
                    status = _updateVideoUri(pInfo->uri());
568 569
                    break;
            }
570 571 572 573 574 575 576
            //-- 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:
577
                        _updateThermalVideoUri(pTinfo->uri());
578 579
                        break;
                    case VIDEO_STREAM_TYPE_RTPUDP:
580
                        _updateThermalVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(pTinfo->uri()));
581 582
                        break;
                    case VIDEO_STREAM_TYPE_MPEG_TS_H264:
583
                        _updateThermalVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(pTinfo->uri()));
584 585
                        break;
                    default:
586
                        _updateThermalVideoUri(pTinfo->uri());
587 588 589
                        break;
                }
            }
590
            return status;
591 592
        }
    }
593
    QString source = _videoSettings->videoSource()->rawValue().toString();
Gus Grubba's avatar
Gus Grubba committed
594
    if (source == VideoSettings::videoSourceUDPH264)
595
        return _updateVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
Gus Grubba's avatar
Gus Grubba committed
596
    else if (source == VideoSettings::videoSourceUDPH265)
597
        return _updateVideoUri(QStringLiteral("udp265://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
598
    else if (source == VideoSettings::videoSourceMPEGTS)
599
        return _updateVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
600
    else if (source == VideoSettings::videoSourceRTSP)
601
        return _updateVideoUri(_videoSettings->rtspUrl()->rawValue().toString());
602
    else if (source == VideoSettings::videoSourceTCP)
603
        return _updateVideoUri(QStringLiteral("tcp://%1").arg(_videoSettings->tcpUrl()->rawValue().toString()));
604 605
}

606 607
bool
VideoManager::_updateVideoUri(const QString& uri)
608 609 610 611
{
#if defined(QGC_GST_TAISYNC_ENABLED) && (defined(__android__) || defined(__ios__))
    //-- Taisync on iOS or Android sends a raw h.264 stream
    if (isTaisync()) {
612
        uri = QString("tsusb://0.0.0.0:%1").arg(TAISYNC_VIDEO_UDP_PORT);
613 614
    }
#endif
615 616 617 618
    if (uri == _videoUri) {
        return false;
    }

619
    _videoUri = uri;
620 621

    return true;
622 623 624
}

void
625
VideoManager::_updateThermalVideoUri(const QString& uri)
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
{
#if defined(QGC_GST_TAISYNC_ENABLED) && (defined(__android__) || defined(__ios__))
    //-- Taisync on iOS or Android sends a raw h.264 stream
    if (isTaisync()) {
        // FIXME: AV: TAISYNC_VIDEO_UDP_PORT is used by video stream, thermal stream should go via its own proxy
        _thermalVideoUri = QString("tsusb://0.0.0.0:%1").arg(TAISYNC_VIDEO_UDP_PORT);
        return;
    }
#endif
    _thermalVideoUri = uri;
}

//-----------------------------------------------------------------------------
void
VideoManager::_streamingChanged()
{
#if defined(QGC_GST_STREAMING)
    // FIXME: AV: we need VideoReceiver::running() to avoid restarting if one of streams is not active
    // but since VideoManager is going to be relpaced by Video Model during multiple video streaming development activity
    // I'll leave it as is for week or two
    if ((_videoReceiver && !_videoReceiver->streaming())
            /*|| (_thermalVideoReceiver && !_thermalVideoReceiver->streaming())*/) {
        _restartVideo();
    }
#endif
651 652
}

653 654 655 656 657 658 659 660 661 662 663
//-----------------------------------------------------------------------------
void
VideoManager::_streamChanged()
{
    if (_updateSettings()) {
        _restartVideo();
    }
}

//-----------------------------------------------------------------------------
void
664
VideoManager::_onStartComplete(VideoReceiver::STATUS status)
665 666 667
{
    disconnect(_videoReceiver, &VideoReceiver::onStartComplete, this, &VideoManager::_onStartComplete);

668
    if (status == VideoReceiver::STATUS_OK) {
669 670 671 672 673 674 675 676 677
        connect(_videoReceiver, &VideoReceiver::timeout, this, &VideoManager::_restartVideo);
        connect(_videoReceiver, &VideoReceiver::streamingChanged, this, &VideoManager::_streamingChanged);
    } else {
        QTimer::singleShot(1000, [this](){
            _restartVideo();
        });
    }
}

678
//-----------------------------------------------------------------------------
679
void
680
VideoManager::_restartVideo()
681
{
682
#if defined(QGC_GST_STREAMING)
683
    qCDebug(VideoManagerLog) << "Restart video streaming";
684
    stopVideo();
685
    _updateSettings();
686
    startVideo();
687
//    emit aspectRatioChanged();
688 689
#endif
}
690

691 692 693 694 695 696 697 698 699 700 701
//-----------------------------------------------------------------------------
void
VideoManager::_recordingStarted()
{
    _subtitleWriter.startCapturingTelemetry(_videoFile);
}

//-----------------------------------------------------------------------------
void
VideoManager::_recordingChanged()
{
702
#if defined(QGC_GST_STREAMING)
703 704 705
    if (_videoReceiver && !_videoReceiver->recording()) {
        _subtitleWriter.stopCapturingTelemetry();
    }
706
#endif
707 708 709 710
}

//----------------------------------------------------------------------------------------
void
711
VideoManager::_onTakeScreenshotComplete(VideoReceiver::STATUS status)
712 713 714
{
}

715 716 717 718 719
//----------------------------------------------------------------------------------------
void
VideoManager::_setActiveVehicle(Vehicle* vehicle)
{
    if(_activeVehicle) {
720
        disconnect(_activeVehicle, &Vehicle::connectionLostChanged, this, &VideoManager::_connectionLostChanged);
721 722 723 724 725
        if(_activeVehicle->dynamicCameras()) {
            QGCCameraControl* pCamera = _activeVehicle->dynamicCameras()->currentCameraInstance();
            if(pCamera) {
                pCamera->stopStream();
            }
726
            disconnect(_activeVehicle->dynamicCameras(), &QGCCameraManager::streamChanged, this, &VideoManager::_restartVideo);
727
        }
728 729 730
    }
    _activeVehicle = vehicle;
    if(_activeVehicle) {
731
        connect(_activeVehicle, &Vehicle::connectionLostChanged, this, &VideoManager::_connectionLostChanged);
732
        if(_activeVehicle->dynamicCameras()) {
733
            connect(_activeVehicle->dynamicCameras(), &QGCCameraManager::streamChanged, this, &VideoManager::_streamChanged);
734 735 736 737
            QGCCameraControl* pCamera = _activeVehicle->dynamicCameras()->currentCameraInstance();
            if(pCamera) {
                pCamera->resumeStream();
            }
738
        }
739 740 741
    } else {
        //-- Disable full screen video if vehicle is gone
        setfullScreen(false);
742
    }
743
    emit autoStreamConfiguredChanged();
744
    _restartVideo();
745 746
}

747 748 749 750 751 752 753 754 755 756
//----------------------------------------------------------------------------------------
void
VideoManager::_connectionLostChanged(bool connectionLost)
{
    if(connectionLost) {
        //-- Disable full screen video if connection is lost
        setfullScreen(false);
    }
}

757 758
//----------------------------------------------------------------------------------------
void
759
VideoManager::_aspectRatioChanged()
760
{
761
    emit aspectRatioChanged();
762
}