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