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