-
Notifications
You must be signed in to change notification settings - Fork 1k
/
inroom.cpp
388 lines (334 loc) · 12.3 KB
/
inroom.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "agoraobject.h"
#include "inroom.h"
#include "ui_inroom.h"
#include "agorawindowmanager.h"
#ifdef Q_OS_WIN32
#include <Windows.h>
#endif
InRoom::InRoom(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::InRoom)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint
| Qt::WindowMinMaxButtonsHint );
CAgoraObject* pObject = CAgoraObject::getInstance();
AgoraWindowManager::getInstance()->insertItem(qWndType_InRoom,this);
connect(pObject,SIGNAL(sender_videoStopped()),
this,SLOT(receive_videoStopped()));
connect(pObject,SIGNAL(sender_joinedChannelSuccess(const QString&,unsigned int ,int)),
this,SLOT(receive_joinedChannelSuccess(const QString&,unsigned int,int)));
connect(pObject,SIGNAL(sender_userJoined(unsigned int,int)),
this,SLOT(receive_userJoined(unsigned int,int)));
connect(pObject,SIGNAL(sender_userOffline(unsigned int,USER_OFFLINE_REASON_TYPE)),
this,SLOT(receive_userOffline(unsigned int,USER_OFFLINE_REASON_TYPE)));
connect(pObject,SIGNAL(sender_firstLocalVideoFrame(int,int,int)),
this,SLOT(receive_firstLocalVideoFrame(int,int,int)));
connect(pObject,SIGNAL(sender_firstRemoteVideoDecoded(unsigned int,int,int,int)),
this,SLOT(receive_firstRemoteVideoDecoded(unsigned int,int,int,int)));
connect(pObject,SIGNAL(sender_firstRemoteVideoFrameDrawn(unsigned int,int,int,int)),
this,SLOT(receive_firstRemoteVideoFrameDrawn(unsigned int,int,int,int)));
connect(pObject,SIGNAL(sender_localVideoStats(LocalVideoStats)),
this,SLOT(receive_localVideoStats(LocalVideoStats)));
connect(pObject,SIGNAL(sender_remoteVideoStats(RemoteVideoStats)),
this,SLOT(receive_remoteVideoStats(RemoteVideoStats)));
connect(pObject,SIGNAL(sender_rtcStats(RtcStats)),
this,SLOT(receive_rtcStats(RtcStats)));
ui->widget_l->lower();
ui->widget_r1->raise();
ui->widget_r2->raise();
ui->widget_r3->raise();
ui->widget_r1->setWindowOpacity(1);//opaque.
ui->widget_r2->setWindowOpacity(1);
ui->widget_r3->setWindowOpacity(1);
ui->widget_l->installEventFilter(this);
ui->widget_r1->installEventFilter(this);
ui->widget_r2->installEventFilter(this);
ui->widget_r3->installEventFilter(this);
m_timer_fps = new QTimer(this);
connect(m_timer_fps,SIGNAL(timeout()),this,SLOT(receive_timer_pfs()));
}
InRoom::~InRoom()
{
delete ui;
}
void InRoom::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton) {
m_mousePosition = e->pos();
if(m_mousePosition.x() < lnTitleWidth &&
m_mousePosition.y() < lnTitleHeight)
m_bMousePressed = true;
}
QApplication::sendEvent(m_uper.get(),e);
}
void InRoom::mouseMoveEvent(QMouseEvent *e)
{
if(m_bMousePressed == true) {
QPoint movePos = e->globalPos() - m_mousePosition;
move(movePos);
e->accept();
}
QApplication::sendEvent(m_uper.get(),e);
}
void InRoom::mouseReleaseEvent(QMouseEvent *e)
{
m_bMousePressed = false;
QApplication::sendEvent(m_uper.get(),e);
}
void InRoom::focusInEvent(QFocusEvent *event)
{
QApplication::sendEvent(m_uper.get(),event);
}
void InRoom::focusOutEvent(QFocusEvent *event)
{
QApplication::sendEvent(m_uper.get(),event);
}
void InRoom::joinchannel(const QString& qsChannelId,const QString& qsChannelKey,uint uid)
{
this->show();
CAgoraObject::getInstance()->joinChannel((qsChannelKey.length() > 0 ? qsChannelKey:NULL),qsChannelId,uid);
m_uper.reset(new EnterRoom);
m_uper->joinchannel(qsChannelId,uid);
}
void InRoom::leavechannel()
{
m_timer_fps->stop();
{
std::lock_guard<std::mutex> autoLock(m_vsMutex);
m_qivs.clear();
delete m_timer_fps;
m_bigUid = 0;
m_uid = 0;
}
CAgoraObject::getInstance()->leaveChannel();
this->hide();
QMainWindow* pLastWnd = AgoraWindowManager::getInstance()->getQWnd(qWndType_OpenVideoCall);
pLastWnd->show();
}
void InRoom::receive_videoStopped()
{
}
void InRoom::receive_joinedChannelSuccess(const QString &qsChannel, unsigned int uid, int elapsed)
{
{
std::lock_guard<std::mutex> autoLock(m_vsMutex);
m_uid = uid;
if(!m_qivs.contains(uid)) {
m_qivs[uid] = Video_Stats(uid);
}
}
adjustPos();
QString qsChannelInfo;
m_qsChannel = qsChannel;
qsChannelInfo.sprintf("%s %u",qsChannel.toStdString().c_str(),uid);
m_uper->setChannelName(qsChannelInfo);
m_bigUid = uid;
m_timer_fps->start(2000);
}
void InRoom::receive_userJoined(uid_t uid, int elapsed)
{
{
std::lock_guard<std::mutex> autoLock(m_vsMutex);
if(!m_qivs.contains(uid)) {
m_qivs[uid] = Video_Stats(uid);
}
}
adjustPos();
}
void InRoom::receive_userOffline(uid_t uid, USER_OFFLINE_REASON_TYPE reason)
{
{
std::lock_guard<std::mutex> autoLock(m_vsMutex);
if(m_qivs.contains(uid)) {
auto it = m_qivs.find(uid);
m_qivs.erase(it);
}
}
adjustPos();
}
void InRoom::receive_firstLocalVideoFrame(int width, int height, int elapsed)
{
}
void InRoom::receive_firstRemoteVideoDecoded(uid_t uid, int width, int height, int elapsed)
{
}
void InRoom::receive_firstRemoteVideoFrameDrawn(uid_t uid, int width, int height, int elapsed)
{
}
void InRoom::receive_localVideoStats(const LocalVideoStats &stats)
{
{
std::lock_guard<std::mutex> autoLock(m_vsMutex);
if(m_qivs.contains(m_uid)) {
m_qivs[m_uid].nWidth = stats.encodedFrameWidth;
m_qivs[m_uid].nHeight = stats.encodedFrameHeight;
m_qivs[m_uid].nFps = stats.sentFrameRate;
m_qivs[m_uid].nBitrate = stats.sentBitrate;
}
}
}
void InRoom::receive_remoteVideoStats(const RemoteVideoStats &stats)
{
{
std::lock_guard<std::mutex> autoLock(m_vsMutex);
if(m_qivs.contains(stats.uid)) {
m_qivs[stats.uid].nLastmileDelay = stats.delay;
m_qivs[stats.uid].nWidth = stats.width;
m_qivs[stats.uid].nHeight = stats.height;
m_qivs[stats.uid].nFps = stats.rendererOutputFrameRate;
m_qivs[stats.uid].nBitrate = stats.receivedBitrate;
}
}
}
void InRoom::receive_rtcStats(const RtcStats &stats)
{
m_nlastmileDelay = stats.lastmileDelay;
std::lock_guard<std::mutex> autoLock(m_vsMutex);
if(m_qivs.contains(m_uid))
m_qivs[m_uid].nLastmileDelay = stats.lastmileDelay;
}
void InRoom::receive_timer_pfs()
{
std::lock_guard<std::mutex> autoLock(m_vsMutex);
if(m_qivs.contains(m_bigUid)) {
QString qsFps;
auto it = m_qivs.value(m_bigUid);
qsFps.sprintf("SD-RTN: %dms. Video: %dfps %dx%d",it.nLastmileDelay,it.nFps,it.nWidth,it.nHeight);
m_uper->setParam(qsFps);
}
}
void InRoom::adjustPos()
{
ui->widget_l->setVisible(false);
ui->widget_r1->setVisible(false);
ui->widget_r2->setVisible(false);
ui->widget_r3->setVisible(false);
std::lock_guard<std::mutex> autoLock(m_vsMutex);
auto it = m_qivs.begin();
int nIndex = 0;
while(m_qivs.end() != it) {
if(it->uid == m_uid) {
m_bigUid == m_uid;
it->pWidget = ui->widget_l;
it->nIndex = 0;
CAgoraObject::getInstance()->LocalVideoPreview((HWND)(it->pWidget->winId()),
TRUE,
(it->uid == m_bigUid) ? RENDER_MODE_FIT : RENDER_MODE_HIDDEN);
ui->widget_l->setVisible(true);
ui->widget_l->setGeometry(0,0,1366,768);
ui->widget_l->lower();
QString qsChannelInfo;
qsChannelInfo.sprintf("%s %u",m_qsChannel.toStdString().c_str(),m_uid);
m_uper->setChannelName(qsChannelInfo);
}
else {
nIndex++;
if(nIndex == 1) {
it->pWidget = ui->widget_r1;
it->nIndex = 1;
CAgoraObject::getInstance()->RemoteVideoRender(it->uid,
(HWND)(it->pWidget->winId()),
(it->uid == m_bigUid) ? RENDER_MODE_FIT:RENDER_MODE_HIDDEN);
m_uper->setR1(it->uid);
ui->widget_r1->setVisible(true);
ui->widget_r1->setGeometry(30,94,200,150);
ui->widget_r1->raise();
}
if(nIndex == 2) {
it->pWidget = ui->widget_r2;
it->nIndex = 2;
CAgoraObject::getInstance()->RemoteVideoRender(it->uid,
(HWND)(it->pWidget->winId()),
(it->uid == m_bigUid) ? RENDER_MODE_FIT:RENDER_MODE_HIDDEN);
m_uper->setR2(it->uid);
ui->widget_r2->setVisible(true);
ui->widget_r2->setGeometry(30,254,200,150);
ui->widget_r2->raise();
}
if(nIndex == 3) {
it->pWidget = ui->widget_r3;
it->nIndex = 3;
CAgoraObject::getInstance()->RemoteVideoRender(it->uid,
(HWND)(it->pWidget->winId()),
(it->uid == m_bigUid) ? RENDER_MODE_FIT:RENDER_MODE_HIDDEN);
m_uper->setR3(it->uid);
ui->widget_r3->setVisible(true);
ui->widget_r3->setGeometry(30,414,200,150);
ui->widget_r3->raise();
}
}
it++;
}
m_uper->setAllRAtt(nIndex);
}
bool InRoom::eventFilter(QObject *watched,QEvent *event)
{
bool bFind = false;
int nIndex = 0;
Video_Stats vsr,vsbig;
if(watched==ui->widget_r1 && event->type() == QEvent::MouseButtonDblClick) {
qDebug("DBClick r1");
std::lock_guard<std::mutex> autoLock(m_vsMutex);
uid_t uid = m_uper->getR1();
if(m_qivs.contains(uid) && m_qivs.contains(m_bigUid)) {
vsr = m_qivs[uid];
vsbig = m_qivs[m_bigUid];
bFind = ( vsr.uid == m_bigUid ) ? false : true;
nIndex = vsr.nIndex;
}
}
else if(watched == ui->widget_r2 && event->type() == QEvent::MouseButtonDblClick) {
qDebug("DBClick r2");
std::lock_guard<std::mutex> autoLock(m_vsMutex);
uid_t uid = m_uper->getR2();
if(m_qivs.contains(uid) && m_qivs.contains(m_bigUid)) {
vsr = m_qivs[uid];
vsbig = m_qivs[m_bigUid];
bFind = (vsr.uid == m_bigUid) ? false : true;
nIndex = vsr.nIndex;
}
}
else if(watched == ui->widget_r3 && event->type() == QEvent::MouseButtonDblClick) {
qDebug("DBClick r3");
std::lock_guard<std::mutex> autoLock(m_vsMutex);
uid_t uid = m_uper->getR3();
if(m_qivs.contains(uid) && m_qivs.contains(m_bigUid)) {
vsr = m_qivs[uid];
vsbig = m_qivs[m_bigUid];
bFind = (vsr.uid == m_bigUid) ? false : true;
nIndex = vsr.nIndex;
}
}
else if(watched == ui->widget_l && event->type() == QEvent::MouseButtonDblClick) {
qDebug("DBClick local");
std::lock_guard<std::mutex> autoLock(m_vsMutex);
uid_t uid = m_uid;
if(m_qivs.contains(uid) && m_qivs.contains(m_bigUid)) {
vsr = m_qivs[uid];
vsbig = m_qivs[m_bigUid];
bFind = (vsr.uid == m_bigUid) ? false : true;
nIndex = vsr.nIndex;
}
}
if(bFind) {
std::lock_guard<std::mutex> autolock(m_vsMutex);
QRect qrr,qrbig;
if(vsr.pWidget)
qrr = vsr.pWidget->geometry();
if(vsbig.pWidget)
qrbig = vsbig.pWidget->geometry();
vsr.pWidget->setGeometry(qrbig);
vsbig.pWidget->setGeometry(qrr);
vsr.pWidget->lower();
vsbig.pWidget->raise();
m_qivs[vsr.uid].nIndex = 0;
m_qivs[vsbig.uid].nIndex = nIndex;
QString qsSrc,qsDest;
qsSrc.sprintf("%s %u",m_qsChannel.toStdString().c_str(),vsr.uid);
qsDest.sprintf(" uid:%u",vsbig.uid);
m_uper->switchUidText(qsSrc,qsDest,nIndex);
m_bigUid = vsr.uid;
}
return QWidget::eventFilter(watched,event);
}