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

QGC_LOGGING_CATEGORY(VideoManagerLog, "VideoManagerLog")

33 34 35 36 37 38
static const char* kFileExtension[VideoReceiver::FILE_FORMAT_MAX - VideoReceiver::FILE_FORMAT_MIN] = {
    "mkv",
    "mov",
    "mp4"
};

39
//-----------------------------------------------------------------------------
40 41
VideoManager::VideoManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
42 43 44 45 46 47
{
}

//-----------------------------------------------------------------------------
VideoManager::~VideoManager()
{
48 49 50 51
    delete _videoReceiver;
    _videoReceiver = nullptr;
    delete _thermalVideoReceiver;
    _thermalVideoReceiver = nullptr;
52 53 54 55 56 57 58 59 60 61 62
#if defined(QGC_GST_STREAMING)
    if (_thermalVideoSink != nullptr) {
        gst_object_unref(_thermalVideoSink);
        _thermalVideoSink = nullptr;
    }

    if (_videoSink != nullptr) {
        gst_object_unref(_videoSink);
        _videoSink = nullptr;
    }
#endif
63 64 65 66 67 68 69 70
}

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

   // TODO: Those connections should be Per Video, not per VideoManager.
75 76
   _videoSettings = toolbox->settingsManager()->videoSettings();
   QString videoSource = _videoSettings->videoSource()->rawValue().toString();
77 78 79
   connect(_videoSettings->videoSource(),   &Fact::rawValueChanged, this, &VideoManager::_videoSourceChanged);
   connect(_videoSettings->udpPort(),       &Fact::rawValueChanged, this, &VideoManager::_udpPortChanged);
   connect(_videoSettings->rtspUrl(),       &Fact::rawValueChanged, this, &VideoManager::_rtspUrlChanged);
80
   connect(_videoSettings->tcpUrl(),        &Fact::rawValueChanged, this, &VideoManager::_tcpUrlChanged);
81
   connect(_videoSettings->aspectRatio(),   &Fact::rawValueChanged, this, &VideoManager::_aspectRatioChanged);
82 83
   MultiVehicleManager *pVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
   connect(pVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &VideoManager::_setActiveVehicle);
84

85
#if defined(QGC_GST_STREAMING)
86 87
#ifndef QGC_DISABLE_UVC
   // If we are using a UVC camera setup the device name
Gus Grubba's avatar
Gus Grubba committed
88
   _updateUVC();
89
#endif
90 91 92

    emit isGStreamerChanged();
    qCDebug(VideoManagerLog) << "New Video Source:" << videoSource;
93
    _videoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
94
    _thermalVideoReceiver = toolbox->corePlugin()->createVideoReceiver(this);
95

96 97 98 99 100
    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);
101

102 103 104 105
    // 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);
106

107 108
    _updateSettings();
    if(isGStreamer()) {
109
        startVideo();
110
    } else {
111
        stopVideo();
112 113
    }

114
#endif
115 116
}

117
void VideoManager::_cleanupOldVideos()
118 119 120 121 122 123 124 125 126 127 128 129
{
#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;
130 131 132

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

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    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();
            qCDebug(VideoReceiverLog) << "Removing old video file:" << vidList.last().filePath();
            QFile file (vidList.last().filePath());
            file.remove();
            vidList.removeLast();
        }
    }
#endif
}

158 159 160 161
//-----------------------------------------------------------------------------
void
VideoManager::startVideo()
{
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    if (qgcApp()->runningUnitTests()) {
        return;
    }

    if(!_videoSettings->streamEnabled()->rawValue().toBool() || !_videoSettings->streamConfigured()) {
        qCDebug(VideoReceiverLog) << "Stream not enabled/configured";
        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
188 189 190 191 192 193
}

//-----------------------------------------------------------------------------
void
VideoManager::stopVideo()
{
194 195 196 197
    if (qgcApp()->runningUnitTests()) {
        return;
    }

198 199 200 201
    if(_videoReceiver) _videoReceiver->stop();
    if(_thermalVideoReceiver) _thermalVideoReceiver->stop();
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
void
VideoManager::startRecording(const QString& videoFile)
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }

    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);
}

void
VideoManager::stopRecording()
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }

    if (!_videoReceiver) {
        return;
    }

    _videoReceiver->stopRecording();
}

void
VideoManager::grabImage(const QString& imageFile)
{
    if (qgcApp()->runningUnitTests()) {
        return;
    }

    if (!_videoReceiver) {
        return;
    }

    _imageFile = imageFile;

    emit imageFileChanged();

    _videoReceiver->takeScreenshot(_imageFile);
}

270 271 272
//-----------------------------------------------------------------------------
double VideoManager::aspectRatio()
{
273
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
274
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
275 276 277 278 279
        if(pInfo) {
            qCDebug(VideoManagerLog) << "Primary AR: " << pInfo->aspectRatio();
            return pInfo->aspectRatio();
        }
    }
