-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkStreamManager.cpp
More file actions
264 lines (220 loc) · 6.94 KB
/
NetworkStreamManager.cpp
File metadata and controls
264 lines (220 loc) · 6.94 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
#include "NetworkStreamManager.h"
#include "NetworkConfig.h"
#include "StreamProtocolHandler.h"
#include <QDebug>
#include <QTime>
#include <QNetworkRequest>
NetworkStreamManager::NetworkStreamManager(QObject *parent)
: QObject(parent)
, m_status(Disconnected)
, m_protocol(Unknown)
, m_formatContext(nullptr)
, m_config(new NetworkConfig(NetworkConfig::defaultConfig()))
, m_protocolHandler(nullptr)
, m_connectionTimer(new QTimer(this))
, m_statusTimer(new QTimer(this))
, m_networkManager(new QNetworkAccessManager(this))
, m_currentReply(nullptr)
, m_bufferSize(0)
, m_connectionLatency(0)
{
// 设置连接超时定时器
m_connectionTimer->setSingleShot(true);
connect(m_connectionTimer, &QTimer::timeout, this, &NetworkStreamManager::handleConnectionTimeout);
// 设置状态更新定时器
m_statusTimer->setInterval(1000); // 每秒更新一次状态
connect(m_statusTimer, &QTimer::timeout, this, &NetworkStreamManager::updateConnectionStatus);
// 网络管理器信号连接
connect(m_networkManager, &QNetworkAccessManager::finished, this, &NetworkStreamManager::handleNetworkReply);
}
NetworkStreamManager::~NetworkStreamManager()
{
disconnectStream();
delete m_config;
if (m_protocolHandler) {
delete m_protocolHandler;
}
}
bool NetworkStreamManager::connectToStream(const QString &url)
{
if (url.isEmpty()) {
emit streamError("Empty URL provided");
return false;
}
// 断开现有连接
disconnectStream();
m_currentUrl = url;
m_status = Connecting;
emit statusChanged();
// 检测协议类型
m_protocol = detectProtocol(url);
// 创建协议处理器
StreamProtocolHandler::ProtocolType protocolType =
static_cast<StreamProtocolHandler::ProtocolType>(m_protocol);
m_protocolHandler = StreamProtocolHandler::createHandler(protocolType, this);
if (!m_protocolHandler) {
emit streamError("Unsupported protocol");
return false;
}
// 连接协议处理器信号
connect(m_protocolHandler, &StreamProtocolHandler::connectionProgress,
this, &NetworkStreamManager::connectionProgress);
// 连接错误信号
connect(m_protocolHandler, &StreamProtocolHandler::connectionError,
this, &NetworkStreamManager::streamError);
// 开始连接
m_connectionTimer->start(m_config->connectionTimeout);
m_statusTimer->start();
// 这里可以添加实际的连接逻辑
// 为了演示,我们模拟一个成功的连接
QTimer::singleShot(1000, this, [this]() {
m_status = Connected;
emit streamConnected();
emit statusChanged();
});
return true;
}
void NetworkStreamManager::disconnectStream()
{
if (m_status == Disconnected) {
return;
}
m_connectionTimer->stop();
m_statusTimer->stop();
if (m_currentReply) {
m_currentReply->abort();
m_currentReply->deleteLater();
m_currentReply = nullptr;
}
if (m_protocolHandler) {
m_protocolHandler->disconnectFromStream();
delete m_protocolHandler;
m_protocolHandler = nullptr;
}
cleanupFFmpegContext();
m_status = Disconnected;
m_currentUrl.clear();
m_bufferSize = 0;
m_connectionLatency = 0;
emit streamDisconnected();
emit statusChanged();
}
void NetworkStreamManager::reconnect()
{
if (!m_currentUrl.isEmpty()) {
connectToStream(m_currentUrl);
}
}
bool NetworkStreamManager::isConnected() const
{
return m_status == Connected;
}
NetworkStreamManager::StreamStatus NetworkStreamManager::getStatus() const
{
return m_status;
}
QString NetworkStreamManager::getStatusText() const
{
switch (m_status) {
case Disconnected: return "未连接";
case Connecting: return "连接中...";
case Connected: return "已连接";
case Buffering: return "缓冲中...";
case Error: return "连接错误";
default: return "未知状态";
}
}
NetworkStreamManager::StreamProtocol NetworkStreamManager::getProtocol() const
{
return m_protocol;
}
void NetworkStreamManager::setNetworkConfig(const NetworkConfig &config)
{
*m_config = config;
}
NetworkConfig NetworkStreamManager::getNetworkConfig() const
{
return *m_config;
}
QString NetworkStreamManager::getCurrentUrl() const
{
return m_currentUrl;
}
qint64 NetworkStreamManager::getBufferSize() const
{
return m_bufferSize;
}
int NetworkStreamManager::getConnectionLatency() const
{
return m_connectionLatency;
}
AVFormatContext* NetworkStreamManager::getFormatContext() const
{
return m_formatContext;
}
void NetworkStreamManager::handleConnectionTimeout()
{
m_status = Error;
emit streamError("Connection timeout");
emit statusChanged();
}
void NetworkStreamManager::updateConnectionStatus()
{
// 更新连接状态和缓冲区信息
if (m_status == Connected) {
// 模拟缓冲区状态更新
static int bufferLevel = 0;
bufferLevel = (bufferLevel + 10) % 100;
emit bufferStatusChanged(bufferLevel);
}
}
void NetworkStreamManager::handleNetworkReply()
{
if (!m_currentReply) {
return;
}
if (m_currentReply->error() != QNetworkReply::NoError) {
emit streamError(m_currentReply->errorString());
}
m_currentReply->deleteLater();
m_currentReply = nullptr;
}
NetworkStreamManager::StreamProtocol NetworkStreamManager::detectProtocol(const QString &url)
{
StreamProtocolHandler::ProtocolType type = StreamProtocolHandler::detectProtocol(url);
switch (type) {
case StreamProtocolHandler::HTTP_PROTOCOL: return HTTP;
case StreamProtocolHandler::HTTPS_PROTOCOL: return HTTPS;
case StreamProtocolHandler::RTMP_PROTOCOL: return RTMP;
case StreamProtocolHandler::RTSP_PROTOCOL: return RTSP;
case StreamProtocolHandler::UDP_PROTOCOL: return UDP;
case StreamProtocolHandler::TCP_PROTOCOL: return TCP;
default: return Unknown;
}
}
bool NetworkStreamManager::setupFFmpegContext()
{
// FFmpeg 上下文设置
m_formatContext = avformat_alloc_context();
return m_formatContext != nullptr;
}
void NetworkStreamManager::cleanupFFmpegContext()
{
if (m_formatContext) {
avformat_free_context(m_formatContext);
m_formatContext = nullptr;
}
}
void NetworkStreamManager::resetConnection()
{
disconnectStream();
// 可以在这里添加重置逻辑
}
void NetworkStreamManager::updateLatency()
{
// 更新连接延迟
static QTime lastUpdate = QTime::currentTime();
QTime now = QTime::currentTime();
m_connectionLatency = lastUpdate.msecsTo(now);
lastUpdate = now;
}