-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamProtocolHandler.cpp
More file actions
349 lines (285 loc) · 8.73 KB
/
StreamProtocolHandler.cpp
File metadata and controls
349 lines (285 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "StreamProtocolHandler.h"
#include "NetworkConfig.h"
#include <QNetworkRequest>
#include <QNetworkReply>
// StreamProtocolHandler 基类实现
StreamProtocolHandler::StreamProtocolHandler(QObject *parent)
: QObject(parent)
, m_isConnected(false)
, m_protocolType(UNKNOWN_PROTOCOL)
{
}
StreamProtocolHandler::ProtocolType StreamProtocolHandler::detectProtocol(const QString &url)
{
QUrl qurl(url);
QString scheme = qurl.scheme().toLower();
if (scheme == "http") {
return HTTP_PROTOCOL;
} else if (scheme == "https") {
return HTTPS_PROTOCOL;
} else if (scheme == "rtmp") {
return RTMP_PROTOCOL;
} else if (scheme == "rtsp") {
return RTSP_PROTOCOL;
} else if (scheme == "udp") {
return UDP_PROTOCOL;
} else if (scheme == "tcp") {
return TCP_PROTOCOL;
}
return UNKNOWN_PROTOCOL;
}
QString StreamProtocolHandler::protocolToString(ProtocolType protocol)
{
switch (protocol) {
case HTTP_PROTOCOL: return "HTTP";
case HTTPS_PROTOCOL: return "HTTPS";
case RTMP_PROTOCOL: return "RTMP";
case RTSP_PROTOCOL: return "RTSP";
case UDP_PROTOCOL: return "UDP";
case TCP_PROTOCOL: return "TCP";
default: return "Unknown";
}
}
StreamProtocolHandler* StreamProtocolHandler::createHandler(ProtocolType protocol, QObject *parent)
{
switch (protocol) {
case HTTP_PROTOCOL:
case HTTPS_PROTOCOL:
return new HttpStreamHandler(parent);
case RTMP_PROTOCOL:
return new RtmpStreamHandler(parent);
case RTSP_PROTOCOL:
return new RtspStreamHandler(parent);
case UDP_PROTOCOL:
case TCP_PROTOCOL:
// 可以在这里添加UDP/TCP处理器
return nullptr;
default:
return nullptr;
}
}
bool StreamProtocolHandler::validateUrl(const QString &url) const
{
QUrl qurl(url);
return qurl.isValid() && !qurl.scheme().isEmpty();
}
QString StreamProtocolHandler::normalizeUrl(const QString &url) const
{
QUrl qurl(url);
return qurl.toString();
}
int StreamProtocolHandler::getDefaultPort() const
{
return 80; // 默认端口
}
void StreamProtocolHandler::setCommonOptions(AVDictionary** options, const NetworkConfig &config)
{
if (!options) return;
// 设置超时
av_dict_set_int(options, "timeout", config.connectionTimeout * 1000, 0);
// 设置用户代理
if (!config.userAgent.isEmpty()) {
av_dict_set(options, "user_agent", config.userAgent.toUtf8().constData(), 0);
}
// 设置引用页面
if (!config.referer.isEmpty()) {
av_dict_set(options, "referer", config.referer.toUtf8().constData(), 0);
}
// 设置重定向
if (config.followRedirects) {
av_dict_set_int(options, "followlocation", 1, 0);
av_dict_set_int(options, "maxredirs", config.maxRedirects, 0);
}
// 设置缓冲区大小
av_dict_set_int(options, "buffer_size", config.bufferSize, 0);
}
bool StreamProtocolHandler::isSecureProtocol() const
{
return false; // 基类默认不安全
}
// HttpStreamHandler 实现
HttpStreamHandler::HttpStreamHandler(QObject *parent)
: StreamProtocolHandler(parent)
, m_networkManager(new QNetworkAccessManager(this))
, m_currentReply(nullptr)
{
m_protocolType = HTTP_PROTOCOL;
connect(m_networkManager, &QNetworkAccessManager::finished,
this, &HttpStreamHandler::handleNetworkReply);
}
HttpStreamHandler::~HttpStreamHandler()
{
disconnectFromStream();
}
bool HttpStreamHandler::connectToStream(const QString &url, const NetworkConfig &config)
{
if (!validateUrl(url)) {
emit connectionError("Invalid URL");
return false;
}
disconnectFromStream(); // 断开现有连接
m_currentUrl = normalizeUrl(url);
QNetworkRequest request(m_currentUrl);
// 设置请求头
if (!config.userAgent.isEmpty()) {
request.setRawHeader("User-Agent", config.userAgent.toUtf8());
}
if (!config.referer.isEmpty()) {
request.setRawHeader("Referer", config.referer.toUtf8());
}
// 发起连接
m_currentReply = m_networkManager->get(request);
if (!m_currentReply) {
emit connectionError("Failed to create network request");
return false;
}
return true;
}
void HttpStreamHandler::disconnectFromStream()
{
if (m_currentReply) {
m_currentReply->abort();
m_currentReply->deleteLater();
m_currentReply = nullptr;
}
m_isConnected = false;
m_currentUrl.clear();
}
bool HttpStreamHandler::isConnected() const
{
return m_isConnected && m_currentReply;
}
bool HttpStreamHandler::isSecureProtocol() const
{
return m_protocolType == HTTPS_PROTOCOL;
}
void HttpStreamHandler::handleNetworkReply()
{
if (!m_currentReply) {
return;
}
if (m_currentReply->error() == QNetworkReply::NoError) {
m_isConnected = true;
emit connectionEstablished();
} else {
emit connectionError(m_currentReply->errorString());
}
m_currentReply->deleteLater();
m_currentReply = nullptr;
}
// RtmpStreamHandler 实现
RtmpStreamHandler::RtmpStreamHandler(QObject *parent)
: StreamProtocolHandler(parent)
, m_formatContext(nullptr)
{
m_protocolType = RTMP_PROTOCOL;
}
RtmpStreamHandler::~RtmpStreamHandler()
{
disconnectFromStream();
}
bool RtmpStreamHandler::connectToStream(const QString &url, const NetworkConfig &config)
{
if (!validateUrl(url)) {
emit connectionError("Invalid RTMP URL");
return false;
}
disconnectFromStream();
m_currentUrl = normalizeUrl(url);
// 分配格式上下文
m_formatContext = avformat_alloc_context();
if (!m_formatContext) {
emit connectionError("Failed to allocate format context");
return false;
}
// 设置选项
AVDictionary* options = nullptr;
setCommonOptions(&options, config);
// 尝试打开RTMP流
int ret = avformat_open_input(&m_formatContext, m_currentUrl.toUtf8().constData(), nullptr, &options);
av_dict_free(&options);
if (ret < 0) {
emit connectionError("Failed to open RTMP stream");
avformat_free_context(m_formatContext);
m_formatContext = nullptr;
return false;
}
m_isConnected = true;
emit connectionEstablished();
return true;
}
void RtmpStreamHandler::disconnectFromStream()
{
if (m_formatContext) {
avformat_close_input(&m_formatContext);
m_formatContext = nullptr;
}
m_isConnected = false;
m_currentUrl.clear();
}
bool RtmpStreamHandler::isConnected() const
{
return m_isConnected && m_formatContext;
}
int RtmpStreamHandler::getDefaultPort() const
{
return 1935; // RTMP默认端口
}
// RtspStreamHandler 实现
RtspStreamHandler::RtspStreamHandler(QObject *parent)
: StreamProtocolHandler(parent)
, m_formatContext(nullptr)
{
m_protocolType = RTSP_PROTOCOL;
}
RtspStreamHandler::~RtspStreamHandler()
{
disconnectFromStream();
}
bool RtspStreamHandler::connectToStream(const QString &url, const NetworkConfig &config)
{
if (!validateUrl(url)) {
emit connectionError("Invalid RTSP URL");
return false;
}
disconnectFromStream();
m_currentUrl = normalizeUrl(url);
// 分配格式上下文
m_formatContext = avformat_alloc_context();
if (!m_formatContext) {
emit connectionError("Failed to allocate format context");
return false;
}
// 设置选项
AVDictionary* options = nullptr;
setCommonOptions(&options, config);
// 尝试打开RTSP流
int ret = avformat_open_input(&m_formatContext, m_currentUrl.toUtf8().constData(), nullptr, &options);
av_dict_free(&options);
if (ret < 0) {
emit connectionError("Failed to open RTSP stream");
avformat_free_context(m_formatContext);
m_formatContext = nullptr;
return false;
}
m_isConnected = true;
emit connectionEstablished();
return true;
}
void RtspStreamHandler::disconnectFromStream()
{
if (m_formatContext) {
avformat_close_input(&m_formatContext);
m_formatContext = nullptr;
}
m_isConnected = false;
m_currentUrl.clear();
}
bool RtspStreamHandler::isConnected() const
{
return m_isConnected && m_formatContext;
}
int RtspStreamHandler::getDefaultPort() const
{
return 554; // RTSP默认端口
}