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