[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;
|
---|
[1414] | 31 | _eventLoop = new QEventLoop(this);
|
---|
[1410] | 32 | }
|
---|
| 33 |
|
---|
| 34 | // Destructor
|
---|
| 35 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 36 | bncNetQueryRtp::~bncNetQueryRtp() {
|
---|
[1414] | 37 | delete _eventLoop;
|
---|
[1410] | 38 | delete _socket;
|
---|
[1412] | 39 | delete _udpSocket;
|
---|
[1410] | 40 | }
|
---|
| 41 |
|
---|
| 42 | //
|
---|
| 43 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 44 | void bncNetQueryRtp::stop() {
|
---|
[1414] | 45 | _eventLoop->quit();
|
---|
[1410] | 46 | _status = finished;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | //
|
---|
| 50 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 51 | void bncNetQueryRtp::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | //
|
---|
| 55 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 56 | void bncNetQueryRtp::waitForReadyRead(QByteArray& outData) {
|
---|
[1414] | 57 |
|
---|
| 58 | // Wait Loop
|
---|
| 59 | // ---------
|
---|
| 60 | if (!_udpSocket->hasPendingDatagrams()) {
|
---|
| 61 | _eventLoop->exec();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | // Append Data
|
---|
| 65 | // -----------
|
---|
| 66 | QByteArray datagram;
|
---|
| 67 | datagram.resize(_udpSocket->pendingDatagramSize());
|
---|
| 68 | _udpSocket->readDatagram(datagram.data(), datagram.size());
|
---|
[1415] | 69 |
|
---|
[1416] | 70 | if (datagram.size() > 12) {
|
---|
| 71 | outData.append(datagram.mid(12));
|
---|
| 72 | }
|
---|
[1410] | 73 | }
|
---|
| 74 |
|
---|
| 75 | // Connect to Caster, send the Request
|
---|
| 76 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 77 | void bncNetQueryRtp::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
| 78 |
|
---|
| 79 | const int timeOut = 5000;
|
---|
| 80 |
|
---|
| 81 | _status = running;
|
---|
| 82 |
|
---|
| 83 | delete _socket;
|
---|
| 84 | _socket = new QTcpSocket();
|
---|
| 85 |
|
---|
[1411] | 86 | // Default scheme
|
---|
| 87 | // --------------
|
---|
[1410] | 88 | QUrl urlLoc(url);
|
---|
[1411] | 89 | urlLoc.setScheme("rtsp");
|
---|
[1410] | 90 |
|
---|
| 91 | // Connect the Socket
|
---|
| 92 | // ------------------
|
---|
| 93 | QSettings settings;
|
---|
| 94 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 95 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
| 96 |
|
---|
| 97 | if ( proxyHost.isEmpty() ) {
|
---|
| 98 | _socket->connectToHost(urlLoc.host(), urlLoc.port());
|
---|
| 99 | }
|
---|
| 100 | else {
|
---|
| 101 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[1411] | 104 | // Send Request 1
|
---|
| 105 | // --------------
|
---|
| 106 | if (_socket->waitForConnected(timeOut)) {
|
---|
| 107 | QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
|
---|
| 108 | QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
|
---|
| 109 | QByteArray userAndPwd;
|
---|
| 110 |
|
---|
| 111 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
| 112 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
| 113 | passW.toAscii()).toBase64() + "\r\n";
|
---|
| 114 | }
|
---|
[1418] | 115 |
|
---|
| 116 | // Find a free port number
|
---|
| 117 | // -----------------------
|
---|
| 118 | int clientPortInt = 0;
|
---|
| 119 | QTcpSocket* hlpSocket = new QTcpSocket();
|
---|
| 120 | if ( proxyHost.isEmpty() ) {
|
---|
| 121 | hlpSocket->connectToHost(urlLoc.host(), urlLoc.port());
|
---|
| 122 | }
|
---|
| 123 | else {
|
---|
| 124 | hlpSocket->connectToHost(proxyHost, proxyPort);
|
---|
| 125 | }
|
---|
| 126 | if (hlpSocket->waitForConnected(timeOut)) {
|
---|
| 127 | clientPortInt = hlpSocket->localPort();
|
---|
| 128 | }
|
---|
| 129 | delete hlpSocket;
|
---|
[1419] | 130 |
|
---|
| 131 | // Setup the RTSP Connection
|
---|
| 132 | // -------------------------
|
---|
| 133 | if (clientPortInt) {
|
---|
| 134 | QByteArray clientPort = QString("%1").arg(clientPortInt).toAscii();
|
---|
| 135 | delete _udpSocket;
|
---|
| 136 | _udpSocket = new QUdpSocket();
|
---|
| 137 | _udpSocket->bind(clientPortInt);
|
---|
| 138 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
| 139 |
|
---|
| 140 | QByteArray reqStr;
|
---|
| 141 | reqStr = "SETUP " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
|
---|
[1463] | 142 | + "CSeq: 1\r\n"
|
---|
[1419] | 143 | + "Ntrip-Version: Ntrip/2.0\r\n"
|
---|
| 144 | + "Ntrip-Component: Ntripclient\r\n"
|
---|
| 145 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
| 146 | + "Transport: RTP/GNSS;unicast;client_port=" + clientPort + "\r\n"
|
---|
| 147 | + userAndPwd
|
---|
| 148 | + "\r\n";
|
---|
| 149 | _socket->write(reqStr, reqStr.length());
|
---|
| 150 |
|
---|
| 151 | // Read Server Answer 1
|
---|
| 152 | // --------------------
|
---|
| 153 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
| 154 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
| 155 | QTextStream in(_socket);
|
---|
| 156 | QByteArray session;
|
---|
| 157 | QString line = in.readLine();
|
---|
| 158 | while (!line.isEmpty()) {
|
---|
| 159 | if (line.indexOf("Session:") == 0) {
|
---|
| 160 | session = line.mid(9).toAscii();
|
---|
| 161 | break;
|
---|
| 162 | }
|
---|
| 163 | line = in.readLine();
|
---|
[1411] | 164 | }
|
---|
[1419] | 165 |
|
---|
| 166 | // Send Request 2
|
---|
| 167 | // --------------
|
---|
| 168 | if (!session.isEmpty()) {
|
---|
| 169 | reqStr = "PLAY " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
|
---|
[1463] | 170 | + "CSeq: 2\r\n"
|
---|
[1419] | 171 | + "Session: " + session + "\r\n"
|
---|
| 172 | + "\r\n";
|
---|
| 173 | _socket->write(reqStr, reqStr.length());
|
---|
| 174 |
|
---|
| 175 | // Read Server Answer 2
|
---|
| 176 | // --------------------
|
---|
| 177 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
| 178 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
| 179 | QTextStream in(_socket);
|
---|
| 180 | line = in.readLine();
|
---|
| 181 | while (!line.isEmpty()) {
|
---|
| 182 | if (line.indexOf("200 OK") != -1) {
|
---|
[1464] | 183 | emit newMessage("NetQuery: UDP connection established", true);
|
---|
[1419] | 184 | return;
|
---|
| 185 | }
|
---|
| 186 | line = in.readLine();
|
---|
[1411] | 187 | }
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
[1410] | 194 | }
|
---|
| 195 |
|
---|
[1411] | 196 | delete _socket;
|
---|
| 197 | _socket = 0;
|
---|
| 198 | _status = error;
|
---|
[1410] | 199 | }
|
---|
| 200 |
|
---|