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
|
// SPDX-FileCopyrightText: 2010 Jeremy Lainé <jeremy.laine@m4x.org>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppCallManager.h"
#include "QXmppCall.h"
#include "QXmppCallManager_p.h"
#include "QXmppCall_p.h"
#include "QXmppClient.h"
#include "QXmppConstants_p.h"
#include "QXmppJingleIq.h"
#include "QXmppStun.h"
#include "QXmppUtils.h"
#include <gst/gst.h>
#include <QDomElement>
#include <QTimer>
/// \cond
QXmppCallManagerPrivate::QXmppCallManagerPrivate(QXmppCallManager *qq)
: turnPort(0),
q(qq)
{
// Initialize GStreamer
gst_init(nullptr, nullptr);
}
QXmppCall *QXmppCallManagerPrivate::findCall(const QString &sid) const
{
for (auto *call : calls) {
if (call->sid() == sid) {
return call;
}
}
return nullptr;
}
QXmppCall *QXmppCallManagerPrivate::findCall(const QString &sid, QXmppCall::Direction direction) const
{
for (auto *call : calls) {
if (call->sid() == sid && call->direction() == direction) {
return call;
}
}
return nullptr;
}
/// \endcond
///
/// Constructs a QXmppCallManager object to handle incoming and outgoing
/// Voice-Over-IP calls.
///
QXmppCallManager::QXmppCallManager()
{
d = new QXmppCallManagerPrivate(this);
}
///
/// Destroys the QXmppCallManager object.
///
QXmppCallManager::~QXmppCallManager()
{
delete d;
}
/// \cond
QStringList QXmppCallManager::discoveryFeatures() const
{
return {
ns_jingle, // XEP-0166 : Jingle
ns_jingle_rtp, // XEP-0167 : Jingle RTP Sessions
ns_jingle_rtp_audio,
ns_jingle_rtp_video,
ns_jingle_ice_udp, // XEP-0176 : Jingle ICE-UDP Transport Method
};
}
bool QXmppCallManager::handleStanza(const QDomElement &element)
{
if (element.tagName() == "iq") {
// XEP-0166: Jingle
if (QXmppJingleIq::isJingleIq(element)) {
QXmppJingleIq jingleIq;
jingleIq.parse(element);
_q_jingleIqReceived(jingleIq);
return true;
}
}
return false;
}
void QXmppCallManager::setClient(QXmppClient *client)
{
QXmppClientExtension::setClient(client);
connect(client, &QXmppClient::disconnected,
this, &QXmppCallManager::_q_disconnected);
connect(client, &QXmppClient::iqReceived,
this, &QXmppCallManager::_q_iqReceived);
connect(client, &QXmppClient::presenceReceived,
this, &QXmppCallManager::_q_presenceReceived);
}
/// \endcond
///
/// Initiates a new outgoing call to the specified recipient.
///
/// \param jid
///
QXmppCall *QXmppCallManager::call(const QString &jid)
{
if (jid.isEmpty()) {
warning("Refusing to call an empty jid");
return nullptr;
}
if (jid == client()->configuration().jid()) {
warning("Refusing to call self");
return nullptr;
}
QXmppCall *call = new QXmppCall(jid, QXmppCall::OutgoingDirection, this);
QXmppCallStream *stream = call->d->createStream("audio", "initiator", "microphone");
call->d->streams << stream;
call->d->sid = QXmppUtils::generateStanzaHash();
// register call
d->calls << call;
connect(call, &QObject::destroyed,
this, &QXmppCallManager::_q_callDestroyed);
Q_EMIT callStarted(call);
call->d->sendInvite();
return call;
}
///
/// Sets multiple STUN servers to use to determine server-reflexive addresses
/// and ports.
///
/// \note This may only be called prior to calling bind().
///
/// \param servers List of the STUN servers.
///
/// \since QXmpp 1.3
///
void QXmppCallManager::setStunServers(const QList<QPair<QHostAddress, quint16>> &servers)
{
d->stunServers = servers;
}
///
/// Sets a single STUN server to use to determine server-reflexive addresses
/// and ports.
///
/// \note This may only be called prior to calling bind().
///
/// \param host The address of the STUN server.
/// \param port The port of the STUN server.
///
void QXmppCallManager::setStunServer(const QHostAddress &host, quint16 port)
{
d->stunServers.clear();
d->stunServers.push_back(qMakePair(host, port));
}
///
/// Sets the TURN server to use to relay packets in double-NAT configurations.
///
/// \param host The address of the TURN server.
/// \param port The port of the TURN server.
///
void QXmppCallManager::setTurnServer(const QHostAddress &host, quint16 port)
{
d->turnHost = host;
d->turnPort = port;
}
///
/// Sets the \a user used for authentication with the TURN server.
///
/// \param user
///
void QXmppCallManager::setTurnUser(const QString &user)
{
d->turnUser = user;
}
///
/// Sets the \a password used for authentication with the TURN server.
///
/// \param password
///
void QXmppCallManager::setTurnPassword(const QString &password)
{
d->turnPassword = password;
}
///
/// Handles call destruction.
///
void QXmppCallManager::_q_callDestroyed(QObject *object)
{
d->calls.removeAll(static_cast<QXmppCall *>(object));
}
///
/// Handles disconnection from server.
///
void QXmppCallManager::_q_disconnected()
{
for (auto *call : std::as_const(d->calls)) {
call->d->terminate(QXmppJingleIq::Reason::Gone);
}
}
///
/// Handles acknowledgements.
///
void QXmppCallManager::_q_iqReceived(const QXmppIq &ack)
{
if (ack.type() != QXmppIq::Result) {
return;
}
// find request
for (auto *call : std::as_const(d->calls)) {
call->d->handleAck(ack);
}
}
///
/// Handles a Jingle IQ.
///
void QXmppCallManager::_q_jingleIqReceived(const QXmppJingleIq &iq)
{
if (iq.type() != QXmppIq::Set) {
return;
}
if (iq.action() == QXmppJingleIq::SessionInitiate) {
// build call
QXmppCall *call = new QXmppCall(iq.from(), QXmppCall::IncomingDirection, this);
call->d->sid = iq.sid();
const auto content = iq.contents().isEmpty() ? QXmppJingleIq::Content()
: iq.contents().constFirst();
auto *stream = call->d->createStream(content.descriptionMedia(), content.creator(), content.name());
if (!stream) {
// FIXME: delete call here?
return;
}
call->d->streams << stream;
// send ack
call->d->sendAck(iq);
// check content description and transport
if (!call->d->handleDescription(stream, content) ||
!call->d->handleTransport(stream, content)) {
// terminate call
call->d->terminate(QXmppJingleIq::Reason::FailedApplication);
call->terminated();
delete call;
return;
}
// register call
d->calls << call;
connect(call, &QObject::destroyed,
this, &QXmppCallManager::_q_callDestroyed);
// send ringing indication
QXmppJingleIq ringing;
ringing.setTo(call->jid());
ringing.setType(QXmppIq::Set);
ringing.setSid(call->sid());
ringing.setRtpSessionState(QXmppJingleIq::RtpSessionStateRinging());
call->d->sendRequest(ringing);
// notify user
Q_EMIT callReceived(call);
return;
} else {
// for all other requests, require a valid call
QXmppCall *call = d->findCall(iq.sid());
if (!call) {
warning(QString("Remote party %1 sent a request for an unknown call %2").arg(iq.from(), iq.sid()));
return;
}
call->d->handleRequest(iq);
}
}
///
/// Handles a presence.
///
void QXmppCallManager::_q_presenceReceived(const QXmppPresence &presence)
{
if (presence.type() != QXmppPresence::Unavailable) {
return;
}
for (auto *call : std::as_const(d->calls)) {
if (presence.from() == call->jid()) {
// the remote party has gone away, terminate call
call->d->terminate(QXmppJingleIq::Reason::Gone);
}
}
}
|