[1410] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Client
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
| 5 | * Class: bncNetQueryRtp
|
---|
| 6 | *
|
---|
[1411] | 7 | * Purpose: Blocking Network Requests (NTRIP Version 2 with RTSP)
|
---|
[1410] | 8 | *
|
---|
| 9 | * Author: L. Mervart
|
---|
| 10 | *
|
---|
| 11 | * Created: 27-Dec-2008
|
---|
| 12 | *
|
---|
[8231] | 13 | * Changes:
|
---|
[1410] | 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
| 17 | #include <iostream>
|
---|
| 18 | #include <iomanip>
|
---|
| 19 |
|
---|
| 20 | #include "bncnetqueryrtp.h"
|
---|
[1535] | 21 | #include "bncsettings.h"
|
---|
[2011] | 22 | #include "bncversion.h"
|
---|
[1410] | 23 |
|
---|
| 24 | using namespace std;
|
---|
| 25 |
|
---|
| 26 | // Constructor
|
---|
| 27 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 28 | bncNetQueryRtp::bncNetQueryRtp() {
|
---|
[1412] | 29 | _socket = 0;
|
---|
| 30 | _udpSocket = 0;
|
---|
[1530] | 31 | _CSeq = 0;
|
---|
[1414] | 32 | _eventLoop = new QEventLoop(this);
|
---|
[1410] | 33 | }
|
---|
| 34 |
|
---|
| 35 | // Destructor
|
---|
| 36 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 37 | bncNetQueryRtp::~bncNetQueryRtp() {
|
---|
[1414] | 38 | delete _eventLoop;
|
---|
[1410] | 39 | delete _socket;
|
---|
[1412] | 40 | delete _udpSocket;
|
---|
[1410] | 41 | }
|
---|
| 42 |
|
---|
[8231] | 43 | //
|
---|
[1410] | 44 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 45 | void bncNetQueryRtp::stop() {
|
---|
[1414] | 46 | _eventLoop->quit();
|
---|
[1410] | 47 | _status = finished;
|
---|
[1532] | 48 | if (_socket) {
|
---|
| 49 | QByteArray reqStr = "TEARDOWN " + _url.toEncoded() + " RTSP/1.0\r\n"
|
---|
[8203] | 50 | + "CSeq: " + QString("%1").arg(++_CSeq).toLatin1() + "\r\n"
|
---|
[1532] | 51 | + "Session: " + _session + "\r\n"
|
---|
| 52 | + "\r\n";
|
---|
| 53 | _socket->write(reqStr, reqStr.length());
|
---|
| 54 | }
|
---|
[1410] | 55 | }
|
---|
| 56 |
|
---|
[8231] | 57 | //
|
---|
[1410] | 58 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1532] | 59 | void bncNetQueryRtp::slotKeepAlive() {
|
---|
| 60 | if (_socket) {
|
---|
| 61 | QByteArray reqStr = "GET_PARAMETER " + _url.toEncoded() + " RTSP/1.0\r\n"
|
---|
[8203] | 62 | + "CSeq: " + QString("%1").arg(++_CSeq).toLatin1() + "\r\n"
|
---|
[1532] | 63 | + "Session: " + _session + "\r\n"
|
---|
| 64 | + "\r\n";
|
---|
| 65 | _socket->write(reqStr, reqStr.length());
|
---|
| 66 | }
|
---|
| 67 | QTimer::singleShot(30000, this, SLOT(slotKeepAlive()));
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[8231] | 70 | //
|
---|
[1532] | 71 | ////////////////////////////////////////////////////////////////////////////
|
---|
[1410] | 72 | void bncNetQueryRtp::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[8231] | 75 | //
|
---|
[1410] | 76 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 77 | void bncNetQueryRtp::waitForReadyRead(QByteArray& outData) {
|
---|
[1414] | 78 |
|
---|
| 79 | // Wait Loop
|
---|
| 80 | // ---------
|
---|
| 81 | if (!_udpSocket->hasPendingDatagrams()) {
|
---|
| 82 | _eventLoop->exec();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // Append Data
|
---|
| 86 | // -----------
|
---|
| 87 | QByteArray datagram;
|
---|
| 88 | datagram.resize(_udpSocket->pendingDatagramSize());
|
---|
| 89 | _udpSocket->readDatagram(datagram.data(), datagram.size());
|
---|
[1415] | 90 |
|
---|
[1416] | 91 | if (datagram.size() > 12) {
|
---|
| 92 | outData.append(datagram.mid(12));
|
---|
| 93 | }
|
---|
[1410] | 94 | }
|
---|
| 95 |
|
---|
| 96 | // Connect to Caster, send the Request
|
---|
| 97 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 98 | void bncNetQueryRtp::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
| 99 |
|
---|
| 100 | const int timeOut = 5000;
|
---|
| 101 |
|
---|
| 102 | _status = running;
|
---|
| 103 |
|
---|
| 104 | delete _socket;
|
---|
| 105 | _socket = new QTcpSocket();
|
---|
| 106 |
|
---|
[1411] | 107 | // Default scheme
|
---|
| 108 | // --------------
|
---|
[1528] | 109 | _url = url;
|
---|
| 110 | _url.setScheme("rtsp");
|
---|
[1410] | 111 |
|
---|
| 112 | // Connect the Socket
|
---|
| 113 | // ------------------
|
---|
[1535] | 114 | bncSettings settings;
|
---|
[1410] | 115 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 116 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
[8231] | 117 |
|
---|
[1410] | 118 | if ( proxyHost.isEmpty() ) {
|
---|
[1528] | 119 | _socket->connectToHost(_url.host(), _url.port());
|
---|
[1410] | 120 | }
|
---|
| 121 | else {
|
---|
| 122 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[1411] | 125 | // Send Request 1
|
---|
| 126 | // --------------
|
---|
| 127 | if (_socket->waitForConnected(timeOut)) {
|
---|
[8203] | 128 | QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
|
---|
| 129 | QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
|
---|
[1411] | 130 | QByteArray userAndPwd;
|
---|
[8231] | 131 |
|
---|
[1411] | 132 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
[8203] | 133 | userAndPwd = "Authorization: Basic " + (uName.toLatin1() + ":" +
|
---|
| 134 | passW.toLatin1()).toBase64() + "\r\n";
|
---|
[1411] | 135 | }
|
---|
[1418] | 136 |
|
---|
[1419] | 137 | // Setup the RTSP Connection
|
---|
| 138 | // -------------------------
|
---|
[1518] | 139 | delete _udpSocket;
|
---|
| 140 | _udpSocket = new QUdpSocket();
|
---|
| 141 | _udpSocket->bind(0);
|
---|
| 142 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
[8203] | 143 | QByteArray clientPort = QString("%1").arg(_udpSocket->localPort()).toLatin1();
|
---|
[1518] | 144 |
|
---|
| 145 | QByteArray reqStr;
|
---|
[1528] | 146 | reqStr = "SETUP " + _url.toEncoded() + " RTSP/1.0\r\n"
|
---|
[8203] | 147 | + "CSeq: " + QString("%1").arg(++_CSeq).toLatin1() + "\r\n"
|
---|
[1518] | 148 | + "Ntrip-Version: Ntrip/2.0\r\n"
|
---|
| 149 | + "Ntrip-Component: Ntripclient\r\n"
|
---|
[6573] | 150 | + "User-Agent: NTRIP BNC/" BNCVERSION " (" BNC_OS ")\r\n"
|
---|
[1518] | 151 | + "Transport: RTP/GNSS;unicast;client_port=" + clientPort + "\r\n"
|
---|
[1533] | 152 | + userAndPwd;
|
---|
| 153 | if (!gga.isEmpty()) {
|
---|
| 154 | reqStr += "Ntrip-GGA: " + gga + "\r\n";
|
---|
| 155 | }
|
---|
| 156 | reqStr += "\r\n";
|
---|
| 157 |
|
---|
[1518] | 158 | _socket->write(reqStr, reqStr.length());
|
---|
[8231] | 159 |
|
---|
[1518] | 160 | // Read Server Answer 1
|
---|
| 161 | // --------------------
|
---|
| 162 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
| 163 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
| 164 | QTextStream in(_socket);
|
---|
[1520] | 165 | QByteArray serverPort;
|
---|
[1518] | 166 | QString line = in.readLine();
|
---|
| 167 | while (!line.isEmpty()) {
|
---|
| 168 | if (line.indexOf("Session:") == 0) {
|
---|
[8203] | 169 | _session = line.mid(9).toLatin1();
|
---|
[1411] | 170 | }
|
---|
[1520] | 171 | int iSrv = line.indexOf("server_port=");
|
---|
| 172 | if (iSrv != -1) {
|
---|
[8203] | 173 | serverPort = line.mid(iSrv+12).toLatin1();
|
---|
[1520] | 174 | }
|
---|
[1518] | 175 | line = in.readLine();
|
---|
| 176 | }
|
---|
[8231] | 177 |
|
---|
[1518] | 178 | // Send Request 2
|
---|
| 179 | // --------------
|
---|
[8231] | 180 | if (!_session.isEmpty()) {
|
---|
[1520] | 181 |
|
---|
| 182 | // Send initial RTP packet for firewall handling
|
---|
| 183 | // ---------------------------------------------
|
---|
| 184 | if (!serverPort.isEmpty()) {
|
---|
[1818] | 185 | unsigned sessInt = _session.toInt();
|
---|
[1520] | 186 | char rtpbuffer[12];
|
---|
[1721] | 187 | rtpbuffer[0] = 128;
|
---|
| 188 | rtpbuffer[1] = 96;
|
---|
| 189 | rtpbuffer[2] = 0;
|
---|
| 190 | rtpbuffer[3] = 0;
|
---|
| 191 | rtpbuffer[4] = 0;
|
---|
| 192 | rtpbuffer[5] = 0;
|
---|
| 193 | rtpbuffer[6] = 0;
|
---|
| 194 | rtpbuffer[7] = 0;
|
---|
| 195 | rtpbuffer[8] = (sessInt >> 24) & 0xFF;
|
---|
| 196 | rtpbuffer[9] = (sessInt >> 16) & 0xFF;
|
---|
| 197 | rtpbuffer[10] = (sessInt >> 8) & 0xFF;
|
---|
| 198 | rtpbuffer[11] = (sessInt ) & 0xFF;
|
---|
[1520] | 199 |
|
---|
[8231] | 200 | _udpSocket->writeDatagram(rtpbuffer, 12,
|
---|
[1522] | 201 | _socket->peerAddress(), serverPort.toInt());
|
---|
[1520] | 202 | }
|
---|
| 203 |
|
---|
[1528] | 204 | reqStr = "PLAY " + _url.toEncoded() + " RTSP/1.0\r\n"
|
---|
[8203] | 205 | + "CSeq: " + QString("%1").arg(++_CSeq).toLatin1() + "\r\n"
|
---|
[1530] | 206 | + "Session: " + _session + "\r\n"
|
---|
[1518] | 207 | + "\r\n";
|
---|
| 208 | _socket->write(reqStr, reqStr.length());
|
---|
[8231] | 209 |
|
---|
[1518] | 210 | // Read Server Answer 2
|
---|
| 211 | // --------------------
|
---|
| 212 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
| 213 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
| 214 | QTextStream in(_socket);
|
---|
| 215 | line = in.readLine();
|
---|
| 216 | while (!line.isEmpty()) {
|
---|
| 217 | if (line.indexOf("200 OK") != -1) {
|
---|
[8231] | 218 | emit newMessage(_url.toEncoded().replace(0,1,"")
|
---|
[1650] | 219 | + ": UDP connection established", true);
|
---|
[1532] | 220 | slotKeepAlive();
|
---|
[1518] | 221 | return;
|
---|
| 222 | }
|
---|
[1419] | 223 | line = in.readLine();
|
---|
[1411] | 224 | }
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
[1410] | 230 | }
|
---|
| 231 |
|
---|
[1411] | 232 | delete _socket;
|
---|
| 233 | _socket = 0;
|
---|
| 234 | _status = error;
|
---|
[8231] | 235 | emit newMessage(_url.toEncoded().replace(0,1,"")
|
---|
[1653] | 236 | + ": NetQuery, waiting for connect", true);
|
---|
[1410] | 237 | }
|
---|
| 238 |
|
---|
[6787] | 239 | void bncNetQueryRtp::keepAliveRequest(const QUrl& /* url */, const QByteArray& /* gga */) {
|
---|
| 240 | }
|
---|