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 | _CSeq = 0;
|
---|
32 | _eventLoop = new QEventLoop(this);
|
---|
33 | }
|
---|
34 |
|
---|
35 | // Destructor
|
---|
36 | ////////////////////////////////////////////////////////////////////////////
|
---|
37 | bncNetQueryRtp::~bncNetQueryRtp() {
|
---|
38 | delete _eventLoop;
|
---|
39 | delete _socket;
|
---|
40 | delete _udpSocket;
|
---|
41 | }
|
---|
42 |
|
---|
43 | //
|
---|
44 | ////////////////////////////////////////////////////////////////////////////
|
---|
45 | void bncNetQueryRtp::stop() {
|
---|
46 | _eventLoop->quit();
|
---|
47 | _status = finished;
|
---|
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());
|
---|
53 | }
|
---|
54 |
|
---|
55 | //
|
---|
56 | ////////////////////////////////////////////////////////////////////////////
|
---|
57 | void bncNetQueryRtp::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
58 | }
|
---|
59 |
|
---|
60 | //
|
---|
61 | ////////////////////////////////////////////////////////////////////////////
|
---|
62 | void bncNetQueryRtp::waitForReadyRead(QByteArray& outData) {
|
---|
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());
|
---|
75 |
|
---|
76 | if (datagram.size() > 12) {
|
---|
77 | outData.append(datagram.mid(12));
|
---|
78 | }
|
---|
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 |
|
---|
92 | // Default scheme
|
---|
93 | // --------------
|
---|
94 | _url = url;
|
---|
95 | _url.setScheme("rtsp");
|
---|
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() ) {
|
---|
104 | _socket->connectToHost(_url.host(), _url.port());
|
---|
105 | }
|
---|
106 | else {
|
---|
107 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
108 | }
|
---|
109 |
|
---|
110 | // Send Request 1
|
---|
111 | // --------------
|
---|
112 | if (_socket->waitForConnected(timeOut)) {
|
---|
113 | QString uName = QUrl::fromPercentEncoding(_url.userName().toAscii());
|
---|
114 | QString passW = QUrl::fromPercentEncoding(_url.password().toAscii());
|
---|
115 | QByteArray userAndPwd;
|
---|
116 |
|
---|
117 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
118 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
119 | passW.toAscii()).toBase64() + "\r\n";
|
---|
120 | }
|
---|
121 |
|
---|
122 | // Setup the RTSP Connection
|
---|
123 | // -------------------------
|
---|
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;
|
---|
131 | reqStr = "SETUP " + _url.toEncoded() + " RTSP/1.0\r\n"
|
---|
132 | + "CSeq: " + QString("%1").arg(++_CSeq).toAscii() + "\r\n"
|
---|
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);
|
---|
146 | QByteArray serverPort;
|
---|
147 | QString line = in.readLine();
|
---|
148 | while (!line.isEmpty()) {
|
---|
149 | if (line.indexOf("Session:") == 0) {
|
---|
150 | _session = line.mid(9).toAscii();
|
---|
151 | }
|
---|
152 | int iSrv = line.indexOf("server_port=");
|
---|
153 | if (iSrv != -1) {
|
---|
154 | serverPort = line.mid(iSrv+12).toAscii();
|
---|
155 | }
|
---|
156 | line = in.readLine();
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Send Request 2
|
---|
160 | // --------------
|
---|
161 | if (!_session.isEmpty()) {
|
---|
162 |
|
---|
163 | // Send initial RTP packet for firewall handling
|
---|
164 | // ---------------------------------------------
|
---|
165 | if (!serverPort.isEmpty()) {
|
---|
166 | int sessInt = _session.toInt();
|
---|
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 |
|
---|
181 | _udpSocket->writeDatagram(rtpbuffer, 12,
|
---|
182 | _socket->peerAddress(), serverPort.toInt());
|
---|
183 | }
|
---|
184 |
|
---|
185 | reqStr = "PLAY " + _url.toEncoded() + " RTSP/1.0\r\n"
|
---|
186 | + "CSeq: " + QString("%1").arg(++_CSeq).toAscii() + "\r\n"
|
---|
187 | + "Session: " + _session + "\r\n"
|
---|
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) {
|
---|
199 | emit newMessage(_url.host().toAscii() +
|
---|
200 | _url.path().toAscii() +
|
---|
201 | ": UDP connection established", true);
|
---|
202 | return;
|
---|
203 | }
|
---|
204 | line = in.readLine();
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | delete _socket;
|
---|
214 | _socket = 0;
|
---|
215 | _status = error;
|
---|
216 | }
|
---|
217 |
|
---|