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

37 38
QGC_LOGGING_CATEGORY(VideoManagerLog, "VideoManagerLog")

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

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

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

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

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

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

    emit isGStreamerChanged();
    qCDebug(VideoManagerLog) << "New Video Source:" << videoSource;
104
#if defined(QGC_GST_STREAMING)
105
    _videoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
106
    _thermalVideoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
107

108 109 110 111 112
    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);
    connect(_videoReceiver, &VideoReceiver::screenshotComplete, this,  &VideoManager::_screenshotComplete);
113

114 115 116 117
    // 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
    connect(_thermalVideoReceiver, &VideoReceiver::timeout, this, &VideoManager::_restartVideo);
    connect(_thermalVideoReceiver, &VideoReceiver::streamingChanged, this, &VideoManager::_streamingChanged);
118
#endif
119 120
    _updateSettings();
    if(isGStreamer()) {
121
        startVideo();
122
    } else {
123
        stopVideo();
124 125
    }

126
#endif
127 128
}

129
void VideoManager::_cleanupOldVideos()
130 131 132 133 134 135 136 137 138 139 140 141
{
#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;
142 143 144

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

147 148 149 150 151 152 153 154 155 156 157 158 159 160
    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();
161
            qCDebug(VideoManagerLog) << "Removing old video file:" << vidList.last().filePath();
162 163 164 165 166 167 168 169
            QFile file (vidList.last().filePath());
            file.remove();
            vidList.removeLast();
        }
    }
#endif
}

170 171 172 173
//-----------------------------------------------------------------------------
void
VideoManager::startVideo()
{
174 175 176 177 178
    if (qgcApp()->runningUnitTests()) {
        return;
    }

    if(!_videoSettings->streamEnabled()->rawValue().toBool() || !_videoSettings->streamConfigured()) {
179
        qCDebug(VideoManagerLog) << "Stream not enabled/configured";
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        return;
    }

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

    if(_videoReceiver != nullptr) {
        _videoReceiver->start(_videoUri, timeout);
        if (_videoSink != nullptr) {
            _videoReceiver->startDecoding(_videoSink);
        }
    }

    if(_thermalVideoReceiver != nullptr) {
        _thermalVideoReceiver->start(_thermalVideoUri, timeout);
        if (_thermalVideoSink != nullptr) {
            _thermalVideoReceiver->startDecoding(_thermalVideoSink);
        }
    }
#endif
200 201 202 203 204 205
}

//-----------------------------------------------------------------------------
void
VideoManager::stopVideo()
{
206 207 208
    if (qgcApp()->runningUnitTests()) {
        return;
    }
209
#if defined(QGC_GST_STREAMING)
210 211
    if(_videoReceiver) _videoReceiver->stop();
    if(_thermalVideoReceiver) _thermalVideoReceiver->stop();
212
#endif
213 214
}

215 216 217 218 219 220
void
VideoManager::startRecording(const QString& videoFile)
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }
221
#if defined(QGC_GST_STREAMING)
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    if (!_videoReceiver) {
        qgcApp()->showMessage(tr("Video receiver is not ready."));
        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) {
        qgcApp()->showMessage(tr("Invalid video format defined."));
        return;
    }

    //-- Disk usage maintenance
    _cleanupOldVideos();

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

    if(savePath.isEmpty()) {
        qgcApp()->showMessage(tr("Unabled to record video. Video save path must be specified in Settings."));
        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);
249 250 251
#else
    Q_UNUSED(videoFile)
#endif
252 253 254 255 256 257 258 259
}

void
VideoManager::stopRecording()
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }
260
#if defined(QGC_GST_STREAMING)
261 262 263 264 265
    if (!_videoReceiver) {
        return;
    }

    _videoReceiver->stopRecording();
266
#endif
267 268 269 270 271 272 273 274
}

void
VideoManager::grabImage(const QString& imageFile)
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }
275
#if defined(QGC_GST_STREAMING)
276 277 278 279 280 281 282 283 284
    if (!_videoReceiver) {
        return;
    }

    _imageFile = imageFile;

    emit imageFileChanged();

    _videoReceiver->takeScreenshot(_imageFile);
285 286 287
#else
    Q_UNUSED(imageFile)
#endif
288 289
}

290 291 292
//-----------------------------------------------------------------------------
double VideoManager::aspectRatio()
{
293
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
294
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
295 296 297 298 299
        if(pInfo) {
            qCDebug(VideoManagerLog) << "Primary AR: " << pInfo->aspectRatio();
            return pInfo->aspectRatio();
        }
    }
300
    // FIXME: AV: use _videoReceiver->videoSize() to calculate AR (if AR is not specified in the settings?)
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
    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();
334 335 336 337 338 339 340
        if(pInfo) {
            return pInfo->aspectRatio();
        }
    }
    return _videoSettings->aspectRatio()->rawValue().toDouble();
}

