summaryrefslogtreecommitdiffstats
path: root/pica-client/callwindow.cpp
blob: 81959e3173029dff1696c65a748e873b18bd6536 (plain) (blame)
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
/*
	(c) Copyright  2012 - 2020 Anton Sviridenko
	https://picapica.im

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, version 3.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "callwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCloseEvent>
#include <QDebug>

CallWindow::CallWindow(QByteArray peer_id, bool incoming)
	: m_peer_id(peer_id), is_incoming(incoming)
{
	setAttribute(Qt::WA_DeleteOnClose);

	QVBoxLayout *lv = new QVBoxLayout(this);
	QHBoxLayout *lh = new QHBoxLayout();

	pbAccept = new QPushButton(tr("Accept"), this);
	pbCall = new QPushButton(tr("Call"), this);
	pbHang = new QPushButton(tr("Hang Up"), this);
	lbTimer = new QLabel(this);
	lbTimer->setAlignment(Qt::AlignCenter);
	lbTimer->hide();
	lbTransport = new QLabel(this);
	lbTransport->setAlignment(Qt::AlignCenter);
	lbTransport->hide();
	lbVideo = new QLabel(this);
	lbVideo->setAlignment(Qt::AlignCenter);
	lbVideo->setMinimumSize(320, 240);
	lbVideo->hide();
#ifdef HAVE_VAAPI
	videoGpu = VaapiRenderWidget::create(this);
	videoGpu->widget()->setMinimumSize(320, 240);
	videoGpu->widget()->hide();
	connect(videoGpu->widget(), SIGNAL(renderingFailed(QString)), this, SLOT(gpu_rendering_failed(QString)));
#endif
	callTimer = new QTimer(this);
	callElapsedSeconds = 0;

	lv->addWidget(lbTimer);
	lv->addWidget(lbTransport);
	lv->addWidget(lbVideo);
#ifdef HAVE_VAAPI
	lv->addWidget(videoGpu->widget());
#endif

	lh->addWidget(pbAccept, Qt::AlignLeft);
	lh->addWidget(pbCall, Qt::AlignLeft);
	lh->addWidget(pbHang, Qt::AlignRight);
	lv->addLayout(lh);
	setLayout(lv);

	connect(pbCall, SIGNAL(clicked()), this, SLOT(call()));
	connect(pbHang, SIGNAL(clicked()), this, SLOT(hang()));
	connect(pbAccept, SIGNAL(clicked()), this, SLOT(accept()));
	connect(callTimer, SIGNAL(timeout()), this, SLOT(update_call_timer()));

	if (is_incoming)
	{
		pbCall->hide();
	}
	else
	{
		pbAccept->hide();
	}
}

void CallWindow::call_started()
{
	pbCall->hide();
	pbAccept->hide();

	callElapsedSeconds = 0;
	lbTimer->setText("00:00");
	lbTimer->show();
	callTimer->start(1000);
}

void CallWindow::call_ended()
{
	callTimer->stop();
	pbHang->setDisabled(true);
	lbTransport->hide();
}

void CallWindow::setMediaTransport(bool direct_udp, QString ciphersuitename)
{
	if (direct_udp)
		lbTransport->setText(tr("🔐 media: direct UDP, %1").arg(ciphersuitename));
	else
		lbTransport->setText(tr("media: through the chat connection"));

	lbTransport->show();
}

void CallWindow::showRemoteFrame(QImage frame)
{
	if (frame.isNull())
		return;

	if (lbVideo->isHidden())
		lbVideo->show();

	lbVideo->setPixmap(QPixmap::fromImage(frame).scaled(lbVideo->size(),
	                                                    Qt::KeepAspectRatio,
	                                                    Qt::SmoothTransformation));
}

#ifdef HAVE_VAAPI
void CallWindow::showRemoteHwFrame(AVFramePtr frame)
{
	if (!frame)
		return;

	// The widget has given up on drawing - ask again for frames the label can
	// show, and keep the video area with it.
	if (!videoGpu->isWorking())
	{
		emit video_rendering_failed();
		return;
	}

	// These frames hold a GPU surface rather than pixels, so the label cannot
	// show them - the GL widget takes over the video area instead.
	if (videoGpu->widget()->isHidden())
	{
		lbVideo->hide();
		videoGpu->widget()->show();
	}

	videoGpu->setFrame(frame);
}

void CallWindow::gpu_rendering_failed(QString message)
{
	// Nothing will ever appear in the GL widget now, so hand the video area
	// back to the label and ask for frames it can actually show. Without this
	// the rest of the call would be spent looking at an empty black rectangle.
	qWarning() << message;

	videoGpu->widget()->hide();
	lbVideo->show();

	emit video_rendering_failed();
}
#endif

void CallWindow::update_call_timer()
{
	int hours, minutes, seconds;
	QString text;

	callElapsedSeconds++;

	hours = callElapsedSeconds / 3600;
	minutes = (callElapsedSeconds % 3600) / 60;
	seconds = callElapsedSeconds % 60;

	if (hours > 0)
		text = QString("%1:%2:%3").
		    arg(hours, 2, 10, QChar('0')).
		    arg(minutes, 2, 10, QChar('0')).
		    arg(seconds, 2, 10, QChar('0'));
	else
		text = QString("%1:%2").
		    arg(minutes, 2, 10, QChar('0')).
		    arg(seconds, 2, 10, QChar('0'));

	lbTimer->setText(text);
}

void CallWindow::call()
{
	emit start_call_pressed();
}

void CallWindow::hang()
{
	emit hang_call_pressed();
}

void CallWindow::accept()
{
	emit accept_call_pressed();
}

void CallWindow::closeEvent(QCloseEvent *e)
{
	emit callwindow_closed(this);

	e->accept();
}