source: ntrip/trunk/BNC/bncnetqueryrtp.cpp@ 1858

Last change on this file since 1858 was 1818, checked in by mervart, 16 years ago

* empty log message *

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