341 342 343 344 345 346 347 348 349 350 351 352 353
//-----------------------------------------------------------------------------
bool
VideoManager::hasThermal()
{
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->thermalStreamInstance();
        if(pInfo) {
            return true;
        }
    }
    return false;
}

354 355 356 357 358 359 360
//-----------------------------------------------------------------------------
QString
VideoManager::imageFile()
{
    return _imageFile;
}

361 362 363 364
//-----------------------------------------------------------------------------
bool
VideoManager::autoStreamConfigured()
{
Gus Grubba's avatar
Gus Grubba committed
365
#if defined(QGC_GST_STREAMING)
366
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
367 368 369
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
        if(pInfo) {
            return !pInfo->uri().isEmpty();
370 371
        }
    }
Gus Grubba's avatar
Gus Grubba committed
372
#endif
373
    return false;
374 375
}

Gus Grubba's avatar
Gus Grubba committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
//-----------------------------------------------------------------------------
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
}

394 395 396
//-----------------------------------------------------------------------------
void
VideoManager::_videoSourceChanged()
397
{
Gus Grubba's avatar
Gus Grubba committed
398
    _updateUVC();
399 400
    emit hasVideoChanged();
    emit isGStreamerChanged();
401
    emit isAutoStreamChanged();
402
    _restartVideo();
403 404
}

405 406 407
//-----------------------------------------------------------------------------
void
VideoManager::_udpPortChanged()
408
{
409
    _restartVideo();
410 411
}

412 413
//-----------------------------------------------------------------------------
void
414
VideoManager::_rtspUrlChanged()
415
{
416
    _restartVideo();
417 418
}

419 420 421 422
//-----------------------------------------------------------------------------
void
VideoManager::_tcpUrlChanged()
{
423
    _restartVideo();
424 425
}

426 427 428 429 430 431 432
//-----------------------------------------------------------------------------
void
VideoManager::_lowLatencyModeChanged()
{
    restartVideo();
}

433 434 435 436
//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
{
437 438 439
    if(autoStreamConfigured()) {
        return true;
    }
440
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
441
    return !videoSource.isEmpty() && videoSource != VideoSettings::videoSourceNoVideo && videoSource != VideoSettings::videoDisabled;
442 443 444 445 446 447 448
}

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
449
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
450
    return
Gus Grubba's avatar
Gus Grubba committed
451 452
        videoSource == VideoSettings::videoSourceUDPH264 ||
        videoSource == VideoSettings::videoSourceUDPH265 ||
453
        videoSource == VideoSettings::videoSourceRTSP ||
454
        videoSource == VideoSettings::videoSourceTCP ||
455 456
        videoSource == VideoSettings::videoSourceMPEGTS ||
        autoStreamConfigured();
457 458 459 460 461
#else
    return false;
#endif
}

462 463 464 465 466 467 468 469 470
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

471 472 473 474 475 476 477 478 479 480 481 482 483 484
//-----------------------------------------------------------------------------
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();
}

485 486 487 488
void
VideoManager::_initVideo()
{
#if defined(QGC_GST_STREAMING)
489 490 491
    QQuickItem* root = qgcApp()->mainRootWindow();

    if (root == nullptr) {
492
        qCDebug(VideoManagerLog) << "mainRootWindow() failed. No root window";
493 494 495 496 497
        return;
    }

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

498
    if (widget != nullptr && _videoReceiver != nullptr) {
499
        if ((_videoSink = qgcApp()->toolbox()->corePlugin()->createVideoSink(this, widget)) != nullptr) {
500 501
            _videoReceiver->startDecoding(_videoSink);
        } else {
502
            qCDebug(VideoManagerLog) << "createVideoSink() failed";
503
        }
504
    } else {
505
        qCDebug(VideoManagerLog) << "video receiver disabled";
506 507 508 509
    }

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

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

522
//-----------------------------------------------------------------------------
523 524
void
VideoManager::_updateSettings()
525
{
526
    if(!_videoSettings)
527
        return;
528
    //-- Auto discovery
529
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
530
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
531
        if(pInfo) {
532
            qCDebug(VideoManagerLog) << "Configure primary stream: " << pInfo->uri();
533 534
            switch(pInfo->type()) {
                case VIDEO_STREAM_TYPE_RTSP:
535
                    _setVideoUri(pInfo->uri());
536 537
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceRTSP);
                    break;
538
                case VIDEO_STREAM_TYPE_TCP_MPEG:
539
                    _setVideoUri(pInfo->uri());
540
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceTCP);
541 542
                    break;
                case VIDEO_STREAM_TYPE_RTPUDP:
543
                    _setVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(pInfo->uri()));
544
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceUDPH264);
545 546
                    break;
                case VIDEO_STREAM_TYPE_MPEG_TS_H264:
547
                    _setVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(pInfo->uri()));
548
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceMPEGTS);
549 550
                    break;
                default:
551
                    _setVideoUri(pInfo->uri());
552 553
                    break;
            }
554 555 556 557 558 559 560
            //-- 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:
561
                        _setThermalVideoUri(pTinfo->uri());
