VideoManager.cc 23.4 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 93
   MultiVehicleManager *pVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
   connect(pVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &VideoManager::_setActiveVehicle);
94

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

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

107 108 109 110 111
    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);
112

113 114 115 116
    // 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);
117
#endif
118 119
    _updateSettings();
    if(isGStreamer()) {
120
        startVideo();
121
    } else {
122
        stopVideo();
123 124
    }

125
#endif
126 127
}

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

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

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

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

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

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

214 215 216 217 218 219
void
VideoManager::startRecording(const QString& videoFile)
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }
220
#if defined(QGC_GST_STREAMING)
221 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
    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);
248 249 250
#else
    Q_UNUSED(videoFile)
#endif
251 252 253 254 255 256 257 258
}

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

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

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

    _imageFile = imageFile;

    emit imageFileChanged();

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

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

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

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

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

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

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

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

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

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

425 426 427 428
//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
{
429 430 431
    if(autoStreamConfigured()) {
        return true;
    }
432
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
433
    return !videoSource.isEmpty() && videoSource != VideoSettings::videoSourceNoVideo && videoSource != VideoSettings::videoDisabled;
434 435 436 437 438 439 440
}

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
441
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
442
    return
Gus Grubba's avatar
Gus Grubba committed
443 444
        videoSource == VideoSettings::videoSourceUDPH264 ||
        videoSource == VideoSettings::videoSourceUDPH265 ||
445
        videoSource == VideoSettings::videoSourceRTSP ||
446
        videoSource == VideoSettings::videoSourceTCP ||
447 448
        videoSource == VideoSettings::videoSourceMPEGTS ||
        autoStreamConfigured();
449 450 451 452 453
#else
    return false;
#endif
}

454 455 456 457 458 459 460 461 462
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

463 464 465 466 467 468 469 470 471 472 473 474 475 476
//-----------------------------------------------------------------------------
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();
}

477 478 479 480
void
VideoManager::_initVideo()
{
#if defined(QGC_GST_STREAMING)
481 482 483
    QQuickItem* root = qgcApp()->mainRootWindow();

    if (root == nullptr) {
484
        qCDebug(VideoManagerLog) << "mainRootWindow() failed. No root window";
485 486 487 488 489
        return;
    }

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

490
    if (widget != nullptr && _videoReceiver != nullptr) {
491
        if ((_videoSink = qgcApp()->toolbox()->corePlugin()->createVideoSink(this, widget)) != nullptr) {
492 493
            _videoReceiver->startDecoding(_videoSink);
        } else {
494
            qCDebug(VideoManagerLog) << "createVideoSink() failed";
495
        }
496
    } else {
497
        qCDebug(VideoManagerLog) << "video receiver disabled";
498 499 500 501
    }

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

502
    if (widget != nullptr && _thermalVideoReceiver != nullptr) {
503
        if ((_thermalVideoSink = qgcApp()->toolbox()->corePlugin()->createVideoSink(this, widget)) != nullptr) {
504 505
            _thermalVideoReceiver->startDecoding(_thermalVideoSink);
        } else {
506
            qCDebug(VideoManagerLog) << "createVideoSink() failed";
507
        }
508
    } else {
509
        qCDebug(VideoManagerLog) << "thermal video receiver disabled";
510
    }
511 512 513
#endif
}

514
//-----------------------------------------------------------------------------
515 516
void
VideoManager::_updateSettings()
517
{
518
    if(!_videoSettings)
519
        return;
520
    //-- Auto discovery
521
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
522
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
523
        if(pInfo) {
524
            qCDebug(VideoManagerLog) << "Configure primary stream: " << pInfo->uri();
525 526
            switch(pInfo->type()) {
                case VIDEO_STREAM_TYPE_RTSP:
527
                    _setVideoUri(pInfo->uri());
528 529
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceRTSP);
                    break;
530
                case VIDEO_STREAM_TYPE_TCP_MPEG:
531
                    _setVideoUri(pInfo->uri());
532
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceTCP);
533 534
                    break;
                case VIDEO_STREAM_TYPE_RTPUDP:
535
                    _setVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(pInfo->uri()));
536
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceUDPH264);
537 538
                    break;
                case VIDEO_STREAM_TYPE_MPEG_TS_H264:
539
                    _setVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(pInfo->uri()));
540
                    _toolbox->settingsManager()->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceMPEGTS);
541 542
                    break;
                default:
543
                    _setVideoUri(pInfo->uri());
544 545
                    break;
            }
546 547 548 549 550 551 552
            //-- 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:
553
                        _setThermalVideoUri(pTinfo->uri());
554 555
                        break;
                    case VIDEO_STREAM_TYPE_RTPUDP:
556
                        _setThermalVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(pTinfo->uri()));
557 558
                        break;
                    case VIDEO_STREAM_TYPE_MPEG_TS_H264:
559
                        _setThermalVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(pTinfo->uri()));
560 561
                        break;
                    default:
562
                        _setThermalVideoUri(pTinfo->uri());
563 564 565
                        break;
                }
            }
566 567 568
            return;
        }
    }
569
    QString source = _videoSettings->videoSource()->rawValue().toString();
Gus Grubba's avatar
Gus Grubba committed
570
    if (source == VideoSettings::videoSourceUDPH264)
571
        _setVideoUri(QStringLiteral("udp://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
Gus Grubba's avatar
Gus Grubba committed
572
    else if (source == VideoSettings::videoSourceUDPH265)
573
        _setVideoUri(QStringLiteral("udp265://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
574
    else if (source == VideoSettings::videoSourceMPEGTS)
575
        _setVideoUri(QStringLiteral("mpegts://0.0.0.0:%1").arg(_videoSettings->udpPort()->rawValue().toInt()));
576
    else if (source == VideoSettings::videoSourceRTSP)
577
        _setVideoUri(_videoSettings->rtspUrl()->rawValue().toString());
578
    else if (source == VideoSettings::videoSourceTCP)
579 580 581 582 583 584 585 586 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
        _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
622 623
}

624
//-----------------------------------------------------------------------------
625
void
626
VideoManager::_restartVideo()
627
{
628
#if defined(QGC_GST_STREAMING)
629
    qCDebug(VideoManagerLog) << "Restart video streaming";
630
    stopVideo();
631
    _updateSettings();
632
    startVideo();
633
    emit aspectRatioChanged();
634 635
#endif
}
636

637 638 639 640 641 642 643 644 645 646 647
//-----------------------------------------------------------------------------
void
VideoManager::_recordingStarted()
{
    _subtitleWriter.startCapturingTelemetry(_videoFile);
}

//-----------------------------------------------------------------------------
void
VideoManager::_recordingChanged()
{
648
#if defined(QGC_GST_STREAMING)
649 650 651
    if (_videoReceiver && !_videoReceiver->recording()) {
        _subtitleWriter.stopCapturingTelemetry();
    }
652
#endif
653 654 655 656 657 658 659 660
}

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

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

693 694 695 696 697 698 699 700 701 702
//----------------------------------------------------------------------------------------
void
VideoManager::_connectionLostChanged(bool connectionLost)
{
    if(connectionLost) {
        //-- Disable full screen video if connection is lost
        setfullScreen(false);
    }
}

703 704
//----------------------------------------------------------------------------------------
void
705
VideoManager::_aspectRatioChanged()
706
{
707
    emit aspectRatioChanged();
708
}