-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysessionmanager.cpp
336 lines (294 loc) · 9.19 KB
/
mysessionmanager.cpp
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
#include "mysessionmanager.h"
#include "Config.h"
#include "QMessageBox"
#include <QtCore>
#include "MyUDPPacketBuilder.h"
#include "mainwindow.h"
extern Config* cfg;
MySessionManager::MySessionManager(QWidget* parent):
stat(0),
sessionID(0),
m_udpSocket(nullptr),
init(false),
video_enc_thread(nullptr),
m_pushTimer(new QTimer(this)),
cancelBox(nullptr),
video_dec_thread(nullptr),
audio_dec_thread(nullptr),
audio_enc_thread(nullptr),
last_time_rec(0),
parent(parent)
{
uh = new UdpHanlder(m_udpSocket, cfg->target_ip, cfg->target_port);
connect(m_pushTimer, &QTimer::timeout, this, [=]()mutable{
if(getStat() != 3) return;
auto cur_time = getCurTime();
// 10s没数据就关闭
if(last_time_rec != 0 && cur_time - last_time_rec > 10000){
stopSession(true);
QMessageBox::warning(nullptr, "提示", "超时,已断开会话");
}
}, Qt::DirectConnection);
m_pushTimer->start(500);
// CamMicLoader*& camloader = cfg->CML;
// if(!camloader){
// camloader = new CamMicLoader(cfg->w,cfg->h);
// auto v_device = cfg->cur_cam;
// auto a_device = cfg->cur_mic;
// int tt = camloader->initLoader(v_device, a_device);
// camloader->StartCapture();
// }
// audio_enc_thread = new AudioEncodeThread(this);
// audio_enc_thread->start();
// audio_dec_thread = new AudioDecodeThread(this);
// audio_dec_thread->start();
updateUDP();
uh->start();
}
int MySessionManager::updateUDP(){
qDebug() << "stat:" + QString::number(stat);
if(stat != 0) return false;
auto sock_new = new QUdpSocket(this);
if(!sock_new->bind(cfg->local_port)){
QMessageBox::warning(nullptr, "提示", "绑定新udp端口失败");
return false;
}
closeSock();
m_udpSocket = sock_new;
uh->setSock(sock_new);
//当服务端收到客户端发来的消息时会触发readyRead,信号
//onReciveMsg是我自定义的槽函数,用来处理从客户端接受到的消息
connect(m_udpSocket,&QUdpSocket::readyRead, this, &MySessionManager::recUDP);
init = true;
return true;
}
void MySessionManager::closeSock(){
if(m_udpSocket){
m_udpSocket->disconnect();
if(m_udpSocket->isOpen()) m_udpSocket->close();
delete m_udpSocket;
m_udpSocket = nullptr;
}
}
void MySessionManager::recUDP(){
//定义QByteArray类型变量
QByteArray datagram;
//设置data的大小为等待处理的数据报的大小,这样才能接收到完整的数据
datagram.resize(m_udpSocket->pendingDatagramSize());
// 接收数据报,将其存放到datagram中
QHostAddress addr;
quint16 port;
m_udpSocket->readDatagram(datagram.data(), datagram.size(), &addr, &port);
cfg->target_ip = addr.toString();
cfg->target_port = port;
//处理接收数据
char* data = datagram.data();
MyUDPPacket* pkt = MyUDPPacketBuilder::parse(data, datagram.size());
if(!pkt || (stat != 0 && pkt->sessiongID != sessionID)) return;
// qDebug() << "pkt->type: " + QString::number(pkt->type);
// qDebug() << "stat: " + QString::number(stat);
//会话状态转移
switch (pkt->type) {
case 0:
if(stat == 0){
recCall(pkt);
}
break;
case 1:
if(stat == 1){
callAccepted();
if(cancelBox) cancelBox->hide();
}
break;
case 2:
if(stat == 1){
callReject();
setStat(0);
if(cancelBox) cancelBox->hide();
}
break;
case 3:
if(stat == 3){
hupAccepted();
}
setStat(0);
break;
case 4:
if(stat == 2){
stopSession(true);
}
setStat(0);
break;
case 9:
if(stat == 3){
//方到队列中
cfg->rec_video_queue.push(pkt);
last_time_rec = getCurTime();
}
break;
case 10:
if(stat == 3){
//方到队列中
cfg->rec_audio_queue.push(pkt);
last_time_rec = getCurTime();
}
break;
default:
break;
}
if(pkt->type != 9 && pkt->type != 10){
delete pkt;
}
}
void MySessionManager::hupAccepted()
{
stopSession();
sendControlPkt(cfg->target_ip, cfg->target_port, MyUDPPacketBuilder::buildHupRespPkt);
setWindowTitle("正在发送挂断回应...");
setStat(0);
int res = sendHup(cfg->target_ip, cfg->target_port);
setWindowTitle("未开始会话");
}
void MySessionManager::callAccepted()
{
setStat(3);
setWindowTitle("正在通话...");
CamMicLoader*& camloader = cfg->CML;
if(!camloader){
camloader = new CamMicLoader(cfg->w,cfg->h);
auto v_device = cfg->cur_cam;
auto a_device = cfg->cur_mic;
int tt = camloader->initLoader(v_device, a_device);
camloader->StartCapture();
}
video_enc_thread = new VideoEncodeThread(this);
video_enc_thread->start();
video_dec_thread = new VideoDecodeThread(this);
video_dec_thread->start();
audio_enc_thread = new AudioEncodeThread(this);
audio_enc_thread->start();
audio_dec_thread = new AudioDecodeThread(this);
audio_dec_thread->start();
}
void MySessionManager::callReject()
{
}
void MySessionManager::stopSendUDP()
{
cfg->send_que.clear_use_delete();
}
int MySessionManager::call(const QString sHostAddr,const int sHostPort)
{
if(stat != 0) return -1;
setSessionID(rand() % 255);
int res = sendControlPkt(sHostAddr, sHostPort, MyUDPPacketBuilder::buildCallPkt);
if(res >= 0){
setStat(1);
}
if(cancelBox) delete cancelBox;
cancelBox = new QMessageBox(QMessageBox::Information, "提示", "正在呼叫...",
QMessageBox::Cancel);
cancelBox->setButtonText(QMessageBox::Cancel,QString("取 消"));
connect(cancelBox, SIGNAL(finished(int)), this, SLOT(cancelCall(int)));
cancelBox->show();
return res;
}
void MySessionManager::cancelCall(int v){
if(stat == 1){
setStat(0);
}
}
int MySessionManager::sendControlPkt(const QString sHostAddr,const int sHostPort, MyUDPPacket* (*func)(int sID))
{
if(!init) return -1;
uh->setIP(sHostAddr);
uh->setPort(sHostPort);
int temp_id = getSessionID();
MyUDPPacket* pkt = func(temp_id);
cfg->send_que.push(pkt);
return 0;
}
//挂断回话
int MySessionManager::hupSession(){
if(stat != 3) return -1;
setStat(2);
// stopSession(false);
int res = sendHup(cfg->target_ip, cfg->target_port);
setWindowTitle("正在挂断...");
return res;
}
int MySessionManager::sendHup(const QString sHostAddr,const int sHostPort){
return sendControlPkt(sHostAddr, sHostPort, MyUDPPacketBuilder::buildHupPkt);
}
int MySessionManager::sendAccept(const QString sHostAddr,const int sHostPort)
{
return sendControlPkt(sHostAddr, sHostPort, MyUDPPacketBuilder::buildAcceptPkt);
}
int MySessionManager::sendReject(const QString sHostAddr,const int sHostPort)
{
return sendControlPkt(sHostAddr, sHostPort, MyUDPPacketBuilder::buildRejectPkt);
}
void MySessionManager::recCall(MyUDPPacket* pkt)
{
if(!init || sessionRecord.find(pkt->sessiongID) != sessionRecord.end()) return;
sessionRecord.insert(pkt->sessiongID);
QMessageBox box(QMessageBox::Question, "提示", "是否接受连接",
QMessageBox::Yes|QMessageBox::No);
box.setButtonText(QMessageBox::Yes,QString("接 受"));
box.setButtonText(QMessageBox::No,QString("拒 绝"));
// 弹出对话框
int ret = box.exec();
switch (ret) {
case QMessageBox::Yes:
qDebug() << "接受连接";
setStat(3);
setSessionID(pkt->sessiongID);
sendAccept(cfg->target_ip, cfg->target_port);
callAccepted();
break;
case QMessageBox::No:
default:
qDebug() << "拒绝连接";
sendReject(cfg->target_ip, cfg->target_port);
setStat(0);
break;
}
}
void MySessionManager::stopSession(bool clear_sid){
if(clear_sid) setSessionID(0);
if(getStat() != 0) setStat(0);
CamMicLoader*& camloader = cfg->CML;
if(camloader){
camloader->CloseInputStream();
delete camloader;
camloader = nullptr;
}
if(video_dec_thread){
video_dec_thread->stop();
video_dec_thread = nullptr;
}
if(video_enc_thread){
video_enc_thread->stop();
video_enc_thread = nullptr;
}
if(audio_enc_thread){
audio_enc_thread->stop();
audio_enc_thread = nullptr;
}
if(audio_dec_thread){
audio_dec_thread->stop();
audio_dec_thread = nullptr;
}
cfg->rec_audio_queue.clear_use_delete();
cfg->rec_video_queue.clear_use_delete();
cfg->send_que.clear_use_delete();
last_time_rec = 0;
setWindowTitle("未开始会话");
}
void MySessionManager::setWindowTitle(const char* s){
//跨线程,刷新界面送往UI主线程,此时必须捕获data数组,如果只捕获data分量指针,有可能由于导致跨线程读写问题引起crash
QMetaObject::invokeMethod(this, [this, s]() mutable {
((MainWindow*)parent)->setTitle(s);
((MainWindow*)parent)->updateStartButtion();
});
}