Newer
Older
/****************************************************************************
*
* (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/
#include <QtGlobal>
#include <QDebug>
#include "TaisyncLink.h"
#include "QGC.h"
#include "QGCApplication.h"
#include "SettingsManager.h"
#include "VideoSettings.h"
#include "VideoManager.h"
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "TaisyncTelemetry.h"
#include "TaisyncSettings.h"
#include "TaisyncVideoReceiver.h"
static const char* kEnableVideo = "enableVideo";
//-----------------------------------------------------------------------------
TaisyncLink::TaisyncLink(SharedLinkConfigurationPointer& config)
: LinkInterface(config)
, _taiConfig(qobject_cast<TaisyncConfiguration*>(config.data()))
{
if (!_taiConfig) {
qWarning() << "Internal error";
}
moveToThread(this);
}
//-----------------------------------------------------------------------------
TaisyncLink::~TaisyncLink()
{
_disconnect();
// Tell the thread to exit
_running = false;
quit();
// Wait for it to exit
wait();
this->deleteLater();
}
//-----------------------------------------------------------------------------
void
TaisyncLink::run()
{
if(_hardwareConnect()) {
exec();
}
_hardwareDisconnect();
}
//-----------------------------------------------------------------------------
void
TaisyncLink::_restartConnection()
{
if(this->isConnected())
{
_disconnect();
_connect();
}
}
//-----------------------------------------------------------------------------
QString
TaisyncLink::getName() const
{
return _taiConfig->name();
}
//-----------------------------------------------------------------------------
void
TaisyncLink::_writeBytes(const QByteArray data)
{
if (_taiTelemetery) {
_taiTelemetery->writeBytes(data);
_logOutputDataRate(static_cast<quint64>(data.size()), QDateTime::currentMSecsSinceEpoch());
}
}
//-----------------------------------------------------------------------------
void
TaisyncLink::_readBytes(QByteArray bytes)
{
emit bytesReceived(this, bytes);
_logInputDataRate(static_cast<quint64>(bytes.size()), QDateTime::currentMSecsSinceEpoch());
}
//-----------------------------------------------------------------------------
void
TaisyncLink::_disconnect()
{
_running = false;
quit();
wait();
if (_taiTelemetery) {
_hardwareDisconnect();
emit disconnected();
}
#if defined(__ios__) || defined(__android__)
qgcApp()->toolbox()->videoManager()->setIsTaisync(false);
#endif
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//-- Restore video settings
if(!_savedVideoSource.isNull()) {
VideoSettings* pVSettings = qgcApp()->toolbox()->settingsManager()->videoSettings();
pVSettings->videoSource()->setRawValue(_savedVideoSource);
pVSettings->udpPort()->setRawValue(_savedVideoUDP);
pVSettings->aspectRatio()->setRawValue(_savedAR);
pVSettings->setVisible(_savedVideoState);
}
}
//-----------------------------------------------------------------------------
bool
TaisyncLink::_connect(void)
{
if(this->isRunning() || _running) {
_running = false;
quit();
wait();
}
_running = true;
if(_taiConfig->videoEnabled()) {
//-- Hide video selection as we will be fixed to Taisync video and set the way we need it.
VideoSettings* pVSettings = qgcApp()->toolbox()->settingsManager()->videoSettings();
_savedVideoSource = pVSettings->videoSource()->rawValue();
_savedVideoUDP = pVSettings->udpPort()->rawValue();
_savedAR = pVSettings->aspectRatio()->rawValue();
_savedVideoState = pVSettings->visible();
#if defined(__ios__) || defined(__android__)
//-- iOS and Android receive raw h.264 and need a different pipeline
qgcApp()->toolbox()->videoManager()->setIsTaisync(true);
#endif
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
pVSettings->setVisible(false);
pVSettings->udpPort()->setRawValue(5600);
pVSettings->aspectRatio()->setRawValue(1024.0 / 768.0);
pVSettings->videoSource()->setRawValue(QString(VideoSettings::videoSourceUDP));
}
start(NormalPriority);
return true;
}
//-----------------------------------------------------------------------------
void
TaisyncLink::_hardwareDisconnect()
{
if (_taiTelemetery) {
_taiTelemetery->close();
_taiTelemetery->deleteLater();
_taiTelemetery = nullptr;
}
if (_taiSettings) {
_taiSettings->close();
_taiSettings->deleteLater();
_taiSettings = nullptr;
}
#if defined(__ios__) || defined(__android__)
if (_taiVideo) {
_taiVideo->close();
_taiVideo->deleteLater();
_taiVideo = nullptr;
}
#endif
}
//-----------------------------------------------------------------------------
bool
TaisyncLink::_hardwareConnect()
{
_hardwareDisconnect();
_taiTelemetery = new TaisyncTelemetry(this);
QObject::connect(_taiTelemetery, &TaisyncTelemetry::bytesReady, this, &TaisyncLink::_readBytes);
_taiTelemetery->start();
_taiSettings = new TaisyncSettings(this);
_taiSettings->start();
#if defined(__ios__) || defined(__android__)
if(_taiConfig->videoEnabled()) {
_taiVideo = new TaisyncVideoReceiver(this);
_taiVideo->start();
}
#endif
return true;
}
//-----------------------------------------------------------------------------
bool
TaisyncLink::isConnected() const
{
return _taiTelemetery != nullptr;
}
//-----------------------------------------------------------------------------
qint64
TaisyncLink::getConnectionSpeed() const
{
return 57600; // 57.6 Kbit
}
//-----------------------------------------------------------------------------
qint64
TaisyncLink::getCurrentInDataRate() const
{
return 0;
}
//-----------------------------------------------------------------------------
qint64
TaisyncLink::getCurrentOutDataRate() const
{
return 0;
}
//--------------------------------------------------------------------------
//-- TaisyncConfiguration
//--------------------------------------------------------------------------
TaisyncConfiguration::TaisyncConfiguration(const QString& name) : LinkConfiguration(name)
{
}
//--------------------------------------------------------------------------
TaisyncConfiguration::TaisyncConfiguration(TaisyncConfiguration* source) : LinkConfiguration(source)
{
_copyFrom(source);
}
//--------------------------------------------------------------------------
TaisyncConfiguration::~TaisyncConfiguration()
{
}
//--------------------------------------------------------------------------
void
TaisyncConfiguration::copyFrom(LinkConfiguration *source)
{
LinkConfiguration::copyFrom(source);
_copyFrom(source);
}
//--------------------------------------------------------------------------
void
TaisyncConfiguration::_copyFrom(LinkConfiguration *source)
{
TaisyncConfiguration* usource = dynamic_cast<TaisyncConfiguration*>(source);
if (usource) {
_enableVideo = usource->videoEnabled();
} else {
qWarning() << "Internal error";
}
}
//--------------------------------------------------------------------------
void
TaisyncConfiguration::setVideoEnabled(bool enable)
{
_enableVideo = enable;
emit enableVideoChanged();
}
//--------------------------------------------------------------------------
void
TaisyncConfiguration::saveSettings(QSettings& settings, const QString& root)
{
settings.beginGroup(root);
settings.setValue(kEnableVideo, _enableVideo);
settings.endGroup();
}
//--------------------------------------------------------------------------
void
TaisyncConfiguration::loadSettings(QSettings& settings, const QString& root)
{
settings.beginGroup(root);
_enableVideo = settings.value(kEnableVideo, true).toBool();
settings.endGroup();
}
//--------------------------------------------------------------------------
void
TaisyncConfiguration::updateSettings()
{
if(_link) {
TaisyncLink* ulink = dynamic_cast<TaisyncLink*>(_link);
if(ulink) {
ulink->_restartConnection();
}
}
}