562 563
                        break;
                    case VIDEO_STREAM_TYPE_RTPUDP:
564
                        _setThermalVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(pTinfo->uri()));
565 566
                        break;
                    case VIDEO_STREAM_TYPE_MPEG_TS_H264:
567
                        _setThermalVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(pTinfo->uri()));
568 569
                        break;
                    default:
570
                        _setThermalVideoUri(pTinfo->uri());
571 572 573
                        break;
                }
            }
574 575 576
            return;
        }
    }
577
    QString source = _videoSettings->videoSource()->rawValue().toString();
Gus Grubba's avatar
Gus Grubba committed
578
    if (source == VideoSettings::videoSourceUDPH264)
579
        _setVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
Gus Grubba's avatar
Gus Grubba committed
580
    else if (source == VideoSettings::videoSourceUDPH265)
581
        _setVideoUri(QStringLiteral("udp265://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
582
    else if (source == VideoSettings::videoSourceMPEGTS)
583
        _setVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
584
    else if (source == VideoSettings::videoSourceRTSP)
585
        _setVideoUri(_videoSettings->rtspUrl()->rawValue().toString());
586
    else if (source == VideoSettings::videoSourceTCP)
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
        _setVideoUri(QStringLiteral("tcp://%1").arg(_videoSettings->tcpUrl()->rawValue().toString()));
}

void
VideoManager::_setVideoUri(const QString& uri)
{
#if defined(QGC_GST_TAISYNC_ENABLED) && (defined(__android__) || defined(__ios__))
    //-- Taisync on iOS or Android sends a raw h.264 stream
    if (isTaisync()) {
        _videoUri = QString("tsusb://0.0.0.0:%1").arg(TAISYNC_VIDEO_UDP_PORT);
        return;
    }
#endif
    _videoUri = uri;
}

void
VideoManager::_setThermalVideoUri(const QString& uri)
{
#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
630 631
}

632
//-----------------------------------------------------------------------------
633
void
634
VideoManager::_restartVideo()
635
{
636
#if defined(QGC_GST_STREAMING)
637
    qCDebug(VideoManagerLog) << "Restart video streaming";
638
    stopVideo();
639
    _updateSettings();
640
    startVideo();
641
    emit aspectRatioChanged();
642 643
#endif
}
644

645 646 647 648 649 650 651 652 653 654 655
//-----------------------------------------------------------------------------
void
VideoManager::_recordingStarted()
{
    _subtitleWriter.startCapturingTelemetry(_videoFile);
}

//-----------------------------------------------------------------------------
void
VideoManager::_recordingChanged()
{
656
#if defined(QGC_GST_STREAMING)
657 658 659
    if (_videoReceiver && !_videoReceiver->recording()) {
        _subtitleWriter.stopCapturingTelemetry();
    }
660
#endif
661 662 663 664 665 666 667 668
}

//----------------------------------------------------------------------------------------
void
VideoManager::_screenshotComplete()
{
}

669 670 671 672 673
//----------------------------------------------------------------------------------------
void
VideoManager::_setActiveVehicle(Vehicle* vehicle)
{
    if(_activeVehicle) {
674
        disconnect(_activeVehicle, &Vehicle::connectionLostChanged, this, &VideoManager::_connectionLostChanged);
675 676 677 678 679
        if(_activeVehicle->dynamicCameras()) {
            QGCCameraControl* pCamera = _activeVehicle->dynamicCameras()->currentCameraInstance();
            if(pCamera) {
                pCamera->stopStream();
            }
680
            disconnect(_activeVehicle->dynamicCameras(), &QGCCameraManager::streamChanged, this, &VideoManager::_restartVideo);
681
        }
682 683 684
    }
    _activeVehicle = vehicle;
    if(_activeVehicle) {
685
        connect(_activeVehicle, &Vehicle::connectionLostChanged, this, &VideoManager::_connectionLostChanged);
686
        if(_activeVehicle->dynamicCameras()) {
687
            connect(_activeVehicle->dynamicCameras(), &QGCCameraManager::streamChanged, this, &VideoManager::_restartVideo);
688 689 690 691
            QGCCameraControl* pCamera = _activeVehicle->dynamicCameras()->currentCameraInstance();
            if(pCamera) {
                pCamera->resumeStream();
            }
692
        }
693 694 695
    } else {
        //-- Disable full screen video if vehicle is gone
        setfullScreen(false);
696
    }
697
    emit autoStreamConfiguredChanged();
698
    _restartVideo();
699 700
}

701 702 703 704 705 706 707 708 709 710
//----------------------------------------------------------------------------------------
void
VideoManager::_connectionLostChanged(bool connectionLost)
{
    if(connectionLost) {
        //-- Disable full screen video if connection is lost
        setfullScreen(false);
    }
}

711 712
//----------------------------------------------------------------------------------------
void
713
VideoManager::_aspectRatioChanged()
714
{
715
    emit aspectRatioChanged();
716
}