280
    // FIXME: AV: use _videoReceiver->videoSize() to calculate AR (if AR is not specified in the settings?)
281 282 283 284 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
    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();
314 315 316 317 318 319 320
        if(pInfo) {
            return pInfo->aspectRatio();
        }
    }
    return _videoSettings->aspectRatio()->rawValue().toDouble();
}

321 322 323 324 325 326 327 328 329 330 331 332 333
//-----------------------------------------------------------------------------
bool
VideoManager::hasThermal()
{
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->thermalStreamInstance();
        if(pInfo) {
            return true;
        }
    }
    return false;
}

334 335 336 337 338 339 340
//-----------------------------------------------------------------------------
QString
VideoManager::imageFile()
{
    return _imageFile;
}

341 342 343 344
//-----------------------------------------------------------------------------
bool
VideoManager::autoStreamConfigured()
{
Gus Grubba's avatar
Gus Grubba committed
345
#if defined(QGC_GST_STREAMING)
346
    if(_activeVehicle && _activeVehicle->dynamicCameras()) {
347 348 349
        QGCVideoStreamInfo* pInfo = _activeVehicle->dynamicCameras()->currentStreamInstance();
        if(pInfo) {
            return !pInfo->uri().isEmpty();
350 351
        }
    }
Gus Grubba's avatar
Gus Grubba committed
352
#endif
353
    return false;
354 355
}

Gus Grubba's avatar
Gus Grubba committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
//-----------------------------------------------------------------------------
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
}

374 375 376
//-----------------------------------------------------------------------------
void
VideoManager::_videoSourceChanged()
377
{
Gus Grubba's avatar
Gus Grubba committed
378
    _updateUVC();
379 380
    emit hasVideoChanged();
    emit isGStreamerChanged();
381
    emit isAutoStreamChanged();
382
    _restartVideo();
383 384
}

385 386 387
//-----------------------------------------------------------------------------
void
VideoManager::_udpPortChanged()
388
{
389
    _restartVideo();
390 391
}

392 393
//-----------------------------------------------------------------------------
void
394
VideoManager::_rtspUrlChanged()
395
{
396
    _restartVideo();
397 398
}

399 400 401 402
//-----------------------------------------------------------------------------
void
VideoManager::_tcpUrlChanged()
{
403
    _restartVideo();
404 405
}

406 407 408 409
//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
{
410 411 412
    if(autoStreamConfigured()) {
        return true;
    }
413
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
414
    return !videoSource.isEmpty() && videoSource != VideoSettings::videoSourceNoVideo && videoSource != VideoSettings::videoDisabled;
415 416 417 418 419 420 421
}

//-----------------------------------------------------------------------------
bool
VideoManager::isGStreamer()
{
#if defined(QGC_GST_STREAMING)
422
    QString videoSource = _videoSettings->videoSource()->rawValue().toString();
423
    return
Gus Grubba's avatar
Gus Grubba committed
424 425
        videoSource == VideoSettings::videoSourceUDPH264 ||
        videoSource == VideoSettings::videoSourceUDPH265 ||
426
        videoSource == VideoSettings::videoSourceRTSP ||
427
        videoSource == VideoSettings::videoSourceTCP ||
428 429
        videoSource == VideoSettings::videoSourceMPEGTS ||
        autoStreamConfigured();
430 431 432 433 434
#else
    return false;
#endif
}

435 436 437 438 439 440 441 442 443
//-----------------------------------------------------------------------------
#ifndef QGC_DISABLE_UVC
bool
VideoManager::uvcEnabled()
{
    return QCameraInfo::availableCameras().count() > 0;
}
#endif

444 445 446 447 448 449 450 451 452 453 454 455 456 457
//-----------------------------------------------------------------------------
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();
}

458 459 460
//-----------------------------------------------------------------------------
#if defined(QGC_GST_STREAMING)
GstElement*
461
VideoManager::_makeVideoSink(gpointer widget)
462
{
463
    GstElement* sink;
464

465 466 467
    if ((sink = gst_element_factory_make("qgcvideosinkbin", nullptr)) != nullptr) {
        g_object_set(sink, "widget", widget, NULL);
    } else {
468
        qCritical() << "gst_element_factory_make('qgcvideosinkbin') failed";
469 470 471 472 473 474 475 476 477 478 479
    }

    return sink;
}
#endif

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

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

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

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

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

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

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

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

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
//-----------------------------------------------------------------------------
void
VideoManager::_recordingStarted()
{
    _subtitleWriter.startCapturingTelemetry(_videoFile);
}

//-----------------------------------------------------------------------------
void
VideoManager::_recordingChanged()
{
    if (_videoReceiver && !_videoReceiver->recording()) {
        _subtitleWriter.stopCapturingTelemetry();
    }
}

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

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

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

700 701
//----------------------------------------------------------------------------------------
void
702
VideoManager::_aspectRatioChanged()
703
{
704
    emit aspectRatioChanged();
705
}