summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/jinglevoicesession.cpp
blob: daa0d7c09c8e80b40fc6a8b86935cfe35443e55a (plain)
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
/*
    jinglevoicesession.cpp - Define a Jingle voice session.

    Copyright (c) 2006      by MichaĆ«l Larouche     <[email protected]>

    Kopete    (c) 2001-2006 by the Kopete developers <[email protected]>

    *************************************************************************
    *                                                                       *
    * 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; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

// libjingle before everything else to not clash with TQt
#define POSIX
#include "talk/xmpp/constants.h"
#include "talk/base/sigslot.h"
#include "talk/xmpp/jid.h"
#include "talk/xmllite/xmlelement.h"
#include "talk/xmllite/xmlprinter.h"
#include "talk/base/network.h"
#include "talk/p2p/base/session.h"
#include "talk/p2p/base/sessionmanager.h"
#include "talk/p2p/base/helpers.h"
#include "talk/p2p/client/basicportallocator.h"
#include "talk/p2p/client/sessionclient.h"
#include "talk/base/physicalsocketserver.h"
#include "talk/base/thread.h"
#include "talk/base/socketaddress.h"
#include "talk/session/phone/call.h"
#include "talk/session/phone/phonesessionclient.h"
#include "talk/session/sessionsendtask.h"

#include "jinglevoicesession.h"
#include "jinglesessionmanager.h"

// TQt includes
#include <tqdom.h>

// KDE includes
#include <kdebug.h>

// Kopete Jabber includes
#include "jabberaccount.h"
#include "jabberprotocol.h"

#include <xmpp.h>
#include <xmpp_xmlcommon.h>

#define JINGLE_NS "http://www.google.com/session"
#define JINGLE_VOICE_SESSION_NS "http://www.google.com/session/phone"

static bool hasPeer(const JingleVoiceSession::JidList &jidList, const XMPP::Jid &peer)
{
	JingleVoiceSession::JidList::ConstIterator it, itEnd = jidList.constEnd();
	for(it = jidList.constBegin(); it != itEnd; ++it)
	{
		if( (*it).compare(peer, true) )
			return true;
	}

	return false;
}
//BEGIN SlotsProxy
/**
 * This class is used to receive signals from libjingle, 
 * which is are not compatible with TQt signals.
 * So it's a proxy between JingeVoiceSession(qt)<->linjingle class.
 */
class JingleVoiceSession::SlotsProxy : public sigslot::has_slots<> 
{
public:
	SlotsProxy(JingleVoiceSession *parent)
	 : voiceSession(parent)
	{}
	
	void OnCallCreated(cricket::Call* call)
	{
		kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "SlotsProxy: CallCreated." << endl;

		call->SignalSessionState.connect(this, &JingleVoiceSession::SlotsProxy::PhoneSessionStateChanged);
		voiceSession->setCall(call);
	}
		
	void PhoneSessionStateChanged(cricket::Call *call, cricket::Session *session, cricket::Session::State state)
	{
		kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "State changed: " << state << endl;

		XMPP::Jid jid(session->remote_address().c_str());
		
		// Do nothing if the session do not contain a peers.
		//if( !voiceSession->peers().contains(jid) )
		if( !hasPeer(voiceSession->peers(), jid) )
			return;

		if (state == cricket::Session::STATE_INIT) 
		{}
		else if (state == cricket::Session::STATE_SENTINITIATE) 
		{}
		else if (state == cricket::Session::STATE_RECEIVEDINITIATE) 
		{
			voiceSession->setCall(call);
		}
		else if (state == cricket::Session::STATE_SENTACCEPT) 
		{}
		else if (state == cricket::Session::STATE_RECEIVEDACCEPT) 
		{
			emit voiceSession->accepted();
		}
		else if (state == cricket::Session::STATE_SENTMODIFY) 
		{}
		else if (state == cricket::Session::STATE_RECEIVEDMODIFY) 
		{
			//qWarning(TQString("jinglevoicecaller.cpp: RECEIVEDMODIFY not implemented yet (was from %1)").arg(jid.full()));
		}
		else if (state == cricket::Session::STATE_SENTREJECT) 
		{}
		else if (state == cricket::Session::STATE_RECEIVEDREJECT) 
		{
			emit voiceSession->declined();
		}
		else if (state == cricket::Session::STATE_SENTREDIRECT) 
		{}
		else if (state == cricket::Session::STATE_SENTTERMINATE) 
		{
			emit voiceSession->terminated();
		}
		else if (state == cricket::Session::STATE_RECEIVEDTERMINATE) 
		{
			emit voiceSession->terminated();
		}
		else if (state == cricket::Session::STATE_INPROGRESS) 
		{
			emit voiceSession->sessionStarted();
		}
	}

	void OnSendingStanza(cricket::SessionClient*, const buzz::XmlElement *buzzStanza)
	{
		TQString irisStanza(buzzStanza->Str().c_str());
		irisStanza.replace("cli:iq","iq");
		irisStanza.replace(":cli=","=");
	
		voiceSession->sendStanza(irisStanza);
	}
private:
	JingleVoiceSession *voiceSession;
};
//END SlotsProxy

