1 | /* -------------------------------------------------------------------------
|
---|
2 | * BKG NTRIP Client
|
---|
3 | * -------------------------------------------------------------------------
|
---|
4 | *
|
---|
5 | * Class: bncNetQueryRtp
|
---|
6 | *
|
---|
7 | * Purpose: Blocking Network Requests (NTRIP Version 2 with RTSP)
|
---|
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() {
|
---|
29 | _socket = 0;
|
---|
30 | _udpSocket = 0;
|
---|
31 | _eventLoop = new QEventLoop(this);
|
---|
32 | }
|
---|
33 |
|
---|
34 | // Destructor
|
---|
35 | ////////////////////////////////////////////////////////////////////////////
|
---|
36 | bncNetQueryRtp::~bncNetQueryRtp() {
|
---|
37 | delete _eventLoop;
|
---|
38 | delete _socket;
|
---|
39 | delete _udpSocket;
|
---|
40 | }
|
---|
41 |
|
---|
42 | //
|
---|
43 | ////////////////////////////////////////////////////////////////////////////
|
---|
44 | void bncNetQueryRtp::stop() {
|
---|
45 | _eventLoop->quit();
|
---|
46 | _status = finished;
|
---|
47 | }
|
---|
48 |
|
---|
49 | //
|
---|
50 | ////////////////////////////////////////////////////////////////////////////
|
---|
51 | void bncNetQueryRtp::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
52 | }
|
---|
53 |
|
---|
54 | //
|
---|
55 | ////////////////////////////////////////////////////////////////////////////
|
---|
56 | void bncNetQueryRtp::waitForReadyRead(QByteArray& outData) {
|
---|
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());
|
---|
69 |
|
---|
70 | if (datagram.size() > 12) {
|
---|
71 | outData.append(datagram.mid(12));
|
---|
72 | }
|
---|
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 |
|
---|
86 | // Default scheme
|
---|
87 | // --------------
|
---|
88 | QUrl urlLoc(url);
|
---|
89 | urlLoc.setScheme("rtsp");
|
---|
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 |
|
---|
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 | }
|
---|
115 |
|
---|
116 | // Setup the RTSP Connection
|
---|
117 | // -------------------------
|
---|
118 | delete _udpSocket;
|
---|
119 | _udpSocket = new QUdpSocket();
|
---|
120 | _udpSocket->bind(0);
|
---|
121 | connect(_udpSocket, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
---|
122 | QByteArray clientPort = QString("%1").arg(_udpSocket->localPort()).toAscii();
|
---|
123 |
|
---|
124 | QByteArray reqStr;
|
---|
125 | reqStr = "SETUP " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
|
---|
126 | + "CSeq: 1\r\n"
|
---|
127 | + "Ntrip-Version: Ntrip/2.0\r\n"
|
---|
128 | + "Ntrip-Component: Ntripclient\r\n"
|
---|
129 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
130 | + "Transport: RTP/GNSS;unicast;client_port=" + clientPort + "\r\n"
|
---|
131 | + userAndPwd
|
---|
132 | + "\r\n";
|
---|
133 | _socket->write(reqStr, reqStr.length());
|
---|
134 |
|
---|
135 | // Read Server Answer 1
|
---|
136 | // --------------------
|
---|
137 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
138 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
139 | QTextStream in(_socket);
|
---|
140 | QByteArray session;
|
---|
141 | QByteArray serverPort;
|
---|
142 | QString line = in.readLine();
|
---|
143 | while (!line.isEmpty()) {
|
---|
144 | if (line.indexOf("Session:") == 0) {
|
---|
145 | session = line.mid(9).toAscii();
|
---|
146 | }
|
---|
147 | int iSrv = line.indexOf("server_port=");
|
---|
148 | if (iSrv != -1) {
|
---|
149 | serverPort = line.mid(iSrv+12).toAscii();
|
---|
150 | }
|
---|
151 | line = in.readLine();
|
---|
152 | }
|
---|
153 |
|
---|
154 | // Send Request 2
|
---|
155 | // --------------
|
---|
156 | if (!session.isEmpty()) {
|
---|
157 |
|
---|
158 | // Send initial RTP packet for firewall handling
|
---|
159 | // ---------------------------------------------
|
---|
160 | if (!serverPort.isEmpty()) {
|
---|
161 | int sessInt = session.toInt();
|
---|
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 |
|
---|
176 | int irc = _udpSocket->writeDatagram(rtpbuffer, 12,
|
---|
177 | _socket->peerAddress(), serverPort.toInt());
|
---|
178 | cout << "irc = " << irc << endl;
|
---|
179 | }
|
---|
180 |
|
---|
181 | reqStr = "PLAY " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
|
---|
182 | + "CSeq: 2\r\n"
|
---|
183 | + "Session: " + session + "\r\n"
|
---|
184 | + "\r\n";
|
---|
185 | _socket->write(reqStr, reqStr.length());
|
---|
186 |
|
---|
187 | // Read Server Answer 2
|
---|
188 | // --------------------
|
---|
189 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
190 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
191 | QTextStream in(_socket);
|
---|
192 | line = in.readLine();
|
---|
193 | while (!line.isEmpty()) {
|
---|
194 | if (line.indexOf("200 OK") != -1) {
|
---|
195 | emit newMessage(urlLoc.host().toAscii() +
|
---|
196 | urlLoc.path().toAscii() +
|
---|
197 | ": UDP connection established", true);
|
---|
198 | return;
|
---|
199 | }
|
---|
200 | line = in.readLine();
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | delete _socket;
|
---|
210 | _socket = 0;
|
---|
211 | _status = error;
|
---|
212 | }
|
---|
213 |
|
---|