-
Notifications
You must be signed in to change notification settings - Fork 344
/
cetonrtsp.cpp
507 lines (439 loc) · 13.2 KB
/
cetonrtsp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
106
107
108
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
136
137
138
139
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
295
296
297
298
299
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/** -*- Mode: c++ -*-
* CetonRTSP
* Copyright (c) 2011 Ronald Frazier
* Distributed as part of MythTV under GPL v2 and later.
*/
#include <QRegularExpression>
#include <QStringList>
#include <QTcpSocket>
#include <QUrl>
#include <QVector>
// MythTV includes
#include "libmythbase/mythlogging.h"
#include "libmythbase/mythsocket.h"
#include "cetonrtsp.h"
#define LOC QString("CetonRTSP(%1): ").arg(m_requestUrl.toString())
QMutex CetonRTSP::s_rtspMutex;
CetonRTSP::CetonRTSP(const QString &ip, uint tuner, ushort port)
{
m_requestUrl.setHost(ip);
m_requestUrl.setPort(port);
m_requestUrl.setScheme("rtsp");
m_requestUrl.setPath(QString("cetonmpeg%1").arg(tuner));
}
CetonRTSP::CetonRTSP(const QUrl &url) :
m_requestUrl(url)
{
if (url.port() < 0)
{
// default rtsp port
m_requestUrl.setPort(554);
}
}
CetonRTSP::~CetonRTSP()
{
StopKeepAlive();
}
bool CetonRTSP::ProcessRequest(
const QString &method, const QStringList* headers,
bool use_control, bool waitforanswer, const QString &alternative)
{
QMutexLocker locker(&s_rtspMutex);
m_responseHeaders.clear();
m_responseContent.clear();
// Create socket if socket object has never been created or in non-connected state
if (!m_socket || m_socket->state() != QAbstractSocket::ConnectedState)
{
if (!m_socket)
{
m_socket = new QTcpSocket();
}
else
{
m_socket->close();
}
m_socket->connectToHost(m_requestUrl.host(), m_requestUrl.port(),
QAbstractSocket::ReadWrite);
bool ok = m_socket->waitForConnected();
if (!ok)
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Could not connect to server %1:%2")
.arg(m_requestUrl.host()).arg(m_requestUrl.port()));
delete m_socket;
m_socket = nullptr;
return false;
}
}
else
{
// empty socket's waiting data just in case
m_socket->waitForReadyRead(30);
QVector<char> trash;
do
{
uint avail = m_socket->bytesAvailable();
trash.resize(std::max((uint)trash.size(), avail));
m_socket->read(trash.data(), avail);
m_socket->waitForReadyRead(30);
}
while (m_socket->bytesAvailable() > 0);
}
QStringList requestHeaders;
QString uri;
if (!alternative.isEmpty())
uri = alternative;
else if (use_control)
uri = m_controlUrl.toString();
else
uri = m_requestUrl.toString();
requestHeaders.append(QString("%1 %2 RTSP/1.0").arg(method, uri));
requestHeaders.append(QString("User-Agent: MythTV Ceton Recorder"));
requestHeaders.append(QString("CSeq: %1").arg(++m_sequenceNumber));
if (m_sessionId != "0")
requestHeaders.append(QString("Session: %1").arg(m_sessionId));
if (headers != nullptr)
{
for(int i = 0; i < headers->count(); i++)
{
const QString& header = headers->at(i);
requestHeaders.append(header);
}
}
requestHeaders.append(QString("\r\n"));
QString request = requestHeaders.join("\r\n");
LOG(VB_RECORD, LOG_DEBUG, LOC + QString("write: %1").arg(request));
m_socket->write(request.toLatin1());
m_responseHeaders.clear();
m_responseContent.clear();
if (!waitforanswer)
return true;
static const QRegularExpression kFirstLineRE { "^RTSP/1.0 (\\d+) ([^\r\n]+)" };
static const QRegularExpression kHeaderRE { R"(^([^:]+):\s*([^\r\n]+))" };
static const QRegularExpression kBlankLineRE { R"(^[\r\n]*$)" };
bool firstLine = true;
while (true)
{
if (!m_socket->canReadLine())
{
bool ready = m_socket->waitForReadyRead(30 * 1000);
if (!ready)
{
LOG(VB_RECORD, LOG_ERR, LOC + "RTSP server did not respond after 30s");
return false;
}
continue;
}
QString line = m_socket->readLine();
LOG(VB_RECORD, LOG_DEBUG, LOC + QString("read: %1").arg(line));
QRegularExpressionMatch match;
if (firstLine)
{
match = kFirstLineRE.match(line);
if (!match.hasMatch())
{
m_responseCode = -1;
m_responseMessage =
QString("Could not parse first line of response: '%1'")
.arg(line);
return false;
}
QStringList parts = match.capturedTexts();
m_responseCode = parts.at(1).toInt();
m_responseMessage = parts.at(2);
if (m_responseCode != 200)
{
m_responseMessage =
QString("Server couldn't process the request: '%1'")
.arg(m_responseMessage);
return false;
}
firstLine = false;
continue;
}
match = kBlankLineRE.match(line);
if (match.hasMatch()) break;
match = kHeaderRE.match(line);
if (!match.hasMatch())
{
m_responseCode = -1;
m_responseMessage = QString("Could not parse response header: '%1'")
.arg(line);
return false;
}
QStringList parts = match.capturedTexts();
m_responseHeaders.insert(parts.at(1), parts.at(2));
}
QString cSeq;
if (m_responseHeaders.contains("CSeq"))
{
cSeq = m_responseHeaders["CSeq"];
}
else
{
// Handle broken implementation, such as VLC
// doesn't respect the case of "CSeq", so find it regardless of the spelling
auto it = std::find_if(m_responseHeaders.cbegin(), m_responseHeaders.cend(),
[](const QString& key) -> bool
{return key.compare("CSeq", Qt::CaseInsensitive) == 0;});
if (it != m_responseHeaders.cend())
cSeq = it.value();
}
if (cSeq != QString("%1").arg(m_sequenceNumber))
{
LOG(VB_RECORD, LOG_WARNING, LOC +
QString("Expected CSeq of %1 but got %2")
.arg(m_sequenceNumber).arg(cSeq));
}
m_responseContent.clear();
int contentLength = m_responseHeaders.value("Content-Length").toInt();
if (contentLength > 0)
{
m_responseContent.resize(contentLength);
char* data = m_responseContent.data();
int bytesRead = 0;
while (bytesRead < contentLength)
{
if (m_socket->bytesAvailable() == 0)
m_socket->waitForReadyRead();
int count = m_socket->read(data+bytesRead, contentLength-bytesRead);
if (count == -1)
{
m_responseCode = -1;
m_responseMessage = "Could not read response content";
return false;
}
bytesRead += count;
}
LOG(VB_RECORD, LOG_DEBUG, LOC +
QString("received: %1").arg(m_responseContent.constData()));
}
return true;
}
bool CetonRTSP::GetOptions(QStringList &options)
{
if (ProcessRequest("OPTIONS"))
{
static const QRegularExpression kSeparatorRE { ",\\s*" };
options = m_responseHeaders.value("Public").split(kSeparatorRE);
m_canGetParameter = options.contains("GET_PARAMETER");
return true;
}
return false;
}
/**
* splitLines. prepare SDP content for easy read
*/
QStringList CetonRTSP::splitLines(const QByteArray &lines)
{
QStringList list;
QTextStream stream(lines);
QString line;
do
{
line = stream.readLine();
if (!line.isNull())
{
list.append(line);
}
}
while (!line.isNull());
return list;
}
/**
* readParameters. Scan a line like: Session: 1234556;destination=xx;client_port
* and return the first entry and fill the arguments in the provided Params
*/
QString CetonRTSP::readParameters(const QString &key, Params ¶meters)
{
QString val;
if (!m_responseHeaders.contains(key))
{
return val;
}
QStringList header = m_responseHeaders.value(key).split(";");
for (int i = 0; i < header.size(); i++)
{
QString entry = header[i].trimmed();
if (i ==0)
{
val = entry;
continue;
}
QStringList args = entry.split("=");
parameters.insert(args[0].trimmed(),
args.size() > 1 ? args[1].trimmed() : QString());
}
return val;
}
/**
* Return the base URL for the last DESCRIBE answer
*/
QUrl CetonRTSP::GetBaseUrl(void)
{
if (m_responseHeaders.contains("Content-Base"))
{
return m_responseHeaders["Content-Base"];
}
if (m_responseHeaders.contains("Content-Location"))
{
return m_responseHeaders["Content-Location"];
}
return m_requestUrl;
}
bool CetonRTSP::Describe(void)
{
QStringList headers;
headers.append("Accept: application/sdp");
if (!ProcessRequest("DESCRIBE", &headers))
return false;
// find control url
QStringList lines = splitLines(m_responseContent);
bool found = false;
QUrl base = m_controlUrl = GetBaseUrl();
for (const QString& line : std::as_const(lines))
{
if (line.startsWith("m="))
{
if (found)
{
// another new stream, no need to parse further
break;
}
if (!line.startsWith("m=video"))
{
// not a video stream
continue;
}
QStringList args = line.split(" ");
if (args[2] == "RTP/AVP" && args[3] == "33")
{
found = true;
}
continue;
}
if (line.startsWith("c="))
{
// TODO, connection parameter
// assume we will always get a control entry
continue;
}
if (line.startsWith("a=control:"))
{
// Per RFC: a=control:rtsp://example.com/foo
// This attribute may contain either relative and absolute URLs,
// following the rules and conventions set out in RFC 1808 [25].
QString url = line.mid(10).trimmed();
m_controlUrl = url;
if (url == "*")
{
m_controlUrl = base;
}
else if (m_controlUrl.isRelative())
{
m_controlUrl = base.resolved(m_controlUrl);
}
continue;
}
}
if (!found)
{
LOG(VB_RECORD, LOG_ERR, LOC + "expected content to be type "
"\"m=video 0 RTP/AVP 33\" but it appears not to be");
m_controlUrl = QUrl();
return false;
}
return true;
}
bool CetonRTSP::Setup(ushort clientPort1, ushort clientPort2,
ushort &rtpPort, ushort &rtcpPort,
uint32_t &ssrc)
{
LOG(VB_GENERAL, LOG_INFO, QString("CetonRTSP: ") +
QString("Transport: RTP/AVP;unicast;client_port=%1-%2")
.arg(clientPort1).arg(clientPort2));
QStringList extraHeaders;
extraHeaders.append(
QString("Transport: RTP/AVP;unicast;client_port=%1-%2")
.arg(clientPort1).arg(clientPort2));
if (!ProcessRequest("SETUP", &extraHeaders, true))
return false;
Params params;
QString session = readParameters("Session", params);
if (session.isEmpty())
{
LOG(VB_RECORD, LOG_ERR, LOC +
"session id not found in SETUP response");
return false;
}
if (session.size() < 8)
{
LOG(VB_RECORD, LOG_WARNING, LOC +
"invalid session id received");
}
m_sessionId = session;
if (params.contains("timeout"))
{
m_timeout = std::chrono::seconds(params["timeout"].toInt());
}
// QString transport = readParameters("Transport", params);
if (params.contains("ssrc"))
{
bool ok = false;
ssrc = params["ssrc"].toUInt(&ok, 16);
}
if (params.contains("server_port"))
{
QString line = params["server_port"];
QStringList val = line.split("-");
rtpPort = val[0].toInt();
rtcpPort = val.size() > 1 ? val[1].toInt() : 0;
}
return true;
}
bool CetonRTSP::Play(void)
{
bool result = ProcessRequest("PLAY");
StartKeepAlive();
return result;
}
bool CetonRTSP::Teardown(void)
{
StopKeepAlive();
bool result = ProcessRequest("TEARDOWN");
QMutexLocker locker(&s_rtspMutex);
delete m_socket;
m_socket = nullptr;
m_sessionId = "0";
return result;
}
void CetonRTSP::StartKeepAlive()
{
if (m_timer)
return;
auto timeout = std::max(m_timeout - 5s, 5s);
LOG(VB_RECORD, LOG_DEBUG, LOC +
QString("Start KeepAlive, every %1s").arg(timeout.count()));
m_timer = startTimer(timeout);
}
void CetonRTSP::StopKeepAlive()
{
if (m_timer)
{
killTimer(m_timer);
LOG(VB_RECORD, LOG_DEBUG, LOC + "Stop KeepAlive");
}
m_timer = 0;
}
void CetonRTSP::timerEvent(QTimerEvent* /*event*/)
{
LOG(VB_RECORD, LOG_DEBUG, LOC + "Sending KeepAlive");
if (m_canGetParameter)
{
ProcessRequest("GET_PARAMETER", nullptr, false, false);
}
else
{
ProcessRequest("OPTIONS", nullptr, false, false, "*");
}
}