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 | }
|
---|
32 |
|
---|
33 | // Destructor
|
---|
34 | ////////////////////////////////////////////////////////////////////////////
|
---|
35 | bncNetQueryRtp::~bncNetQueryRtp() {
|
---|
36 | delete _socket;
|
---|
37 | delete _udpSocket;
|
---|
38 | }
|
---|
39 |
|
---|
40 | //
|
---|
41 | ////////////////////////////////////////////////////////////////////////////
|
---|
42 | void bncNetQueryRtp::stop() {
|
---|
43 | #ifndef sparc
|
---|
44 | if (_socket) {
|
---|
45 | _socket->abort();
|
---|
46 | }
|
---|
47 | #endif
|
---|
48 | _status = finished;
|
---|
49 | }
|
---|
50 |
|
---|
51 | //
|
---|
52 | ////////////////////////////////////////////////////////////////////////////
|
---|
53 | void bncNetQueryRtp::waitForRequestResult(const QUrl&, QByteArray&) {
|
---|
54 | }
|
---|
55 |
|
---|
56 | //
|
---|
57 | ////////////////////////////////////////////////////////////////////////////
|
---|
58 | void bncNetQueryRtp::waitForReadyRead(QByteArray& outData) {
|
---|
59 | }
|
---|
60 |
|
---|
61 | // Connect to Caster, send the Request
|
---|
62 | ////////////////////////////////////////////////////////////////////////////
|
---|
63 | void bncNetQueryRtp::startRequest(const QUrl& url, const QByteArray& gga) {
|
---|
64 |
|
---|
65 | const int timeOut = 5000;
|
---|
66 |
|
---|
67 | _status = running;
|
---|
68 |
|
---|
69 | delete _socket;
|
---|
70 | _socket = new QTcpSocket();
|
---|
71 |
|
---|
72 | // Default scheme
|
---|
73 | // --------------
|
---|
74 | QUrl urlLoc(url);
|
---|
75 | urlLoc.setScheme("rtsp");
|
---|
76 |
|
---|
77 | // Connect the Socket
|
---|
78 | // ------------------
|
---|
79 | QSettings settings;
|
---|
80 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
81 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
82 |
|
---|
83 | if ( proxyHost.isEmpty() ) {
|
---|
84 | _socket->connectToHost(urlLoc.host(), urlLoc.port());
|
---|
85 | }
|
---|
86 | else {
|
---|
87 | _socket->connectToHost(proxyHost, proxyPort);
|
---|
88 | }
|
---|
89 |
|
---|
90 | // Send Request 1
|
---|
91 | // --------------
|
---|
92 | if (_socket->waitForConnected(timeOut)) {
|
---|
93 | QString uName = QUrl::fromPercentEncoding(urlLoc.userName().toAscii());
|
---|
94 | QString passW = QUrl::fromPercentEncoding(urlLoc.password().toAscii());
|
---|
95 | QByteArray userAndPwd;
|
---|
96 |
|
---|
97 | if(!uName.isEmpty() || !passW.isEmpty()) {
|
---|
98 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
99 | passW.toAscii()).toBase64() + "\r\n";
|
---|
100 | }
|
---|
101 |
|
---|
102 | QByteArray clientPort = "7777"; // TODO: make it an option
|
---|
103 | delete _udpSocket;
|
---|
104 | _udpSocket = new QUdpSocket();
|
---|
105 | _udpSocket->bind(clientPort.toInt());
|
---|
106 |
|
---|
107 | QByteArray reqStr;
|
---|
108 | reqStr = "SETUP " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
|
---|
109 | + "Cseq: 1\r\n"
|
---|
110 | + "Ntrip-Version: Ntrip/2.0\r\n"
|
---|
111 | + "Ntrip-Component: Ntripclient\r\n"
|
---|
112 | + "User-Agent: NTRIP BNC/" BNCVERSION "\r\n"
|
---|
113 | + "Transport: RTP/GNSS;unicast;client_port=" + clientPort + "\r\n"
|
---|
114 | + userAndPwd
|
---|
115 | + "\r\n";
|
---|
116 | _socket->write(reqStr, reqStr.length());
|
---|
117 |
|
---|
118 | // Read Server Answer 1
|
---|
119 | // --------------------
|
---|
120 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
121 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
122 | QTextStream in(_socket);
|
---|
123 | QByteArray session;
|
---|
124 | QString line = in.readLine();
|
---|
125 | while (!line.isEmpty()) {
|
---|
126 | if (line.indexOf("Session:") == 0) {
|
---|
127 | session = line.mid(9).toAscii();
|
---|
128 | break;
|
---|
129 | }
|
---|
130 | line = in.readLine();
|
---|
131 | }
|
---|
132 |
|
---|
133 | // Send Request 2
|
---|
134 | // --------------
|
---|
135 | if (!session.isEmpty()) {
|
---|
136 | reqStr = "PLAY " + urlLoc.toEncoded() + " RTSP/1.0\r\n"
|
---|
137 | + "Cseq: 2\r\n"
|
---|
138 | + "Session: " + session + "\r\n"
|
---|
139 | + "\r\n";
|
---|
140 | _socket->write(reqStr, reqStr.length());
|
---|
141 |
|
---|
142 | // Read Server Answer 2
|
---|
143 | // --------------------
|
---|
144 | if (_socket->waitForBytesWritten(timeOut)) {
|
---|
145 | if (_socket->waitForReadyRead(timeOut)) {
|
---|
146 | QTextStream in(_socket);
|
---|
147 | line = in.readLine();
|
---|
148 | while (!line.isEmpty()) {
|
---|
149 | if (line.indexOf("200 OK") != -1) {
|
---|
150 | cout << "Connection Established" << endl;
|
---|
151 | return;
|
---|
152 | }
|
---|
153 | line = in.readLine();
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | delete _socket;
|
---|
163 | _socket = 0;
|
---|
164 | _status = error;
|
---|
165 | }
|
---|
166 |
|
---|