Skip to content

Commit

Permalink
Merge pull request #8551 from Williangalvani/low-latency
Browse files Browse the repository at this point in the history
Add Low latency mode for video
  • Loading branch information
DonLakeFlyer authored Mar 12, 2020
2 parents f351526 + ef675ec commit 1a66829
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Settings/Video.SettingsGroup.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,12 @@
"longDescription": "Disable Video Stream when disarmed.",
"type": "bool",
"defaultValue": false
},
{
"name": "lowLatencyMode",
"shortDescription": "Tweaks video for lower latency",
"longDescription": "If this option is enabled, the rtpjitterbuffer is removed and the video sink is set to assynchronous mode, reducing the latency by about 200 ms.",
"type": "bool",
"defaultValue": false
}
]
1 change: 1 addition & 0 deletions src/Settings/VideoSettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ DECLARE_SETTINGSFACT(VideoSettings, enableStorageLimit)
DECLARE_SETTINGSFACT(VideoSettings, rtspTimeout)
DECLARE_SETTINGSFACT(VideoSettings, streamEnabled)
DECLARE_SETTINGSFACT(VideoSettings, disableWhenDisarmed)
DECLARE_SETTINGSFACT(VideoSettings, lowLatencyMode)

DECLARE_SETTINGSFACT_NO_FUNC(VideoSettings, videoSource)
{
Expand Down
1 change: 1 addition & 0 deletions src/Settings/VideoSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class VideoSettings : public SettingsGroup
DEFINE_SETTINGFACT(rtspTimeout)
DEFINE_SETTINGFACT(streamEnabled)
DEFINE_SETTINGFACT(disableWhenDisarmed)
DEFINE_SETTINGFACT(lowLatencyMode)

Q_PROPERTY(bool streamConfigured READ streamConfigured NOTIFY streamConfiguredChanged)
Q_PROPERTY(QString rtspVideoSource READ rtspVideoSource CONSTANT)
Expand Down
1 change: 1 addition & 0 deletions src/Vehicle/Vehicle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ void Vehicle::_commonInit()
// Set video stream to udp if running ArduSub and Video is disabled
if (sub() && _settingsManager->videoSettings()->videoSource()->rawValue() == VideoSettings::videoDisabled) {
_settingsManager->videoSettings()->videoSource()->setRawValue(VideoSettings::videoSourceUDPH264);
_settingsManager->videoSettings()->lowLatencyMode()->setRawValue(true);
}

//-- Airspace Management
Expand Down
8 changes: 8 additions & 0 deletions src/VideoStreaming/VideoManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ VideoManager::setToolbox(QGCToolbox *toolbox)
connect(_videoSettings->rtspUrl(), &Fact::rawValueChanged, this, &VideoManager::_rtspUrlChanged);
connect(_videoSettings->tcpUrl(), &Fact::rawValueChanged, this, &VideoManager::_tcpUrlChanged);
connect(_videoSettings->aspectRatio(), &Fact::rawValueChanged, this, &VideoManager::_aspectRatioChanged);
connect(_videoSettings->lowLatencyMode(),&Fact::rawValueChanged, this, &VideoManager::_lowLatencyModeChanged);
MultiVehicleManager *pVehicleMgr = qgcApp()->toolbox()->multiVehicleManager();
connect(pVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &VideoManager::_setActiveVehicle);

Expand Down Expand Up @@ -228,6 +229,13 @@ VideoManager::_tcpUrlChanged()
restartVideo();
}

//-----------------------------------------------------------------------------
void
VideoManager::_lowLatencyModeChanged()
{
restartVideo();
}

//-----------------------------------------------------------------------------
bool
VideoManager::hasVideo()
Expand Down
1 change: 1 addition & 0 deletions src/VideoStreaming/VideoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ protected slots:
void _udpPortChanged ();
void _rtspUrlChanged ();
void _tcpUrlChanged ();
void _lowLatencyModeChanged ();
void _updateUVC ();
void _setActiveVehicle (Vehicle* vehicle);
void _aspectRatioChanged ();
Expand Down
11 changes: 9 additions & 2 deletions src/VideoStreaming/VideoReceiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ VideoReceiver::_makeSource(const QString& uri)
gst_element_foreach_src_pad(source, _padProbe, &probeRes);

if (probeRes & 1) {
if (probeRes & 2) {
if (probeRes & 2 && !_videoSettings->lowLatencyMode()->rawValue().toBool()) {
if ((buffer = gst_element_factory_make("rtpjitterbuffer", nullptr)) == nullptr) {
qCCritical(VideoReceiverLog) << "gst_element_factory_make('rtpjitterbuffer') failed";
break;
Expand All @@ -414,7 +414,11 @@ VideoReceiver::_makeSource(const QString& uri)
}
}
} else {
g_signal_connect(source, "pad-added", G_CALLBACK(_linkPadWithOptionalBuffer), parser);
if (_videoSettings->lowLatencyMode()->rawValue().toBool()) {
g_signal_connect(source, "pad-added", G_CALLBACK(newPadCB), parser);
} else {
g_signal_connect(source, "pad-added", G_CALLBACK(_linkPadWithOptionalBuffer), parser);
}
}

g_signal_connect(parser, "pad-added", G_CALLBACK(_wrapWithGhostPad), nullptr);
Expand Down Expand Up @@ -568,6 +572,9 @@ VideoReceiver::start()
qCWarning(VideoReceiverLog) << "Failed because video sink is not set";
return;
}

g_object_set(_videoSink, "sync", !_videoSettings->lowLatencyMode()->rawValue().toBool(), NULL);

if(_running) {
qCDebug(VideoReceiverLog) << "Already running!";
return;
Expand Down
10 changes: 10 additions & 0 deletions src/ui/preferences/GeneralSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,16 @@ Rectangle {
fact: QGroundControl.settingsManager.videoSettings.disableWhenDisarmed
visible: _isGst && QGroundControl.settingsManager.videoSettings.disableWhenDisarmed.visible
}

QGCLabel {
text: qsTr("Low Latency Mode")
visible: _isGst && QGroundControl.settingsManager.videoSettings.lowLatencyMode.visible
}
FactCheckBox {
text: ""
fact: QGroundControl.settingsManager.videoSettings.lowLatencyMode
visible: _isGst && QGroundControl.settingsManager.videoSettings.lowLatencyMode.visible
}
}
}

Expand Down

0 comments on commit 1a66829

Please sign in to comment.