-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVideoWindow.cpp
249 lines (221 loc) · 6.76 KB
/
VideoWindow.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
#include <QMessageBox>
#include <QPixmap>
#include <QTimer>
#include <QDebug>
#include <QTime>
#include <QPainter>
#include <QRectF>
#include "VideoWindow.h"
#include <Params.h>
#include <QLineEdit>
#include <QInputDialog>
#define MAX_NAME_LEN 30
/***********************************
* displays at most 9 videos for now
***********************************/
VideoWindow::VideoWindow(): QDialog(0) {
QSettings settings("UCLA_IRL", "KIWI");
localUsername = settings.value("KiwiLocalUsername", QString("")).toString();
changeUsernameButton = new QPushButton("Change Username");
changeUsernameButton->setDefault(false);
enableButton = new QPushButton("Enable My Video");
enableButton->setDefault(false);
enabled = false;
if (localUsername == "") {
// QTimer::singleShot(50, this, SLOT(changeLocalUsername()));
changeLocalUsername();
}
QVBoxLayout *mainLayout = new QVBoxLayout;
layout = new QGridLayout;
source = new VideoStreamSource();
sink = new VideoStreamSink();
connect(sink->sourceList, SIGNAL(mediaSourceImageDecoded(QString, IplImage *)), this, SLOT(refreshImage(QString, IplImage *)));
connect(sink, SIGNAL(sourceNumChanged(QString, int)), this, SLOT(alterDisplayNumber(QString, int)));
// connect(source, SIGNAL(imageCaptured(QString, const unsigned char *, int)), sink, SLOT(decodeImage(QString, const unsigned char *, int)));
connect(source, SIGNAL(imageCaptured(QString, IplImage *)), this, SLOT(refreshImage(QString, IplImage *)));
connect(changeUsernameButton, SIGNAL(clicked()), this, SLOT(changeLocalUsername()));
connect(enableButton, SIGNAL(clicked()), this, SLOT(toggleEnabled()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(changeUsernameButton);
buttonLayout->addWidget(enableButton);
mainLayout->addLayout(buttonLayout);
mainLayout->addLayout(layout);
setLayout(mainLayout);
alterDisplayNumber("Me", 1);
}
void VideoWindow::changeLocalUsername() {
bool ok;
QString text = QInputDialog::getText(this, tr("Choose Your Username"), tr("Username:"), QLineEdit::Normal, localUsername, &ok);
if (ok && !text.isEmpty()) {
localUsername = text;
QSettings settings("UCLA_IRL", "KIWI");
settings.setValue("KiwiLocalUsername", localUsername);
}
}
void VideoWindow::toggleEnabled() {
if (enabled) {
enabled = false;
source->toggleState();
QNamedFrame *nf = NULL;
nf = displays["Me"];
if (nf != NULL) {
QImage image("default.jpg");
nf->setImage(image);
}
enableButton->setText("Enable My Video");
}
else {
enabled = true;
source->toggleState();
enableButton->setText("Disable My Video");
}
}
VideoWindow::~VideoWindow() {
if (source != NULL)
delete source;
if (sink != NULL)
delete sink;
QHash<QString, QNamedFrame*>::const_iterator it = displays.constBegin();
for(; it != displays.constEnd(); it++) {
QNamedFrame *nf = NULL;
nf = it.value();
if (nf != NULL)
delete nf;
}
}
QImage VideoWindow::IplImage2QImage(const IplImage *iplImage) {
if (iplImage == NULL)
return QImage();
int height = iplImage->height;
int width = iplImage->width;
int displayNum = displays.size();
int scaleHeight;
switch (displayNum) {
case 1: scaleHeight = 480; break;
case 2: scaleHeight = 480; break;
case 3:
case 4:
case 5:
case 6: scaleHeight = 320; break;
default: scaleHeight = 320;
}
if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3) {
// Must be const to achieve faster QImage construction.
const uchar *qImageBuffer = (const uchar *)iplImage->imageData;
// Must specify byesPerLine; otherwise Qt assumes data is 32-bit aligned and each line of data in image is also 32-bit aligned.
// The default interpretation without bytesPerLine will produces 3 vauge images (red, green blue) instead of a clear one
QImage img(qImageBuffer, width, height, iplImage->widthStep, QImage::Format_RGB888);
return img.rgbSwapped().scaledToHeight(scaleHeight);
} else {
qWarning() << QString("Can not convert image, nChannels is %1").arg(iplImage->nChannels);
return QImage();
}
}
void VideoWindow::refreshImage(QString name, IplImage *iplImage) {
if (iplImage == NULL) {
alterDisplayNumber(name, 0);
return;
}
if (!displays.contains(name)) {
alterDisplayNumber(name, 1);
}
QImage image = IplImage2QImage(iplImage);
QNamedFrame *nf = NULL;
nf = displays[name];
if (nf != NULL)
nf->setImage(image);
if (iplImage != NULL)
cvReleaseImage(&iplImage);
}
/**************
* 1 | 2 | 5 |
* 3 | 4 | 6 |
* 7 | 8 | 9 |
*************/
static struct coordinates posTable[9] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}, \
{0, 2}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
void VideoWindow::alterDisplayNumber(QString name, int addOrDel) {
// add
if (addOrDel > 0) {
if (displays.contains(name) ) {
qWarning() << "User " + name + " already exists. No display added.\n";
adjustSize();
return;
}
qWarning() << "User " + name + " added.\n";
QNamedFrame *disp = new QNamedFrame();
disp->setName(name);
displays.insert(name, disp);
int displayNum = displays.size();
disp->setPosition(displayNum);
struct coordinates co = posTable[displayNum - 1];
layout->addWidget(disp, co.row, co.col);
}
// del
else {
if (!displays.contains(name)) {
qWarning() << "User " + name + " does not exist. No display deleted.\n";
adjustSize();
return;
}
else {
QNamedFrame *disp = displays[name];
if (disp != NULL) {
layout->removeWidget(disp);
int pos = disp->getPosition();
delete disp;
displays.remove(name);
QHash<QString, QNamedFrame *>::iterator i = displays.begin();
while(i != displays.end()) {
QNamedFrame *qf = i.value();
if (qf != NULL) {
int qfPos = qf->getPosition();
if (qfPos > pos) {
qfPos--;
qf->setPosition(qfPos);
struct coordinates co = posTable[qfPos - 1];
layout->removeWidget(qf);
layout->addWidget(qf, co.row, co.col);
}
++i;
}
else {
i = displays.erase(i);
}
}
} else {
adjustSize();
qWarning() << "User frame was set to NULL: " + name + " \n";
return;
}
}
}
adjustSize();
}
QNamedFrame::QNamedFrame() {
imageLabel = new QLabel;
layout = new QVBoxLayout;
layout->addWidget(imageLabel);
setLayout(layout);
}
QNamedFrame::~QNamedFrame() {
delete imageLabel;
delete layout;
}
void QNamedFrame::setName(QString name) {
if (name.size() > MAX_NAME_LEN) {
name.truncate(MAX_NAME_LEN);
}
this->name = name;
}
void QNamedFrame::setImage(QImage image) {
QPainter *painter = new QPainter(&image);
painter->setPen(Qt::red);
painter->setFont(QFont("Helvetica", 16));
QRectF br;
painter->drawText(image.rect(), Qt::AlignLeft | Qt::AlignTop, name, &br);
painter->fillRect(br.x(), br.y(), br.width() + 3, br.height(), Qt::lightGray);
painter->drawText(image.rect(), Qt::AlignLeft | Qt::AlignTop, name);
imageLabel->setPixmap(QPixmap::fromImage(image));
delete painter;
}