//BEGIN JingleITQResponder
class JingleVoiceSession::JingleITQResponder : public XMPP::Task
{
public:
	JingleITQResponder(XMPP::Task *);
	~JingleITQResponder();

	bool take(const TQDomElement &);
};

/**
 * \class JingleITQResponder
 * \brief A task that responds to jingle candidate queries with an empty reply.
 */
 
JingleVoiceSession::JingleITQResponder::JingleITQResponder(Task *parent) :Task(parent)
{
}

JingleVoiceSession::JingleITQResponder::~JingleITQResponder()
{
}

bool JingleVoiceSession::JingleITQResponder::take(const TQDomElement &e)
{
	if(e.tagName() != "iq")
		return false;
	
	TQDomElement first = e.firstChild().toElement();
	if (!first.isNull() && first.attribute("xmlns") == JINGLE_NS) {
		TQDomElement iq = createIQ(doc(), "result", e.attribute("from"), e.attribute("id"));
		send(iq);
		return true;
	}
	
	return false;
}
//END JingleITQResponder

class JingleVoiceSession::Private
{
public:
	Private()
	 : phoneSessionClient(0L), currentCall(0L)
	{}

	~Private()
	{
		if(currentCall)
			currentCall->Terminate();

		delete currentCall;
	}

	cricket::PhoneSessionClient *phoneSessionClient;
	cricket::Call* currentCall;
};

JingleVoiceSession::JingleVoiceSession(JabberAccount *account, const JidList &peers)
 : JingleSession(account, peers), d(new Private)
{
	slotsProxy = new SlotsProxy(this);

	buzz::Jid buzzJid( account->client()->jid().full().ascii() );

	// Create the phone(voice) session.
	d->phoneSessionClient = new cricket::PhoneSessionClient( buzzJid, account->sessionManager()->cricketSessionManager() );

	d->phoneSessionClient->SignalSendStanza.connect(slotsProxy, &JingleVoiceSession::SlotsProxy::OnSendingStanza);
	d->phoneSessionClient->SignalCallCreate.connect(slotsProxy, &JingleVoiceSession::SlotsProxy::OnCallCreated);

	// Listen to incoming packets
	connect(account->client()->client(), TQT_SIGNAL(xmlIncoming(const TQString&)), this, TQT_SLOT(receiveStanza(const TQString&)));

	new JingleITQResponder(account->client()->rootTask());
}

JingleVoiceSession::~JingleVoiceSession()
{
	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << endl;
	delete slotsProxy;
	delete d;
}

TQString JingleVoiceSession::sessionType()
{
	return TQString(JINGLE_VOICE_SESSION_NS);
}

void JingleVoiceSession::start()
{
	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Starting a voice session..." << endl;
	d->currentCall = d->phoneSessionClient->CreateCall();

	TQString firstPeerJid = ((XMPP::Jid)peers().first()).full();
	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "With peer: " << firstPeerJid << endl;
	d->currentCall->InitiateSession( buzz::Jid(firstPeerJid.ascii()) );

	d->phoneSessionClient->SetFocus(d->currentCall);
}

void JingleVoiceSession::accept()
{	
	if(d->currentCall)
	{
		kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Accepting a voice session..." << endl;

		d->currentCall->AcceptSession(d->currentCall->sessions()[0]);
		d->phoneSessionClient->SetFocus(d->currentCall);
	}
}

void JingleVoiceSession::decline()
{
	if(d->currentCall)
	{
		d->currentCall->RejectSession(d->currentCall->sessions()[0]);
	}
}

void JingleVoiceSession::terminate()
{
	if(d->currentCall)
	{
		d->currentCall->Terminate();
	}
}

void JingleVoiceSession::setCall(cricket::Call *call)
{
	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Updating cricket::call object." << endl;
	d->currentCall = call;
	d->phoneSessionClient->SetFocus(d->currentCall);
}

void JingleVoiceSession::receiveStanza(const TQString &stanza)
{
	TQDomDocument doc;
	doc.setContent(stanza);

	// Check if it is offline presence from an open chat
	if( doc.documentElement().tagName() == "presence" ) 
	{
		XMPP::Jid from = XMPP::Jid(doc.documentElement().attribute("from"));
		TQString type = doc.documentElement().attribute("type");
		if( type == "unavailable" && hasPeer(peers(), from) ) 
		{
			//qDebug("JingleVoiceCaller: User went offline without closing a call.");
			kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "User went offline without closing a call." << endl;
			emit terminated();
		}
		return;
	}
	
	// Check if the packet is destined for libjingle.
	// We could use Session::IsClientStanza to check this, but this one crashes
	// for some reason.
	TQDomNode node = doc.documentElement().firstChild();
	bool ok = false;
	while( !node.isNull() && !ok ) 
	{
		TQDomElement element = node.toElement();
		if( !element.isNull() && element.attribute("xmlns") == JINGLE_NS) 
		{
			ok = true;
		}
		node = node.nextSibling();
	}
	
	// Spread the word
	if( ok )
	{
		kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Handing down buzz::stanza" << endl;
		buzz::XmlElement *e = buzz::XmlElement::ForStr(stanza.ascii());
		d->phoneSessionClient->OnIncomingStanza(e);
	}
}

#include "jinglevoicesession.moc"